|
|
1.117 paf 1: /** @file
1.118 paf 2: Parser: executor part of request class.
3:
1.305 paf 4: Copyright (c) 2001-2004 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.307 ! paf 8: static const char * const IDENT_EXECUTE_C="$Date: 2004/07/30 10:01: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"
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.66 paf 36: "GET_CLASS",
1.182 parser 37: "CONSTRUCT_VALUE", "CONSTRUCT_EXPR", "CURLY_CODE__CONSTRUCT",
1.108 paf 38: "WRITE_VALUE", "WRITE_EXPR_RESULT", "STRING__WRITE",
1.284 paf 39: "GET_ELEMENT_OR_OPERATOR", "GET_ELEMENT", "GET_ELEMENT__WRITE",
1.232 paf 40: "OBJECT_POOL", "STRING_POOL",
1.1 paf 41: "STORE_PARAM",
1.232 paf 42: "PREPARE_TO_CONSTRUCT_OBJECT", "PREPARE_TO_EXPRESSION",
43: "CALL", "CALL__WRITE",
1.49 paf 44:
45: // expression ops: unary
1.148 paf 46: "NEG", "INV", "NOT", "DEF", "IN", "FEXISTS", "DEXISTS",
1.49 paf 47: // expression ops: binary
1.201 paf 48: "SUB", "ADD", "MUL", "DIV", "MOD", "INTDIV",
1.275 paf 49: "BIN_SL", "BIN_SR",
1.56 paf 50: "BIN_AND", "BIN_OR", "BIN_XOR",
51: "LOG_AND", "LOG_OR", "LOG_XOR",
1.49 paf 52: "NUM_LT", "NUM_GT", "NUM_LE", "NUM_GE", "NUM_EQ", "NUM_NE",
1.103 paf 53: "STR_LT", "STR_GT", "STR_LE", "STR_GE", "STR_EQ", "STR_NE",
54: "IS"
1.1 paf 55: };
56:
1.297 paf 57: void va_debug_printf(SAPI_Info& sapi_info, const char* fmt,va_list args) {
1.127 paf 58: char buf[MAX_STRING];
59: vsnprintf(buf, MAX_STRING, fmt, args);
1.297 paf 60: SAPI::log(sapi_info, "%s", buf);
1.107 paf 61: }
62:
1.297 paf 63: void debug_printf(SAPI_Info& sapi_info, const char* fmt, ...) {
1.107 paf 64: va_list args;
1.297 paf 65: va_start(args, fmt);
66: va_debug_printf(sapi_info, fmt, args);
1.107 paf 67: va_end(args);
1.105 paf 68: }
69:
1.297 paf 70: void debug_dump(SAPI_Info& sapi_info, int level, ArrayOperation& ops) {
71: Array_iterator<Operation> i(ops);
1.190 parser 72: while(i.has_next()) {
1.297 paf 73: OPCODE opcode=i.next().code;
1.1 paf 74:
1.297 paf 75: if(opcode==OP_VALUE || opcode==OP_STRING__WRITE) {
76: Operation::Origin origin=i.next().origin;
77: Value& value=*i.next().value;
78: debug_printf(sapi_info,
1.133 paf 79: "%*s%s"
80: " \"%s\" %s",
1.297 paf 81: level*4, "", opcode_name[opcode],
82: value.get_string()->cstr(), value.type());
1.133 paf 83: continue;
1.15 paf 84: }
1.297 paf 85: debug_printf(sapi_info, "%*s%s", level*4, "", opcode_name[opcode]);
1.1 paf 86:
1.297 paf 87: switch(opcode) {
1.184 parser 88: case OP_CURLY_CODE__STORE_PARAM:
89: case OP_EXPR_CODE__STORE_PARAM:
90: case OP_CURLY_CODE__CONSTRUCT:
1.209 paf 91: case OP_NESTED_CODE:
1.226 paf 92: case OP_OBJECT_POOL:
93: case OP_STRING_POOL:
1.237 paf 94: case OP_CALL:
1.297 paf 95: case OP_CALL__WRITE:
96: if(ArrayOperation* local_ops=i.next().ops)
97: debug_dump(sapi_info, level+1, *local_ops);
1.1 paf 98: }
99: }
100: }
1.157 parser 101: #endif
1.1 paf 102:
1.297 paf 103: // Request
104:
105: void Request::execute(ArrayOperation& ops) {
106: register Stack<StackItem>& stack=this->stack; // helps a lot on MSVC: 'esi'
1.159 parser 107:
1.304 paf 108: const String* debug_name=0; Operation::Origin debug_origin={0, 0, 0};
1.297 paf 109: try{
1.157 parser 110: #ifdef DEBUG_EXECUTE
1.297 paf 111: debug_printf(sapi_info, "source----------------------------\n");
112: debug_dump(sapi_info, 0, ops);
113: debug_printf(sapi_info, "execution-------------------------\n");
1.157 parser 114: #endif
1.297 paf 115: for(Array_iterator<Operation> i(ops); i.has_next(); ) {
116: if(get_interrupted()) {
117: set_interrupted(false);
1.293 paf 118: throw Exception("parser.interrupted",
119: 0,
120: "execution stopped");
1.297 paf 121: }
122: OPCODE opcode=i.next().code;
1.293 paf 123:
1.157 parser 124: #ifdef DEBUG_EXECUTE
1.297 paf 125: debug_printf(sapi_info, "%d:%s", stack.top_index()+1, opcode_name[opcode]);
1.157 parser 126: #endif
1.11 paf 127:
1.297 paf 128: switch(opcode) {
1.51 paf 129: // param in next instruction
1.52 paf 130: case OP_VALUE:
1.32 paf 131: {
1.297 paf 132: debug_origin=i.next().origin;
133: Value& value=*i.next().value;
1.157 parser 134: #ifdef DEBUG_EXECUTE
1.297 paf 135: debug_printf(sapi_info, " \"%s\" %s", value.get_string()->cstr(), value.type());
1.157 parser 136: #endif
1.297 paf 137: stack.push(value);
1.32 paf 138: break;
139: }
1.66 paf 140: case OP_GET_CLASS:
1.38 paf 141: {
1.130 paf 142: // maybe they do ^class:method[] call, remember the fact
1.215 paf 143: wcontext->set_somebody_entered_some_class();
1.98 paf 144:
1.297 paf 145: const String& name=stack.pop().string();
146: Value* value=classes().get(name);
1.120 paf 147: if(!value)
1.223 paf 148: throw Exception("parser.runtime",
1.66 paf 149: &name,
1.143 paf 150: "class is undefined");
1.66 paf 151:
1.297 paf 152: stack.push(*value);
1.38 paf 153: break;
154: }
1.32 paf 155:
1.51 paf 156: // OP_WITH
1.187 parser 157: case OP_WITH_ROOT:
158: {
1.297 paf 159: stack.push(*method_frame);
1.187 parser 160: break;
161: }
1.37 paf 162: case OP_WITH_SELF:
163: {
1.297 paf 164: stack.push(get_self());
1.37 paf 165: break;
166: }
1.15 paf 167: case OP_WITH_READ:
168: {
1.297 paf 169: stack.push(*rcontext);
1.20 paf 170: break;
171: }
1.37 paf 172: case OP_WITH_WRITE:
1.20 paf 173: {
1.287 paf 174: if(wcontext==method_frame)
175: throw Exception("parser.runtime",
176: 0,
177: "$.name outside of $name[...]");
178:
1.297 paf 179: stack.push(*wcontext);
1.20 paf 180: break;
181: }
1.37 paf 182:
1.51 paf 183: // OTHER ACTIONS BUT WITHs
1.80 paf 184: case OP_CONSTRUCT_VALUE:
1.20 paf 185: {
1.297 paf 186: Value& value=stack.pop().value();
187: const String& name=stack.pop().string(); debug_name=&name;
188: Value& ncontext=stack.pop().value();
189: ncontext.put_element(name, &value, false);
1.213 paf 190: break;
191: }
1.80 paf 192: case OP_CONSTRUCT_EXPR:
193: {
1.219 paf 194: // see OP_PREPARE_TO_EXPRESSION
195: wcontext->set_in_expression(false);
196:
1.297 paf 197: Value& value=stack.pop().value();
198: const String& name=stack.pop().string(); debug_name=&name;
199: Value& ncontext=stack.pop().value();
200: ncontext.put_element(name, &value.as_expr_result(), false);
1.80 paf 201: break;
202: }
1.182 parser 203: case OP_CURLY_CODE__CONSTRUCT:
204: {
1.297 paf 205: ArrayOperation& local_ops=*i.next().ops;
1.182 parser 206: #ifdef DEBUG_EXECUTE
1.297 paf 207: debug_printf(sapi_info, " (%d)\n", local_ops.count());
208: debug_dump(sapi_info, 1, local_ops);
1.182 parser 209: #endif
1.297 paf 210: Value& value=*new VJunction(new Junction(
211: get_self(), 0,
1.257 paf 212: method_frame,
1.240 paf 213: rcontext,
214: wcontext,
1.297 paf 215: &local_ops));
1.238 paf 216:
1.297 paf 217: const String& name=stack.pop().string(); debug_name=&name;
218: Value& ncontext=stack.pop().value();
219: ncontext.put_element(name, &value, false);
1.182 parser 220: break;
221: }
1.209 paf 222: case OP_NESTED_CODE:
223: {
1.297 paf 224: ArrayOperation& local_ops=*i.next().ops;
1.209 paf 225: #ifdef DEBUG_EXECUTE
1.297 paf 226: debug_printf(sapi_info, " (%d)\n", local_ops.count());
227: debug_dump(sapi_info, 1, local_ops);
1.209 paf 228: #endif
1.297 paf 229: stack.push(local_ops);
1.209 paf 230: break;
231: }
1.108 paf 232: case OP_WRITE_VALUE:
1.13 paf 233: {
1.297 paf 234: Value& value=stack.pop().value();
235: write_assign_lang(value);
1.86 paf 236: break;
237: }
1.108 paf 238: case OP_WRITE_EXPR_RESULT:
239: {
1.297 paf 240: Value& value=stack.pop().value();
241: write_no_lang(value.as_expr_result());
1.230 paf 242:
243: // must be after write(result) and
1.219 paf 244: // see OP_PREPARE_TO_EXPRESSION
245: wcontext->set_in_expression(false);
1.108 paf 246: break;
247: }
1.86 paf 248: case OP_STRING__WRITE:
249: {
1.297 paf 250: i.next(); // ignore origin
251: Value* value=i.next().value;
1.157 parser 252: #ifdef DEBUG_EXECUTE
1.297 paf 253: debug_printf(sapi_info, " \"%s\"", value->get_string()->cstr());
1.157 parser 254: #endif
1.297 paf 255: write_no_lang(*value->get_string());
1.13 paf 256: break;
1.14 paf 257: }
1.13 paf 258:
1.215 paf 259: case OP_GET_ELEMENT_OR_OPERATOR:
260: {
1.297 paf 261: const String& name=stack.pop().string(); debug_name=&name;
262: Value& ncontext=stack.pop().value();
263: Value& value=get_element(ncontext, name, true);
264: stack.push(value);
1.215 paf 265: break;
266: }
1.15 paf 267: case OP_GET_ELEMENT:
1.11 paf 268: {
1.297 paf 269: const String& name=stack.pop().string(); debug_name=&name;
270: Value& ncontext=stack.pop().value();
271: Value& value=get_element(ncontext, name, false);
272: stack.push(value);
1.17 paf 273: break;
274: }
275:
1.18 paf 276: case OP_GET_ELEMENT__WRITE:
1.17 paf 277: {
1.297 paf 278: const String& name=stack.pop().string(); debug_name=&name;
279: Value& ncontext=stack.pop().value();
280: Value& value=get_element(ncontext, name, false);
281: write_assign_lang(value);
1.17 paf 282: break;
283: }
284:
1.32 paf 285:
1.226 paf 286: case OP_OBJECT_POOL:
1.17 paf 287: {
1.297 paf 288: ArrayOperation& local_ops=*i.next().ops;
1.226 paf 289:
1.257 paf 290: WContext *saved_wcontext=wcontext;
1.297 paf 291: String::Language saved_lang=flang;
292: flang=String::L_PASS_APPENDED;
293: WWrapper local(0/*empty*/, wcontext);
1.226 paf 294: wcontext=&local;
295:
1.297 paf 296: execute(local_ops);
1.226 paf 297:
1.297 paf 298: stack.push(wcontext->result().as_value());
1.257 paf 299: flang=saved_lang;
300: wcontext=saved_wcontext;
1.13 paf 301: break;
1.15 paf 302: }
1.13 paf 303:
1.226 paf 304: case OP_STRING_POOL:
1.49 paf 305: {
1.297 paf 306: ArrayOperation& local_ops=*i.next().ops;
1.226 paf 307:
1.257 paf 308: WContext *saved_wcontext=wcontext;
1.297 paf 309: WWrapper local(0 /*empty*/, wcontext);
1.226 paf 310: wcontext=&local;
311:
1.297 paf 312: execute(local_ops);
1.226 paf 313:
1.297 paf 314: Value* value;
1.49 paf 315: // from "$a $b" part of expression taking only string value,
316: // ignoring any other content of wcontext
1.297 paf 317: if(const String* string=wcontext->get_string())
318: value=new VString(*string);
1.80 paf 319: else
1.297 paf 320: value=new VVoid;
321: stack.push(*value);
322:
1.257 paf 323: wcontext=saved_wcontext;
1.49 paf 324: break;
325: }
326:
1.51 paf 327: // CALL
1.237 paf 328: case OP_STORE_PARAM:
1.28 paf 329: {
1.297 paf 330: Value& value=stack.pop().value();
1.299 paf 331: VMethodFrame& frame=stack.top_value().method_frame();
1.297 paf 332: // this op is executed from CALL local_ops only, so may skip the check "method_frame_to_fill==0"
333: frame.store_param(value);
1.28 paf 334: break;
335: }
1.237 paf 336: case OP_CURLY_CODE__STORE_PARAM:
337: case OP_EXPR_CODE__STORE_PARAM:
1.28 paf 338: {
1.237 paf 339: // code
1.297 paf 340: ArrayOperation& local_ops=*i.next().ops;
1.299 paf 341: VMethodFrame& frame=stack.top_value().method_frame();
1.237 paf 342: #ifdef DEBUG_EXECUTE
1.297 paf 343: debug_printf(sapi_info, " (%d)\n", local_ops.count());
344: debug_dump(sapi_info, 1, local_ops);
1.237 paf 345: #endif
346: // when they evaluate expression parameter,
347: // the object expression result
348: // does not need to be written into calling frame
349: // it must go into any expressions using that parameter
350: // hence, we zero junction.wcontext here, and later
351: // in .process we would test that field
352: // in decision "which wwrapper to use"
1.297 paf 353: Value& value=*new VJunction(new Junction(
354: get_self(), 0,
1.257 paf 355: method_frame,
1.237 paf 356: rcontext,
1.297 paf 357: opcode==OP_EXPR_CODE__STORE_PARAM?0:wcontext,
358: &local_ops));
1.237 paf 359: // store param
360: // this op is executed from CALL local_ops only, so can not check method_frame_to_fill==0
1.297 paf 361: frame.store_param(value);
1.29 paf 362: break;
363: }
364:
1.186 parser 365: case OP_PREPARE_TO_CONSTRUCT_OBJECT:
366: {
1.200 paf 367: wcontext->set_constructing(true);
1.186 parser 368: break;
369: }
1.219 paf 370:
371: case OP_PREPARE_TO_EXPRESSION:
372: {
373: wcontext->set_in_expression(true);
374: break;
375: }
376:
1.186 parser 377: case OP_CALL:
1.232 paf 378: case OP_CALL__WRITE:
1.29 paf 379: {
1.297 paf 380: //is_debug_junction=true;
381: ArrayOperation* local_ops=i.next().ops;
1.157 parser 382: #ifdef DEBUG_EXECUTE
1.297 paf 383: debug_printf(sapi_info, " (%d)\n", local_ops?local_ops->count():0);
384: if(local_ops)
385: debug_dump(sapi_info, 1, *local_ops);
1.237 paf 386:
1.297 paf 387: debug_printf(sapi_info, "->\n");
1.157 parser 388: #endif
1.297 paf 389: Value& value=stack.pop().value();
1.237 paf 390:
1.297 paf 391: Junction* junction=value.get_junction();
392: if(!junction) {
393: if(value.is("void"))
394: throw Exception("parser.runtime",
395: 0,
396: "undefined method");
397: else
398: throw Exception("parser.runtime",
399: 0,
400: "is '%s', not a method or junction, can not call it",
401: value.type());
402: }
1.282 paf 403: /* no check needed, code compiled the way that that's impossible
1.276 paf 404: // check:
405: // that this is method-junction, not a code-junction
1.278 paf 406: // [disabling these contstructions:]
407: // $junction{code}
408: // ^junction[]
1.276 paf 409: if(!junction->method)
410: throw Exception("parser.runtime",
1.297 paf 411:
412: "is '%s', it is code junction, can not call it",
413: value.type());
1.282 paf 414: */
1.237 paf 415:
1.297 paf 416: VMethodFrame frame(*junction, method_frame/*caller*/);
1.237 paf 417: if(local_ops){ // store param code goes here
1.297 paf 418: stack.push(frame); // argument for *STORE_PARAM ops
1.237 paf 419: execute(*local_ops);
1.297 paf 420: stack.pop().value();
1.237 paf 421: }
422: frame.fill_unspecified_params();
1.257 paf 423: VMethodFrame *saved_method_frame=method_frame;
1.297 paf 424: Value* saved_rcontext=rcontext;
1.257 paf 425: WContext *saved_wcontext=wcontext;
1.45 paf 426:
1.297 paf 427: VStateless_class& called_class=*frame.junction.self.get_class();
1.307 ! paf 428: Value* new_self;
1.200 paf 429: if(wcontext->get_constructing()) {
430: wcontext->set_constructing(false);
1.307 ! paf 431: new_self=&get_self();
1.237 paf 432: if(frame.junction.method->call_type!=Method::CT_STATIC) {
1.138 paf 433: // this is a constructor call
1.185 parser 434:
1.300 paf 435: if(Value* value=called_class.create_new_value(fpool)) {
1.204 paf 436: // some stateless_class creatable derivates
1.285 paf 437: new_self=value;
1.204 paf 438: } else
1.223 paf 439: throw Exception("parser.runtime",
1.297 paf 440: 0, //&frame.name(),
1.204 paf 441: "is not a constructor, system class '%s' can be constructed only implicitly",
1.297 paf 442: called_class.name().cstr());
1.204 paf 443:
1.285 paf 444: frame.write(*new_self,
1.297 paf 445: String::L_CLEAN // not used, always an object, not string
1.83 paf 446: );
1.185 parser 447: } else
1.223 paf 448: throw Exception("parser.runtime",
1.297 paf 449: 0, //&frame.name(),
1.185 parser 450: "method is static and can not be used as constructor");
1.249 paf 451: } else
1.285 paf 452: new_self=&frame.junction.self;
1.75 paf 453:
1.285 paf 454: frame.set_self(*new_self);
1.219 paf 455:
456: // see OP_PREPARE_TO_EXPRESSION
1.237 paf 457: frame.set_in_expression(wcontext->get_in_expression());
1.219 paf 458:
1.237 paf 459: rcontext=wcontext=&frame;
1.47 paf 460: {
1.237 paf 461: const Method& method=*frame.junction.method;
1.134 paf 462: Method::Call_type call_type=
1.297 paf 463: &called_class==new_self ? Method::CT_STATIC : Method::CT_DYNAMIC;
1.134 paf 464: if(
465: method.call_type==Method::CT_ANY ||
1.197 parser 466: method.call_type==call_type) { // allowed call type?
1.297 paf 467: method_frame=&frame;
468: if(method.native_code) { // native code?
469: method.check_actual_numbered_params(
470: frame.junction.self,
471: /*frame.name(), */frame.numbered_params());
472: method.native_code(
473: *this,
474: *frame.numbered_params()); // execute it
475: } else // parser code, execute it
1.307 ! paf 476: recoursion_checked_execute(*method.parser_code);
1.197 parser 477: } else
1.223 paf 478: throw Exception("parser.runtime",
1.297 paf 479: 0, //&frame.name(),
1.134 paf 480: "is not allowed to be called %s",
481: call_type==Method::CT_STATIC?"statically":"dynamically");
482:
1.47 paf 483: }
1.232 paf 484: StringOrValue result=wcontext->result();
1.45 paf 485:
1.257 paf 486: wcontext=saved_wcontext;
487: rcontext=saved_rcontext;
488: method_frame=saved_method_frame;
1.220 paf 489:
1.297 paf 490: if(opcode==OP_CALL__WRITE) {
491: write_assign_lang(result);
1.232 paf 492: } else { // OP_CALL
1.297 paf 493: stack.push(result.as_value());
1.232 paf 494: }
495:
1.157 parser 496: #ifdef DEBUG_EXECUTE
1.297 paf 497: debug_printf(sapi_info, "<-returned");
1.157 parser 498: #endif
1.297 paf 499: //is_debug_junction=false;
1.49 paf 500: break;
501: }
502:
1.55 paf 503: // expression ops: unary
504: case OP_NEG:
505: {
1.297 paf 506: Value& a=stack.pop().value();
507: Value& value=*new VDouble(-a.as_double());
508: stack.push(value);
1.55 paf 509: break;
510: }
511: case OP_INV:
512: {
1.297 paf 513: Value& a=stack.pop().value();
514: Value& value=*new VDouble(~a.as_int());
515: stack.push(value);
1.55 paf 516: break;
517: }
518: case OP_NOT:
519: {
1.297 paf 520: Value& a=stack.pop().value();
521: Value& value=*new VBool(!a.as_bool());
522: stack.push(value);
1.55 paf 523: break;
524: }
1.62 paf 525: case OP_DEF:
526: {
1.297 paf 527: Value& a=stack.pop().value();
528: Value& value=*new VBool(a.is_defined());
529: stack.push(value);
1.62 paf 530: break;
531: }
532: case OP_IN:
533: {
1.297 paf 534: Value& a=stack.pop().value();
535: const String& path=a.as_string();
536: Value& value=*new VBool(request_info.uri && *request_info.uri && path.this_starts(request_info.uri));
537: stack.push(value);
1.62 paf 538: break;
539: }
540: case OP_FEXISTS:
541: {
1.297 paf 542: Value& a=stack.pop().value();
543: Value& value=*new VBool(file_readable(absolute(a.as_string())));
544: stack.push(value);
1.148 paf 545: break;
546: }
547: case OP_DEXISTS:
548: {
1.297 paf 549: Value& a=stack.pop().value();
550: Value& value=*new VBool(dir_readable(absolute(a.as_string())));
551: stack.push(value);
1.62 paf 552: break;
553: }
1.55 paf 554:
555: // expression ops: binary
556: case OP_SUB:
1.53 paf 557: {
1.297 paf 558: Value& b=stack.pop().value(); Value& a=stack.pop().value();
559: Value& value=*new VDouble(a.as_double() - b.as_double());
560: stack.push(value);
1.53 paf 561: break;
562: }
1.55 paf 563: case OP_ADD:
1.53 paf 564: {
1.297 paf 565: Value& b=stack.pop().value(); Value& a=stack.pop().value();
566: Value& value=*new VDouble(a.as_double() + b.as_double());
567: stack.push(value);
1.53 paf 568: break;
569: }
1.49 paf 570: case OP_MUL:
571: {
1.297 paf 572: Value& b=stack.pop().value(); Value& a=stack.pop().value();
573: Value& value=*new VDouble(a.as_double() * b.as_double());
574: stack.push(value);
1.53 paf 575: break;
576: }
577: case OP_DIV:
578: {
1.297 paf 579: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1.170 parser 580:
1.297 paf 581: double a_double=a.as_double();
582: double b_double=b.as_double();
1.170 parser 583:
1.171 parser 584: if(b_double == 0) {
1.297 paf 585: //const String* problem_source=b.as_string();
1.223 paf 586: throw Exception("number.zerodivision",
1.297 paf 587: 0, //problem_source,
1.170 parser 588: "Division by zero");
1.171 parser 589: }
1.170 parser 590:
1.297 paf 591: Value& value=*new VDouble(a_double / b_double);
592: stack.push(value);
1.54 paf 593: break;
594: }
1.55 paf 595: case OP_MOD:
596: {
1.297 paf 597: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1.170 parser 598:
1.297 paf 599: double a_double=a.as_double();
600: double b_double=b.as_double();
1.170 parser 601:
1.173 parser 602: if(b_double == 0) {
1.297 paf 603: //const String* problem_source=b.as_string();
1.223 paf 604: throw Exception("number.zerodivision",
1.297 paf 605: 0, //problem_source,
1.170 parser 606: "Modulus by zero");
1.171 parser 607: }
1.170 parser 608:
1.297 paf 609: Value& value=*new VDouble(fmod(a_double, b_double));
610: stack.push(value);
1.201 paf 611: break;
612: }
613: case OP_INTDIV:
614: {
1.297 paf 615: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1.201 paf 616:
1.297 paf 617: int a_int=a.as_int();
618: int b_int=b.as_int();
1.201 paf 619:
620: if(b_int == 0) {
1.297 paf 621: //const String* problem_source=b.as_string();
1.223 paf 622: throw Exception("number.zerodivision",
1.297 paf 623: 0, //problem_source,
1.201 paf 624: "Division by zero");
625: }
626:
1.297 paf 627: Value& value=*new VInt(a_int / b_int);
628: stack.push(value);
1.55 paf 629: break;
630: }
1.274 paf 631: case OP_BIN_SL:
632: {
1.297 paf 633: Value& b=stack.pop().value(); Value& a=stack.pop().value();
634: Value& value=*new VInt(
635: a.as_int() <<
636: b.as_int());
637: stack.push(value);
1.274 paf 638: break;
639: }
640: case OP_BIN_SR:
641: {
1.297 paf 642: Value& b=stack.pop().value(); Value& a=stack.pop().value();
643: Value& value=*new VInt(
644: a.as_int() >>
645: b.as_int());
646: stack.push(value);
1.274 paf 647: break;
648: }
1.55 paf 649: case OP_BIN_AND:
1.54 paf 650: {
1.297 paf 651: Value& b=stack.pop().value(); Value& a=stack.pop().value();
652: Value& value=*new VInt(
653: a.as_int() &
654: b.as_int());
655: stack.push(value);
1.54 paf 656: break;
657: }
1.55 paf 658: case OP_BIN_OR:
1.54 paf 659: {
1.297 paf 660: Value& b=stack.pop().value(); Value& a=stack.pop().value();
661: Value& value=*new VInt(
662: a.as_int() |
663: b.as_int());
664: stack.push(value);
1.55 paf 665: break;
666: }
1.56 paf 667: case OP_BIN_XOR:
668: {
1.297 paf 669: Value& b=stack.pop().value(); Value& a=stack.pop().value();
670: Value& value=*new VInt(
671: a.as_int() ^
672: b.as_int());
673: stack.push(value);
1.56 paf 674: break;
675: }
1.55 paf 676: case OP_LOG_AND:
677: {
1.297 paf 678: ArrayOperation& local_ops=stack.pop().ops(); Value& a=stack.pop().value();
1.263 paf 679: bool result;
1.297 paf 680: if(a.as_bool()) {
681: execute(local_ops);
682: Value& b=stack.pop().value();
683: result=b.as_bool();
1.209 paf 684: } else
1.263 paf 685: result=false;
1.297 paf 686: Value& value=*new VBool(result);
687: stack.push(value);
1.55 paf 688: break;
689: }
690: case OP_LOG_OR:
691: {
1.297 paf 692: ArrayOperation& local_ops=stack.pop().ops(); Value& a=stack.pop().value();
1.263 paf 693: bool result;
1.297 paf 694: if(a.as_bool())
1.263 paf 695: result=true;
1.209 paf 696: else {
1.297 paf 697: execute(local_ops);
698: Value& b=stack.pop().value();
699: result=b.as_bool();
1.209 paf 700: }
1.297 paf 701: Value& value=*new VBool(result);
702: stack.push(value);
1.56 paf 703: break;
704: }
705: case OP_LOG_XOR:
706: {
1.297 paf 707: Value& b=stack.pop().value(); Value& a=stack.pop().value();
708: Value& value=*new VBool(a.as_bool() ^ b.as_bool());
709: stack.push(value);
1.55 paf 710: break;
711: }
712: case OP_NUM_LT:
713: {
1.297 paf 714: volatile double b_double=stack.pop().value().as_double();
715: volatile double a_double=stack.pop().value().as_double();
716: Value& value=*new VBool(a_double<b_double);
717: stack.push(value);
1.55 paf 718: break;
719: }
720: case OP_NUM_GT:
721: {
1.297 paf 722: volatile double b_double=stack.pop().value().as_double();
723: volatile double a_double=stack.pop().value().as_double();
724: Value& value=*new VBool(a_double>b_double);
725: stack.push(value);
1.55 paf 726: break;
727: }
728: case OP_NUM_LE:
729: {
1.297 paf 730: volatile double b_double=stack.pop().value().as_double();
731: volatile double a_double=stack.pop().value().as_double();
732: Value& value=*new VBool(a_double<=b_double);
733: stack.push(value);
1.55 paf 734: break;
735: }
736: case OP_NUM_GE:
737: {
1.297 paf 738: volatile double b_double=stack.pop().value().as_double();
739: volatile double a_double=stack.pop().value().as_double();
740: Value& value=*new VBool(a_double>=b_double);
741: stack.push(value);
1.55 paf 742: break;
743: }
744: case OP_NUM_EQ:
745: {
1.297 paf 746: volatile double b_double=stack.pop().value().as_double();
747: volatile double a_double=stack.pop().value().as_double();
748: Value& value=*new VBool(a_double==b_double);
749: stack.push(value);
1.55 paf 750: break;
751: }
752: case OP_NUM_NE:
753: {
1.297 paf 754: volatile double b_double=stack.pop().value().as_double();
755: volatile double a_double=stack.pop().value().as_double();
756: Value& value=*new VBool(a_double!=b_double);
757: stack.push(value);
1.54 paf 758: break;
759: }
1.58 paf 760: case OP_STR_LT:
761: {
1.297 paf 762: Value& b=stack.pop().value(); Value& a=stack.pop().value();
763: Value& value=*new VBool(a.as_string() < b.as_string());
764: stack.push(value);
1.58 paf 765: break;
766: }
767: case OP_STR_GT:
768: {
1.297 paf 769: Value& b=stack.pop().value(); Value& a=stack.pop().value();
770: Value& value=*new VBool(a.as_string() > b.as_string());
771: stack.push(value);
1.58 paf 772: break;
773: }
1.55 paf 774: case OP_STR_LE:
1.58 paf 775: {
1.297 paf 776: Value& b=stack.pop().value(); Value& a=stack.pop().value();
777: Value& value=*new VBool(a.as_string() <= b.as_string());
778: stack.push(value);
1.58 paf 779: break;
780: }
1.55 paf 781: case OP_STR_GE:
1.54 paf 782: {
1.297 paf 783: Value& b=stack.pop().value(); Value& a=stack.pop().value();
784: Value& value=*new VBool(a.as_string() >= b.as_string());
785: stack.push(value);
1.58 paf 786: break;
787: }
788: case OP_STR_EQ:
789: {
1.297 paf 790: Value& b=stack.pop().value(); Value& a=stack.pop().value();
791: Value& value=*new VBool(a.as_string() == b.as_string());
792: stack.push(value);
1.58 paf 793: break;
794: }
795: case OP_STR_NE:
796: {
1.297 paf 797: Value& b=stack.pop().value(); Value& a=stack.pop().value();
798: Value& value=*new VBool(a.as_string() != b.as_string());
799: stack.push(value);
1.103 paf 800: break;
801: }
802: case OP_IS:
803: {
1.297 paf 804: Value& b=stack.pop().value(); Value& a=stack.pop().value();
805: Value& value=*new VBool(a.is(b.as_string().cstr()));
806: stack.push(value);
1.28 paf 807: break;
808: }
809:
1.11 paf 810: default:
1.223 paf 811: throw Exception(0,
1.67 paf 812: 0,
1.297 paf 813: "invalid opcode %d", opcode);
1.11 paf 814: }
815: }
1.306 paf 816: } catch(const Exception&) {
1.297 paf 817: // record it to stack trace
1.301 paf 818: if(debug_name)
819: exception_trace.push(Trace(debug_name, debug_origin));
1.297 paf 820: rethrow;
821: }
1.1 paf 822: }
1.17 paf 823:
1.292 paf 824: /**
825: @todo cache|prepare junctions
826: @bug ^superbase:method would dynamically call ^base:method if there is any
827: */
1.297 paf 828: Value& Request::get_element(Value& ncontext, const String& name, bool can_call_operator) {
829: Value* value=0;
1.249 paf 830: if(can_call_operator) {
1.297 paf 831: if(Method* method=main_class.get_method(name)) // looking operator of that name FIRST
1.307 ! paf 832: value=new VJunction(new Junction(main_class, method));
1.249 paf 833: }
834: if(!value) {
835: if(!wcontext->get_constructing() // not constructing
836: && wcontext->get_somebody_entered_some_class() ) // ^class:method
1.297 paf 837: if(VStateless_class *called_class=ncontext.get_class())
1.249 paf 838: if(VStateless_class *read_class=rcontext->get_class())
839: if(read_class->derived_from(*called_class)) // current derived from called
1.297 paf 840: if(Value* base=get_self().base()) { // doing DYNAMIC call
1.290 paf 841: Temp_derived temp_derived(*base, 0); // temporarily prevent go-back-down virtual calls
842: value=base->get_element(name, *base, false); // virtual-up lookup starting from parent
1.289 paf 843: goto value_ready;
1.249 paf 844: }
845: }
1.216 paf 846: if(!value)
1.297 paf 847: value=ncontext.get_element(name, ncontext, false);
1.291 paf 848:
849: if(value && wcontext->get_constructing())
1.297 paf 850: if(Junction* junction=value->get_junction()) {
851: if(junction->self.get_class()!=&ncontext)
1.291 paf 852: throw Exception("parser.runtime",
853: &name,
1.297 paf 854: "constructor must be declared in class '%s'",
855: ncontext.get_class()->name_cstr());
1.291 paf 856: }
1.249 paf 857:
1.289 paf 858: value_ready:
1.284 paf 859: if(value)
1.294 paf 860: value=&process_to_value(*value); // process possible code-junction
1.284 paf 861: else
1.297 paf 862: value=new VVoid;
1.63 paf 863:
1.297 paf 864: return *value;
1.34 paf 865: }
1.70 paf 866:
1.162 parser 867: /** @param intercept_string
1.116 paf 868: - true:
869: they want result=string value,
870: possible object result goes to wcontext
871: - false:
872: they want any result[string|object]
873: nothing goes to wcontext.
1.125 paf 874: used in @c (expression) params evaluation
1.224 paf 875:
876: using the fact it's either string_ or value_ result requested to speed up checkes
1.116 paf 877: */
1.229 paf 878: StringOrValue Request::process(Value& input_value, bool intercept_string) {
1.297 paf 879: Junction* junction=input_value.get_junction();
1.307 ! paf 880: if(junction) {
! 881: if(junction->is_getter) { // is it a getter-junction?
! 882: // process it
! 883:
! 884: VMethodFrame frame(*junction, method_frame/*caller*/);
! 885: if(junction->method->params_names)
! 886: throw Exception("parser.runtime",
! 887: 0,
! 888: "getter method must have no parameters");
! 889:
! 890: frame.set_self(frame.junction.self);
! 891:
! 892: VMethodFrame *saved_method_frame=method_frame;
! 893: Value* saved_rcontext=rcontext;
! 894: WContext *saved_wcontext=wcontext;
! 895:
! 896: rcontext=wcontext=&frame;
! 897: method_frame=&frame;
! 898:
! 899: recoursion_checked_execute(*frame.junction.method->parser_code); // parser code, execute it
! 900: StringOrValue result=wcontext->result();
! 901:
! 902: method_frame=saved_method_frame;
! 903: wcontext=saved_wcontext;
! 904: rcontext=saved_rcontext;
! 905:
! 906: return result;
! 907: }
! 908:
! 909: if(junction->code) { // is it a code-junction?
! 910: // process it
! 911: StringOrValue result;
1.157 parser 912: #ifdef DEBUG_EXECUTE
1.307 ! paf 913: debug_printf(sapi_info, "ja->\n");
1.157 parser 914: #endif
1.238 paf 915:
1.307 ! paf 916: if(!junction->method_frame)
! 917: throw Exception("parser.runtime",
1.240 paf 918: 0,
919: "junction used outside of context");
920:
1.307 ! paf 921: VMethodFrame *saved_method_frame=method_frame;
! 922: Value* saved_rcontext=rcontext;
! 923: WContext *saved_wcontext=wcontext;
! 924:
! 925: method_frame=junction->method_frame;
! 926: rcontext=junction->rcontext;
! 927:
! 928: // for expression method params
! 929: // wcontext is set 0
! 930: // using the fact in decision "which wwrapper to use"
! 931: bool using_code_frame=intercept_string && junction->wcontext;
! 932: if(using_code_frame) {
! 933: // almost plain wwrapper about junction wcontext,
! 934: // BUT intercepts string writes
! 935: VCodeFrame local(*junction->wcontext, junction->wcontext);
! 936: wcontext=&local;
! 937:
! 938: // execute it
! 939: recoursion_checked_execute(*junction->code);
! 940:
! 941: // CodeFrame soul:
! 942: // string writes were intercepted
! 943: // returning them as the result of getting code-junction
! 944: result.set_string(*wcontext->get_string());
! 945: } else {
! 946: // plain wwrapper
! 947: WWrapper local(0/*empty*/, wcontext);
! 948: wcontext=&local;
! 949:
! 950: // execute it
! 951: recoursion_checked_execute(*junction->code);
! 952:
! 953: result=wcontext->result();
! 954: }
! 955:
! 956: wcontext=saved_wcontext;
! 957: rcontext=saved_rcontext;
! 958: method_frame=saved_method_frame;
1.207 paf 959:
1.157 parser 960: #ifdef DEBUG_EXECUTE
1.307 ! paf 961: debug_printf(sapi_info, "<-ja returned");
1.157 parser 962: #endif
1.307 ! paf 963: return result;
! 964: }
! 965:
! 966: // it is then method-junction, do not explode it
! 967: // just return it as we do for usual objects
! 968: }
! 969:
! 970: return input_value;
1.85 paf 971: }
972:
1.298 paf 973: StringOrValue Request::execute_method(VMethodFrame& amethod_frame, const Method& method) {
1.257 paf 974: VMethodFrame *saved_method_frame=method_frame;
1.297 paf 975: Value* saved_rcontext=rcontext;
1.257 paf 976: WContext *saved_wcontext=wcontext;
1.99 paf 977:
978: // initialize contexts
1.286 paf 979: rcontext=wcontext=method_frame=&amethod_frame;
1.99 paf 980:
981: // execute!
982: execute(*method.parser_code);
983:
984: // result
1.298 paf 985: StringOrValue result=wcontext->result();
1.208 paf 986:
1.257 paf 987: wcontext=saved_wcontext;
988: rcontext=saved_rcontext;
989: method_frame=saved_method_frame;
1.242 paf 990:
991: // return
992: return result;
1.208 paf 993: }
994:
1.297 paf 995: const String* Request::execute_method(Value& aself,
996: const Method& method, VString* optional_param,
997: bool do_return_string) {
1.257 paf 998: VMethodFrame *saved_method_frame=method_frame;
1.297 paf 999: Value* saved_rcontext=rcontext;
1.257 paf 1000: WContext *saved_wcontext=wcontext;
1.208 paf 1001:
1.307 ! paf 1002: Junction local_junction(aself, &method);
1.297 paf 1003: VMethodFrame local_frame(local_junction, method_frame/*caller*/);
1.242 paf 1004: if(optional_param && local_frame.can_store_param()) {
1.297 paf 1005: local_frame.store_param(*optional_param);
1.242 paf 1006: local_frame.fill_unspecified_params();
1007: }
1.285 paf 1008: local_frame.set_self(aself);
1.257 paf 1009: rcontext=wcontext=method_frame=&local_frame;
1.246 paf 1010:
1011: // prevent non-string writes for better error reporting
1.297 paf 1012: if(do_return_string)
1.246 paf 1013: wcontext->write(local_frame);
1.208 paf 1014:
1015: // execute!
1016: execute(*method.parser_code);
1017:
1018: // result
1.297 paf 1019: const String* result=0;
1020: if(do_return_string)
1021: result=&wcontext->result().as_string();
1.99 paf 1022:
1.257 paf 1023: wcontext=saved_wcontext;
1024: rcontext=saved_rcontext;
1025: method_frame=saved_method_frame;
1.297 paf 1026:
1027: return result;
1.242 paf 1028: }
1029:
1.297 paf 1030: Request::Execute_nonvirtual_method_result
1031: Request::execute_nonvirtual_method(VStateless_class& aclass,
1032: const String& method_name, VString* optional_param,
1033: bool do_return_string) {
1034: Execute_nonvirtual_method_result result;
1035: result.method=aclass.get_method(method_name);
1036: if(result.method)
1037: result.string=execute_method(aclass, *result.method, optional_param,
1038: do_return_string);
1039: return result;
1.99 paf 1040: }
1041:
1.297 paf 1042: const String* Request::execute_virtual_method(Value& aself,
1043: const String& method_name) {
1044: if(Value* value=aself.get_element(method_name, aself, false))
1045: if(Junction* junction=value->get_junction())
1046: if(const Method *method=junction->method)
1047: return execute_method(aself, *method, 0/*no params*/, true);
1.188 parser 1048:
1.176 parser 1049: return 0;
1050: }