|
|
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.324 ! misha 8: static const char * const IDENT_EXECUTE_C="$Date: 2008-08-19 14:24:29 $";
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.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.322 misha 231: case OP::OP_NESTED_CODE:
1.209 paf 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.322 misha 241: case OP::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.322 misha 247: case OP::OP_WRITE_EXPR_RESULT:
1.108 paf 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.322 misha 257: case OP::OP_STRING__WRITE:
1.86 paf 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.322 misha 268: case OP::OP_GET_ELEMENT_OR_OPERATOR:
1.215 paf 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.322 misha 276: case OP::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.322 misha 285: case OP::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.322 misha 295: case OP::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.322 misha 313: case OP::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.324 ! misha 329: value=VVoid::get();
1.297 paf 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.322 misha 337: case OP::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.322 misha 345: case OP::OP_CURLY_CODE__STORE_PARAM:
346: case OP::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.322 misha 366: opcode==OP::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.322 misha 374: case OP::OP_PREPARE_TO_CONSTRUCT_OBJECT:
1.186 parser 375: {
1.200 paf 376: wcontext->set_constructing(true);
1.186 parser 377: break;
378: }
1.219 paf 379:
1.322 misha 380: case OP::OP_PREPARE_TO_EXPRESSION:
1.219 paf 381: {
382: wcontext->set_in_expression(true);
383: break;
384: }
385:
1.322 misha 386: case OP::OP_CALL:
387: case OP::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: "is '%s', it is code junction, can not call it",
421: value.type());
1.282 paf 422: */
1.237 paf 423:
1.321 misha 424: VMethodFrame frame(*junction, method_frame);
425: if(local_ops){ // store param code goes here
426: stack.push(frame); // argument for *STORE_PARAM ops
427: execute(*local_ops);
428: stack.pop().value();
429: }
430: WContext* result_wcontext=op_call(frame);
1.322 misha 431: if(opcode==OP::OP_CALL__WRITE) {
1.321 misha 432: write_assign_lang(result_wcontext->result());
433: } else { // OP_CALL
434: stack.push(result_wcontext->result().as_value());
1.237 paf 435: }
1.220 paf 436:
1.157 parser 437: #ifdef DEBUG_EXECUTE
1.297 paf 438: debug_printf(sapi_info, "<-returned");
1.157 parser 439: #endif
1.297 paf 440: //is_debug_junction=false;
1.49 paf 441: break;
442: }
443:
1.55 paf 444: // expression ops: unary
1.322 misha 445: case OP::OP_NEG:
1.55 paf 446: {
1.297 paf 447: Value& a=stack.pop().value();
448: Value& value=*new VDouble(-a.as_double());
449: stack.push(value);
1.55 paf 450: break;
451: }
1.322 misha 452: case OP::OP_INV:
1.55 paf 453: {
1.297 paf 454: Value& a=stack.pop().value();
455: Value& value=*new VDouble(~a.as_int());
456: stack.push(value);
1.55 paf 457: break;
458: }
1.322 misha 459: case OP::OP_NOT:
1.55 paf 460: {
1.297 paf 461: Value& a=stack.pop().value();
462: Value& value=*new VBool(!a.as_bool());
463: stack.push(value);
1.55 paf 464: break;
465: }
1.322 misha 466: case OP::OP_DEF:
1.62 paf 467: {
1.297 paf 468: Value& a=stack.pop().value();
469: Value& value=*new VBool(a.is_defined());
470: stack.push(value);
1.62 paf 471: break;
472: }
1.322 misha 473: case OP::OP_IN:
1.62 paf 474: {
1.297 paf 475: Value& a=stack.pop().value();
476: const String& path=a.as_string();
477: Value& value=*new VBool(request_info.uri && *request_info.uri && path.this_starts(request_info.uri));
478: stack.push(value);
1.62 paf 479: break;
480: }
1.322 misha 481: case OP::OP_FEXISTS:
1.62 paf 482: {
1.297 paf 483: Value& a=stack.pop().value();
1.314 paf 484: Value& value=*new VBool(file_exist(absolute(a.as_string())));
1.297 paf 485: stack.push(value);
1.148 paf 486: break;
487: }
1.322 misha 488: case OP::OP_DEXISTS:
1.148 paf 489: {
1.297 paf 490: Value& a=stack.pop().value();
1.314 paf 491: Value& value=*new VBool(dir_exists(absolute(a.as_string())));
1.297 paf 492: stack.push(value);
1.62 paf 493: break;
494: }
1.55 paf 495:
496: // expression ops: binary
1.322 misha 497: case OP::OP_SUB:
1.53 paf 498: {
1.297 paf 499: Value& b=stack.pop().value(); Value& a=stack.pop().value();
500: Value& value=*new VDouble(a.as_double() - b.as_double());
501: stack.push(value);
1.53 paf 502: break;
503: }
1.322 misha 504: case OP::OP_ADD:
1.53 paf 505: {
1.297 paf 506: Value& b=stack.pop().value(); Value& a=stack.pop().value();
507: Value& value=*new VDouble(a.as_double() + b.as_double());
508: stack.push(value);
1.53 paf 509: break;
510: }
1.322 misha 511: case OP::OP_MUL:
1.49 paf 512: {
1.297 paf 513: Value& b=stack.pop().value(); Value& a=stack.pop().value();
514: Value& value=*new VDouble(a.as_double() * b.as_double());
515: stack.push(value);
1.53 paf 516: break;
517: }
1.322 misha 518: case OP::OP_DIV:
1.53 paf 519: {
1.297 paf 520: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1.170 parser 521:
1.297 paf 522: double a_double=a.as_double();
523: double b_double=b.as_double();
1.170 parser 524:
1.171 parser 525: if(b_double == 0) {
1.297 paf 526: //const String* problem_source=b.as_string();
1.223 paf 527: throw Exception("number.zerodivision",
1.297 paf 528: 0, //problem_source,
1.170 parser 529: "Division by zero");
1.171 parser 530: }
1.170 parser 531:
1.297 paf 532: Value& value=*new VDouble(a_double / b_double);
533: stack.push(value);
1.54 paf 534: break;
535: }
1.322 misha 536: case OP::OP_MOD:
1.55 paf 537: {
1.297 paf 538: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1.170 parser 539:
1.297 paf 540: double a_double=a.as_double();
541: double b_double=b.as_double();
1.170 parser 542:
1.173 parser 543: if(b_double == 0) {
1.297 paf 544: //const String* problem_source=b.as_string();
1.223 paf 545: throw Exception("number.zerodivision",
1.297 paf 546: 0, //problem_source,
1.170 parser 547: "Modulus by zero");
1.171 parser 548: }
1.170 parser 549:
1.297 paf 550: Value& value=*new VDouble(fmod(a_double, b_double));
551: stack.push(value);
1.201 paf 552: break;
553: }
1.322 misha 554: case OP::OP_INTDIV:
1.201 paf 555: {
1.297 paf 556: Value& b=stack.pop().value(); Value& a=stack.pop().value();
1.201 paf 557:
1.297 paf 558: int a_int=a.as_int();
559: int b_int=b.as_int();
1.201 paf 560:
561: if(b_int == 0) {
1.297 paf 562: //const String* problem_source=b.as_string();
1.223 paf 563: throw Exception("number.zerodivision",
1.297 paf 564: 0, //problem_source,
1.201 paf 565: "Division by zero");
566: }
567:
1.297 paf 568: Value& value=*new VInt(a_int / b_int);
569: stack.push(value);
1.55 paf 570: break;
571: }
1.322 misha 572: case OP::OP_BIN_SL:
1.274 paf 573: {
1.297 paf 574: Value& b=stack.pop().value(); Value& a=stack.pop().value();
575: Value& value=*new VInt(
576: a.as_int() <<
577: b.as_int());
578: stack.push(value);
1.274 paf 579: break;
580: }
1.322 misha 581: case OP::OP_BIN_SR:
1.274 paf 582: {
1.297 paf 583: Value& b=stack.pop().value(); Value& a=stack.pop().value();
584: Value& value=*new VInt(
585: a.as_int() >>
586: b.as_int());
587: stack.push(value);
1.274 paf 588: break;
589: }
1.322 misha 590: case OP::OP_BIN_AND:
1.54 paf 591: {
1.297 paf 592: Value& b=stack.pop().value(); Value& a=stack.pop().value();
593: Value& value=*new VInt(
594: a.as_int() &
595: b.as_int());
596: stack.push(value);
1.54 paf 597: break;
598: }
1.322 misha 599: case OP::OP_BIN_OR:
1.54 paf 600: {
1.297 paf 601: Value& b=stack.pop().value(); Value& a=stack.pop().value();
602: Value& value=*new VInt(
603: a.as_int() |
604: b.as_int());
605: stack.push(value);
1.55 paf 606: break;
607: }
1.322 misha 608: case OP::OP_BIN_XOR:
1.56 paf 609: {
1.297 paf 610: Value& b=stack.pop().value(); Value& a=stack.pop().value();
611: Value& value=*new VInt(
612: a.as_int() ^
613: b.as_int());
614: stack.push(value);
1.56 paf 615: break;
616: }
1.322 misha 617: case OP::OP_LOG_AND:
1.55 paf 618: {
1.297 paf 619: ArrayOperation& local_ops=stack.pop().ops(); Value& a=stack.pop().value();
1.263 paf 620: bool result;
1.297 paf 621: if(a.as_bool()) {
622: execute(local_ops);
623: Value& b=stack.pop().value();
624: result=b.as_bool();
1.209 paf 625: } else
1.263 paf 626: result=false;
1.297 paf 627: Value& value=*new VBool(result);
628: stack.push(value);
1.55 paf 629: break;
630: }
1.322 misha 631: case OP::OP_LOG_OR:
1.55 paf 632: {
1.297 paf 633: ArrayOperation& local_ops=stack.pop().ops(); Value& a=stack.pop().value();
1.263 paf 634: bool result;
1.297 paf 635: if(a.as_bool())
1.263 paf 636: result=true;
1.209 paf 637: else {
1.297 paf 638: execute(local_ops);
639: Value& b=stack.pop().value();
640: result=b.as_bool();
1.209 paf 641: }
1.297 paf 642: Value& value=*new VBool(result);
643: stack.push(value);
1.56 paf 644: break;
645: }
1.322 misha 646: case OP::OP_LOG_XOR:
1.56 paf 647: {
1.297 paf 648: Value& b=stack.pop().value(); Value& a=stack.pop().value();
649: Value& value=*new VBool(a.as_bool() ^ b.as_bool());
650: stack.push(value);
1.55 paf 651: break;
652: }
1.322 misha 653: case OP::OP_NUM_LT:
1.55 paf 654: {
1.297 paf 655: volatile double b_double=stack.pop().value().as_double();
656: volatile double a_double=stack.pop().value().as_double();
657: Value& value=*new VBool(a_double<b_double);
658: stack.push(value);
1.55 paf 659: break;
660: }
1.322 misha 661: case OP::OP_NUM_GT:
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_LE:
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_GE:
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_EQ:
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_NE:
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.54 paf 699: break;
700: }
1.322 misha 701: case OP::OP_STR_LT:
1.58 paf 702: {
1.297 paf 703: Value& b=stack.pop().value(); Value& a=stack.pop().value();
704: Value& value=*new VBool(a.as_string() < b.as_string());
705: stack.push(value);
1.58 paf 706: break;
707: }
1.322 misha 708: case OP::OP_STR_GT:
1.58 paf 709: {
1.297 paf 710: Value& b=stack.pop().value(); Value& a=stack.pop().value();
711: Value& value=*new VBool(a.as_string() > b.as_string());
712: stack.push(value);
1.58 paf 713: break;
714: }
1.322 misha 715: case OP::OP_STR_LE:
1.58 paf 716: {
1.297 paf 717: Value& b=stack.pop().value(); Value& a=stack.pop().value();
718: Value& value=*new VBool(a.as_string() <= b.as_string());
719: stack.push(value);
1.58 paf 720: break;
721: }
1.322 misha 722: case OP::OP_STR_GE:
1.54 paf 723: {
1.297 paf 724: Value& b=stack.pop().value(); Value& a=stack.pop().value();
725: Value& value=*new VBool(a.as_string() >= b.as_string());
726: stack.push(value);
1.58 paf 727: break;
728: }
1.322 misha 729: case OP::OP_STR_EQ:
1.58 paf 730: {
1.297 paf 731: Value& b=stack.pop().value(); Value& a=stack.pop().value();
732: Value& value=*new VBool(a.as_string() == b.as_string());
733: stack.push(value);
1.58 paf 734: break;
735: }
1.322 misha 736: case OP::OP_STR_NE:
1.58 paf 737: {
1.297 paf 738: Value& b=stack.pop().value(); Value& a=stack.pop().value();
739: Value& value=*new VBool(a.as_string() != b.as_string());
740: stack.push(value);
1.103 paf 741: break;
742: }
1.322 misha 743: case OP::OP_IS:
1.103 paf 744: {
1.297 paf 745: Value& b=stack.pop().value(); Value& a=stack.pop().value();
746: Value& value=*new VBool(a.is(b.as_string().cstr()));
747: stack.push(value);
1.28 paf 748: break;
749: }
750:
1.11 paf 751: default:
1.223 paf 752: throw Exception(0,
1.67 paf 753: 0,
1.297 paf 754: "invalid opcode %d", opcode);
1.11 paf 755: }
756: }
1.306 paf 757: } catch(const Exception&) {
1.297 paf 758: // record it to stack trace
1.301 paf 759: if(debug_name)
760: exception_trace.push(Trace(debug_name, debug_origin));
1.297 paf 761: rethrow;
762: }
1.1 paf 763: }
1.17 paf 764:
1.319 misha 765: WContext* Request::op_call(VMethodFrame& frame){
766: frame.fill_unspecified_params();
767: VMethodFrame *saved_method_frame=method_frame;
768: Value* saved_rcontext=rcontext;
769: WContext *saved_wcontext=wcontext;
770:
771: VStateless_class& called_class=*frame.junction.self.get_class();
772: Value* new_self;
773: if(wcontext->get_constructing()) {
774: wcontext->set_constructing(false);
775: new_self=&get_self();
776: if(frame.junction.method->call_type!=Method::CT_STATIC) {
777: // this is a constructor call
778: HashStringValue& new_object_fields=*new HashStringValue();
779: if(Value* value=called_class.create_new_value(fpool, new_object_fields)) {
780: // some stateless_class creatable derivates
781: new_self=value;
782: } else
783: throw Exception(PARSER_RUNTIME,
784: 0, //&frame.name(),
785: "is not a constructor, system class '%s' can be constructed only implicitly",
786: called_class.name().cstr());
787:
788: frame.write(*new_self,
789: String::L_CLEAN // not used, always an object, not string
790: );
791: } else
792: throw Exception(PARSER_RUNTIME,
793: 0, //&frame.name(),
794: "method is static and can not be used as constructor");
795: } else
796: new_self=&frame.junction.self;
797:
798: frame.set_self(*new_self);
799:
800: // see OP_PREPARE_TO_EXPRESSION
801: frame.set_in_expression(wcontext->get_in_expression());
802:
803: rcontext=wcontext=&frame;
804: {
805: const Method& method=*frame.junction.method;
806: Method::Call_type call_type=&called_class==new_self ? Method::CT_STATIC : Method::CT_DYNAMIC;
807: if(
808: method.call_type==Method::CT_ANY ||
809: method.call_type==call_type) { // allowed call type?
810: method_frame=&frame;
811: if(method.native_code) { // native code?
812: method.check_actual_numbered_params(
813: frame.junction.self,
814: /*frame.name(), */frame.numbered_params());
815: method.native_code(
816: *this,
817: *frame.numbered_params()); // execute it
818: } else // parser code, execute it
819: recoursion_checked_execute(*method.parser_code);
820: } else
821: throw Exception(PARSER_RUNTIME,
822: 0, //&frame.name(),
823: "is not allowed to be called %s",
824: call_type==Method::CT_STATIC?"statically":"dynamically");
825: }
826:
827: WContext *result_wcontext=wcontext;
828: // StringOrValue result=wcontext->result();
829:
830: wcontext=saved_wcontext;
831: rcontext=saved_rcontext;
832: method_frame=saved_method_frame;
833:
834: return result_wcontext;
835: }
836:
837:
1.292 paf 838: /**
839: @todo cache|prepare junctions
840: @bug ^superbase:method would dynamically call ^base:method if there is any
841: */
1.297 paf 842: Value& Request::get_element(Value& ncontext, const String& name, bool can_call_operator) {
843: Value* value=0;
1.249 paf 844: if(can_call_operator) {
1.297 paf 845: if(Method* method=main_class.get_method(name)) // looking operator of that name FIRST
1.312 paf 846: value=new VJunction(main_class, method);
1.249 paf 847: }
848: if(!value) {
849: if(!wcontext->get_constructing() // not constructing
850: && wcontext->get_somebody_entered_some_class() ) // ^class:method
1.297 paf 851: if(VStateless_class *called_class=ncontext.get_class())
1.249 paf 852: if(VStateless_class *read_class=rcontext->get_class())
853: if(read_class->derived_from(*called_class)) // current derived from called
1.297 paf 854: if(Value* base=get_self().base()) { // doing DYNAMIC call
1.290 paf 855: Temp_derived temp_derived(*base, 0); // temporarily prevent go-back-down virtual calls
1.320 misha 856: value=base->get_element(name, *base, true); // virtual-up lookup starting from parent
1.289 paf 857: goto value_ready;
1.249 paf 858: }
859: }
1.216 paf 860: if(!value)
1.320 misha 861: value=ncontext.get_element(name, ncontext, true);
1.291 paf 862:
863: if(value && wcontext->get_constructing())
1.297 paf 864: if(Junction* junction=value->get_junction()) {
865: if(junction->self.get_class()!=&ncontext)
1.316 misha 866: throw Exception(PARSER_RUNTIME,
1.291 paf 867: &name,
1.297 paf 868: "constructor must be declared in class '%s'",
869: ncontext.get_class()->name_cstr());
1.291 paf 870: }
1.249 paf 871:
1.289 paf 872: value_ready:
1.284 paf 873: if(value)
1.294 paf 874: value=&process_to_value(*value); // process possible code-junction
1.284 paf 875: else
1.324 ! misha 876: value=VVoid::get();
1.63 paf 877:
1.297 paf 878: return *value;
1.309 paf 879: }
880:
881: void Request::put_element(Value& ncontext, const String& name, Value& value) {
882: // put_element can return property-setting-junction
1.312 paf 883: if(const VJunction* vjunction=ncontext.put_element(ncontext, name, &value, false))
884: if(vjunction!=PUT_ELEMENT_REPLACED_ELEMENT) {
885: const Junction& junction=vjunction->junction();
1.309 paf 886: // process it
1.312 paf 887: ArrayString* params_names=junction.method->params_names;
1.309 paf 888: int param_count=params_names? params_names->count(): 0;
889: if(param_count!=1)
1.316 misha 890: throw Exception(PARSER_RUNTIME,
1.309 paf 891: 0,
892: "setter method must have ONE parameter (has %d parameters)", param_count);
893:
1.321 misha 894: VMethodFrame frame(junction, method_frame/*caller*/);
1.309 paf 895: frame.store_param(value);
896:
897: frame.set_self(frame.junction.self);
898:
899: VMethodFrame *saved_method_frame=method_frame;
900: Value* saved_rcontext=rcontext;
901: WContext *saved_wcontext=wcontext;
902:
903: rcontext=wcontext=&frame;
904: method_frame=&frame;
905:
1.311 paf 906: // prevent non-string writes for better error reporting [setters are not expected to return anything]
1.309 paf 907: wcontext->write(*method_frame);
908:
909: recoursion_checked_execute(*frame.junction.method->parser_code); // parser code, execute it
910: // we don't need it StringOrValue result=wcontext->result();
911:
912: method_frame=saved_method_frame;
913: wcontext=saved_wcontext;
914: rcontext=saved_rcontext;
915: }
1.34 paf 916: }
1.70 paf 917:
1.162 parser 918: /** @param intercept_string
1.116 paf 919: - true:
920: they want result=string value,
921: possible object result goes to wcontext
922: - false:
923: they want any result[string|object]
924: nothing goes to wcontext.
1.125 paf 925: used in @c (expression) params evaluation
1.224 paf 926:
927: using the fact it's either string_ or value_ result requested to speed up checkes
1.116 paf 928: */
1.229 paf 929: StringOrValue Request::process(Value& input_value, bool intercept_string) {
1.297 paf 930: Junction* junction=input_value.get_junction();
1.307 paf 931: if(junction) {
932: if(junction->is_getter) { // is it a getter-junction?
1.320 misha 933: int param_count=junction->method->params_names?junction->method->params_names->count():0;
934: if(junction->auto_name){ // default getter
935: if(param_count>1)
936: throw Exception(PARSER_RUNTIME,
937: 0,
938: "default getter method can't have more then 1 parameter (has %d parameters)", param_count);
939: } else if(param_count)
1.316 misha 940: throw Exception(PARSER_RUNTIME,
1.307 paf 941: 0,
1.320 misha 942: "getter method must have no parameters (has %d parameters)", param_count);
1.307 paf 943:
1.321 misha 944: VMethodFrame frame(*junction, method_frame/*caller*/);
1.317 misha 945:
1.320 misha 946: if(junction->auto_name && param_count)
947: frame.store_param(*new VString(*junction->auto_name));
948:
1.307 paf 949: frame.set_self(frame.junction.self);
950:
951: VMethodFrame *saved_method_frame=method_frame;
952: Value* saved_rcontext=rcontext;
953: WContext *saved_wcontext=wcontext;
954:
955: rcontext=wcontext=&frame;
956: method_frame=&frame;
957:
958: recoursion_checked_execute(*frame.junction.method->parser_code); // parser code, execute it
959: StringOrValue result=wcontext->result();
960:
961: method_frame=saved_method_frame;
962: wcontext=saved_wcontext;
963: rcontext=saved_rcontext;
964:
965: return result;
966: }
967:
968: if(junction->code) { // is it a code-junction?
969: // process it
970: StringOrValue result;
1.157 parser 971: #ifdef DEBUG_EXECUTE
1.307 paf 972: debug_printf(sapi_info, "ja->\n");
1.157 parser 973: #endif
1.238 paf 974:
1.307 paf 975: if(!junction->method_frame)
1.316 misha 976: throw Exception(PARSER_RUNTIME,
1.240 paf 977: 0,
978: "junction used outside of context");
979:
1.307 paf 980: VMethodFrame *saved_method_frame=method_frame;
981: Value* saved_rcontext=rcontext;
982: WContext *saved_wcontext=wcontext;
983:
984: method_frame=junction->method_frame;
985: rcontext=junction->rcontext;
986:
987: // for expression method params
988: // wcontext is set 0
989: // using the fact in decision "which wwrapper to use"
990: bool using_code_frame=intercept_string && junction->wcontext;
991: if(using_code_frame) {
1.318 misha 992: // almost plain wwrapper about junction wcontext
993:
994: VCodeFrame local(*junction->wcontext);
1.307 paf 995: wcontext=&local;
996:
997: // execute it
998: recoursion_checked_execute(*junction->code);
999:
1000: // CodeFrame soul:
1.318 misha 1001: result=wcontext->result();
1.307 paf 1002: } else {
1003: // plain wwrapper
1004: WWrapper local(0/*empty*/, wcontext);
1005: wcontext=&local;
1006:
1007: // execute it
1008: recoursion_checked_execute(*junction->code);
1009:
1010: result=wcontext->result();
1011: }
1012:
1013: wcontext=saved_wcontext;
1014: rcontext=saved_rcontext;
1015: method_frame=saved_method_frame;
1.207 paf 1016:
1.157 parser 1017: #ifdef DEBUG_EXECUTE
1.307 paf 1018: debug_printf(sapi_info, "<-ja returned");
1.157 parser 1019: #endif
1.307 paf 1020: return result;
1021: }
1022:
1023: // it is then method-junction, do not explode it
1024: // just return it as we do for usual objects
1025: }
1026:
1027: return input_value;
1.85 paf 1028: }
1029:
1.298 paf 1030: StringOrValue Request::execute_method(VMethodFrame& amethod_frame, const Method& method) {
1.257 paf 1031: VMethodFrame *saved_method_frame=method_frame;
1.297 paf 1032: Value* saved_rcontext=rcontext;
1.257 paf 1033: WContext *saved_wcontext=wcontext;
1.99 paf 1034:
1035: // initialize contexts
1.286 paf 1036: rcontext=wcontext=method_frame=&amethod_frame;
1.99 paf 1037:
1038: // execute!
1039: execute(*method.parser_code);
1040:
1041: // result
1.298 paf 1042: StringOrValue result=wcontext->result();
1.208 paf 1043:
1.257 paf 1044: wcontext=saved_wcontext;
1045: rcontext=saved_rcontext;
1046: method_frame=saved_method_frame;
1.242 paf 1047:
1048: // return
1049: return result;
1.208 paf 1050: }
1051:
1.297 paf 1052: const String* Request::execute_method(Value& aself,
1053: const Method& method, VString* optional_param,
1054: bool do_return_string) {
1.317 misha 1055:
1.257 paf 1056: VMethodFrame *saved_method_frame=method_frame;
1.297 paf 1057: Value* saved_rcontext=rcontext;
1.257 paf 1058: WContext *saved_wcontext=wcontext;
1.208 paf 1059:
1.307 paf 1060: Junction local_junction(aself, &method);
1.321 misha 1061: VMethodFrame local_frame(local_junction, method_frame/*caller*/);
1.242 paf 1062: if(optional_param && local_frame.can_store_param()) {
1.297 paf 1063: local_frame.store_param(*optional_param);
1.242 paf 1064: local_frame.fill_unspecified_params();
1065: }
1.285 paf 1066: local_frame.set_self(aself);
1.257 paf 1067: rcontext=wcontext=method_frame=&local_frame;
1.246 paf 1068:
1069: // prevent non-string writes for better error reporting
1.297 paf 1070: if(do_return_string)
1.246 paf 1071: wcontext->write(local_frame);
1.208 paf 1072:
1073: // execute!
1074: execute(*method.parser_code);
1075:
1076: // result
1.297 paf 1077: const String* result=0;
1078: if(do_return_string)
1079: result=&wcontext->result().as_string();
1.99 paf 1080:
1.257 paf 1081: wcontext=saved_wcontext;
1082: rcontext=saved_rcontext;
1083: method_frame=saved_method_frame;
1.297 paf 1084:
1085: return result;
1.242 paf 1086: }
1087:
1.297 paf 1088: Request::Execute_nonvirtual_method_result
1089: Request::execute_nonvirtual_method(VStateless_class& aclass,
1.317 misha 1090: const String& method_name,
1091: VString* optional_param,
1.297 paf 1092: bool do_return_string) {
1093: Execute_nonvirtual_method_result result;
1094: result.method=aclass.get_method(method_name);
1095: if(result.method)
1.317 misha 1096: result.string=execute_method(aclass, *result.method, optional_param, do_return_string);
1.297 paf 1097: return result;
1.99 paf 1098: }
1099:
1.297 paf 1100: const String* Request::execute_virtual_method(Value& aself,
1101: const String& method_name) {
1102: if(Value* value=aself.get_element(method_name, aself, false))
1103: if(Junction* junction=value->get_junction())
1104: if(const Method *method=junction->method)
1105: return execute_method(aself, *method, 0/*no params*/, true);
1.188 parser 1106:
1.176 parser 1107: return 0;
1108: }