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