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