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