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