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