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