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