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