Annotation of parser3/src/main/execute.C, revision 1.429
1.117 paf 1: /** @file
1.118 paf 2: Parser: executor part of request class.
3:
1.425 moko 4: Copyright (c) 2001-2024 Art. Lebedev Studio (http://www.artlebedev.com)
1.414 moko 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.429 ! moko 25: volatile const char * IDENT_EXECUTE_C="$Id: execute.C,v 1.428 2026/01/06 16:36:39 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.428 moko 217: case OP::OP_CURLY_CODE__STORE_PARAM:
1.323 misha 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.426 moko 445: if(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: {
1.429 ! moko 453: debug_name=0;
! 454:
1.421 moko 455: ArrayOperation* local_ops=i.next().ops;
456:
457: DEBUG_PRINT_OPS(local_ops)
458: DEBUG_PRINT_STR("->\n")
459:
460: size_t first = stack.top_index();
461: execute(*local_ops);
462: Value *value=new VArray(stack.top_index()-first, (Value**)stack.ptr(first));
463: stack.set_top_index(first);
464:
465: const String& name=stack.pop().string(); debug_name=&name;
466: Value& ncontext=stack.pop().value();
1.429 ! moko 467:
1.421 moko 468: put_element(ncontext, name, value);
469:
470: DEBUG_PRINT_STR("<-returned")
471: goto check_skip;
472: }
473:
1.322 misha 474: case OP::OP_NESTED_CODE:
1.209 paf 475: {
1.297 paf 476: ArrayOperation& local_ops=*i.next().ops;
1.336 misha 477:
478: DEBUG_PRINT_OPS((&local_ops))
479:
1.297 paf 480: stack.push(local_ops);
1.209 paf 481: break;
482: }
1.322 misha 483: case OP::OP_WRITE_VALUE:
1.13 paf 484: {
1.297 paf 485: Value& value=stack.pop().value();
1.396 moko 486: write(value);
1.86 paf 487: break;
488: }
1.322 misha 489: case OP::OP_WRITE_EXPR_RESULT:
1.108 paf 490: {
1.351 misha 491: Value& value=stack.pop().value();
492: wcontext->write(value.as_expr_result());
1.108 paf 493: break;
494: }
1.322 misha 495: case OP::OP_STRING__WRITE:
1.86 paf 496: {
1.297 paf 497: i.next(); // ignore origin
498: Value* value=i.next().value;
1.336 misha 499:
500: const String& string_value=*value->get_string();
501:
502: DEBUG_PRINT_STRING(string_value)
503:
1.396 moko 504: write(string_value);
1.13 paf 505: break;
1.14 paf 506: }
1.328 misha 507:
508: #ifdef OPTIMIZE_BYTECODE_GET_ELEMENT
509: case OP::OP_VALUE__GET_ELEMENT_OR_OPERATOR:
510: {
511: debug_origin=i.next().origin;
512: const String& name=*i.next().value->get_string(); debug_name=&name;
513:
1.336 misha 514: DEBUG_PRINT_STRING(name)
515:
1.330 misha 516: if(Method* method=main_class.get_method(name)){ // looking operator of that name FIRST
1.427 moko 517: stack.push(*method->get_vjunction(main_class));
1.330 misha 518: break;
519: }
520: Value& value=get_element(*rcontext, name);
1.328 misha 521: stack.push(value);
1.403 moko 522: goto check_skip;
1.328 misha 523: }
524: #else
1.322 misha 525: case OP::OP_GET_ELEMENT_OR_OPERATOR:
1.215 paf 526: {
1.297 paf 527: const String& name=stack.pop().string(); debug_name=&name;
528: Value& ncontext=stack.pop().value();
1.330 misha 529: if(Method* method=main_class.get_method(name)){ // looking operator of that name FIRST
1.427 moko 530: stack.push(*method->get_vjunction(main_class));
1.330 misha 531: break;
532: }
533: Value& value=get_element(ncontext, name);
1.297 paf 534: stack.push(value);
1.403 moko 535: goto check_skip;
1.215 paf 536: }
1.328 misha 537: #endif
538:
1.335 misha 539: #ifdef OPTIMIZE_BYTECODE_GET_OBJECT_ELEMENT
1.336 misha 540:
1.376 moko 541: #define DO_GET_OBJECT_ELEMENT(code) {\
542: debug_origin=i.next().origin; \
543: const String& context_name=*i.next().value->get_string(); debug_name=&context_name; \
544: DEBUG_PRINT_STRING(context_name) \
545: Value& object=get_element(*rcontext, context_name); \
546: debug_origin=i.next().origin; \
547: const String& field_name=*i.next().value->get_string(); debug_name=&field_name; \
548: DEBUG_PRINT_STRING(field_name) \
549: code; \
1.403 moko 550: goto check_skip; \
1.376 moko 551: }
1.336 misha 552:
1.376 moko 553: case OP::OP_GET_OBJECT_ELEMENT: DO_GET_OBJECT_ELEMENT({
554: Value& value=get_element(object, field_name);
555: stack.push(value);
556: })
557: case OP::OP_GET_OBJECT_ELEMENT__WRITE: DO_GET_OBJECT_ELEMENT({
1.334 misha 558: Value& value=get_element(object, field_name);
1.396 moko 559: write(value);
1.376 moko 560: })
561: #ifdef FEATURE_GET_ELEMENT4CALL
562: case OP::OP_GET_OBJECT_ELEMENT4CALL: DO_GET_OBJECT_ELEMENT({
563: Value& value=get_element4call(object, field_name);
564: stack.push(value);
565: })
566: #endif
1.335 misha 567: #endif
568:
569: #ifdef OPTIMIZE_BYTECODE_GET_OBJECT_VAR_ELEMENT
1.336 misha 570:
1.376 moko 571: #define DO_GET_OBJECT_VAR_ELEMENT(code) {\
572: debug_origin=i.next().origin; \
573: const String& context_name=*i.next().value->get_string(); debug_name=&context_name; \
574: DEBUG_PRINT_STRING(context_name) \
575: Value& object=get_element(*rcontext, context_name); \
576: debug_origin=i.next().origin; \
577: const String& var_name=*i.next().value->get_string(); debug_name=&var_name; \
578: DEBUG_PRINT_STRING(var_name) \
579: const String* field=&get_element(*rcontext, var_name).as_string(); \
580: code; \
1.403 moko 581: goto check_skip; \
1.376 moko 582: }
1.335 misha 583:
1.376 moko 584: case OP::OP_GET_OBJECT_VAR_ELEMENT: DO_GET_OBJECT_VAR_ELEMENT({
585: Value& value=get_element(object, *field);
586: stack.push(value);
587: })
588: case OP::OP_GET_OBJECT_VAR_ELEMENT__WRITE: DO_GET_OBJECT_VAR_ELEMENT({
1.335 misha 589: Value& value=get_element(object, *field);
1.396 moko 590: write(value);
1.376 moko 591: })
592: #ifdef FEATURE_GET_ELEMENT4CALL
593: case OP::OP_GET_OBJECT_VAR_ELEMENT4CALL: DO_GET_OBJECT_VAR_ELEMENT({
594: Value& value=get_element4call(object, *field);
595: stack.push(value);
596: })
597: #endif
598: #endif
1.335 misha 599:
1.376 moko 600: case OP::OP_GET_ELEMENT:
601: {
602: const String& name=stack.pop().string(); debug_name=&name;
603: Value& ncontext=stack.pop().value();
604: Value& value=get_element(ncontext, name);
605: stack.push(value);
1.403 moko 606: goto check_skip;
1.334 misha 607: }
608:
1.376 moko 609: #ifdef FEATURE_GET_ELEMENT4CALL
610: case OP::OP_GET_ELEMENT4CALL:
1.11 paf 611: {
1.297 paf 612: const String& name=stack.pop().string(); debug_name=&name;
613: Value& ncontext=stack.pop().value();
1.376 moko 614: Value& value=get_element4call(ncontext, name);
1.297 paf 615: stack.push(value);
1.403 moko 616: goto check_skip;
1.17 paf 617: }
1.376 moko 618: #endif
1.17 paf 619:
1.346 misha 620: case OP::OP_GET_ELEMENT__WRITE:
621: {
622: const String& name=stack.pop().string(); debug_name=&name;
623: Value& ncontext=stack.pop().value();
624: Value& value=get_element(ncontext, name);
1.396 moko 625: write(value);
1.403 moko 626: goto check_skip;
1.346 misha 627: }
628:
1.328 misha 629: #ifdef OPTIMIZE_BYTECODE_GET_ELEMENT
630: case OP::OP_VALUE__GET_ELEMENT:
631: {
632: debug_origin=i.next().origin;
633: const String& name=*i.next().value->get_string(); debug_name=&name;
634:
1.336 misha 635: DEBUG_PRINT_STRING(name)
1.328 misha 636:
1.330 misha 637: Value& value=get_element(*rcontext, name);
1.328 misha 638: stack.push(value);
1.403 moko 639: goto check_skip;
1.328 misha 640: }
641:
642: case OP::OP_VALUE__GET_ELEMENT__WRITE:
643: {
644: debug_origin=i.next().origin;
645: const String& name=*i.next().value->get_string(); debug_name=&name;
646:
1.336 misha 647: DEBUG_PRINT_STRING(name)
1.328 misha 648:
1.330 misha 649: Value& value=get_element(*rcontext, name);
1.396 moko 650: write(value);
1.403 moko 651: goto check_skip;
1.328 misha 652: }
1.347 misha 653:
654: case OP::OP_WITH_ROOT__VALUE__GET_ELEMENT:
655: {
656: debug_origin=i.next().origin;
657: const String& name=*i.next().value->get_string(); debug_name=&name;
658:
659: DEBUG_PRINT_STRING(name)
660:
661: Value& value=get_element(*method_frame, name);
662: stack.push(value);
1.403 moko 663: goto check_skip;
1.347 misha 664: }
1.328 misha 665: #endif
1.32 paf 666:
1.345 misha 667: #ifdef OPTIMIZE_BYTECODE_GET_SELF_ELEMENT
668: case OP::OP_WITH_SELF__VALUE__GET_ELEMENT:
669: case OP::OP_WITH_SELF__VALUE__GET_ELEMENT__WRITE:
670: {
671: debug_origin=i.next().origin;
672: const String& name=*i.next().value->get_string(); debug_name=&name;
673:
674: DEBUG_PRINT_STRING(name)
675:
676: Value& value=get_element(get_self(), name);
677:
678: if(opcode==OP::OP_WITH_SELF__VALUE__GET_ELEMENT){
679: stack.push(value);
680: } else {
1.396 moko 681: write(value);
1.345 misha 682: }
1.403 moko 683: goto check_skip;
1.345 misha 684: }
685: #endif
686:
1.322 misha 687: case OP::OP_OBJECT_POOL:
1.17 paf 688: {
1.406 moko 689: debug_name=0;
690:
1.297 paf 691: ArrayOperation& local_ops=*i.next().ops;
1.226 paf 692:
1.257 paf 693: WContext *saved_wcontext=wcontext;
1.348 misha 694: #ifdef OPTIMIZE_SINGLE_STRING_WRITE
1.351 misha 695: WObjectPoolWrapper local(wcontext);
1.348 misha 696: #else
1.351 misha 697: WWrapper local(wcontext);
1.348 misha 698: #endif
1.226 paf 699: wcontext=&local;
700:
1.297 paf 701: execute(local_ops);
1.226 paf 702:
1.400 moko 703: stack.push((Value&)wcontext->result());
1.403 moko 704:
1.257 paf 705: wcontext=saved_wcontext;
1.403 moko 706: goto check_skip;
1.15 paf 707: }
1.13 paf 708:
1.322 misha 709: case OP::OP_STRING_POOL:
1.49 paf 710: {
1.297 paf 711: ArrayOperation& local_ops=*i.next().ops;
1.226 paf 712:
1.257 paf 713: WContext *saved_wcontext=wcontext;
1.351 misha 714: WWrapper local(wcontext);
1.226 paf 715: wcontext=&local;
716:
1.297 paf 717: execute(local_ops);
1.226 paf 718:
1.49 paf 719: // from "$a $b" part of expression taking only string value,
720: // ignoring any other content of wcontext
1.367 moko 721: const String* string=wcontext->get_string();
722: Value* value=string ? new VString(*string) : new VString();
1.297 paf 723: stack.push(*value);
724:
1.257 paf 725: wcontext=saved_wcontext;
1.403 moko 726: goto check_skip;
1.49 paf 727: }
728:
1.51 paf 729: // CALL
1.322 misha 730: case OP::OP_CURLY_CODE__STORE_PARAM:
731: case OP::OP_EXPR_CODE__STORE_PARAM:
1.28 paf 732: {
1.237 paf 733: // code
1.297 paf 734: ArrayOperation& local_ops=*i.next().ops;
1.336 misha 735:
736: DEBUG_PRINT_OPS((&local_ops))
737:
1.237 paf 738: // when they evaluate expression parameter,
739: // the object expression result
740: // does not need to be written into calling frame
741: // it must go into any expressions using that parameter
742: // hence, we zero junction.wcontext here, and later
743: // in .process we would test that field
744: // in decision "which wwrapper to use"
1.326 misha 745: VJunction& value=*new VJunction(
1.297 paf 746: get_self(), 0,
1.257 paf 747: method_frame,
1.237 paf 748: rcontext,
1.322 misha 749: opcode==OP::OP_EXPR_CODE__STORE_PARAM?0:wcontext,
1.312 paf 750: &local_ops);
1.343 misha 751: #ifndef USE_DESTRUCTORS
1.326 misha 752: if (opcode!=OP::OP_EXPR_CODE__STORE_PARAM)
1.343 misha 753: wcontext->attach_junction(&value);
1.326 misha 754: #endif
1.237 paf 755: // store param
1.329 misha 756: stack.push(value);
1.29 paf 757: break;
758: }
759:
1.397 moko 760: #define METHOD_PARAMS_ACTION(action) \
1.386 moko 761: if(local_ops){ \
762: size_t first = stack.top_index(); \
763: execute(*local_ops); \
1.403 moko 764: if(!fskip){ \
765: frame.store_params((Value**)stack.ptr(first), stack.top_index()-first); \
766: action; \
767: } \
1.386 moko 768: stack.set_top_index(first); \
769: } else { \
770: frame.empty_params(); \
771: action; \
1.350 misha 772: }
773:
1.322 misha 774: case OP::OP_CALL:
1.29 paf 775: {
1.297 paf 776: ArrayOperation* local_ops=i.next().ops;
1.237 paf 777:
1.336 misha 778: DEBUG_PRINT_OPS(local_ops)
779: DEBUG_PRINT_STR("->\n")
780:
1.297 paf 781: Value& value=stack.pop().value();
1.237 paf 782:
1.297 paf 783: Junction* junction=value.get_junction();
784: if(!junction) {
1.420 moko 785: if(dynamic_cast<VVoid*>(&value))
1.386 moko 786: throw Exception(PARSER_RUNTIME, 0, "undefined method");
1.297 paf 787: else
1.422 moko 788: throw Exception(PARSER_RUNTIME, 0, "is '%s', not a method or junction, cannot call it", value.type());
1.297 paf 789: }
1.237 paf 790:
1.370 moko 791: Value *result;
792: {
1.397 moko 793: EXPRESSION_FRAME_ACTION(*junction->method, method_frame, junction->self, {
794: METHOD_PARAMS_ACTION(call(frame));
795: result=&frame.result();
796: // desctructor deletes junctions in stack params here
797: });
1.370 moko 798: }
799: stack.push(*result);
1.332 misha 800:
1.336 misha 801: DEBUG_PRINT_STR("<-returned")
1.403 moko 802: goto check_skip;
1.332 misha 803: }
1.220 paf 804:
1.332 misha 805: case OP::OP_CALL__WRITE:
806: {
807: ArrayOperation* local_ops=i.next().ops;
808:
1.336 misha 809: DEBUG_PRINT_OPS(local_ops)
810: DEBUG_PRINT_STR("->\n")
811:
1.332 misha 812: Value& value=stack.pop().value();
813:
814: Junction* junction=value.get_junction();
815: if(!junction) {
1.420 moko 816: if(dynamic_cast<VVoid*>(&value))
1.386 moko 817: throw Exception(PARSER_RUNTIME, 0, "undefined method");
1.332 misha 818: else
1.422 moko 819: throw Exception(PARSER_RUNTIME, 0, "is '%s', not a method or junction, cannot call it", value.type());
1.332 misha 820: }
821:
1.391 moko 822: const Method& method=*junction->method;
1.332 misha 823: #ifdef OPTIMIZE_CALL
1.353 misha 824: if(method.call_optimization==Method::CO_WITHOUT_FRAME){
1.332 misha 825: if(local_ops){ // store param code goes here
826: size_t first = stack.top_index();
827: execute(*local_ops);
828:
1.403 moko 829: if(!fskip){
830: MethodParams method_params;
831: method_params.store_params((Value**)stack.ptr(first), stack.top_index()-first);
832: method.check_actual_numbered_params(junction->self, &method_params);
833: method.native_code(*this, method_params); // execute it
834: }
1.332 misha 835:
836: stack.set_top_index(first);
837: } else {
838: MethodParams method_params;
839: method.check_actual_numbered_params(junction->self, &method_params);
840: method.native_code(*this, method_params); // execute it
841: }
1.353 misha 842: } else if(method.call_optimization==Method::CO_WITHOUT_WCONTEXT){
1.397 moko 843: METHOD_FRAME_ACTION(method, method_frame, junction->self, {
844: METHOD_PARAMS_ACTION(call_write(frame))
845: });
1.332 misha 846: } else
847: #endif // OPTIMIZE_CALL
848: {
1.397 moko 849: METHOD_FRAME_ACTION(method, method_frame, junction->self, {
850: METHOD_PARAMS_ACTION(call(frame));
851: write(frame.result());
852: });
1.332 misha 853: }
1.336 misha 854:
855: DEBUG_PRINT_STR("<-returned")
1.403 moko 856: goto check_skip;
1.49 paf 857: }
858:
1.348 misha 859: case OP::OP_CONSTRUCT_OBJECT:
860: case OP::OP_CONSTRUCT_OBJECT__WRITE:
861: {
862: debug_origin=i.next().origin;
863: Value& vclass_name=*i.next().value;
1.360 pretende 864: const String& class_name=*vclass_name.get_string(); debug_name=&class_name;
1.348 misha 865:
866: DEBUG_PRINT_STRING(class_name)
867:
1.413 moko 868: VStateless_class& vclass=get_class_ref(class_name);
1.348 misha 869:
870: debug_origin=i.next().origin;
871: Value& vconstructor_name=*i.next().value;
872: const String& constructor_name=*vconstructor_name.get_string(); debug_name=&constructor_name;
873:
874: DEBUG_PRINT_STRING(constructor_name)
875:
1.424 moko 876: // not get_element4call for better error reporting
1.413 moko 877: Junction* constructor_junction=get_element(vclass, constructor_name).get_junction();
1.363 moko 878: if(!constructor_junction)
1.424 moko 879: throw Exception(PARSER_RUNTIME, &constructor_name, "%s constructor not found", vclass.type());
1.350 misha 880:
1.348 misha 881: ArrayOperation* local_ops=i.next().ops;
882: DEBUG_PRINT_OPS(local_ops)
883: DEBUG_PRINT_STR("->\n")
884:
1.370 moko 885: Value *result;
886: {
1.413 moko 887: Value& object=construct(vclass, *constructor_junction->method);
1.397 moko 888: CONSTRUCTOR_FRAME_ACTION(*constructor_junction->method, method_frame, object, {
889: METHOD_PARAMS_ACTION(call(frame));
890: object.enable_default_setter();
891: result=&frame.result();
892: // desctructor deletes junctions in stack params here
893: });
1.370 moko 894: }
1.355 misha 895:
1.350 misha 896: if(opcode==OP::OP_CONSTRUCT_OBJECT)
1.370 moko 897: stack.push(*result);
1.350 misha 898: else
1.396 moko 899: write(*result);
1.348 misha 900:
901: DEBUG_PRINT_STR("<-returned")
1.403 moko 902: goto check_skip;
1.348 misha 903: }
904:
1.55 paf 905: // expression ops: unary
1.322 misha 906: case OP::OP_NEG:
1.55 paf 907: {
1.297 paf 908: Value& a=stack.pop().value();
909: Value& value=*new VDouble(-a.as_double());
910: stack.push(value);
1.55 paf 911: break;
912: }
1.322 misha 913: case OP::OP_INV:
1.55 paf 914: {
1.297 paf 915: Value& a=stack.pop().value();
916: Value& value=*new VDouble(~a.as_int());
917: stack.push(value);
1.55 paf 918: break;
919: }
1.322 misha 920: case OP::OP_NOT:
1.55 paf 921: {
1.297 paf 922: Value& a=stack.pop().value();
1.327 misha 923: Value& value=VBool::get(!a.as_bool());
1.297 paf 924: stack.push(value);
1.55 paf 925: break;
926: }
1.322 misha 927: case OP::OP_DEF:
1.62 paf 928: {
1.297 paf 929: Value& a=stack.pop().value();
1.327 misha 930: Value& value=VBool::get(a.is_defined());
1.297 paf 931: stack.push(value);
1.62 paf 932: break;
933: }
1.322 misha 934: case OP::OP_IN:
1.62 paf 935: {
1.297 paf 936: Value& a=stack.pop().value();
937: const String& path=a.as_string();
1.327 misha 938: Value& value=VBool::get(request_info.uri && *request_info.uri && path.this_starts(request_info.uri));
1.297 paf 939: stack.push(value);
1.62 paf 940: break;
941: }
1.322 misha 942: case OP::OP_FEXISTS:
1.62 paf 943: {
1.297 paf 944: Value& a=stack.pop().value();
1.411 moko 945: Value& value=VBool::get(file_exist(full_disk_path(a.as_string())));
1.297 paf 946: stack.push(value);
1.148 paf 947: break;
948: }
1.322 misha 949: case OP::OP_DEXISTS:
1.148 paf 950: {
1.297 paf 951: Value& a=stack.pop().value();
1.411 moko 952: Value& value=VBool::get(dir_exists(full_disk_path(a.as_string())));
1.297 paf 953: stack.push(value);
1.62 paf 954: break;
955: }
1.55 paf 956:
957: // expression ops: binary
1.415 moko 958: case OP::OP_SUB:
1.53 paf 959: {
1.416 moko 960: volatile double b_double=stack.pop().value().as_double();
961: volatile double a_double=stack.pop().value().as_double();
1.415 moko 962:
963: Value& value=*new VDouble(a_double - b_double);
1.297 paf 964: stack.push(value);
1.53 paf 965: break;
966: }
1.415 moko 967: case OP::OP_ADD:
1.53 paf 968: {
1.416 moko 969: volatile double b_double=stack.pop().value().as_double();
970: volatile double a_double=stack.pop().value().as_double();
1.415 moko 971:
972: Value& value=*new VDouble(a_double + b_double);
1.297 paf 973: stack.push(value);
1.53 paf 974: break;
975: }
1.415 moko 976: case OP::OP_MUL:
1.49 paf 977: {
1.416 moko 978: volatile double b_double=stack.pop().value().as_double();
979: volatile double a_double=stack.pop().value().as_double();
1.415 moko 980:
981: Value& value=*new VDouble(a_double * b_double);
1.297 paf 982: stack.push(value);
1.53 paf 983: break;
984: }
1.415 moko 985: case OP::OP_DIV:
1.53 paf 986: {
1.416 moko 987: volatile double b_double=stack.pop().value().as_double();
988: volatile double a_double=stack.pop().value().as_double();
1.170 parser 989:
1.171 parser 990: if(b_double == 0) {
1.297 paf 991: //const String* problem_source=b.as_string();
1.386 moko 992: throw Exception("number.zerodivision", 0, "Division by zero");
1.171 parser 993: }
1.170 parser 994:
1.297 paf 995: Value& value=*new VDouble(a_double / b_double);
996: stack.push(value);
1.54 paf 997: break;
998: }
1.415 moko 999: case OP::OP_MOD:
1.55 paf 1000: {
1.416 moko 1001: volatile double b_double=stack.pop().value().as_double();
1002: volatile double a_double=stack.pop().value().as_double();
1.170 parser 1003:
1.173 parser 1004: if(b_double == 0) {
1.297 paf 1005: //const String* problem_source=b.as_string();
1.386 moko 1006: throw Exception("number.zerodivision", 0, "Modulus by zero");
1.171 parser 1007: }
1.170 parser 1008:
1.297 paf 1009: Value& value=*new VDouble(fmod(a_double, b_double));
1010: stack.push(value);
1.201 paf 1011: break;
1012: }
1.322 misha 1013: case OP::OP_INTDIV:
1.201 paf 1014: {
1.428 moko 1015: pa_wint b_int=stack.pop().value().as_wint();
1016: pa_wint a_int=stack.pop().value().as_wint();
1.201 paf 1017:
1018: if(b_int == 0) {
1.297 paf 1019: //const String* problem_source=b.as_string();
1.386 moko 1020: throw Exception("number.zerodivision", 0, "Division by zero");
1.201 paf 1021: }
1022:
1.297 paf 1023: Value& value=*new VInt(a_int / b_int);
1024: stack.push(value);
1.55 paf 1025: break;
1026: }
1.322 misha 1027: case OP::OP_BIN_SL:
1.274 paf 1028: {
1.297 paf 1029: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1030: Value& value=*new VInt(
1.428 moko 1031: a.as_wint() <<
1032: b.as_wint());
1.297 paf 1033: stack.push(value);
1.274 paf 1034: break;
1035: }
1.322 misha 1036: case OP::OP_BIN_SR:
1.274 paf 1037: {
1.297 paf 1038: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1039: Value& value=*new VInt(
1.428 moko 1040: a.as_wint() >>
1041: b.as_wint());
1.297 paf 1042: stack.push(value);
1.274 paf 1043: break;
1044: }
1.322 misha 1045: case OP::OP_BIN_AND:
1.54 paf 1046: {
1.297 paf 1047: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1048: Value& value=*new VInt(
1.428 moko 1049: a.as_wint() &
1050: b.as_wint());
1.297 paf 1051: stack.push(value);
1.54 paf 1052: break;
1053: }
1.322 misha 1054: case OP::OP_BIN_OR:
1.54 paf 1055: {
1.297 paf 1056: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1057: Value& value=*new VInt(
1.428 moko 1058: a.as_wint() |
1059: b.as_wint());
1.297 paf 1060: stack.push(value);
1.55 paf 1061: break;
1062: }
1.322 misha 1063: case OP::OP_BIN_XOR:
1.56 paf 1064: {
1.297 paf 1065: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1066: Value& value=*new VInt(
1.428 moko 1067: a.as_wint() ^
1068: b.as_wint());
1.297 paf 1069: stack.push(value);
1.56 paf 1070: break;
1071: }
1.322 misha 1072: case OP::OP_LOG_AND:
1.55 paf 1073: {
1.297 paf 1074: ArrayOperation& local_ops=stack.pop().ops(); Value& a=stack.pop().value();
1.263 paf 1075: bool result;
1.297 paf 1076: if(a.as_bool()) {
1077: execute(local_ops);
1078: Value& b=stack.pop().value();
1079: result=b.as_bool();
1.209 paf 1080: } else
1.263 paf 1081: result=false;
1.327 misha 1082: Value& value=VBool::get(result);
1.297 paf 1083: stack.push(value);
1.403 moko 1084: goto check_skip;
1.55 paf 1085: }
1.322 misha 1086: case OP::OP_LOG_OR:
1.55 paf 1087: {
1.297 paf 1088: ArrayOperation& local_ops=stack.pop().ops(); Value& a=stack.pop().value();
1.263 paf 1089: bool result;
1.297 paf 1090: if(a.as_bool())
1.263 paf 1091: result=true;
1.209 paf 1092: else {
1.297 paf 1093: execute(local_ops);
1094: Value& b=stack.pop().value();
1095: result=b.as_bool();
1.209 paf 1096: }
1.327 misha 1097: Value& value=VBool::get(result);
1.297 paf 1098: stack.push(value);
1.403 moko 1099: goto check_skip;
1.56 paf 1100: }
1.322 misha 1101: case OP::OP_LOG_XOR:
1.56 paf 1102: {
1.297 paf 1103: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1.327 misha 1104: Value& value=VBool::get(a.as_bool() ^ b.as_bool());
1.297 paf 1105: stack.push(value);
1.55 paf 1106: break;
1107: }
1.428 moko 1108: case OP::OP_NUM_LT:
1.55 paf 1109: {
1.297 paf 1110: volatile double b_double=stack.pop().value().as_double();
1111: volatile double a_double=stack.pop().value().as_double();
1.327 misha 1112: Value& value=VBool::get(a_double<b_double);
1.297 paf 1113: stack.push(value);
1.55 paf 1114: break;
1115: }
1.428 moko 1116: case OP::OP_NUM_GT:
1.55 paf 1117: {
1.297 paf 1118: volatile double b_double=stack.pop().value().as_double();
1119: volatile double a_double=stack.pop().value().as_double();
1.327 misha 1120: Value& value=VBool::get(a_double>b_double);
1.297 paf 1121: stack.push(value);
1.55 paf 1122: break;
1123: }
1.428 moko 1124: case OP::OP_NUM_LE:
1.55 paf 1125: {
1.297 paf 1126: volatile double b_double=stack.pop().value().as_double();
1127: volatile double a_double=stack.pop().value().as_double();
1.327 misha 1128: Value& value=VBool::get(a_double<=b_double);
1.297 paf 1129: stack.push(value);
1.55 paf 1130: break;
1131: }
1.428 moko 1132: case OP::OP_NUM_GE:
1.55 paf 1133: {
1.297 paf 1134: volatile double b_double=stack.pop().value().as_double();
1135: volatile double a_double=stack.pop().value().as_double();
1.327 misha 1136: Value& value=VBool::get(a_double>=b_double);
1.297 paf 1137: stack.push(value);
1.55 paf 1138: break;
1139: }
1.428 moko 1140: case OP::OP_NUM_EQ:
1.55 paf 1141: {
1.297 paf 1142: volatile double b_double=stack.pop().value().as_double();
1143: volatile double a_double=stack.pop().value().as_double();
1.327 misha 1144: Value& value=VBool::get(a_double==b_double);
1.297 paf 1145: stack.push(value);
1.55 paf 1146: break;
1147: }
1.428 moko 1148: case OP::OP_NUM_NE:
1.55 paf 1149: {
1.297 paf 1150: volatile double b_double=stack.pop().value().as_double();
1151: volatile double a_double=stack.pop().value().as_double();
1.327 misha 1152: Value& value=VBool::get(a_double!=b_double);
1.297 paf 1153: stack.push(value);
1.54 paf 1154: break;
1155: }
1.428 moko 1156: case OP::OP_STR_LT:
1.58 paf 1157: {
1.297 paf 1158: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1.327 misha 1159: Value& value=VBool::get(a.as_string() < b.as_string());
1.297 paf 1160: stack.push(value);
1.58 paf 1161: break;
1162: }
1.428 moko 1163: case OP::OP_STR_GT:
1.58 paf 1164: {
1.297 paf 1165: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1.327 misha 1166: Value& value=VBool::get(a.as_string() > b.as_string());
1.297 paf 1167: stack.push(value);
1.58 paf 1168: break;
1169: }
1.428 moko 1170: case OP::OP_STR_LE:
1.58 paf 1171: {
1.297 paf 1172: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1.327 misha 1173: Value& value=VBool::get(a.as_string() <= b.as_string());
1.297 paf 1174: stack.push(value);
1.58 paf 1175: break;
1176: }
1.428 moko 1177: case OP::OP_STR_GE:
1.54 paf 1178: {
1.297 paf 1179: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1.327 misha 1180: Value& value=VBool::get(a.as_string() >= b.as_string());
1.297 paf 1181: stack.push(value);
1.58 paf 1182: break;
1183: }
1.428 moko 1184: case OP::OP_STR_EQ:
1.58 paf 1185: {
1.297 paf 1186: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1.327 misha 1187: Value& value=VBool::get(a.as_string() == b.as_string());
1.297 paf 1188: stack.push(value);
1.58 paf 1189: break;
1190: }
1.428 moko 1191: case OP::OP_STR_NE:
1.58 paf 1192: {
1.297 paf 1193: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1.327 misha 1194: Value& value=VBool::get(a.as_string() != b.as_string());
1.297 paf 1195: stack.push(value);
1.103 paf 1196: break;
1197: }
1.322 misha 1198: case OP::OP_IS:
1.103 paf 1199: {
1.297 paf 1200: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1.327 misha 1201: Value& value=VBool::get(a.is(b.as_string().cstr()));
1.297 paf 1202: stack.push(value);
1.28 paf 1203: break;
1204: }
1.11 paf 1205: default:
1.403 moko 1206: {
1207: throw Exception(0, 0, "invalid opcode %d", opcode);
1208: }
1209: check_skip:
1210: {
1211: if(fskip){
1212: if(fskip==Request::SKIP_INTERRUPTED){
1213: set_skip(Request::SKIP_NOTHING);
1214: throw Exception("parser.interrupted", 0, "execution stopped");
1215: }
1216: return;
1217: }
1218: break;
1219: }
1.11 paf 1220: }
1221: }
1.306 paf 1222: } catch(const Exception&) {
1.297 paf 1223: // record it to stack trace
1.301 paf 1224: if(debug_name)
1225: exception_trace.push(Trace(debug_name, debug_origin));
1.297 paf 1226: rethrow;
1227: }
1.1 paf 1228: }
1.17 paf 1229:
1.330 misha 1230: #define SAVE_CONTEXT \
1.386 moko 1231: VMethodFrame *saved_method_frame=method_frame; \
1232: Value* saved_rcontext=rcontext; \
1.330 misha 1233: WContext *saved_wcontext=wcontext;
1234:
1.386 moko 1235: #define RESTORE_CONTEXT \
1236: wcontext=saved_wcontext; \
1237: rcontext=saved_rcontext; \
1.330 misha 1238: method_frame=saved_method_frame;
1239:
1.384 moko 1240: Value& Request::construct(VStateless_class &called_class, const Method &method){
1.363 moko 1241:
1242: if(method.call_type!=Method::CT_STATIC) {
1243: // this is a constructor call
1244: if(Value* result=called_class.create_new_value(fpool)) {
1.319 misha 1245: // some stateless_class creatable derivates
1.363 moko 1246: return *result;
1247: } else
1.401 moko 1248: throw Exception(PARSER_RUNTIME, method.name,
1249: "is not a constructor, system class '%s' can be constructed only implicitly", called_class.type());
1.363 moko 1250: } else
1.422 moko 1251: throw Exception(PARSER_RUNTIME, method.name, "method of '%s' is static and cannot be used as constructor", called_class.type());
1.363 moko 1252: }
1.319 misha 1253:
1.330 misha 1254: Value& Request::get_element(Value& ncontext, const String& name) {
1.376 moko 1255: Value* value=ncontext.get_element(name);
1.388 moko 1256: return *(value ? &process(*value) : VVoid::get());
1.376 moko 1257: }
1258:
1.383 moko 1259: #ifdef FEATURE_GET_ELEMENT4CALL
1.376 moko 1260: Value& Request::get_element4call(Value& ncontext, const String& name) {
1261: Value* value=ncontext.get_element4call(name);
1.388 moko 1262: return *(value ? &process(*value) : VVoid::get());
1.309 paf 1263: }
1.383 moko 1264: #endif
1.309 paf 1265:
1.329 misha 1266: void Request::put_element(Value& ncontext, const String& name, Value* value) {
1.309 paf 1267: // put_element can return property-setting-junction
1.407 moko 1268: if(const VJunction* vjunction=ncontext.put_element(name, value)) {
1269: const Junction& junction = vjunction->junction();
1270: int param_count=junction.method->params_count;
1271:
1272: if(junction.auto_name) {
1273: // default setter
1274: if(param_count!=2)
1275: throw Exception(PARSER_RUNTIME, 0, "default setter method must have TWO parameters (has %d parameters)", param_count);
1276:
1277: Value* params[2] = { new VString(*junction.auto_name), value };
1278:
1279: CONSTRUCTOR_FRAME_ACTION(*junction.method, method_frame /*caller*/, junction.self, {
1280: frame.store_params(params, 2);
1281: Temp_disable_default_setter temp(junction.self);
1282: call(frame);
1283: });
1284: } else {
1285: // setter
1286: if(param_count!=1)
1287: throw Exception(PARSER_RUNTIME, 0, "setter method must have ONE parameter (has %d parameters)", param_count);
1288:
1289: CONSTRUCTOR_FRAME_ACTION(*junction.method, method_frame /*caller*/, junction.self, {
1290: frame.store_params(&value, 1);
1291: call(frame);
1292: });
1.364 moko 1293: }
1.407 moko 1294: }
1.364 moko 1295: }
1296:
1.385 moko 1297: Value& Request::process_getter(Junction& junction) {
1.397 moko 1298: int param_count=junction.method->params_count;
1.364 moko 1299:
1.397 moko 1300: if(junction.auto_name){
1.364 moko 1301: // default getter
1.397 moko 1302: if(param_count>1)
1.422 moko 1303: throw Exception(PARSER_RUNTIME, 0, "default getter method can't have more than 1 parameter (has %d parameters)", param_count);
1.397 moko 1304:
1.364 moko 1305: Value *param;
1.397 moko 1306: METHOD_FRAME_ACTION(*junction.method, method_frame/*caller*/, junction.self, {
1.364 moko 1307:
1.397 moko 1308: if(param_count){
1309: param=new VString(*junction.auto_name);
1310: frame.store_params(¶m, 1);
1311: } // no need for else frame.empty_params()
1312:
1313: Temp_disable_default_getter temp(junction.self);
1314: call(frame);
1315: return frame.result();
1316: });
1.364 moko 1317: } else {
1318: // getter
1.397 moko 1319: if(param_count>0)
1.386 moko 1320: throw Exception(PARSER_RUNTIME, 0, "getter method must have no parameters (has %d parameters)", param_count);
1.364 moko 1321:
1.397 moko 1322: METHOD_FRAME_ACTION(*junction.method, method_frame/*caller*/, junction.self, {
1323: // no need for frame.empty_params()
1324: call(frame);
1325: return frame.result();
1326: });
1.364 moko 1327: }
1.309 paf 1328:
1.34 paf 1329: }
1.70 paf 1330:
1.389 moko 1331: Value& Request::process(Value& input_value) {
1.297 paf 1332: Junction* junction=input_value.get_junction();
1.307 paf 1333: if(junction) {
1334: if(junction->is_getter) { // is it a getter-junction?
1.389 moko 1335: return process(process_getter(*junction));
1.307 paf 1336: }
1337:
1338: if(junction->code) { // is it a code-junction?
1339: // process it
1.385 moko 1340: ValueRef result;
1.336 misha 1341:
1342: DEBUG_PRINT_STR("ja->\n")
1.238 paf 1343:
1.307 paf 1344: if(!junction->method_frame)
1.386 moko 1345: throw Exception(PARSER_RUNTIME, 0, "junction used outside of context");
1.240 paf 1346:
1.330 misha 1347: SAVE_CONTEXT
1.307 paf 1348:
1349: method_frame=junction->method_frame;
1350: rcontext=junction->rcontext;
1351:
1.389 moko 1352: // for code in [] and () wcontext is set 0
1353: // using the fact in decision "which wrapper to use"
1.399 moko 1354: if(junction->wcontext) {
1.389 moko 1355: // {} code wrapper
1.318 misha 1356: VCodeFrame local(*junction->wcontext);
1.307 paf 1357: wcontext=&local;
1358:
1359: // execute it
1.423 moko 1360: recursion_checked_execute(*junction->code);
1.307 paf 1361:
1.318 misha 1362: result=wcontext->result();
1.307 paf 1363: } else {
1.389 moko 1364: // [] or () code wrapper
1.351 misha 1365: WWrapper local(wcontext);
1.307 paf 1366: wcontext=&local;
1367:
1368: // execute it
1.423 moko 1369: recursion_checked_execute(*junction->code);
1.307 paf 1370:
1371: result=wcontext->result();
1372: }
1373:
1.330 misha 1374: RESTORE_CONTEXT
1.207 paf 1375:
1.336 misha 1376: DEBUG_PRINT_STR("<-ja returned")
1377:
1.307 paf 1378: return result;
1379: }
1380:
1.422 moko 1381: // then it is a method-junction, do not explode it
1.307 paf 1382: // just return it as we do for usual objects
1383: }
1384:
1385: return input_value;
1.85 paf 1386: }
1387:
1.332 misha 1388: void Request::process_write(Value& input_value) {
1389: Junction* junction=input_value.get_junction();
1390: if(junction) {
1.375 moko 1391: // no getter-junction check as process_write is called
1392: // to process method arguments, not from get_element
1.332 misha 1393:
1394: if(junction->code) { // is it a code-junction?
1.389 moko 1395: // process it
1.336 misha 1396:
1397: DEBUG_PRINT_STR("ja->\n")
1398:
1.332 misha 1399: if(!junction->method_frame)
1.386 moko 1400: throw Exception(PARSER_RUNTIME, 0, "junction used outside of context");
1.332 misha 1401:
1402: SAVE_CONTEXT
1403:
1404: method_frame=junction->method_frame;
1405: rcontext=junction->rcontext;
1406:
1.389 moko 1407: // for code in [] and () wcontext is set 0
1.332 misha 1408: // using the fact in decision "which wwrapper to use"
1409: #ifdef OPTIMIZE_CALL
1410: if(wcontext==junction->wcontext){
1411: // no wrappers for wcontext
1.423 moko 1412: recursion_checked_execute(*junction->code);
1.332 misha 1413: RESTORE_CONTEXT
1414:
1415: } else
1416: #endif
1417: if(junction->wcontext) {
1.389 moko 1418: // {} code wrapper
1.332 misha 1419: VCodeFrame local(*junction->wcontext);
1420: wcontext=&local;
1421:
1422: // execute it
1.423 moko 1423: recursion_checked_execute(*junction->code);
1.332 misha 1424: RESTORE_CONTEXT
1.396 moko 1425: write(local.result());
1.332 misha 1426: } else {
1.389 moko 1427: // [] or () code wrapper
1.351 misha 1428: WWrapper local(wcontext);
1.332 misha 1429: wcontext=&local;
1430:
1431: // execute it
1.423 moko 1432: recursion_checked_execute(*junction->code);
1.332 misha 1433: RESTORE_CONTEXT
1.396 moko 1434: write(local.result());
1.332 misha 1435: }
1.336 misha 1436:
1437: DEBUG_PRINT_STR("<-ja returned")
1438:
1.332 misha 1439: return;
1440: }
1441:
1.422 moko 1442: // then it is a method-junction, do not explode it
1.332 misha 1443: // just return it as we do for usual objects
1444: }
1445:
1.396 moko 1446: write(input_value);
1.332 misha 1447: }
1448:
1.408 moko 1449: const String* Request::execute_method(VStateless_class& aclass, const String& method_name, Value* optional_param) {
1450: if(const Method *method=aclass.get_method(method_name)){
1451: METHOD_FRAME_ACTION(*method, method_frame/*caller*/, aclass, {
1.317 misha 1452:
1.408 moko 1453: if(optional_param && method->params_count>0) {
1454: frame.store_params(&optional_param, 1);
1455: } else {
1456: frame.empty_params();
1457: }
1.246 paf 1458:
1.408 moko 1459: // prevent non-string writes for better error reporting
1.397 moko 1460: frame.write(frame);
1.408 moko 1461:
1462: call(frame);
1.208 paf 1463:
1.410 moko 1464: Value &result=frame.result();
1465: if(const String* sresult=result.get_string()){
1466: return sresult;
1467: }
1468: result.bark("is '%s', it has no string representation", &method_name);
1.408 moko 1469: });
1470: }
1471: return 0;
1.242 paf 1472: }
1473:
1.408 moko 1474: bool Request::execute_method_if_exists(VStateless_class& aclass, const String& method_name, Value* optional_param) {
1475: if(const Method *method=aclass.get_method(method_name)){
1476: METHOD_FRAME_ACTION(*method, method_frame/*caller*/, aclass, {
1477:
1478: if(optional_param && method->params_count>0) {
1479: frame.store_params(&optional_param, 1);
1480: } else {
1481: frame.empty_params();
1482: }
1.99 paf 1483:
1.408 moko 1484: call(frame);
1485: });
1486: return true;
1487: }
1488: return false;
1.176 parser 1489: }
1.362 misha 1490:
1.417 moko 1491: bool Request::execute_auto_method_if_exists(VStateless_class& aclass, const String& method_name, Value* optional_param) {
1492: if(const Method *method=aclass.get_method(method_name)){
1493: METHOD_FRAME_ACTION(*method, method_frame/*caller*/, aclass, {
1494: Value* two_params[2];
1495:
1496: if (optional_param && method->params_count>1) {
1497: two_params[0] = optional_param;
1498: two_params[1] = new VString(*new String(aclass.type()));
1499: frame.store_params(two_params, 2);
1500: } else if (optional_param && method->params_count>0) {
1501: frame.store_params(&optional_param, 1);
1502: } else {
1503: frame.empty_params();
1504: }
1505:
1506: call(frame);
1507: });
1508: return true;
1509: }
1510: return false;
1511: }
1512:
1.398 moko 1513: const String* Request::get_method_filespec(const Method* method){
1.404 moko 1514: Operation::Origin origin=get_method_origin(method);
1515: return origin.file_no ? get_used_filespec(origin.file_no) : NULL;
1516: }
1517:
1518: const Operation::Origin Request::get_method_origin(const Method* method){
1519: Operation::Origin origin={0, 0, 0};
1520:
1.362 misha 1521: if(ArrayOperation* code=method->parser_code)
1522: if(code){
1.419 moko 1523: for(Array_iterator<Operation> i(*code); i; ){
1.362 misha 1524: switch( i.next().code ){
1525: case OP::OP_CURLY_CODE__STORE_PARAM:
1526: case OP::OP_EXPR_CODE__STORE_PARAM:
1527: case OP::OP_CURLY_CODE__CONSTRUCT:
1528: case OP::OP_NESTED_CODE:
1529: case OP::OP_OBJECT_POOL:
1530: case OP::OP_STRING_POOL:
1531: case OP::OP_CALL:
1532: case OP::OP_CALL__WRITE:
1533: {
1534: i.next(); // skip local ops
1535: i.next(); // skip next opcode
1536: // continue execution
1537: }
1538: case OP::OP_CONSTRUCT_OBJECT:
1539: case OP::OP_CONSTRUCT_OBJECT__WRITE:
1540: case OP::OP_VALUE:
1541: case OP::OP_STRING__WRITE:
1542: case OP::OP_VALUE__GET_CLASS:
1.383 moko 1543: case OP::OP_VALUE__GET_BASE_CLASS:
1.362 misha 1544: #ifdef OPTIMIZE_BYTECODE_GET_OBJECT_ELEMENT
1545: case OP::OP_GET_OBJECT_ELEMENT:
1546: case OP::OP_GET_OBJECT_ELEMENT__WRITE:
1547: #endif
1548: #ifdef OPTIMIZE_BYTECODE_GET_OBJECT_VAR_ELEMENT
1549: case OP::OP_GET_OBJECT_VAR_ELEMENT:
1550: case OP::OP_GET_OBJECT_VAR_ELEMENT__WRITE:
1551: #endif
1552: #ifdef OPTIMIZE_BYTECODE_GET_ELEMENT
1553: case OP::OP_VALUE__GET_ELEMENT:
1554: case OP::OP_VALUE__GET_ELEMENT__WRITE:
1555: case OP::OP_VALUE__GET_ELEMENT_OR_OPERATOR:
1556: case OP::OP_WITH_ROOT__VALUE__GET_ELEMENT:
1557: #endif
1558: #ifdef OPTIMIZE_BYTECODE_GET_SELF_ELEMENT
1559: case OP::OP_WITH_SELF__VALUE__GET_ELEMENT:
1560: case OP::OP_WITH_SELF__VALUE__GET_ELEMENT__WRITE:
1561: #endif
1.377 moko 1562:
1563: #ifdef FEATURE_GET_ELEMENT4CALL
1564: case OP::OP_GET_ELEMENT4CALL:
1565: #ifdef OPTIMIZE_BYTECODE_GET_OBJECT_ELEMENT
1566: case OP::OP_GET_OBJECT_ELEMENT4CALL:
1567: #endif
1568: #ifdef OPTIMIZE_BYTECODE_GET_OBJECT_VAR_ELEMENT
1569: case OP::OP_GET_OBJECT_VAR_ELEMENT4CALL:
1570: #endif
1571: #endif // FEATURE_GET_ELEMENT4CALL
1572:
1.362 misha 1573: #ifdef OPTIMIZE_BYTECODE_CONSTRUCT
1574: case OP::OP_WITH_ROOT__VALUE__CONSTRUCT_EXPR:
1575: case OP::OP_WITH_ROOT__VALUE__CONSTRUCT_VALUE:
1576: case OP::OP_WITH_WRITE__VALUE__CONSTRUCT_EXPR:
1577: case OP::OP_WITH_WRITE__VALUE__CONSTRUCT_VALUE:
1578: case OP::OP_WITH_SELF__VALUE__CONSTRUCT_EXPR:
1579: case OP::OP_WITH_SELF__VALUE__CONSTRUCT_VALUE:
1580: #endif
1581: {
1582: origin=i.next().origin;
1583: break;
1584: }
1.373 moko 1585: default: break;
1.362 misha 1586: }
1587: if(origin.file_no)
1.404 moko 1588: return origin;
1.362 misha 1589: }
1590: }
1.404 moko 1591: return origin;
1.362 misha 1592: }
1593:
E-mail: