|
|
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.305.6.1! paf 8: static const char * const IDENT_EXECUTE_C="$Date: 2004/02/11 15:33:15 $";
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.305.6.1! paf 38: "WRITE_VALUE", "WRITE_VALUE__EXPLICIT_RESULT", "WRITE_EXPR_RESULT",
! 39: "STRING__WRITE",
1.284 paf 40: "GET_ELEMENT_OR_OPERATOR", "GET_ELEMENT", "GET_ELEMENT__WRITE",
1.232 paf 41: "OBJECT_POOL", "STRING_POOL",
1.1 paf 42: "STORE_PARAM",
1.232 paf 43: "PREPARE_TO_CONSTRUCT_OBJECT", "PREPARE_TO_EXPRESSION",
1.305.6.1! paf 44: "CALL", "CALL__WRITE", "CALL__WRITE__EXPLICIT_RESULT",
1.49 paf 45:
46: // expression ops: unary
1.148 paf 47: "NEG", "INV", "NOT", "DEF", "IN", "FEXISTS", "DEXISTS",
1.49 paf 48: // expression ops: binary
1.201 paf 49: "SUB", "ADD", "MUL", "DIV", "MOD", "INTDIV",
1.275 paf 50: "BIN_SL", "BIN_SR",
1.56 paf 51: "BIN_AND", "BIN_OR", "BIN_XOR",
52: "LOG_AND", "LOG_OR", "LOG_XOR",
1.49 paf 53: "NUM_LT", "NUM_GT", "NUM_LE", "NUM_GE", "NUM_EQ", "NUM_NE",
1.103 paf 54: "STR_LT", "STR_GT", "STR_LE", "STR_GE", "STR_EQ", "STR_NE",
55: "IS"
1.1 paf 56: };
57:
1.297 paf 58: void va_debug_printf(SAPI_Info& sapi_info, const char* fmt,va_list args) {
1.127 paf 59: char buf[MAX_STRING];
60: vsnprintf(buf, MAX_STRING, fmt, args);
1.297 paf 61: SAPI::log(sapi_info, "%s", buf);
1.107 paf 62: }
63:
1.297 paf 64: void debug_printf(SAPI_Info& sapi_info, const char* fmt, ...) {
1.107 paf 65: va_list args;
1.297 paf 66: va_start(args, fmt);
67: va_debug_printf(sapi_info, fmt, args);
1.107 paf 68: va_end(args);
1.105 paf 69: }
70:
1.297 paf 71: void debug_dump(SAPI_Info& sapi_info, int level, ArrayOperation& ops) {
72: Array_iterator<Operation> i(ops);
1.190 parser 73: while(i.has_next()) {
1.297 paf 74: OPCODE opcode=i.next().code;
1.1 paf 75:
1.297 paf 76: if(opcode==OP_VALUE || opcode==OP_STRING__WRITE) {
77: Operation::Origin origin=i.next().origin;
78: Value& value=*i.next().value;
79: debug_printf(sapi_info,
1.133 paf 80: "%*s%s"
81: " \"%s\" %s",
1.297 paf 82: level*4, "", opcode_name[opcode],
83: value.get_string()->cstr(), value.type());
1.133 paf 84: continue;
1.15 paf 85: }
1.297 paf 86: debug_printf(sapi_info, "%*s%s", level*4, "", opcode_name[opcode]);
1.1 paf 87:
1.297 paf 88: switch(opcode) {
1.184 parser 89: case OP_CURLY_CODE__STORE_PARAM:
90: case OP_EXPR_CODE__STORE_PARAM:
91: case OP_CURLY_CODE__CONSTRUCT:
1.209 paf 92: case OP_NESTED_CODE:
1.226 paf 93: case OP_OBJECT_POOL:
94: case OP_STRING_POOL:
1.305.6.1! paf 95: case OP_CALL: case OP_CALL__WRITE: case OP_CALL__WRITE__EXPLICIT_RESULT:
1.297 paf 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.305.6.1! paf 238: case OP_WRITE_VALUE__EXPLICIT_RESULT:
! 239: {
! 240: //todo change me
! 241: Value& value=stack.pop().value();
! 242: write_assign_lang(value);
! 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.305.6.1! paf 386: case OP_CALL__WRITE__EXPLICIT_RESULT:
1.29 paf 387: {
1.297 paf 388: //is_debug_junction=true;
389: ArrayOperation* local_ops=i.next().ops;
1.157 parser 390: #ifdef DEBUG_EXECUTE
1.297 paf 391: debug_printf(sapi_info, " (%d)\n", local_ops?local_ops->count():0);
392: if(local_ops)
393: debug_dump(sapi_info, 1, *local_ops);
1.237 paf 394:
1.297 paf 395: debug_printf(sapi_info, "->\n");
1.157 parser 396: #endif
1.297 paf 397: Value& value=stack.pop().value();
1.237 paf 398:
1.297 paf 399: Junction* junction=value.get_junction();
400: if(!junction) {
401: if(value.is("void"))
402: throw Exception("parser.runtime",
403: 0,
404: "undefined method");
405: else
406: throw Exception("parser.runtime",
407: 0,
408: "is '%s', not a method or junction, can not call it",
409: value.type());
410: }
1.282 paf 411: /* no check needed, code compiled the way that that's impossible
1.276 paf 412: // check:
413: // that this is method-junction, not a code-junction
1.278 paf 414: // [disabling these contstructions:]
415: // $junction{code}
416: // ^junction[]
1.276 paf 417: if(!junction->method)
418: throw Exception("parser.runtime",
1.297 paf 419:
420: "is '%s', it is code junction, can not call it",
421: value.type());
1.282 paf 422: */
1.237 paf 423:
1.297 paf 424: VMethodFrame frame(*junction, method_frame/*caller*/);
1.237 paf 425: if(local_ops){ // store param code goes here
1.297 paf 426: stack.push(frame); // argument for *STORE_PARAM ops
1.237 paf 427: execute(*local_ops);
1.297 paf 428: stack.pop().value();
1.237 paf 429: }
430: frame.fill_unspecified_params();
1.257 paf 431: VMethodFrame *saved_method_frame=method_frame;
1.297 paf 432: Value* saved_rcontext=rcontext;
1.257 paf 433: WContext *saved_wcontext=wcontext;
1.45 paf 434:
1.297 paf 435: VStateless_class& called_class=*frame.junction.self.get_class();
436: Value* new_self=&get_self();
1.200 paf 437: if(wcontext->get_constructing()) {
438: wcontext->set_constructing(false);
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
483: recoursion_checked_execute(/*frame.name(), */*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.305.6.1! paf 497: switch(opcode) {
! 498: case OP_CALL__WRITE:
! 499: write_assign_lang(result);
! 500: break;
! 501: case OP_CALL:
! 502: stack.push(result.as_value());
! 503: break;
! 504: default: //case OP_CALL__WRITE__EXPLICIT_RESULT:
! 505: if(const String* string=result.get_string()) {
! 506: if(!string->is_empty())
! 507: throw Exception("parser.runtime",
! 508: 0,
! 509: "returned some nonempty string, while it should not (explicit result)");
! 510: } else // result.object
! 511: throw Exception("parser.runtime",
! 512: 0,
! 513: "returned some object, while it should not (explicit result)");
! 514: break;
1.232 paf 515: }
516:
1.157 parser 517: #ifdef DEBUG_EXECUTE
1.297 paf 518: debug_printf(sapi_info, "<-returned");
1.157 parser 519: #endif
1.297 paf 520: //is_debug_junction=false;
1.49 paf 521: break;
522: }
523:
1.55 paf 524: // expression ops: unary
525: case OP_NEG:
526: {
1.297 paf 527: Value& a=stack.pop().value();
528: Value& value=*new VDouble(-a.as_double());
529: stack.push(value);
1.55 paf 530: break;
531: }
532: case OP_INV:
533: {
1.297 paf 534: Value& a=stack.pop().value();
535: Value& value=*new VDouble(~a.as_int());
536: stack.push(value);
1.55 paf 537: break;
538: }
539: case OP_NOT:
540: {
1.297 paf 541: Value& a=stack.pop().value();
542: Value& value=*new VBool(!a.as_bool());
543: stack.push(value);
1.55 paf 544: break;
545: }
1.62 paf 546: case OP_DEF:
547: {
1.297 paf 548: Value& a=stack.pop().value();
549: Value& value=*new VBool(a.is_defined());
550: stack.push(value);
1.62 paf 551: break;
552: }
553: case OP_IN:
554: {
1.297 paf 555: Value& a=stack.pop().value();
556: const String& path=a.as_string();
557: Value& value=*new VBool(request_info.uri && *request_info.uri && path.this_starts(request_info.uri));
558: stack.push(value);
1.62 paf 559: break;
560: }
561: case OP_FEXISTS:
562: {
1.297 paf 563: Value& a=stack.pop().value();
564: Value& value=*new VBool(file_readable(absolute(a.as_string())));
565: stack.push(value);
1.148 paf 566: break;
567: }
568: case OP_DEXISTS:
569: {
1.297 paf 570: Value& a=stack.pop().value();
571: Value& value=*new VBool(dir_readable(absolute(a.as_string())));
572: stack.push(value);
1.62 paf 573: break;
574: }
1.55 paf 575:
576: // expression ops: binary
577: case OP_SUB:
1.53 paf 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: }
1.55 paf 584: case OP_ADD:
1.53 paf 585: {
1.297 paf 586: Value& b=stack.pop().value(); Value& a=stack.pop().value();
587: Value& value=*new VDouble(a.as_double() + b.as_double());
588: stack.push(value);
1.53 paf 589: break;
590: }
1.49 paf 591: case OP_MUL:
592: {
1.297 paf 593: Value& b=stack.pop().value(); Value& a=stack.pop().value();
594: Value& value=*new VDouble(a.as_double() * b.as_double());
595: stack.push(value);
1.53 paf 596: break;
597: }
598: case OP_DIV:
599: {
1.297 paf 600: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1.170 parser 601:
1.297 paf 602: double a_double=a.as_double();
603: double b_double=b.as_double();
1.170 parser 604:
1.171 parser 605: if(b_double == 0) {
1.297 paf 606: //const String* problem_source=b.as_string();
1.223 paf 607: throw Exception("number.zerodivision",
1.297 paf 608: 0, //problem_source,
1.170 parser 609: "Division by zero");
1.171 parser 610: }
1.170 parser 611:
1.297 paf 612: Value& value=*new VDouble(a_double / b_double);
613: stack.push(value);
1.54 paf 614: break;
615: }
1.55 paf 616: case OP_MOD:
617: {
1.297 paf 618: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1.170 parser 619:
1.297 paf 620: double a_double=a.as_double();
621: double b_double=b.as_double();
1.170 parser 622:
1.173 parser 623: if(b_double == 0) {
1.297 paf 624: //const String* problem_source=b.as_string();
1.223 paf 625: throw Exception("number.zerodivision",
1.297 paf 626: 0, //problem_source,
1.170 parser 627: "Modulus by zero");
1.171 parser 628: }
1.170 parser 629:
1.297 paf 630: Value& value=*new VDouble(fmod(a_double, b_double));
631: stack.push(value);
1.201 paf 632: break;
633: }
634: case OP_INTDIV:
635: {
1.297 paf 636: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1.201 paf 637:
1.297 paf 638: int a_int=a.as_int();
639: int b_int=b.as_int();
1.201 paf 640:
641: if(b_int == 0) {
1.297 paf 642: //const String* problem_source=b.as_string();
1.223 paf 643: throw Exception("number.zerodivision",
1.297 paf 644: 0, //problem_source,
1.201 paf 645: "Division by zero");
646: }
647:
1.297 paf 648: Value& value=*new VInt(a_int / b_int);
649: stack.push(value);
1.55 paf 650: break;
651: }
1.274 paf 652: case OP_BIN_SL:
653: {
1.297 paf 654: Value& b=stack.pop().value(); Value& a=stack.pop().value();
655: Value& value=*new VInt(
656: a.as_int() <<
657: b.as_int());
658: stack.push(value);
1.274 paf 659: break;
660: }
661: case OP_BIN_SR:
662: {
1.297 paf 663: Value& b=stack.pop().value(); Value& a=stack.pop().value();
664: Value& value=*new VInt(
665: a.as_int() >>
666: b.as_int());
667: stack.push(value);
1.274 paf 668: break;
669: }
1.55 paf 670: case OP_BIN_AND:
1.54 paf 671: {
1.297 paf 672: Value& b=stack.pop().value(); Value& a=stack.pop().value();
673: Value& value=*new VInt(
674: a.as_int() &
675: b.as_int());
676: stack.push(value);
1.54 paf 677: break;
678: }
1.55 paf 679: case OP_BIN_OR:
1.54 paf 680: {
1.297 paf 681: Value& b=stack.pop().value(); Value& a=stack.pop().value();
682: Value& value=*new VInt(
683: a.as_int() |
684: b.as_int());
685: stack.push(value);
1.55 paf 686: break;
687: }
1.56 paf 688: case OP_BIN_XOR:
689: {
1.297 paf 690: Value& b=stack.pop().value(); Value& a=stack.pop().value();
691: Value& value=*new VInt(
692: a.as_int() ^
693: b.as_int());
694: stack.push(value);
1.56 paf 695: break;
696: }
1.55 paf 697: case OP_LOG_AND:
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()) {
702: execute(local_ops);
703: Value& b=stack.pop().value();
704: result=b.as_bool();
1.209 paf 705: } else
1.263 paf 706: result=false;
1.297 paf 707: Value& value=*new VBool(result);
708: stack.push(value);
1.55 paf 709: break;
710: }
711: case OP_LOG_OR:
712: {
1.297 paf 713: ArrayOperation& local_ops=stack.pop().ops(); Value& a=stack.pop().value();
1.263 paf 714: bool result;
1.297 paf 715: if(a.as_bool())
1.263 paf 716: result=true;
1.209 paf 717: else {
1.297 paf 718: execute(local_ops);
719: Value& b=stack.pop().value();
720: result=b.as_bool();
1.209 paf 721: }
1.297 paf 722: Value& value=*new VBool(result);
723: stack.push(value);
1.56 paf 724: break;
725: }
726: case OP_LOG_XOR:
727: {
1.297 paf 728: Value& b=stack.pop().value(); Value& a=stack.pop().value();
729: Value& value=*new VBool(a.as_bool() ^ b.as_bool());
730: stack.push(value);
1.55 paf 731: break;
732: }
733: case OP_NUM_LT:
734: {
1.297 paf 735: volatile double b_double=stack.pop().value().as_double();
736: volatile double a_double=stack.pop().value().as_double();
737: Value& value=*new VBool(a_double<b_double);
738: stack.push(value);
1.55 paf 739: break;
740: }
741: case OP_NUM_GT:
742: {
1.297 paf 743: volatile double b_double=stack.pop().value().as_double();
744: volatile double a_double=stack.pop().value().as_double();
745: Value& value=*new VBool(a_double>b_double);
746: stack.push(value);
1.55 paf 747: break;
748: }
749: case OP_NUM_LE:
750: {
1.297 paf 751: volatile double b_double=stack.pop().value().as_double();
752: volatile double a_double=stack.pop().value().as_double();
753: Value& value=*new VBool(a_double<=b_double);
754: stack.push(value);
1.55 paf 755: break;
756: }
757: case OP_NUM_GE:
758: {
1.297 paf 759: volatile double b_double=stack.pop().value().as_double();
760: volatile double a_double=stack.pop().value().as_double();
761: Value& value=*new VBool(a_double>=b_double);
762: stack.push(value);
1.55 paf 763: break;
764: }
765: case OP_NUM_EQ:
766: {
1.297 paf 767: volatile double b_double=stack.pop().value().as_double();
768: volatile double a_double=stack.pop().value().as_double();
769: Value& value=*new VBool(a_double==b_double);
770: stack.push(value);
1.55 paf 771: break;
772: }
773: case OP_NUM_NE:
774: {
1.297 paf 775: volatile double b_double=stack.pop().value().as_double();
776: volatile double a_double=stack.pop().value().as_double();
777: Value& value=*new VBool(a_double!=b_double);
778: stack.push(value);
1.54 paf 779: break;
780: }
1.58 paf 781: case OP_STR_LT:
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_GT:
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: }
1.55 paf 795: case OP_STR_LE:
1.58 paf 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: }
1.55 paf 802: case OP_STR_GE:
1.54 paf 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.58 paf 807: break;
808: }
809: case OP_STR_EQ:
810: {
1.297 paf 811: Value& b=stack.pop().value(); Value& a=stack.pop().value();
812: Value& value=*new VBool(a.as_string() == b.as_string());
813: stack.push(value);
1.58 paf 814: break;
815: }
816: case OP_STR_NE:
817: {
1.297 paf 818: Value& b=stack.pop().value(); Value& a=stack.pop().value();
819: Value& value=*new VBool(a.as_string() != b.as_string());
820: stack.push(value);
1.103 paf 821: break;
822: }
823: case OP_IS:
824: {
1.297 paf 825: Value& b=stack.pop().value(); Value& a=stack.pop().value();
826: Value& value=*new VBool(a.is(b.as_string().cstr()));
827: stack.push(value);
1.28 paf 828: break;
829: }
830:
1.11 paf 831: default:
1.223 paf 832: throw Exception(0,
1.67 paf 833: 0,
1.297 paf 834: "invalid opcode %d", opcode);
1.11 paf 835: }
836: }
1.297 paf 837: } catch(...) {
838: // record it to stack trace
1.301 paf 839: if(debug_name)
840: exception_trace.push(Trace(debug_name, debug_origin));
1.297 paf 841: rethrow;
842: }
1.1 paf 843: }
1.17 paf 844:
1.292 paf 845: /**
846: @todo cache|prepare junctions
847: @bug ^superbase:method would dynamically call ^base:method if there is any
848: */
1.297 paf 849: Value& Request::get_element(Value& ncontext, const String& name, bool can_call_operator) {
850: Value* value=0;
1.249 paf 851: if(can_call_operator) {
1.297 paf 852: if(Method* method=main_class.get_method(name)) // looking operator of that name FIRST
853: value=new VJunction(new Junction(
854: main_class, method, 0,0,0, 0));
1.249 paf 855: }
856: if(!value) {
857: if(!wcontext->get_constructing() // not constructing
858: && wcontext->get_somebody_entered_some_class() ) // ^class:method
1.297 paf 859: if(VStateless_class *called_class=ncontext.get_class())
1.249 paf 860: if(VStateless_class *read_class=rcontext->get_class())
861: if(read_class->derived_from(*called_class)) // current derived from called
1.297 paf 862: if(Value* base=get_self().base()) { // doing DYNAMIC call
1.290 paf 863: Temp_derived temp_derived(*base, 0); // temporarily prevent go-back-down virtual calls
864: value=base->get_element(name, *base, false); // virtual-up lookup starting from parent
1.289 paf 865: goto value_ready;
1.249 paf 866: }
867: }
1.216 paf 868: if(!value)
1.297 paf 869: value=ncontext.get_element(name, ncontext, false);
1.291 paf 870:
871: if(value && wcontext->get_constructing())
1.297 paf 872: if(Junction* junction=value->get_junction()) {
873: if(junction->self.get_class()!=&ncontext)
1.291 paf 874: throw Exception("parser.runtime",
875: &name,
1.297 paf 876: "constructor must be declared in class '%s'",
877: ncontext.get_class()->name_cstr());
1.291 paf 878: }
1.249 paf 879:
1.289 paf 880: value_ready:
1.284 paf 881: if(value)
1.294 paf 882: value=&process_to_value(*value); // process possible code-junction
1.284 paf 883: else
1.297 paf 884: value=new VVoid;
1.63 paf 885:
1.297 paf 886: return *value;
1.34 paf 887: }
1.70 paf 888:
1.162 parser 889: /** @param intercept_string
1.116 paf 890: - true:
891: they want result=string value,
892: possible object result goes to wcontext
893: - false:
894: they want any result[string|object]
895: nothing goes to wcontext.
1.125 paf 896: used in @c (expression) params evaluation
1.224 paf 897:
898: using the fact it's either string_ or value_ result requested to speed up checkes
1.116 paf 899: */
1.229 paf 900: StringOrValue Request::process(Value& input_value, bool intercept_string) {
1.228 paf 901: StringOrValue result;
1.297 paf 902: Junction* junction=input_value.get_junction();
1.70 paf 903: if(junction && junction->code) { // is it a code-junction?
1.92 paf 904: // process it
1.157 parser 905: #ifdef DEBUG_EXECUTE
1.297 paf 906: debug_printf(sapi_info, "ja->\n");
1.157 parser 907: #endif
1.238 paf 908:
1.257 paf 909: if(!junction->method_frame)
1.240 paf 910: throw Exception("parser.runtime",
911: 0,
912: "junction used outside of context");
913:
1.257 paf 914: VMethodFrame *saved_method_frame=method_frame;
1.297 paf 915: Value* saved_rcontext=rcontext;
1.257 paf 916: WContext *saved_wcontext=wcontext;
1.240 paf 917:
1.257 paf 918: method_frame=junction->method_frame;
1.240 paf 919: rcontext=junction->rcontext;
1.225 paf 920:
1.109 paf 921: // for expression method params
922: // wcontext is set 0
923: // using the fact in decision "which wwrapper to use"
1.240 paf 924: bool using_code_frame=intercept_string && junction->wcontext;
1.109 paf 925: if(using_code_frame) {
1.71 paf 926: // almost plain wwrapper about junction wcontext,
927: // BUT intercepts string writes
1.297 paf 928: VCodeFrame local(*junction->wcontext, junction->wcontext);
1.225 paf 929: wcontext=&local;
1.207 paf 930:
1.225 paf 931: // execute it
1.303 paf 932: recoursion_checked_execute(*junction->code);
1.226 paf 933:
1.71 paf 934: // CodeFrame soul:
935: // string writes were intercepted
936: // returning them as the result of getting code-junction
1.229 paf 937: result.set_string(*wcontext->get_string());
1.224 paf 938: } else {
1.225 paf 939: // plain wwrapper
1.297 paf 940: WWrapper local(0/*empty*/, wcontext);
1.225 paf 941: wcontext=&local;
942:
943: // execute it
1.303 paf 944: recoursion_checked_execute(*junction->code);
1.226 paf 945:
1.228 paf 946: result=wcontext->result();
1.224 paf 947: }
1.70 paf 948:
1.257 paf 949: wcontext=saved_wcontext;
950: rcontext=saved_rcontext;
951: method_frame=saved_method_frame;
1.70 paf 952:
1.157 parser 953: #ifdef DEBUG_EXECUTE
1.297 paf 954: debug_printf(sapi_info, "<-ja returned");
1.157 parser 955: #endif
1.224 paf 956: } else {
1.228 paf 957: result.set_value(input_value);
1.224 paf 958: }
1.228 paf 959: return result;
1.85 paf 960: }
961:
1.298 paf 962: StringOrValue Request::execute_method(VMethodFrame& amethod_frame, const Method& method) {
1.257 paf 963: VMethodFrame *saved_method_frame=method_frame;
1.297 paf 964: Value* saved_rcontext=rcontext;
1.257 paf 965: WContext *saved_wcontext=wcontext;
1.99 paf 966:
967: // initialize contexts
1.286 paf 968: rcontext=wcontext=method_frame=&amethod_frame;
1.99 paf 969:
970: // execute!
971: execute(*method.parser_code);
972:
973: // result
1.298 paf 974: StringOrValue result=wcontext->result();
1.208 paf 975:
1.257 paf 976: wcontext=saved_wcontext;
977: rcontext=saved_rcontext;
978: method_frame=saved_method_frame;
1.242 paf 979:
980: // return
981: return result;
1.208 paf 982: }
983:
1.297 paf 984: const String* Request::execute_method(Value& aself,
985: const Method& method, VString* optional_param,
986: bool do_return_string) {
1.257 paf 987: VMethodFrame *saved_method_frame=method_frame;
1.297 paf 988: Value* saved_rcontext=rcontext;
1.257 paf 989: WContext *saved_wcontext=wcontext;
1.208 paf 990:
1.297 paf 991: Junction local_junction(aself, &method, 0,0,0, 0);
992: VMethodFrame local_frame(local_junction, method_frame/*caller*/);
1.242 paf 993: if(optional_param && local_frame.can_store_param()) {
1.297 paf 994: local_frame.store_param(*optional_param);
1.242 paf 995: local_frame.fill_unspecified_params();
996: }
1.285 paf 997: local_frame.set_self(aself);
1.257 paf 998: rcontext=wcontext=method_frame=&local_frame;
1.246 paf 999:
1000: // prevent non-string writes for better error reporting
1.297 paf 1001: if(do_return_string)
1.246 paf 1002: wcontext->write(local_frame);
1.208 paf 1003:
1004: // execute!
1005: execute(*method.parser_code);
1006:
1007: // result
1.297 paf 1008: const String* result=0;
1009: if(do_return_string)
1010: result=&wcontext->result().as_string();
1.99 paf 1011:
1.257 paf 1012: wcontext=saved_wcontext;
1013: rcontext=saved_rcontext;
1014: method_frame=saved_method_frame;
1.297 paf 1015:
1016: return result;
1.242 paf 1017: }
1018:
1.297 paf 1019: Request::Execute_nonvirtual_method_result
1020: Request::execute_nonvirtual_method(VStateless_class& aclass,
1021: const String& method_name, VString* optional_param,
1022: bool do_return_string) {
1023: Execute_nonvirtual_method_result result;
1024: result.method=aclass.get_method(method_name);
1025: if(result.method)
1026: result.string=execute_method(aclass, *result.method, optional_param,
1027: do_return_string);
1028: return result;
1.99 paf 1029: }
1030:
1.297 paf 1031: const String* Request::execute_virtual_method(Value& aself,
1032: const String& method_name) {
1033: if(Value* value=aself.get_element(method_name, aself, false))
1034: if(Junction* junction=value->get_junction())
1035: if(const Method *method=junction->method)
1036: return execute_method(aself, *method, 0/*no params*/, true);
1.188 parser 1037:
1.176 parser 1038: return 0;
1039: }