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