|
|
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.309 ! paf 8: static const char * const IDENT_EXECUTE_C="$Date: 2005/07/26 12:43:05 $";
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();
1.309 ! paf 189: put_element(ncontext, name, value);
1.308 paf 190:
1.213 paf 191: break;
192: }
1.80 paf 193: case OP_CONSTRUCT_EXPR:
194: {
1.219 paf 195: // see OP_PREPARE_TO_EXPRESSION
196: wcontext->set_in_expression(false);
197:
1.308 paf 198: Value& expr=stack.pop().value();
1.297 paf 199: const String& name=stack.pop().string(); debug_name=&name;
200: Value& ncontext=stack.pop().value();
1.308 paf 201: Value& value=expr.as_expr_result();
1.309 ! paf 202: put_element(ncontext, name, value);
1.80 paf 203: break;
204: }
1.182 parser 205: case OP_CURLY_CODE__CONSTRUCT:
206: {
1.297 paf 207: ArrayOperation& local_ops=*i.next().ops;
1.182 parser 208: #ifdef DEBUG_EXECUTE
1.297 paf 209: debug_printf(sapi_info, " (%d)\n", local_ops.count());
210: debug_dump(sapi_info, 1, local_ops);
1.182 parser 211: #endif
1.297 paf 212: Value& value=*new VJunction(new Junction(
213: get_self(), 0,
1.257 paf 214: method_frame,
1.240 paf 215: rcontext,
216: wcontext,
1.297 paf 217: &local_ops));
1.238 paf 218:
1.297 paf 219: const String& name=stack.pop().string(); debug_name=&name;
220: Value& ncontext=stack.pop().value();
1.308 paf 221: if(const Junction* junction=ncontext.put_element(name, &value, false))
222: if(junction!=PUT_ELEMENT_REPLACED_ELEMENT)
223: throw Exception("parser.runtime",
224: 0,
225: "property value can not be code, use [] or () brackets");
226:
1.182 parser 227: break;
228: }
1.209 paf 229: case OP_NESTED_CODE:
230: {
1.297 paf 231: ArrayOperation& local_ops=*i.next().ops;
1.209 paf 232: #ifdef DEBUG_EXECUTE
1.297 paf 233: debug_printf(sapi_info, " (%d)\n", local_ops.count());
234: debug_dump(sapi_info, 1, local_ops);
1.209 paf 235: #endif
1.297 paf 236: stack.push(local_ops);
1.209 paf 237: break;
238: }
1.108 paf 239: case OP_WRITE_VALUE:
1.13 paf 240: {
1.297 paf 241: Value& value=stack.pop().value();
242: write_assign_lang(value);
1.86 paf 243: break;
244: }
1.108 paf 245: case OP_WRITE_EXPR_RESULT:
246: {
1.297 paf 247: Value& value=stack.pop().value();
248: write_no_lang(value.as_expr_result());
1.230 paf 249:
250: // must be after write(result) and
1.219 paf 251: // see OP_PREPARE_TO_EXPRESSION
252: wcontext->set_in_expression(false);
1.108 paf 253: break;
254: }
1.86 paf 255: case OP_STRING__WRITE:
256: {
1.297 paf 257: i.next(); // ignore origin
258: Value* value=i.next().value;
1.157 parser 259: #ifdef DEBUG_EXECUTE
1.297 paf 260: debug_printf(sapi_info, " \"%s\"", value->get_string()->cstr());
1.157 parser 261: #endif
1.297 paf 262: write_no_lang(*value->get_string());
1.13 paf 263: break;
1.14 paf 264: }
1.13 paf 265:
1.215 paf 266: case OP_GET_ELEMENT_OR_OPERATOR:
267: {
1.297 paf 268: const String& name=stack.pop().string(); debug_name=&name;
269: Value& ncontext=stack.pop().value();
270: Value& value=get_element(ncontext, name, true);
271: stack.push(value);
1.215 paf 272: break;
273: }
1.15 paf 274: case OP_GET_ELEMENT:
1.11 paf 275: {
1.297 paf 276: const String& name=stack.pop().string(); debug_name=&name;
277: Value& ncontext=stack.pop().value();
278: Value& value=get_element(ncontext, name, false);
279: stack.push(value);
1.17 paf 280: break;
281: }
282:
1.18 paf 283: case OP_GET_ELEMENT__WRITE:
1.17 paf 284: {
1.297 paf 285: const String& name=stack.pop().string(); debug_name=&name;
286: Value& ncontext=stack.pop().value();
287: Value& value=get_element(ncontext, name, false);
288: write_assign_lang(value);
1.17 paf 289: break;
290: }
291:
1.32 paf 292:
1.226 paf 293: case OP_OBJECT_POOL:
1.17 paf 294: {
1.297 paf 295: ArrayOperation& local_ops=*i.next().ops;
1.226 paf 296:
1.257 paf 297: WContext *saved_wcontext=wcontext;
1.297 paf 298: String::Language saved_lang=flang;
299: flang=String::L_PASS_APPENDED;
300: WWrapper local(0/*empty*/, wcontext);
1.226 paf 301: wcontext=&local;
302:
1.297 paf 303: execute(local_ops);
1.226 paf 304:
1.297 paf 305: stack.push(wcontext->result().as_value());
1.257 paf 306: flang=saved_lang;
307: wcontext=saved_wcontext;
1.13 paf 308: break;
1.15 paf 309: }
1.13 paf 310:
1.226 paf 311: case OP_STRING_POOL:
1.49 paf 312: {
1.297 paf 313: ArrayOperation& local_ops=*i.next().ops;
1.226 paf 314:
1.257 paf 315: WContext *saved_wcontext=wcontext;
1.297 paf 316: WWrapper local(0 /*empty*/, wcontext);
1.226 paf 317: wcontext=&local;
318:
1.297 paf 319: execute(local_ops);
1.226 paf 320:
1.297 paf 321: Value* value;
1.49 paf 322: // from "$a $b" part of expression taking only string value,
323: // ignoring any other content of wcontext
1.297 paf 324: if(const String* string=wcontext->get_string())
325: value=new VString(*string);
1.80 paf 326: else
1.297 paf 327: value=new VVoid;
328: stack.push(*value);
329:
1.257 paf 330: wcontext=saved_wcontext;
1.49 paf 331: break;
332: }
333:
1.51 paf 334: // CALL
1.237 paf 335: case OP_STORE_PARAM:
1.28 paf 336: {
1.297 paf 337: Value& value=stack.pop().value();
1.299 paf 338: VMethodFrame& frame=stack.top_value().method_frame();
1.297 paf 339: // this op is executed from CALL local_ops only, so may skip the check "method_frame_to_fill==0"
340: frame.store_param(value);
1.28 paf 341: break;
342: }
1.237 paf 343: case OP_CURLY_CODE__STORE_PARAM:
344: case OP_EXPR_CODE__STORE_PARAM:
1.28 paf 345: {
1.237 paf 346: // code
1.297 paf 347: ArrayOperation& local_ops=*i.next().ops;
1.299 paf 348: VMethodFrame& frame=stack.top_value().method_frame();
1.237 paf 349: #ifdef DEBUG_EXECUTE
1.297 paf 350: debug_printf(sapi_info, " (%d)\n", local_ops.count());
351: debug_dump(sapi_info, 1, local_ops);
1.237 paf 352: #endif
353: // when they evaluate expression parameter,
354: // the object expression result
355: // does not need to be written into calling frame
356: // it must go into any expressions using that parameter
357: // hence, we zero junction.wcontext here, and later
358: // in .process we would test that field
359: // in decision "which wwrapper to use"
1.297 paf 360: Value& value=*new VJunction(new Junction(
361: get_self(), 0,
1.257 paf 362: method_frame,
1.237 paf 363: rcontext,
1.297 paf 364: opcode==OP_EXPR_CODE__STORE_PARAM?0:wcontext,
365: &local_ops));
1.237 paf 366: // store param
367: // this op is executed from CALL local_ops only, so can not check method_frame_to_fill==0
1.297 paf 368: frame.store_param(value);
1.29 paf 369: break;
370: }
371:
1.186 parser 372: case OP_PREPARE_TO_CONSTRUCT_OBJECT:
373: {
1.200 paf 374: wcontext->set_constructing(true);
1.186 parser 375: break;
376: }
1.219 paf 377:
378: case OP_PREPARE_TO_EXPRESSION:
379: {
380: wcontext->set_in_expression(true);
381: break;
382: }
383:
1.186 parser 384: case OP_CALL:
1.232 paf 385: case OP_CALL__WRITE:
1.29 paf 386: {
1.297 paf 387: //is_debug_junction=true;
388: ArrayOperation* local_ops=i.next().ops;
1.157 parser 389: #ifdef DEBUG_EXECUTE
1.297 paf 390: debug_printf(sapi_info, " (%d)\n", local_ops?local_ops->count():0);
391: if(local_ops)
392: debug_dump(sapi_info, 1, *local_ops);
1.237 paf 393:
1.297 paf 394: debug_printf(sapi_info, "->\n");
1.157 parser 395: #endif
1.297 paf 396: Value& value=stack.pop().value();
1.237 paf 397:
1.297 paf 398: Junction* junction=value.get_junction();
399: if(!junction) {
400: if(value.is("void"))
401: throw Exception("parser.runtime",
402: 0,
403: "undefined method");
404: else
405: throw Exception("parser.runtime",
406: 0,
407: "is '%s', not a method or junction, can not call it",
408: value.type());
409: }
1.282 paf 410: /* no check needed, code compiled the way that that's impossible
1.276 paf 411: // check:
412: // that this is method-junction, not a code-junction
1.278 paf 413: // [disabling these contstructions:]
414: // $junction{code}
415: // ^junction[]
1.276 paf 416: if(!junction->method)
417: throw Exception("parser.runtime",
1.297 paf 418:
419: "is '%s', it is code junction, can not call it",
420: value.type());
1.282 paf 421: */
1.237 paf 422:
1.297 paf 423: VMethodFrame frame(*junction, method_frame/*caller*/);
1.237 paf 424: if(local_ops){ // store param code goes here
1.297 paf 425: stack.push(frame); // argument for *STORE_PARAM ops
1.237 paf 426: execute(*local_ops);
1.297 paf 427: stack.pop().value();
1.237 paf 428: }
429: frame.fill_unspecified_params();
1.257 paf 430: VMethodFrame *saved_method_frame=method_frame;
1.297 paf 431: Value* saved_rcontext=rcontext;
1.257 paf 432: WContext *saved_wcontext=wcontext;
1.45 paf 433:
1.297 paf 434: VStateless_class& called_class=*frame.junction.self.get_class();
1.307 paf 435: Value* new_self;
1.200 paf 436: if(wcontext->get_constructing()) {
437: wcontext->set_constructing(false);
1.307 paf 438: new_self=&get_self();
1.237 paf 439: if(frame.junction.method->call_type!=Method::CT_STATIC) {
1.138 paf 440: // this is a constructor call
1.185 parser 441:
1.300 paf 442: if(Value* value=called_class.create_new_value(fpool)) {
1.204 paf 443: // some stateless_class creatable derivates
1.285 paf 444: new_self=value;
1.204 paf 445: } else
1.223 paf 446: throw Exception("parser.runtime",
1.297 paf 447: 0, //&frame.name(),
1.204 paf 448: "is not a constructor, system class '%s' can be constructed only implicitly",
1.297 paf 449: called_class.name().cstr());
1.204 paf 450:
1.285 paf 451: frame.write(*new_self,
1.297 paf 452: String::L_CLEAN // not used, always an object, not string
1.83 paf 453: );
1.185 parser 454: } else
1.223 paf 455: throw Exception("parser.runtime",
1.297 paf 456: 0, //&frame.name(),
1.185 parser 457: "method is static and can not be used as constructor");
1.249 paf 458: } else
1.285 paf 459: new_self=&frame.junction.self;
1.75 paf 460:
1.285 paf 461: frame.set_self(*new_self);
1.219 paf 462:
463: // see OP_PREPARE_TO_EXPRESSION
1.237 paf 464: frame.set_in_expression(wcontext->get_in_expression());
1.219 paf 465:
1.237 paf 466: rcontext=wcontext=&frame;
1.47 paf 467: {
1.237 paf 468: const Method& method=*frame.junction.method;
1.134 paf 469: Method::Call_type call_type=
1.297 paf 470: &called_class==new_self ? Method::CT_STATIC : Method::CT_DYNAMIC;
1.134 paf 471: if(
472: method.call_type==Method::CT_ANY ||
1.197 parser 473: method.call_type==call_type) { // allowed call type?
1.297 paf 474: method_frame=&frame;
475: if(method.native_code) { // native code?
476: method.check_actual_numbered_params(
477: frame.junction.self,
478: /*frame.name(), */frame.numbered_params());
479: method.native_code(
480: *this,
481: *frame.numbered_params()); // execute it
482: } else // parser code, execute it
1.307 paf 483: recoursion_checked_execute(*method.parser_code);
1.197 parser 484: } else
1.223 paf 485: throw Exception("parser.runtime",
1.297 paf 486: 0, //&frame.name(),
1.134 paf 487: "is not allowed to be called %s",
488: call_type==Method::CT_STATIC?"statically":"dynamically");
489:
1.47 paf 490: }
1.232 paf 491: StringOrValue result=wcontext->result();
1.45 paf 492:
1.257 paf 493: wcontext=saved_wcontext;
494: rcontext=saved_rcontext;
495: method_frame=saved_method_frame;
1.220 paf 496:
1.297 paf 497: if(opcode==OP_CALL__WRITE) {
498: write_assign_lang(result);
1.232 paf 499: } else { // OP_CALL
1.297 paf 500: stack.push(result.as_value());
1.232 paf 501: }
502:
1.157 parser 503: #ifdef DEBUG_EXECUTE
1.297 paf 504: debug_printf(sapi_info, "<-returned");
1.157 parser 505: #endif
1.297 paf 506: //is_debug_junction=false;
1.49 paf 507: break;
508: }
509:
1.55 paf 510: // expression ops: unary
511: case OP_NEG:
512: {
1.297 paf 513: Value& a=stack.pop().value();
514: Value& value=*new VDouble(-a.as_double());
515: stack.push(value);
1.55 paf 516: break;
517: }
518: case OP_INV:
519: {
1.297 paf 520: Value& a=stack.pop().value();
521: Value& value=*new VDouble(~a.as_int());
522: stack.push(value);
1.55 paf 523: break;
524: }
525: case OP_NOT:
526: {
1.297 paf 527: Value& a=stack.pop().value();
528: Value& value=*new VBool(!a.as_bool());
529: stack.push(value);
1.55 paf 530: break;
531: }
1.62 paf 532: case OP_DEF:
533: {
1.297 paf 534: Value& a=stack.pop().value();
535: Value& value=*new VBool(a.is_defined());
536: stack.push(value);
1.62 paf 537: break;
538: }
539: case OP_IN:
540: {
1.297 paf 541: Value& a=stack.pop().value();
542: const String& path=a.as_string();
543: Value& value=*new VBool(request_info.uri && *request_info.uri && path.this_starts(request_info.uri));
544: stack.push(value);
1.62 paf 545: break;
546: }
547: case OP_FEXISTS:
548: {
1.297 paf 549: Value& a=stack.pop().value();
550: Value& value=*new VBool(file_readable(absolute(a.as_string())));
551: stack.push(value);
1.148 paf 552: break;
553: }
554: case OP_DEXISTS:
555: {
1.297 paf 556: Value& a=stack.pop().value();
557: Value& value=*new VBool(dir_readable(absolute(a.as_string())));
558: stack.push(value);
1.62 paf 559: break;
560: }
1.55 paf 561:
562: // expression ops: binary
563: case OP_SUB:
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.55 paf 570: case OP_ADD:
1.53 paf 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: }
1.49 paf 577: case OP_MUL:
578: {
1.297 paf 579: Value& b=stack.pop().value(); Value& a=stack.pop().value();
580: Value& value=*new VDouble(a.as_double() * b.as_double());
581: stack.push(value);
1.53 paf 582: break;
583: }
584: case OP_DIV:
585: {
1.297 paf 586: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1.170 parser 587:
1.297 paf 588: double a_double=a.as_double();
589: double b_double=b.as_double();
1.170 parser 590:
1.171 parser 591: if(b_double == 0) {
1.297 paf 592: //const String* problem_source=b.as_string();
1.223 paf 593: throw Exception("number.zerodivision",
1.297 paf 594: 0, //problem_source,
1.170 parser 595: "Division by zero");
1.171 parser 596: }
1.170 parser 597:
1.297 paf 598: Value& value=*new VDouble(a_double / b_double);
599: stack.push(value);
1.54 paf 600: break;
601: }
1.55 paf 602: case OP_MOD:
603: {
1.297 paf 604: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1.170 parser 605:
1.297 paf 606: double a_double=a.as_double();
607: double b_double=b.as_double();
1.170 parser 608:
1.173 parser 609: if(b_double == 0) {
1.297 paf 610: //const String* problem_source=b.as_string();
1.223 paf 611: throw Exception("number.zerodivision",
1.297 paf 612: 0, //problem_source,
1.170 parser 613: "Modulus by zero");
1.171 parser 614: }
1.170 parser 615:
1.297 paf 616: Value& value=*new VDouble(fmod(a_double, b_double));
617: stack.push(value);
1.201 paf 618: break;
619: }
620: case OP_INTDIV:
621: {
1.297 paf 622: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1.201 paf 623:
1.297 paf 624: int a_int=a.as_int();
625: int b_int=b.as_int();
1.201 paf 626:
627: if(b_int == 0) {
1.297 paf 628: //const String* problem_source=b.as_string();
1.223 paf 629: throw Exception("number.zerodivision",
1.297 paf 630: 0, //problem_source,
1.201 paf 631: "Division by zero");
632: }
633:
1.297 paf 634: Value& value=*new VInt(a_int / b_int);
635: stack.push(value);
1.55 paf 636: break;
637: }
1.274 paf 638: case OP_BIN_SL:
639: {
1.297 paf 640: Value& b=stack.pop().value(); Value& a=stack.pop().value();
641: Value& value=*new VInt(
642: a.as_int() <<
643: b.as_int());
644: stack.push(value);
1.274 paf 645: break;
646: }
647: case OP_BIN_SR:
648: {
1.297 paf 649: Value& b=stack.pop().value(); Value& a=stack.pop().value();
650: Value& value=*new VInt(
651: a.as_int() >>
652: b.as_int());
653: stack.push(value);
1.274 paf 654: break;
655: }
1.55 paf 656: case OP_BIN_AND:
1.54 paf 657: {
1.297 paf 658: Value& b=stack.pop().value(); Value& a=stack.pop().value();
659: Value& value=*new VInt(
660: a.as_int() &
661: b.as_int());
662: stack.push(value);
1.54 paf 663: break;
664: }
1.55 paf 665: case OP_BIN_OR:
1.54 paf 666: {
1.297 paf 667: Value& b=stack.pop().value(); Value& a=stack.pop().value();
668: Value& value=*new VInt(
669: a.as_int() |
670: b.as_int());
671: stack.push(value);
1.55 paf 672: break;
673: }
1.56 paf 674: case OP_BIN_XOR:
675: {
1.297 paf 676: Value& b=stack.pop().value(); Value& a=stack.pop().value();
677: Value& value=*new VInt(
678: a.as_int() ^
679: b.as_int());
680: stack.push(value);
1.56 paf 681: break;
682: }
1.55 paf 683: case OP_LOG_AND:
684: {
1.297 paf 685: ArrayOperation& local_ops=stack.pop().ops(); Value& a=stack.pop().value();
1.263 paf 686: bool result;
1.297 paf 687: if(a.as_bool()) {
688: execute(local_ops);
689: Value& b=stack.pop().value();
690: result=b.as_bool();
1.209 paf 691: } else
1.263 paf 692: result=false;
1.297 paf 693: Value& value=*new VBool(result);
694: stack.push(value);
1.55 paf 695: break;
696: }
697: case OP_LOG_OR:
698: {
1.297 paf 699: ArrayOperation& local_ops=stack.pop().ops(); Value& a=stack.pop().value();
1.263 paf 700: bool result;
1.297 paf 701: if(a.as_bool())
1.263 paf 702: result=true;
1.209 paf 703: else {
1.297 paf 704: execute(local_ops);
705: Value& b=stack.pop().value();
706: result=b.as_bool();
1.209 paf 707: }
1.297 paf 708: Value& value=*new VBool(result);
709: stack.push(value);
1.56 paf 710: break;
711: }
712: case OP_LOG_XOR:
713: {
1.297 paf 714: Value& b=stack.pop().value(); Value& a=stack.pop().value();
715: Value& value=*new VBool(a.as_bool() ^ b.as_bool());
716: stack.push(value);
1.55 paf 717: break;
718: }
719: case OP_NUM_LT:
720: {
1.297 paf 721: volatile double b_double=stack.pop().value().as_double();
722: volatile double a_double=stack.pop().value().as_double();
723: Value& value=*new VBool(a_double<b_double);
724: stack.push(value);
1.55 paf 725: break;
726: }
727: case OP_NUM_GT:
728: {
1.297 paf 729: volatile double b_double=stack.pop().value().as_double();
730: volatile double a_double=stack.pop().value().as_double();
731: Value& value=*new VBool(a_double>b_double);
732: stack.push(value);
1.55 paf 733: break;
734: }
735: case OP_NUM_LE:
736: {
1.297 paf 737: volatile double b_double=stack.pop().value().as_double();
738: volatile double a_double=stack.pop().value().as_double();
739: Value& value=*new VBool(a_double<=b_double);
740: stack.push(value);
1.55 paf 741: break;
742: }
743: case OP_NUM_GE:
744: {
1.297 paf 745: volatile double b_double=stack.pop().value().as_double();
746: volatile double a_double=stack.pop().value().as_double();
747: Value& value=*new VBool(a_double>=b_double);
748: stack.push(value);
1.55 paf 749: break;
750: }
751: case OP_NUM_EQ:
752: {
1.297 paf 753: volatile double b_double=stack.pop().value().as_double();
754: volatile double a_double=stack.pop().value().as_double();
755: Value& value=*new VBool(a_double==b_double);
756: stack.push(value);
1.55 paf 757: break;
758: }
759: case OP_NUM_NE:
760: {
1.297 paf 761: volatile double b_double=stack.pop().value().as_double();
762: volatile double a_double=stack.pop().value().as_double();
763: Value& value=*new VBool(a_double!=b_double);
764: stack.push(value);
1.54 paf 765: break;
766: }
1.58 paf 767: case OP_STR_LT:
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: }
774: case OP_STR_GT:
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_LE:
1.58 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: }
1.55 paf 788: case OP_STR_GE:
1.54 paf 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_EQ:
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.58 paf 800: break;
801: }
802: case OP_STR_NE:
803: {
1.297 paf 804: Value& b=stack.pop().value(); Value& a=stack.pop().value();
805: Value& value=*new VBool(a.as_string() != b.as_string());
806: stack.push(value);
1.103 paf 807: break;
808: }
809: case OP_IS:
810: {
1.297 paf 811: Value& b=stack.pop().value(); Value& a=stack.pop().value();
812: Value& value=*new VBool(a.is(b.as_string().cstr()));
813: stack.push(value);
1.28 paf 814: break;
815: }
816:
1.11 paf 817: default:
1.223 paf 818: throw Exception(0,
1.67 paf 819: 0,
1.297 paf 820: "invalid opcode %d", opcode);
1.11 paf 821: }
822: }
1.306 paf 823: } catch(const Exception&) {
1.297 paf 824: // record it to stack trace
1.301 paf 825: if(debug_name)
826: exception_trace.push(Trace(debug_name, debug_origin));
1.297 paf 827: rethrow;
828: }
1.1 paf 829: }
1.17 paf 830:
1.292 paf 831: /**
832: @todo cache|prepare junctions
833: @bug ^superbase:method would dynamically call ^base:method if there is any
834: */
1.297 paf 835: Value& Request::get_element(Value& ncontext, const String& name, bool can_call_operator) {
836: Value* value=0;
1.249 paf 837: if(can_call_operator) {
1.297 paf 838: if(Method* method=main_class.get_method(name)) // looking operator of that name FIRST
1.307 paf 839: value=new VJunction(new Junction(main_class, method));
1.249 paf 840: }
841: if(!value) {
842: if(!wcontext->get_constructing() // not constructing
843: && wcontext->get_somebody_entered_some_class() ) // ^class:method
1.297 paf 844: if(VStateless_class *called_class=ncontext.get_class())
1.249 paf 845: if(VStateless_class *read_class=rcontext->get_class())
846: if(read_class->derived_from(*called_class)) // current derived from called
1.297 paf 847: if(Value* base=get_self().base()) { // doing DYNAMIC call
1.290 paf 848: Temp_derived temp_derived(*base, 0); // temporarily prevent go-back-down virtual calls
849: value=base->get_element(name, *base, false); // virtual-up lookup starting from parent
1.289 paf 850: goto value_ready;
1.249 paf 851: }
852: }
1.216 paf 853: if(!value)
1.297 paf 854: value=ncontext.get_element(name, ncontext, false);
1.291 paf 855:
856: if(value && wcontext->get_constructing())
1.297 paf 857: if(Junction* junction=value->get_junction()) {
858: if(junction->self.get_class()!=&ncontext)
1.291 paf 859: throw Exception("parser.runtime",
860: &name,
1.297 paf 861: "constructor must be declared in class '%s'",
862: ncontext.get_class()->name_cstr());
1.291 paf 863: }
1.249 paf 864:
1.289 paf 865: value_ready:
1.284 paf 866: if(value)
1.294 paf 867: value=&process_to_value(*value); // process possible code-junction
1.284 paf 868: else
1.297 paf 869: value=new VVoid;
1.63 paf 870:
1.297 paf 871: return *value;
1.309 ! paf 872: }
! 873:
! 874: void Request::put_element(Value& ncontext, const String& name, Value& value) {
! 875: // put_element can return property-setting-junction
! 876: if(const Junction* junction=ncontext.put_element(name, &value, false))
! 877: if(junction!=PUT_ELEMENT_REPLACED_ELEMENT) {
! 878: // process it
! 879: ArrayString* params_names=junction->method->params_names;
! 880: int param_count=params_names? params_names->count(): 0;
! 881: if(param_count!=1)
! 882: throw Exception("parser.runtime",
! 883: 0,
! 884: "setter method must have ONE parameter (has %d parameters)", param_count);
! 885:
! 886: VMethodFrame frame(*junction, method_frame/*caller*/);
! 887: frame.store_param(value);
! 888:
! 889: frame.set_self(frame.junction.self);
! 890:
! 891: VMethodFrame *saved_method_frame=method_frame;
! 892: Value* saved_rcontext=rcontext;
! 893: WContext *saved_wcontext=wcontext;
! 894:
! 895: rcontext=wcontext=&frame;
! 896: method_frame=&frame;
! 897:
! 898: // prevent non-string writes for better error reporting
! 899: wcontext->write(*method_frame);
! 900:
! 901: recoursion_checked_execute(*frame.junction.method->parser_code); // parser code, execute it
! 902: // we don't need it StringOrValue result=wcontext->result();
! 903:
! 904: method_frame=saved_method_frame;
! 905: wcontext=saved_wcontext;
! 906: rcontext=saved_rcontext;
! 907: }
1.34 paf 908: }
1.70 paf 909:
1.162 parser 910: /** @param intercept_string
1.116 paf 911: - true:
912: they want result=string value,
913: possible object result goes to wcontext
914: - false:
915: they want any result[string|object]
916: nothing goes to wcontext.
1.125 paf 917: used in @c (expression) params evaluation
1.224 paf 918:
919: using the fact it's either string_ or value_ result requested to speed up checkes
1.116 paf 920: */
1.229 paf 921: StringOrValue Request::process(Value& input_value, bool intercept_string) {
1.297 paf 922: Junction* junction=input_value.get_junction();
1.307 paf 923: if(junction) {
924: if(junction->is_getter) { // is it a getter-junction?
925: // process it
926:
927: VMethodFrame frame(*junction, method_frame/*caller*/);
928: if(junction->method->params_names)
929: throw Exception("parser.runtime",
930: 0,
931: "getter method must have no parameters");
932:
933: frame.set_self(frame.junction.self);
934:
935: VMethodFrame *saved_method_frame=method_frame;
936: Value* saved_rcontext=rcontext;
937: WContext *saved_wcontext=wcontext;
938:
939: rcontext=wcontext=&frame;
940: method_frame=&frame;
941:
942: recoursion_checked_execute(*frame.junction.method->parser_code); // parser code, execute it
943: StringOrValue result=wcontext->result();
944:
945: method_frame=saved_method_frame;
946: wcontext=saved_wcontext;
947: rcontext=saved_rcontext;
948:
949: return result;
950: }
951:
952: if(junction->code) { // is it a code-junction?
953: // process it
954: StringOrValue result;
1.157 parser 955: #ifdef DEBUG_EXECUTE
1.307 paf 956: debug_printf(sapi_info, "ja->\n");
1.157 parser 957: #endif
1.238 paf 958:
1.307 paf 959: if(!junction->method_frame)
960: throw Exception("parser.runtime",
1.240 paf 961: 0,
962: "junction used outside of context");
963:
1.307 paf 964: VMethodFrame *saved_method_frame=method_frame;
965: Value* saved_rcontext=rcontext;
966: WContext *saved_wcontext=wcontext;
967:
968: method_frame=junction->method_frame;
969: rcontext=junction->rcontext;
970:
971: // for expression method params
972: // wcontext is set 0
973: // using the fact in decision "which wwrapper to use"
974: bool using_code_frame=intercept_string && junction->wcontext;
975: if(using_code_frame) {
976: // almost plain wwrapper about junction wcontext,
977: // BUT intercepts string writes
978: VCodeFrame local(*junction->wcontext, junction->wcontext);
979: wcontext=&local;
980:
981: // execute it
982: recoursion_checked_execute(*junction->code);
983:
984: // CodeFrame soul:
985: // string writes were intercepted
986: // returning them as the result of getting code-junction
987: result.set_string(*wcontext->get_string());
988: } else {
989: // plain wwrapper
990: WWrapper local(0/*empty*/, wcontext);
991: wcontext=&local;
992:
993: // execute it
994: recoursion_checked_execute(*junction->code);
995:
996: result=wcontext->result();
997: }
998:
999: wcontext=saved_wcontext;
1000: rcontext=saved_rcontext;
1001: method_frame=saved_method_frame;
1.207 paf 1002:
1.157 parser 1003: #ifdef DEBUG_EXECUTE
1.307 paf 1004: debug_printf(sapi_info, "<-ja returned");
1.157 parser 1005: #endif
1.307 paf 1006: return result;
1007: }
1008:
1009: // it is then method-junction, do not explode it
1010: // just return it as we do for usual objects
1011: }
1012:
1013: return input_value;
1.85 paf 1014: }
1015:
1.298 paf 1016: StringOrValue Request::execute_method(VMethodFrame& amethod_frame, const Method& method) {
1.257 paf 1017: VMethodFrame *saved_method_frame=method_frame;
1.297 paf 1018: Value* saved_rcontext=rcontext;
1.257 paf 1019: WContext *saved_wcontext=wcontext;
1.99 paf 1020:
1021: // initialize contexts
1.286 paf 1022: rcontext=wcontext=method_frame=&amethod_frame;
1.99 paf 1023:
1024: // execute!
1025: execute(*method.parser_code);
1026:
1027: // result
1.298 paf 1028: StringOrValue result=wcontext->result();
1.208 paf 1029:
1.257 paf 1030: wcontext=saved_wcontext;
1031: rcontext=saved_rcontext;
1032: method_frame=saved_method_frame;
1.242 paf 1033:
1034: // return
1035: return result;
1.208 paf 1036: }
1037:
1.297 paf 1038: const String* Request::execute_method(Value& aself,
1039: const Method& method, VString* optional_param,
1040: bool do_return_string) {
1.257 paf 1041: VMethodFrame *saved_method_frame=method_frame;
1.297 paf 1042: Value* saved_rcontext=rcontext;
1.257 paf 1043: WContext *saved_wcontext=wcontext;
1.208 paf 1044:
1.307 paf 1045: Junction local_junction(aself, &method);
1.297 paf 1046: VMethodFrame local_frame(local_junction, method_frame/*caller*/);
1.242 paf 1047: if(optional_param && local_frame.can_store_param()) {
1.297 paf 1048: local_frame.store_param(*optional_param);
1.242 paf 1049: local_frame.fill_unspecified_params();
1050: }
1.285 paf 1051: local_frame.set_self(aself);
1.257 paf 1052: rcontext=wcontext=method_frame=&local_frame;
1.246 paf 1053:
1054: // prevent non-string writes for better error reporting
1.297 paf 1055: if(do_return_string)
1.246 paf 1056: wcontext->write(local_frame);
1.208 paf 1057:
1058: // execute!
1059: execute(*method.parser_code);
1060:
1061: // result
1.297 paf 1062: const String* result=0;
1063: if(do_return_string)
1064: result=&wcontext->result().as_string();
1.99 paf 1065:
1.257 paf 1066: wcontext=saved_wcontext;
1067: rcontext=saved_rcontext;
1068: method_frame=saved_method_frame;
1.297 paf 1069:
1070: return result;
1.242 paf 1071: }
1072:
1.297 paf 1073: Request::Execute_nonvirtual_method_result
1074: Request::execute_nonvirtual_method(VStateless_class& aclass,
1075: const String& method_name, VString* optional_param,
1076: bool do_return_string) {
1077: Execute_nonvirtual_method_result result;
1078: result.method=aclass.get_method(method_name);
1079: if(result.method)
1080: result.string=execute_method(aclass, *result.method, optional_param,
1081: do_return_string);
1082: return result;
1.99 paf 1083: }
1084:
1.297 paf 1085: const String* Request::execute_virtual_method(Value& aself,
1086: const String& method_name) {
1087: if(Value* value=aself.get_element(method_name, aself, false))
1088: if(Junction* junction=value->get_junction())
1089: if(const Method *method=junction->method)
1090: return execute_method(aself, *method, 0/*no params*/, true);
1.188 parser 1091:
1.176 parser 1092: return 0;
1093: }