|
|
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.351 ! misha 8: static const char * const IDENT_EXECUTE_C="$Date: 2009-06-16 08:40:32 $";
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.219 paf 454: // see OP_PREPARE_TO_EXPRESSION
455: wcontext->set_in_expression(false);
1.351 ! misha 456:
! 457: Value& value=stack.pop().value();
! 458: wcontext->write(value.as_expr_result());
1.108 paf 459: break;
460: }
1.322 misha 461: case OP::OP_STRING__WRITE:
1.86 paf 462: {
1.297 paf 463: i.next(); // ignore origin
464: Value* value=i.next().value;
1.336 misha 465:
466: const String& string_value=*value->get_string();
467:
468: DEBUG_PRINT_STRING(string_value)
469:
470: write_no_lang(string_value);
1.13 paf 471: break;
1.14 paf 472: }
1.328 misha 473:
474: #ifdef OPTIMIZE_BYTECODE_GET_ELEMENT
475: case OP::OP_VALUE__GET_ELEMENT_OR_OPERATOR:
476: {
477: debug_origin=i.next().origin;
478: const String& name=*i.next().value->get_string(); debug_name=&name;
479:
1.336 misha 480: DEBUG_PRINT_STRING(name)
481:
1.330 misha 482: if(Method* method=main_class.get_method(name)){ // looking operator of that name FIRST
483: if(!method->junction_template) method->junction_template=new VJunction(main_class, method);
484: stack.push(*method->junction_template);
485: break;
486: }
487: Value& value=get_element(*rcontext, name);
1.328 misha 488: stack.push(value);
489: break;
490: }
491: #else
1.322 misha 492: case OP::OP_GET_ELEMENT_OR_OPERATOR:
1.215 paf 493: {
1.297 paf 494: const String& name=stack.pop().string(); debug_name=&name;
495: Value& ncontext=stack.pop().value();
1.330 misha 496: if(Method* method=main_class.get_method(name)){ // looking operator of that name FIRST
497: if(!method->junction_template) method->junction_template=new VJunction(main_class, method);
498: stack.push(*method->junction_template);
499: break;
500: }
501: Value& value=get_element(ncontext, name);
1.297 paf 502: stack.push(value);
1.215 paf 503: break;
504: }
1.328 misha 505: #endif
506:
1.335 misha 507: #ifdef OPTIMIZE_BYTECODE_GET_OBJECT_ELEMENT
508: case OP::OP_GET_OBJECT_ELEMENT:
509: case OP::OP_GET_OBJECT_ELEMENT__WRITE:
1.334 misha 510: {
511: debug_origin=i.next().origin;
512: const String& context_name=*i.next().value->get_string(); debug_name=&context_name;
1.336 misha 513:
514: DEBUG_PRINT_STRING(context_name)
515:
1.334 misha 516: Value& object=get_element(*rcontext, context_name);
517:
518: debug_origin=i.next().origin;
519: const String& field_name=*i.next().value->get_string(); debug_name=&field_name;
1.336 misha 520:
521: DEBUG_PRINT_STRING(field_name)
522:
1.334 misha 523: Value& value=get_element(object, field_name);
524:
1.335 misha 525: if(opcode==OP::OP_GET_OBJECT_ELEMENT){
526: stack.push(value);
527: } else {
528: write_assign_lang(value);
529: }
530: break;
531: }
532: #endif
533:
534: #ifdef OPTIMIZE_BYTECODE_GET_OBJECT_VAR_ELEMENT
535: case OP::OP_GET_OBJECT_VAR_ELEMENT:
536: case OP::OP_GET_OBJECT_VAR_ELEMENT__WRITE:
537: {
538: debug_origin=i.next().origin;
539: const String& context_name=*i.next().value->get_string(); debug_name=&context_name;
1.336 misha 540:
541: DEBUG_PRINT_STRING(context_name)
542:
1.335 misha 543: Value& object=get_element(*rcontext, context_name);
544:
545: debug_origin=i.next().origin;
546: const String& var_name=*i.next().value->get_string(); debug_name=&var_name;
1.336 misha 547:
548: DEBUG_PRINT_STRING(var_name)
549:
1.335 misha 550: const String* field=get_element(*rcontext, var_name).get_string();
551:
552: Value& value=get_element(object, *field);
553:
554: if(opcode==OP::OP_GET_OBJECT_VAR_ELEMENT){
1.334 misha 555: stack.push(value);
556: } else {
557: write_assign_lang(value);
558: }
559: break;
560: }
561: #endif
562:
1.322 misha 563: case OP::OP_GET_ELEMENT:
1.11 paf 564: {
1.297 paf 565: const String& name=stack.pop().string(); debug_name=&name;
566: Value& ncontext=stack.pop().value();
1.330 misha 567: Value& value=get_element(ncontext, name);
1.297 paf 568: stack.push(value);
1.17 paf 569: break;
570: }
571:
1.346 misha 572: case OP::OP_GET_ELEMENT__WRITE:
573: {
574: const String& name=stack.pop().string(); debug_name=&name;
575: Value& ncontext=stack.pop().value();
576: Value& value=get_element(ncontext, name);
577: write_assign_lang(value);
578: break;
579: }
580:
1.328 misha 581: #ifdef OPTIMIZE_BYTECODE_GET_ELEMENT
582: case OP::OP_VALUE__GET_ELEMENT:
583: {
584: debug_origin=i.next().origin;
585: const String& name=*i.next().value->get_string(); debug_name=&name;
586:
1.336 misha 587: DEBUG_PRINT_STRING(name)
1.328 misha 588:
1.330 misha 589: Value& value=get_element(*rcontext, name);
1.328 misha 590: stack.push(value);
591: break;
592: }
593:
594: case OP::OP_VALUE__GET_ELEMENT__WRITE:
595: {
596: debug_origin=i.next().origin;
597: const String& name=*i.next().value->get_string(); debug_name=&name;
598:
1.336 misha 599: DEBUG_PRINT_STRING(name)
1.328 misha 600:
1.330 misha 601: Value& value=get_element(*rcontext, name);
1.328 misha 602: write_assign_lang(value);
603: break;
604: }
1.347 misha 605:
606: case OP::OP_WITH_ROOT__VALUE__GET_ELEMENT:
607: {
608: debug_origin=i.next().origin;
609: const String& name=*i.next().value->get_string(); debug_name=&name;
610:
611: DEBUG_PRINT_STRING(name)
612:
613: Value& value=get_element(*method_frame, name);
614: stack.push(value);
615: break;
616: }
1.328 misha 617: #endif
1.32 paf 618:
1.345 misha 619: #ifdef OPTIMIZE_BYTECODE_GET_SELF_ELEMENT
620: case OP::OP_WITH_SELF__VALUE__GET_ELEMENT:
621: case OP::OP_WITH_SELF__VALUE__GET_ELEMENT__WRITE:
622: {
623: debug_origin=i.next().origin;
624: const String& name=*i.next().value->get_string(); debug_name=&name;
625:
626: DEBUG_PRINT_STRING(name)
627:
628: Value& value=get_element(get_self(), name);
629:
630: if(opcode==OP::OP_WITH_SELF__VALUE__GET_ELEMENT){
631: stack.push(value);
632: } else {
633: write_assign_lang(value);
634: }
635: break;
636: }
637: #endif
638:
1.322 misha 639: case OP::OP_OBJECT_POOL:
1.17 paf 640: {
1.297 paf 641: ArrayOperation& local_ops=*i.next().ops;
1.226 paf 642:
1.257 paf 643: WContext *saved_wcontext=wcontext;
1.297 paf 644: String::Language saved_lang=flang;
645: flang=String::L_PASS_APPENDED;
1.348 misha 646: #ifdef OPTIMIZE_SINGLE_STRING_WRITE
1.351 ! misha 647: WObjectPoolWrapper local(wcontext);
1.348 misha 648: #else
1.351 ! misha 649: WWrapper local(wcontext);
1.348 misha 650: #endif
1.226 paf 651: wcontext=&local;
652:
1.297 paf 653: execute(local_ops);
1.226 paf 654:
1.297 paf 655: stack.push(wcontext->result().as_value());
1.257 paf 656: flang=saved_lang;
657: wcontext=saved_wcontext;
1.13 paf 658: break;
1.15 paf 659: }
1.13 paf 660:
1.322 misha 661: case OP::OP_STRING_POOL:
1.49 paf 662: {
1.297 paf 663: ArrayOperation& local_ops=*i.next().ops;
1.226 paf 664:
1.257 paf 665: WContext *saved_wcontext=wcontext;
1.351 ! misha 666: WWrapper local(wcontext);
1.226 paf 667: wcontext=&local;
668:
1.297 paf 669: execute(local_ops);
1.226 paf 670:
1.297 paf 671: Value* value;
1.49 paf 672: // from "$a $b" part of expression taking only string value,
673: // ignoring any other content of wcontext
1.297 paf 674: if(const String* string=wcontext->get_string())
675: value=new VString(*string);
1.80 paf 676: else
1.324 misha 677: value=VVoid::get();
1.297 paf 678: stack.push(*value);
679:
1.257 paf 680: wcontext=saved_wcontext;
1.49 paf 681: break;
682: }
683:
1.51 paf 684: // CALL
1.322 misha 685: case OP::OP_CURLY_CODE__STORE_PARAM:
686: case OP::OP_EXPR_CODE__STORE_PARAM:
1.28 paf 687: {
1.237 paf 688: // code
1.297 paf 689: ArrayOperation& local_ops=*i.next().ops;
1.336 misha 690:
691: DEBUG_PRINT_OPS((&local_ops))
692:
1.237 paf 693: // when they evaluate expression parameter,
694: // the object expression result
695: // does not need to be written into calling frame
696: // it must go into any expressions using that parameter
697: // hence, we zero junction.wcontext here, and later
698: // in .process we would test that field
699: // in decision "which wwrapper to use"
1.326 misha 700: VJunction& value=*new VJunction(
1.297 paf 701: get_self(), 0,
1.257 paf 702: method_frame,
1.237 paf 703: rcontext,
1.322 misha 704: opcode==OP::OP_EXPR_CODE__STORE_PARAM?0:wcontext,
1.312 paf 705: &local_ops);
1.343 misha 706: #ifndef USE_DESTRUCTORS
1.326 misha 707: if (opcode!=OP::OP_EXPR_CODE__STORE_PARAM)
1.343 misha 708: wcontext->attach_junction(&value);
1.326 misha 709: #endif
1.237 paf 710: // store param
1.329 misha 711: stack.push(value);
1.29 paf 712: break;
713: }
714:
1.348 misha 715: #ifndef OPTIMIZE_BYTECODE_CONSTRUCT_OBJECT
1.322 misha 716: case OP::OP_PREPARE_TO_CONSTRUCT_OBJECT:
1.186 parser 717: {
1.350 misha 718: SET_CONSTRUCTING(wcontext,true)
1.186 parser 719: break;
720: }
1.348 misha 721: #endif
1.219 paf 722:
1.322 misha 723: case OP::OP_PREPARE_TO_EXPRESSION:
1.219 paf 724: {
725: wcontext->set_in_expression(true);
726: break;
727: }
728:
1.350 misha 729: #define METHOD_FRAME_ACTION(action) \
730: if(local_ops){ \
731: size_t first = stack.top_index(); \
732: execute(*local_ops); \
733: frame.store_params((Value**)stack.ptr(first), stack.top_index()-first); \
734: action; \
735: stack.set_top_index(first); \
736: } else { \
737: frame.empty_params(); \
738: action; \
739: }
740:
1.322 misha 741: case OP::OP_CALL:
1.29 paf 742: {
1.297 paf 743: ArrayOperation* local_ops=i.next().ops;
1.237 paf 744:
1.336 misha 745: DEBUG_PRINT_OPS(local_ops)
746: DEBUG_PRINT_STR("->\n")
747:
1.297 paf 748: Value& value=stack.pop().value();
1.237 paf 749:
1.297 paf 750: Junction* junction=value.get_junction();
751: if(!junction) {
752: if(value.is("void"))
1.316 misha 753: throw Exception(PARSER_RUNTIME,
1.297 paf 754: 0,
755: "undefined method");
756: else
1.316 misha 757: throw Exception(PARSER_RUNTIME,
1.297 paf 758: 0,
759: "is '%s', not a method or junction, can not call it",
760: value.type());
761: }
1.237 paf 762:
1.321 misha 763: VMethodFrame frame(*junction, method_frame);
1.350 misha 764: METHOD_FRAME_ACTION(op_call(frame, GET_CONSTRUCTING(wcontext)));
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;
1.350 misha 804: if (!GET_CONSTRUCTING(wcontext) && method.call_optimization==Method::CO_WITHOUT_FRAME){
1.332 misha 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: }
1.350 misha 820: } else if (!GET_CONSTRUCTING(wcontext) && method.call_optimization==Method::CO_WITHOUT_WCONTEXT){
1.332 misha 821: VMethodFrame frame(*junction, method_frame);
1.350 misha 822: METHOD_FRAME_ACTION(op_call_write(frame));
1.332 misha 823: } else
824: #endif // OPTIMIZE_CALL
825: {
826: VMethodFrame frame(*junction, method_frame);
1.350 misha 827: METHOD_FRAME_ACTION(op_call(frame, GET_CONSTRUCTING(wcontext)));
1.346 misha 828: write_pass_lang(frame.result());
1.332 misha 829: }
1.336 misha 830:
831: DEBUG_PRINT_STR("<-returned")
1.327 misha 832:
833: if(get_skip())
834: return;
835: if(get_interrupted()) {
836: set_interrupted(false);
837: throw Exception("parser.interrupted",
838: 0,
839: "execution stopped");
840: }
1.49 paf 841: break;
842: }
843:
1.348 misha 844: #ifdef OPTIMIZE_BYTECODE_CONSTRUCT_OBJECT
845: case OP::OP_CONSTRUCT_OBJECT:
846: case OP::OP_CONSTRUCT_OBJECT__WRITE:
847: {
848: debug_origin=i.next().origin;
849: Value& vclass_name=*i.next().value;
850: const String& class_name=*vclass_name.get_string();
851:
852: DEBUG_PRINT_STRING(class_name)
853:
854: Value* class_value=classes().get(class_name);
855: if(!class_value)
856: throw Exception(PARSER_RUNTIME,
857: &class_name,
858: "class is undefined");
859:
1.350 misha 860: SET_CONSTRUCTING(wcontext,true)
1.348 misha 861:
862: debug_origin=i.next().origin;
863: Value& vconstructor_name=*i.next().value;
864: const String& constructor_name=*vconstructor_name.get_string(); debug_name=&constructor_name;
865:
866: DEBUG_PRINT_STRING(constructor_name)
867:
868: Value& constructor_value=get_element(*class_value, constructor_name);
869:
1.350 misha 870: Junction* junction=constructor_value.get_junction();
871: #ifdef OPTIMIZE_CONSTRUCT_OBJECT
872: if(!junction || junction->self.get_class()!=class_value)
873: throw Exception(PARSER_RUNTIME,
874: &constructor_name,
875: "constructor must be declared in class '%s'",
876: class_value->get_class()->name_cstr());
877: #endif
878:
1.348 misha 879: ArrayOperation* local_ops=i.next().ops;
880: DEBUG_PRINT_OPS(local_ops)
881: DEBUG_PRINT_STR("->\n")
882:
1.350 misha 883: VMethodFrame frame(*junction, method_frame);
884: METHOD_FRAME_ACTION(op_call(frame, true /* constructing */));
885: if(opcode==OP::OP_CONSTRUCT_OBJECT)
886: stack.push(frame.result().as_value());
887: else
888: write_pass_lang(frame.result());
1.348 misha 889:
890: DEBUG_PRINT_STR("<-returned")
891: break;
892: }
893: #endif // OPTIMIZE_BYTECODE_CONSTRUCT_OBJECT
894:
1.55 paf 895: // expression ops: unary
1.322 misha 896: case OP::OP_NEG:
1.55 paf 897: {
1.297 paf 898: Value& a=stack.pop().value();
899: Value& value=*new VDouble(-a.as_double());
900: stack.push(value);
1.55 paf 901: break;
902: }
1.322 misha 903: case OP::OP_INV:
1.55 paf 904: {
1.297 paf 905: Value& a=stack.pop().value();
906: Value& value=*new VDouble(~a.as_int());
907: stack.push(value);
1.55 paf 908: break;
909: }
1.322 misha 910: case OP::OP_NOT:
1.55 paf 911: {
1.297 paf 912: Value& a=stack.pop().value();
1.327 misha 913: Value& value=VBool::get(!a.as_bool());
1.297 paf 914: stack.push(value);
1.55 paf 915: break;
916: }
1.322 misha 917: case OP::OP_DEF:
1.62 paf 918: {
1.297 paf 919: Value& a=stack.pop().value();
1.327 misha 920: Value& value=VBool::get(a.is_defined());
1.297 paf 921: stack.push(value);
1.62 paf 922: break;
923: }
1.322 misha 924: case OP::OP_IN:
1.62 paf 925: {
1.297 paf 926: Value& a=stack.pop().value();
927: const String& path=a.as_string();
1.327 misha 928: Value& value=VBool::get(request_info.uri && *request_info.uri && path.this_starts(request_info.uri));
1.297 paf 929: stack.push(value);
1.62 paf 930: break;
931: }
1.322 misha 932: case OP::OP_FEXISTS:
1.62 paf 933: {
1.297 paf 934: Value& a=stack.pop().value();
1.327 misha 935: Value& value=VBool::get(file_exist(absolute(a.as_string())));
1.297 paf 936: stack.push(value);
1.148 paf 937: break;
938: }
1.322 misha 939: case OP::OP_DEXISTS:
1.148 paf 940: {
1.297 paf 941: Value& a=stack.pop().value();
1.327 misha 942: Value& value=VBool::get(dir_exists(absolute(a.as_string())));
1.297 paf 943: stack.push(value);
1.62 paf 944: break;
945: }
1.55 paf 946:
947: // expression ops: binary
1.322 misha 948: case OP::OP_SUB:
1.53 paf 949: {
1.297 paf 950: Value& b=stack.pop().value(); Value& a=stack.pop().value();
951: Value& value=*new VDouble(a.as_double() - b.as_double());
952: stack.push(value);
1.53 paf 953: break;
954: }
1.322 misha 955: case OP::OP_ADD:
1.53 paf 956: {
1.297 paf 957: Value& b=stack.pop().value(); Value& a=stack.pop().value();
958: Value& value=*new VDouble(a.as_double() + b.as_double());
959: stack.push(value);
1.53 paf 960: break;
961: }
1.322 misha 962: case OP::OP_MUL:
1.49 paf 963: {
1.297 paf 964: Value& b=stack.pop().value(); Value& a=stack.pop().value();
965: Value& value=*new VDouble(a.as_double() * b.as_double());
966: stack.push(value);
1.53 paf 967: break;
968: }
1.322 misha 969: case OP::OP_DIV:
1.53 paf 970: {
1.297 paf 971: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1.170 parser 972:
1.297 paf 973: double a_double=a.as_double();
974: double b_double=b.as_double();
1.170 parser 975:
1.171 parser 976: if(b_double == 0) {
1.297 paf 977: //const String* problem_source=b.as_string();
1.223 paf 978: throw Exception("number.zerodivision",
1.297 paf 979: 0, //problem_source,
1.170 parser 980: "Division by zero");
1.171 parser 981: }
1.170 parser 982:
1.297 paf 983: Value& value=*new VDouble(a_double / b_double);
984: stack.push(value);
1.54 paf 985: break;
986: }
1.322 misha 987: case OP::OP_MOD:
1.55 paf 988: {
1.297 paf 989: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1.170 parser 990:
1.297 paf 991: double a_double=a.as_double();
992: double b_double=b.as_double();
1.170 parser 993:
1.173 parser 994: if(b_double == 0) {
1.297 paf 995: //const String* problem_source=b.as_string();
1.223 paf 996: throw Exception("number.zerodivision",
1.297 paf 997: 0, //problem_source,
1.170 parser 998: "Modulus by zero");
1.171 parser 999: }
1.170 parser 1000:
1.297 paf 1001: Value& value=*new VDouble(fmod(a_double, b_double));
1002: stack.push(value);
1.201 paf 1003: break;
1004: }
1.322 misha 1005: case OP::OP_INTDIV:
1.201 paf 1006: {
1.297 paf 1007: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1.201 paf 1008:
1.297 paf 1009: int a_int=a.as_int();
1010: int b_int=b.as_int();
1.201 paf 1011:
1012: if(b_int == 0) {
1.297 paf 1013: //const String* problem_source=b.as_string();
1.223 paf 1014: throw Exception("number.zerodivision",
1.297 paf 1015: 0, //problem_source,
1.201 paf 1016: "Division by zero");
1017: }
1018:
1.297 paf 1019: Value& value=*new VInt(a_int / b_int);
1020: stack.push(value);
1.55 paf 1021: break;
1022: }
1.322 misha 1023: case OP::OP_BIN_SL:
1.274 paf 1024: {
1.297 paf 1025: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1026: Value& value=*new VInt(
1027: a.as_int() <<
1028: b.as_int());
1029: stack.push(value);
1.274 paf 1030: break;
1031: }
1.322 misha 1032: case OP::OP_BIN_SR:
1.274 paf 1033: {
1.297 paf 1034: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1035: Value& value=*new VInt(
1036: a.as_int() >>
1037: b.as_int());
1038: stack.push(value);
1.274 paf 1039: break;
1040: }
1.322 misha 1041: case OP::OP_BIN_AND:
1.54 paf 1042: {
1.297 paf 1043: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1044: Value& value=*new VInt(
1045: a.as_int() &
1046: b.as_int());
1047: stack.push(value);
1.54 paf 1048: break;
1049: }
1.322 misha 1050: case OP::OP_BIN_OR:
1.54 paf 1051: {
1.297 paf 1052: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1053: Value& value=*new VInt(
1054: a.as_int() |
1055: b.as_int());
1056: stack.push(value);
1.55 paf 1057: break;
1058: }
1.322 misha 1059: case OP::OP_BIN_XOR:
1.56 paf 1060: {
1.297 paf 1061: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1062: Value& value=*new VInt(
1063: a.as_int() ^
1064: b.as_int());
1065: stack.push(value);
1.56 paf 1066: break;
1067: }
1.322 misha 1068: case OP::OP_LOG_AND:
1.55 paf 1069: {
1.297 paf 1070: ArrayOperation& local_ops=stack.pop().ops(); Value& a=stack.pop().value();
1.263 paf 1071: bool result;
1.297 paf 1072: if(a.as_bool()) {
1073: execute(local_ops);
1074: Value& b=stack.pop().value();
1075: result=b.as_bool();
1.209 paf 1076: } else
1.263 paf 1077: result=false;
1.327 misha 1078: Value& value=VBool::get(result);
1.297 paf 1079: stack.push(value);
1.55 paf 1080: break;
1081: }
1.322 misha 1082: case OP::OP_LOG_OR:
1.55 paf 1083: {
1.297 paf 1084: ArrayOperation& local_ops=stack.pop().ops(); Value& a=stack.pop().value();
1.263 paf 1085: bool result;
1.297 paf 1086: if(a.as_bool())
1.263 paf 1087: result=true;
1.209 paf 1088: else {
1.297 paf 1089: execute(local_ops);
1090: Value& b=stack.pop().value();
1091: result=b.as_bool();
1.209 paf 1092: }
1.327 misha 1093: Value& value=VBool::get(result);
1.297 paf 1094: stack.push(value);
1.56 paf 1095: break;
1096: }
1.322 misha 1097: case OP::OP_LOG_XOR:
1.56 paf 1098: {
1.297 paf 1099: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1.327 misha 1100: Value& value=VBool::get(a.as_bool() ^ b.as_bool());
1.297 paf 1101: stack.push(value);
1.55 paf 1102: break;
1103: }
1.322 misha 1104: case OP::OP_NUM_LT:
1.55 paf 1105: {
1.297 paf 1106: volatile double b_double=stack.pop().value().as_double();
1107: volatile double a_double=stack.pop().value().as_double();
1.327 misha 1108: Value& value=VBool::get(a_double<b_double);
1.297 paf 1109: stack.push(value);
1.55 paf 1110: break;
1111: }
1.322 misha 1112: case OP::OP_NUM_GT:
1.55 paf 1113: {
1.297 paf 1114: volatile double b_double=stack.pop().value().as_double();
1115: volatile double a_double=stack.pop().value().as_double();
1.327 misha 1116: Value& value=VBool::get(a_double>b_double);
1.297 paf 1117: stack.push(value);
1.55 paf 1118: break;
1119: }
1.322 misha 1120: case OP::OP_NUM_LE:
1.55 paf 1121: {
1.297 paf 1122: volatile double b_double=stack.pop().value().as_double();
1123: volatile double a_double=stack.pop().value().as_double();
1.327 misha 1124: Value& value=VBool::get(a_double<=b_double);
1.297 paf 1125: stack.push(value);
1.55 paf 1126: break;
1127: }
1.322 misha 1128: case OP::OP_NUM_GE:
1.55 paf 1129: {
1.297 paf 1130: volatile double b_double=stack.pop().value().as_double();
1131: volatile double a_double=stack.pop().value().as_double();
1.327 misha 1132: Value& value=VBool::get(a_double>=b_double);
1.297 paf 1133: stack.push(value);
1.55 paf 1134: break;
1135: }
1.322 misha 1136: case OP::OP_NUM_EQ:
1.55 paf 1137: {
1.297 paf 1138: volatile double b_double=stack.pop().value().as_double();
1139: volatile double a_double=stack.pop().value().as_double();
1.327 misha 1140: Value& value=VBool::get(a_double==b_double);
1.297 paf 1141: stack.push(value);
1.55 paf 1142: break;
1143: }
1.322 misha 1144: case OP::OP_NUM_NE:
1.55 paf 1145: {
1.297 paf 1146: volatile double b_double=stack.pop().value().as_double();
1147: volatile double a_double=stack.pop().value().as_double();
1.327 misha 1148: Value& value=VBool::get(a_double!=b_double);
1.297 paf 1149: stack.push(value);
1.54 paf 1150: break;
1151: }
1.322 misha 1152: case OP::OP_STR_LT:
1.58 paf 1153: {
1.297 paf 1154: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1.327 misha 1155: Value& value=VBool::get(a.as_string() < b.as_string());
1.297 paf 1156: stack.push(value);
1.58 paf 1157: break;
1158: }
1.322 misha 1159: case OP::OP_STR_GT:
1.58 paf 1160: {
1.297 paf 1161: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1.327 misha 1162: Value& value=VBool::get(a.as_string() > b.as_string());
1.297 paf 1163: stack.push(value);
1.58 paf 1164: break;
1165: }
1.322 misha 1166: case OP::OP_STR_LE:
1.58 paf 1167: {
1.297 paf 1168: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1.327 misha 1169: Value& value=VBool::get(a.as_string() <= b.as_string());
1.297 paf 1170: stack.push(value);
1.58 paf 1171: break;
1172: }
1.322 misha 1173: case OP::OP_STR_GE:
1.54 paf 1174: {
1.297 paf 1175: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1.327 misha 1176: Value& value=VBool::get(a.as_string() >= b.as_string());
1.297 paf 1177: stack.push(value);
1.58 paf 1178: break;
1179: }
1.322 misha 1180: case OP::OP_STR_EQ:
1.58 paf 1181: {
1.297 paf 1182: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1.327 misha 1183: Value& value=VBool::get(a.as_string() == b.as_string());
1.297 paf 1184: stack.push(value);
1.58 paf 1185: break;
1186: }
1.322 misha 1187: case OP::OP_STR_NE:
1.58 paf 1188: {
1.297 paf 1189: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1.327 misha 1190: Value& value=VBool::get(a.as_string() != b.as_string());
1.297 paf 1191: stack.push(value);
1.103 paf 1192: break;
1193: }
1.322 misha 1194: case OP::OP_IS:
1.103 paf 1195: {
1.297 paf 1196: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1.327 misha 1197: Value& value=VBool::get(a.is(b.as_string().cstr()));
1.297 paf 1198: stack.push(value);
1.28 paf 1199: break;
1200: }
1201:
1.11 paf 1202: default:
1.223 paf 1203: throw Exception(0,
1.67 paf 1204: 0,
1.297 paf 1205: "invalid opcode %d", opcode);
1.11 paf 1206: }
1207: }
1.306 paf 1208: } catch(const Exception&) {
1.297 paf 1209: // record it to stack trace
1.301 paf 1210: if(debug_name)
1211: exception_trace.push(Trace(debug_name, debug_origin));
1.297 paf 1212: rethrow;
1213: }
1.1 paf 1214: }
1.17 paf 1215:
1.330 misha 1216: #define SAVE_CONTEXT \
1217: VMethodFrame *saved_method_frame=method_frame; \
1218: Value* saved_rcontext=rcontext; \
1219: WContext *saved_wcontext=wcontext;
1220:
1221: #define RESTORE_CONTEXT \
1222: wcontext=saved_wcontext; \
1223: rcontext=saved_rcontext; \
1224: method_frame=saved_method_frame;
1225:
1.350 misha 1226: void Request::op_call(VMethodFrame& frame, bool constructing){
1.332 misha 1227: const Junction &junction=frame.junction;
1228: VStateless_class& called_class=*junction.self.get_class();
1.319 misha 1229: Value* new_self;
1.332 misha 1230:
1.350 misha 1231: if(constructing) {
1232: SET_CONSTRUCTING(wcontext,false)
1.332 misha 1233: if(junction.method->call_type!=Method::CT_STATIC) {
1.319 misha 1234: // this is a constructor call
1.349 misha 1235: if(new_self=called_class.create_new_value(fpool, 0 /*creating fields only in VClass*/)) {
1.319 misha 1236: // some stateless_class creatable derivates
1237: } else
1238: throw Exception(PARSER_RUNTIME,
1239: 0, //&frame.name(),
1240: "is not a constructor, system class '%s' can be constructed only implicitly",
1241: called_class.name().cstr());
1242:
1243: frame.write(*new_self,
1244: String::L_CLEAN // not used, always an object, not string
1245: );
1246: } else
1247: throw Exception(PARSER_RUNTIME,
1248: 0, //&frame.name(),
1249: "method is static and can not be used as constructor");
1.332 misha 1250: } else
1251: new_self=&junction.self;
1.319 misha 1252:
1253: frame.set_self(*new_self);
1254:
1255: // see OP_PREPARE_TO_EXPRESSION
1256: frame.set_in_expression(wcontext->get_in_expression());
1257:
1.332 misha 1258: SAVE_CONTEXT
1259:
1.319 misha 1260: rcontext=wcontext=&frame;
1.332 misha 1261: method_frame=&frame;
1.319 misha 1262:
1.332 misha 1263: const Method& method=*junction.method;
1264: Method::Call_type call_type=&called_class==new_self ? Method::CT_STATIC : Method::CT_DYNAMIC;
1.319 misha 1265:
1.332 misha 1266: if(method.call_type==Method::CT_ANY || method.call_type==call_type) { // allowed call type?
1267: if(method.native_code) { // native code?
1268: method.check_actual_numbered_params(junction.self, frame.numbered_params());
1269: method.native_code(*this, *frame.numbered_params()); // execute it
1270: } else // parser code, execute it
1271: recoursion_checked_execute(*method.parser_code);
1272: } else
1273: throw Exception(PARSER_RUNTIME,
1274: 0,
1275: "is not allowed to be called %s",
1276: call_type==Method::CT_STATIC?"statically":"dynamically");
1.334 misha 1277:
1.330 misha 1278: RESTORE_CONTEXT
1.332 misha 1279: //return &frame;
1280: }
1281:
1282: void Request::op_call_write(VMethodFrame& frame){
1283: const Junction &junction=frame.junction;
1284:
1285: frame.set_self(junction.self);
1286:
1287: VMethodFrame *saved_method_frame=method_frame;
1288: Value* saved_rcontext=rcontext;
1.319 misha 1289:
1.332 misha 1290: rcontext=&frame;
1291: method_frame=&frame;
1292:
1293: const Method& method=*junction.method;
1294: Method::Call_type call_type=junction.self.get_class()==&junction.self ? Method::CT_STATIC : Method::CT_DYNAMIC;
1295:
1.333 misha 1296: if(method.call_type==Method::CT_ANY || method.call_type==call_type) { // allowed call type?
1297: if(method.native_code) { // native code?
1298: method.check_actual_numbered_params(junction.self, frame.numbered_params());
1299: method.native_code(*this, *frame.numbered_params()); // execute it
1300: } else // parser code, execute it
1301: recoursion_checked_execute(*method.parser_code);
1.332 misha 1302: } else
1303: throw Exception(PARSER_RUNTIME,
1304: 0,
1305: "is not allowed to be called %s",
1306: call_type==Method::CT_STATIC?"statically":"dynamically");
1307:
1308: rcontext=saved_rcontext;
1309: method_frame=saved_method_frame;
1.319 misha 1310: }
1311:
1.292 paf 1312: /**
1313: @bug ^superbase:method would dynamically call ^base:method if there is any
1314: */
1.330 misha 1315: Value& Request::get_element(Value& ncontext, const String& name) {
1.350 misha 1316: if(!GET_CONSTRUCTING(wcontext) // not constructing
1.330 misha 1317: && wcontext->get_somebody_entered_some_class() ) // ^class:method
1.297 paf 1318: if(VStateless_class *called_class=ncontext.get_class())
1.249 paf 1319: if(VStateless_class *read_class=rcontext->get_class())
1320: if(read_class->derived_from(*called_class)) // current derived from called
1.297 paf 1321: if(Value* base=get_self().base()) { // doing DYNAMIC call
1.330 misha 1322: Temp_derived temp_derived(*base, 0); // temporarily prevent go-back-down virtual calls
1323: Value *value=base->get_element(name, *base, true); // virtual-up lookup starting from parent
1324: return *(value ? &process_to_value(*value) : VVoid::get());
1.249 paf 1325: }
1.330 misha 1326:
1327: Value* value=ncontext.get_element(name, ncontext, true);
1.291 paf 1328:
1.350 misha 1329: if(value && GET_CONSTRUCTING(wcontext))
1.330 misha 1330: if(Junction* junction=value->get_junction())
1.297 paf 1331: if(junction->self.get_class()!=&ncontext)
1.316 misha 1332: throw Exception(PARSER_RUNTIME,
1.291 paf 1333: &name,
1.297 paf 1334: "constructor must be declared in class '%s'",
1335: ncontext.get_class()->name_cstr());
1.63 paf 1336:
1.330 misha 1337: return *(value ? &process_to_value(*value) : VVoid::get());
1.309 paf 1338: }
1339:
1.329 misha 1340: void Request::put_element(Value& ncontext, const String& name, Value* value) {
1.309 paf 1341: // put_element can return property-setting-junction
1.329 misha 1342: if(const VJunction* vjunction=ncontext.put_element(ncontext, name, value, false))
1.312 paf 1343: if(vjunction!=PUT_ELEMENT_REPLACED_ELEMENT) {
1.309 paf 1344: // process it
1.329 misha 1345: VMethodFrame frame(vjunction->junction(), method_frame/*caller*/);
1346: int param_count=frame.method_params_count();
1347:
1.309 paf 1348: if(param_count!=1)
1.316 misha 1349: throw Exception(PARSER_RUNTIME,
1.309 paf 1350: 0,
1351: "setter method must have ONE parameter (has %d parameters)", param_count);
1352:
1.329 misha 1353: frame.store_params(&value, 1);
1.309 paf 1354: frame.set_self(frame.junction.self);
1355:
1.330 misha 1356: SAVE_CONTEXT
1.309 paf 1357:
1358: rcontext=wcontext=&frame;
1359: method_frame=&frame;
1360:
1.311 paf 1361: // prevent non-string writes for better error reporting [setters are not expected to return anything]
1.309 paf 1362: wcontext->write(*method_frame);
1363:
1364: recoursion_checked_execute(*frame.junction.method->parser_code); // parser code, execute it
1365: // we don't need it StringOrValue result=wcontext->result();
1366:
1.330 misha 1367: RESTORE_CONTEXT
1.309 paf 1368: }
1.34 paf 1369: }
1.70 paf 1370:
1.162 parser 1371: /** @param intercept_string
1.116 paf 1372: - true:
1373: they want result=string value,
1374: possible object result goes to wcontext
1375: - false:
1376: they want any result[string|object]
1377: nothing goes to wcontext.
1.125 paf 1378: used in @c (expression) params evaluation
1.224 paf 1379:
1.334 misha 1380: using the fact it's either string_ or value_ result requested to speed up checkes
1.116 paf 1381: */
1.229 paf 1382: StringOrValue Request::process(Value& input_value, bool intercept_string) {
1.297 paf 1383: Junction* junction=input_value.get_junction();
1.307 paf 1384: if(junction) {
1385: if(junction->is_getter) { // is it a getter-junction?
1.329 misha 1386: VMethodFrame frame(*junction, method_frame/*caller*/);
1387: int param_count=frame.method_params_count();
1388:
1.331 misha 1389: if(param_count){
1390: if(junction->auto_name){ // default getter
1391: if(param_count==1){
1392: Value *param=new VString(*junction->auto_name);
1393: frame.store_params(¶m, 1);
1394: } else
1395: throw Exception(PARSER_RUNTIME,
1396: 0,
1397: "default getter method can't have more then 1 parameter (has %d parameters)", param_count);
1398: } else
1.320 misha 1399: throw Exception(PARSER_RUNTIME,
1400: 0,
1.331 misha 1401: "getter method must have no parameters (has %d parameters)", param_count);
1402: } // no need for else frame.empty_params()
1.320 misha 1403:
1.307 paf 1404: frame.set_self(frame.junction.self);
1405:
1.330 misha 1406: SAVE_CONTEXT
1.307 paf 1407:
1408: rcontext=wcontext=&frame;
1409: method_frame=&frame;
1410:
1411: recoursion_checked_execute(*frame.junction.method->parser_code); // parser code, execute it
1412: StringOrValue result=wcontext->result();
1413:
1.330 misha 1414: RESTORE_CONTEXT
1.307 paf 1415:
1416: return result;
1417: }
1418:
1419: if(junction->code) { // is it a code-junction?
1420: // process it
1421: StringOrValue result;
1.336 misha 1422:
1423: DEBUG_PRINT_STR("ja->\n")
1.238 paf 1424:
1.307 paf 1425: if(!junction->method_frame)
1.316 misha 1426: throw Exception(PARSER_RUNTIME,
1.240 paf 1427: 0,
1428: "junction used outside of context");
1429:
1.330 misha 1430: SAVE_CONTEXT
1.307 paf 1431:
1432: method_frame=junction->method_frame;
1433: rcontext=junction->rcontext;
1434:
1435: // for expression method params
1436: // wcontext is set 0
1437: // using the fact in decision "which wwrapper to use"
1438: bool using_code_frame=intercept_string && junction->wcontext;
1439: if(using_code_frame) {
1.318 misha 1440: // almost plain wwrapper about junction wcontext
1441:
1442: VCodeFrame local(*junction->wcontext);
1.307 paf 1443: wcontext=&local;
1444:
1445: // execute it
1446: recoursion_checked_execute(*junction->code);
1447:
1448: // CodeFrame soul:
1.318 misha 1449: result=wcontext->result();
1.307 paf 1450: } else {
1451: // plain wwrapper
1.351 ! misha 1452: WWrapper local(wcontext);
1.307 paf 1453: wcontext=&local;
1454:
1455: // execute it
1456: recoursion_checked_execute(*junction->code);
1457:
1458: result=wcontext->result();
1459: }
1460:
1.330 misha 1461: RESTORE_CONTEXT
1.207 paf 1462:
1.336 misha 1463: DEBUG_PRINT_STR("<-ja returned")
1464:
1.307 paf 1465: return result;
1466: }
1467:
1468: // it is then method-junction, do not explode it
1469: // just return it as we do for usual objects
1470: }
1471:
1472: return input_value;
1.85 paf 1473: }
1474:
1.332 misha 1475: void Request::process_write(Value& input_value) {
1476: Junction* junction=input_value.get_junction();
1477: if(junction) {
1478: if(junction->is_getter) { // is it a getter-junction?
1479: VMethodFrame frame(*junction, method_frame/*caller*/);
1480: int param_count=frame.method_params_count();
1481:
1482: if(param_count){
1483: if(junction->auto_name){ // default getter
1484: if(param_count==1){
1485: Value *param=new VString(*junction->auto_name);
1486: frame.store_params(¶m, 1);
1487: } else
1488: throw Exception(PARSER_RUNTIME,
1489: 0,
1490: "default getter method can't have more then 1 parameter (has %d parameters)", param_count);
1491: } else
1492: throw Exception(PARSER_RUNTIME,
1493: 0,
1494: "getter method must have no parameters (has %d parameters)", param_count);
1495: } // no need for else frame.empty_params()
1496:
1497: frame.set_self(frame.junction.self);
1498:
1499: SAVE_CONTEXT
1500:
1501: rcontext=wcontext=&frame;
1502: method_frame=&frame;
1503:
1504: recoursion_checked_execute(*frame.junction.method->parser_code); // parser code, execute it
1505:
1506: RESTORE_CONTEXT
1507:
1508: write_pass_lang(frame.result());
1509: return;
1510: }
1511:
1512: if(junction->code) { // is it a code-junction?
1513: // process it
1.336 misha 1514:
1515: DEBUG_PRINT_STR("ja->\n")
1516:
1.332 misha 1517: if(!junction->method_frame)
1518: throw Exception(PARSER_RUNTIME,
1519: 0,
1520: "junction used outside of context");
1521:
1522: SAVE_CONTEXT
1523:
1524: method_frame=junction->method_frame;
1525: rcontext=junction->rcontext;
1526:
1527: // for expression method params
1528: // wcontext is set 0
1529: // using the fact in decision "which wwrapper to use"
1530: #ifdef OPTIMIZE_CALL
1531: if(wcontext==junction->wcontext){
1532: // no wrappers for wcontext
1533: recoursion_checked_execute(*junction->code);
1534: RESTORE_CONTEXT
1535:
1536: } else
1537: #endif
1538: if(junction->wcontext) {
1539: // almost plain wwrapper about junction wcontext
1540: VCodeFrame local(*junction->wcontext);
1541: wcontext=&local;
1542:
1543: // execute it
1544: recoursion_checked_execute(*junction->code);
1545: RESTORE_CONTEXT
1546: write_pass_lang(local.result());
1547: } else {
1548: // plain wwrapper
1.351 ! misha 1549: WWrapper local(wcontext);
1.332 misha 1550: wcontext=&local;
1551:
1552: // execute it
1553: recoursion_checked_execute(*junction->code);
1554: RESTORE_CONTEXT
1555: write_pass_lang(local.result());
1556: }
1.336 misha 1557:
1558: DEBUG_PRINT_STR("<-ja returned")
1559:
1.332 misha 1560: return;
1561: }
1562:
1563: // it is then method-junction, do not explode it
1564: // just return it as we do for usual objects
1565: }
1566:
1567: write_pass_lang(input_value);
1568: }
1569:
1.298 paf 1570: StringOrValue Request::execute_method(VMethodFrame& amethod_frame, const Method& method) {
1.330 misha 1571: SAVE_CONTEXT
1.99 paf 1572:
1573: // initialize contexts
1.286 paf 1574: rcontext=wcontext=method_frame=&amethod_frame;
1.99 paf 1575:
1576: // execute!
1577: execute(*method.parser_code);
1578:
1579: // result
1.298 paf 1580: StringOrValue result=wcontext->result();
1.208 paf 1581:
1.330 misha 1582: RESTORE_CONTEXT
1.242 paf 1583:
1584: // return
1585: return result;
1.208 paf 1586: }
1587:
1.297 paf 1588: const String* Request::execute_method(Value& aself,
1.329 misha 1589: const Method& method, Value* optional_param,
1590: bool do_return_string) {
1.317 misha 1591:
1.330 misha 1592: SAVE_CONTEXT
1.208 paf 1593:
1.307 paf 1594: Junction local_junction(aself, &method);
1.321 misha 1595: VMethodFrame local_frame(local_junction, method_frame/*caller*/);
1.329 misha 1596: if(optional_param && local_frame.method_params_count()>0) {
1597: local_frame.store_params(&optional_param, 1);
1.330 misha 1598: } else {
1599: local_frame.empty_params();
1.242 paf 1600: }
1.285 paf 1601: local_frame.set_self(aself);
1.257 paf 1602: rcontext=wcontext=method_frame=&local_frame;
1.246 paf 1603:
1604: // prevent non-string writes for better error reporting
1.297 paf 1605: if(do_return_string)
1.246 paf 1606: wcontext->write(local_frame);
1.208 paf 1607:
1608: // execute!
1609: execute(*method.parser_code);
1610:
1611: // result
1.297 paf 1612: const String* result=0;
1613: if(do_return_string)
1614: result=&wcontext->result().as_string();
1.99 paf 1615:
1.330 misha 1616: RESTORE_CONTEXT
1.297 paf 1617:
1618: return result;
1.242 paf 1619: }
1620:
1.297 paf 1621: Request::Execute_nonvirtual_method_result
1622: Request::execute_nonvirtual_method(VStateless_class& aclass,
1.330 misha 1623: const String& method_name,
1624: VString* optional_param,
1625: bool do_return_string) {
1.297 paf 1626: Execute_nonvirtual_method_result result;
1627: result.method=aclass.get_method(method_name);
1628: if(result.method)
1.317 misha 1629: result.string=execute_method(aclass, *result.method, optional_param, do_return_string);
1.297 paf 1630: return result;
1.99 paf 1631: }
1632:
1.297 paf 1633: const String* Request::execute_virtual_method(Value& aself,
1.330 misha 1634: const String& method_name) {
1.297 paf 1635: if(Value* value=aself.get_element(method_name, aself, false))
1636: if(Junction* junction=value->get_junction())
1637: if(const Method *method=junction->method)
1638: return execute_method(aself, *method, 0/*no params*/, true);
1.188 parser 1639:
1.176 parser 1640: return 0;
1641: }