|
|
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.363 ! moko 8: static const char * const IDENT_EXECUTE_C="$Date: 2010-05-25 03:58:25 $";
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.355 misha 842:
1.350 misha 843: if(opcode==OP::OP_CONSTRUCT_OBJECT)
1.363 ! moko 844: stack.push(object);
1.350 misha 845: else
1.363 ! moko 846: write_pass_lang(object);
1.348 misha 847:
848: DEBUG_PRINT_STR("<-returned")
849: break;
850: }
851:
1.55 paf 852: // expression ops: unary
1.322 misha 853: case OP::OP_NEG:
1.55 paf 854: {
1.297 paf 855: Value& a=stack.pop().value();
856: Value& value=*new VDouble(-a.as_double());
857: stack.push(value);
1.55 paf 858: break;
859: }
1.322 misha 860: case OP::OP_INV:
1.55 paf 861: {
1.297 paf 862: Value& a=stack.pop().value();
863: Value& value=*new VDouble(~a.as_int());
864: stack.push(value);
1.55 paf 865: break;
866: }
1.322 misha 867: case OP::OP_NOT:
1.55 paf 868: {
1.297 paf 869: Value& a=stack.pop().value();
1.327 misha 870: Value& value=VBool::get(!a.as_bool());
1.297 paf 871: stack.push(value);
1.55 paf 872: break;
873: }
1.322 misha 874: case OP::OP_DEF:
1.62 paf 875: {
1.297 paf 876: Value& a=stack.pop().value();
1.327 misha 877: Value& value=VBool::get(a.is_defined());
1.297 paf 878: stack.push(value);
1.62 paf 879: break;
880: }
1.322 misha 881: case OP::OP_IN:
1.62 paf 882: {
1.297 paf 883: Value& a=stack.pop().value();
884: const String& path=a.as_string();
1.327 misha 885: Value& value=VBool::get(request_info.uri && *request_info.uri && path.this_starts(request_info.uri));
1.297 paf 886: stack.push(value);
1.62 paf 887: break;
888: }
1.322 misha 889: case OP::OP_FEXISTS:
1.62 paf 890: {
1.297 paf 891: Value& a=stack.pop().value();
1.327 misha 892: Value& value=VBool::get(file_exist(absolute(a.as_string())));
1.297 paf 893: stack.push(value);
1.148 paf 894: break;
895: }
1.322 misha 896: case OP::OP_DEXISTS:
1.148 paf 897: {
1.297 paf 898: Value& a=stack.pop().value();
1.327 misha 899: Value& value=VBool::get(dir_exists(absolute(a.as_string())));
1.297 paf 900: stack.push(value);
1.62 paf 901: break;
902: }
1.55 paf 903:
904: // expression ops: binary
1.322 misha 905: case OP::OP_SUB:
1.53 paf 906: {
1.297 paf 907: Value& b=stack.pop().value(); Value& a=stack.pop().value();
908: Value& value=*new VDouble(a.as_double() - b.as_double());
909: stack.push(value);
1.53 paf 910: break;
911: }
1.322 misha 912: case OP::OP_ADD:
1.53 paf 913: {
1.297 paf 914: Value& b=stack.pop().value(); Value& a=stack.pop().value();
915: Value& value=*new VDouble(a.as_double() + b.as_double());
916: stack.push(value);
1.53 paf 917: break;
918: }
1.322 misha 919: case OP::OP_MUL:
1.49 paf 920: {
1.297 paf 921: Value& b=stack.pop().value(); Value& a=stack.pop().value();
922: Value& value=*new VDouble(a.as_double() * b.as_double());
923: stack.push(value);
1.53 paf 924: break;
925: }
1.322 misha 926: case OP::OP_DIV:
1.53 paf 927: {
1.297 paf 928: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1.170 parser 929:
1.297 paf 930: double a_double=a.as_double();
931: double b_double=b.as_double();
1.170 parser 932:
1.171 parser 933: if(b_double == 0) {
1.297 paf 934: //const String* problem_source=b.as_string();
1.223 paf 935: throw Exception("number.zerodivision",
1.297 paf 936: 0, //problem_source,
1.170 parser 937: "Division by zero");
1.171 parser 938: }
1.170 parser 939:
1.297 paf 940: Value& value=*new VDouble(a_double / b_double);
941: stack.push(value);
1.54 paf 942: break;
943: }
1.322 misha 944: case OP::OP_MOD:
1.55 paf 945: {
1.297 paf 946: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1.170 parser 947:
1.297 paf 948: double a_double=a.as_double();
949: double b_double=b.as_double();
1.170 parser 950:
1.173 parser 951: if(b_double == 0) {
1.297 paf 952: //const String* problem_source=b.as_string();
1.223 paf 953: throw Exception("number.zerodivision",
1.297 paf 954: 0, //problem_source,
1.170 parser 955: "Modulus by zero");
1.171 parser 956: }
1.170 parser 957:
1.297 paf 958: Value& value=*new VDouble(fmod(a_double, b_double));
959: stack.push(value);
1.201 paf 960: break;
961: }
1.322 misha 962: case OP::OP_INTDIV:
1.201 paf 963: {
1.297 paf 964: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1.201 paf 965:
1.297 paf 966: int a_int=a.as_int();
967: int b_int=b.as_int();
1.201 paf 968:
969: if(b_int == 0) {
1.297 paf 970: //const String* problem_source=b.as_string();
1.223 paf 971: throw Exception("number.zerodivision",
1.297 paf 972: 0, //problem_source,
1.201 paf 973: "Division by zero");
974: }
975:
1.297 paf 976: Value& value=*new VInt(a_int / b_int);
977: stack.push(value);
1.55 paf 978: break;
979: }
1.322 misha 980: case OP::OP_BIN_SL:
1.274 paf 981: {
1.297 paf 982: Value& b=stack.pop().value(); Value& a=stack.pop().value();
983: Value& value=*new VInt(
984: a.as_int() <<
985: b.as_int());
986: stack.push(value);
1.274 paf 987: break;
988: }
1.322 misha 989: case OP::OP_BIN_SR:
1.274 paf 990: {
1.297 paf 991: Value& b=stack.pop().value(); Value& a=stack.pop().value();
992: Value& value=*new VInt(
993: a.as_int() >>
994: b.as_int());
995: stack.push(value);
1.274 paf 996: break;
997: }
1.322 misha 998: case OP::OP_BIN_AND:
1.54 paf 999: {
1.297 paf 1000: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1001: Value& value=*new VInt(
1002: a.as_int() &
1003: b.as_int());
1004: stack.push(value);
1.54 paf 1005: break;
1006: }
1.322 misha 1007: case OP::OP_BIN_OR:
1.54 paf 1008: {
1.297 paf 1009: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1010: Value& value=*new VInt(
1011: a.as_int() |
1012: b.as_int());
1013: stack.push(value);
1.55 paf 1014: break;
1015: }
1.322 misha 1016: case OP::OP_BIN_XOR:
1.56 paf 1017: {
1.297 paf 1018: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1019: Value& value=*new VInt(
1020: a.as_int() ^
1021: b.as_int());
1022: stack.push(value);
1.56 paf 1023: break;
1024: }
1.322 misha 1025: case OP::OP_LOG_AND:
1.55 paf 1026: {
1.297 paf 1027: ArrayOperation& local_ops=stack.pop().ops(); Value& a=stack.pop().value();
1.263 paf 1028: bool result;
1.297 paf 1029: if(a.as_bool()) {
1030: execute(local_ops);
1031: Value& b=stack.pop().value();
1032: result=b.as_bool();
1.209 paf 1033: } else
1.263 paf 1034: result=false;
1.327 misha 1035: Value& value=VBool::get(result);
1.297 paf 1036: stack.push(value);
1.55 paf 1037: break;
1038: }
1.322 misha 1039: case OP::OP_LOG_OR:
1.55 paf 1040: {
1.297 paf 1041: ArrayOperation& local_ops=stack.pop().ops(); Value& a=stack.pop().value();
1.263 paf 1042: bool result;
1.297 paf 1043: if(a.as_bool())
1.263 paf 1044: result=true;
1.209 paf 1045: else {
1.297 paf 1046: execute(local_ops);
1047: Value& b=stack.pop().value();
1048: result=b.as_bool();
1.209 paf 1049: }
1.327 misha 1050: Value& value=VBool::get(result);
1.297 paf 1051: stack.push(value);
1.56 paf 1052: break;
1053: }
1.322 misha 1054: case OP::OP_LOG_XOR:
1.56 paf 1055: {
1.297 paf 1056: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1.327 misha 1057: Value& value=VBool::get(a.as_bool() ^ b.as_bool());
1.297 paf 1058: stack.push(value);
1.55 paf 1059: break;
1060: }
1.322 misha 1061: case OP::OP_NUM_LT:
1.55 paf 1062: {
1.297 paf 1063: volatile double b_double=stack.pop().value().as_double();
1064: volatile double a_double=stack.pop().value().as_double();
1.327 misha 1065: Value& value=VBool::get(a_double<b_double);
1.297 paf 1066: stack.push(value);
1.55 paf 1067: break;
1068: }
1.322 misha 1069: case OP::OP_NUM_GT:
1.55 paf 1070: {
1.297 paf 1071: volatile double b_double=stack.pop().value().as_double();
1072: volatile double a_double=stack.pop().value().as_double();
1.327 misha 1073: Value& value=VBool::get(a_double>b_double);
1.297 paf 1074: stack.push(value);
1.55 paf 1075: break;
1076: }
1.322 misha 1077: case OP::OP_NUM_LE:
1.55 paf 1078: {
1.297 paf 1079: volatile double b_double=stack.pop().value().as_double();
1080: volatile double a_double=stack.pop().value().as_double();
1.327 misha 1081: Value& value=VBool::get(a_double<=b_double);
1.297 paf 1082: stack.push(value);
1.55 paf 1083: break;
1084: }
1.322 misha 1085: case OP::OP_NUM_GE:
1.55 paf 1086: {
1.297 paf 1087: volatile double b_double=stack.pop().value().as_double();
1088: volatile double a_double=stack.pop().value().as_double();
1.327 misha 1089: Value& value=VBool::get(a_double>=b_double);
1.297 paf 1090: stack.push(value);
1.55 paf 1091: break;
1092: }
1.322 misha 1093: case OP::OP_NUM_EQ:
1.55 paf 1094: {
1.297 paf 1095: volatile double b_double=stack.pop().value().as_double();
1096: volatile double a_double=stack.pop().value().as_double();
1.327 misha 1097: Value& value=VBool::get(a_double==b_double);
1.297 paf 1098: stack.push(value);
1.55 paf 1099: break;
1100: }
1.322 misha 1101: case OP::OP_NUM_NE:
1.55 paf 1102: {
1.297 paf 1103: volatile double b_double=stack.pop().value().as_double();
1104: volatile double a_double=stack.pop().value().as_double();
1.327 misha 1105: Value& value=VBool::get(a_double!=b_double);
1.297 paf 1106: stack.push(value);
1.54 paf 1107: break;
1108: }
1.322 misha 1109: case OP::OP_STR_LT:
1.58 paf 1110: {
1.297 paf 1111: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1.327 misha 1112: Value& value=VBool::get(a.as_string() < b.as_string());
1.297 paf 1113: stack.push(value);
1.58 paf 1114: break;
1115: }
1.322 misha 1116: case OP::OP_STR_GT:
1.58 paf 1117: {
1.297 paf 1118: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1.327 misha 1119: Value& value=VBool::get(a.as_string() > b.as_string());
1.297 paf 1120: stack.push(value);
1.58 paf 1121: break;
1122: }
1.322 misha 1123: case OP::OP_STR_LE:
1.58 paf 1124: {
1.297 paf 1125: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1.327 misha 1126: Value& value=VBool::get(a.as_string() <= b.as_string());
1.297 paf 1127: stack.push(value);
1.58 paf 1128: break;
1129: }
1.322 misha 1130: case OP::OP_STR_GE:
1.54 paf 1131: {
1.297 paf 1132: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1.327 misha 1133: Value& value=VBool::get(a.as_string() >= b.as_string());
1.297 paf 1134: stack.push(value);
1.58 paf 1135: break;
1136: }
1.322 misha 1137: case OP::OP_STR_EQ:
1.58 paf 1138: {
1.297 paf 1139: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1.327 misha 1140: Value& value=VBool::get(a.as_string() == b.as_string());
1.297 paf 1141: stack.push(value);
1.58 paf 1142: break;
1143: }
1.322 misha 1144: case OP::OP_STR_NE:
1.58 paf 1145: {
1.297 paf 1146: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1.327 misha 1147: Value& value=VBool::get(a.as_string() != b.as_string());
1.297 paf 1148: stack.push(value);
1.103 paf 1149: break;
1150: }
1.322 misha 1151: case OP::OP_IS:
1.103 paf 1152: {
1.297 paf 1153: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1.327 misha 1154: Value& value=VBool::get(a.is(b.as_string().cstr()));
1.297 paf 1155: stack.push(value);
1.28 paf 1156: break;
1157: }
1158:
1.11 paf 1159: default:
1.223 paf 1160: throw Exception(0,
1.67 paf 1161: 0,
1.297 paf 1162: "invalid opcode %d", opcode);
1.11 paf 1163: }
1164: }
1.306 paf 1165: } catch(const Exception&) {
1.297 paf 1166: // record it to stack trace
1.301 paf 1167: if(debug_name)
1168: exception_trace.push(Trace(debug_name, debug_origin));
1.297 paf 1169: rethrow;
1170: }
1.1 paf 1171: }
1.17 paf 1172:
1.330 misha 1173: #define SAVE_CONTEXT \
1174: VMethodFrame *saved_method_frame=method_frame; \
1175: Value* saved_rcontext=rcontext; \
1176: WContext *saved_wcontext=wcontext;
1177:
1178: #define RESTORE_CONTEXT \
1179: wcontext=saved_wcontext; \
1180: rcontext=saved_rcontext; \
1181: method_frame=saved_method_frame;
1182:
1.363 ! moko 1183:
! 1184: Value& Request::construct(Value &class_value, const Method &method){
! 1185: VStateless_class& called_class=*class_value.get_class();
! 1186:
! 1187: if(method.call_type!=Method::CT_STATIC) {
! 1188: // this is a constructor call
! 1189: if(Value* result=called_class.create_new_value(fpool)) {
1.319 misha 1190: // some stateless_class creatable derivates
1.363 ! moko 1191: return *result;
! 1192: } else
1.319 misha 1193: throw Exception(PARSER_RUNTIME,
1194: 0, //&frame.name(),
1.363 ! moko 1195: "is not a constructor, system class '%s' can be constructed only implicitly",
! 1196: called_class.name().cstr());
! 1197: } else
! 1198: throw Exception(PARSER_RUNTIME,
! 1199: 0, //&frame.name(),
1.319 misha 1200: "method is static and can not be used as constructor");
1.363 ! moko 1201: }
1.319 misha 1202:
1.363 ! moko 1203: void Request::op_call(VMethodFrame& frame){
1.319 misha 1204: // see OP_PREPARE_TO_EXPRESSION
1205: frame.set_in_expression(wcontext->get_in_expression());
1206:
1.332 misha 1207: SAVE_CONTEXT
1208:
1.319 misha 1209: rcontext=wcontext=&frame;
1.332 misha 1210: 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.309 paf 1276: // process it
1.363 ! moko 1277: const Junction& junction = vjunction->junction();
! 1278: VMethodFrame frame(*junction.method, method_frame /*caller*/, junction.self);
1.329 misha 1279:
1.352 misha 1280: size_t param_count=frame.method_params_count();
1.309 paf 1281: if(param_count!=1)
1.316 misha 1282: throw Exception(PARSER_RUNTIME,
1.309 paf 1283: 0,
1284: "setter method must have ONE parameter (has %d parameters)", param_count);
1285:
1.329 misha 1286: frame.store_params(&value, 1);
1.309 paf 1287:
1.330 misha 1288: SAVE_CONTEXT
1.309 paf 1289:
1290: rcontext=wcontext=&frame;
1291: method_frame=&frame;
1292:
1.311 paf 1293: // prevent non-string writes for better error reporting [setters are not expected to return anything]
1.309 paf 1294: wcontext->write(*method_frame);
1295:
1.363 ! moko 1296: recoursion_checked_execute(*frame.method.parser_code); // parser code, execute it
1.309 paf 1297: // we don't need it StringOrValue result=wcontext->result();
1298:
1.330 misha 1299: RESTORE_CONTEXT
1.309 paf 1300: }
1.34 paf 1301: }
1.70 paf 1302:
1.162 parser 1303: /** @param intercept_string
1.116 paf 1304: - true:
1305: they want result=string value,
1306: possible object result goes to wcontext
1307: - false:
1308: they want any result[string|object]
1309: nothing goes to wcontext.
1.125 paf 1310: used in @c (expression) params evaluation
1.224 paf 1311:
1.334 misha 1312: using the fact it's either string_ or value_ result requested to speed up checkes
1.116 paf 1313: */
1.229 paf 1314: StringOrValue Request::process(Value& input_value, bool intercept_string) {
1.297 paf 1315: Junction* junction=input_value.get_junction();
1.307 paf 1316: if(junction) {
1317: if(junction->is_getter) { // is it a getter-junction?
1.363 ! moko 1318: VMethodFrame frame(*junction->method, method_frame/*caller*/, junction->self);
1.356 misha 1319: Value *param;
1.329 misha 1320:
1.352 misha 1321: if(size_t param_count=frame.method_params_count()){
1.331 misha 1322: if(junction->auto_name){ // default getter
1323: if(param_count==1){
1.356 misha 1324: param=new VString(*junction->auto_name);
1.331 misha 1325: frame.store_params(¶m, 1);
1326: } else
1327: throw Exception(PARSER_RUNTIME,
1328: 0,
1329: "default getter method can't have more then 1 parameter (has %d parameters)", param_count);
1330: } else
1.320 misha 1331: throw Exception(PARSER_RUNTIME,
1332: 0,
1.331 misha 1333: "getter method must have no parameters (has %d parameters)", param_count);
1334: } // no need for else frame.empty_params()
1.320 misha 1335:
1.330 misha 1336: SAVE_CONTEXT
1.307 paf 1337:
1338: rcontext=wcontext=&frame;
1339: method_frame=&frame;
1340:
1.363 ! moko 1341: recoursion_checked_execute(*frame.method.parser_code); // parser code, execute it
1.307 paf 1342:
1.330 misha 1343: RESTORE_CONTEXT
1.307 paf 1344:
1.356 misha 1345: return frame.result();
1.307 paf 1346: }
1347:
1348: if(junction->code) { // is it a code-junction?
1349: // process it
1350: StringOrValue result;
1.336 misha 1351:
1352: DEBUG_PRINT_STR("ja->\n")
1.238 paf 1353:
1.307 paf 1354: if(!junction->method_frame)
1.316 misha 1355: throw Exception(PARSER_RUNTIME,
1.352 misha 1356: 0,
1357: "junction used outside of context");
1.240 paf 1358:
1.330 misha 1359: SAVE_CONTEXT
1.307 paf 1360:
1361: method_frame=junction->method_frame;
1362: rcontext=junction->rcontext;
1363:
1364: // for expression method params
1365: // wcontext is set 0
1366: // using the fact in decision "which wwrapper to use"
1367: bool using_code_frame=intercept_string && junction->wcontext;
1368: if(using_code_frame) {
1.318 misha 1369: // almost plain wwrapper about junction wcontext
1370:
1371: VCodeFrame local(*junction->wcontext);
1.307 paf 1372: wcontext=&local;
1373:
1374: // execute it
1375: recoursion_checked_execute(*junction->code);
1376:
1377: // CodeFrame soul:
1.318 misha 1378: result=wcontext->result();
1.307 paf 1379: } else {
1380: // plain wwrapper
1.351 misha 1381: WWrapper local(wcontext);
1.307 paf 1382: wcontext=&local;
1383:
1384: // execute it
1385: recoursion_checked_execute(*junction->code);
1386:
1387: result=wcontext->result();
1388: }
1389:
1.330 misha 1390: RESTORE_CONTEXT
1.207 paf 1391:
1.336 misha 1392: DEBUG_PRINT_STR("<-ja returned")
1393:
1.307 paf 1394: return result;
1395: }
1396:
1397: // it is then method-junction, do not explode it
1398: // just return it as we do for usual objects
1399: }
1400:
1401: return input_value;
1.85 paf 1402: }
1403:
1.332 misha 1404: void Request::process_write(Value& input_value) {
1405: Junction* junction=input_value.get_junction();
1406: if(junction) {
1407: if(junction->is_getter) { // is it a getter-junction?
1.363 ! moko 1408: VMethodFrame frame(*junction->method, method_frame /*caller*/, junction->self);
1.356 misha 1409: Value *param;
1.332 misha 1410:
1.352 misha 1411: if(size_t param_count=frame.method_params_count()){
1.332 misha 1412: if(junction->auto_name){ // default getter
1413: if(param_count==1){
1.356 misha 1414: param=new VString(*junction->auto_name);
1.332 misha 1415: frame.store_params(¶m, 1);
1416: } else
1417: throw Exception(PARSER_RUNTIME,
1418: 0,
1419: "default getter method can't have more then 1 parameter (has %d parameters)", param_count);
1420: } else
1421: throw Exception(PARSER_RUNTIME,
1422: 0,
1423: "getter method must have no parameters (has %d parameters)", param_count);
1424: } // no need for else frame.empty_params()
1425:
1426: SAVE_CONTEXT
1427:
1428: rcontext=wcontext=&frame;
1429: method_frame=&frame;
1430:
1.363 ! moko 1431: recoursion_checked_execute(*frame.method.parser_code); // parser code, execute it
1.332 misha 1432:
1433: RESTORE_CONTEXT
1434:
1435: write_pass_lang(frame.result());
1436: return;
1437: }
1438:
1439: if(junction->code) { // is it a code-junction?
1440: // process it
1.336 misha 1441:
1442: DEBUG_PRINT_STR("ja->\n")
1443:
1.332 misha 1444: if(!junction->method_frame)
1445: throw Exception(PARSER_RUNTIME,
1446: 0,
1447: "junction used outside of context");
1448:
1449: SAVE_CONTEXT
1450:
1451: method_frame=junction->method_frame;
1452: rcontext=junction->rcontext;
1453:
1454: // for expression method params
1455: // wcontext is set 0
1456: // using the fact in decision "which wwrapper to use"
1457: #ifdef OPTIMIZE_CALL
1458: if(wcontext==junction->wcontext){
1459: // no wrappers for wcontext
1460: recoursion_checked_execute(*junction->code);
1461: RESTORE_CONTEXT
1462:
1463: } else
1464: #endif
1465: if(junction->wcontext) {
1466: // almost plain wwrapper about junction wcontext
1467: VCodeFrame local(*junction->wcontext);
1468: wcontext=&local;
1469:
1470: // execute it
1471: recoursion_checked_execute(*junction->code);
1472: RESTORE_CONTEXT
1473: write_pass_lang(local.result());
1474: } else {
1475: // plain wwrapper
1.351 misha 1476: WWrapper local(wcontext);
1.332 misha 1477: wcontext=&local;
1478:
1479: // execute it
1480: recoursion_checked_execute(*junction->code);
1481: RESTORE_CONTEXT
1482: write_pass_lang(local.result());
1483: }
1.336 misha 1484:
1485: DEBUG_PRINT_STR("<-ja returned")
1486:
1.332 misha 1487: return;
1488: }
1489:
1490: // it is then method-junction, do not explode it
1491: // just return it as we do for usual objects
1492: }
1493:
1494: write_pass_lang(input_value);
1495: }
1496:
1.298 paf 1497: StringOrValue Request::execute_method(VMethodFrame& amethod_frame, const Method& method) {
1.330 misha 1498: SAVE_CONTEXT
1.99 paf 1499:
1500: // initialize contexts
1.286 paf 1501: rcontext=wcontext=method_frame=&amethod_frame;
1.99 paf 1502:
1503: // execute!
1504: execute(*method.parser_code);
1505:
1506: // result
1.298 paf 1507: StringOrValue result=wcontext->result();
1.208 paf 1508:
1.330 misha 1509: RESTORE_CONTEXT
1.242 paf 1510:
1511: // return
1512: return result;
1.208 paf 1513: }
1514:
1.297 paf 1515: const String* Request::execute_method(Value& aself,
1.329 misha 1516: const Method& method, Value* optional_param,
1517: bool do_return_string) {
1.317 misha 1518:
1.330 misha 1519: SAVE_CONTEXT
1.208 paf 1520:
1.363 ! moko 1521: VMethodFrame local_frame(method, method_frame/*caller*/, aself);
1.329 misha 1522: if(optional_param && local_frame.method_params_count()>0) {
1523: local_frame.store_params(&optional_param, 1);
1.330 misha 1524: } else {
1525: local_frame.empty_params();
1.242 paf 1526: }
1.257 paf 1527: rcontext=wcontext=method_frame=&local_frame;
1.246 paf 1528:
1529: // prevent non-string writes for better error reporting
1.297 paf 1530: if(do_return_string)
1.357 misha 1531: local_frame.write(local_frame);
1.208 paf 1532:
1533: // execute!
1534: execute(*method.parser_code);
1535:
1536: // result
1.357 misha 1537: const String* result=do_return_string ? local_frame.get_string() : 0;
1.99 paf 1538:
1.330 misha 1539: RESTORE_CONTEXT
1.297 paf 1540:
1541: return result;
1.242 paf 1542: }
1543:
1.297 paf 1544: Request::Execute_nonvirtual_method_result
1545: Request::execute_nonvirtual_method(VStateless_class& aclass,
1.330 misha 1546: const String& method_name,
1547: VString* optional_param,
1548: bool do_return_string) {
1.297 paf 1549: Execute_nonvirtual_method_result result;
1550: result.method=aclass.get_method(method_name);
1551: if(result.method)
1.317 misha 1552: result.string=execute_method(aclass, *result.method, optional_param, do_return_string);
1.297 paf 1553: return result;
1.99 paf 1554: }
1555:
1.297 paf 1556: const String* Request::execute_virtual_method(Value& aself,
1.330 misha 1557: const String& method_name) {
1.354 misha 1558: if(Value* value=aself.get_element(method_name))
1.297 paf 1559: if(Junction* junction=value->get_junction())
1560: if(const Method *method=junction->method)
1561: return execute_method(aself, *method, 0/*no params*/, true);
1.188 parser 1562:
1.176 parser 1563: return 0;
1564: }
1.362 misha 1565:
1566: const String* Request::get_method_filename(const Method* method){
1567: if(ArrayOperation* code=method->parser_code)
1568: if(code){
1569: Operation::Origin origin={0, 0, 0};
1570: Array_iterator<Operation> i(*code);
1571: while( i.has_next() ){
1572: switch( i.next().code ){
1573: case OP::OP_CURLY_CODE__STORE_PARAM:
1574: case OP::OP_EXPR_CODE__STORE_PARAM:
1575: case OP::OP_CURLY_CODE__CONSTRUCT:
1576: case OP::OP_NESTED_CODE:
1577: case OP::OP_OBJECT_POOL:
1578: case OP::OP_STRING_POOL:
1579: case OP::OP_CALL:
1580: case OP::OP_CALL__WRITE:
1581: {
1582: i.next(); // skip local ops
1583: i.next(); // skip next opcode
1584: // continue execution
1585: }
1586: case OP::OP_CONSTRUCT_OBJECT:
1587: case OP::OP_CONSTRUCT_OBJECT__WRITE:
1588: case OP::OP_VALUE:
1589: case OP::OP_STRING__WRITE:
1590: case OP::OP_VALUE__GET_CLASS:
1591: #ifdef OPTIMIZE_BYTECODE_GET_OBJECT_ELEMENT
1592: case OP::OP_GET_OBJECT_ELEMENT:
1593: case OP::OP_GET_OBJECT_ELEMENT__WRITE:
1594: #endif
1595: #ifdef OPTIMIZE_BYTECODE_GET_OBJECT_VAR_ELEMENT
1596: case OP::OP_GET_OBJECT_VAR_ELEMENT:
1597: case OP::OP_GET_OBJECT_VAR_ELEMENT__WRITE:
1598: #endif
1599: #ifdef OPTIMIZE_BYTECODE_GET_ELEMENT
1600: case OP::OP_VALUE__GET_ELEMENT:
1601: case OP::OP_VALUE__GET_ELEMENT__WRITE:
1602: case OP::OP_VALUE__GET_ELEMENT_OR_OPERATOR:
1603: case OP::OP_WITH_ROOT__VALUE__GET_ELEMENT:
1604: #endif
1605: #ifdef OPTIMIZE_BYTECODE_GET_SELF_ELEMENT
1606: case OP::OP_WITH_SELF__VALUE__GET_ELEMENT:
1607: case OP::OP_WITH_SELF__VALUE__GET_ELEMENT__WRITE:
1608: #endif
1609: #ifdef OPTIMIZE_BYTECODE_CONSTRUCT
1610: case OP::OP_WITH_ROOT__VALUE__CONSTRUCT_EXPR:
1611: case OP::OP_WITH_ROOT__VALUE__CONSTRUCT_VALUE:
1612: case OP::OP_WITH_WRITE__VALUE__CONSTRUCT_EXPR:
1613: case OP::OP_WITH_WRITE__VALUE__CONSTRUCT_VALUE:
1614: case OP::OP_WITH_SELF__VALUE__CONSTRUCT_EXPR:
1615: case OP::OP_WITH_SELF__VALUE__CONSTRUCT_VALUE:
1616: #endif
1617: {
1618: origin=i.next().origin;
1619: break;
1620: }
1621: }
1622: if(origin.file_no)
1623: return get_used_filename(origin.file_no);
1624: }
1625: }
1626: return 0;
1627: }
1628: