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