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