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