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