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