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