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