Annotation of parser3/src/main/execute.C, revision 1.396
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.396 ! moko 24: volatile const char * IDENT_EXECUTE_C="$Id: execute.C,v 1.395 2016/11/01 22:53:20 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.386 moko 745: #define METHOD_FRAME_ACTION(action) \
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.394 moko 776: VExpressionFrame frame(*junction->method, method_frame, junction->self);
1.395 moko 777: METHOD_FRAME_ACTION(call(frame));
1.385 moko 778: result=&frame.result();
1.395 moko 779: // desctructor deletes junctions in stack params here
1.370 moko 780: }
781: stack.push(*result);
1.332 misha 782:
1.336 misha 783: DEBUG_PRINT_STR("<-returned")
784:
1.332 misha 785: if(get_skip())
786: return;
787: if(get_interrupted()) {
788: set_interrupted(false);
1.386 moko 789: throw Exception("parser.interrupted", 0, "execution stopped");
1.237 paf 790: }
1.332 misha 791: break;
792: }
1.220 paf 793:
1.332 misha 794: case OP::OP_CALL__WRITE:
795: {
796: ArrayOperation* local_ops=i.next().ops;
797:
1.336 misha 798: DEBUG_PRINT_OPS(local_ops)
799: DEBUG_PRINT_STR("->\n")
800:
1.332 misha 801: Value& value=stack.pop().value();
802:
803: Junction* junction=value.get_junction();
804: if(!junction) {
805: if(value.is("void"))
1.386 moko 806: throw Exception(PARSER_RUNTIME, 0, "undefined method");
1.332 misha 807: else
1.386 moko 808: throw Exception(PARSER_RUNTIME, 0, "is '%s', not a method or junction, can not call it", value.type());
1.332 misha 809: }
810:
1.391 moko 811: const Method& method=*junction->method;
1.332 misha 812: #ifdef OPTIMIZE_CALL
1.353 misha 813: if(method.call_optimization==Method::CO_WITHOUT_FRAME){
1.332 misha 814: if(local_ops){ // store param code goes here
815: size_t first = stack.top_index();
816: execute(*local_ops);
817:
818: MethodParams method_params;
819: method_params.store_params((Value**)stack.ptr(first), stack.top_index()-first);
820: method.check_actual_numbered_params(junction->self, &method_params);
821: method.native_code(*this, method_params); // execute it
822:
823: stack.set_top_index(first);
824: } else {
825: MethodParams method_params;
826: method.check_actual_numbered_params(junction->self, &method_params);
827: method.native_code(*this, method_params); // execute it
828: }
1.353 misha 829: } else if(method.call_optimization==Method::CO_WITHOUT_WCONTEXT){
1.363 moko 830: VMethodFrame frame(method, method_frame, junction->self);
1.392 moko 831: METHOD_FRAME_ACTION(call_write(frame));
1.332 misha 832: } else
833: #endif // OPTIMIZE_CALL
834: {
1.363 moko 835: VMethodFrame frame(method, method_frame, junction->self);
1.392 moko 836: METHOD_FRAME_ACTION(call(frame));
1.396 ! moko 837: write(frame.result());
1.332 misha 838: }
1.336 misha 839:
840: DEBUG_PRINT_STR("<-returned")
1.327 misha 841:
842: if(get_skip())
843: return;
844: if(get_interrupted()) {
845: set_interrupted(false);
1.386 moko 846: throw Exception("parser.interrupted", 0, "execution stopped");
1.327 misha 847: }
1.49 paf 848: break;
849: }
850:
1.348 misha 851: case OP::OP_CONSTRUCT_OBJECT:
852: case OP::OP_CONSTRUCT_OBJECT__WRITE:
853: {
854: debug_origin=i.next().origin;
855: Value& vclass_name=*i.next().value;
1.360 pretende 856: const String& class_name=*vclass_name.get_string(); debug_name=&class_name;
1.348 misha 857:
858: DEBUG_PRINT_STRING(class_name)
859:
1.384 moko 860: VStateless_class* vclass=get_class(class_name);
861: if(!vclass)
862: throw Exception(PARSER_RUNTIME, &class_name, "class is undefined");
1.348 misha 863:
864: debug_origin=i.next().origin;
865: Value& vconstructor_name=*i.next().value;
866: const String& constructor_name=*vconstructor_name.get_string(); debug_name=&constructor_name;
867:
868: DEBUG_PRINT_STRING(constructor_name)
869:
1.384 moko 870: Junction* constructor_junction=get_element(*vclass, constructor_name).get_junction();
1.363 moko 871: if(!constructor_junction)
1.384 moko 872: throw Exception(PARSER_RUNTIME, &constructor_name, "constructor must be declared in class '%s'", vclass->type());
1.350 misha 873:
1.348 misha 874: ArrayOperation* local_ops=i.next().ops;
875: DEBUG_PRINT_OPS(local_ops)
876: DEBUG_PRINT_STR("->\n")
877:
1.370 moko 878: Value *result;
879: {
1.384 moko 880: Value& object=construct(*vclass, *constructor_junction->method);
1.370 moko 881: VConstructorFrame frame(*constructor_junction->method, method_frame, object);
1.392 moko 882: METHOD_FRAME_ACTION(call(frame));
1.370 moko 883: object.enable_default_setter();
1.385 moko 884: result=&frame.result();
1.395 moko 885: // desctructor deletes junctions in stack params here
1.370 moko 886: }
1.355 misha 887:
1.350 misha 888: if(opcode==OP::OP_CONSTRUCT_OBJECT)
1.370 moko 889: stack.push(*result);
1.350 misha 890: else
1.396 ! moko 891: write(*result);
1.348 misha 892:
893: DEBUG_PRINT_STR("<-returned")
894: break;
895: }
896:
1.55 paf 897: // expression ops: unary
1.322 misha 898: case OP::OP_NEG:
1.55 paf 899: {
1.297 paf 900: Value& a=stack.pop().value();
901: Value& value=*new VDouble(-a.as_double());
902: stack.push(value);
1.55 paf 903: break;
904: }
1.322 misha 905: case OP::OP_INV:
1.55 paf 906: {
1.297 paf 907: Value& a=stack.pop().value();
908: Value& value=*new VDouble(~a.as_int());
909: stack.push(value);
1.55 paf 910: break;
911: }
1.322 misha 912: case OP::OP_NOT:
1.55 paf 913: {
1.297 paf 914: Value& a=stack.pop().value();
1.327 misha 915: Value& value=VBool::get(!a.as_bool());
1.297 paf 916: stack.push(value);
1.55 paf 917: break;
918: }
1.322 misha 919: case OP::OP_DEF:
1.62 paf 920: {
1.297 paf 921: Value& a=stack.pop().value();
1.327 misha 922: Value& value=VBool::get(a.is_defined());
1.297 paf 923: stack.push(value);
1.62 paf 924: break;
925: }
1.322 misha 926: case OP::OP_IN:
1.62 paf 927: {
1.297 paf 928: Value& a=stack.pop().value();
929: const String& path=a.as_string();
1.327 misha 930: Value& value=VBool::get(request_info.uri && *request_info.uri && path.this_starts(request_info.uri));
1.297 paf 931: stack.push(value);
1.62 paf 932: break;
933: }
1.322 misha 934: case OP::OP_FEXISTS:
1.62 paf 935: {
1.297 paf 936: Value& a=stack.pop().value();
1.327 misha 937: Value& value=VBool::get(file_exist(absolute(a.as_string())));
1.297 paf 938: stack.push(value);
1.148 paf 939: break;
940: }
1.322 misha 941: case OP::OP_DEXISTS:
1.148 paf 942: {
1.297 paf 943: Value& a=stack.pop().value();
1.327 misha 944: Value& value=VBool::get(dir_exists(absolute(a.as_string())));
1.297 paf 945: stack.push(value);
1.62 paf 946: break;
947: }
1.55 paf 948:
949: // expression ops: binary
1.322 misha 950: case OP::OP_SUB:
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_ADD:
1.53 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_MUL:
1.49 paf 965: {
1.297 paf 966: Value& b=stack.pop().value(); Value& a=stack.pop().value();
967: Value& value=*new VDouble(a.as_double() * b.as_double());
968: stack.push(value);
1.53 paf 969: break;
970: }
1.322 misha 971: case OP::OP_DIV:
1.53 paf 972: {
1.297 paf 973: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1.170 parser 974:
1.297 paf 975: double a_double=a.as_double();
976: double b_double=b.as_double();
1.170 parser 977:
1.171 parser 978: if(b_double == 0) {
1.297 paf 979: //const String* problem_source=b.as_string();
1.386 moko 980: throw Exception("number.zerodivision", 0, "Division by zero");
1.171 parser 981: }
1.170 parser 982:
1.297 paf 983: Value& value=*new VDouble(a_double / b_double);
984: stack.push(value);
1.54 paf 985: break;
986: }
1.322 misha 987: case OP::OP_MOD:
1.55 paf 988: {
1.297 paf 989: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1.170 parser 990:
1.297 paf 991: double a_double=a.as_double();
992: double b_double=b.as_double();
1.170 parser 993:
1.173 parser 994: if(b_double == 0) {
1.297 paf 995: //const String* problem_source=b.as_string();
1.386 moko 996: throw Exception("number.zerodivision", 0, "Modulus by zero");
1.171 parser 997: }
1.170 parser 998:
1.297 paf 999: Value& value=*new VDouble(fmod(a_double, b_double));
1000: stack.push(value);
1.201 paf 1001: break;
1002: }
1.322 misha 1003: case OP::OP_INTDIV:
1.201 paf 1004: {
1.297 paf 1005: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1.201 paf 1006:
1.297 paf 1007: int a_int=a.as_int();
1008: int b_int=b.as_int();
1.201 paf 1009:
1010: if(b_int == 0) {
1.297 paf 1011: //const String* problem_source=b.as_string();
1.386 moko 1012: throw Exception("number.zerodivision", 0, "Division by zero");
1.201 paf 1013: }
1014:
1.297 paf 1015: Value& value=*new VInt(a_int / b_int);
1016: stack.push(value);
1.55 paf 1017: break;
1018: }
1.322 misha 1019: case OP::OP_BIN_SL:
1.274 paf 1020: {
1.297 paf 1021: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1022: Value& value=*new VInt(
1023: a.as_int() <<
1024: b.as_int());
1025: stack.push(value);
1.274 paf 1026: break;
1027: }
1.322 misha 1028: case OP::OP_BIN_SR:
1.274 paf 1029: {
1.297 paf 1030: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1031: Value& value=*new VInt(
1032: a.as_int() >>
1033: b.as_int());
1034: stack.push(value);
1.274 paf 1035: break;
1036: }
1.322 misha 1037: case OP::OP_BIN_AND:
1.54 paf 1038: {
1.297 paf 1039: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1040: Value& value=*new VInt(
1041: a.as_int() &
1042: b.as_int());
1043: stack.push(value);
1.54 paf 1044: break;
1045: }
1.322 misha 1046: case OP::OP_BIN_OR:
1.54 paf 1047: {
1.297 paf 1048: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1049: Value& value=*new VInt(
1050: a.as_int() |
1051: b.as_int());
1052: stack.push(value);
1.55 paf 1053: break;
1054: }
1.322 misha 1055: case OP::OP_BIN_XOR:
1.56 paf 1056: {
1.297 paf 1057: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1058: Value& value=*new VInt(
1059: a.as_int() ^
1060: b.as_int());
1061: stack.push(value);
1.56 paf 1062: break;
1063: }
1.322 misha 1064: case OP::OP_LOG_AND:
1.55 paf 1065: {
1.297 paf 1066: ArrayOperation& local_ops=stack.pop().ops(); Value& a=stack.pop().value();
1.263 paf 1067: bool result;
1.297 paf 1068: if(a.as_bool()) {
1069: execute(local_ops);
1070: Value& b=stack.pop().value();
1071: result=b.as_bool();
1.209 paf 1072: } else
1.263 paf 1073: result=false;
1.327 misha 1074: Value& value=VBool::get(result);
1.297 paf 1075: stack.push(value);
1.55 paf 1076: break;
1077: }
1.322 misha 1078: case OP::OP_LOG_OR:
1.55 paf 1079: {
1.297 paf 1080: ArrayOperation& local_ops=stack.pop().ops(); Value& a=stack.pop().value();
1.263 paf 1081: bool result;
1.297 paf 1082: if(a.as_bool())
1.263 paf 1083: result=true;
1.209 paf 1084: else {
1.297 paf 1085: execute(local_ops);
1086: Value& b=stack.pop().value();
1087: result=b.as_bool();
1.209 paf 1088: }
1.327 misha 1089: Value& value=VBool::get(result);
1.297 paf 1090: stack.push(value);
1.56 paf 1091: break;
1092: }
1.322 misha 1093: case OP::OP_LOG_XOR:
1.56 paf 1094: {
1.297 paf 1095: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1.327 misha 1096: Value& value=VBool::get(a.as_bool() ^ b.as_bool());
1.297 paf 1097: stack.push(value);
1.55 paf 1098: break;
1099: }
1.322 misha 1100: case OP::OP_NUM_LT:
1.55 paf 1101: {
1.297 paf 1102: volatile double b_double=stack.pop().value().as_double();
1103: volatile double a_double=stack.pop().value().as_double();
1.327 misha 1104: Value& value=VBool::get(a_double<b_double);
1.297 paf 1105: stack.push(value);
1.55 paf 1106: break;
1107: }
1.322 misha 1108: case OP::OP_NUM_GT:
1.55 paf 1109: {
1.297 paf 1110: volatile double b_double=stack.pop().value().as_double();
1111: volatile double a_double=stack.pop().value().as_double();
1.327 misha 1112: Value& value=VBool::get(a_double>b_double);
1.297 paf 1113: stack.push(value);
1.55 paf 1114: break;
1115: }
1.322 misha 1116: case OP::OP_NUM_LE:
1.55 paf 1117: {
1.297 paf 1118: volatile double b_double=stack.pop().value().as_double();
1119: volatile double a_double=stack.pop().value().as_double();
1.327 misha 1120: Value& value=VBool::get(a_double<=b_double);
1.297 paf 1121: stack.push(value);
1.55 paf 1122: break;
1123: }
1.322 misha 1124: case OP::OP_NUM_GE:
1.55 paf 1125: {
1.297 paf 1126: volatile double b_double=stack.pop().value().as_double();
1127: volatile double a_double=stack.pop().value().as_double();
1.327 misha 1128: Value& value=VBool::get(a_double>=b_double);
1.297 paf 1129: stack.push(value);
1.55 paf 1130: break;
1131: }
1.322 misha 1132: case OP::OP_NUM_EQ:
1.55 paf 1133: {
1.297 paf 1134: volatile double b_double=stack.pop().value().as_double();
1135: volatile double a_double=stack.pop().value().as_double();
1.327 misha 1136: Value& value=VBool::get(a_double==b_double);
1.297 paf 1137: stack.push(value);
1.55 paf 1138: break;
1139: }
1.322 misha 1140: case OP::OP_NUM_NE:
1.55 paf 1141: {
1.297 paf 1142: volatile double b_double=stack.pop().value().as_double();
1143: volatile double a_double=stack.pop().value().as_double();
1.327 misha 1144: Value& value=VBool::get(a_double!=b_double);
1.297 paf 1145: stack.push(value);
1.54 paf 1146: break;
1147: }
1.322 misha 1148: case OP::OP_STR_LT:
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_GT:
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_LE:
1.58 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_GE:
1.54 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_EQ:
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.58 paf 1181: break;
1182: }
1.322 misha 1183: case OP::OP_STR_NE:
1.58 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.as_string() != b.as_string());
1.297 paf 1187: stack.push(value);
1.103 paf 1188: break;
1189: }
1.322 misha 1190: case OP::OP_IS:
1.103 paf 1191: {
1.297 paf 1192: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1.327 misha 1193: Value& value=VBool::get(a.is(b.as_string().cstr()));
1.297 paf 1194: stack.push(value);
1.28 paf 1195: break;
1196: }
1197:
1.11 paf 1198: default:
1.386 moko 1199: throw Exception(0, 0, "invalid opcode %d", opcode);
1.11 paf 1200: }
1201: }
1.306 paf 1202: } catch(const Exception&) {
1.297 paf 1203: // record it to stack trace
1.301 paf 1204: if(debug_name)
1205: exception_trace.push(Trace(debug_name, debug_origin));
1.297 paf 1206: rethrow;
1207: }
1.1 paf 1208: }
1.17 paf 1209:
1.330 misha 1210: #define SAVE_CONTEXT \
1.386 moko 1211: VMethodFrame *saved_method_frame=method_frame; \
1212: Value* saved_rcontext=rcontext; \
1.330 misha 1213: WContext *saved_wcontext=wcontext;
1214:
1.386 moko 1215: #define RESTORE_CONTEXT \
1216: wcontext=saved_wcontext; \
1217: rcontext=saved_rcontext; \
1.330 misha 1218: method_frame=saved_method_frame;
1219:
1.384 moko 1220: Value& Request::construct(VStateless_class &called_class, const Method &method){
1.363 moko 1221:
1222: if(method.call_type!=Method::CT_STATIC) {
1223: // this is a constructor call
1224: if(Value* result=called_class.create_new_value(fpool)) {
1.319 misha 1225: // some stateless_class creatable derivates
1.363 moko 1226: return *result;
1227: } else
1.319 misha 1228: throw Exception(PARSER_RUNTIME,
1229: 0, //&frame.name(),
1.363 moko 1230: "is not a constructor, system class '%s' can be constructed only implicitly",
1.379 moko 1231: called_class.type());
1.363 moko 1232: } else
1.386 moko 1233: throw Exception(PARSER_RUNTIME, 0, "method is static and can not be used as constructor");
1.363 moko 1234: }
1.319 misha 1235:
1.392 moko 1236: inline void do_call(VMethodFrame& frame, Request &r){
1.363 moko 1237: Value& self=frame.self();
1238: const Method& method=frame.method;
1239: Method::Call_type call_type=self.get_class()==&self ? Method::CT_STATIC : Method::CT_DYNAMIC;
1.319 misha 1240:
1.332 misha 1241: if(method.call_type==Method::CT_ANY || method.call_type==call_type) { // allowed call type?
1242: if(method.native_code) { // native code?
1.363 moko 1243: method.check_actual_numbered_params(self, frame.numbered_params());
1.392 moko 1244: method.native_code(r, *frame.numbered_params()); // execute it
1.332 misha 1245: } else // parser code, execute it
1.392 moko 1246: r.recoursion_checked_execute(*method.parser_code);
1.332 misha 1247: } else
1.386 moko 1248: throw Exception(PARSER_RUNTIME, 0, "is not allowed to be called %s", call_type==Method::CT_STATIC ? "statically" : "dynamically");
1.392 moko 1249: }
1250:
1251: void Request::call(VMethodFrame& frame){
1252: SAVE_CONTEXT
1253:
1254: rcontext=wcontext=method_frame=&frame;
1255:
1256: do_call(frame, *this);
1.334 misha 1257:
1.330 misha 1258: RESTORE_CONTEXT
1.332 misha 1259: }
1260:
1.392 moko 1261: void Request::call_write(VMethodFrame& frame){
1.332 misha 1262: VMethodFrame *saved_method_frame=method_frame;
1263: Value* saved_rcontext=rcontext;
1.319 misha 1264:
1.392 moko 1265: rcontext=method_frame=&frame;
1.332 misha 1266:
1.392 moko 1267: do_call(frame, *this);
1.332 misha 1268:
1269: rcontext=saved_rcontext;
1270: method_frame=saved_method_frame;
1.319 misha 1271: }
1272:
1.330 misha 1273: Value& Request::get_element(Value& ncontext, const String& name) {
1.376 moko 1274: Value* value=ncontext.get_element(name);
1.388 moko 1275: return *(value ? &process(*value) : VVoid::get());
1.376 moko 1276: }
1277:
1.383 moko 1278: #ifdef FEATURE_GET_ELEMENT4CALL
1.376 moko 1279: Value& Request::get_element4call(Value& ncontext, const String& name) {
1280: Value* value=ncontext.get_element4call(name);
1.388 moko 1281: return *(value ? &process(*value) : VVoid::get());
1.309 paf 1282: }
1.383 moko 1283: #endif
1.309 paf 1284:
1.329 misha 1285: void Request::put_element(Value& ncontext, const String& name, Value* value) {
1.309 paf 1286: // put_element can return property-setting-junction
1.371 moko 1287: if(const VJunction* vjunction=ncontext.put_element(name, value))
1.312 paf 1288: if(vjunction!=PUT_ELEMENT_REPLACED_ELEMENT) {
1.363 moko 1289: const Junction& junction = vjunction->junction();
1.364 moko 1290: VConstructorFrame frame(*junction.method, method_frame /*caller*/, junction.self);
1.329 misha 1291:
1.352 misha 1292: size_t param_count=frame.method_params_count();
1.364 moko 1293:
1294: if(junction.auto_name){
1295: // default setter
1296: if(param_count!=2)
1.386 moko 1297: throw Exception(PARSER_RUNTIME, 0, "default setter method must have TWO parameters (has %d parameters)", param_count);
1.364 moko 1298:
1299: Value* params[2] = { new VString(*junction.auto_name), value };
1300: frame.store_params(params, 2);
1301:
1302: Temp_disable_default_setter temp(junction.self);
1.365 moko 1303: execute_method(frame);
1.364 moko 1304: } else {
1305: // setter
1306: if(param_count!=1)
1.386 moko 1307: throw Exception(PARSER_RUNTIME, 0, "setter method must have ONE parameter (has %d parameters)", param_count);
1.364 moko 1308:
1309: frame.store_params(&value, 1);
1.365 moko 1310: execute_method(frame);
1.364 moko 1311: }
1312: }
1313: }
1314:
1.385 moko 1315: Value& Request::process_getter(Junction& junction) {
1.364 moko 1316: VMethodFrame frame(*junction.method, method_frame/*caller*/, junction.self);
1317: size_t param_count=frame.method_params_count();
1318:
1319: if(junction.auto_name){
1320: // default getter
1321: Value *param;
1322:
1323: if(param_count){
1324: if(param_count>1)
1.386 moko 1325: throw Exception(PARSER_RUNTIME, 0, "default getter method can't have more then 1 parameter (has %d parameters)", param_count);
1.364 moko 1326: param=new VString(*junction.auto_name);
1327: frame.store_params(¶m, 1);
1328: } // no need for else frame.empty_params()
1329:
1330: Temp_disable_default_getter temp(junction.self);
1.365 moko 1331: execute_method(frame);
1.364 moko 1332: } else {
1333: // getter
1334: if(param_count!=0)
1.386 moko 1335: throw Exception(PARSER_RUNTIME, 0, "getter method must have no parameters (has %d parameters)", param_count);
1.364 moko 1336:
1337: // no need for frame.empty_params()
1.365 moko 1338: execute_method(frame);
1.364 moko 1339: }
1.309 paf 1340:
1.364 moko 1341: return frame.result();
1.34 paf 1342: }
1.70 paf 1343:
1.389 moko 1344: Value& Request::process(Value& input_value) {
1.297 paf 1345: Junction* junction=input_value.get_junction();
1.307 paf 1346: if(junction) {
1347: if(junction->is_getter) { // is it a getter-junction?
1.389 moko 1348: return process(process_getter(*junction));
1.307 paf 1349: }
1350:
1351: if(junction->code) { // is it a code-junction?
1352: // process it
1.385 moko 1353: ValueRef result;
1.336 misha 1354:
1355: DEBUG_PRINT_STR("ja->\n")
1.238 paf 1356:
1.307 paf 1357: if(!junction->method_frame)
1.386 moko 1358: throw Exception(PARSER_RUNTIME, 0, "junction used outside of context");
1.240 paf 1359:
1.330 misha 1360: SAVE_CONTEXT
1.307 paf 1361:
1362: method_frame=junction->method_frame;
1363: rcontext=junction->rcontext;
1364:
1.389 moko 1365: // for code in [] and () wcontext is set 0
1366: // using the fact in decision "which wrapper to use"
1367: bool using_code_frame=junction->wcontext;
1.307 paf 1368: if(using_code_frame) {
1.389 moko 1369: // {} code wrapper
1.318 misha 1370: VCodeFrame local(*junction->wcontext);
1.307 paf 1371: wcontext=&local;
1372:
1373: // execute it
1374: recoursion_checked_execute(*junction->code);
1375:
1.318 misha 1376: result=wcontext->result();
1.307 paf 1377: } else {
1.389 moko 1378: // [] or () code wrapper
1.351 misha 1379: WWrapper local(wcontext);
1.307 paf 1380: wcontext=&local;
1381:
1382: // execute it
1383: recoursion_checked_execute(*junction->code);
1384:
1385: result=wcontext->result();
1386: }
1387:
1.330 misha 1388: RESTORE_CONTEXT
1.207 paf 1389:
1.336 misha 1390: DEBUG_PRINT_STR("<-ja returned")
1391:
1.307 paf 1392: return result;
1393: }
1394:
1395: // it is then method-junction, do not explode it
1396: // just return it as we do for usual objects
1397: }
1398:
1399: return input_value;
1.85 paf 1400: }
1401:
1.332 misha 1402: void Request::process_write(Value& input_value) {
1403: Junction* junction=input_value.get_junction();
1404: if(junction) {
1.375 moko 1405: // no getter-junction check as process_write is called
1406: // to process method arguments, not from get_element
1.332 misha 1407:
1408: if(junction->code) { // is it a code-junction?
1.389 moko 1409: // process it
1.336 misha 1410:
1411: DEBUG_PRINT_STR("ja->\n")
1412:
1.332 misha 1413: if(!junction->method_frame)
1.386 moko 1414: throw Exception(PARSER_RUNTIME, 0, "junction used outside of context");
1.332 misha 1415:
1416: SAVE_CONTEXT
1417:
1418: method_frame=junction->method_frame;
1419: rcontext=junction->rcontext;
1420:
1.389 moko 1421: // for code in [] and () wcontext is set 0
1.332 misha 1422: // using the fact in decision "which wwrapper to use"
1423: #ifdef OPTIMIZE_CALL
1424: if(wcontext==junction->wcontext){
1425: // no wrappers for wcontext
1426: recoursion_checked_execute(*junction->code);
1427: RESTORE_CONTEXT
1428:
1429: } else
1430: #endif
1431: if(junction->wcontext) {
1.389 moko 1432: // {} code wrapper
1.332 misha 1433: VCodeFrame local(*junction->wcontext);
1434: wcontext=&local;
1435:
1436: // execute it
1437: recoursion_checked_execute(*junction->code);
1438: RESTORE_CONTEXT
1.396 ! moko 1439: write(local.result());
1.332 misha 1440: } else {
1.389 moko 1441: // [] or () code wrapper
1.351 misha 1442: WWrapper local(wcontext);
1.332 misha 1443: wcontext=&local;
1444:
1445: // execute it
1446: recoursion_checked_execute(*junction->code);
1447: RESTORE_CONTEXT
1.396 ! moko 1448: write(local.result());
1.332 misha 1449: }
1.336 misha 1450:
1451: DEBUG_PRINT_STR("<-ja returned")
1452:
1.332 misha 1453: return;
1454: }
1455:
1456: // it is then method-junction, do not explode it
1457: // just return it as we do for usual objects
1458: }
1459:
1.396 ! moko 1460: write(input_value);
1.332 misha 1461: }
1462:
1.365 moko 1463: void Request::execute_method(VMethodFrame& aframe) {
1.330 misha 1464: SAVE_CONTEXT
1.99 paf 1465:
1466: // initialize contexts
1.365 moko 1467: rcontext=wcontext=method_frame=&aframe;
1.99 paf 1468:
1.386 moko 1469: // execute!
1.365 moko 1470: recoursion_checked_execute(*aframe.method.parser_code);
1.208 paf 1471:
1.330 misha 1472: RESTORE_CONTEXT
1.208 paf 1473: }
1474:
1.386 moko 1475: const String* Request::execute_method(Value& aself, const Method& method, Value* optional_param, bool do_return_string) {
1.317 misha 1476:
1.363 moko 1477: VMethodFrame local_frame(method, method_frame/*caller*/, aself);
1.365 moko 1478:
1.329 misha 1479: if(optional_param && local_frame.method_params_count()>0) {
1480: local_frame.store_params(&optional_param, 1);
1.330 misha 1481: } else {
1482: local_frame.empty_params();
1.242 paf 1483: }
1.246 paf 1484:
1485: // prevent non-string writes for better error reporting
1.297 paf 1486: if(do_return_string)
1.357 misha 1487: local_frame.write(local_frame);
1.208 paf 1488:
1.365 moko 1489: execute_method(local_frame);
1.99 paf 1490:
1.365 moko 1491: return do_return_string ? local_frame.get_string() : 0;
1.242 paf 1492: }
1493:
1.297 paf 1494: Request::Execute_nonvirtual_method_result
1.386 moko 1495: Request::execute_nonvirtual_method(VStateless_class& aclass, const String& method_name, VString* optional_param, bool do_return_string) {
1.297 paf 1496: Execute_nonvirtual_method_result result;
1497: result.method=aclass.get_method(method_name);
1498: if(result.method)
1.317 misha 1499: result.string=execute_method(aclass, *result.method, optional_param, do_return_string);
1.297 paf 1500: return result;
1.99 paf 1501: }
1502:
1.386 moko 1503: const String* Request::execute_virtual_method(Value& aself, const String& method_name) {
1.354 misha 1504: if(Value* value=aself.get_element(method_name))
1.297 paf 1505: if(Junction* junction=value->get_junction())
1506: if(const Method *method=junction->method)
1507: return execute_method(aself, *method, 0/*no params*/, true);
1.188 parser 1508:
1.176 parser 1509: return 0;
1510: }
1.362 misha 1511:
1512: const String* Request::get_method_filename(const Method* method){
1513: if(ArrayOperation* code=method->parser_code)
1514: if(code){
1515: Operation::Origin origin={0, 0, 0};
1516: Array_iterator<Operation> i(*code);
1517: while( i.has_next() ){
1518: switch( i.next().code ){
1519: case OP::OP_CURLY_CODE__STORE_PARAM:
1520: case OP::OP_EXPR_CODE__STORE_PARAM:
1521: case OP::OP_CURLY_CODE__CONSTRUCT:
1522: case OP::OP_NESTED_CODE:
1523: case OP::OP_OBJECT_POOL:
1524: case OP::OP_STRING_POOL:
1525: case OP::OP_CALL:
1526: case OP::OP_CALL__WRITE:
1527: {
1528: i.next(); // skip local ops
1529: i.next(); // skip next opcode
1530: // continue execution
1531: }
1532: case OP::OP_CONSTRUCT_OBJECT:
1533: case OP::OP_CONSTRUCT_OBJECT__WRITE:
1534: case OP::OP_VALUE:
1535: case OP::OP_STRING__WRITE:
1536: case OP::OP_VALUE__GET_CLASS:
1.383 moko 1537: case OP::OP_VALUE__GET_BASE_CLASS:
1.362 misha 1538: #ifdef OPTIMIZE_BYTECODE_GET_OBJECT_ELEMENT
1539: case OP::OP_GET_OBJECT_ELEMENT:
1540: case OP::OP_GET_OBJECT_ELEMENT__WRITE:
1541: #endif
1542: #ifdef OPTIMIZE_BYTECODE_GET_OBJECT_VAR_ELEMENT
1543: case OP::OP_GET_OBJECT_VAR_ELEMENT:
1544: case OP::OP_GET_OBJECT_VAR_ELEMENT__WRITE:
1545: #endif
1546: #ifdef OPTIMIZE_BYTECODE_GET_ELEMENT
1547: case OP::OP_VALUE__GET_ELEMENT:
1548: case OP::OP_VALUE__GET_ELEMENT__WRITE:
1549: case OP::OP_VALUE__GET_ELEMENT_OR_OPERATOR:
1550: case OP::OP_WITH_ROOT__VALUE__GET_ELEMENT:
1551: #endif
1552: #ifdef OPTIMIZE_BYTECODE_GET_SELF_ELEMENT
1553: case OP::OP_WITH_SELF__VALUE__GET_ELEMENT:
1554: case OP::OP_WITH_SELF__VALUE__GET_ELEMENT__WRITE:
1555: #endif
1.377 moko 1556:
1557: #ifdef FEATURE_GET_ELEMENT4CALL
1558: case OP::OP_GET_ELEMENT4CALL:
1559: #ifdef OPTIMIZE_BYTECODE_GET_OBJECT_ELEMENT
1560: case OP::OP_GET_OBJECT_ELEMENT4CALL:
1561: #endif
1562: #ifdef OPTIMIZE_BYTECODE_GET_OBJECT_VAR_ELEMENT
1563: case OP::OP_GET_OBJECT_VAR_ELEMENT4CALL:
1564: #endif
1565: #endif // FEATURE_GET_ELEMENT4CALL
1566:
1.362 misha 1567: #ifdef OPTIMIZE_BYTECODE_CONSTRUCT
1568: case OP::OP_WITH_ROOT__VALUE__CONSTRUCT_EXPR:
1569: case OP::OP_WITH_ROOT__VALUE__CONSTRUCT_VALUE:
1570: case OP::OP_WITH_WRITE__VALUE__CONSTRUCT_EXPR:
1571: case OP::OP_WITH_WRITE__VALUE__CONSTRUCT_VALUE:
1572: case OP::OP_WITH_SELF__VALUE__CONSTRUCT_EXPR:
1573: case OP::OP_WITH_SELF__VALUE__CONSTRUCT_VALUE:
1574: #endif
1575: {
1576: origin=i.next().origin;
1577: break;
1578: }
1.373 moko 1579: default: break;
1.362 misha 1580: }
1581: if(origin.file_no)
1582: return get_used_filename(origin.file_no);
1583: }
1584: }
1585: return 0;
1586: }
1587:
E-mail: