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