Annotation of parser3/src/main/execute.C, revision 1.424
1.117 paf 1: /** @file
1.118 paf 2: Parser: executor part of request class.
3:
1.414 moko 4: Copyright (c) 2001-2023 Art. Lebedev Studio (http://www.artlebedev.com)
5: Authors: Konstantin Morshnev <moko@design.ru>, Alexandr Petrosian <paf@design.ru>
1.243 paf 6: */
1.118 paf 7:
1.152 paf 8: #include "pa_opcode.h"
1.8 paf 9: #include "pa_array.h"
1.11 paf 10: #include "pa_request.h"
1.15 paf 11: #include "pa_vstring.h"
1.22 paf 12: #include "pa_vhash.h"
1.167 parser 13: #include "pa_vvoid.h"
1.145 paf 14: #include "pa_vcode_frame.h"
1.321 misha 15: #include "pa_vmethod_frame.h"
1.38 paf 16: #include "pa_vobject.h"
1.49 paf 17: #include "pa_vdouble.h"
1.54 paf 18: #include "pa_vbool.h"
1.94 paf 19: #include "pa_vtable.h"
1.132 paf 20: #include "pa_vfile.h"
1.144 paf 21: #include "pa_vimage.h"
1.421 moko 22: #include "pa_varray.h"
1.194 parser 23: #include "pa_wwrapper.h"
1.1 paf 24:
1.424 ! moko 25: volatile const char * IDENT_EXECUTE_C="$Id: execute.C,v 1.423 2024/10/02 21:24:41 moko Exp $" IDENT_PA_OPCODE_H IDENT_PA_OPERATION_H IDENT_PA_VCODE_FRAME_H IDENT_PA_WWRAPPER_H;
1.368 moko 26:
1.233 paf 27: //#define DEBUG_EXECUTE
1.157 parser 28:
29: #ifdef DEBUG_EXECUTE
1.387 moko 30: const 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.387 moko 37: "VALUE__GET_CLASS", "VALUE__GET_BASE_CLASS",
1.421 moko 38: "CONSTRUCT_VALUE", "CONSTRUCT_EXPR", "CURLY_CODE__CONSTRUCT", "CONSTRUCT_ARRAY",
1.108 paf 39: "WRITE_VALUE", "WRITE_EXPR_RESULT", "STRING__WRITE",
1.328 misha 40: #ifdef OPTIMIZE_BYTECODE_GET_ELEMENT
41: "VALUE__GET_ELEMENT_OR_OPERATOR",
42: #else
43: "GET_ELEMENT_OR_OPERATOR",
44: #endif
45: "GET_ELEMENT",
1.346 misha 46: "GET_ELEMENT__WRITE",
47: #ifdef OPTIMIZE_BYTECODE_GET_ELEMENT
48: "VALUE__GET_ELEMENT",
49: "VALUE__GET_ELEMENT__WRITE",
1.347 misha 50: "WITH_ROOT__VALUE__GET_ELEMENT",
1.346 misha 51: #endif
1.335 misha 52: #ifdef OPTIMIZE_BYTECODE_GET_OBJECT_ELEMENT
53: "GET_OBJECT_ELEMENT",
54: "GET_OBJECT_ELEMENT__WRITE",
55: #endif
56: #ifdef OPTIMIZE_BYTECODE_GET_OBJECT_VAR_ELEMENT
57: "GET_OBJECT_VAR_ELEMENT",
58: "GET_OBJECT_VAR_ELEMENT__WRITE",
1.334 misha 59: #endif
1.345 misha 60: #ifdef OPTIMIZE_BYTECODE_GET_SELF_ELEMENT
61: "WITH_SELF__VALUE__GET_ELEMENT",
62: "WITH_SELF__VALUE__GET_ELEMENT__WRITE",
63: #endif
1.376 moko 64:
65: #ifdef FEATURE_GET_ELEMENT4CALL
66: "GET_ELEMENT4CALL",
67: #ifdef OPTIMIZE_BYTECODE_GET_OBJECT_ELEMENT
68: "GET_OBJECT_ELEMENT4CALL",
69: #endif
70: #ifdef OPTIMIZE_BYTECODE_GET_OBJECT_VAR_ELEMENT
71: "GET_OBJECT_VAR_ELEMENT4CALL",
72: #endif
73: #endif // FEATURE_GET_ELEMENT4CALL
74:
1.232 paf 75: "OBJECT_POOL", "STRING_POOL",
1.348 misha 76: "PREPARE_TO_CONSTRUCT_OBJECT",
77: "CONSTRUCT_OBJECT",
78: "CONSTRUCT_OBJECT__WRITE",
1.232 paf 79: "CALL", "CALL__WRITE",
1.49 paf 80:
1.337 misha 81: #ifdef OPTIMIZE_BYTECODE_CONSTRUCT
1.344 misha 82: "WITH_ROOT__VALUE__CONSTRUCT_EXPR",
83: "WITH_ROOT__VALUE__CONSTRUCT_VALUE",
84: "WITH_WRITE__VALUE__CONSTRUCT_EXPR",
85: "WITH_WRITE__VALUE__CONSTRUCT_VALUE",
1.345 misha 86: "WITH_SELF__VALUE__CONSTRUCT_EXPR",
87: "WITH_SELF__VALUE__CONSTRUCT_VALUE",
1.337 misha 88: #endif
89:
1.372 misha 90: #ifdef OPTIMIZE_BYTECODE_GET_ELEMENT__SPECIAL
91: "GET_ELEMENT__SPECIAL",
92: "GET_ELEMENT__SPECIAL__WRITE",
93: #endif
1.49 paf 94: // expression ops: unary
1.148 paf 95: "NEG", "INV", "NOT", "DEF", "IN", "FEXISTS", "DEXISTS",
1.49 paf 96: // expression ops: binary
1.201 paf 97: "SUB", "ADD", "MUL", "DIV", "MOD", "INTDIV",
1.275 paf 98: "BIN_SL", "BIN_SR",
1.56 paf 99: "BIN_AND", "BIN_OR", "BIN_XOR",
100: "LOG_AND", "LOG_OR", "LOG_XOR",
1.49 paf 101: "NUM_LT", "NUM_GT", "NUM_LE", "NUM_GE", "NUM_EQ", "NUM_NE",
1.103 paf 102: "STR_LT", "STR_GT", "STR_LE", "STR_GE", "STR_EQ", "STR_NE",
103: "IS"
1.1 paf 104: };
105:
1.342 misha 106: const char* debug_value_to_cstr(Value& value){
107: const String* string=value.get_string();
108:
109: if(string)
110: return string->cstr();
111: else
112: if(value.is_bool())
1.386 moko 113: return value.as_bool() ? "<true>" : "<false>";
1.342 misha 114: else
115: return "<value>";
116: }
117:
1.297 paf 118: void va_debug_printf(SAPI_Info& sapi_info, const char* fmt,va_list args) {
1.127 paf 119: char buf[MAX_STRING];
120: vsnprintf(buf, MAX_STRING, fmt, args);
1.297 paf 121: SAPI::log(sapi_info, "%s", buf);
1.107 paf 122: }
123:
1.297 paf 124: void debug_printf(SAPI_Info& sapi_info, const char* fmt, ...) {
1.334 misha 125: va_list args;
126: va_start(args, fmt);
127: va_debug_printf(sapi_info, fmt, args);
128: va_end(args);
1.105 paf 129: }
130:
1.297 paf 131: void debug_dump(SAPI_Info& sapi_info, int level, ArrayOperation& ops) {
1.419 moko 132: for(Array_iterator<Operation> i(ops); i;) {
1.323 misha 133: OP::OPCODE opcode=i.next().code;
1.1 paf 134:
1.337 misha 135: #if defined(OPTIMIZE_BYTECODE_GET_OBJECT_ELEMENT) || defined(OPTIMIZE_BYTECODE_GET_OBJECT_VAR_ELEMENT)
1.334 misha 136: if(
1.335 misha 137: 1==0
138: #ifdef OPTIMIZE_BYTECODE_GET_OBJECT_ELEMENT
139: || opcode==OP::OP_GET_OBJECT_ELEMENT
140: || opcode==OP::OP_GET_OBJECT_ELEMENT__WRITE
1.376 moko 141: #ifdef FEATURE_GET_ELEMENT4CALL
142: || opcode==OP::OP_GET_OBJECT_ELEMENT4CALL
143: #endif
1.335 misha 144: #endif
145: #ifdef OPTIMIZE_BYTECODE_GET_OBJECT_VAR_ELEMENT
146: || opcode==OP::OP_GET_OBJECT_VAR_ELEMENT
147: || opcode==OP::OP_GET_OBJECT_VAR_ELEMENT__WRITE
1.376 moko 148: #ifdef FEATURE_GET_ELEMENT4CALL
149: || opcode==OP::OP_GET_OBJECT_VAR_ELEMENT4CALL
150: #endif
1.335 misha 151: #endif
1.334 misha 152: ){
153: i.next(); // skip origin
154: Value& value1=*i.next().value;
155: i.next(); // skip origin
156: Value& value2=*i.next().value;
1.386 moko 157: debug_printf(sapi_info, "%*s%s \"%s\" \"%s\"", level*4, "", opcode_name[opcode], debug_value_to_cstr(value1), debug_value_to_cstr(value2));
1.334 misha 158: continue;
159: }
160: #endif
1.348 misha 161: if(
162: opcode==OP::OP_CONSTRUCT_OBJECT
163: || opcode==OP::OP_CONSTRUCT_OBJECT__WRITE
164: ){
165: i.next(); // skip origin
166: Value& value1=*i.next().value;
167: i.next(); // skip origin
168: Value& value2=*i.next().value;
169: debug_printf(sapi_info,
170: "%*s%s"
171: " \"%s\" \"%s\"",
172: level*4, "", opcode_name[opcode],
173: debug_value_to_cstr(value1), debug_value_to_cstr(value2));
174:
175: if(ArrayOperation* local_ops=i.next().ops)
176: debug_dump(sapi_info, level+1, *local_ops);
177: continue;
178: }
1.328 misha 179: if(
180: opcode==OP::OP_VALUE
181: || opcode==OP::OP_STRING__WRITE
182: || opcode==OP::OP_VALUE__GET_CLASS
1.383 moko 183: || opcode==OP::OP_VALUE__GET_BASE_CLASS
1.328 misha 184: #ifdef OPTIMIZE_BYTECODE_GET_ELEMENT
185: || opcode==OP::OP_VALUE__GET_ELEMENT
186: || opcode==OP::OP_VALUE__GET_ELEMENT__WRITE
187: || opcode==OP::OP_VALUE__GET_ELEMENT_OR_OPERATOR
1.347 misha 188: || opcode==OP::OP_WITH_ROOT__VALUE__GET_ELEMENT
1.328 misha 189: #endif
1.345 misha 190: #ifdef OPTIMIZE_BYTECODE_GET_SELF_ELEMENT
191: || opcode==OP::OP_WITH_SELF__VALUE__GET_ELEMENT
192: || opcode==OP::OP_WITH_SELF__VALUE__GET_ELEMENT__WRITE
193: #endif
1.342 misha 194: #ifdef OPTIMIZE_BYTECODE_CONSTRUCT
1.344 misha 195: || opcode==OP::OP_WITH_ROOT__VALUE__CONSTRUCT_EXPR
196: || opcode==OP::OP_WITH_ROOT__VALUE__CONSTRUCT_VALUE
197: || opcode==OP::OP_WITH_WRITE__VALUE__CONSTRUCT_EXPR
198: || opcode==OP::OP_WITH_WRITE__VALUE__CONSTRUCT_VALUE
1.345 misha 199: || opcode==OP::OP_WITH_SELF__VALUE__CONSTRUCT_EXPR
200: || opcode==OP::OP_WITH_SELF__VALUE__CONSTRUCT_VALUE
1.342 misha 201: #endif
1.328 misha 202: ) {
1.297 paf 203: Operation::Origin origin=i.next().origin;
204: Value& value=*i.next().value;
1.342 misha 205:
1.297 paf 206: debug_printf(sapi_info,
1.133 paf 207: "%*s%s"
208: " \"%s\" %s",
1.297 paf 209: level*4, "", opcode_name[opcode],
1.342 misha 210: debug_value_to_cstr(value), value.type());
1.133 paf 211: continue;
1.15 paf 212: }
1.348 misha 213:
1.297 paf 214: debug_printf(sapi_info, "%*s%s", level*4, "", opcode_name[opcode]);
1.1 paf 215:
1.297 paf 216: switch(opcode) {
1.323 misha 217: case OP::OP_CURLY_CODE__STORE_PARAM:
218: case OP::OP_EXPR_CODE__STORE_PARAM:
219: case OP::OP_CURLY_CODE__CONSTRUCT:
220: case OP::OP_NESTED_CODE:
1.346 misha 221: case OP::OP_OBJECT_POOL:
1.323 misha 222: case OP::OP_STRING_POOL:
1.421 moko 223: case OP::OP_CONSTRUCT_ARRAY:
1.323 misha 224: case OP::OP_CALL:
225: case OP::OP_CALL__WRITE:
1.297 paf 226: if(ArrayOperation* local_ops=i.next().ops)
227: debug_dump(sapi_info, level+1, *local_ops);
1.1 paf 228: }
229: }
230: }
1.336 misha 231: #define DEBUG_PRINT_STR(str) debug_printf(sapi_info, str);
232: #define DEBUG_PRINT_STRING(value) debug_printf(sapi_info, " \"%s\" ", value.cstr());
1.342 misha 233: #define DEBUG_PRINT_VALUE_AND_TYPE(value) debug_printf(sapi_info, " \"%s\" %s", debug_value_to_cstr(value), value.type());
1.336 misha 234: #define DEBUG_PRINT_OPS(local_ops) \
235: debug_printf(sapi_info, \
236: " (%d)\n", local_ops?local_ops->count():0); \
237: if(local_ops) debug_dump(sapi_info, 1, *local_ops);
238:
239: #else
240: #define DEBUG_PRINT_STR(str)
241: #define DEBUG_PRINT_STRING(value)
1.342 misha 242: #define DEBUG_PRINT_VALUE_AND_TYPE(value)
1.336 misha 243: #define DEBUG_PRINT_OPS(local_ops)
1.157 parser 244: #endif
1.1 paf 245:
1.297 paf 246: // Request
247:
248: void Request::execute(ArrayOperation& ops) {
1.418 moko 249: Stack<StackItem>& stack=this->stack; // helps a lot on MSVC: 'esi'
1.159 parser 250:
1.304 paf 251: const String* debug_name=0; Operation::Origin debug_origin={0, 0, 0};
1.297 paf 252: try{
1.157 parser 253: #ifdef DEBUG_EXECUTE
1.297 paf 254: debug_printf(sapi_info, "source----------------------------\n");
255: debug_dump(sapi_info, 0, ops);
256: debug_printf(sapi_info, "execution-------------------------\n");
1.157 parser 257: #endif
1.419 moko 258: for(Array_iterator<Operation> i(ops); i; ) {
1.322 misha 259: OP::OPCODE opcode=i.next().code;
1.293 paf 260:
1.157 parser 261: #ifdef DEBUG_EXECUTE
1.297 paf 262: debug_printf(sapi_info, "%d:%s", stack.top_index()+1, opcode_name[opcode]);
1.157 parser 263: #endif
1.11 paf 264:
1.297 paf 265: switch(opcode) {
1.51 paf 266: // param in next instruction
1.322 misha 267: case OP::OP_VALUE:
1.32 paf 268: {
1.297 paf 269: debug_origin=i.next().origin;
270: Value& value=*i.next().value;
1.342 misha 271:
272: DEBUG_PRINT_VALUE_AND_TYPE(value)
273:
1.297 paf 274: stack.push(value);
1.32 paf 275: break;
276: }
1.328 misha 277: case OP::OP_VALUE__GET_CLASS:
278: {
1.383 moko 279: debug_origin=i.next().origin;
280: const String& name=*i.next().value->get_string(); debug_name=&name;
281:
282: DEBUG_PRINT_STRING(name)
283:
1.413 moko 284: stack.push(get_class_ref(name));
1.383 moko 285: break;
286: }
287:
288: case OP::OP_VALUE__GET_BASE_CLASS:
289: {
290: // ^class:method[] call
1.328 misha 291:
292: debug_origin=i.next().origin;
1.372 misha 293: const String& name=*i.next().value->get_string(); debug_name=&name;
1.328 misha 294:
1.336 misha 295: DEBUG_PRINT_STRING(name)
1.328 misha 296:
1.413 moko 297: stack.push(*new VBaseClassWrapper(get_class_ref(name), get_self()));
1.328 misha 298: break;
299: }
1.372 misha 300:
301: #ifdef OPTIMIZE_BYTECODE_GET_ELEMENT__SPECIAL
302: case OP::OP_GET_ELEMENT__SPECIAL:
303: case OP::OP_GET_ELEMENT__SPECIAL__WRITE:
304: {
305: const String& name=stack.pop().string(); debug_name=&name;
306: Value& ncontext=stack.pop().value();
307:
308: Value* value=0;
309: if(VStateless_class* vclass=ncontext.get_class()){
1.382 moko 310: if(SYMBOLS_EQ(name,CLASS_SYMBOL)){
1.372 misha 311: value=vclass;
1.382 moko 312: } else if(SYMBOLS_EQ(name,CLASS_NAME_SYMBOL)){
1.379 moko 313: value=new VString(*new String(vclass->type()));
1.372 misha 314: }
315: } else {
1.381 moko 316: // VJunction is without class, returning self
1.382 moko 317: if(SYMBOLS_EQ(name,CLASS_SYMBOL)){
1.381 moko 318: value=&ncontext;
1.382 moko 319: } else if(SYMBOLS_EQ(name,CLASS_NAME_SYMBOL)){
1.381 moko 320: value=new VString(*new String(ncontext.type()));
321: }
1.380 moko 322: }
1.372 misha 323: if(opcode==OP::OP_GET_ELEMENT__SPECIAL){
324: stack.push(*value);
325: } else {
1.396 moko 326: write(*value);
1.372 misha 327: }
328: break;
329: }
330: #endif
331:
1.51 paf 332: // OP_WITH
1.322 misha 333: case OP::OP_WITH_ROOT:
1.187 parser 334: {
1.297 paf 335: stack.push(*method_frame);
1.187 parser 336: break;
337: }
1.336 misha 338: case OP::OP_WITH_SELF:
1.37 paf 339: {
1.297 paf 340: stack.push(get_self());
1.37 paf 341: break;
342: }
1.336 misha 343: case OP::OP_WITH_READ:
1.15 paf 344: {
1.297 paf 345: stack.push(*rcontext);
1.20 paf 346: break;
347: }
1.336 misha 348: case OP::OP_WITH_WRITE:
1.20 paf 349: {
1.287 paf 350: if(wcontext==method_frame)
1.384 moko 351: throw Exception(PARSER_RUNTIME, 0, "$.name outside of $name[...]");
1.287 paf 352:
1.297 paf 353: stack.push(*wcontext);
1.20 paf 354: break;
355: }
1.37 paf 356:
1.51 paf 357: // OTHER ACTIONS BUT WITHs
1.337 misha 358:
359: #ifdef OPTIMIZE_BYTECODE_CONSTRUCT
1.392 moko 360: #define DO_CONSTRUCT(context, vvalue) { \
1.347 misha 361: debug_origin=i.next().origin; \
362: const String& name=*i.next().value->get_string(); debug_name=&name; \
363: DEBUG_PRINT_STRING(name) \
364: Value& value=stack.pop().value(); \
365: put_element( context, name, vvalue ); \
1.403 moko 366: goto check_skip; \
1.347 misha 367: }
368: #define DO_CONSTRUCT_VALUE(context) DO_CONSTRUCT(context, &value)
1.392 moko 369: #define DO_CONSTRUCT_EXPR(context) { \
1.347 misha 370: DO_CONSTRUCT(context, &value.as_expr_result()) \
371: }
372:
1.344 misha 373: case OP::OP_WITH_WRITE__VALUE__CONSTRUCT_EXPR:
1.347 misha 374: {
375: if(wcontext==method_frame)
376: throw Exception(PARSER_RUNTIME, 0, "$.name outside of $name[...]");
377: DO_CONSTRUCT_EXPR(*wcontext)
378: }
379:
1.344 misha 380: case OP::OP_WITH_WRITE__VALUE__CONSTRUCT_VALUE:
1.338 misha 381: {
382: if(wcontext==method_frame)
1.347 misha 383: throw Exception(PARSER_RUNTIME, 0, "$.name outside of $name[...]");
384: DO_CONSTRUCT_VALUE(*wcontext)
1.338 misha 385: }
1.345 misha 386:
1.347 misha 387: case OP::OP_WITH_ROOT__VALUE__CONSTRUCT_EXPR: DO_CONSTRUCT_EXPR(*method_frame)
1.337 misha 388:
1.347 misha 389: case OP::OP_WITH_ROOT__VALUE__CONSTRUCT_VALUE: DO_CONSTRUCT_VALUE(*method_frame)
1.337 misha 390:
1.347 misha 391: case OP::OP_WITH_SELF__VALUE__CONSTRUCT_EXPR: DO_CONSTRUCT_EXPR(get_self())
1.342 misha 392:
1.347 misha 393: case OP::OP_WITH_SELF__VALUE__CONSTRUCT_VALUE: DO_CONSTRUCT_VALUE(get_self())
1.345 misha 394:
1.340 misha 395: #endif // OPTIMIZE_BYTECODE_CONSTRUCT
1.337 misha 396:
1.344 misha 397: case OP::OP_CONSTRUCT_VALUE:
1.337 misha 398: {
1.344 misha 399: #ifdef OPTIMIZE_BYTECODE_CONSTRUCT
400: const String& name=stack.pop().string(); debug_name=&name;
401: Value& ncontext=stack.pop().value();
402: Value& value=stack.pop().value();
403: #else
404: Value& value=stack.pop().value();
405: const String& name=stack.pop().string(); debug_name=&name;
406: Value& ncontext=stack.pop().value();
407: #endif
408: put_element(ncontext, name, &value);
1.403 moko 409: goto check_skip;
1.337 misha 410: }
411:
1.322 misha 412: case OP::OP_CONSTRUCT_EXPR:
1.80 paf 413: {
1.344 misha 414: #ifdef OPTIMIZE_BYTECODE_CONSTRUCT
415: const String& name=stack.pop().string(); debug_name=&name;
416: Value& ncontext=stack.pop().value();
417: Value& expr=stack.pop().value();
418: #else
1.308 paf 419: Value& expr=stack.pop().value();
1.297 paf 420: const String& name=stack.pop().string(); debug_name=&name;
421: Value& ncontext=stack.pop().value();
1.344 misha 422: #endif
1.308 paf 423: Value& value=expr.as_expr_result();
1.329 misha 424: put_element(ncontext, name, &value);
1.403 moko 425: goto check_skip;
1.80 paf 426: }
1.337 misha 427:
1.322 misha 428: case OP::OP_CURLY_CODE__CONSTRUCT:
1.182 parser 429: {
1.297 paf 430: ArrayOperation& local_ops=*i.next().ops;
1.336 misha 431:
432: DEBUG_PRINT_OPS((&local_ops))
433:
1.326 misha 434: VJunction& value=*new VJunction(
1.297 paf 435: get_self(), 0,
1.257 paf 436: method_frame,
1.240 paf 437: rcontext,
438: wcontext,
1.312 paf 439: &local_ops);
1.238 paf 440:
1.326 misha 441: wcontext->attach_junction(&value);
442:
1.297 paf 443: const String& name=stack.pop().string(); debug_name=&name;
444: Value& ncontext=stack.pop().value();
1.371 moko 445: if(const VJunction* vjunction=ncontext.put_element(name, &value))
1.422 moko 446: throw Exception(PARSER_RUNTIME, 0, "property value cannot be code, use [] or () brackets");
1.403 moko 447:
1.182 parser 448: break;
449: }
1.421 moko 450:
451: case OP::OP_CONSTRUCT_ARRAY:
452: {
453: ArrayOperation* local_ops=i.next().ops;
454:
455: DEBUG_PRINT_OPS(local_ops)
456: DEBUG_PRINT_STR("->\n")
457:
458: size_t first = stack.top_index();
459: execute(*local_ops);
460: Value *value=new VArray(stack.top_index()-first, (Value**)stack.ptr(first));
461: stack.set_top_index(first);
462:
463: #ifdef OPTIMIZE_BYTECODE_CONSTRUCT
464: const String& name=stack.pop().string(); debug_name=&name;
465: Value& ncontext=stack.pop().value();
466: #else
467: const String& name=stack.pop().string(); debug_name=&name;
468: Value& ncontext=stack.pop().value();
469: #endif
470: put_element(ncontext, name, value);
471:
472: DEBUG_PRINT_STR("<-returned")
473: goto check_skip;
474: }
475:
1.322 misha 476: case OP::OP_NESTED_CODE:
1.209 paf 477: {
1.297 paf 478: ArrayOperation& local_ops=*i.next().ops;
1.336 misha 479:
480: DEBUG_PRINT_OPS((&local_ops))
481:
1.297 paf 482: stack.push(local_ops);
1.209 paf 483: break;
484: }
1.322 misha 485: case OP::OP_WRITE_VALUE:
1.13 paf 486: {
1.297 paf 487: Value& value=stack.pop().value();
1.396 moko 488: write(value);
1.86 paf 489: break;
490: }
1.322 misha 491: case OP::OP_WRITE_EXPR_RESULT:
1.108 paf 492: {
1.351 misha 493: Value& value=stack.pop().value();
494: wcontext->write(value.as_expr_result());
1.108 paf 495: break;
496: }
1.322 misha 497: case OP::OP_STRING__WRITE:
1.86 paf 498: {
1.297 paf 499: i.next(); // ignore origin
500: Value* value=i.next().value;
1.336 misha 501:
502: const String& string_value=*value->get_string();
503:
504: DEBUG_PRINT_STRING(string_value)
505:
1.396 moko 506: write(string_value);
1.13 paf 507: break;
1.14 paf 508: }
1.328 misha 509:
510: #ifdef OPTIMIZE_BYTECODE_GET_ELEMENT
511: case OP::OP_VALUE__GET_ELEMENT_OR_OPERATOR:
512: {
513: debug_origin=i.next().origin;
514: const String& name=*i.next().value->get_string(); debug_name=&name;
515:
1.336 misha 516: DEBUG_PRINT_STRING(name)
517:
1.330 misha 518: if(Method* method=main_class.get_method(name)){ // looking operator of that name FIRST
519: if(!method->junction_template) method->junction_template=new VJunction(main_class, method);
520: stack.push(*method->junction_template);
521: break;
522: }
523: Value& value=get_element(*rcontext, name);
1.328 misha 524: stack.push(value);
1.403 moko 525: goto check_skip;
1.328 misha 526: }
527: #else
1.322 misha 528: case OP::OP_GET_ELEMENT_OR_OPERATOR:
1.215 paf 529: {
1.297 paf 530: const String& name=stack.pop().string(); debug_name=&name;
531: Value& ncontext=stack.pop().value();
1.330 misha 532: if(Method* method=main_class.get_method(name)){ // looking operator of that name FIRST
533: if(!method->junction_template) method->junction_template=new VJunction(main_class, method);
534: stack.push(*method->junction_template);
535: break;
536: }
537: Value& value=get_element(ncontext, name);
1.297 paf 538: stack.push(value);
1.403 moko 539: goto check_skip;
1.215 paf 540: }
1.328 misha 541: #endif
542:
1.335 misha 543: #ifdef OPTIMIZE_BYTECODE_GET_OBJECT_ELEMENT
1.336 misha 544:
1.376 moko 545: #define DO_GET_OBJECT_ELEMENT(code) {\
546: debug_origin=i.next().origin; \
547: const String& context_name=*i.next().value->get_string(); debug_name=&context_name; \
548: DEBUG_PRINT_STRING(context_name) \
549: Value& object=get_element(*rcontext, context_name); \
550: debug_origin=i.next().origin; \
551: const String& field_name=*i.next().value->get_string(); debug_name=&field_name; \
552: DEBUG_PRINT_STRING(field_name) \
553: code; \
1.403 moko 554: goto check_skip; \
1.376 moko 555: }
1.336 misha 556:
1.376 moko 557: case OP::OP_GET_OBJECT_ELEMENT: DO_GET_OBJECT_ELEMENT({
558: Value& value=get_element(object, field_name);
559: stack.push(value);
560: })
561: case OP::OP_GET_OBJECT_ELEMENT__WRITE: DO_GET_OBJECT_ELEMENT({
1.334 misha 562: Value& value=get_element(object, field_name);
1.396 moko 563: write(value);
1.376 moko 564: })
565: #ifdef FEATURE_GET_ELEMENT4CALL
566: case OP::OP_GET_OBJECT_ELEMENT4CALL: DO_GET_OBJECT_ELEMENT({
567: Value& value=get_element4call(object, field_name);
568: stack.push(value);
569: })
570: #endif
1.335 misha 571: #endif
572:
573: #ifdef OPTIMIZE_BYTECODE_GET_OBJECT_VAR_ELEMENT
1.336 misha 574:
1.376 moko 575: #define DO_GET_OBJECT_VAR_ELEMENT(code) {\
576: debug_origin=i.next().origin; \
577: const String& context_name=*i.next().value->get_string(); debug_name=&context_name; \
578: DEBUG_PRINT_STRING(context_name) \
579: Value& object=get_element(*rcontext, context_name); \
580: debug_origin=i.next().origin; \
581: const String& var_name=*i.next().value->get_string(); debug_name=&var_name; \
582: DEBUG_PRINT_STRING(var_name) \
583: const String* field=&get_element(*rcontext, var_name).as_string(); \
584: code; \
1.403 moko 585: goto check_skip; \
1.376 moko 586: }
1.335 misha 587:
1.376 moko 588: case OP::OP_GET_OBJECT_VAR_ELEMENT: DO_GET_OBJECT_VAR_ELEMENT({
589: Value& value=get_element(object, *field);
590: stack.push(value);
591: })
592: case OP::OP_GET_OBJECT_VAR_ELEMENT__WRITE: DO_GET_OBJECT_VAR_ELEMENT({
1.335 misha 593: Value& value=get_element(object, *field);
1.396 moko 594: write(value);
1.376 moko 595: })
596: #ifdef FEATURE_GET_ELEMENT4CALL
597: case OP::OP_GET_OBJECT_VAR_ELEMENT4CALL: DO_GET_OBJECT_VAR_ELEMENT({
598: Value& value=get_element4call(object, *field);
599: stack.push(value);
600: })
601: #endif
602: #endif
1.335 misha 603:
1.376 moko 604: case OP::OP_GET_ELEMENT:
605: {
606: const String& name=stack.pop().string(); debug_name=&name;
607: Value& ncontext=stack.pop().value();
608: Value& value=get_element(ncontext, name);
609: stack.push(value);
1.403 moko 610: goto check_skip;
1.334 misha 611: }
612:
1.376 moko 613: #ifdef FEATURE_GET_ELEMENT4CALL
614: case OP::OP_GET_ELEMENT4CALL:
1.11 paf 615: {
1.297 paf 616: const String& name=stack.pop().string(); debug_name=&name;
617: Value& ncontext=stack.pop().value();
1.376 moko 618: Value& value=get_element4call(ncontext, name);
1.297 paf 619: stack.push(value);
1.403 moko 620: goto check_skip;
1.17 paf 621: }
1.376 moko 622: #endif
1.17 paf 623:
1.346 misha 624: case OP::OP_GET_ELEMENT__WRITE:
625: {
626: const String& name=stack.pop().string(); debug_name=&name;
627: Value& ncontext=stack.pop().value();
628: Value& value=get_element(ncontext, name);
1.396 moko 629: write(value);
1.403 moko 630: goto check_skip;
1.346 misha 631: }
632:
1.328 misha 633: #ifdef OPTIMIZE_BYTECODE_GET_ELEMENT
634: case OP::OP_VALUE__GET_ELEMENT:
635: {
636: debug_origin=i.next().origin;
637: const String& name=*i.next().value->get_string(); debug_name=&name;
638:
1.336 misha 639: DEBUG_PRINT_STRING(name)
1.328 misha 640:
1.330 misha 641: Value& value=get_element(*rcontext, name);
1.328 misha 642: stack.push(value);
1.403 moko 643: goto check_skip;
1.328 misha 644: }
645:
646: case OP::OP_VALUE__GET_ELEMENT__WRITE:
647: {
648: debug_origin=i.next().origin;
649: const String& name=*i.next().value->get_string(); debug_name=&name;
650:
1.336 misha 651: DEBUG_PRINT_STRING(name)
1.328 misha 652:
1.330 misha 653: Value& value=get_element(*rcontext, name);
1.396 moko 654: write(value);
1.403 moko 655: goto check_skip;
1.328 misha 656: }
1.347 misha 657:
658: case OP::OP_WITH_ROOT__VALUE__GET_ELEMENT:
659: {
660: debug_origin=i.next().origin;
661: const String& name=*i.next().value->get_string(); debug_name=&name;
662:
663: DEBUG_PRINT_STRING(name)
664:
665: Value& value=get_element(*method_frame, name);
666: stack.push(value);
1.403 moko 667: goto check_skip;
1.347 misha 668: }
1.328 misha 669: #endif
1.32 paf 670:
1.345 misha 671: #ifdef OPTIMIZE_BYTECODE_GET_SELF_ELEMENT
672: case OP::OP_WITH_SELF__VALUE__GET_ELEMENT:
673: case OP::OP_WITH_SELF__VALUE__GET_ELEMENT__WRITE:
674: {
675: debug_origin=i.next().origin;
676: const String& name=*i.next().value->get_string(); debug_name=&name;
677:
678: DEBUG_PRINT_STRING(name)
679:
680: Value& value=get_element(get_self(), name);
681:
682: if(opcode==OP::OP_WITH_SELF__VALUE__GET_ELEMENT){
683: stack.push(value);
684: } else {
1.396 moko 685: write(value);
1.345 misha 686: }
1.403 moko 687: goto check_skip;
1.345 misha 688: }
689: #endif
690:
1.322 misha 691: case OP::OP_OBJECT_POOL:
1.17 paf 692: {
1.406 moko 693: debug_name=0;
694:
1.297 paf 695: ArrayOperation& local_ops=*i.next().ops;
1.226 paf 696:
1.257 paf 697: WContext *saved_wcontext=wcontext;
1.348 misha 698: #ifdef OPTIMIZE_SINGLE_STRING_WRITE
1.351 misha 699: WObjectPoolWrapper local(wcontext);
1.348 misha 700: #else
1.351 misha 701: WWrapper local(wcontext);
1.348 misha 702: #endif
1.226 paf 703: wcontext=&local;
704:
1.297 paf 705: execute(local_ops);
1.226 paf 706:
1.400 moko 707: stack.push((Value&)wcontext->result());
1.403 moko 708:
1.257 paf 709: wcontext=saved_wcontext;
1.403 moko 710: goto check_skip;
1.15 paf 711: }
1.13 paf 712:
1.322 misha 713: case OP::OP_STRING_POOL:
1.49 paf 714: {
1.297 paf 715: ArrayOperation& local_ops=*i.next().ops;
1.226 paf 716:
1.257 paf 717: WContext *saved_wcontext=wcontext;
1.351 misha 718: WWrapper local(wcontext);
1.226 paf 719: wcontext=&local;
720:
1.297 paf 721: execute(local_ops);
1.226 paf 722:
1.49 paf 723: // from "$a $b" part of expression taking only string value,
724: // ignoring any other content of wcontext
1.367 moko 725: const String* string=wcontext->get_string();
726: Value* value=string ? new VString(*string) : new VString();
1.297 paf 727: stack.push(*value);
728:
1.257 paf 729: wcontext=saved_wcontext;
1.403 moko 730: goto check_skip;
1.49 paf 731: }
732:
1.51 paf 733: // CALL
1.322 misha 734: case OP::OP_CURLY_CODE__STORE_PARAM:
735: case OP::OP_EXPR_CODE__STORE_PARAM:
1.28 paf 736: {
1.237 paf 737: // code
1.297 paf 738: ArrayOperation& local_ops=*i.next().ops;
1.336 misha 739:
740: DEBUG_PRINT_OPS((&local_ops))
741:
1.237 paf 742: // when they evaluate expression parameter,
743: // the object expression result
744: // does not need to be written into calling frame
745: // it must go into any expressions using that parameter
746: // hence, we zero junction.wcontext here, and later
747: // in .process we would test that field
748: // in decision "which wwrapper to use"
1.326 misha 749: VJunction& value=*new VJunction(
1.297 paf 750: get_self(), 0,
1.257 paf 751: method_frame,
1.237 paf 752: rcontext,
1.322 misha 753: opcode==OP::OP_EXPR_CODE__STORE_PARAM?0:wcontext,
1.312 paf 754: &local_ops);
1.343 misha 755: #ifndef USE_DESTRUCTORS
1.326 misha 756: if (opcode!=OP::OP_EXPR_CODE__STORE_PARAM)
1.343 misha 757: wcontext->attach_junction(&value);
1.326 misha 758: #endif
1.237 paf 759: // store param
1.329 misha 760: stack.push(value);
1.29 paf 761: break;
762: }
763:
1.397 moko 764: #define METHOD_PARAMS_ACTION(action) \
1.386 moko 765: if(local_ops){ \
766: size_t first = stack.top_index(); \
767: execute(*local_ops); \
1.403 moko 768: if(!fskip){ \
769: frame.store_params((Value**)stack.ptr(first), stack.top_index()-first); \
770: action; \
771: } \
1.386 moko 772: stack.set_top_index(first); \
773: } else { \
774: frame.empty_params(); \
775: action; \
1.350 misha 776: }
777:
1.322 misha 778: case OP::OP_CALL:
1.29 paf 779: {
1.297 paf 780: ArrayOperation* local_ops=i.next().ops;
1.237 paf 781:
1.336 misha 782: DEBUG_PRINT_OPS(local_ops)
783: DEBUG_PRINT_STR("->\n")
784:
1.297 paf 785: Value& value=stack.pop().value();
1.237 paf 786:
1.297 paf 787: Junction* junction=value.get_junction();
788: if(!junction) {
1.420 moko 789: if(dynamic_cast<VVoid*>(&value))
1.386 moko 790: throw Exception(PARSER_RUNTIME, 0, "undefined method");
1.297 paf 791: else
1.422 moko 792: throw Exception(PARSER_RUNTIME, 0, "is '%s', not a method or junction, cannot call it", value.type());
1.297 paf 793: }
1.237 paf 794:
1.370 moko 795: Value *result;
796: {
1.397 moko 797: EXPRESSION_FRAME_ACTION(*junction->method, method_frame, junction->self, {
798: METHOD_PARAMS_ACTION(call(frame));
799: result=&frame.result();
800: // desctructor deletes junctions in stack params here
801: });
1.370 moko 802: }
803: stack.push(*result);
1.332 misha 804:
1.336 misha 805: DEBUG_PRINT_STR("<-returned")
1.403 moko 806: goto check_skip;
1.332 misha 807: }
1.220 paf 808:
1.332 misha 809: case OP::OP_CALL__WRITE:
810: {
811: ArrayOperation* local_ops=i.next().ops;
812:
1.336 misha 813: DEBUG_PRINT_OPS(local_ops)
814: DEBUG_PRINT_STR("->\n")
815:
1.332 misha 816: Value& value=stack.pop().value();
817:
818: Junction* junction=value.get_junction();
819: if(!junction) {
1.420 moko 820: if(dynamic_cast<VVoid*>(&value))
1.386 moko 821: throw Exception(PARSER_RUNTIME, 0, "undefined method");
1.332 misha 822: else
1.422 moko 823: throw Exception(PARSER_RUNTIME, 0, "is '%s', not a method or junction, cannot call it", value.type());
1.332 misha 824: }
825:
1.391 moko 826: const Method& method=*junction->method;
1.332 misha 827: #ifdef OPTIMIZE_CALL
1.353 misha 828: if(method.call_optimization==Method::CO_WITHOUT_FRAME){
1.332 misha 829: if(local_ops){ // store param code goes here
830: size_t first = stack.top_index();
831: execute(*local_ops);
832:
1.403 moko 833: if(!fskip){
834: MethodParams method_params;
835: method_params.store_params((Value**)stack.ptr(first), stack.top_index()-first);
836: method.check_actual_numbered_params(junction->self, &method_params);
837: method.native_code(*this, method_params); // execute it
838: }
1.332 misha 839:
840: stack.set_top_index(first);
841: } else {
842: MethodParams method_params;
843: method.check_actual_numbered_params(junction->self, &method_params);
844: method.native_code(*this, method_params); // execute it
845: }
1.353 misha 846: } else if(method.call_optimization==Method::CO_WITHOUT_WCONTEXT){
1.397 moko 847: METHOD_FRAME_ACTION(method, method_frame, junction->self, {
848: METHOD_PARAMS_ACTION(call_write(frame))
849: });
1.332 misha 850: } else
851: #endif // OPTIMIZE_CALL
852: {
1.397 moko 853: METHOD_FRAME_ACTION(method, method_frame, junction->self, {
854: METHOD_PARAMS_ACTION(call(frame));
855: write(frame.result());
856: });
1.332 misha 857: }
1.336 misha 858:
859: DEBUG_PRINT_STR("<-returned")
1.403 moko 860: goto check_skip;
1.49 paf 861: }
862:
1.348 misha 863: case OP::OP_CONSTRUCT_OBJECT:
864: case OP::OP_CONSTRUCT_OBJECT__WRITE:
865: {
866: debug_origin=i.next().origin;
867: Value& vclass_name=*i.next().value;
1.360 pretende 868: const String& class_name=*vclass_name.get_string(); debug_name=&class_name;
1.348 misha 869:
870: DEBUG_PRINT_STRING(class_name)
871:
1.413 moko 872: VStateless_class& vclass=get_class_ref(class_name);
1.348 misha 873:
874: debug_origin=i.next().origin;
875: Value& vconstructor_name=*i.next().value;
876: const String& constructor_name=*vconstructor_name.get_string(); debug_name=&constructor_name;
877:
878: DEBUG_PRINT_STRING(constructor_name)
879:
1.424 ! moko 880: // not get_element4call for better error reporting
1.413 moko 881: Junction* constructor_junction=get_element(vclass, constructor_name).get_junction();
1.363 moko 882: if(!constructor_junction)
1.424 ! moko 883: throw Exception(PARSER_RUNTIME, &constructor_name, "%s constructor not found", vclass.type());
1.350 misha 884:
1.348 misha 885: ArrayOperation* local_ops=i.next().ops;
886: DEBUG_PRINT_OPS(local_ops)
887: DEBUG_PRINT_STR("->\n")
888:
1.370 moko 889: Value *result;
890: {
1.413 moko 891: Value& object=construct(vclass, *constructor_junction->method);
1.397 moko 892: CONSTRUCTOR_FRAME_ACTION(*constructor_junction->method, method_frame, object, {
893: METHOD_PARAMS_ACTION(call(frame));
894: object.enable_default_setter();
895: result=&frame.result();
896: // desctructor deletes junctions in stack params here
897: });
1.370 moko 898: }
1.355 misha 899:
1.350 misha 900: if(opcode==OP::OP_CONSTRUCT_OBJECT)
1.370 moko 901: stack.push(*result);
1.350 misha 902: else
1.396 moko 903: write(*result);
1.348 misha 904:
905: DEBUG_PRINT_STR("<-returned")
1.403 moko 906: goto check_skip;
1.348 misha 907: }
908:
1.55 paf 909: // expression ops: unary
1.322 misha 910: case OP::OP_NEG:
1.55 paf 911: {
1.297 paf 912: Value& a=stack.pop().value();
913: Value& value=*new VDouble(-a.as_double());
914: stack.push(value);
1.55 paf 915: break;
916: }
1.322 misha 917: case OP::OP_INV:
1.55 paf 918: {
1.297 paf 919: Value& a=stack.pop().value();
920: Value& value=*new VDouble(~a.as_int());
921: stack.push(value);
1.55 paf 922: break;
923: }
1.322 misha 924: case OP::OP_NOT:
1.55 paf 925: {
1.297 paf 926: Value& a=stack.pop().value();
1.327 misha 927: Value& value=VBool::get(!a.as_bool());
1.297 paf 928: stack.push(value);
1.55 paf 929: break;
930: }
1.322 misha 931: case OP::OP_DEF:
1.62 paf 932: {
1.297 paf 933: Value& a=stack.pop().value();
1.327 misha 934: Value& value=VBool::get(a.is_defined());
1.297 paf 935: stack.push(value);
1.62 paf 936: break;
937: }
1.322 misha 938: case OP::OP_IN:
1.62 paf 939: {
1.297 paf 940: Value& a=stack.pop().value();
941: const String& path=a.as_string();
1.327 misha 942: Value& value=VBool::get(request_info.uri && *request_info.uri && path.this_starts(request_info.uri));
1.297 paf 943: stack.push(value);
1.62 paf 944: break;
945: }
1.322 misha 946: case OP::OP_FEXISTS:
1.62 paf 947: {
1.297 paf 948: Value& a=stack.pop().value();
1.411 moko 949: Value& value=VBool::get(file_exist(full_disk_path(a.as_string())));
1.297 paf 950: stack.push(value);
1.148 paf 951: break;
952: }
1.322 misha 953: case OP::OP_DEXISTS:
1.148 paf 954: {
1.297 paf 955: Value& a=stack.pop().value();
1.411 moko 956: Value& value=VBool::get(dir_exists(full_disk_path(a.as_string())));
1.297 paf 957: stack.push(value);
1.62 paf 958: break;
959: }
1.55 paf 960:
961: // expression ops: binary
1.415 moko 962: case OP::OP_SUB:
1.53 paf 963: {
1.416 moko 964: volatile double b_double=stack.pop().value().as_double();
965: volatile double a_double=stack.pop().value().as_double();
1.415 moko 966:
967: Value& value=*new VDouble(a_double - b_double);
1.297 paf 968: stack.push(value);
1.53 paf 969: break;
970: }
1.415 moko 971: case OP::OP_ADD:
1.53 paf 972: {
1.416 moko 973: volatile double b_double=stack.pop().value().as_double();
974: volatile double a_double=stack.pop().value().as_double();
1.415 moko 975:
976: Value& value=*new VDouble(a_double + b_double);
1.297 paf 977: stack.push(value);
1.53 paf 978: break;
979: }
1.415 moko 980: case OP::OP_MUL:
1.49 paf 981: {
1.416 moko 982: volatile double b_double=stack.pop().value().as_double();
983: volatile double a_double=stack.pop().value().as_double();
1.415 moko 984:
985: Value& value=*new VDouble(a_double * b_double);
1.297 paf 986: stack.push(value);
1.53 paf 987: break;
988: }
1.415 moko 989: case OP::OP_DIV:
1.53 paf 990: {
1.416 moko 991: volatile double b_double=stack.pop().value().as_double();
992: volatile double a_double=stack.pop().value().as_double();
1.170 parser 993:
1.171 parser 994: if(b_double == 0) {
1.297 paf 995: //const String* problem_source=b.as_string();
1.386 moko 996: throw Exception("number.zerodivision", 0, "Division by zero");
1.171 parser 997: }
1.170 parser 998:
1.297 paf 999: Value& value=*new VDouble(a_double / b_double);
1000: stack.push(value);
1.54 paf 1001: break;
1002: }
1.415 moko 1003: case OP::OP_MOD:
1.55 paf 1004: {
1.416 moko 1005: volatile double b_double=stack.pop().value().as_double();
1006: volatile double a_double=stack.pop().value().as_double();
1.170 parser 1007:
1.173 parser 1008: if(b_double == 0) {
1.297 paf 1009: //const String* problem_source=b.as_string();
1.386 moko 1010: throw Exception("number.zerodivision", 0, "Modulus by zero");
1.171 parser 1011: }
1.170 parser 1012:
1.297 paf 1013: Value& value=*new VDouble(fmod(a_double, b_double));
1014: stack.push(value);
1.201 paf 1015: break;
1016: }
1.322 misha 1017: case OP::OP_INTDIV:
1.201 paf 1018: {
1.416 moko 1019: int b_int=stack.pop().value().as_int();
1020: int a_int=stack.pop().value().as_int();
1.201 paf 1021:
1022: if(b_int == 0) {
1.297 paf 1023: //const String* problem_source=b.as_string();
1.386 moko 1024: throw Exception("number.zerodivision", 0, "Division by zero");
1.201 paf 1025: }
1026:
1.297 paf 1027: Value& value=*new VInt(a_int / b_int);
1028: stack.push(value);
1.55 paf 1029: break;
1030: }
1.322 misha 1031: case OP::OP_BIN_SL:
1.274 paf 1032: {
1.297 paf 1033: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1034: Value& value=*new VInt(
1035: a.as_int() <<
1036: b.as_int());
1037: stack.push(value);
1.274 paf 1038: break;
1039: }
1.322 misha 1040: case OP::OP_BIN_SR:
1.274 paf 1041: {
1.297 paf 1042: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1043: Value& value=*new VInt(
1044: a.as_int() >>
1045: b.as_int());
1046: stack.push(value);
1.274 paf 1047: break;
1048: }
1.322 misha 1049: case OP::OP_BIN_AND:
1.54 paf 1050: {
1.297 paf 1051: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1052: Value& value=*new VInt(
1053: a.as_int() &
1054: b.as_int());
1055: stack.push(value);
1.54 paf 1056: break;
1057: }
1.322 misha 1058: case OP::OP_BIN_OR:
1.54 paf 1059: {
1.297 paf 1060: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1061: Value& value=*new VInt(
1062: a.as_int() |
1063: b.as_int());
1064: stack.push(value);
1.55 paf 1065: break;
1066: }
1.322 misha 1067: case OP::OP_BIN_XOR:
1.56 paf 1068: {
1.297 paf 1069: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1070: Value& value=*new VInt(
1071: a.as_int() ^
1072: b.as_int());
1073: stack.push(value);
1.56 paf 1074: break;
1075: }
1.322 misha 1076: case OP::OP_LOG_AND:
1.55 paf 1077: {
1.297 paf 1078: ArrayOperation& local_ops=stack.pop().ops(); Value& a=stack.pop().value();
1.263 paf 1079: bool result;
1.297 paf 1080: if(a.as_bool()) {
1081: execute(local_ops);
1082: Value& b=stack.pop().value();
1083: result=b.as_bool();
1.209 paf 1084: } else
1.263 paf 1085: result=false;
1.327 misha 1086: Value& value=VBool::get(result);
1.297 paf 1087: stack.push(value);
1.403 moko 1088: goto check_skip;
1.55 paf 1089: }
1.322 misha 1090: case OP::OP_LOG_OR:
1.55 paf 1091: {
1.297 paf 1092: ArrayOperation& local_ops=stack.pop().ops(); Value& a=stack.pop().value();
1.263 paf 1093: bool result;
1.297 paf 1094: if(a.as_bool())
1.263 paf 1095: result=true;
1.209 paf 1096: else {
1.297 paf 1097: execute(local_ops);
1098: Value& b=stack.pop().value();
1099: result=b.as_bool();
1.209 paf 1100: }
1.327 misha 1101: Value& value=VBool::get(result);
1.297 paf 1102: stack.push(value);
1.403 moko 1103: goto check_skip;
1.56 paf 1104: }
1.322 misha 1105: case OP::OP_LOG_XOR:
1.56 paf 1106: {
1.297 paf 1107: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1.327 misha 1108: Value& value=VBool::get(a.as_bool() ^ b.as_bool());
1.297 paf 1109: stack.push(value);
1.55 paf 1110: break;
1111: }
1.322 misha 1112: case OP::OP_NUM_LT:
1.55 paf 1113: {
1.297 paf 1114: volatile double b_double=stack.pop().value().as_double();
1115: volatile double a_double=stack.pop().value().as_double();
1.327 misha 1116: Value& value=VBool::get(a_double<b_double);
1.297 paf 1117: stack.push(value);
1.55 paf 1118: break;
1119: }
1.322 misha 1120: case OP::OP_NUM_GT:
1.55 paf 1121: {
1.297 paf 1122: volatile double b_double=stack.pop().value().as_double();
1123: volatile double a_double=stack.pop().value().as_double();
1.327 misha 1124: Value& value=VBool::get(a_double>b_double);
1.297 paf 1125: stack.push(value);
1.55 paf 1126: break;
1127: }
1.322 misha 1128: case OP::OP_NUM_LE:
1.55 paf 1129: {
1.297 paf 1130: volatile double b_double=stack.pop().value().as_double();
1131: volatile double a_double=stack.pop().value().as_double();
1.327 misha 1132: Value& value=VBool::get(a_double<=b_double);
1.297 paf 1133: stack.push(value);
1.55 paf 1134: break;
1135: }
1.322 misha 1136: case OP::OP_NUM_GE:
1.55 paf 1137: {
1.297 paf 1138: volatile double b_double=stack.pop().value().as_double();
1139: volatile double a_double=stack.pop().value().as_double();
1.327 misha 1140: Value& value=VBool::get(a_double>=b_double);
1.297 paf 1141: stack.push(value);
1.55 paf 1142: break;
1143: }
1.322 misha 1144: case OP::OP_NUM_EQ:
1.55 paf 1145: {
1.297 paf 1146: volatile double b_double=stack.pop().value().as_double();
1147: volatile double a_double=stack.pop().value().as_double();
1.327 misha 1148: Value& value=VBool::get(a_double==b_double);
1.297 paf 1149: stack.push(value);
1.55 paf 1150: break;
1151: }
1.322 misha 1152: case OP::OP_NUM_NE:
1.55 paf 1153: {
1.297 paf 1154: volatile double b_double=stack.pop().value().as_double();
1155: volatile double a_double=stack.pop().value().as_double();
1.327 misha 1156: Value& value=VBool::get(a_double!=b_double);
1.297 paf 1157: stack.push(value);
1.54 paf 1158: break;
1159: }
1.322 misha 1160: case OP::OP_STR_LT:
1.58 paf 1161: {
1.297 paf 1162: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1.327 misha 1163: Value& value=VBool::get(a.as_string() < b.as_string());
1.297 paf 1164: stack.push(value);
1.58 paf 1165: break;
1166: }
1.322 misha 1167: case OP::OP_STR_GT:
1.58 paf 1168: {
1.297 paf 1169: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1.327 misha 1170: Value& value=VBool::get(a.as_string() > b.as_string());
1.297 paf 1171: stack.push(value);
1.58 paf 1172: break;
1173: }
1.322 misha 1174: case OP::OP_STR_LE:
1.58 paf 1175: {
1.297 paf 1176: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1.327 misha 1177: Value& value=VBool::get(a.as_string() <= b.as_string());
1.297 paf 1178: stack.push(value);
1.58 paf 1179: break;
1180: }
1.322 misha 1181: case OP::OP_STR_GE:
1.54 paf 1182: {
1.297 paf 1183: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1.327 misha 1184: Value& value=VBool::get(a.as_string() >= b.as_string());
1.297 paf 1185: stack.push(value);
1.58 paf 1186: break;
1187: }
1.322 misha 1188: case OP::OP_STR_EQ:
1.58 paf 1189: {
1.297 paf 1190: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1.327 misha 1191: Value& value=VBool::get(a.as_string() == b.as_string());
1.297 paf 1192: stack.push(value);
1.58 paf 1193: break;
1194: }
1.322 misha 1195: case OP::OP_STR_NE:
1.58 paf 1196: {
1.297 paf 1197: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1.327 misha 1198: Value& value=VBool::get(a.as_string() != b.as_string());
1.297 paf 1199: stack.push(value);
1.103 paf 1200: break;
1201: }
1.322 misha 1202: case OP::OP_IS:
1.103 paf 1203: {
1.297 paf 1204: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1.327 misha 1205: Value& value=VBool::get(a.is(b.as_string().cstr()));
1.297 paf 1206: stack.push(value);
1.28 paf 1207: break;
1208: }
1.11 paf 1209: default:
1.403 moko 1210: {
1211: throw Exception(0, 0, "invalid opcode %d", opcode);
1212: }
1213: check_skip:
1214: {
1215: if(fskip){
1216: if(fskip==Request::SKIP_INTERRUPTED){
1217: set_skip(Request::SKIP_NOTHING);
1218: throw Exception("parser.interrupted", 0, "execution stopped");
1219: }
1220: return;
1221: }
1222: break;
1223: }
1.11 paf 1224: }
1225: }
1.306 paf 1226: } catch(const Exception&) {
1.297 paf 1227: // record it to stack trace
1.301 paf 1228: if(debug_name)
1229: exception_trace.push(Trace(debug_name, debug_origin));
1.297 paf 1230: rethrow;
1231: }
1.1 paf 1232: }
1.17 paf 1233:
1.330 misha 1234: #define SAVE_CONTEXT \
1.386 moko 1235: VMethodFrame *saved_method_frame=method_frame; \
1236: Value* saved_rcontext=rcontext; \
1.330 misha 1237: WContext *saved_wcontext=wcontext;
1238:
1.386 moko 1239: #define RESTORE_CONTEXT \
1240: wcontext=saved_wcontext; \
1241: rcontext=saved_rcontext; \
1.330 misha 1242: method_frame=saved_method_frame;
1243:
1.384 moko 1244: Value& Request::construct(VStateless_class &called_class, const Method &method){
1.363 moko 1245:
1246: if(method.call_type!=Method::CT_STATIC) {
1247: // this is a constructor call
1248: if(Value* result=called_class.create_new_value(fpool)) {
1.319 misha 1249: // some stateless_class creatable derivates
1.363 moko 1250: return *result;
1251: } else
1.401 moko 1252: throw Exception(PARSER_RUNTIME, method.name,
1253: "is not a constructor, system class '%s' can be constructed only implicitly", called_class.type());
1.363 moko 1254: } else
1.422 moko 1255: throw Exception(PARSER_RUNTIME, method.name, "method of '%s' is static and cannot be used as constructor", called_class.type());
1.363 moko 1256: }
1.319 misha 1257:
1.330 misha 1258: Value& Request::get_element(Value& ncontext, const String& name) {
1.376 moko 1259: Value* value=ncontext.get_element(name);
1.388 moko 1260: return *(value ? &process(*value) : VVoid::get());
1.376 moko 1261: }
1262:
1.383 moko 1263: #ifdef FEATURE_GET_ELEMENT4CALL
1.376 moko 1264: Value& Request::get_element4call(Value& ncontext, const String& name) {
1265: Value* value=ncontext.get_element4call(name);
1.388 moko 1266: return *(value ? &process(*value) : VVoid::get());
1.309 paf 1267: }
1.383 moko 1268: #endif
1.309 paf 1269:
1.329 misha 1270: void Request::put_element(Value& ncontext, const String& name, Value* value) {
1.309 paf 1271: // put_element can return property-setting-junction
1.407 moko 1272: if(const VJunction* vjunction=ncontext.put_element(name, value)) {
1273: const Junction& junction = vjunction->junction();
1274: int param_count=junction.method->params_count;
1275:
1276: if(junction.auto_name) {
1277: // default setter
1278: if(param_count!=2)
1279: throw Exception(PARSER_RUNTIME, 0, "default setter method must have TWO parameters (has %d parameters)", param_count);
1280:
1281: Value* params[2] = { new VString(*junction.auto_name), value };
1282:
1283: CONSTRUCTOR_FRAME_ACTION(*junction.method, method_frame /*caller*/, junction.self, {
1284: frame.store_params(params, 2);
1285: Temp_disable_default_setter temp(junction.self);
1286: call(frame);
1287: });
1288: } else {
1289: // setter
1290: if(param_count!=1)
1291: throw Exception(PARSER_RUNTIME, 0, "setter method must have ONE parameter (has %d parameters)", param_count);
1292:
1293: CONSTRUCTOR_FRAME_ACTION(*junction.method, method_frame /*caller*/, junction.self, {
1294: frame.store_params(&value, 1);
1295: call(frame);
1296: });
1.364 moko 1297: }
1.407 moko 1298: }
1.364 moko 1299: }
1300:
1.385 moko 1301: Value& Request::process_getter(Junction& junction) {
1.397 moko 1302: int param_count=junction.method->params_count;
1.364 moko 1303:
1.397 moko 1304: if(junction.auto_name){
1.364 moko 1305: // default getter
1.397 moko 1306: if(param_count>1)
1.422 moko 1307: throw Exception(PARSER_RUNTIME, 0, "default getter method can't have more than 1 parameter (has %d parameters)", param_count);
1.397 moko 1308:
1.364 moko 1309: Value *param;
1.397 moko 1310: METHOD_FRAME_ACTION(*junction.method, method_frame/*caller*/, junction.self, {
1.364 moko 1311:
1.397 moko 1312: if(param_count){
1313: param=new VString(*junction.auto_name);
1314: frame.store_params(¶m, 1);
1315: } // no need for else frame.empty_params()
1316:
1317: Temp_disable_default_getter temp(junction.self);
1318: call(frame);
1319: return frame.result();
1320: });
1.364 moko 1321: } else {
1322: // getter
1.397 moko 1323: if(param_count>0)
1.386 moko 1324: throw Exception(PARSER_RUNTIME, 0, "getter method must have no parameters (has %d parameters)", param_count);
1.364 moko 1325:
1.397 moko 1326: METHOD_FRAME_ACTION(*junction.method, method_frame/*caller*/, junction.self, {
1327: // no need for frame.empty_params()
1328: call(frame);
1329: return frame.result();
1330: });
1.364 moko 1331: }
1.309 paf 1332:
1.34 paf 1333: }
1.70 paf 1334:
1.389 moko 1335: Value& Request::process(Value& input_value) {
1.297 paf 1336: Junction* junction=input_value.get_junction();
1.307 paf 1337: if(junction) {
1338: if(junction->is_getter) { // is it a getter-junction?
1.389 moko 1339: return process(process_getter(*junction));
1.307 paf 1340: }
1341:
1342: if(junction->code) { // is it a code-junction?
1343: // process it
1.385 moko 1344: ValueRef result;
1.336 misha 1345:
1346: DEBUG_PRINT_STR("ja->\n")
1.238 paf 1347:
1.307 paf 1348: if(!junction->method_frame)
1.386 moko 1349: throw Exception(PARSER_RUNTIME, 0, "junction used outside of context");
1.240 paf 1350:
1.330 misha 1351: SAVE_CONTEXT
1.307 paf 1352:
1353: method_frame=junction->method_frame;
1354: rcontext=junction->rcontext;
1355:
1.389 moko 1356: // for code in [] and () wcontext is set 0
1357: // using the fact in decision "which wrapper to use"
1.399 moko 1358: if(junction->wcontext) {
1.389 moko 1359: // {} code wrapper
1.318 misha 1360: VCodeFrame local(*junction->wcontext);
1.307 paf 1361: wcontext=&local;
1362:
1363: // execute it
1.423 moko 1364: recursion_checked_execute(*junction->code);
1.307 paf 1365:
1.318 misha 1366: result=wcontext->result();
1.307 paf 1367: } else {
1.389 moko 1368: // [] or () code wrapper
1.351 misha 1369: WWrapper local(wcontext);
1.307 paf 1370: wcontext=&local;
1371:
1372: // execute it
1.423 moko 1373: recursion_checked_execute(*junction->code);
1.307 paf 1374:
1375: result=wcontext->result();
1376: }
1377:
1.330 misha 1378: RESTORE_CONTEXT
1.207 paf 1379:
1.336 misha 1380: DEBUG_PRINT_STR("<-ja returned")
1381:
1.307 paf 1382: return result;
1383: }
1384:
1.422 moko 1385: // then it is a method-junction, do not explode it
1.307 paf 1386: // just return it as we do for usual objects
1387: }
1388:
1389: return input_value;
1.85 paf 1390: }
1391:
1.332 misha 1392: void Request::process_write(Value& input_value) {
1393: Junction* junction=input_value.get_junction();
1394: if(junction) {
1.375 moko 1395: // no getter-junction check as process_write is called
1396: // to process method arguments, not from get_element
1.332 misha 1397:
1398: if(junction->code) { // is it a code-junction?
1.389 moko 1399: // process it
1.336 misha 1400:
1401: DEBUG_PRINT_STR("ja->\n")
1402:
1.332 misha 1403: if(!junction->method_frame)
1.386 moko 1404: throw Exception(PARSER_RUNTIME, 0, "junction used outside of context");
1.332 misha 1405:
1406: SAVE_CONTEXT
1407:
1408: method_frame=junction->method_frame;
1409: rcontext=junction->rcontext;
1410:
1.389 moko 1411: // for code in [] and () wcontext is set 0
1.332 misha 1412: // using the fact in decision "which wwrapper to use"
1413: #ifdef OPTIMIZE_CALL
1414: if(wcontext==junction->wcontext){
1415: // no wrappers for wcontext
1.423 moko 1416: recursion_checked_execute(*junction->code);
1.332 misha 1417: RESTORE_CONTEXT
1418:
1419: } else
1420: #endif
1421: if(junction->wcontext) {
1.389 moko 1422: // {} code wrapper
1.332 misha 1423: VCodeFrame local(*junction->wcontext);
1424: wcontext=&local;
1425:
1426: // execute it
1.423 moko 1427: recursion_checked_execute(*junction->code);
1.332 misha 1428: RESTORE_CONTEXT
1.396 moko 1429: write(local.result());
1.332 misha 1430: } else {
1.389 moko 1431: // [] or () code wrapper
1.351 misha 1432: WWrapper local(wcontext);
1.332 misha 1433: wcontext=&local;
1434:
1435: // execute it
1.423 moko 1436: recursion_checked_execute(*junction->code);
1.332 misha 1437: RESTORE_CONTEXT
1.396 moko 1438: write(local.result());
1.332 misha 1439: }
1.336 misha 1440:
1441: DEBUG_PRINT_STR("<-ja returned")
1442:
1.332 misha 1443: return;
1444: }
1445:
1.422 moko 1446: // then it is a method-junction, do not explode it
1.332 misha 1447: // just return it as we do for usual objects
1448: }
1449:
1.396 moko 1450: write(input_value);
1.332 misha 1451: }
1452:
1.408 moko 1453: const String* Request::execute_method(VStateless_class& aclass, const String& method_name, Value* optional_param) {
1454: if(const Method *method=aclass.get_method(method_name)){
1455: METHOD_FRAME_ACTION(*method, method_frame/*caller*/, aclass, {
1.317 misha 1456:
1.408 moko 1457: if(optional_param && method->params_count>0) {
1458: frame.store_params(&optional_param, 1);
1459: } else {
1460: frame.empty_params();
1461: }
1.246 paf 1462:
1.408 moko 1463: // prevent non-string writes for better error reporting
1.397 moko 1464: frame.write(frame);
1.408 moko 1465:
1466: call(frame);
1.208 paf 1467:
1.410 moko 1468: Value &result=frame.result();
1469: if(const String* sresult=result.get_string()){
1470: return sresult;
1471: }
1472: result.bark("is '%s', it has no string representation", &method_name);
1.408 moko 1473: });
1474: }
1475: return 0;
1.242 paf 1476: }
1477:
1.408 moko 1478: bool Request::execute_method_if_exists(VStateless_class& aclass, const String& method_name, Value* optional_param) {
1479: if(const Method *method=aclass.get_method(method_name)){
1480: METHOD_FRAME_ACTION(*method, method_frame/*caller*/, aclass, {
1481:
1482: if(optional_param && method->params_count>0) {
1483: frame.store_params(&optional_param, 1);
1484: } else {
1485: frame.empty_params();
1486: }
1.99 paf 1487:
1.408 moko 1488: call(frame);
1489: });
1490: return true;
1491: }
1492: return false;
1.176 parser 1493: }
1.362 misha 1494:
1.417 moko 1495: bool Request::execute_auto_method_if_exists(VStateless_class& aclass, const String& method_name, Value* optional_param) {
1496: if(const Method *method=aclass.get_method(method_name)){
1497: METHOD_FRAME_ACTION(*method, method_frame/*caller*/, aclass, {
1498: Value* two_params[2];
1499:
1500: if (optional_param && method->params_count>1) {
1501: two_params[0] = optional_param;
1502: two_params[1] = new VString(*new String(aclass.type()));
1503: frame.store_params(two_params, 2);
1504: } else if (optional_param && method->params_count>0) {
1505: frame.store_params(&optional_param, 1);
1506: } else {
1507: frame.empty_params();
1508: }
1509:
1510: call(frame);
1511: });
1512: return true;
1513: }
1514: return false;
1515: }
1516:
1.398 moko 1517: const String* Request::get_method_filespec(const Method* method){
1.404 moko 1518: Operation::Origin origin=get_method_origin(method);
1519: return origin.file_no ? get_used_filespec(origin.file_no) : NULL;
1520: }
1521:
1522: const Operation::Origin Request::get_method_origin(const Method* method){
1523: Operation::Origin origin={0, 0, 0};
1524:
1.362 misha 1525: if(ArrayOperation* code=method->parser_code)
1526: if(code){
1.419 moko 1527: for(Array_iterator<Operation> i(*code); i; ){
1.362 misha 1528: switch( i.next().code ){
1529: case OP::OP_CURLY_CODE__STORE_PARAM:
1530: case OP::OP_EXPR_CODE__STORE_PARAM:
1531: case OP::OP_CURLY_CODE__CONSTRUCT:
1532: case OP::OP_NESTED_CODE:
1533: case OP::OP_OBJECT_POOL:
1534: case OP::OP_STRING_POOL:
1535: case OP::OP_CALL:
1536: case OP::OP_CALL__WRITE:
1537: {
1538: i.next(); // skip local ops
1539: i.next(); // skip next opcode
1540: // continue execution
1541: }
1542: case OP::OP_CONSTRUCT_OBJECT:
1543: case OP::OP_CONSTRUCT_OBJECT__WRITE:
1544: case OP::OP_VALUE:
1545: case OP::OP_STRING__WRITE:
1546: case OP::OP_VALUE__GET_CLASS:
1.383 moko 1547: case OP::OP_VALUE__GET_BASE_CLASS:
1.362 misha 1548: #ifdef OPTIMIZE_BYTECODE_GET_OBJECT_ELEMENT
1549: case OP::OP_GET_OBJECT_ELEMENT:
1550: case OP::OP_GET_OBJECT_ELEMENT__WRITE:
1551: #endif
1552: #ifdef OPTIMIZE_BYTECODE_GET_OBJECT_VAR_ELEMENT
1553: case OP::OP_GET_OBJECT_VAR_ELEMENT:
1554: case OP::OP_GET_OBJECT_VAR_ELEMENT__WRITE:
1555: #endif
1556: #ifdef OPTIMIZE_BYTECODE_GET_ELEMENT
1557: case OP::OP_VALUE__GET_ELEMENT:
1558: case OP::OP_VALUE__GET_ELEMENT__WRITE:
1559: case OP::OP_VALUE__GET_ELEMENT_OR_OPERATOR:
1560: case OP::OP_WITH_ROOT__VALUE__GET_ELEMENT:
1561: #endif
1562: #ifdef OPTIMIZE_BYTECODE_GET_SELF_ELEMENT
1563: case OP::OP_WITH_SELF__VALUE__GET_ELEMENT:
1564: case OP::OP_WITH_SELF__VALUE__GET_ELEMENT__WRITE:
1565: #endif
1.377 moko 1566:
1567: #ifdef FEATURE_GET_ELEMENT4CALL
1568: case OP::OP_GET_ELEMENT4CALL:
1569: #ifdef OPTIMIZE_BYTECODE_GET_OBJECT_ELEMENT
1570: case OP::OP_GET_OBJECT_ELEMENT4CALL:
1571: #endif
1572: #ifdef OPTIMIZE_BYTECODE_GET_OBJECT_VAR_ELEMENT
1573: case OP::OP_GET_OBJECT_VAR_ELEMENT4CALL:
1574: #endif
1575: #endif // FEATURE_GET_ELEMENT4CALL
1576:
1.362 misha 1577: #ifdef OPTIMIZE_BYTECODE_CONSTRUCT
1578: case OP::OP_WITH_ROOT__VALUE__CONSTRUCT_EXPR:
1579: case OP::OP_WITH_ROOT__VALUE__CONSTRUCT_VALUE:
1580: case OP::OP_WITH_WRITE__VALUE__CONSTRUCT_EXPR:
1581: case OP::OP_WITH_WRITE__VALUE__CONSTRUCT_VALUE:
1582: case OP::OP_WITH_SELF__VALUE__CONSTRUCT_EXPR:
1583: case OP::OP_WITH_SELF__VALUE__CONSTRUCT_VALUE:
1584: #endif
1585: {
1586: origin=i.next().origin;
1587: break;
1588: }
1.373 moko 1589: default: break;
1.362 misha 1590: }
1591: if(origin.file_no)
1.404 moko 1592: return origin;
1.362 misha 1593: }
1594: }
1.404 moko 1595: return origin;
1.362 misha 1596: }
1597:
E-mail: