|
|
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.349 ! misha 8: static const char * const IDENT_EXECUTE_C="$Date: 2009-06-13 07:05:57 $";
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: {
837: VMethodFrame frame(*junction, method_frame);
838: if(local_ops){ // store param code goes here
839: size_t first = stack.top_index();
840: execute(*local_ops);
841:
842: frame.store_params((Value**)stack.ptr(first), stack.top_index()-first);
843: op_call(frame);
844:
845: stack.set_top_index(first);
846: } else {
847: frame.empty_params();
848: op_call(frame);
849: }
1.346 misha 850: write_pass_lang(frame.result());
1.332 misha 851: }
1.336 misha 852:
853: DEBUG_PRINT_STR("<-returned")
1.327 misha 854:
855: if(get_skip())
856: return;
857: if(get_interrupted()) {
858: set_interrupted(false);
859: throw Exception("parser.interrupted",
860: 0,
861: "execution stopped");
862: }
1.49 paf 863: break;
864: }
865:
1.348 misha 866: #ifdef OPTIMIZE_BYTECODE_CONSTRUCT_OBJECT
867: case OP::OP_CONSTRUCT_OBJECT:
868: case OP::OP_CONSTRUCT_OBJECT__WRITE:
869: {
870: // maybe they do ^class:method[] call, remember the fact
871: wcontext->set_somebody_entered_some_class();
872:
873: debug_origin=i.next().origin;
874: Value& vclass_name=*i.next().value;
875: const String& class_name=*vclass_name.get_string();
876:
877: DEBUG_PRINT_STRING(class_name)
878:
879: Value* class_value=classes().get(class_name);
880: if(!class_value)
881: throw Exception(PARSER_RUNTIME,
882: &class_name,
883: "class is undefined");
884:
885: wcontext->set_constructing(true);
886:
887: debug_origin=i.next().origin;
888: Value& vconstructor_name=*i.next().value;
889: const String& constructor_name=*vconstructor_name.get_string(); debug_name=&constructor_name;
890:
891: DEBUG_PRINT_STRING(constructor_name)
892:
893: Value& constructor_value=get_element(*class_value, constructor_name);
894:
895: ArrayOperation* local_ops=i.next().ops;
896: DEBUG_PRINT_OPS(local_ops)
897: DEBUG_PRINT_STR("->\n")
898:
899: Junction* junction=constructor_value.get_junction();
900: if(!junction) {
901: if(constructor_value.is("void"))
902: throw Exception(PARSER_RUNTIME,
903: 0,
904: "undefined method");
905: else
906: throw Exception(PARSER_RUNTIME,
907: 0,
908: "is '%s', not a method or junction, can not call it",
909: constructor_value.type());
910: } else {
911: VMethodFrame frame(*junction, method_frame);
912: if(local_ops){ // store param code goes here
913: size_t first = stack.top_index();
914: execute(*local_ops);
915:
916: frame.store_params((Value**)stack.ptr(first), stack.top_index()-first);
917: op_call(frame);
918:
919: stack.set_top_index(first);
920: } else {
921: frame.empty_params();
922: op_call(frame);
923: }
924:
925: if(opcode==OP::OP_CONSTRUCT_OBJECT)
926: stack.push(frame.result().as_value());
927: else
928: write_pass_lang(frame.result());
929: }
930:
931: DEBUG_PRINT_STR("<-returned")
932:
933:
934: /*
935: if(get_skip())
936: return;
937: if(get_interrupted()) {
938: set_interrupted(false);
939: throw Exception("parser.interrupted",
940: 0,
941: "execution stopped");
942: }
943: */
944: break;
945: }
946: #endif // OPTIMIZE_BYTECODE_CONSTRUCT_OBJECT
947:
1.55 paf 948: // expression ops: unary
1.322 misha 949: case OP::OP_NEG:
1.55 paf 950: {
1.297 paf 951: Value& a=stack.pop().value();
952: Value& value=*new VDouble(-a.as_double());
953: stack.push(value);
1.55 paf 954: break;
955: }
1.322 misha 956: case OP::OP_INV:
1.55 paf 957: {
1.297 paf 958: Value& a=stack.pop().value();
959: Value& value=*new VDouble(~a.as_int());
960: stack.push(value);
1.55 paf 961: break;
962: }
1.322 misha 963: case OP::OP_NOT:
1.55 paf 964: {
1.297 paf 965: Value& a=stack.pop().value();
1.327 misha 966: Value& value=VBool::get(!a.as_bool());
1.297 paf 967: stack.push(value);
1.55 paf 968: break;
969: }
1.322 misha 970: case OP::OP_DEF:
1.62 paf 971: {
1.297 paf 972: Value& a=stack.pop().value();
1.327 misha 973: Value& value=VBool::get(a.is_defined());
1.297 paf 974: stack.push(value);
1.62 paf 975: break;
976: }
1.322 misha 977: case OP::OP_IN:
1.62 paf 978: {
1.297 paf 979: Value& a=stack.pop().value();
980: const String& path=a.as_string();
1.327 misha 981: Value& value=VBool::get(request_info.uri && *request_info.uri && path.this_starts(request_info.uri));
1.297 paf 982: stack.push(value);
1.62 paf 983: break;
984: }
1.322 misha 985: case OP::OP_FEXISTS:
1.62 paf 986: {
1.297 paf 987: Value& a=stack.pop().value();
1.327 misha 988: Value& value=VBool::get(file_exist(absolute(a.as_string())));
1.297 paf 989: stack.push(value);
1.148 paf 990: break;
991: }
1.322 misha 992: case OP::OP_DEXISTS:
1.148 paf 993: {
1.297 paf 994: Value& a=stack.pop().value();
1.327 misha 995: Value& value=VBool::get(dir_exists(absolute(a.as_string())));
1.297 paf 996: stack.push(value);
1.62 paf 997: break;
998: }
1.55 paf 999:
1000: // expression ops: binary
1.322 misha 1001: case OP::OP_SUB:
1.53 paf 1002: {
1.297 paf 1003: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1004: Value& value=*new VDouble(a.as_double() - b.as_double());
1005: stack.push(value);
1.53 paf 1006: break;
1007: }
1.322 misha 1008: case OP::OP_ADD:
1.53 paf 1009: {
1.297 paf 1010: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1011: Value& value=*new VDouble(a.as_double() + b.as_double());
1012: stack.push(value);
1.53 paf 1013: break;
1014: }
1.322 misha 1015: case OP::OP_MUL:
1.49 paf 1016: {
1.297 paf 1017: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1018: Value& value=*new VDouble(a.as_double() * b.as_double());
1019: stack.push(value);
1.53 paf 1020: break;
1021: }
1.322 misha 1022: case OP::OP_DIV:
1.53 paf 1023: {
1.297 paf 1024: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1.170 parser 1025:
1.297 paf 1026: double a_double=a.as_double();
1027: double b_double=b.as_double();
1.170 parser 1028:
1.171 parser 1029: if(b_double == 0) {
1.297 paf 1030: //const String* problem_source=b.as_string();
1.223 paf 1031: throw Exception("number.zerodivision",
1.297 paf 1032: 0, //problem_source,
1.170 parser 1033: "Division by zero");
1.171 parser 1034: }
1.170 parser 1035:
1.297 paf 1036: Value& value=*new VDouble(a_double / b_double);
1037: stack.push(value);
1.54 paf 1038: break;
1039: }
1.322 misha 1040: case OP::OP_MOD:
1.55 paf 1041: {
1.297 paf 1042: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1.170 parser 1043:
1.297 paf 1044: double a_double=a.as_double();
1045: double b_double=b.as_double();
1.170 parser 1046:
1.173 parser 1047: if(b_double == 0) {
1.297 paf 1048: //const String* problem_source=b.as_string();
1.223 paf 1049: throw Exception("number.zerodivision",
1.297 paf 1050: 0, //problem_source,
1.170 parser 1051: "Modulus by zero");
1.171 parser 1052: }
1.170 parser 1053:
1.297 paf 1054: Value& value=*new VDouble(fmod(a_double, b_double));
1055: stack.push(value);
1.201 paf 1056: break;
1057: }
1.322 misha 1058: case OP::OP_INTDIV:
1.201 paf 1059: {
1.297 paf 1060: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1.201 paf 1061:
1.297 paf 1062: int a_int=a.as_int();
1063: int b_int=b.as_int();
1.201 paf 1064:
1065: if(b_int == 0) {
1.297 paf 1066: //const String* problem_source=b.as_string();
1.223 paf 1067: throw Exception("number.zerodivision",
1.297 paf 1068: 0, //problem_source,
1.201 paf 1069: "Division by zero");
1070: }
1071:
1.297 paf 1072: Value& value=*new VInt(a_int / b_int);
1073: stack.push(value);
1.55 paf 1074: break;
1075: }
1.322 misha 1076: case OP::OP_BIN_SL:
1.274 paf 1077: {
1.297 paf 1078: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1079: Value& value=*new VInt(
1080: a.as_int() <<
1081: b.as_int());
1082: stack.push(value);
1.274 paf 1083: break;
1084: }
1.322 misha 1085: case OP::OP_BIN_SR:
1.274 paf 1086: {
1.297 paf 1087: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1088: Value& value=*new VInt(
1089: a.as_int() >>
1090: b.as_int());
1091: stack.push(value);
1.274 paf 1092: break;
1093: }
1.322 misha 1094: case OP::OP_BIN_AND:
1.54 paf 1095: {
1.297 paf 1096: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1097: Value& value=*new VInt(
1098: a.as_int() &
1099: b.as_int());
1100: stack.push(value);
1.54 paf 1101: break;
1102: }
1.322 misha 1103: case OP::OP_BIN_OR:
1.54 paf 1104: {
1.297 paf 1105: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1106: Value& value=*new VInt(
1107: a.as_int() |
1108: b.as_int());
1109: stack.push(value);
1.55 paf 1110: break;
1111: }
1.322 misha 1112: case OP::OP_BIN_XOR:
1.56 paf 1113: {
1.297 paf 1114: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1115: Value& value=*new VInt(
1116: a.as_int() ^
1117: b.as_int());
1118: stack.push(value);
1.56 paf 1119: break;
1120: }
1.322 misha 1121: case OP::OP_LOG_AND:
1.55 paf 1122: {
1.297 paf 1123: ArrayOperation& local_ops=stack.pop().ops(); Value& a=stack.pop().value();
1.263 paf 1124: bool result;
1.297 paf 1125: if(a.as_bool()) {
1126: execute(local_ops);
1127: Value& b=stack.pop().value();
1128: result=b.as_bool();
1.209 paf 1129: } else
1.263 paf 1130: result=false;
1.327 misha 1131: Value& value=VBool::get(result);
1.297 paf 1132: stack.push(value);
1.55 paf 1133: break;
1134: }
1.322 misha 1135: case OP::OP_LOG_OR:
1.55 paf 1136: {
1.297 paf 1137: ArrayOperation& local_ops=stack.pop().ops(); Value& a=stack.pop().value();
1.263 paf 1138: bool result;
1.297 paf 1139: if(a.as_bool())
1.263 paf 1140: result=true;
1.209 paf 1141: else {
1.297 paf 1142: execute(local_ops);
1143: Value& b=stack.pop().value();
1144: result=b.as_bool();
1.209 paf 1145: }
1.327 misha 1146: Value& value=VBool::get(result);
1.297 paf 1147: stack.push(value);
1.56 paf 1148: break;
1149: }
1.322 misha 1150: case OP::OP_LOG_XOR:
1.56 paf 1151: {
1.297 paf 1152: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1.327 misha 1153: Value& value=VBool::get(a.as_bool() ^ b.as_bool());
1.297 paf 1154: stack.push(value);
1.55 paf 1155: break;
1156: }
1.322 misha 1157: case OP::OP_NUM_LT:
1.55 paf 1158: {
1.297 paf 1159: volatile double b_double=stack.pop().value().as_double();
1160: volatile double a_double=stack.pop().value().as_double();
1.327 misha 1161: Value& value=VBool::get(a_double<b_double);
1.297 paf 1162: stack.push(value);
1.55 paf 1163: break;
1164: }
1.322 misha 1165: case OP::OP_NUM_GT:
1.55 paf 1166: {
1.297 paf 1167: volatile double b_double=stack.pop().value().as_double();
1168: volatile double a_double=stack.pop().value().as_double();
1.327 misha 1169: Value& value=VBool::get(a_double>b_double);
1.297 paf 1170: stack.push(value);
1.55 paf 1171: break;
1172: }
1.322 misha 1173: case OP::OP_NUM_LE:
1.55 paf 1174: {
1.297 paf 1175: volatile double b_double=stack.pop().value().as_double();
1176: volatile double a_double=stack.pop().value().as_double();
1.327 misha 1177: Value& value=VBool::get(a_double<=b_double);
1.297 paf 1178: stack.push(value);
1.55 paf 1179: break;
1180: }
1.322 misha 1181: case OP::OP_NUM_GE:
1.55 paf 1182: {
1.297 paf 1183: volatile double b_double=stack.pop().value().as_double();
1184: volatile double a_double=stack.pop().value().as_double();
1.327 misha 1185: Value& value=VBool::get(a_double>=b_double);
1.297 paf 1186: stack.push(value);
1.55 paf 1187: break;
1188: }
1.322 misha 1189: case OP::OP_NUM_EQ:
1.55 paf 1190: {
1.297 paf 1191: volatile double b_double=stack.pop().value().as_double();
1192: volatile double a_double=stack.pop().value().as_double();
1.327 misha 1193: Value& value=VBool::get(a_double==b_double);
1.297 paf 1194: stack.push(value);
1.55 paf 1195: break;
1196: }
1.322 misha 1197: case OP::OP_NUM_NE:
1.55 paf 1198: {
1.297 paf 1199: volatile double b_double=stack.pop().value().as_double();
1200: volatile double a_double=stack.pop().value().as_double();
1.327 misha 1201: Value& value=VBool::get(a_double!=b_double);
1.297 paf 1202: stack.push(value);
1.54 paf 1203: break;
1204: }
1.322 misha 1205: case OP::OP_STR_LT:
1.58 paf 1206: {
1.297 paf 1207: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1.327 misha 1208: Value& value=VBool::get(a.as_string() < b.as_string());
1.297 paf 1209: stack.push(value);
1.58 paf 1210: break;
1211: }
1.322 misha 1212: case OP::OP_STR_GT:
1.58 paf 1213: {
1.297 paf 1214: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1.327 misha 1215: Value& value=VBool::get(a.as_string() > b.as_string());
1.297 paf 1216: stack.push(value);
1.58 paf 1217: break;
1218: }
1.322 misha 1219: case OP::OP_STR_LE:
1.58 paf 1220: {
1.297 paf 1221: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1.327 misha 1222: Value& value=VBool::get(a.as_string() <= b.as_string());
1.297 paf 1223: stack.push(value);
1.58 paf 1224: break;
1225: }
1.322 misha 1226: case OP::OP_STR_GE:
1.54 paf 1227: {
1.297 paf 1228: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1.327 misha 1229: Value& value=VBool::get(a.as_string() >= b.as_string());
1.297 paf 1230: stack.push(value);
1.58 paf 1231: break;
1232: }
1.322 misha 1233: case OP::OP_STR_EQ:
1.58 paf 1234: {
1.297 paf 1235: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1.327 misha 1236: Value& value=VBool::get(a.as_string() == b.as_string());
1.297 paf 1237: stack.push(value);
1.58 paf 1238: break;
1239: }
1.322 misha 1240: case OP::OP_STR_NE:
1.58 paf 1241: {
1.297 paf 1242: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1.327 misha 1243: Value& value=VBool::get(a.as_string() != b.as_string());
1.297 paf 1244: stack.push(value);
1.103 paf 1245: break;
1246: }
1.322 misha 1247: case OP::OP_IS:
1.103 paf 1248: {
1.297 paf 1249: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1.327 misha 1250: Value& value=VBool::get(a.is(b.as_string().cstr()));
1.297 paf 1251: stack.push(value);
1.28 paf 1252: break;
1253: }
1254:
1.11 paf 1255: default:
1.223 paf 1256: throw Exception(0,
1.67 paf 1257: 0,
1.297 paf 1258: "invalid opcode %d", opcode);
1.11 paf 1259: }
1260: }
1.306 paf 1261: } catch(const Exception&) {
1.297 paf 1262: // record it to stack trace
1.301 paf 1263: if(debug_name)
1264: exception_trace.push(Trace(debug_name, debug_origin));
1.297 paf 1265: rethrow;
1266: }
1.1 paf 1267: }
1.17 paf 1268:
1.330 misha 1269: #define SAVE_CONTEXT \
1270: VMethodFrame *saved_method_frame=method_frame; \
1271: Value* saved_rcontext=rcontext; \
1272: WContext *saved_wcontext=wcontext;
1273:
1274: #define RESTORE_CONTEXT \
1275: wcontext=saved_wcontext; \
1276: rcontext=saved_rcontext; \
1277: method_frame=saved_method_frame;
1278:
1.332 misha 1279: void Request::op_call(VMethodFrame& frame){
1280: const Junction &junction=frame.junction;
1281: VStateless_class& called_class=*junction.self.get_class();
1.319 misha 1282: Value* new_self;
1.332 misha 1283:
1.319 misha 1284: if(wcontext->get_constructing()) {
1285: wcontext->set_constructing(false);
1.332 misha 1286: if(junction.method->call_type!=Method::CT_STATIC) {
1.319 misha 1287: // this is a constructor call
1.349 ! misha 1288: if(new_self=called_class.create_new_value(fpool, 0 /*creating fields only in VClass*/)) {
1.319 misha 1289: // some stateless_class creatable derivates
1290: } else
1291: throw Exception(PARSER_RUNTIME,
1292: 0, //&frame.name(),
1293: "is not a constructor, system class '%s' can be constructed only implicitly",
1294: called_class.name().cstr());
1295:
1296: frame.write(*new_self,
1297: String::L_CLEAN // not used, always an object, not string
1298: );
1299: } else
1300: throw Exception(PARSER_RUNTIME,
1301: 0, //&frame.name(),
1302: "method is static and can not be used as constructor");
1.332 misha 1303: } else
1304: new_self=&junction.self;
1.319 misha 1305:
1306: frame.set_self(*new_self);
1307:
1308: // see OP_PREPARE_TO_EXPRESSION
1309: frame.set_in_expression(wcontext->get_in_expression());
1310:
1.332 misha 1311: SAVE_CONTEXT
1312:
1.319 misha 1313: rcontext=wcontext=&frame;
1.332 misha 1314: method_frame=&frame;
1.319 misha 1315:
1.332 misha 1316: const Method& method=*junction.method;
1317: Method::Call_type call_type=&called_class==new_self ? Method::CT_STATIC : Method::CT_DYNAMIC;
1.319 misha 1318:
1.332 misha 1319: if(method.call_type==Method::CT_ANY || method.call_type==call_type) { // allowed call type?
1320: if(method.native_code) { // native code?
1321: method.check_actual_numbered_params(junction.self, frame.numbered_params());
1322: method.native_code(*this, *frame.numbered_params()); // execute it
1323: } else // parser code, execute it
1324: recoursion_checked_execute(*method.parser_code);
1325: } else
1326: throw Exception(PARSER_RUNTIME,
1327: 0,
1328: "is not allowed to be called %s",
1329: call_type==Method::CT_STATIC?"statically":"dynamically");
1.334 misha 1330:
1.330 misha 1331: RESTORE_CONTEXT
1.332 misha 1332: //return &frame;
1333: }
1334:
1335: void Request::op_call_write(VMethodFrame& frame){
1336: const Junction &junction=frame.junction;
1337:
1338: frame.set_self(junction.self);
1339:
1340: VMethodFrame *saved_method_frame=method_frame;
1341: Value* saved_rcontext=rcontext;
1.319 misha 1342:
1.332 misha 1343: rcontext=&frame;
1344: method_frame=&frame;
1345:
1346: const Method& method=*junction.method;
1347: Method::Call_type call_type=junction.self.get_class()==&junction.self ? Method::CT_STATIC : Method::CT_DYNAMIC;
1348:
1.333 misha 1349: if(method.call_type==Method::CT_ANY || method.call_type==call_type) { // allowed call type?
1350: if(method.native_code) { // native code?
1351: method.check_actual_numbered_params(junction.self, frame.numbered_params());
1352: method.native_code(*this, *frame.numbered_params()); // execute it
1353: } else // parser code, execute it
1354: recoursion_checked_execute(*method.parser_code);
1.332 misha 1355: } else
1356: throw Exception(PARSER_RUNTIME,
1357: 0,
1358: "is not allowed to be called %s",
1359: call_type==Method::CT_STATIC?"statically":"dynamically");
1360:
1361: rcontext=saved_rcontext;
1362: method_frame=saved_method_frame;
1.319 misha 1363: }
1364:
1.292 paf 1365: /**
1366: @bug ^superbase:method would dynamically call ^base:method if there is any
1367: */
1.330 misha 1368: Value& Request::get_element(Value& ncontext, const String& name) {
1369: if(!wcontext->get_constructing() // not constructing
1370: && wcontext->get_somebody_entered_some_class() ) // ^class:method
1.297 paf 1371: if(VStateless_class *called_class=ncontext.get_class())
1.249 paf 1372: if(VStateless_class *read_class=rcontext->get_class())
1373: if(read_class->derived_from(*called_class)) // current derived from called
1.297 paf 1374: if(Value* base=get_self().base()) { // doing DYNAMIC call
1.330 misha 1375: Temp_derived temp_derived(*base, 0); // temporarily prevent go-back-down virtual calls
1376: Value *value=base->get_element(name, *base, true); // virtual-up lookup starting from parent
1377: return *(value ? &process_to_value(*value) : VVoid::get());
1.249 paf 1378: }
1.330 misha 1379:
1380: Value* value=ncontext.get_element(name, ncontext, true);
1.291 paf 1381:
1382: if(value && wcontext->get_constructing())
1.330 misha 1383: if(Junction* junction=value->get_junction())
1.297 paf 1384: if(junction->self.get_class()!=&ncontext)
1.316 misha 1385: throw Exception(PARSER_RUNTIME,
1.291 paf 1386: &name,
1.297 paf 1387: "constructor must be declared in class '%s'",
1388: ncontext.get_class()->name_cstr());
1.63 paf 1389:
1.330 misha 1390: return *(value ? &process_to_value(*value) : VVoid::get());
1.309 paf 1391: }
1392:
1.329 misha 1393: void Request::put_element(Value& ncontext, const String& name, Value* value) {
1.309 paf 1394: // put_element can return property-setting-junction
1.329 misha 1395: if(const VJunction* vjunction=ncontext.put_element(ncontext, name, value, false))
1.312 paf 1396: if(vjunction!=PUT_ELEMENT_REPLACED_ELEMENT) {
1.309 paf 1397: // process it
1.329 misha 1398: VMethodFrame frame(vjunction->junction(), method_frame/*caller*/);
1399: int param_count=frame.method_params_count();
1400:
1.309 paf 1401: if(param_count!=1)
1.316 misha 1402: throw Exception(PARSER_RUNTIME,
1.309 paf 1403: 0,
1404: "setter method must have ONE parameter (has %d parameters)", param_count);
1405:
1.329 misha 1406: frame.store_params(&value, 1);
1.309 paf 1407: frame.set_self(frame.junction.self);
1408:
1.330 misha 1409: SAVE_CONTEXT
1.309 paf 1410:
1411: rcontext=wcontext=&frame;
1412: method_frame=&frame;
1413:
1.311 paf 1414: // prevent non-string writes for better error reporting [setters are not expected to return anything]
1.309 paf 1415: wcontext->write(*method_frame);
1416:
1417: recoursion_checked_execute(*frame.junction.method->parser_code); // parser code, execute it
1418: // we don't need it StringOrValue result=wcontext->result();
1419:
1.330 misha 1420: RESTORE_CONTEXT
1.309 paf 1421: }
1.34 paf 1422: }
1.70 paf 1423:
1.162 parser 1424: /** @param intercept_string
1.116 paf 1425: - true:
1426: they want result=string value,
1427: possible object result goes to wcontext
1428: - false:
1429: they want any result[string|object]
1430: nothing goes to wcontext.
1.125 paf 1431: used in @c (expression) params evaluation
1.224 paf 1432:
1.334 misha 1433: using the fact it's either string_ or value_ result requested to speed up checkes
1.116 paf 1434: */
1.229 paf 1435: StringOrValue Request::process(Value& input_value, bool intercept_string) {
1.297 paf 1436: Junction* junction=input_value.get_junction();
1.307 paf 1437: if(junction) {
1438: if(junction->is_getter) { // is it a getter-junction?
1.329 misha 1439: VMethodFrame frame(*junction, method_frame/*caller*/);
1440: int param_count=frame.method_params_count();
1441:
1.331 misha 1442: if(param_count){
1443: if(junction->auto_name){ // default getter
1444: if(param_count==1){
1445: Value *param=new VString(*junction->auto_name);
1446: frame.store_params(¶m, 1);
1447: } else
1448: throw Exception(PARSER_RUNTIME,
1449: 0,
1450: "default getter method can't have more then 1 parameter (has %d parameters)", param_count);
1451: } else
1.320 misha 1452: throw Exception(PARSER_RUNTIME,
1453: 0,
1.331 misha 1454: "getter method must have no parameters (has %d parameters)", param_count);
1455: } // no need for else frame.empty_params()
1.320 misha 1456:
1.307 paf 1457: frame.set_self(frame.junction.self);
1458:
1.330 misha 1459: SAVE_CONTEXT
1.307 paf 1460:
1461: rcontext=wcontext=&frame;
1462: method_frame=&frame;
1463:
1464: recoursion_checked_execute(*frame.junction.method->parser_code); // parser code, execute it
1465: StringOrValue result=wcontext->result();
1466:
1.330 misha 1467: RESTORE_CONTEXT
1.307 paf 1468:
1469: return result;
1470: }
1471:
1472: if(junction->code) { // is it a code-junction?
1473: // process it
1474: StringOrValue result;
1.336 misha 1475:
1476: DEBUG_PRINT_STR("ja->\n")
1.238 paf 1477:
1.307 paf 1478: if(!junction->method_frame)
1.316 misha 1479: throw Exception(PARSER_RUNTIME,
1.240 paf 1480: 0,
1481: "junction used outside of context");
1482:
1.330 misha 1483: SAVE_CONTEXT
1.307 paf 1484:
1485: method_frame=junction->method_frame;
1486: rcontext=junction->rcontext;
1487:
1488: // for expression method params
1489: // wcontext is set 0
1490: // using the fact in decision "which wwrapper to use"
1491: bool using_code_frame=intercept_string && junction->wcontext;
1492: if(using_code_frame) {
1.318 misha 1493: // almost plain wwrapper about junction wcontext
1494:
1495: VCodeFrame local(*junction->wcontext);
1.307 paf 1496: wcontext=&local;
1497:
1498: // execute it
1499: recoursion_checked_execute(*junction->code);
1500:
1501: // CodeFrame soul:
1.318 misha 1502: result=wcontext->result();
1.307 paf 1503: } else {
1504: // plain wwrapper
1505: WWrapper local(0/*empty*/, wcontext);
1506: wcontext=&local;
1507:
1508: // execute it
1509: recoursion_checked_execute(*junction->code);
1510:
1511: result=wcontext->result();
1512: }
1513:
1.330 misha 1514: RESTORE_CONTEXT
1.207 paf 1515:
1.336 misha 1516: DEBUG_PRINT_STR("<-ja returned")
1517:
1.307 paf 1518: return result;
1519: }
1520:
1521: // it is then method-junction, do not explode it
1522: // just return it as we do for usual objects
1523: }
1524:
1525: return input_value;
1.85 paf 1526: }
1527:
1.332 misha 1528: void Request::process_write(Value& input_value) {
1529: Junction* junction=input_value.get_junction();
1530: if(junction) {
1531: if(junction->is_getter) { // is it a getter-junction?
1532: VMethodFrame frame(*junction, method_frame/*caller*/);
1533: int param_count=frame.method_params_count();
1534:
1535: if(param_count){
1536: if(junction->auto_name){ // default getter
1537: if(param_count==1){
1538: Value *param=new VString(*junction->auto_name);
1539: frame.store_params(¶m, 1);
1540: } else
1541: throw Exception(PARSER_RUNTIME,
1542: 0,
1543: "default getter method can't have more then 1 parameter (has %d parameters)", param_count);
1544: } else
1545: throw Exception(PARSER_RUNTIME,
1546: 0,
1547: "getter method must have no parameters (has %d parameters)", param_count);
1548: } // no need for else frame.empty_params()
1549:
1550: frame.set_self(frame.junction.self);
1551:
1552: SAVE_CONTEXT
1553:
1554: rcontext=wcontext=&frame;
1555: method_frame=&frame;
1556:
1557: recoursion_checked_execute(*frame.junction.method->parser_code); // parser code, execute it
1558:
1559: RESTORE_CONTEXT
1560:
1561: write_pass_lang(frame.result());
1562: return;
1563: }
1564:
1565: if(junction->code) { // is it a code-junction?
1566: // process it
1.336 misha 1567:
1568: DEBUG_PRINT_STR("ja->\n")
1569:
1.332 misha 1570: if(!junction->method_frame)
1571: throw Exception(PARSER_RUNTIME,
1572: 0,
1573: "junction used outside of context");
1574:
1575: SAVE_CONTEXT
1576:
1577: method_frame=junction->method_frame;
1578: rcontext=junction->rcontext;
1579:
1580: // for expression method params
1581: // wcontext is set 0
1582: // using the fact in decision "which wwrapper to use"
1583: #ifdef OPTIMIZE_CALL
1584: if(wcontext==junction->wcontext){
1585: // no wrappers for wcontext
1586: recoursion_checked_execute(*junction->code);
1587: RESTORE_CONTEXT
1588:
1589: } else
1590: #endif
1591: if(junction->wcontext) {
1592: // almost plain wwrapper about junction wcontext
1593: VCodeFrame local(*junction->wcontext);
1594: wcontext=&local;
1595:
1596: // execute it
1597: recoursion_checked_execute(*junction->code);
1598: RESTORE_CONTEXT
1599: write_pass_lang(local.result());
1600: } else {
1601: // plain wwrapper
1602: WWrapper local(0/*empty*/, wcontext);
1603: wcontext=&local;
1604:
1605: // execute it
1606: recoursion_checked_execute(*junction->code);
1607: RESTORE_CONTEXT
1608: write_pass_lang(local.result());
1609: }
1.336 misha 1610:
1611: DEBUG_PRINT_STR("<-ja returned")
1612:
1.332 misha 1613: return;
1614: }
1615:
1616: // it is then method-junction, do not explode it
1617: // just return it as we do for usual objects
1618: }
1619:
1620: write_pass_lang(input_value);
1621: }
1622:
1.298 paf 1623: StringOrValue Request::execute_method(VMethodFrame& amethod_frame, const Method& method) {
1.330 misha 1624: SAVE_CONTEXT
1.99 paf 1625:
1626: // initialize contexts
1.286 paf 1627: rcontext=wcontext=method_frame=&amethod_frame;
1.99 paf 1628:
1629: // execute!
1630: execute(*method.parser_code);
1631:
1632: // result
1.298 paf 1633: StringOrValue result=wcontext->result();
1.208 paf 1634:
1.330 misha 1635: RESTORE_CONTEXT
1.242 paf 1636:
1637: // return
1638: return result;
1.208 paf 1639: }
1640:
1.297 paf 1641: const String* Request::execute_method(Value& aself,
1.329 misha 1642: const Method& method, Value* optional_param,
1643: bool do_return_string) {
1.317 misha 1644:
1.330 misha 1645: SAVE_CONTEXT
1.208 paf 1646:
1.307 paf 1647: Junction local_junction(aself, &method);
1.321 misha 1648: VMethodFrame local_frame(local_junction, method_frame/*caller*/);
1.329 misha 1649: if(optional_param && local_frame.method_params_count()>0) {
1650: local_frame.store_params(&optional_param, 1);
1.330 misha 1651: } else {
1652: local_frame.empty_params();
1.242 paf 1653: }
1.285 paf 1654: local_frame.set_self(aself);
1.257 paf 1655: rcontext=wcontext=method_frame=&local_frame;
1.246 paf 1656:
1657: // prevent non-string writes for better error reporting
1.297 paf 1658: if(do_return_string)
1.246 paf 1659: wcontext->write(local_frame);
1.208 paf 1660:
1661: // execute!
1662: execute(*method.parser_code);
1663:
1664: // result
1.297 paf 1665: const String* result=0;
1666: if(do_return_string)
1667: result=&wcontext->result().as_string();
1.99 paf 1668:
1.330 misha 1669: RESTORE_CONTEXT
1.297 paf 1670:
1671: return result;
1.242 paf 1672: }
1673:
1.297 paf 1674: Request::Execute_nonvirtual_method_result
1675: Request::execute_nonvirtual_method(VStateless_class& aclass,
1.330 misha 1676: const String& method_name,
1677: VString* optional_param,
1678: bool do_return_string) {
1.297 paf 1679: Execute_nonvirtual_method_result result;
1680: result.method=aclass.get_method(method_name);
1681: if(result.method)
1.317 misha 1682: result.string=execute_method(aclass, *result.method, optional_param, do_return_string);
1.297 paf 1683: return result;
1.99 paf 1684: }
1685:
1.297 paf 1686: const String* Request::execute_virtual_method(Value& aself,
1.330 misha 1687: const String& method_name) {
1.297 paf 1688: if(Value* value=aself.get_element(method_name, aself, false))
1689: if(Junction* junction=value->get_junction())
1690: if(const Method *method=junction->method)
1691: return execute_method(aself, *method, 0/*no params*/, true);
1.188 parser 1692:
1.176 parser 1693: return 0;
1694: }