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