Annotation of parser3/src/main/execute.C, revision 1.295.2.27.2.13
1.117 paf 1: /** @file
1.118 paf 2: Parser: executor part of request class.
3:
1.295.2.4 paf 4: Copyright (c) 2001-2003 ArtLebedev Group (http://www.artlebedev.com)
1.218 paf 5: Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.243 paf 6: */
1.118 paf 7:
1.295.2.27.2. 3(paf 8:3): static const char* IDENT_EXECUTE_C="$Date: 2003/04/02 09:32:11 $";
1.1 paf 9:
1.152 paf 10: #include "pa_opcode.h"
1.8 paf 11: #include "pa_array.h"
1.11 paf 12: #include "pa_request.h"
1.15 paf 13: #include "pa_vstring.h"
1.22 paf 14: #include "pa_vhash.h"
1.167 parser 15: #include "pa_vvoid.h"
1.145 paf 16: #include "pa_vcode_frame.h"
17: #include "pa_vmethod_frame.h"
1.38 paf 18: #include "pa_vobject.h"
1.49 paf 19: #include "pa_vdouble.h"
1.54 paf 20: #include "pa_vbool.h"
1.94 paf 21: #include "pa_vtable.h"
1.132 paf 22: #include "pa_vfile.h"
1.144 paf 23: #include "pa_vimage.h"
1.194 parser 24: #include "pa_wwrapper.h"
1.1 paf 25:
1.295.2.27.2. 3(paf 26:3): //#define DEBUG_EXECUTE
27:3): #define LATER_STACK_TRACE
1.157 parser 28:
29: #ifdef DEBUG_EXECUTE
1.1 paf 30: char *opcode_name[]={
1.49 paf 31: // literals
1.109 paf 32: "VALUE", "CURLY_CODE__STORE_PARAM", "EXPR_CODE__STORE_PARAM",
1.209 paf 33: "NESTED_CODE",
1.49 paf 34:
35: // actions
1.187 parser 36: "WITH_ROOT", "WITH_SELF", "WITH_READ", "WITH_WRITE",
1.66 paf 37: "GET_CLASS",
1.182 parser 38: "CONSTRUCT_VALUE", "CONSTRUCT_EXPR", "CURLY_CODE__CONSTRUCT",
1.108 paf 39: "WRITE_VALUE", "WRITE_EXPR_RESULT", "STRING__WRITE",
1.284 paf 40: "GET_ELEMENT_OR_OPERATOR", "GET_ELEMENT", "GET_ELEMENT__WRITE",
1.232 paf 41: "OBJECT_POOL", "STRING_POOL",
1.1 paf 42: "STORE_PARAM",
1.232 paf 43: "PREPARE_TO_CONSTRUCT_OBJECT", "PREPARE_TO_EXPRESSION",
44: "CALL", "CALL__WRITE",
1.49 paf 45:
46: // expression ops: unary
1.148 paf 47: "NEG", "INV", "NOT", "DEF", "IN", "FEXISTS", "DEXISTS",
1.49 paf 48: // expression ops: binary
1.201 paf 49: "SUB", "ADD", "MUL", "DIV", "MOD", "INTDIV",
1.275 paf 50: "BIN_SL", "BIN_SR",
1.56 paf 51: "BIN_AND", "BIN_OR", "BIN_XOR",
52: "LOG_AND", "LOG_OR", "LOG_XOR",
1.49 paf 53: "NUM_LT", "NUM_GT", "NUM_LE", "NUM_GE", "NUM_EQ", "NUM_NE",
1.103 paf 54: "STR_LT", "STR_GT", "STR_LE", "STR_GE", "STR_EQ", "STR_NE",
55: "IS"
1.1 paf 56: };
57:
1.295.2.11 paf 58: void va_debug_printf(SAPI_Info& sapi_info, const char* fmt,va_list args) {
1.127 paf 59: char buf[MAX_STRING];
60: vsnprintf(buf, MAX_STRING, fmt, args);
1.295.2.11 paf 61: SAPI::log(sapi_info, "%s", buf);
1.107 paf 62: }
63:
1.295.2.11 paf 64: void debug_printf(SAPI_Info& sapi_info, const char* fmt, ...) {
1.107 paf 65: va_list args;
1.295.2.11 paf 66: va_start(args, fmt);
67: va_debug_printf(sapi_info, fmt, args);
1.107 paf 68: va_end(args);
1.105 paf 69: }
70:
1.295.2.27.2. (paf 71:): void debug_dump(SAPI_Info& sapi_info, int level, ArrayOperation& ops) {
1.295.2.11 paf 72: Array_iterator<Operation> i(ops);
1.190 parser 73: while(i.has_next()) {
1.295.2.27.2. (paf 74:): Operation op=i.next();
1.1 paf 75:
1.86 paf 76: if(op.code==OP_VALUE || op.code==OP_STRING__WRITE) {
1.295.2.27.2. 3(paf 77:3): Value& value=*i.next().value;
1.295.2.11 paf 78: debug_printf(sapi_info,
1.133 paf 79: "%*s%s"
80: " \"%s\" %s",
81: level*4, "", opcode_name[op.code],
1.295.2.27.2. 2(paf 82:3): value.get_string()->cstr(), value.type());
1.133 paf 83: continue;
1.15 paf 84: }
1.295.2.11 paf 85: debug_printf(sapi_info, "%*s%s", level*4, "", opcode_name[op.code]);
1.1 paf 86:
1.184 parser 87: switch(op.code) {
88: case OP_CURLY_CODE__STORE_PARAM:
89: case OP_EXPR_CODE__STORE_PARAM:
90: case OP_CURLY_CODE__CONSTRUCT:
1.209 paf 91: case OP_NESTED_CODE:
1.226 paf 92: case OP_OBJECT_POOL:
93: case OP_STRING_POOL:
1.237 paf 94: case OP_CALL:
1.295.2.12 paf 95: case OP_CALL__WRITE:
1.295.2.27.2. (paf 96:): if(ArrayOperation* local_ops=i.next().ops)
97:): debug_dump(sapi_info, level+1, *local_ops);
1.1 paf 98: }
99: }
100: }
1.157 parser 101: #endif
1.1 paf 102:
1.295.2.3 paf 103: // Request
1.159 parser 104:
1.295.2.6 paf 105: void Request::execute(ArrayOperation& ops) {
1.295.2.27.2. 1(paf 106:3): register Stack<StackItem>& stack=this->stack; // helps a lot on MSVC: 'esi'
107:3):
1.157 parser 108: #ifdef DEBUG_EXECUTE
1.295.2.11 paf 109: debug_printf(sapi_info, "source----------------------------\n");
1.295.2.27.2. (paf 110:): debug_dump(sapi_info, 0, ops);
1.295.2.11 paf 111: debug_printf(sapi_info, "execution-------------------------\n");
1.157 parser 112: #endif
1.295.2.27.2. 2(paf 113:3): for(Array_iterator<Operation> i(ops); i.has_next(); ) {
1.295.2.26 paf 114: if(get_interrupted()) {
115: set_interrupted(false);
1.293 paf 116: throw Exception("parser.interrupted",
1.295.2.27.2. (paf 117:): 0,
1.293 paf 118: "execution stopped");
1.295.2.26 paf 119: }
1.295.2.27.2. 2(paf 120:3): OPCODE opcode=i.next().code;
1.157 parser 121: #ifdef DEBUG_EXECUTE
1.295.2.27.2. 2(paf 122:3): debug_printf(sapi_info, "%d:%s", stack.top_index()+1, opcode_name[opcode]);
1.157 parser 123: #endif
1.11 paf 124:
1.295.2.27.2. 2(paf 125:3): switch(opcode) {
1.51 paf 126: // param in next instruction
1.52 paf 127: case OP_VALUE:
1.32 paf 128: {
1.295.2.27.2. 2(paf 129:3): Value& value=*i.next().value;
1.157 parser 130: #ifdef DEBUG_EXECUTE
1.295.2.27.2. 2(paf 131:3): debug_printf(sapi_info, " \"%s\" %s", value.get_string()->cstr(), value.type());
1.157 parser 132: #endif
1.295.2.6 paf 133: stack.push(value);
1.32 paf 134: break;
135: }
1.66 paf 136: case OP_GET_CLASS:
1.38 paf 137: {
1.130 paf 138: // maybe they do ^class:method[] call, remember the fact
1.215 paf 139: wcontext->set_somebody_entered_some_class();
1.98 paf 140:
1.295.2.27.2. 2(paf 141:3): const String& name=stack.pop().string();
142:3): Value* value=classes().get(name);
1.120 paf 143: if(!value)
1.223 paf 144: throw Exception("parser.runtime",
1.295.2.27.2. 2(paf 145:3): &name,
1.143 paf 146: "class is undefined");
1.66 paf 147:
1.295.2.27.2. 2(paf 148:3): stack.push(*value);
1.38 paf 149: break;
150: }
1.32 paf 151:
1.51 paf 152: // OP_WITH
1.187 parser 153: case OP_WITH_ROOT:
154: {
1.295.2.27.2. 2(paf 155:3): stack.push(*method_frame);
1.187 parser 156: break;
157: }
1.37 paf 158: case OP_WITH_SELF:
159: {
1.295.2.27.2. 2(paf 160:3): stack.push(get_self());
1.37 paf 161: break;
162: }
1.15 paf 163: case OP_WITH_READ:
164: {
1.295.2.27.2. 2(paf 165:3): stack.push(*rcontext);
1.20 paf 166: break;
167: }
1.37 paf 168: case OP_WITH_WRITE:
1.20 paf 169: {
1.287 paf 170: if(wcontext==method_frame)
171: throw Exception("parser.runtime",
1.295.2.27.2. (paf 172:): 0,
1.287 paf 173: "$.name outside of $name[...]");
174:
1.295.2.27.2. 2(paf 175:3): stack.push(*wcontext);
1.20 paf 176: break;
177: }
1.37 paf 178:
1.51 paf 179: // OTHER ACTIONS BUT WITHs
1.80 paf 180: case OP_CONSTRUCT_VALUE:
1.20 paf 181: {
1.295.2.27.2. 2(paf 182:3): Value& value=stack.pop().value();
183:3): const String& name=stack.pop().string();
184:3): Value& ncontext=stack.pop().value();
185:3): ncontext.put_element(name, &value, false);
1.213 paf 186: break;
187: }
1.80 paf 188: case OP_CONSTRUCT_EXPR:
189: {
1.219 paf 190: // see OP_PREPARE_TO_EXPRESSION
191: wcontext->set_in_expression(false);
192:
1.295.2.27.2. 2(paf 193:3): Value& value=stack.pop().value();
194:3): const String& name=stack.pop().string();
195:3): Value& ncontext=stack.pop().value();
196:3): ncontext.put_element(name, &value.as_expr_result(), false);
1.80 paf 197: break;
198: }
1.182 parser 199: case OP_CURLY_CODE__CONSTRUCT:
200: {
1.295.2.27.2. 2(paf 201:3): ArrayOperation& local_ops=*i.next().ops;
1.182 parser 202: #ifdef DEBUG_EXECUTE
1.295.2.27.2. 2(paf 203:3): debug_printf(sapi_info, " (%d)\n", local_ops.count());
3(paf 204:3): debug_dump(sapi_info, 1, local_ops);
1.182 parser 205: #endif
1.295.2.27.2. 2(paf 206:3): Value& value=*new VJunction(new Junction(
1.295.2.7 paf 207: get_self(), 0,
1.257 paf 208: method_frame,
1.240 paf 209: rcontext,
210: wcontext,
1.295.2.27.2. 2(paf 211:3): &local_ops));
1.238 paf 212:
1.295.2.27.2. 2(paf 213:3): const String& name=stack.pop().string();
214:3): Value& ncontext=stack.pop().value();
215:3): ncontext.put_element(name, &value, false);
1.182 parser 216: break;
217: }
1.209 paf 218: case OP_NESTED_CODE:
219: {
1.295.2.27.2. 2(paf 220:3): ArrayOperation& local_ops=*i.next().ops;
1.209 paf 221: #ifdef DEBUG_EXECUTE
1.295.2.27.2. 2(paf 222:3): debug_printf(sapi_info, " (%d)\n", local_ops.count());
3(paf 223:3): debug_dump(sapi_info, 1, local_ops);
1.209 paf 224: #endif
1.295.2.6 paf 225: stack.push(local_ops);
1.209 paf 226: break;
227: }
1.108 paf 228: case OP_WRITE_VALUE:
1.13 paf 229: {
1.295.2.27.2. 2(paf 230:3): Value& value=stack.pop().value();
231:3): write_assign_lang(value);
1.86 paf 232: break;
233: }
1.108 paf 234: case OP_WRITE_EXPR_RESULT:
235: {
1.295.2.27.2. 2(paf 236:3): Value& value=stack.pop().value();
237:3): write_no_lang(value.as_expr_result());
1.230 paf 238:
239: // must be after write(result) and
1.219 paf 240: // see OP_PREPARE_TO_EXPRESSION
241: wcontext->set_in_expression(false);
1.108 paf 242: break;
243: }
1.86 paf 244: case OP_STRING__WRITE:
245: {
1.295.2.27.2. 2(paf 246:3): Value* value=i.next().value;
1.157 parser 247: #ifdef DEBUG_EXECUTE
1.295.2.27.2. (paf 248:): debug_printf(sapi_info, " \"%s\"", value->get_string()->cstr());
1.157 parser 249: #endif
1.295.2.27.2. (paf 250:): write_no_lang(*value->get_string());
1.13 paf 251: break;
1.14 paf 252: }
1.13 paf 253:
1.215 paf 254: case OP_GET_ELEMENT_OR_OPERATOR:
255: {
1.295.2.27.2. 2(paf 256:3): Value& value=get_element(true);
1.295.2.6 paf 257: stack.push(value);
1.215 paf 258: break;
259: }
1.15 paf 260: case OP_GET_ELEMENT:
1.11 paf 261: {
1.295.2.27.2. 2(paf 262:3): Value& value=get_element(false);
1.295.2.6 paf 263: stack.push(value);
1.17 paf 264: break;
265: }
266:
1.18 paf 267: case OP_GET_ELEMENT__WRITE:
1.17 paf 268: {
1.295.2.27.2. 2(paf 269:3): Value& value=get_element(false);
270:3): write_assign_lang(value);
1.17 paf 271: break;
272: }
273:
1.32 paf 274:
1.226 paf 275: case OP_OBJECT_POOL:
1.17 paf 276: {
1.295.2.27.2. 2(paf 277:3): ArrayOperation& local_ops=*i.next().ops;
1.226 paf 278:
1.257 paf 279: WContext *saved_wcontext=wcontext;
1.295.2.27.2. (paf 280:): String::Language saved_lang=flang;
281:): flang=String::L_PASS_APPENDED;
282:): WWrapper local(0/*empty*/, wcontext);
1.226 paf 283: wcontext=&local;
284:
1.295.2.27.2. 2(paf 285:3): execute(local_ops);
1.226 paf 286:
1.295.2.27.2. 2(paf 287:3): stack.push(wcontext->result().as_value());
1.257 paf 288: flang=saved_lang;
289: wcontext=saved_wcontext;
1.13 paf 290: break;
1.15 paf 291: }
1.13 paf 292:
1.226 paf 293: case OP_STRING_POOL:
1.49 paf 294: {
1.295.2.27.2. 2(paf 295:3): ArrayOperation& local_ops=*i.next().ops;
1.226 paf 296:
1.257 paf 297: WContext *saved_wcontext=wcontext;
1.295.2.27.2. (paf 298:): WWrapper local(0 /*empty*/, wcontext);
1.226 paf 299: wcontext=&local;
300:
1.295.2.27.2. 2(paf 301:3): execute(local_ops);
1.226 paf 302:
1.295.2.27.2. 2(paf 303:3): Value* value;
1.49 paf 304: // from "$a $b" part of expression taking only string value,
305: // ignoring any other content of wcontext
1.295.2.27.2. (paf 306:): if(const String* string=wcontext->get_string())
307:): value=new VString(*string);
1.80 paf 308: else
1.295.2.27.2. (paf 309:): value=new VVoid;
2(paf 310:3): stack.push(*value);
1.295.2.7 paf 311:
1.257 paf 312: wcontext=saved_wcontext;
1.49 paf 313: break;
314: }
315:
1.51 paf 316: // CALL
1.237 paf 317: case OP_STORE_PARAM:
1.28 paf 318: {
1.295.2.27.2. 2(paf 319:3): Value& value=stack.pop().value();
320:3): VMethodFrame& frame=stack.top_value().method_frame();
1.295.2.7 paf 321: // this op is executed from CALL local_ops only, so may skip the check "method_frame_to_fill==0"
1.295.2.27.2. 2(paf 322:3): frame.store_param(value);
1.28 paf 323: break;
324: }
1.237 paf 325: case OP_CURLY_CODE__STORE_PARAM:
326: case OP_EXPR_CODE__STORE_PARAM:
1.28 paf 327: {
1.237 paf 328: // code
1.295.2.27.2. 2(paf 329:3): ArrayOperation& local_ops=*i.next().ops;
330:3): VMethodFrame& frame=stack.top_value().method_frame();
1.237 paf 331: #ifdef DEBUG_EXECUTE
1.295.2.27.2. 2(paf 332:3): debug_printf(sapi_info, " (%d)\n", local_ops.count());
3(paf 333:3): debug_dump(sapi_info, 1, local_ops);
1.237 paf 334: #endif
335: // when they evaluate expression parameter,
336: // the object expression result
337: // does not need to be written into calling frame
338: // it must go into any expressions using that parameter
339: // hence, we zero junction.wcontext here, and later
340: // in .process we would test that field
341: // in decision "which wwrapper to use"
1.295.2.27.2. 2(paf 342:3): Value& value=*new VJunction(new Junction(
1.295.2.7 paf 343: get_self(), 0,
1.257 paf 344: method_frame,
1.237 paf 345: rcontext,
1.295.2.27.2. 2(paf 346:3): opcode==OP_EXPR_CODE__STORE_PARAM?0:wcontext,
347:3): &local_ops));
1.237 paf 348: // store param
349: // this op is executed from CALL local_ops only, so can not check method_frame_to_fill==0
1.295.2.27.2. 2(paf 350:3): frame.store_param(value);
1.29 paf 351: break;
352: }
353:
1.186 parser 354: case OP_PREPARE_TO_CONSTRUCT_OBJECT:
355: {
1.200 paf 356: wcontext->set_constructing(true);
1.186 parser 357: break;
358: }
1.219 paf 359:
360: case OP_PREPARE_TO_EXPRESSION:
361: {
362: wcontext->set_in_expression(true);
363: break;
364: }
365:
1.186 parser 366: case OP_CALL:
1.232 paf 367: case OP_CALL__WRITE:
1.29 paf 368: {
1.295.2.27.2. 2(paf 369:3): ArrayOperation* local_ops=i.next().ops;
1.157 parser 370: #ifdef DEBUG_EXECUTE
1.295.2.27.2. 3(paf 371:3): debug_printf(sapi_info, " (%d)\n", local_ops?local_ops->count():0);
1.295.2.13 paf 372: if(local_ops)
1.295.2.27.2. (paf 373:): debug_dump(sapi_info, 1, *local_ops);
1.237 paf 374:
1.295.2.11 paf 375: debug_printf(sapi_info, "->\n");
1.157 parser 376: #endif
1.295.2.27.2. 2(paf 377:3): Value& value=stack.pop().value();
1.237 paf 378:
1.295.2.27.2. 2(paf 379:3): Junction* junction=value.get_junction();
1.237 paf 380: if(!junction)
381: throw Exception("parser.runtime",
1.295.2.27.2. (paf 382:): 0,
1.237 paf 383: "(%s) not a method or junction, can not call it",
1.295.2.27.2. 2(paf 384:3): value.type());
1.282 paf 385: /* no check needed, code compiled the way that that's impossible
1.276 paf 386: // check:
387: // that this is method-junction, not a code-junction
1.278 paf 388: // [disabling these contstructions:]
389: // $junction{code}
390: // ^junction[]
1.276 paf 391: if(!junction->method)
392: throw Exception("parser.runtime",
1.295.2.27.2. (paf 393:):
1.276 paf 394: "(%s) is code junction, can not call it",
1.295.2.27.2. 2(paf 395:3): value.type());
1.282 paf 396: */
1.237 paf 397:
1.295.2.27.2. (paf 398:): VMethodFrame frame(*junction, method_frame/*caller*/);
1.237 paf 399: if(local_ops){ // store param code goes here
1.295.2.27.2. 2(paf 400:3): stack.push(frame); // argument for *STORE_PARAM ops
1.237 paf 401: execute(*local_ops);
1.295.2.27.2. 2(paf 402:3): stack.pop().value();
1.237 paf 403: }
404: frame.fill_unspecified_params();
1.257 paf 405: VMethodFrame *saved_method_frame=method_frame;
1.295.2.6 paf 406: Value* saved_rcontext=rcontext;
1.257 paf 407: WContext *saved_wcontext=wcontext;
1.45 paf 408:
1.295.2.27.2. (paf 409:): VStateless_class& called_class=*frame.junction.self.get_class();
410:): Value* new_self=&get_self();
1.200 paf 411: if(wcontext->get_constructing()) {
412: wcontext->set_constructing(false);
1.237 paf 413: if(frame.junction.method->call_type!=Method::CT_STATIC) {
1.138 paf 414: // this is a constructor call
1.185 parser 415:
1.295.2.27.2. (paf 416:): if(Value* value=called_class.create_new_value()) {
1.204 paf 417: // some stateless_class creatable derivates
1.285 paf 418: new_self=value;
1.204 paf 419: } else
1.223 paf 420: throw Exception("parser.runtime",
1.295.2.27.2. (paf 421:): 0, //&frame.name(),
1.204 paf 422: "is not a constructor, system class '%s' can be constructed only implicitly",
1.295.2.27.2. (paf 423:): called_class.name().cstr());
1.204 paf 424:
1.295.2.27.2. (paf 425:): frame.write(*new_self,
426:): String::L_CLEAN // not used, always an object, not string
1.83 paf 427: );
1.185 parser 428: } else
1.223 paf 429: throw Exception("parser.runtime",
1.295.2.27.2. (paf 430:): 0, //&frame.name(),
1.185 parser 431: "method is static and can not be used as constructor");
1.249 paf 432: } else
1.295.2.27.2. (paf 433:): new_self=&frame.junction.self;
1.75 paf 434:
1.295.2.27.2. (paf 435:): frame.set_self(*new_self);
1.219 paf 436:
437: // see OP_PREPARE_TO_EXPRESSION
1.237 paf 438: frame.set_in_expression(wcontext->get_in_expression());
1.219 paf 439:
1.237 paf 440: rcontext=wcontext=&frame;
1.47 paf 441: {
1.237 paf 442: const Method& method=*frame.junction.method;
1.134 paf 443: Method::Call_type call_type=
1.295.2.27.2. (paf 444:): &called_class==new_self ? Method::CT_STATIC : Method::CT_DYNAMIC;
1.134 paf 445: if(
446: method.call_type==Method::CT_ANY ||
1.197 parser 447: method.call_type==call_type) { // allowed call type?
1.295.2.27.2. 3(paf 448:3): #ifndef LATER_STACK_TRACE
1.197 parser 449: try {
1.295.2.25 paf 450: #endif
1.283 paf 451: method_frame=&frame;
1.197 parser 452: if(method.native_code) { // native code?
453: method.check_actual_numbered_params(
1.295.2.27.2. (paf 454:): frame.junction.self,
455:): /*frame.name(), */frame.numbered_params());
1.197 parser 456: method.native_code(
457: *this,
1.295.2.27.2. (paf 458:): /*frame.name(), */frame.numbered_params()); // execute it
1.283 paf 459: } else // parser code, execute it
1.295.2.27.2. (paf 460:): recoursion_checked_execute(/*frame.name(), */*method.parser_code);
3(paf 461:3): #ifndef LATER_STACK_TRACE
1.197 parser 462: } catch(...) {
463: // record it to stack trace
1.295.2.7 paf 464: exception_trace.push(frame.name());
1.295.2.5 paf 465: rethrow;
1.159 parser 466: }
1.295.2.25 paf 467: #endif
1.197 parser 468: } else
1.223 paf 469: throw Exception("parser.runtime",
1.295.2.27.2. (paf 470:): 0, //&frame.name(),
1.134 paf 471: "is not allowed to be called %s",
472: call_type==Method::CT_STATIC?"statically":"dynamically");
473:
1.47 paf 474: }
1.232 paf 475: StringOrValue result=wcontext->result();
1.45 paf 476:
1.257 paf 477: wcontext=saved_wcontext;
478: rcontext=saved_rcontext;
479: method_frame=saved_method_frame;
1.213 paf 480:
1.295.2.27.2. 2(paf 481:3): if(opcode==OP_CALL__WRITE) {
(paf 482:): write_assign_lang(result);
1.232 paf 483: } else { // OP_CALL
1.295.2.27.2. 2(paf 484:3): stack.push(result.as_value());
1.232 paf 485: }
486:
1.157 parser 487: #ifdef DEBUG_EXECUTE
1.295.2.11 paf 488: debug_printf(sapi_info, "<-returned");
1.157 parser 489: #endif
1.49 paf 490: break;
491: }
492:
1.55 paf 493: // expression ops: unary
494: case OP_NEG:
495: {
1.295.2.27.2. 2(paf 496:3): Value& a=stack.pop().value();
497:3): Value& value=*new VDouble(-a.as_double());
1.295.2.6 paf 498: stack.push(value);
1.55 paf 499: break;
500: }
501: case OP_INV:
502: {
1.295.2.27.2. 2(paf 503:3): Value& a=stack.pop().value();
504:3): Value& value=*new VDouble(~a.as_int());
1.295.2.6 paf 505: stack.push(value);
1.55 paf 506: break;
507: }
508: case OP_NOT:
509: {
1.295.2.27.2. 2(paf 510:3): Value& a=stack.pop().value();
511:3): Value& value=*new VBool(!a.as_bool());
1.295.2.6 paf 512: stack.push(value);
1.55 paf 513: break;
514: }
1.62 paf 515: case OP_DEF:
516: {
1.295.2.27.2. 2(paf 517:3): Value& a=stack.pop().value();
518:3): Value& value=*new VBool(a.is_defined());
1.295.2.6 paf 519: stack.push(value);
1.62 paf 520: break;
521: }
522: case OP_IN:
523: {
1.295.2.27.2. 2(paf 524:3): Value& a=stack.pop().value();
525:3): const String& path=a.as_string();
526:3): Value& value=*new VBool(request_info.uri && *request_info.uri && path.this_starts(request_info.uri));
1.295.2.6 paf 527: stack.push(value);
1.62 paf 528: break;
529: }
530: case OP_FEXISTS:
531: {
1.295.2.27.2. 2(paf 532:3): Value& a=stack.pop().value();
533:3): Value& value=*new VBool(file_readable(absolute(a.as_string())));
1.295.2.6 paf 534: stack.push(value);
1.148 paf 535: break;
536: }
537: case OP_DEXISTS:
538: {
1.295.2.27.2. 2(paf 539:3): Value& a=stack.pop().value();
540:3): Value& value=*new VBool(dir_readable(absolute(a.as_string())));
1.295.2.6 paf 541: stack.push(value);
1.62 paf 542: break;
543: }
1.55 paf 544:
545: // expression ops: binary
546: case OP_SUB:
1.53 paf 547: {
1.295.2.27.2. 2(paf 548:3): Value& b=stack.pop().value(); Value& a=stack.pop().value();
549:3): Value& value=*new VDouble(a.as_double() - b.as_double());
1.295.2.6 paf 550: stack.push(value);
1.53 paf 551: break;
552: }
1.55 paf 553: case OP_ADD:
1.53 paf 554: {
1.295.2.27.2. 2(paf 555:3): Value& b=stack.pop().value(); Value& a=stack.pop().value();
556:3): Value& value=*new VDouble(a.as_double() + b.as_double());
1.295.2.6 paf 557: stack.push(value);
1.53 paf 558: break;
559: }
1.49 paf 560: case OP_MUL:
561: {
1.295.2.27.2. 2(paf 562:3): Value& b=stack.pop().value(); Value& a=stack.pop().value();
563:3): Value& value=*new VDouble(a.as_double() * b.as_double());
1.295.2.6 paf 564: stack.push(value);
1.53 paf 565: break;
566: }
567: case OP_DIV:
568: {
1.295.2.27.2. 2(paf 569:3): Value& b=stack.pop().value(); Value& a=stack.pop().value();
1.170 parser 570:
1.295.2.27.2. 2(paf 571:3): double a_double=a.as_double();
572:3): double b_double=b.as_double();
1.170 parser 573:
1.171 parser 574: if(b_double == 0) {
1.295.2.27.2. 2(paf 575:3): //const String* problem_source=b.as_string();
1.223 paf 576: throw Exception("number.zerodivision",
1.295.2.27.2. (paf 577:): 0, //problem_source,
1.170 parser 578: "Division by zero");
1.171 parser 579: }
1.170 parser 580:
1.295.2.27.2. 2(paf 581:3): Value& value=*new VDouble(a_double / b_double);
1.295.2.6 paf 582: stack.push(value);
1.54 paf 583: break;
584: }
1.55 paf 585: case OP_MOD:
586: {
1.295.2.27.2. 2(paf 587:3): Value& b=stack.pop().value(); Value& a=stack.pop().value();
1.170 parser 588:
1.295.2.27.2. 2(paf 589:3): double a_double=a.as_double();
590:3): double b_double=b.as_double();
1.170 parser 591:
1.173 parser 592: if(b_double == 0) {
1.295.2.27.2. 2(paf 593:3): //const String* problem_source=b.as_string();
1.223 paf 594: throw Exception("number.zerodivision",
1.295.2.27.2. (paf 595:): 0, //problem_source,
1.170 parser 596: "Modulus by zero");
1.171 parser 597: }
1.170 parser 598:
1.295.2.27.2. 2(paf 599:3): Value& value=*new VDouble(fmod(a_double, b_double));
1.295.2.6 paf 600: stack.push(value);
1.201 paf 601: break;
602: }
603: case OP_INTDIV:
604: {
1.295.2.27.2. 2(paf 605:3): Value& b=stack.pop().value(); Value& a=stack.pop().value();
1.201 paf 606:
1.295.2.27.2. 2(paf 607:3): int a_int=a.as_int();
608:3): int b_int=b.as_int();
1.201 paf 609:
610: if(b_int == 0) {
1.295.2.27.2. 2(paf 611:3): //const String* problem_source=b.as_string();
1.223 paf 612: throw Exception("number.zerodivision",
1.295.2.27.2. (paf 613:): 0, //problem_source,
1.201 paf 614: "Division by zero");
615: }
616:
1.295.2.27.2. 2(paf 617:3): Value& value=*new VInt(a_int / b_int);
1.295.2.6 paf 618: stack.push(value);
1.55 paf 619: break;
620: }
1.274 paf 621: case OP_BIN_SL:
622: {
1.295.2.27.2. 2(paf 623:3): Value& b=stack.pop().value(); Value& a=stack.pop().value();
624:3): Value& value=*new VInt(
625:3): a.as_int() <<
626:3): b.as_int());
1.295.2.6 paf 627: stack.push(value);
1.274 paf 628: break;
629: }
630: case OP_BIN_SR:
631: {
1.295.2.27.2. 2(paf 632:3): Value& b=stack.pop().value(); Value& a=stack.pop().value();
633:3): Value& value=*new VInt(
634:3): a.as_int() >>
635:3): b.as_int());
1.295.2.6 paf 636: stack.push(value);
1.274 paf 637: break;
638: }
1.55 paf 639: case OP_BIN_AND:
1.54 paf 640: {
1.295.2.27.2. 2(paf 641:3): Value& b=stack.pop().value(); Value& a=stack.pop().value();
642:3): Value& value=*new VInt(
643:3): a.as_int() &
644:3): b.as_int());
1.295.2.6 paf 645: stack.push(value);
1.54 paf 646: break;
647: }
1.55 paf 648: case OP_BIN_OR:
1.54 paf 649: {
1.295.2.27.2. 2(paf 650:3): Value& b=stack.pop().value(); Value& a=stack.pop().value();
651:3): Value& value=*new VInt(
652:3): a.as_int() |
653:3): b.as_int());
1.295.2.6 paf 654: stack.push(value);
1.55 paf 655: break;
656: }
1.56 paf 657: case OP_BIN_XOR:
658: {
1.295.2.27.2. 2(paf 659:3): Value& b=stack.pop().value(); Value& a=stack.pop().value();
660:3): Value& value=*new VInt(
661:3): a.as_int() ^
662:3): b.as_int());
1.295.2.6 paf 663: stack.push(value);
1.56 paf 664: break;
665: }
1.55 paf 666: case OP_LOG_AND:
667: {
1.295.2.27.2. 2(paf 668:3): ArrayOperation& local_ops=stack.pop().ops(); Value& a=stack.pop().value();
1.263 paf 669: bool result;
1.295.2.27.2. 2(paf 670:3): if(a.as_bool()) {
671:3): execute(local_ops);
672:3): Value& b=stack.pop().value();
673:3): result=b.as_bool();
1.209 paf 674: } else
1.263 paf 675: result=false;
1.295.2.27.2. 2(paf 676:3): Value& value=*new VBool(result);
1.295.2.6 paf 677: stack.push(value);
1.55 paf 678: break;
679: }
680: case OP_LOG_OR:
681: {
1.295.2.27.2. 2(paf 682:3): ArrayOperation& local_ops=stack.pop().ops(); Value& a=stack.pop().value();
1.263 paf 683: bool result;
1.295.2.27.2. 2(paf 684:3): if(a.as_bool())
1.263 paf 685: result=true;
1.209 paf 686: else {
1.295.2.27.2. 2(paf 687:3): execute(local_ops);
688:3): Value& b=stack.pop().value();
689:3): result=b.as_bool();
1.209 paf 690: }
1.295.2.27.2. 2(paf 691:3): Value& value=*new VBool(result);
1.295.2.6 paf 692: stack.push(value);
1.56 paf 693: break;
694: }
695: case OP_LOG_XOR:
696: {
1.295.2.27.2. 2(paf 697:3): Value& b=stack.pop().value(); Value& a=stack.pop().value();
698:3): Value& value=*new VBool(a.as_bool() ^ b.as_bool());
1.295.2.6 paf 699: stack.push(value);
1.55 paf 700: break;
701: }
702: case OP_NUM_LT:
703: {
1.295.2.27.2. 2(paf 704:3): Value& b=stack.pop().value(); Value& a=stack.pop().value();
705:3): double result=a.as_double() - b.as_double();
706:3): Value& value=*new VBool(result < 0.0);
1.295.2.6 paf 707: stack.push(value);
1.55 paf 708: break;
709: }
710: case OP_NUM_GT:
711: {
1.295.2.27.2. 2(paf 712:3): Value& b=stack.pop().value(); Value& a=stack.pop().value();
713:3): double result=a.as_double() - b.as_double();
714:3): Value& value=*new VBool(result > 0.0);
1.295.2.6 paf 715: stack.push(value);
1.55 paf 716: break;
717: }
718: case OP_NUM_LE:
719: {
1.295.2.27.2. 2(paf 720:3): Value& b=stack.pop().value(); Value& a=stack.pop().value();
721:3): double result=a.as_double() - b.as_double();
722:3): Value& value=*new VBool(result <= 0.0);
1.295.2.6 paf 723: stack.push(value);
1.55 paf 724: break;
725: }
726: case OP_NUM_GE:
727: {
1.295.2.27.2. 2(paf 728:3): Value& b=stack.pop().value(); Value& a=stack.pop().value();
729:3): double result=a.as_double() - b.as_double();
730:3): Value& value=*new VBool(result >= 0.0);
1.295.2.6 paf 731: stack.push(value);
1.55 paf 732: break;
733: }
734: case OP_NUM_EQ:
735: {
1.295.2.27.2. 2(paf 736:3): Value& b=stack.pop().value(); Value& a=stack.pop().value();
737:3): double result=a.as_double() - b.as_double();
738:3): Value& value=*new VBool(result == 0.0);
1.295.2.6 paf 739: stack.push(value);
1.55 paf 740: break;
741: }
742: case OP_NUM_NE:
743: {
1.295.2.27.2. 2(paf 744:3): Value& b=stack.pop().value(); Value& a=stack.pop().value();
745:3): double result=a.as_double() - b.as_double();
746:3): Value& value=*new VBool(result != 0.0);
1.295.2.6 paf 747: stack.push(value);
1.54 paf 748: break;
749: }
1.58 paf 750: case OP_STR_LT:
751: {
1.295.2.27.2. 2(paf 752:3): Value& b=stack.pop().value(); Value& a=stack.pop().value();
753:3): Value& value=*new VBool(a.as_string() < b.as_string());
1.295.2.6 paf 754: stack.push(value);
1.58 paf 755: break;
756: }
757: case OP_STR_GT:
758: {
1.295.2.27.2. 2(paf 759:3): Value& b=stack.pop().value(); Value& a=stack.pop().value();
760:3): Value& value=*new VBool(a.as_string() > b.as_string());
1.295.2.6 paf 761: stack.push(value);
1.58 paf 762: break;
763: }
1.55 paf 764: case OP_STR_LE:
1.58 paf 765: {
1.295.2.27.2. 2(paf 766:3): Value& b=stack.pop().value(); Value& a=stack.pop().value();
767:3): Value& value=*new VBool(a.as_string() <= b.as_string());
1.295.2.6 paf 768: stack.push(value);
1.58 paf 769: break;
770: }
1.55 paf 771: case OP_STR_GE:
1.54 paf 772: {
1.295.2.27.2. 2(paf 773:3): Value& b=stack.pop().value(); Value& a=stack.pop().value();
774:3): Value& value=*new VBool(a.as_string() >= b.as_string());
1.295.2.6 paf 775: stack.push(value);
1.58 paf 776: break;
777: }
778: case OP_STR_EQ:
779: {
1.295.2.27.2. 2(paf 780:3): Value& b=stack.pop().value(); Value& a=stack.pop().value();
781:3): Value& value=*new VBool(a.as_string() == b.as_string());
1.295.2.6 paf 782: stack.push(value);
1.58 paf 783: break;
784: }
785: case OP_STR_NE:
786: {
1.295.2.27.2. 2(paf 787:3): Value& b=stack.pop().value(); Value& a=stack.pop().value();
788:3): Value& value=*new VBool(a.as_string() != b.as_string());
1.295.2.6 paf 789: stack.push(value);
1.103 paf 790: break;
791: }
792: case OP_IS:
793: {
1.295.2.27.2. 2(paf 794:3): Value& b=stack.pop().value(); Value& a=stack.pop().value();
795:3): Value& value=*new VBool(a.is(b.as_string().cstr()));
1.295.2.6 paf 796: stack.push(value);
1.28 paf 797: break;
798: }
799:
1.11 paf 800: default:
1.295.2.27.2. (paf 801:): throw Exception(0,
802:): 0,
2(paf 803:3): "invalid opcode %d", opcode);
1.11 paf 804: }
805: }
1.1 paf 806: }
1.17 paf 807:
1.292 paf 808: /**
809: @todo cache|prepare junctions
810: @bug ^superbase:method would dynamically call ^base:method if there is any
811: */
1.295.2.27.2. 2(paf 812:3): Value& Request::get_element(bool can_call_operator) {
813:3): const String& name=stack.pop().string(); //remember_name=name;
814:3): Value& ncontext=stack.pop().value();
(paf 815:): Value* value=0;
1.249 paf 816: if(can_call_operator) {
1.295.2.27.2. (paf 817:): if(Method* method=main_class.get_method(name)) // looking operator of that name FIRST
818:): value=new VJunction(new Junction(
819:): main_class, method, 0,0,0, 0));
1.249 paf 820: }
821: if(!value) {
822: if(!wcontext->get_constructing() // not constructing
823: && wcontext->get_somebody_entered_some_class() ) // ^class:method
1.295.2.27.2. 2(paf 824:3): if(VStateless_class *called_class=ncontext.get_class())
1.249 paf 825: if(VStateless_class *read_class=rcontext->get_class())
826: if(read_class->derived_from(*called_class)) // current derived from called
1.295.2.27.2. (paf 827:): if(Value* base=get_self().base()) { // doing DYNAMIC call
1.295.2.27 paf 828: Temp_derived temp_derived(*base, 0); // temporarily prevent go-back-down virtual calls
1.290 paf 829: value=base->get_element(name, *base, false); // virtual-up lookup starting from parent
1.289 paf 830: goto value_ready;
1.249 paf 831: }
832: }
1.216 paf 833: if(!value)
1.295.2.27.2. 2(paf 834:3): value=ncontext.get_element(name, ncontext, false);
1.291 paf 835:
836: if(value && wcontext->get_constructing())
1.295.2.27.2. (paf 837:): if(Junction* junction=value->get_junction()) {
2(paf 838:3): if(junction->self.get_class()!=&ncontext)
1.291 paf 839: throw Exception("parser.runtime",
1.295.2.27.2. (paf 840:): &name,
1.295.2.20 paf 841: "constructor must be declared in class '%s'",
1.295.2.27.2. 2(paf 842:3): ncontext.get_class()->name_cstr());
1.291 paf 843: }
1.249 paf 844:
1.289 paf 845: value_ready:
1.284 paf 846: if(value)
1.295.2.27.2. (paf 847:): value=&process_to_value(*value); // process possible code-junction
1.284 paf 848: else
1.295.2.27.2. (paf 849:): value=new VVoid;
1.63 paf 850:
1.295.2.27.2. 2(paf 851:3): return *value;
1.34 paf 852: }
1.70 paf 853:
1.162 parser 854: /** @param intercept_string
1.116 paf 855: - true:
856: they want result=string value,
857: possible object result goes to wcontext
858: - false:
859: they want any result[string|object]
860: nothing goes to wcontext.
1.125 paf 861: used in @c (expression) params evaluation
1.224 paf 862:
863: using the fact it's either string_ or value_ result requested to speed up checkes
1.116 paf 864: */
1.295.2.27.2. (paf 865:): StringOrValue Request::process(Value& input_value, bool intercept_string) {
1.228 paf 866: StringOrValue result;
1.295.2.27.2. (paf 867:): Junction* junction=input_value.get_junction();
1.70 paf 868: if(junction && junction->code) { // is it a code-junction?
1.92 paf 869: // process it
1.157 parser 870: #ifdef DEBUG_EXECUTE
1.295.2.11 paf 871: debug_printf(sapi_info, "ja->\n");
1.157 parser 872: #endif
1.238 paf 873:
1.257 paf 874: if(!junction->method_frame)
1.240 paf 875: throw Exception("parser.runtime",
1.295.2.27.2. (paf 876:): 0,
1.240 paf 877: "junction used outside of context");
878:
1.257 paf 879: VMethodFrame *saved_method_frame=method_frame;
1.295.2.6 paf 880: Value* saved_rcontext=rcontext;
1.257 paf 881: WContext *saved_wcontext=wcontext;
1.240 paf 882:
1.257 paf 883: method_frame=junction->method_frame;
1.240 paf 884: rcontext=junction->rcontext;
1.225 paf 885:
1.109 paf 886: // for expression method params
887: // wcontext is set 0
888: // using the fact in decision "which wwrapper to use"
1.240 paf 889: bool using_code_frame=intercept_string && junction->wcontext;
1.109 paf 890: if(using_code_frame) {
1.71 paf 891: // almost plain wwrapper about junction wcontext,
892: // BUT intercepts string writes
1.295.2.27.2. (paf 893:): VCodeFrame local(*junction->wcontext, junction->wcontext);
1.225 paf 894: wcontext=&local;
1.207 paf 895:
1.225 paf 896: // execute it
1.295.2.27.2. (paf 897:): recoursion_checked_execute(/*0/*result_name* /, */ *junction->code);
1.226 paf 898:
1.71 paf 899: // CodeFrame soul:
900: // string writes were intercepted
901: // returning them as the result of getting code-junction
1.295.2.27.2. (paf 902:): result.set_string(*wcontext->get_string());
1.224 paf 903: } else {
1.225 paf 904: // plain wwrapper
1.295.2.27.2. (paf 905:): WWrapper local(0/*empty*/, wcontext);
1.225 paf 906: wcontext=&local;
907:
908: // execute it
1.295.2.27.2. (paf 909:): recoursion_checked_execute(/*0/*result_name* /, */ *junction->code);
1.226 paf 910:
1.228 paf 911: result=wcontext->result();
1.224 paf 912: }
1.70 paf 913:
1.257 paf 914: wcontext=saved_wcontext;
915: rcontext=saved_rcontext;
916: method_frame=saved_method_frame;
1.70 paf 917:
1.157 parser 918: #ifdef DEBUG_EXECUTE
1.295.2.11 paf 919: debug_printf(sapi_info, "<-ja returned");
1.157 parser 920: #endif
1.224 paf 921: } else {
1.228 paf 922: result.set_value(input_value);
1.224 paf 923: }
1.228 paf 924: return result;
1.85 paf 925: }
926:
1.295.2.27.2. (paf 927:): const String& Request::execute_method(VMethodFrame& amethod_frame, const Method& method) {
1.257 paf 928: VMethodFrame *saved_method_frame=method_frame;
1.295.2.6 paf 929: Value* saved_rcontext=rcontext;
1.257 paf 930: WContext *saved_wcontext=wcontext;
1.99 paf 931:
932: // initialize contexts
1.286 paf 933: rcontext=wcontext=method_frame=&amethod_frame;
1.99 paf 934:
935: // execute!
936: execute(*method.parser_code);
937:
938: // result
1.295.2.27.2. (paf 939:): const String& result=wcontext->result().as_string();
1.208 paf 940:
1.257 paf 941: wcontext=saved_wcontext;
942: rcontext=saved_rcontext;
943: method_frame=saved_method_frame;
1.242 paf 944:
945: // return
946: return result;
1.208 paf 947: }
948:
1.295.2.27.2. (paf 949:): const String* Request::execute_method(Value& aself,
950:): const Method& method, VString* optional_param,
1.295.2.7 paf 951: bool do_return_string) {
1.257 paf 952: VMethodFrame *saved_method_frame=method_frame;
1.295.2.6 paf 953: Value* saved_rcontext=rcontext;
1.257 paf 954: WContext *saved_wcontext=wcontext;
1.208 paf 955:
1.295.2.27.2. (paf 956:): Junction local_junction(aself, &method, 0,0,0, 0);
957:): VMethodFrame local_frame(/*method.name, */local_junction, method_frame/*caller*/);
1.242 paf 958: if(optional_param && local_frame.can_store_param()) {
1.295.2.27.2. (paf 959:): local_frame.store_param(*optional_param);
1.242 paf 960: local_frame.fill_unspecified_params();
961: }
1.285 paf 962: local_frame.set_self(aself);
1.257 paf 963: rcontext=wcontext=method_frame=&local_frame;
1.246 paf 964:
965: // prevent non-string writes for better error reporting
1.295.2.7 paf 966: if(do_return_string)
1.295.2.27.2. (paf 967:): wcontext->write(local_frame);
1.208 paf 968:
969: // execute!
970: execute(*method.parser_code);
971:
972: // result
1.295.2.27.2. (paf 973:): const String* result=0;
1.295.2.7 paf 974: if(do_return_string)
1.295.2.27.2. (paf 975:): result=&wcontext->result().as_string();
1.99 paf 976:
1.257 paf 977: wcontext=saved_wcontext;
978: rcontext=saved_rcontext;
979: method_frame=saved_method_frame;
1.295.2.7 paf 980:
981: return result;
1.242 paf 982: }
983:
1.295.2.16 paf 984: Request::Execute_nonvirtual_method_result
985: Request::execute_nonvirtual_method(VStateless_class& aclass,
1.295.2.27.2. (paf 986:): const String& method_name, VString* optional_param,
987:): bool do_return_string) {
1.295.2.16 paf 988: Execute_nonvirtual_method_result result;
989: result.method=aclass.get_method(method_name);
990: if(result.method)
1.295.2.27.2. (paf 991:): result.string=execute_method(aclass, *result.method, optional_param,
1.295.2.16 paf 992: do_return_string);
993: return result;
1.99 paf 994: }
995:
1.295.2.27.2. (paf 996:): const String* Request::execute_virtual_method(Value& aself,
997:): const String& method_name) {
998:): if(Value* value=aself.get_element(method_name, aself, false))
999:): if(Junction* junction=value->get_junction())
1.295.2.7 paf 1000: if(const Method *method=junction->method)
1.295.2.27.2. (paf 1001:): return execute_method(aself, *method, 0/*no params*/, true);
1.188 parser 1002:
1.295.2.27.2. (paf 1003:): return 0;
1.176 parser 1004: }
E-mail: