|
|
1.117 paf 1: /** @file
1.118 paf 2: Parser: executor part of request class.
3:
1.324 misha 4: Copyright (c) 2001-2009 ArtLebedev Group (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.343 ! misha 8: static const char * const IDENT_EXECUTE_C="$Date: 2009-05-24 07:34:10 $";
1.1 paf 9:
1.152 paf 10: #include "pa_opcode.h"
1.8 paf 11: #include "pa_array.h"
1.11 paf 12: #include "pa_request.h"
1.15 paf 13: #include "pa_vstring.h"
1.22 paf 14: #include "pa_vhash.h"
1.167 parser 15: #include "pa_vvoid.h"
1.145 paf 16: #include "pa_vcode_frame.h"
1.321 misha 17: #include "pa_vmethod_frame.h"
1.38 paf 18: #include "pa_vobject.h"
1.49 paf 19: #include "pa_vdouble.h"
1.54 paf 20: #include "pa_vbool.h"
1.94 paf 21: #include "pa_vtable.h"
1.132 paf 22: #include "pa_vfile.h"
1.144 paf 23: #include "pa_vimage.h"
1.194 parser 24: #include "pa_wwrapper.h"
1.1 paf 25:
1.233 paf 26: //#define DEBUG_EXECUTE
1.157 parser 27:
28: #ifdef DEBUG_EXECUTE
1.1 paf 29: 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.328 misha 36: #ifdef OPTIMIZE_BYTECODE_GET_CLASS
37: "VALUE__GET_CLASS",
38: #else
1.66 paf 39: "GET_CLASS",
1.328 misha 40: #endif
1.182 parser 41: "CONSTRUCT_VALUE", "CONSTRUCT_EXPR", "CURLY_CODE__CONSTRUCT",
1.108 paf 42: "WRITE_VALUE", "WRITE_EXPR_RESULT", "STRING__WRITE",
1.328 misha 43: #ifdef OPTIMIZE_BYTECODE_GET_ELEMENT
44: "VALUE__GET_ELEMENT_OR_OPERATOR",
45: #else
46: "GET_ELEMENT_OR_OPERATOR",
47: #endif
48: "GET_ELEMENT",
1.335 misha 49: #ifdef OPTIMIZE_BYTECODE_GET_OBJECT_ELEMENT
50: "GET_OBJECT_ELEMENT",
51: "GET_OBJECT_ELEMENT__WRITE",
52: #endif
53: #ifdef OPTIMIZE_BYTECODE_GET_OBJECT_VAR_ELEMENT
54: "GET_OBJECT_VAR_ELEMENT",
55: "GET_OBJECT_VAR_ELEMENT__WRITE",
1.334 misha 56: #endif
1.328 misha 57: #ifdef OPTIMIZE_BYTECODE_GET_ELEMENT
58: "VALUE__GET_ELEMENT",
59: #endif
60: "GET_ELEMENT__WRITE",
61: #ifdef OPTIMIZE_BYTECODE_GET_ELEMENT
62: "VALUE__GET_ELEMENT__WRITE",
63: #endif
1.232 paf 64: "OBJECT_POOL", "STRING_POOL",
65: "PREPARE_TO_CONSTRUCT_OBJECT", "PREPARE_TO_EXPRESSION",
66: "CALL", "CALL__WRITE",
1.49 paf 67:
1.337 misha 68: #ifdef OPTIMIZE_BYTECODE_CONSTRUCT
1.338 misha 69: "ROOT_CONSTRUCT_EXPR",
70: "ROOT_ELEMENT_CONSTRUCT_EXPR",
1.342 misha 71: "ROOT_OBJECT_ELEMENT_CONSTRUCT_EXPR",
72: "ROOT_OBJECT_VAR_ELEMENT_CONSTRUCT_EXPR",
1.338 misha 73:
74: "ROOT_CONSTRUCT_VALUE",
75: "ROOT_ELEMENT_CONSTRUCT_VALUE",
1.342 misha 76: "ROOT_OBJECT_ELEMENT_CONSTRUCT_VALUE",
77: "ROOT_OBJECT_VAR_ELEMENT_CONSTRUCT_VALUE",
1.338 misha 78:
79: "WRITE_CONSTRUCT_EXPR",
80: "WRITE_ELEMENT_CONSTRUCT_EXPR",
1.342 misha 81: "WRITE_OBJECT_ELEMENT_CONSTRUCT_EXPR",
82: "WRITE_OBJECT_VAR_ELEMENT_CONSTRUCT_EXPR",
1.338 misha 83:
84: "WRITE_CONSTRUCT_VALUE",
85: "WRITE_ELEMENT_CONSTRUCT_VALUE",
1.342 misha 86: "WRITE_OBJECT_ELEMENT_CONSTRUCT_VALUE",
87: "WRITE_OBJECT_VAR_ELEMENT_CONSTRUCT_VALUE",
1.340 misha 88: #endif
89:
90: #ifdef OPTIMIZE_BYTECODE_CALL_CONSTRUCT
91: "ROOT_CALL_CONSTRUCT_EXPR",
92: //"ROOT_CALL_OBJECT_ELEMENT_CONSTRUCT_EXPR",
93:
94: "ROOT_CALL_CONSTRUCT_VALUE",
95: //"ROOT_CALL_OBJECT_ELEMENT_CONSTRUCT_VALUE",
96:
97: "WRITE_CALL_CONSTRUCT_EXPR",
98: //"WRITE_CALL_OBJECT_ELEMENT_CONSTRUCT_EXPR",
99:
1.338 misha 100: "WRITE_CALL_CONSTRUCT_VALUE",
1.340 misha 101: //"WRITE_CALL_OBJECT_ELEMENT_CONSTRUCT_VALUE",
1.337 misha 102: #endif
103:
1.49 paf 104: // expression ops: unary
1.148 paf 105: "NEG", "INV", "NOT", "DEF", "IN", "FEXISTS", "DEXISTS",
1.49 paf 106: // expression ops: binary
1.201 paf 107: "SUB", "ADD", "MUL", "DIV", "MOD", "INTDIV",
1.275 paf 108: "BIN_SL", "BIN_SR",
1.56 paf 109: "BIN_AND", "BIN_OR", "BIN_XOR",
110: "LOG_AND", "LOG_OR", "LOG_XOR",
1.49 paf 111: "NUM_LT", "NUM_GT", "NUM_LE", "NUM_GE", "NUM_EQ", "NUM_NE",
1.103 paf 112: "STR_LT", "STR_GT", "STR_LE", "STR_GE", "STR_EQ", "STR_NE",
113: "IS"
1.1 paf 114: };
115:
1.342 misha 116: const char* debug_value_to_cstr(Value& value){
117: const String* string=value.get_string();
118:
119: if(string)
120: return string->cstr();
121: else
122: if(value.is_bool())
123: return value.as_bool()?"<true>":"<false>";
124: else
125: return "<value>";
126: }
127:
1.297 paf 128: void va_debug_printf(SAPI_Info& sapi_info, const char* fmt,va_list args) {
1.127 paf 129: char buf[MAX_STRING];
130: vsnprintf(buf, MAX_STRING, fmt, args);
1.297 paf 131: SAPI::log(sapi_info, "%s", buf);
1.107 paf 132: }
133:
1.297 paf 134: void debug_printf(SAPI_Info& sapi_info, const char* fmt, ...) {
1.334 misha 135: va_list args;
136: va_start(args, fmt);
137: va_debug_printf(sapi_info, fmt, args);
138: va_end(args);
1.105 paf 139: }
140:
1.297 paf 141: void debug_dump(SAPI_Info& sapi_info, int level, ArrayOperation& ops) {
142: Array_iterator<Operation> i(ops);
1.190 parser 143: while(i.has_next()) {
1.323 misha 144: OP::OPCODE opcode=i.next().code;
1.1 paf 145:
1.337 misha 146: #if defined(OPTIMIZE_BYTECODE_GET_OBJECT_ELEMENT) || defined(OPTIMIZE_BYTECODE_GET_OBJECT_VAR_ELEMENT)
1.334 misha 147: if(
1.335 misha 148: 1==0
149: #ifdef OPTIMIZE_BYTECODE_GET_OBJECT_ELEMENT
150: || opcode==OP::OP_GET_OBJECT_ELEMENT
151: || opcode==OP::OP_GET_OBJECT_ELEMENT__WRITE
152: #endif
153: #ifdef OPTIMIZE_BYTECODE_GET_OBJECT_VAR_ELEMENT
154: || opcode==OP::OP_GET_OBJECT_VAR_ELEMENT
155: || opcode==OP::OP_GET_OBJECT_VAR_ELEMENT__WRITE
156: #endif
1.337 misha 157: #ifdef OPTIMIZE_BYTECODE_CONSTRUCT
158: || opcode==OP::OP_ROOT_CONSTRUCT_EXPR
1.338 misha 159: || opcode==OP::OP_ROOT_ELEMENT_CONSTRUCT_EXPR
160:
1.337 misha 161: || opcode==OP::OP_ROOT_CONSTRUCT_VALUE
1.338 misha 162: || opcode==OP::OP_ROOT_ELEMENT_CONSTRUCT_VALUE
163:
1.337 misha 164: || opcode==OP::OP_WRITE_CONSTRUCT_EXPR
1.338 misha 165: || opcode==OP::OP_WRITE_ELEMENT_CONSTRUCT_EXPR
166:
1.337 misha 167: || opcode==OP::OP_WRITE_CONSTRUCT_VALUE
1.338 misha 168: || opcode==OP::OP_WRITE_ELEMENT_CONSTRUCT_VALUE
1.340 misha 169: #endif
170: #ifdef OPTIMIZE_BYTECODE_CALL_CONSTRUCT
171: || opcode==OP::OP_ROOT_CALL_CONSTRUCT_EXPR
172: || opcode==OP::OP_ROOT_CALL_CONSTRUCT_VALUE
173:
174: || opcode==OP::OP_WRITE_CALL_CONSTRUCT_EXPR
1.338 misha 175: || opcode==OP::OP_WRITE_CALL_CONSTRUCT_VALUE
1.337 misha 176: #endif
1.334 misha 177: ){
178: i.next(); // skip origin
179: Value& value1=*i.next().value;
180: i.next(); // skip origin
181: Value& value2=*i.next().value;
182: debug_printf(sapi_info,
183: "%*s%s"
184: " \"%s\" \"%s\"",
185: level*4, "", opcode_name[opcode],
1.342 misha 186: debug_value_to_cstr(value1), debug_value_to_cstr(value2));
1.334 misha 187: continue;
188: }
189: #endif
1.328 misha 190: if(
191: opcode==OP::OP_VALUE
192: || opcode==OP::OP_STRING__WRITE
193: #ifdef OPTIMIZE_BYTECODE_GET_CLASS
194: || opcode==OP::OP_VALUE__GET_CLASS
195: #endif
196: #ifdef OPTIMIZE_BYTECODE_GET_ELEMENT
197: || opcode==OP::OP_VALUE__GET_ELEMENT
198: || opcode==OP::OP_VALUE__GET_ELEMENT__WRITE
199: || opcode==OP::OP_VALUE__GET_ELEMENT_OR_OPERATOR
200: #endif
1.342 misha 201: #ifdef OPTIMIZE_BYTECODE_CONSTRUCT
202: || opcode==OP::OP_ROOT_OBJECT_ELEMENT_CONSTRUCT_EXPR
203: || opcode==OP::OP_ROOT_OBJECT_VAR_ELEMENT_CONSTRUCT_EXPR
204:
205: || opcode==OP::OP_ROOT_OBJECT_ELEMENT_CONSTRUCT_VALUE
206: || opcode==OP::OP_ROOT_OBJECT_VAR_ELEMENT_CONSTRUCT_VALUE
207:
208: || opcode==OP::OP_WRITE_OBJECT_ELEMENT_CONSTRUCT_EXPR
209: || opcode==OP::OP_WRITE_OBJECT_VAR_ELEMENT_CONSTRUCT_EXPR
210:
211: || opcode==OP::OP_WRITE_OBJECT_ELEMENT_CONSTRUCT_VALUE
212: || opcode==OP::OP_WRITE_OBJECT_VAR_ELEMENT_CONSTRUCT_VALUE
213: #endif
1.328 misha 214: ) {
1.297 paf 215: Operation::Origin origin=i.next().origin;
216: Value& value=*i.next().value;
1.342 misha 217:
1.297 paf 218: debug_printf(sapi_info,
1.133 paf 219: "%*s%s"
220: " \"%s\" %s",
1.297 paf 221: level*4, "", opcode_name[opcode],
1.342 misha 222: debug_value_to_cstr(value), value.type());
1.133 paf 223: continue;
1.15 paf 224: }
1.297 paf 225: debug_printf(sapi_info, "%*s%s", level*4, "", opcode_name[opcode]);
1.1 paf 226:
1.297 paf 227: switch(opcode) {
1.323 misha 228: case OP::OP_CURLY_CODE__STORE_PARAM:
229: case OP::OP_EXPR_CODE__STORE_PARAM:
230: case OP::OP_CURLY_CODE__CONSTRUCT:
231: case OP::OP_NESTED_CODE:
232: case OP::OP_OBJECT_POOL:
233: case OP::OP_STRING_POOL:
234: case OP::OP_CALL:
235: case OP::OP_CALL__WRITE:
1.297 paf 236: if(ArrayOperation* local_ops=i.next().ops)
237: debug_dump(sapi_info, level+1, *local_ops);
1.1 paf 238: }
239: }
240: }
1.336 misha 241: #define DEBUG_PRINT_STR(str) debug_printf(sapi_info, str);
242: #define DEBUG_PRINT_STRING(value) debug_printf(sapi_info, " \"%s\" ", value.cstr());
1.342 misha 243: #define DEBUG_PRINT_VALUE_AND_TYPE(value) debug_printf(sapi_info, " \"%s\" %s", debug_value_to_cstr(value), value.type());
1.336 misha 244: #define DEBUG_PRINT_OPS(local_ops) \
245: debug_printf(sapi_info, \
246: " (%d)\n", local_ops?local_ops->count():0); \
247: if(local_ops) debug_dump(sapi_info, 1, *local_ops);
248:
249: #else
250: #define DEBUG_PRINT_STR(str)
251: #define DEBUG_PRINT_STRING(value)
1.342 misha 252: #define DEBUG_PRINT_VALUE_AND_TYPE(value)
1.336 misha 253: #define DEBUG_PRINT_OPS(local_ops)
1.157 parser 254: #endif
1.1 paf 255:
1.336 misha 256:
1.297 paf 257: // Request
258:
259: void Request::execute(ArrayOperation& ops) {
260: register Stack<StackItem>& stack=this->stack; // helps a lot on MSVC: 'esi'
1.159 parser 261:
1.304 paf 262: const String* debug_name=0; Operation::Origin debug_origin={0, 0, 0};
1.297 paf 263: try{
1.157 parser 264: #ifdef DEBUG_EXECUTE
1.297 paf 265: debug_printf(sapi_info, "source----------------------------\n");
266: debug_dump(sapi_info, 0, ops);
267: debug_printf(sapi_info, "execution-------------------------\n");
1.157 parser 268: #endif
1.297 paf 269: for(Array_iterator<Operation> i(ops); i.has_next(); ) {
1.322 misha 270: OP::OPCODE opcode=i.next().code;
1.293 paf 271:
1.338 misha 272: bool with_root=true;
273:
1.157 parser 274: #ifdef DEBUG_EXECUTE
1.297 paf 275: debug_printf(sapi_info, "%d:%s", stack.top_index()+1, opcode_name[opcode]);
1.157 parser 276: #endif
1.11 paf 277:
1.297 paf 278: switch(opcode) {
1.51 paf 279: // param in next instruction
1.322 misha 280: case OP::OP_VALUE:
1.32 paf 281: {
1.297 paf 282: debug_origin=i.next().origin;
283: Value& value=*i.next().value;
1.342 misha 284:
285: DEBUG_PRINT_VALUE_AND_TYPE(value)
286:
1.297 paf 287: stack.push(value);
1.32 paf 288: break;
289: }
1.328 misha 290:
291: #ifdef OPTIMIZE_BYTECODE_GET_CLASS
292: case OP::OP_VALUE__GET_CLASS:
293: {
294: // maybe they do ^class:method[] call, remember the fact
295: wcontext->set_somebody_entered_some_class();
296:
297: debug_origin=i.next().origin;
298: Value& value=*i.next().value;
299: const String& name=*value.get_string();
300:
1.336 misha 301: DEBUG_PRINT_STRING(name)
1.328 misha 302:
303: Value* class_value=classes().get(name);
304: if(!class_value)
305: throw Exception(PARSER_RUNTIME,
306: &name,
307: "class is undefined");
308:
309: stack.push(*class_value);
310: break;
311: }
312: #else
1.322 misha 313: case OP::OP_GET_CLASS:
1.38 paf 314: {
1.130 paf 315: // maybe they do ^class:method[] call, remember the fact
1.215 paf 316: wcontext->set_somebody_entered_some_class();
1.98 paf 317:
1.297 paf 318: const String& name=stack.pop().string();
319: Value* value=classes().get(name);
1.120 paf 320: if(!value)
1.316 misha 321: throw Exception(PARSER_RUNTIME,
1.66 paf 322: &name,
1.143 paf 323: "class is undefined");
1.66 paf 324:
1.297 paf 325: stack.push(*value);
1.38 paf 326: break;
327: }
1.328 misha 328: #endif
1.51 paf 329: // OP_WITH
1.322 misha 330: case OP::OP_WITH_ROOT:
1.187 parser 331: {
1.297 paf 332: stack.push(*method_frame);
1.187 parser 333: break;
334: }
1.336 misha 335: case OP::OP_WITH_SELF:
1.37 paf 336: {
1.297 paf 337: stack.push(get_self());
1.37 paf 338: break;
339: }
1.336 misha 340: case OP::OP_WITH_READ:
1.15 paf 341: {
1.297 paf 342: stack.push(*rcontext);
1.20 paf 343: break;
344: }
1.336 misha 345: case OP::OP_WITH_WRITE:
1.20 paf 346: {
1.287 paf 347: if(wcontext==method_frame)
1.316 misha 348: throw Exception(PARSER_RUNTIME,
1.287 paf 349: 0,
350: "$.name outside of $name[...]");
351:
1.297 paf 352: stack.push(*wcontext);
1.20 paf 353: break;
354: }
1.37 paf 355:
1.51 paf 356: // OTHER ACTIONS BUT WITHs
1.322 misha 357: case OP::OP_CONSTRUCT_VALUE:
1.20 paf 358: {
1.297 paf 359: Value& value=stack.pop().value();
360: const String& name=stack.pop().string(); debug_name=&name;
361: Value& ncontext=stack.pop().value();
1.329 misha 362: put_element(ncontext, name, &value);
1.308 paf 363:
1.213 paf 364: break;
365: }
1.337 misha 366:
367: #ifdef OPTIMIZE_BYTECODE_CONSTRUCT
1.338 misha 368: case OP::OP_WRITE_CONSTRUCT_EXPR:
369: case OP::OP_WRITE_CONSTRUCT_VALUE:
370: {
371: if(wcontext==method_frame)
372: throw Exception(PARSER_RUNTIME,
373: 0,
374: "$.name outside of $name[...]");
375: with_root=false;
376: // don't stop here. continue in the next block
377: }
1.337 misha 378: case OP::OP_ROOT_CONSTRUCT_EXPR:
1.338 misha 379: case OP::OP_ROOT_CONSTRUCT_VALUE:
1.337 misha 380: {
381: debug_origin=i.next().origin;
382: const String& name=*i.next().value->get_string(); debug_name=&name;
383: DEBUG_PRINT_STRING(name)
384:
1.338 misha 385: debug_origin=i.next().origin;
386: Value& value=*i.next().value;
1.337 misha 387:
1.338 misha 388: if(
389: opcode == OP::OP_WRITE_CONSTRUCT_EXPR
390: || opcode == OP::OP_ROOT_CONSTRUCT_EXPR
391: ){
1.342 misha 392: put_element( with_root?*method_frame:*wcontext, name, &value.as_expr_result());
1.338 misha 393: } else {
1.342 misha 394: put_element( with_root?*method_frame:*wcontext, name, &value);
1.338 misha 395: }
1.337 misha 396: break;
397: }
398:
1.338 misha 399: case OP::OP_WRITE_ELEMENT_CONSTRUCT_EXPR:
400: case OP::OP_WRITE_ELEMENT_CONSTRUCT_VALUE:
1.337 misha 401: {
402: if(wcontext==method_frame)
403: throw Exception(PARSER_RUNTIME,
404: 0,
405: "$.name outside of $name[...]");
1.338 misha 406: with_root=false;
407: // don't stop here. continue in the next block
408: }
409: case OP::OP_ROOT_ELEMENT_CONSTRUCT_EXPR:
410: case OP::OP_ROOT_ELEMENT_CONSTRUCT_VALUE:
411: {
1.337 misha 412: debug_origin=i.next().origin;
413: const String& name=*i.next().value->get_string(); debug_name=&name;
414: DEBUG_PRINT_STRING(name)
415:
416: debug_origin=i.next().origin;
1.338 misha 417: const String& source_var_name=*i.next().value->get_string(); debug_name=&source_var_name;
418: DEBUG_PRINT_STRING(source_var_name)
1.337 misha 419:
1.338 misha 420: Value& value=get_element(*rcontext, source_var_name);
1.337 misha 421:
1.338 misha 422: if(
423: opcode == OP::OP_WRITE_ELEMENT_CONSTRUCT_EXPR
424: || opcode == OP::OP_ROOT_ELEMENT_CONSTRUCT_EXPR
425: ){
1.342 misha 426: put_element( with_root?*method_frame:*wcontext, name, &value.as_expr_result());
427: } else {
428: put_element( with_root?*method_frame:*wcontext, name, value.is_void()?new VString():&value);
429: }
430: break;
431: }
432:
433: case OP::OP_WRITE_OBJECT_ELEMENT_CONSTRUCT_EXPR:
434: case OP::OP_WRITE_OBJECT_ELEMENT_CONSTRUCT_VALUE:
435: {
436: if(wcontext==method_frame)
437: throw Exception(PARSER_RUNTIME,
438: 0,
439: "$.name outside of $name[...]");
440: with_root=false;
441: // don't stop here. continue in the next block
442: }
443: case OP::OP_ROOT_OBJECT_ELEMENT_CONSTRUCT_EXPR:
444: case OP::OP_ROOT_OBJECT_ELEMENT_CONSTRUCT_VALUE:
445: {
446: debug_origin=i.next().origin;
447: const String& name=*i.next().value->get_string(); debug_name=&name;
448: DEBUG_PRINT_STRING(name)
449:
450: i.next(); // skip OP_GET_OBJECT_ELEMENT
451: debug_origin=i.next().origin;
452: const String& context_name=*i.next().value->get_string(); debug_name=&context_name;
453: DEBUG_PRINT_STRING(context_name)
454:
455: Value& object=get_element(*rcontext, context_name);
456:
457: debug_origin=i.next().origin;
458: const String& field_name=*i.next().value->get_string(); debug_name=&field_name;
459: DEBUG_PRINT_STRING(field_name)
460:
461: Value& value=get_element(object, field_name);
462:
463: if(
464: opcode == OP::OP_WRITE_OBJECT_ELEMENT_CONSTRUCT_EXPR
465: || opcode == OP::OP_ROOT_OBJECT_ELEMENT_CONSTRUCT_EXPR
466: ){
467: put_element( with_root?*method_frame:*wcontext, name, &value.as_expr_result());
468: } else {
469: put_element( with_root?*method_frame:*wcontext, name, value.is_void()?new VString():&value);
470: }
471: break;
472: }
473:
474: case OP::OP_WRITE_OBJECT_VAR_ELEMENT_CONSTRUCT_EXPR:
475: case OP::OP_WRITE_OBJECT_VAR_ELEMENT_CONSTRUCT_VALUE:
476: {
477: if(wcontext==method_frame)
478: throw Exception(PARSER_RUNTIME,
479: 0,
480: "$.name outside of $name[...]");
481: with_root=false;
482: // don't stop here. continue in the next block
483: }
484: case OP::OP_ROOT_OBJECT_VAR_ELEMENT_CONSTRUCT_EXPR:
485: case OP::OP_ROOT_OBJECT_VAR_ELEMENT_CONSTRUCT_VALUE:
486: {
487: debug_origin=i.next().origin;
488: const String& name=*i.next().value->get_string(); debug_name=&name;
489: DEBUG_PRINT_STRING(name)
490:
491: i.next(); // skip OP_GET_OBJECT_VAR_ELEMENT
492: debug_origin=i.next().origin;
493: const String& context_name=*i.next().value->get_string(); debug_name=&context_name;
494: DEBUG_PRINT_STRING(context_name)
495:
496: Value& object=get_element(*rcontext, context_name);
497:
498: debug_origin=i.next().origin;
499: const String& var_name=*i.next().value->get_string(); debug_name=&var_name;
500: DEBUG_PRINT_STRING(var_name)
501:
502: const String* field_name=get_element(*rcontext, var_name).get_string();
503: Value& value=get_element(object, *field_name);
504:
505: if(
506: opcode == OP::OP_WRITE_OBJECT_VAR_ELEMENT_CONSTRUCT_EXPR
507: || opcode == OP::OP_ROOT_OBJECT_VAR_ELEMENT_CONSTRUCT_EXPR
508: ){
509: put_element( with_root?*method_frame:*wcontext, name, &value.as_expr_result());
1.338 misha 510: } else {
1.342 misha 511: put_element( with_root?*method_frame:*wcontext, name, value.is_void()?new VString():&value);
1.338 misha 512: }
1.337 misha 513: break;
514: }
1.340 misha 515: #endif // OPTIMIZE_BYTECODE_CONSTRUCT
1.337 misha 516:
1.340 misha 517: #ifdef OPTIMIZE_BYTECODE_CALL_CONSTRUCT
1.338 misha 518: case OP::OP_WRITE_CALL_CONSTRUCT_EXPR:
519: case OP::OP_WRITE_CALL_CONSTRUCT_VALUE:
1.337 misha 520: {
521: if(wcontext==method_frame)
522: throw Exception(PARSER_RUNTIME,
523: 0,
524: "$.name outside of $name[...]");
1.338 misha 525: with_root=false;
526: // don't stop here. continue in the next block
527: }
1.337 misha 528:
1.338 misha 529: case OP::OP_ROOT_CALL_CONSTRUCT_EXPR:
530: case OP::OP_ROOT_CALL_CONSTRUCT_VALUE:
531: {
1.337 misha 532: debug_origin=i.next().origin;
533: const String& name=*i.next().value->get_string(); debug_name=&name;
534:
1.338 misha 535: DEBUG_PRINT_STRING(name)
536:
537: debug_origin=i.next().origin;
538: const String& method_name=*i.next().value->get_string(); debug_name=&method_name;
539:
540: DEBUG_PRINT_STRING(method_name)
541:
1.342 misha 542: bool is_expr=(opcode==OP::OP_WRITE_CALL_CONSTRUCT_EXPR || opcode==OP::OP_ROOT_CALL_CONSTRUCT_EXPR);
543: if(is_expr)
544: wcontext->set_in_expression(true);
545:
1.338 misha 546: Value* vjunction;
547: if(Method* method=main_class.get_method(method_name)){ // looking operator of that name FIRST
548: if(!method->junction_template)
549: method->junction_template=new VJunction(main_class, method);
550: vjunction=method->junction_template;
551: } else {
552: vjunction=&get_element(*rcontext, method_name);
553: }
554:
555: // CALL
1.340 misha 556: Operation call_op=i.next();
1.338 misha 557: ArrayOperation* local_ops=i.next().ops;
558:
559: DEBUG_PRINT_OPS(local_ops)
560: DEBUG_PRINT_STR("->\n")
561:
562: Junction* junction=vjunction->get_junction();
563: if(!junction) {
564: if(vjunction->is("void"))
565: throw Exception(PARSER_RUNTIME,
566: 0,
567: "undefined method");
568: else
569: throw Exception(PARSER_RUNTIME,
570: 0,
571: "is '%s', not a method or junction, can not call it",
572: vjunction->type());
573: }
574:
575: VMethodFrame frame(*junction, method_frame);
576:
577: if(local_ops){ // store param code goes here
578: size_t first = stack.top_index();
579: execute(*local_ops);
580: frame.store_params((Value**)stack.ptr(first), stack.top_index()-first);
581: op_call(frame);
582: stack.set_top_index(first);
583: } else {
584: frame.empty_params();
585: op_call(frame);
586: }
1.340 misha 587:
1.338 misha 588: DEBUG_PRINT_STR("<-returned")
589:
1.340 misha 590: if(call_op.code==OP::OP_CALL__WRITE){
591: write_assign_lang(frame.result());
1.338 misha 592: } else {
1.340 misha 593: Value& value=frame.result().as_value();
594:
1.342 misha 595: if(is_expr){
596: wcontext->set_in_expression(false);
597: put_element( with_root?*method_frame:*wcontext, name, &value.as_expr_result());
1.340 misha 598: } else {
1.342 misha 599: put_element( with_root?*method_frame:*wcontext, name, &value);
1.340 misha 600: }
1.338 misha 601: }
1.337 misha 602:
1.338 misha 603: if(get_skip())
604: return;
605: if(get_interrupted()) {
606: set_interrupted(false);
607: throw Exception("parser.interrupted",
608: 0,
609: "execution stopped");
610: }
1.337 misha 611:
612: break;
613: }
1.340 misha 614: #endif // OPTIMIZE_BYTECODE_CALL_CONSTRUCT
1.337 misha 615:
1.322 misha 616: case OP::OP_CONSTRUCT_EXPR:
1.80 paf 617: {
1.219 paf 618: // see OP_PREPARE_TO_EXPRESSION
619: wcontext->set_in_expression(false);
620:
1.308 paf 621: Value& expr=stack.pop().value();
1.297 paf 622: const String& name=stack.pop().string(); debug_name=&name;
623: Value& ncontext=stack.pop().value();
1.308 paf 624: Value& value=expr.as_expr_result();
1.329 misha 625: put_element(ncontext, name, &value);
1.80 paf 626: break;
627: }
1.337 misha 628:
1.322 misha 629: case OP::OP_CURLY_CODE__CONSTRUCT:
1.182 parser 630: {
1.297 paf 631: ArrayOperation& local_ops=*i.next().ops;
1.336 misha 632:
633: DEBUG_PRINT_OPS((&local_ops))
634:
1.326 misha 635: VJunction& value=*new VJunction(
1.297 paf 636: get_self(), 0,
1.257 paf 637: method_frame,
1.240 paf 638: rcontext,
639: wcontext,
1.312 paf 640: &local_ops);
1.238 paf 641:
1.326 misha 642: wcontext->attach_junction(&value);
643:
1.297 paf 644: const String& name=stack.pop().string(); debug_name=&name;
645: Value& ncontext=stack.pop().value();
1.312 paf 646: if(const VJunction* vjunction=ncontext.put_element(ncontext, name, &value, false))
647: if(vjunction!=PUT_ELEMENT_REPLACED_ELEMENT)
1.316 misha 648: throw Exception(PARSER_RUNTIME,
1.308 paf 649: 0,
650: "property value can not be code, use [] or () brackets");
651:
1.182 parser 652: break;
653: }
1.322 misha 654: case OP::OP_NESTED_CODE:
1.209 paf 655: {
1.297 paf 656: ArrayOperation& local_ops=*i.next().ops;
1.336 misha 657:
658: DEBUG_PRINT_OPS((&local_ops))
659:
1.297 paf 660: stack.push(local_ops);
1.209 paf 661: break;
662: }
1.322 misha 663: case OP::OP_WRITE_VALUE:
1.13 paf 664: {
1.297 paf 665: Value& value=stack.pop().value();
666: write_assign_lang(value);
1.86 paf 667: break;
668: }
1.322 misha 669: case OP::OP_WRITE_EXPR_RESULT:
1.108 paf 670: {
1.297 paf 671: Value& value=stack.pop().value();
672: write_no_lang(value.as_expr_result());
1.230 paf 673:
674: // must be after write(result) and
1.219 paf 675: // see OP_PREPARE_TO_EXPRESSION
676: wcontext->set_in_expression(false);
1.108 paf 677: break;
678: }
1.322 misha 679: case OP::OP_STRING__WRITE:
1.86 paf 680: {
1.297 paf 681: i.next(); // ignore origin
682: Value* value=i.next().value;
1.336 misha 683:
684: const String& string_value=*value->get_string();
685:
686: DEBUG_PRINT_STRING(string_value)
687:
688: write_no_lang(string_value);
1.13 paf 689: break;
1.14 paf 690: }
1.328 misha 691:
692: #ifdef OPTIMIZE_BYTECODE_GET_ELEMENT
693: case OP::OP_VALUE__GET_ELEMENT_OR_OPERATOR:
694: {
695: debug_origin=i.next().origin;
696: const String& name=*i.next().value->get_string(); debug_name=&name;
697:
1.336 misha 698: DEBUG_PRINT_STRING(name)
699:
1.330 misha 700: if(Method* method=main_class.get_method(name)){ // looking operator of that name FIRST
701: if(!method->junction_template) method->junction_template=new VJunction(main_class, method);
702: stack.push(*method->junction_template);
703: break;
704: }
705: Value& value=get_element(*rcontext, name);
1.328 misha 706: stack.push(value);
707: break;
708: }
709: #else
1.322 misha 710: case OP::OP_GET_ELEMENT_OR_OPERATOR:
1.215 paf 711: {
1.297 paf 712: const String& name=stack.pop().string(); debug_name=&name;
713: Value& ncontext=stack.pop().value();
1.330 misha 714: if(Method* method=main_class.get_method(name)){ // looking operator of that name FIRST
715: if(!method->junction_template) method->junction_template=new VJunction(main_class, method);
716: stack.push(*method->junction_template);
717: break;
718: }
719: Value& value=get_element(ncontext, name);
1.297 paf 720: stack.push(value);
1.215 paf 721: break;
722: }
1.328 misha 723: #endif
724:
1.335 misha 725: #ifdef OPTIMIZE_BYTECODE_GET_OBJECT_ELEMENT
726: case OP::OP_GET_OBJECT_ELEMENT:
727: case OP::OP_GET_OBJECT_ELEMENT__WRITE:
1.334 misha 728: {
729: debug_origin=i.next().origin;
730: const String& context_name=*i.next().value->get_string(); debug_name=&context_name;
1.336 misha 731:
732: DEBUG_PRINT_STRING(context_name)
733:
1.334 misha 734: Value& object=get_element(*rcontext, context_name);
735:
736: debug_origin=i.next().origin;
737: const String& field_name=*i.next().value->get_string(); debug_name=&field_name;
1.336 misha 738:
739: DEBUG_PRINT_STRING(field_name)
740:
1.334 misha 741: Value& value=get_element(object, field_name);
742:
743: i.next(); // skip last OP_GET_ELEMENT
744:
1.335 misha 745: if(opcode==OP::OP_GET_OBJECT_ELEMENT){
746: stack.push(value);
747: } else {
748: write_assign_lang(value);
749: }
750: break;
751: }
752: #endif
753:
754: #ifdef OPTIMIZE_BYTECODE_GET_OBJECT_VAR_ELEMENT
755: case OP::OP_GET_OBJECT_VAR_ELEMENT:
756: case OP::OP_GET_OBJECT_VAR_ELEMENT__WRITE:
757: {
758: debug_origin=i.next().origin;
759: const String& context_name=*i.next().value->get_string(); debug_name=&context_name;
1.336 misha 760:
761: DEBUG_PRINT_STRING(context_name)
762:
1.335 misha 763: Value& object=get_element(*rcontext, context_name);
764:
765: debug_origin=i.next().origin;
766: const String& var_name=*i.next().value->get_string(); debug_name=&var_name;
1.336 misha 767:
768: DEBUG_PRINT_STRING(var_name)
769:
1.335 misha 770: const String* field=get_element(*rcontext, var_name).get_string();
771:
772: Value& value=get_element(object, *field);
773:
774: i.next(); // skip last OP_GET_ELEMENT
775:
776: if(opcode==OP::OP_GET_OBJECT_VAR_ELEMENT){
1.334 misha 777: stack.push(value);
778: } else {
779: write_assign_lang(value);
780: }
781: break;
782: }
783: #endif
784:
1.322 misha 785: case OP::OP_GET_ELEMENT:
1.11 paf 786: {
1.297 paf 787: const String& name=stack.pop().string(); debug_name=&name;
788: Value& ncontext=stack.pop().value();
1.330 misha 789: Value& value=get_element(ncontext, name);
1.297 paf 790: stack.push(value);
1.17 paf 791: break;
792: }
793:
1.328 misha 794: #ifdef OPTIMIZE_BYTECODE_GET_ELEMENT
795: case OP::OP_VALUE__GET_ELEMENT:
796: {
797: debug_origin=i.next().origin;
798: const String& name=*i.next().value->get_string(); debug_name=&name;
799:
1.336 misha 800: DEBUG_PRINT_STRING(name)
1.328 misha 801:
1.330 misha 802: Value& value=get_element(*rcontext, name);
1.328 misha 803: stack.push(value);
804: break;
805: }
806: #endif
807:
808:
1.322 misha 809: case OP::OP_GET_ELEMENT__WRITE:
1.17 paf 810: {
1.297 paf 811: const String& name=stack.pop().string(); debug_name=&name;
812: Value& ncontext=stack.pop().value();
1.330 misha 813: Value& value=get_element(ncontext, name);
1.297 paf 814: write_assign_lang(value);
1.17 paf 815: break;
816: }
817:
1.328 misha 818: #ifdef OPTIMIZE_BYTECODE_GET_ELEMENT
819: case OP::OP_VALUE__GET_ELEMENT__WRITE:
820: {
821: debug_origin=i.next().origin;
822: const String& name=*i.next().value->get_string(); debug_name=&name;
823:
1.336 misha 824: DEBUG_PRINT_STRING(name)
1.328 misha 825:
1.330 misha 826: Value& value=get_element(*rcontext, name);
1.328 misha 827: write_assign_lang(value);
828: break;
829: }
830: #endif
1.32 paf 831:
1.322 misha 832: case OP::OP_OBJECT_POOL:
1.17 paf 833: {
1.297 paf 834: ArrayOperation& local_ops=*i.next().ops;
1.226 paf 835:
1.257 paf 836: WContext *saved_wcontext=wcontext;
1.297 paf 837: String::Language saved_lang=flang;
838: flang=String::L_PASS_APPENDED;
839: WWrapper local(0/*empty*/, wcontext);
1.226 paf 840: wcontext=&local;
841:
1.297 paf 842: execute(local_ops);
1.226 paf 843:
1.297 paf 844: stack.push(wcontext->result().as_value());
1.257 paf 845: flang=saved_lang;
846: wcontext=saved_wcontext;
1.13 paf 847: break;
1.15 paf 848: }
1.13 paf 849:
1.322 misha 850: case OP::OP_STRING_POOL:
1.49 paf 851: {
1.297 paf 852: ArrayOperation& local_ops=*i.next().ops;
1.226 paf 853:
1.257 paf 854: WContext *saved_wcontext=wcontext;
1.297 paf 855: WWrapper local(0 /*empty*/, wcontext);
1.226 paf 856: wcontext=&local;
857:
1.297 paf 858: execute(local_ops);
1.226 paf 859:
1.297 paf 860: Value* value;
1.49 paf 861: // from "$a $b" part of expression taking only string value,
862: // ignoring any other content of wcontext
1.297 paf 863: if(const String* string=wcontext->get_string())
864: value=new VString(*string);
1.80 paf 865: else
1.324 misha 866: value=VVoid::get();
1.297 paf 867: stack.push(*value);
868:
1.257 paf 869: wcontext=saved_wcontext;
1.49 paf 870: break;
871: }
872:
1.51 paf 873: // CALL
1.322 misha 874: case OP::OP_CURLY_CODE__STORE_PARAM:
875: case OP::OP_EXPR_CODE__STORE_PARAM:
1.28 paf 876: {
1.237 paf 877: // code
1.297 paf 878: ArrayOperation& local_ops=*i.next().ops;
1.336 misha 879:
880: DEBUG_PRINT_OPS((&local_ops))
881:
1.237 paf 882: // when they evaluate expression parameter,
883: // the object expression result
884: // does not need to be written into calling frame
885: // it must go into any expressions using that parameter
886: // hence, we zero junction.wcontext here, and later
887: // in .process we would test that field
888: // in decision "which wwrapper to use"
1.326 misha 889: VJunction& value=*new VJunction(
1.297 paf 890: get_self(), 0,
1.257 paf 891: method_frame,
1.237 paf 892: rcontext,
1.322 misha 893: opcode==OP::OP_EXPR_CODE__STORE_PARAM?0:wcontext,
1.312 paf 894: &local_ops);
1.343 ! misha 895: #ifndef USE_DESTRUCTORS
1.326 misha 896: if (opcode!=OP::OP_EXPR_CODE__STORE_PARAM)
1.343 ! misha 897: wcontext->attach_junction(&value);
1.326 misha 898: #endif
1.237 paf 899: // store param
1.329 misha 900: stack.push(value);
1.29 paf 901: break;
902: }
903:
1.322 misha 904: case OP::OP_PREPARE_TO_CONSTRUCT_OBJECT:
1.186 parser 905: {
1.200 paf 906: wcontext->set_constructing(true);
1.186 parser 907: break;
908: }
1.219 paf 909:
1.322 misha 910: case OP::OP_PREPARE_TO_EXPRESSION:
1.219 paf 911: {
912: wcontext->set_in_expression(true);
913: break;
914: }
915:
1.322 misha 916: case OP::OP_CALL:
1.29 paf 917: {
1.297 paf 918: ArrayOperation* local_ops=i.next().ops;
1.237 paf 919:
1.336 misha 920: DEBUG_PRINT_OPS(local_ops)
921: DEBUG_PRINT_STR("->\n")
922:
1.297 paf 923: Value& value=stack.pop().value();
1.237 paf 924:
1.297 paf 925: Junction* junction=value.get_junction();
926: if(!junction) {
927: if(value.is("void"))
1.316 misha 928: throw Exception(PARSER_RUNTIME,
1.297 paf 929: 0,
930: "undefined method");
931: else
1.316 misha 932: throw Exception(PARSER_RUNTIME,
1.297 paf 933: 0,
934: "is '%s', not a method or junction, can not call it",
935: value.type());
936: }
1.237 paf 937:
1.321 misha 938: VMethodFrame frame(*junction, method_frame);
1.330 misha 939:
1.321 misha 940: if(local_ops){ // store param code goes here
1.329 misha 941: size_t first = stack.top_index();
1.321 misha 942: execute(*local_ops);
1.329 misha 943: frame.store_params((Value**)stack.ptr(first), stack.top_index()-first);
1.332 misha 944: op_call(frame);
1.329 misha 945: stack.set_top_index(first);
1.330 misha 946: } else {
947: frame.empty_params();
1.332 misha 948: op_call(frame);
1.321 misha 949: }
1.330 misha 950:
1.332 misha 951: stack.push(frame.result().as_value());
952:
1.336 misha 953: DEBUG_PRINT_STR("<-returned")
954:
1.332 misha 955: if(get_skip())
956: return;
957: if(get_interrupted()) {
958: set_interrupted(false);
959: throw Exception("parser.interrupted",
960: 0,
961: "execution stopped");
1.237 paf 962: }
1.332 misha 963: break;
964: }
1.220 paf 965:
1.332 misha 966: case OP::OP_CALL__WRITE:
967: {
968: ArrayOperation* local_ops=i.next().ops;
969:
1.336 misha 970: DEBUG_PRINT_OPS(local_ops)
971: DEBUG_PRINT_STR("->\n")
972:
1.332 misha 973: Value& value=stack.pop().value();
974:
975: Junction* junction=value.get_junction();
976: if(!junction) {
977: if(value.is("void"))
978: throw Exception(PARSER_RUNTIME,
979: 0,
980: "undefined method");
981: else
982: throw Exception(PARSER_RUNTIME,
983: 0,
984: "is '%s', not a method or junction, can not call it",
985: value.type());
986: }
987:
988: #ifdef OPTIMIZE_CALL
989: const Method& method=*junction->method;
990: if (!wcontext->get_constructing() && method.call_optimization==Method::CO_WITHOUT_FRAME){
991: if(local_ops){ // store param code goes here
992: size_t first = stack.top_index();
993: execute(*local_ops);
994:
995: MethodParams method_params;
996: method_params.store_params((Value**)stack.ptr(first), stack.top_index()-first);
997: method.check_actual_numbered_params(junction->self, &method_params);
998: method.native_code(*this, method_params); // execute it
999:
1000: stack.set_top_index(first);
1001: } else {
1002: MethodParams method_params;
1003: method.check_actual_numbered_params(junction->self, &method_params);
1004: method.native_code(*this, method_params); // execute it
1005: }
1006: } else if (!wcontext->get_constructing() && method.call_optimization==Method::CO_WITHOUT_WCONTEXT){
1007: VMethodFrame frame(*junction, method_frame);
1008: if(local_ops){ // store param code goes here
1009: size_t first = stack.top_index();
1010: execute(*local_ops);
1011:
1012: frame.store_params((Value**)stack.ptr(first), stack.top_index()-first);
1013: op_call_write(frame);
1014:
1015: stack.set_top_index(first);
1016: } else {
1017: frame.empty_params();
1018: op_call_write(frame);
1019: }
1020: } else
1021: #endif // OPTIMIZE_CALL
1022: {
1023: VMethodFrame frame(*junction, method_frame);
1024: if(local_ops){ // store param code goes here
1025: size_t first = stack.top_index();
1026: execute(*local_ops);
1027:
1028: frame.store_params((Value**)stack.ptr(first), stack.top_index()-first);
1029: op_call(frame);
1030:
1031: stack.set_top_index(first);
1032: } else {
1033: frame.empty_params();
1034: op_call(frame);
1035: }
1036: write_assign_lang(frame.result());
1037: }
1.336 misha 1038:
1039: DEBUG_PRINT_STR("<-returned")
1.327 misha 1040:
1041: if(get_skip())
1042: return;
1043: if(get_interrupted()) {
1044: set_interrupted(false);
1045: throw Exception("parser.interrupted",
1046: 0,
1047: "execution stopped");
1048: }
1.49 paf 1049: break;
1050: }
1051:
1.55 paf 1052: // expression ops: unary
1.322 misha 1053: case OP::OP_NEG:
1.55 paf 1054: {
1.297 paf 1055: Value& a=stack.pop().value();
1056: Value& value=*new VDouble(-a.as_double());
1057: stack.push(value);
1.55 paf 1058: break;
1059: }
1.322 misha 1060: case OP::OP_INV:
1.55 paf 1061: {
1.297 paf 1062: Value& a=stack.pop().value();
1063: Value& value=*new VDouble(~a.as_int());
1064: stack.push(value);
1.55 paf 1065: break;
1066: }
1.322 misha 1067: case OP::OP_NOT:
1.55 paf 1068: {
1.297 paf 1069: Value& a=stack.pop().value();
1.327 misha 1070: Value& value=VBool::get(!a.as_bool());
1.297 paf 1071: stack.push(value);
1.55 paf 1072: break;
1073: }
1.322 misha 1074: case OP::OP_DEF:
1.62 paf 1075: {
1.297 paf 1076: Value& a=stack.pop().value();
1.327 misha 1077: Value& value=VBool::get(a.is_defined());
1.297 paf 1078: stack.push(value);
1.62 paf 1079: break;
1080: }
1.322 misha 1081: case OP::OP_IN:
1.62 paf 1082: {
1.297 paf 1083: Value& a=stack.pop().value();
1084: const String& path=a.as_string();
1.327 misha 1085: Value& value=VBool::get(request_info.uri && *request_info.uri && path.this_starts(request_info.uri));
1.297 paf 1086: stack.push(value);
1.62 paf 1087: break;
1088: }
1.322 misha 1089: case OP::OP_FEXISTS:
1.62 paf 1090: {
1.297 paf 1091: Value& a=stack.pop().value();
1.327 misha 1092: Value& value=VBool::get(file_exist(absolute(a.as_string())));
1.297 paf 1093: stack.push(value);
1.148 paf 1094: break;
1095: }
1.322 misha 1096: case OP::OP_DEXISTS:
1.148 paf 1097: {
1.297 paf 1098: Value& a=stack.pop().value();
1.327 misha 1099: Value& value=VBool::get(dir_exists(absolute(a.as_string())));
1.297 paf 1100: stack.push(value);
1.62 paf 1101: break;
1102: }
1.55 paf 1103:
1104: // expression ops: binary
1.322 misha 1105: case OP::OP_SUB:
1.53 paf 1106: {
1.297 paf 1107: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1108: Value& value=*new VDouble(a.as_double() - b.as_double());
1109: stack.push(value);
1.53 paf 1110: break;
1111: }
1.322 misha 1112: case OP::OP_ADD:
1.53 paf 1113: {
1.297 paf 1114: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1115: Value& value=*new VDouble(a.as_double() + b.as_double());
1116: stack.push(value);
1.53 paf 1117: break;
1118: }
1.322 misha 1119: case OP::OP_MUL:
1.49 paf 1120: {
1.297 paf 1121: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1122: Value& value=*new VDouble(a.as_double() * b.as_double());
1123: stack.push(value);
1.53 paf 1124: break;
1125: }
1.322 misha 1126: case OP::OP_DIV:
1.53 paf 1127: {
1.297 paf 1128: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1.170 parser 1129:
1.297 paf 1130: double a_double=a.as_double();
1131: double b_double=b.as_double();
1.170 parser 1132:
1.171 parser 1133: if(b_double == 0) {
1.297 paf 1134: //const String* problem_source=b.as_string();
1.223 paf 1135: throw Exception("number.zerodivision",
1.297 paf 1136: 0, //problem_source,
1.170 parser 1137: "Division by zero");
1.171 parser 1138: }
1.170 parser 1139:
1.297 paf 1140: Value& value=*new VDouble(a_double / b_double);
1141: stack.push(value);
1.54 paf 1142: break;
1143: }
1.322 misha 1144: case OP::OP_MOD:
1.55 paf 1145: {
1.297 paf 1146: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1.170 parser 1147:
1.297 paf 1148: double a_double=a.as_double();
1149: double b_double=b.as_double();
1.170 parser 1150:
1.173 parser 1151: if(b_double == 0) {
1.297 paf 1152: //const String* problem_source=b.as_string();
1.223 paf 1153: throw Exception("number.zerodivision",
1.297 paf 1154: 0, //problem_source,
1.170 parser 1155: "Modulus by zero");
1.171 parser 1156: }
1.170 parser 1157:
1.297 paf 1158: Value& value=*new VDouble(fmod(a_double, b_double));
1159: stack.push(value);
1.201 paf 1160: break;
1161: }
1.322 misha 1162: case OP::OP_INTDIV:
1.201 paf 1163: {
1.297 paf 1164: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1.201 paf 1165:
1.297 paf 1166: int a_int=a.as_int();
1167: int b_int=b.as_int();
1.201 paf 1168:
1169: if(b_int == 0) {
1.297 paf 1170: //const String* problem_source=b.as_string();
1.223 paf 1171: throw Exception("number.zerodivision",
1.297 paf 1172: 0, //problem_source,
1.201 paf 1173: "Division by zero");
1174: }
1175:
1.297 paf 1176: Value& value=*new VInt(a_int / b_int);
1177: stack.push(value);
1.55 paf 1178: break;
1179: }
1.322 misha 1180: case OP::OP_BIN_SL:
1.274 paf 1181: {
1.297 paf 1182: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1183: Value& value=*new VInt(
1184: a.as_int() <<
1185: b.as_int());
1186: stack.push(value);
1.274 paf 1187: break;
1188: }
1.322 misha 1189: case OP::OP_BIN_SR:
1.274 paf 1190: {
1.297 paf 1191: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1192: Value& value=*new VInt(
1193: a.as_int() >>
1194: b.as_int());
1195: stack.push(value);
1.274 paf 1196: break;
1197: }
1.322 misha 1198: case OP::OP_BIN_AND:
1.54 paf 1199: {
1.297 paf 1200: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1201: Value& value=*new VInt(
1202: a.as_int() &
1203: b.as_int());
1204: stack.push(value);
1.54 paf 1205: break;
1206: }
1.322 misha 1207: case OP::OP_BIN_OR:
1.54 paf 1208: {
1.297 paf 1209: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1210: Value& value=*new VInt(
1211: a.as_int() |
1212: b.as_int());
1213: stack.push(value);
1.55 paf 1214: break;
1215: }
1.322 misha 1216: case OP::OP_BIN_XOR:
1.56 paf 1217: {
1.297 paf 1218: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1219: Value& value=*new VInt(
1220: a.as_int() ^
1221: b.as_int());
1222: stack.push(value);
1.56 paf 1223: break;
1224: }
1.322 misha 1225: case OP::OP_LOG_AND:
1.55 paf 1226: {
1.297 paf 1227: ArrayOperation& local_ops=stack.pop().ops(); Value& a=stack.pop().value();
1.263 paf 1228: bool result;
1.297 paf 1229: if(a.as_bool()) {
1230: execute(local_ops);
1231: Value& b=stack.pop().value();
1232: result=b.as_bool();
1.209 paf 1233: } else
1.263 paf 1234: result=false;
1.327 misha 1235: Value& value=VBool::get(result);
1.297 paf 1236: stack.push(value);
1.55 paf 1237: break;
1238: }
1.322 misha 1239: case OP::OP_LOG_OR:
1.55 paf 1240: {
1.297 paf 1241: ArrayOperation& local_ops=stack.pop().ops(); Value& a=stack.pop().value();
1.263 paf 1242: bool result;
1.297 paf 1243: if(a.as_bool())
1.263 paf 1244: result=true;
1.209 paf 1245: else {
1.297 paf 1246: execute(local_ops);
1247: Value& b=stack.pop().value();
1248: result=b.as_bool();
1.209 paf 1249: }
1.327 misha 1250: Value& value=VBool::get(result);
1.297 paf 1251: stack.push(value);
1.56 paf 1252: break;
1253: }
1.322 misha 1254: case OP::OP_LOG_XOR:
1.56 paf 1255: {
1.297 paf 1256: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1.327 misha 1257: Value& value=VBool::get(a.as_bool() ^ b.as_bool());
1.297 paf 1258: stack.push(value);
1.55 paf 1259: break;
1260: }
1.322 misha 1261: case OP::OP_NUM_LT:
1.55 paf 1262: {
1.297 paf 1263: volatile double b_double=stack.pop().value().as_double();
1264: volatile double a_double=stack.pop().value().as_double();
1.327 misha 1265: Value& value=VBool::get(a_double<b_double);
1.297 paf 1266: stack.push(value);
1.55 paf 1267: break;
1268: }
1.322 misha 1269: case OP::OP_NUM_GT:
1.55 paf 1270: {
1.297 paf 1271: volatile double b_double=stack.pop().value().as_double();
1272: volatile double a_double=stack.pop().value().as_double();
1.327 misha 1273: Value& value=VBool::get(a_double>b_double);
1.297 paf 1274: stack.push(value);
1.55 paf 1275: break;
1276: }
1.322 misha 1277: case OP::OP_NUM_LE:
1.55 paf 1278: {
1.297 paf 1279: volatile double b_double=stack.pop().value().as_double();
1280: volatile double a_double=stack.pop().value().as_double();
1.327 misha 1281: Value& value=VBool::get(a_double<=b_double);
1.297 paf 1282: stack.push(value);
1.55 paf 1283: break;
1284: }
1.322 misha 1285: case OP::OP_NUM_GE:
1.55 paf 1286: {
1.297 paf 1287: volatile double b_double=stack.pop().value().as_double();
1288: volatile double a_double=stack.pop().value().as_double();
1.327 misha 1289: Value& value=VBool::get(a_double>=b_double);
1.297 paf 1290: stack.push(value);
1.55 paf 1291: break;
1292: }
1.322 misha 1293: case OP::OP_NUM_EQ:
1.55 paf 1294: {
1.297 paf 1295: volatile double b_double=stack.pop().value().as_double();
1296: volatile double a_double=stack.pop().value().as_double();
1.327 misha 1297: Value& value=VBool::get(a_double==b_double);
1.297 paf 1298: stack.push(value);
1.55 paf 1299: break;
1300: }
1.322 misha 1301: case OP::OP_NUM_NE:
1.55 paf 1302: {
1.297 paf 1303: volatile double b_double=stack.pop().value().as_double();
1304: volatile double a_double=stack.pop().value().as_double();
1.327 misha 1305: Value& value=VBool::get(a_double!=b_double);
1.297 paf 1306: stack.push(value);
1.54 paf 1307: break;
1308: }
1.322 misha 1309: case OP::OP_STR_LT:
1.58 paf 1310: {
1.297 paf 1311: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1.327 misha 1312: Value& value=VBool::get(a.as_string() < b.as_string());
1.297 paf 1313: stack.push(value);
1.58 paf 1314: break;
1315: }
1.322 misha 1316: case OP::OP_STR_GT:
1.58 paf 1317: {
1.297 paf 1318: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1.327 misha 1319: Value& value=VBool::get(a.as_string() > b.as_string());
1.297 paf 1320: stack.push(value);
1.58 paf 1321: break;
1322: }
1.322 misha 1323: case OP::OP_STR_LE:
1.58 paf 1324: {
1.297 paf 1325: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1.327 misha 1326: Value& value=VBool::get(a.as_string() <= b.as_string());
1.297 paf 1327: stack.push(value);
1.58 paf 1328: break;
1329: }
1.322 misha 1330: case OP::OP_STR_GE:
1.54 paf 1331: {
1.297 paf 1332: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1.327 misha 1333: Value& value=VBool::get(a.as_string() >= b.as_string());
1.297 paf 1334: stack.push(value);
1.58 paf 1335: break;
1336: }
1.322 misha 1337: case OP::OP_STR_EQ:
1.58 paf 1338: {
1.297 paf 1339: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1.327 misha 1340: Value& value=VBool::get(a.as_string() == b.as_string());
1.297 paf 1341: stack.push(value);
1.58 paf 1342: break;
1343: }
1.322 misha 1344: case OP::OP_STR_NE:
1.58 paf 1345: {
1.297 paf 1346: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1.327 misha 1347: Value& value=VBool::get(a.as_string() != b.as_string());
1.297 paf 1348: stack.push(value);
1.103 paf 1349: break;
1350: }
1.322 misha 1351: case OP::OP_IS:
1.103 paf 1352: {
1.297 paf 1353: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1.327 misha 1354: Value& value=VBool::get(a.is(b.as_string().cstr()));
1.297 paf 1355: stack.push(value);
1.28 paf 1356: break;
1357: }
1358:
1.11 paf 1359: default:
1.223 paf 1360: throw Exception(0,
1.67 paf 1361: 0,
1.297 paf 1362: "invalid opcode %d", opcode);
1.11 paf 1363: }
1364: }
1.306 paf 1365: } catch(const Exception&) {
1.297 paf 1366: // record it to stack trace
1.301 paf 1367: if(debug_name)
1368: exception_trace.push(Trace(debug_name, debug_origin));
1.297 paf 1369: rethrow;
1370: }
1.1 paf 1371: }
1.17 paf 1372:
1.330 misha 1373: #define SAVE_CONTEXT \
1374: VMethodFrame *saved_method_frame=method_frame; \
1375: Value* saved_rcontext=rcontext; \
1376: WContext *saved_wcontext=wcontext;
1377:
1378: #define RESTORE_CONTEXT \
1379: wcontext=saved_wcontext; \
1380: rcontext=saved_rcontext; \
1381: method_frame=saved_method_frame;
1382:
1.332 misha 1383: void Request::op_call(VMethodFrame& frame){
1384: const Junction &junction=frame.junction;
1385: VStateless_class& called_class=*junction.self.get_class();
1.319 misha 1386: Value* new_self;
1.332 misha 1387:
1.319 misha 1388: if(wcontext->get_constructing()) {
1389: wcontext->set_constructing(false);
1.332 misha 1390: if(junction.method->call_type!=Method::CT_STATIC) {
1.319 misha 1391: // this is a constructor call
1392: HashStringValue& new_object_fields=*new HashStringValue();
1.332 misha 1393: if(new_self=called_class.create_new_value(fpool, new_object_fields)) {
1.319 misha 1394: // some stateless_class creatable derivates
1395: } else
1396: throw Exception(PARSER_RUNTIME,
1397: 0, //&frame.name(),
1398: "is not a constructor, system class '%s' can be constructed only implicitly",
1399: called_class.name().cstr());
1400:
1401: frame.write(*new_self,
1402: String::L_CLEAN // not used, always an object, not string
1403: );
1404: } else
1405: throw Exception(PARSER_RUNTIME,
1406: 0, //&frame.name(),
1407: "method is static and can not be used as constructor");
1.332 misha 1408: } else
1409: new_self=&junction.self;
1.319 misha 1410:
1411: frame.set_self(*new_self);
1412:
1413: // see OP_PREPARE_TO_EXPRESSION
1414: frame.set_in_expression(wcontext->get_in_expression());
1415:
1.332 misha 1416: SAVE_CONTEXT
1417:
1.319 misha 1418: rcontext=wcontext=&frame;
1.332 misha 1419: method_frame=&frame;
1.319 misha 1420:
1.332 misha 1421: const Method& method=*junction.method;
1422: Method::Call_type call_type=&called_class==new_self ? Method::CT_STATIC : Method::CT_DYNAMIC;
1.319 misha 1423:
1.332 misha 1424: if(method.call_type==Method::CT_ANY || method.call_type==call_type) { // allowed call type?
1425: if(method.native_code) { // native code?
1426: method.check_actual_numbered_params(junction.self, frame.numbered_params());
1427: method.native_code(*this, *frame.numbered_params()); // execute it
1428: } else // parser code, execute it
1429: recoursion_checked_execute(*method.parser_code);
1430: } else
1431: throw Exception(PARSER_RUNTIME,
1432: 0,
1433: "is not allowed to be called %s",
1434: call_type==Method::CT_STATIC?"statically":"dynamically");
1.334 misha 1435:
1.330 misha 1436: RESTORE_CONTEXT
1.332 misha 1437: //return &frame;
1438: }
1439:
1440: void Request::op_call_write(VMethodFrame& frame){
1441: const Junction &junction=frame.junction;
1442:
1443: frame.set_self(junction.self);
1444:
1445: VMethodFrame *saved_method_frame=method_frame;
1446: Value* saved_rcontext=rcontext;
1.319 misha 1447:
1.332 misha 1448: rcontext=&frame;
1449: method_frame=&frame;
1450:
1451: const Method& method=*junction.method;
1452: Method::Call_type call_type=junction.self.get_class()==&junction.self ? Method::CT_STATIC : Method::CT_DYNAMIC;
1453:
1.333 misha 1454: if(method.call_type==Method::CT_ANY || method.call_type==call_type) { // allowed call type?
1455: if(method.native_code) { // native code?
1456: method.check_actual_numbered_params(junction.self, frame.numbered_params());
1457: method.native_code(*this, *frame.numbered_params()); // execute it
1458: } else // parser code, execute it
1459: recoursion_checked_execute(*method.parser_code);
1.332 misha 1460: } else
1461: throw Exception(PARSER_RUNTIME,
1462: 0,
1463: "is not allowed to be called %s",
1464: call_type==Method::CT_STATIC?"statically":"dynamically");
1465:
1466: rcontext=saved_rcontext;
1467: method_frame=saved_method_frame;
1.319 misha 1468: }
1469:
1.292 paf 1470: /**
1471: @bug ^superbase:method would dynamically call ^base:method if there is any
1472: */
1.330 misha 1473: Value& Request::get_element(Value& ncontext, const String& name) {
1474: if(!wcontext->get_constructing() // not constructing
1475: && wcontext->get_somebody_entered_some_class() ) // ^class:method
1.297 paf 1476: if(VStateless_class *called_class=ncontext.get_class())
1.249 paf 1477: if(VStateless_class *read_class=rcontext->get_class())
1478: if(read_class->derived_from(*called_class)) // current derived from called
1.297 paf 1479: if(Value* base=get_self().base()) { // doing DYNAMIC call
1.330 misha 1480: Temp_derived temp_derived(*base, 0); // temporarily prevent go-back-down virtual calls
1481: Value *value=base->get_element(name, *base, true); // virtual-up lookup starting from parent
1482: return *(value ? &process_to_value(*value) : VVoid::get());
1.249 paf 1483: }
1.330 misha 1484:
1485: Value* value=ncontext.get_element(name, ncontext, true);
1.291 paf 1486:
1487: if(value && wcontext->get_constructing())
1.330 misha 1488: if(Junction* junction=value->get_junction())
1.297 paf 1489: if(junction->self.get_class()!=&ncontext)
1.316 misha 1490: throw Exception(PARSER_RUNTIME,
1.291 paf 1491: &name,
1.297 paf 1492: "constructor must be declared in class '%s'",
1493: ncontext.get_class()->name_cstr());
1.63 paf 1494:
1.330 misha 1495: return *(value ? &process_to_value(*value) : VVoid::get());
1.309 paf 1496: }
1497:
1.329 misha 1498: void Request::put_element(Value& ncontext, const String& name, Value* value) {
1.309 paf 1499: // put_element can return property-setting-junction
1.329 misha 1500: if(const VJunction* vjunction=ncontext.put_element(ncontext, name, value, false))
1.312 paf 1501: if(vjunction!=PUT_ELEMENT_REPLACED_ELEMENT) {
1.309 paf 1502: // process it
1.329 misha 1503: VMethodFrame frame(vjunction->junction(), method_frame/*caller*/);
1504: int param_count=frame.method_params_count();
1505:
1.309 paf 1506: if(param_count!=1)
1.316 misha 1507: throw Exception(PARSER_RUNTIME,
1.309 paf 1508: 0,
1509: "setter method must have ONE parameter (has %d parameters)", param_count);
1510:
1.329 misha 1511: frame.store_params(&value, 1);
1.309 paf 1512: frame.set_self(frame.junction.self);
1513:
1.330 misha 1514: SAVE_CONTEXT
1.309 paf 1515:
1516: rcontext=wcontext=&frame;
1517: method_frame=&frame;
1518:
1.311 paf 1519: // prevent non-string writes for better error reporting [setters are not expected to return anything]
1.309 paf 1520: wcontext->write(*method_frame);
1521:
1522: recoursion_checked_execute(*frame.junction.method->parser_code); // parser code, execute it
1523: // we don't need it StringOrValue result=wcontext->result();
1524:
1.330 misha 1525: RESTORE_CONTEXT
1.309 paf 1526: }
1.34 paf 1527: }
1.70 paf 1528:
1.162 parser 1529: /** @param intercept_string
1.116 paf 1530: - true:
1531: they want result=string value,
1532: possible object result goes to wcontext
1533: - false:
1534: they want any result[string|object]
1535: nothing goes to wcontext.
1.125 paf 1536: used in @c (expression) params evaluation
1.224 paf 1537:
1.334 misha 1538: using the fact it's either string_ or value_ result requested to speed up checkes
1.116 paf 1539: */
1.229 paf 1540: StringOrValue Request::process(Value& input_value, bool intercept_string) {
1.297 paf 1541: Junction* junction=input_value.get_junction();
1.307 paf 1542: if(junction) {
1543: if(junction->is_getter) { // is it a getter-junction?
1.329 misha 1544: VMethodFrame frame(*junction, method_frame/*caller*/);
1545: int param_count=frame.method_params_count();
1546:
1.331 misha 1547: if(param_count){
1548: if(junction->auto_name){ // default getter
1549: if(param_count==1){
1550: Value *param=new VString(*junction->auto_name);
1551: frame.store_params(¶m, 1);
1552: } else
1553: throw Exception(PARSER_RUNTIME,
1554: 0,
1555: "default getter method can't have more then 1 parameter (has %d parameters)", param_count);
1556: } else
1.320 misha 1557: throw Exception(PARSER_RUNTIME,
1558: 0,
1.331 misha 1559: "getter method must have no parameters (has %d parameters)", param_count);
1560: } // no need for else frame.empty_params()
1.320 misha 1561:
1.307 paf 1562: frame.set_self(frame.junction.self);
1563:
1.330 misha 1564: SAVE_CONTEXT
1.307 paf 1565:
1566: rcontext=wcontext=&frame;
1567: method_frame=&frame;
1568:
1569: recoursion_checked_execute(*frame.junction.method->parser_code); // parser code, execute it
1570: StringOrValue result=wcontext->result();
1571:
1.330 misha 1572: RESTORE_CONTEXT
1.307 paf 1573:
1574: return result;
1575: }
1576:
1577: if(junction->code) { // is it a code-junction?
1578: // process it
1579: StringOrValue result;
1.336 misha 1580:
1581: DEBUG_PRINT_STR("ja->\n")
1.238 paf 1582:
1.307 paf 1583: if(!junction->method_frame)
1.316 misha 1584: throw Exception(PARSER_RUNTIME,
1.240 paf 1585: 0,
1586: "junction used outside of context");
1587:
1.330 misha 1588: SAVE_CONTEXT
1.307 paf 1589:
1590: method_frame=junction->method_frame;
1591: rcontext=junction->rcontext;
1592:
1593: // for expression method params
1594: // wcontext is set 0
1595: // using the fact in decision "which wwrapper to use"
1596: bool using_code_frame=intercept_string && junction->wcontext;
1597: if(using_code_frame) {
1.318 misha 1598: // almost plain wwrapper about junction wcontext
1599:
1600: VCodeFrame local(*junction->wcontext);
1.307 paf 1601: wcontext=&local;
1602:
1603: // execute it
1604: recoursion_checked_execute(*junction->code);
1605:
1606: // CodeFrame soul:
1.318 misha 1607: result=wcontext->result();
1.307 paf 1608: } else {
1609: // plain wwrapper
1610: WWrapper local(0/*empty*/, wcontext);
1611: wcontext=&local;
1612:
1613: // execute it
1614: recoursion_checked_execute(*junction->code);
1615:
1616: result=wcontext->result();
1617: }
1618:
1.330 misha 1619: RESTORE_CONTEXT
1.207 paf 1620:
1.336 misha 1621: DEBUG_PRINT_STR("<-ja returned")
1622:
1.307 paf 1623: return result;
1624: }
1625:
1626: // it is then method-junction, do not explode it
1627: // just return it as we do for usual objects
1628: }
1629:
1630: return input_value;
1.85 paf 1631: }
1632:
1.332 misha 1633: void Request::process_write(Value& input_value) {
1634: Junction* junction=input_value.get_junction();
1635: if(junction) {
1636: if(junction->is_getter) { // is it a getter-junction?
1637: VMethodFrame frame(*junction, method_frame/*caller*/);
1638: int param_count=frame.method_params_count();
1639:
1640: if(param_count){
1641: if(junction->auto_name){ // default getter
1642: if(param_count==1){
1643: Value *param=new VString(*junction->auto_name);
1644: frame.store_params(¶m, 1);
1645: } else
1646: throw Exception(PARSER_RUNTIME,
1647: 0,
1648: "default getter method can't have more then 1 parameter (has %d parameters)", param_count);
1649: } else
1650: throw Exception(PARSER_RUNTIME,
1651: 0,
1652: "getter method must have no parameters (has %d parameters)", param_count);
1653: } // no need for else frame.empty_params()
1654:
1655: frame.set_self(frame.junction.self);
1656:
1657: SAVE_CONTEXT
1658:
1659: rcontext=wcontext=&frame;
1660: method_frame=&frame;
1661:
1662: recoursion_checked_execute(*frame.junction.method->parser_code); // parser code, execute it
1663:
1664: RESTORE_CONTEXT
1665:
1666: write_pass_lang(frame.result());
1667: return;
1668: }
1669:
1670: if(junction->code) { // is it a code-junction?
1671: // process it
1.336 misha 1672:
1673: DEBUG_PRINT_STR("ja->\n")
1674:
1.332 misha 1675: if(!junction->method_frame)
1676: throw Exception(PARSER_RUNTIME,
1677: 0,
1678: "junction used outside of context");
1679:
1680: SAVE_CONTEXT
1681:
1682: method_frame=junction->method_frame;
1683: rcontext=junction->rcontext;
1684:
1685: // for expression method params
1686: // wcontext is set 0
1687: // using the fact in decision "which wwrapper to use"
1688: #ifdef OPTIMIZE_CALL
1689: if(wcontext==junction->wcontext){
1690: // no wrappers for wcontext
1691: recoursion_checked_execute(*junction->code);
1692: RESTORE_CONTEXT
1693:
1694: } else
1695: #endif
1696: if(junction->wcontext) {
1697: // almost plain wwrapper about junction wcontext
1698: VCodeFrame local(*junction->wcontext);
1699: wcontext=&local;
1700:
1701: // execute it
1702: recoursion_checked_execute(*junction->code);
1703: RESTORE_CONTEXT
1704: write_pass_lang(local.result());
1705: } else {
1706: // plain wwrapper
1707: WWrapper local(0/*empty*/, wcontext);
1708: wcontext=&local;
1709:
1710: // execute it
1711: recoursion_checked_execute(*junction->code);
1712: RESTORE_CONTEXT
1713: write_pass_lang(local.result());
1714: }
1.336 misha 1715:
1716: DEBUG_PRINT_STR("<-ja returned")
1717:
1.332 misha 1718: return;
1719: }
1720:
1721: // it is then method-junction, do not explode it
1722: // just return it as we do for usual objects
1723: }
1724:
1725: write_pass_lang(input_value);
1726: }
1727:
1.298 paf 1728: StringOrValue Request::execute_method(VMethodFrame& amethod_frame, const Method& method) {
1.330 misha 1729: SAVE_CONTEXT
1.99 paf 1730:
1731: // initialize contexts
1.286 paf 1732: rcontext=wcontext=method_frame=&amethod_frame;
1.99 paf 1733:
1734: // execute!
1735: execute(*method.parser_code);
1736:
1737: // result
1.298 paf 1738: StringOrValue result=wcontext->result();
1.208 paf 1739:
1.330 misha 1740: RESTORE_CONTEXT
1.242 paf 1741:
1742: // return
1743: return result;
1.208 paf 1744: }
1745:
1.297 paf 1746: const String* Request::execute_method(Value& aself,
1.329 misha 1747: const Method& method, Value* optional_param,
1748: bool do_return_string) {
1.317 misha 1749:
1.330 misha 1750: SAVE_CONTEXT
1.208 paf 1751:
1.307 paf 1752: Junction local_junction(aself, &method);
1.321 misha 1753: VMethodFrame local_frame(local_junction, method_frame/*caller*/);
1.329 misha 1754: if(optional_param && local_frame.method_params_count()>0) {
1755: local_frame.store_params(&optional_param, 1);
1.330 misha 1756: } else {
1757: local_frame.empty_params();
1.242 paf 1758: }
1.285 paf 1759: local_frame.set_self(aself);
1.257 paf 1760: rcontext=wcontext=method_frame=&local_frame;
1.246 paf 1761:
1762: // prevent non-string writes for better error reporting
1.297 paf 1763: if(do_return_string)
1.246 paf 1764: wcontext->write(local_frame);
1.208 paf 1765:
1766: // execute!
1767: execute(*method.parser_code);
1768:
1769: // result
1.297 paf 1770: const String* result=0;
1771: if(do_return_string)
1772: result=&wcontext->result().as_string();
1.99 paf 1773:
1.330 misha 1774: RESTORE_CONTEXT
1.297 paf 1775:
1776: return result;
1.242 paf 1777: }
1778:
1.297 paf 1779: Request::Execute_nonvirtual_method_result
1780: Request::execute_nonvirtual_method(VStateless_class& aclass,
1.330 misha 1781: const String& method_name,
1782: VString* optional_param,
1783: bool do_return_string) {
1.297 paf 1784: Execute_nonvirtual_method_result result;
1785: result.method=aclass.get_method(method_name);
1786: if(result.method)
1.317 misha 1787: result.string=execute_method(aclass, *result.method, optional_param, do_return_string);
1.297 paf 1788: return result;
1.99 paf 1789: }
1790:
1.297 paf 1791: const String* Request::execute_virtual_method(Value& aself,
1.330 misha 1792: const String& method_name) {
1.297 paf 1793: if(Value* value=aself.get_element(method_name, aself, false))
1794: if(Junction* junction=value->get_junction())
1795: if(const Method *method=junction->method)
1796: return execute_method(aself, *method, 0/*no params*/, true);
1.188 parser 1797:
1.176 parser 1798: return 0;
1799: }