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