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