|
|
1.117 paf 1: /** @file
1.118 paf 2: Parser: executor part of request class.
3:
1.324 misha 4: Copyright (c) 2001-2009 ArtLebedev Group (http://www.artlebedev.com)
1.218 paf 5: Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.243 paf 6: */
1.118 paf 7:
1.365 ! moko 8: static const char * const IDENT_EXECUTE_C="$Date: 2010-08-11 16:21:52 $";
1.1 paf 9:
1.152 paf 10: #include "pa_opcode.h"
1.8 paf 11: #include "pa_array.h"
1.11 paf 12: #include "pa_request.h"
1.15 paf 13: #include "pa_vstring.h"
1.22 paf 14: #include "pa_vhash.h"
1.167 parser 15: #include "pa_vvoid.h"
1.145 paf 16: #include "pa_vcode_frame.h"
1.321 misha 17: #include "pa_vmethod_frame.h"
1.38 paf 18: #include "pa_vobject.h"
1.49 paf 19: #include "pa_vdouble.h"
1.54 paf 20: #include "pa_vbool.h"
1.94 paf 21: #include "pa_vtable.h"
1.132 paf 22: #include "pa_vfile.h"
1.144 paf 23: #include "pa_vimage.h"
1.194 parser 24: #include "pa_wwrapper.h"
1.1 paf 25:
1.233 paf 26: //#define DEBUG_EXECUTE
1.157 parser 27:
28: #ifdef DEBUG_EXECUTE
1.1 paf 29: char *opcode_name[]={
1.49 paf 30: // literals
1.109 paf 31: "VALUE", "CURLY_CODE__STORE_PARAM", "EXPR_CODE__STORE_PARAM",
1.209 paf 32: "NESTED_CODE",
1.49 paf 33:
34: // actions
1.187 parser 35: "WITH_ROOT", "WITH_SELF", "WITH_READ", "WITH_WRITE",
1.328 misha 36: "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.297 paf 642: Value* value;
1.49 paf 643: // from "$a $b" part of expression taking only string value,
644: // ignoring any other content of wcontext
1.297 paf 645: if(const String* string=wcontext->get_string())
646: value=new VString(*string);
1.80 paf 647: else
1.324 misha 648: value=VVoid::get();
1.297 paf 649: stack.push(*value);
650:
1.257 paf 651: wcontext=saved_wcontext;
1.49 paf 652: break;
653: }
654:
1.51 paf 655: // CALL
1.322 misha 656: case OP::OP_CURLY_CODE__STORE_PARAM:
657: case OP::OP_EXPR_CODE__STORE_PARAM:
1.28 paf 658: {
1.237 paf 659: // code
1.297 paf 660: ArrayOperation& local_ops=*i.next().ops;
1.336 misha 661:
662: DEBUG_PRINT_OPS((&local_ops))
663:
1.237 paf 664: // when they evaluate expression parameter,
665: // the object expression result
666: // does not need to be written into calling frame
667: // it must go into any expressions using that parameter
668: // hence, we zero junction.wcontext here, and later
669: // in .process we would test that field
670: // in decision "which wwrapper to use"
1.326 misha 671: VJunction& value=*new VJunction(
1.297 paf 672: get_self(), 0,
1.257 paf 673: method_frame,
1.237 paf 674: rcontext,
1.322 misha 675: opcode==OP::OP_EXPR_CODE__STORE_PARAM?0:wcontext,
1.312 paf 676: &local_ops);
1.343 misha 677: #ifndef USE_DESTRUCTORS
1.326 misha 678: if (opcode!=OP::OP_EXPR_CODE__STORE_PARAM)
1.343 misha 679: wcontext->attach_junction(&value);
1.326 misha 680: #endif
1.237 paf 681: // store param
1.329 misha 682: stack.push(value);
1.29 paf 683: break;
684: }
685:
1.322 misha 686: case OP::OP_PREPARE_TO_EXPRESSION:
1.219 paf 687: {
688: wcontext->set_in_expression(true);
689: break;
690: }
691:
1.350 misha 692: #define METHOD_FRAME_ACTION(action) \
693: if(local_ops){ \
694: size_t first = stack.top_index(); \
695: execute(*local_ops); \
696: frame.store_params((Value**)stack.ptr(first), stack.top_index()-first); \
697: action; \
698: stack.set_top_index(first); \
699: } else { \
700: frame.empty_params(); \
701: action; \
702: }
703:
1.322 misha 704: case OP::OP_CALL:
1.29 paf 705: {
1.297 paf 706: ArrayOperation* local_ops=i.next().ops;
1.237 paf 707:
1.336 misha 708: DEBUG_PRINT_OPS(local_ops)
709: DEBUG_PRINT_STR("->\n")
710:
1.297 paf 711: Value& value=stack.pop().value();
1.237 paf 712:
1.297 paf 713: Junction* junction=value.get_junction();
714: if(!junction) {
715: if(value.is("void"))
1.316 misha 716: throw Exception(PARSER_RUNTIME,
1.297 paf 717: 0,
718: "undefined method");
719: else
1.316 misha 720: throw Exception(PARSER_RUNTIME,
1.297 paf 721: 0,
722: "is '%s', not a method or junction, can not call it",
723: value.type());
724: }
1.237 paf 725:
1.363 moko 726: VMethodFrame frame(*junction->method, method_frame, junction->self);
727: METHOD_FRAME_ACTION(op_call(frame));
1.332 misha 728: stack.push(frame.result().as_value());
729:
1.336 misha 730: DEBUG_PRINT_STR("<-returned")
731:
1.332 misha 732: if(get_skip())
733: return;
734: if(get_interrupted()) {
735: set_interrupted(false);
736: throw Exception("parser.interrupted",
737: 0,
738: "execution stopped");
1.237 paf 739: }
1.332 misha 740: break;
741: }
1.220 paf 742:
1.332 misha 743: case OP::OP_CALL__WRITE:
744: {
745: ArrayOperation* local_ops=i.next().ops;
746:
1.336 misha 747: DEBUG_PRINT_OPS(local_ops)
748: DEBUG_PRINT_STR("->\n")
749:
1.332 misha 750: Value& value=stack.pop().value();
751:
752: Junction* junction=value.get_junction();
753: if(!junction) {
754: if(value.is("void"))
755: throw Exception(PARSER_RUNTIME,
756: 0,
757: "undefined method");
758: else
759: throw Exception(PARSER_RUNTIME,
760: 0,
761: "is '%s', not a method or junction, can not call it",
762: value.type());
763: }
764:
765: #ifdef OPTIMIZE_CALL
766: const Method& method=*junction->method;
1.353 misha 767: if(method.call_optimization==Method::CO_WITHOUT_FRAME){
1.332 misha 768: if(local_ops){ // store param code goes here
769: size_t first = stack.top_index();
770: execute(*local_ops);
771:
772: MethodParams method_params;
773: method_params.store_params((Value**)stack.ptr(first), stack.top_index()-first);
774: method.check_actual_numbered_params(junction->self, &method_params);
775: method.native_code(*this, method_params); // execute it
776:
777: stack.set_top_index(first);
778: } else {
779: MethodParams method_params;
780: method.check_actual_numbered_params(junction->self, &method_params);
781: method.native_code(*this, method_params); // execute it
782: }
1.353 misha 783: } else if(method.call_optimization==Method::CO_WITHOUT_WCONTEXT){
1.363 moko 784: VMethodFrame frame(method, method_frame, junction->self);
1.350 misha 785: METHOD_FRAME_ACTION(op_call_write(frame));
1.332 misha 786: } else
787: #endif // OPTIMIZE_CALL
788: {
1.363 moko 789: VMethodFrame frame(method, method_frame, junction->self);
790: METHOD_FRAME_ACTION(op_call(frame));
1.346 misha 791: write_pass_lang(frame.result());
1.332 misha 792: }
1.336 misha 793:
794: DEBUG_PRINT_STR("<-returned")
1.327 misha 795:
796: if(get_skip())
797: return;
798: if(get_interrupted()) {
799: set_interrupted(false);
800: throw Exception("parser.interrupted",
801: 0,
802: "execution stopped");
803: }
1.49 paf 804: break;
805: }
806:
1.348 misha 807: case OP::OP_CONSTRUCT_OBJECT:
808: case OP::OP_CONSTRUCT_OBJECT__WRITE:
809: {
810: debug_origin=i.next().origin;
811: Value& vclass_name=*i.next().value;
1.360 pretende 812: const String& class_name=*vclass_name.get_string(); debug_name=&class_name;
1.348 misha 813:
814: DEBUG_PRINT_STRING(class_name)
815:
1.358 misha 816: Value* class_value=get_class(class_name);
1.348 misha 817: if(!class_value)
818: throw Exception(PARSER_RUNTIME,
819: &class_name,
820: "class is undefined");
821:
822: debug_origin=i.next().origin;
823: Value& vconstructor_name=*i.next().value;
824: const String& constructor_name=*vconstructor_name.get_string(); debug_name=&constructor_name;
825:
826: DEBUG_PRINT_STRING(constructor_name)
827:
1.363 moko 828: Junction* constructor_junction=get_element(*class_value, constructor_name).get_junction();
829: if(!constructor_junction)
1.350 misha 830: throw Exception(PARSER_RUNTIME,
831: &constructor_name,
832: "constructor must be declared in class '%s'",
833: class_value->get_class()->name_cstr());
834:
1.348 misha 835: ArrayOperation* local_ops=i.next().ops;
836: DEBUG_PRINT_OPS(local_ops)
837: DEBUG_PRINT_STR("->\n")
838:
1.363 moko 839: Value &object=construct(*class_value, *constructor_junction->method);
840: VConstructorFrame frame(*constructor_junction->method, method_frame, object);
841: METHOD_FRAME_ACTION(op_call(frame));
1.364 moko 842: object.enable_default_setter();
1.355 misha 843:
1.350 misha 844: if(opcode==OP::OP_CONSTRUCT_OBJECT)
1.363 moko 845: stack.push(object);
1.350 misha 846: else
1.363 moko 847: write_pass_lang(object);
1.348 misha 848:
849: DEBUG_PRINT_STR("<-returned")
850: break;
851: }
852:
1.55 paf 853: // expression ops: unary
1.322 misha 854: case OP::OP_NEG:
1.55 paf 855: {
1.297 paf 856: Value& a=stack.pop().value();
857: Value& value=*new VDouble(-a.as_double());
858: stack.push(value);
1.55 paf 859: break;
860: }
1.322 misha 861: case OP::OP_INV:
1.55 paf 862: {
1.297 paf 863: Value& a=stack.pop().value();
864: Value& value=*new VDouble(~a.as_int());
865: stack.push(value);
1.55 paf 866: break;
867: }
1.322 misha 868: case OP::OP_NOT:
1.55 paf 869: {
1.297 paf 870: Value& a=stack.pop().value();
1.327 misha 871: Value& value=VBool::get(!a.as_bool());
1.297 paf 872: stack.push(value);
1.55 paf 873: break;
874: }
1.322 misha 875: case OP::OP_DEF:
1.62 paf 876: {
1.297 paf 877: Value& a=stack.pop().value();
1.327 misha 878: Value& value=VBool::get(a.is_defined());
1.297 paf 879: stack.push(value);
1.62 paf 880: break;
881: }
1.322 misha 882: case OP::OP_IN:
1.62 paf 883: {
1.297 paf 884: Value& a=stack.pop().value();
885: const String& path=a.as_string();
1.327 misha 886: Value& value=VBool::get(request_info.uri && *request_info.uri && path.this_starts(request_info.uri));
1.297 paf 887: stack.push(value);
1.62 paf 888: break;
889: }
1.322 misha 890: case OP::OP_FEXISTS:
1.62 paf 891: {
1.297 paf 892: Value& a=stack.pop().value();
1.327 misha 893: Value& value=VBool::get(file_exist(absolute(a.as_string())));
1.297 paf 894: stack.push(value);
1.148 paf 895: break;
896: }
1.322 misha 897: case OP::OP_DEXISTS:
1.148 paf 898: {
1.297 paf 899: Value& a=stack.pop().value();
1.327 misha 900: Value& value=VBool::get(dir_exists(absolute(a.as_string())));
1.297 paf 901: stack.push(value);
1.62 paf 902: break;
903: }
1.55 paf 904:
905: // expression ops: binary
1.322 misha 906: case OP::OP_SUB:
1.53 paf 907: {
1.297 paf 908: Value& b=stack.pop().value(); Value& a=stack.pop().value();
909: Value& value=*new VDouble(a.as_double() - b.as_double());
910: stack.push(value);
1.53 paf 911: break;
912: }
1.322 misha 913: case OP::OP_ADD:
1.53 paf 914: {
1.297 paf 915: Value& b=stack.pop().value(); Value& a=stack.pop().value();
916: Value& value=*new VDouble(a.as_double() + b.as_double());
917: stack.push(value);
1.53 paf 918: break;
919: }
1.322 misha 920: case OP::OP_MUL:
1.49 paf 921: {
1.297 paf 922: Value& b=stack.pop().value(); Value& a=stack.pop().value();
923: Value& value=*new VDouble(a.as_double() * b.as_double());
924: stack.push(value);
1.53 paf 925: break;
926: }
1.322 misha 927: case OP::OP_DIV:
1.53 paf 928: {
1.297 paf 929: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1.170 parser 930:
1.297 paf 931: double a_double=a.as_double();
932: double b_double=b.as_double();
1.170 parser 933:
1.171 parser 934: if(b_double == 0) {
1.297 paf 935: //const String* problem_source=b.as_string();
1.223 paf 936: throw Exception("number.zerodivision",
1.297 paf 937: 0, //problem_source,
1.170 parser 938: "Division by zero");
1.171 parser 939: }
1.170 parser 940:
1.297 paf 941: Value& value=*new VDouble(a_double / b_double);
942: stack.push(value);
1.54 paf 943: break;
944: }
1.322 misha 945: case OP::OP_MOD:
1.55 paf 946: {
1.297 paf 947: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1.170 parser 948:
1.297 paf 949: double a_double=a.as_double();
950: double b_double=b.as_double();
1.170 parser 951:
1.173 parser 952: if(b_double == 0) {
1.297 paf 953: //const String* problem_source=b.as_string();
1.223 paf 954: throw Exception("number.zerodivision",
1.297 paf 955: 0, //problem_source,
1.170 parser 956: "Modulus by zero");
1.171 parser 957: }
1.170 parser 958:
1.297 paf 959: Value& value=*new VDouble(fmod(a_double, b_double));
960: stack.push(value);
1.201 paf 961: break;
962: }
1.322 misha 963: case OP::OP_INTDIV:
1.201 paf 964: {
1.297 paf 965: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1.201 paf 966:
1.297 paf 967: int a_int=a.as_int();
968: int b_int=b.as_int();
1.201 paf 969:
970: if(b_int == 0) {
1.297 paf 971: //const String* problem_source=b.as_string();
1.223 paf 972: throw Exception("number.zerodivision",
1.297 paf 973: 0, //problem_source,
1.201 paf 974: "Division by zero");
975: }
976:
1.297 paf 977: Value& value=*new VInt(a_int / b_int);
978: stack.push(value);
1.55 paf 979: break;
980: }
1.322 misha 981: case OP::OP_BIN_SL:
1.274 paf 982: {
1.297 paf 983: Value& b=stack.pop().value(); Value& a=stack.pop().value();
984: Value& value=*new VInt(
985: a.as_int() <<
986: b.as_int());
987: stack.push(value);
1.274 paf 988: break;
989: }
1.322 misha 990: case OP::OP_BIN_SR:
1.274 paf 991: {
1.297 paf 992: Value& b=stack.pop().value(); Value& a=stack.pop().value();
993: Value& value=*new VInt(
994: a.as_int() >>
995: b.as_int());
996: stack.push(value);
1.274 paf 997: break;
998: }
1.322 misha 999: case OP::OP_BIN_AND:
1.54 paf 1000: {
1.297 paf 1001: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1002: Value& value=*new VInt(
1003: a.as_int() &
1004: b.as_int());
1005: stack.push(value);
1.54 paf 1006: break;
1007: }
1.322 misha 1008: case OP::OP_BIN_OR:
1.54 paf 1009: {
1.297 paf 1010: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1011: Value& value=*new VInt(
1012: a.as_int() |
1013: b.as_int());
1014: stack.push(value);
1.55 paf 1015: break;
1016: }
1.322 misha 1017: case OP::OP_BIN_XOR:
1.56 paf 1018: {
1.297 paf 1019: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1020: Value& value=*new VInt(
1021: a.as_int() ^
1022: b.as_int());
1023: stack.push(value);
1.56 paf 1024: break;
1025: }
1.322 misha 1026: case OP::OP_LOG_AND:
1.55 paf 1027: {
1.297 paf 1028: ArrayOperation& local_ops=stack.pop().ops(); Value& a=stack.pop().value();
1.263 paf 1029: bool result;
1.297 paf 1030: if(a.as_bool()) {
1031: execute(local_ops);
1032: Value& b=stack.pop().value();
1033: result=b.as_bool();
1.209 paf 1034: } else
1.263 paf 1035: result=false;
1.327 misha 1036: Value& value=VBool::get(result);
1.297 paf 1037: stack.push(value);
1.55 paf 1038: break;
1039: }
1.322 misha 1040: case OP::OP_LOG_OR:
1.55 paf 1041: {
1.297 paf 1042: ArrayOperation& local_ops=stack.pop().ops(); Value& a=stack.pop().value();
1.263 paf 1043: bool result;
1.297 paf 1044: if(a.as_bool())
1.263 paf 1045: result=true;
1.209 paf 1046: else {
1.297 paf 1047: execute(local_ops);
1048: Value& b=stack.pop().value();
1049: result=b.as_bool();
1.209 paf 1050: }
1.327 misha 1051: Value& value=VBool::get(result);
1.297 paf 1052: stack.push(value);
1.56 paf 1053: break;
1054: }
1.322 misha 1055: case OP::OP_LOG_XOR:
1.56 paf 1056: {
1.297 paf 1057: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1.327 misha 1058: Value& value=VBool::get(a.as_bool() ^ b.as_bool());
1.297 paf 1059: stack.push(value);
1.55 paf 1060: break;
1061: }
1.322 misha 1062: case OP::OP_NUM_LT:
1.55 paf 1063: {
1.297 paf 1064: volatile double b_double=stack.pop().value().as_double();
1065: volatile double a_double=stack.pop().value().as_double();
1.327 misha 1066: Value& value=VBool::get(a_double<b_double);
1.297 paf 1067: stack.push(value);
1.55 paf 1068: break;
1069: }
1.322 misha 1070: case OP::OP_NUM_GT:
1.55 paf 1071: {
1.297 paf 1072: volatile double b_double=stack.pop().value().as_double();
1073: volatile double a_double=stack.pop().value().as_double();
1.327 misha 1074: Value& value=VBool::get(a_double>b_double);
1.297 paf 1075: stack.push(value);
1.55 paf 1076: break;
1077: }
1.322 misha 1078: case OP::OP_NUM_LE:
1.55 paf 1079: {
1.297 paf 1080: volatile double b_double=stack.pop().value().as_double();
1081: volatile double a_double=stack.pop().value().as_double();
1.327 misha 1082: Value& value=VBool::get(a_double<=b_double);
1.297 paf 1083: stack.push(value);
1.55 paf 1084: break;
1085: }
1.322 misha 1086: case OP::OP_NUM_GE:
1.55 paf 1087: {
1.297 paf 1088: volatile double b_double=stack.pop().value().as_double();
1089: volatile double a_double=stack.pop().value().as_double();
1.327 misha 1090: Value& value=VBool::get(a_double>=b_double);
1.297 paf 1091: stack.push(value);
1.55 paf 1092: break;
1093: }
1.322 misha 1094: case OP::OP_NUM_EQ:
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_NE:
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.54 paf 1108: break;
1109: }
1.322 misha 1110: case OP::OP_STR_LT:
1.58 paf 1111: {
1.297 paf 1112: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1.327 misha 1113: Value& value=VBool::get(a.as_string() < b.as_string());
1.297 paf 1114: stack.push(value);
1.58 paf 1115: break;
1116: }
1.322 misha 1117: case OP::OP_STR_GT:
1.58 paf 1118: {
1.297 paf 1119: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1.327 misha 1120: Value& value=VBool::get(a.as_string() > b.as_string());
1.297 paf 1121: stack.push(value);
1.58 paf 1122: break;
1123: }
1.322 misha 1124: case OP::OP_STR_LE:
1.58 paf 1125: {
1.297 paf 1126: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1.327 misha 1127: Value& value=VBool::get(a.as_string() <= b.as_string());
1.297 paf 1128: stack.push(value);
1.58 paf 1129: break;
1130: }
1.322 misha 1131: case OP::OP_STR_GE:
1.54 paf 1132: {
1.297 paf 1133: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1.327 misha 1134: Value& value=VBool::get(a.as_string() >= b.as_string());
1.297 paf 1135: stack.push(value);
1.58 paf 1136: break;
1137: }
1.322 misha 1138: case OP::OP_STR_EQ:
1.58 paf 1139: {
1.297 paf 1140: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1.327 misha 1141: Value& value=VBool::get(a.as_string() == b.as_string());
1.297 paf 1142: stack.push(value);
1.58 paf 1143: break;
1144: }
1.322 misha 1145: case OP::OP_STR_NE:
1.58 paf 1146: {
1.297 paf 1147: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1.327 misha 1148: Value& value=VBool::get(a.as_string() != b.as_string());
1.297 paf 1149: stack.push(value);
1.103 paf 1150: break;
1151: }
1.322 misha 1152: case OP::OP_IS:
1.103 paf 1153: {
1.297 paf 1154: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1.327 misha 1155: Value& value=VBool::get(a.is(b.as_string().cstr()));
1.297 paf 1156: stack.push(value);
1.28 paf 1157: break;
1158: }
1159:
1.11 paf 1160: default:
1.223 paf 1161: throw Exception(0,
1.67 paf 1162: 0,
1.297 paf 1163: "invalid opcode %d", opcode);
1.11 paf 1164: }
1165: }
1.306 paf 1166: } catch(const Exception&) {
1.297 paf 1167: // record it to stack trace
1.301 paf 1168: if(debug_name)
1169: exception_trace.push(Trace(debug_name, debug_origin));
1.297 paf 1170: rethrow;
1171: }
1.1 paf 1172: }
1.17 paf 1173:
1.330 misha 1174: #define SAVE_CONTEXT \
1175: VMethodFrame *saved_method_frame=method_frame; \
1176: Value* saved_rcontext=rcontext; \
1177: WContext *saved_wcontext=wcontext;
1178:
1179: #define RESTORE_CONTEXT \
1180: wcontext=saved_wcontext; \
1181: rcontext=saved_rcontext; \
1182: method_frame=saved_method_frame;
1183:
1.363 moko 1184:
1185: Value& Request::construct(Value &class_value, const Method &method){
1186: VStateless_class& called_class=*class_value.get_class();
1187:
1188: if(method.call_type!=Method::CT_STATIC) {
1189: // this is a constructor call
1190: if(Value* result=called_class.create_new_value(fpool)) {
1.319 misha 1191: // some stateless_class creatable derivates
1.363 moko 1192: return *result;
1193: } else
1.319 misha 1194: throw Exception(PARSER_RUNTIME,
1195: 0, //&frame.name(),
1.363 moko 1196: "is not a constructor, system class '%s' can be constructed only implicitly",
1197: called_class.name().cstr());
1198: } else
1199: throw Exception(PARSER_RUNTIME,
1200: 0, //&frame.name(),
1.319 misha 1201: "method is static and can not be used as constructor");
1.363 moko 1202: }
1.319 misha 1203:
1.363 moko 1204: void Request::op_call(VMethodFrame& frame){
1.319 misha 1205: // see OP_PREPARE_TO_EXPRESSION
1206: frame.set_in_expression(wcontext->get_in_expression());
1207:
1.332 misha 1208: SAVE_CONTEXT
1209:
1.365 ! moko 1210: rcontext=wcontext=method_frame=&frame;
1.319 misha 1211:
1.363 moko 1212: Value& self=frame.self();
1213: const Method& method=frame.method;
1214: Method::Call_type call_type=self.get_class()==&self ? Method::CT_STATIC : Method::CT_DYNAMIC;
1.319 misha 1215:
1.332 misha 1216: if(method.call_type==Method::CT_ANY || method.call_type==call_type) { // allowed call type?
1217: if(method.native_code) { // native code?
1.363 moko 1218: method.check_actual_numbered_params(self, frame.numbered_params());
1.332 misha 1219: method.native_code(*this, *frame.numbered_params()); // execute it
1220: } else // parser code, execute it
1221: recoursion_checked_execute(*method.parser_code);
1222: } else
1223: throw Exception(PARSER_RUNTIME,
1224: 0,
1225: "is not allowed to be called %s",
1226: call_type==Method::CT_STATIC?"statically":"dynamically");
1.334 misha 1227:
1.330 misha 1228: RESTORE_CONTEXT
1.332 misha 1229: }
1230:
1231: void Request::op_call_write(VMethodFrame& frame){
1232: VMethodFrame *saved_method_frame=method_frame;
1233: Value* saved_rcontext=rcontext;
1.319 misha 1234:
1.332 misha 1235: rcontext=&frame;
1236: method_frame=&frame;
1237:
1.363 moko 1238: Value& self=frame.self();
1239: const Method& method=frame.method;
1240: Method::Call_type call_type=self.get_class()==&self ? Method::CT_STATIC : Method::CT_DYNAMIC;
1.332 misha 1241:
1.333 misha 1242: if(method.call_type==Method::CT_ANY || method.call_type==call_type) { // allowed call type?
1243: if(method.native_code) { // native code?
1.363 moko 1244: method.check_actual_numbered_params(self, frame.numbered_params());
1.333 misha 1245: method.native_code(*this, *frame.numbered_params()); // execute it
1246: } else // parser code, execute it
1247: recoursion_checked_execute(*method.parser_code);
1.332 misha 1248: } else
1249: throw Exception(PARSER_RUNTIME,
1250: 0,
1251: "is not allowed to be called %s",
1252: call_type==Method::CT_STATIC?"statically":"dynamically");
1253:
1254: rcontext=saved_rcontext;
1255: method_frame=saved_method_frame;
1.319 misha 1256: }
1257:
1.330 misha 1258: Value& Request::get_element(Value& ncontext, const String& name) {
1.353 misha 1259: if(wcontext->get_somebody_entered_some_class()) // ^class:method
1260: if(VStateless_class *called_class=ncontext.get_class())
1261: if(VStateless_class *read_class=rcontext->get_class())
1.354 misha 1262: if(read_class->derived_from(*called_class)){ // current derived from called
1263: Value *value=called_class->get_element(get_self(), name);
1264: return *(value ? &process_to_value(*value) : VVoid::get());
1265: }
1.330 misha 1266:
1.354 misha 1267: Value* value=ncontext.get_element(name);
1.291 paf 1268:
1.330 misha 1269: return *(value ? &process_to_value(*value) : VVoid::get());
1.309 paf 1270: }
1271:
1.329 misha 1272: void Request::put_element(Value& ncontext, const String& name, Value* value) {
1.309 paf 1273: // put_element can return property-setting-junction
1.354 misha 1274: if(const VJunction* vjunction=ncontext.put_element(name, value, false))
1.312 paf 1275: if(vjunction!=PUT_ELEMENT_REPLACED_ELEMENT) {
1.363 moko 1276: const Junction& junction = vjunction->junction();
1.364 moko 1277: VConstructorFrame frame(*junction.method, method_frame /*caller*/, junction.self);
1.329 misha 1278:
1.352 misha 1279: size_t param_count=frame.method_params_count();
1.364 moko 1280:
1281: if(junction.auto_name){
1282: // default setter
1283: if(param_count!=2)
1284: throw Exception(PARSER_RUNTIME,
1285: 0,
1286: "default setter method must have TWO parameters (has %d parameters)", param_count);
1287:
1288: Value* params[2] = { new VString(*junction.auto_name), value };
1289: frame.store_params(params, 2);
1290:
1291: Temp_disable_default_setter temp(junction.self);
1.365 ! moko 1292: execute_method(frame);
1.364 moko 1293: } else {
1294: // setter
1295: if(param_count!=1)
1296: throw Exception(PARSER_RUNTIME,
1297: 0,
1298: "setter method must have ONE parameter (has %d parameters)", param_count);
1299:
1300: frame.store_params(&value, 1);
1.365 ! moko 1301: execute_method(frame);
1.364 moko 1302: }
1303: }
1304: }
1305:
1306: StringOrValue Request::process_getter(Junction& junction) {
1307: VMethodFrame frame(*junction.method, method_frame/*caller*/, junction.self);
1308: size_t param_count=frame.method_params_count();
1309:
1310: if(junction.auto_name){
1311: // default getter
1312: Value *param;
1313:
1314: if(param_count){
1315: if(param_count>1)
1.316 misha 1316: throw Exception(PARSER_RUNTIME,
1.309 paf 1317: 0,
1.364 moko 1318: "default getter method can't have more then 1 parameter (has %d parameters)", param_count);
1319: param=new VString(*junction.auto_name);
1320: frame.store_params(¶m, 1);
1321: } // no need for else frame.empty_params()
1322:
1323: Temp_disable_default_getter temp(junction.self);
1.365 ! moko 1324: execute_method(frame);
1.364 moko 1325: } else {
1326: // getter
1327: if(param_count!=0)
1328: throw Exception(PARSER_RUNTIME,
1329: 0,
1330: "getter method must have no parameters (has %d parameters)", param_count);
1331:
1332: // no need for frame.empty_params()
1.365 ! moko 1333: execute_method(frame);
1.364 moko 1334: }
1.309 paf 1335:
1.364 moko 1336: return frame.result();
1.34 paf 1337: }
1.70 paf 1338:
1.162 parser 1339: /** @param intercept_string
1.116 paf 1340: - true:
1341: they want result=string value,
1342: possible object result goes to wcontext
1343: - false:
1344: they want any result[string|object]
1345: nothing goes to wcontext.
1.125 paf 1346: used in @c (expression) params evaluation
1.224 paf 1347:
1.334 misha 1348: using the fact it's either string_ or value_ result requested to speed up checkes
1.116 paf 1349: */
1.364 moko 1350:
1.229 paf 1351: StringOrValue Request::process(Value& input_value, bool intercept_string) {
1.297 paf 1352: Junction* junction=input_value.get_junction();
1.307 paf 1353: if(junction) {
1354: if(junction->is_getter) { // is it a getter-junction?
1.364 moko 1355: return process_getter(*junction);
1.307 paf 1356: }
1357:
1358: if(junction->code) { // is it a code-junction?
1359: // process it
1360: StringOrValue result;
1.336 misha 1361:
1362: DEBUG_PRINT_STR("ja->\n")
1.238 paf 1363:
1.307 paf 1364: if(!junction->method_frame)
1.316 misha 1365: throw Exception(PARSER_RUNTIME,
1.352 misha 1366: 0,
1367: "junction used outside of context");
1.240 paf 1368:
1.330 misha 1369: SAVE_CONTEXT
1.307 paf 1370:
1371: method_frame=junction->method_frame;
1372: rcontext=junction->rcontext;
1373:
1374: // for expression method params
1375: // wcontext is set 0
1376: // using the fact in decision "which wwrapper to use"
1377: bool using_code_frame=intercept_string && junction->wcontext;
1378: if(using_code_frame) {
1.318 misha 1379: // almost plain wwrapper about junction wcontext
1380:
1381: VCodeFrame local(*junction->wcontext);
1.307 paf 1382: wcontext=&local;
1383:
1384: // execute it
1385: recoursion_checked_execute(*junction->code);
1386:
1387: // CodeFrame soul:
1.318 misha 1388: result=wcontext->result();
1.307 paf 1389: } else {
1390: // plain wwrapper
1.351 misha 1391: WWrapper local(wcontext);
1.307 paf 1392: wcontext=&local;
1393:
1394: // execute it
1395: recoursion_checked_execute(*junction->code);
1396:
1397: result=wcontext->result();
1398: }
1399:
1.330 misha 1400: RESTORE_CONTEXT
1.207 paf 1401:
1.336 misha 1402: DEBUG_PRINT_STR("<-ja returned")
1403:
1.307 paf 1404: return result;
1405: }
1406:
1407: // it is then method-junction, do not explode it
1408: // just return it as we do for usual objects
1409: }
1410:
1411: return input_value;
1.85 paf 1412: }
1413:
1.332 misha 1414: void Request::process_write(Value& input_value) {
1415: Junction* junction=input_value.get_junction();
1416: if(junction) {
1417: if(junction->is_getter) { // is it a getter-junction?
1.364 moko 1418: write_pass_lang(process_getter(*junction));
1.332 misha 1419: return;
1420: }
1421:
1422: if(junction->code) { // is it a code-junction?
1423: // process it
1.336 misha 1424:
1425: DEBUG_PRINT_STR("ja->\n")
1426:
1.332 misha 1427: if(!junction->method_frame)
1428: throw Exception(PARSER_RUNTIME,
1429: 0,
1430: "junction used outside of context");
1431:
1432: SAVE_CONTEXT
1433:
1434: method_frame=junction->method_frame;
1435: rcontext=junction->rcontext;
1436:
1437: // for expression method params
1438: // wcontext is set 0
1439: // using the fact in decision "which wwrapper to use"
1440: #ifdef OPTIMIZE_CALL
1441: if(wcontext==junction->wcontext){
1442: // no wrappers for wcontext
1443: recoursion_checked_execute(*junction->code);
1444: RESTORE_CONTEXT
1445:
1446: } else
1447: #endif
1448: if(junction->wcontext) {
1449: // almost plain wwrapper about junction wcontext
1450: VCodeFrame local(*junction->wcontext);
1451: wcontext=&local;
1452:
1453: // execute it
1454: recoursion_checked_execute(*junction->code);
1455: RESTORE_CONTEXT
1456: write_pass_lang(local.result());
1457: } else {
1458: // plain wwrapper
1.351 misha 1459: WWrapper local(wcontext);
1.332 misha 1460: wcontext=&local;
1461:
1462: // execute it
1463: recoursion_checked_execute(*junction->code);
1464: RESTORE_CONTEXT
1465: write_pass_lang(local.result());
1466: }
1.336 misha 1467:
1468: DEBUG_PRINT_STR("<-ja returned")
1469:
1.332 misha 1470: return;
1471: }
1472:
1473: // it is then method-junction, do not explode it
1474: // just return it as we do for usual objects
1475: }
1476:
1477: write_pass_lang(input_value);
1478: }
1479:
1.365 ! moko 1480: void Request::execute_method(VMethodFrame& aframe) {
1.330 misha 1481: SAVE_CONTEXT
1.99 paf 1482:
1483: // initialize contexts
1.365 ! moko 1484: rcontext=wcontext=method_frame=&aframe;
1.99 paf 1485:
1486: // execute!
1.365 ! moko 1487: recoursion_checked_execute(*aframe.method.parser_code);
1.208 paf 1488:
1.330 misha 1489: RESTORE_CONTEXT
1.208 paf 1490: }
1491:
1.297 paf 1492: const String* Request::execute_method(Value& aself,
1.329 misha 1493: const Method& method, Value* optional_param,
1494: bool do_return_string) {
1.317 misha 1495:
1.363 moko 1496: VMethodFrame local_frame(method, method_frame/*caller*/, aself);
1.365 ! moko 1497:
1.329 misha 1498: if(optional_param && local_frame.method_params_count()>0) {
1499: local_frame.store_params(&optional_param, 1);
1.330 misha 1500: } else {
1501: local_frame.empty_params();
1.242 paf 1502: }
1.246 paf 1503:
1504: // prevent non-string writes for better error reporting
1.297 paf 1505: if(do_return_string)
1.357 misha 1506: local_frame.write(local_frame);
1.208 paf 1507:
1.365 ! moko 1508: execute_method(local_frame);
1.99 paf 1509:
1.365 ! moko 1510: return do_return_string ? local_frame.get_string() : 0;
1.242 paf 1511: }
1512:
1.297 paf 1513: Request::Execute_nonvirtual_method_result
1514: Request::execute_nonvirtual_method(VStateless_class& aclass,
1.330 misha 1515: const String& method_name,
1516: VString* optional_param,
1517: bool do_return_string) {
1.297 paf 1518: Execute_nonvirtual_method_result result;
1519: result.method=aclass.get_method(method_name);
1520: if(result.method)
1.317 misha 1521: result.string=execute_method(aclass, *result.method, optional_param, do_return_string);
1.297 paf 1522: return result;
1.99 paf 1523: }
1524:
1.297 paf 1525: const String* Request::execute_virtual_method(Value& aself,
1.330 misha 1526: const String& method_name) {
1.354 misha 1527: if(Value* value=aself.get_element(method_name))
1.297 paf 1528: if(Junction* junction=value->get_junction())
1529: if(const Method *method=junction->method)
1530: return execute_method(aself, *method, 0/*no params*/, true);
1.188 parser 1531:
1.176 parser 1532: return 0;
1533: }
1.362 misha 1534:
1535: const String* Request::get_method_filename(const Method* method){
1536: if(ArrayOperation* code=method->parser_code)
1537: if(code){
1538: Operation::Origin origin={0, 0, 0};
1539: Array_iterator<Operation> i(*code);
1540: while( i.has_next() ){
1541: switch( i.next().code ){
1542: case OP::OP_CURLY_CODE__STORE_PARAM:
1543: case OP::OP_EXPR_CODE__STORE_PARAM:
1544: case OP::OP_CURLY_CODE__CONSTRUCT:
1545: case OP::OP_NESTED_CODE:
1546: case OP::OP_OBJECT_POOL:
1547: case OP::OP_STRING_POOL:
1548: case OP::OP_CALL:
1549: case OP::OP_CALL__WRITE:
1550: {
1551: i.next(); // skip local ops
1552: i.next(); // skip next opcode
1553: // continue execution
1554: }
1555: case OP::OP_CONSTRUCT_OBJECT:
1556: case OP::OP_CONSTRUCT_OBJECT__WRITE:
1557: case OP::OP_VALUE:
1558: case OP::OP_STRING__WRITE:
1559: case OP::OP_VALUE__GET_CLASS:
1560: #ifdef OPTIMIZE_BYTECODE_GET_OBJECT_ELEMENT
1561: case OP::OP_GET_OBJECT_ELEMENT:
1562: case OP::OP_GET_OBJECT_ELEMENT__WRITE:
1563: #endif
1564: #ifdef OPTIMIZE_BYTECODE_GET_OBJECT_VAR_ELEMENT
1565: case OP::OP_GET_OBJECT_VAR_ELEMENT:
1566: case OP::OP_GET_OBJECT_VAR_ELEMENT__WRITE:
1567: #endif
1568: #ifdef OPTIMIZE_BYTECODE_GET_ELEMENT
1569: case OP::OP_VALUE__GET_ELEMENT:
1570: case OP::OP_VALUE__GET_ELEMENT__WRITE:
1571: case OP::OP_VALUE__GET_ELEMENT_OR_OPERATOR:
1572: case OP::OP_WITH_ROOT__VALUE__GET_ELEMENT:
1573: #endif
1574: #ifdef OPTIMIZE_BYTECODE_GET_SELF_ELEMENT
1575: case OP::OP_WITH_SELF__VALUE__GET_ELEMENT:
1576: case OP::OP_WITH_SELF__VALUE__GET_ELEMENT__WRITE:
1577: #endif
1578: #ifdef OPTIMIZE_BYTECODE_CONSTRUCT
1579: case OP::OP_WITH_ROOT__VALUE__CONSTRUCT_EXPR:
1580: case OP::OP_WITH_ROOT__VALUE__CONSTRUCT_VALUE:
1581: case OP::OP_WITH_WRITE__VALUE__CONSTRUCT_EXPR:
1582: case OP::OP_WITH_WRITE__VALUE__CONSTRUCT_VALUE:
1583: case OP::OP_WITH_SELF__VALUE__CONSTRUCT_EXPR:
1584: case OP::OP_WITH_SELF__VALUE__CONSTRUCT_VALUE:
1585: #endif
1586: {
1587: origin=i.next().origin;
1588: break;
1589: }
1590: }
1591: if(origin.file_no)
1592: return get_used_filename(origin.file_no);
1593: }
1594: }
1595: return 0;
1596: }
1597: