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