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