|
|
1.117 paf 1: /** @file
1.118 paf 2: Parser: executor part of request class.
3:
1.217 paf 4: Copyright (c) 2001, 2002 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.282 ! paf 8: static const char* IDENT_EXECUTE_C="$Date: 2002/10/14 15:22:42 $";
1.1 paf 9:
1.152 paf 10: #include "pa_opcode.h"
1.8 paf 11: #include "pa_array.h"
1.11 paf 12: #include "pa_request.h"
1.15 paf 13: #include "pa_vstring.h"
1.22 paf 14: #include "pa_vhash.h"
1.167 parser 15: #include "pa_vvoid.h"
1.145 paf 16: #include "pa_vcode_frame.h"
17: #include "pa_vmethod_frame.h"
1.38 paf 18: #include "pa_vobject.h"
1.49 paf 19: #include "pa_vdouble.h"
1.54 paf 20: #include "pa_vbool.h"
1.94 paf 21: #include "pa_vtable.h"
1.132 paf 22: #include "pa_vfile.h"
1.144 paf 23: #include "pa_vimage.h"
1.194 parser 24: #include "pa_wwrapper.h"
1.1 paf 25:
1.233 paf 26: //#define DEBUG_EXECUTE
1.221 paf 27: //#define DEBUG_STRING_APPENDS_VS_EXPANDS
1.220 paf 28:
29:
30: #ifdef DEBUG_STRING_APPENDS_VS_EXPANDS
31: ulong wcontext_result_size=0;
32: #endif
33:
34:
1.157 parser 35:
36: #ifdef DEBUG_EXECUTE
1.1 paf 37: char *opcode_name[]={
1.49 paf 38: // literals
1.109 paf 39: "VALUE", "CURLY_CODE__STORE_PARAM", "EXPR_CODE__STORE_PARAM",
1.209 paf 40: "NESTED_CODE",
1.49 paf 41:
42: // actions
1.187 parser 43: "WITH_ROOT", "WITH_SELF", "WITH_READ", "WITH_WRITE",
1.66 paf 44: "GET_CLASS",
1.182 parser 45: "CONSTRUCT_VALUE", "CONSTRUCT_EXPR", "CURLY_CODE__CONSTRUCT",
1.108 paf 46: "WRITE_VALUE", "WRITE_EXPR_RESULT", "STRING__WRITE",
1.275 paf 47: "GET_ELEMENT_OR_OPERATOR", "OP_GET_ELEMENT_OR_JUNCTION_EXPAND",
48: "GET_ELEMENT", "GET_ELEMENT__WRITE",
1.232 paf 49: "OBJECT_POOL", "STRING_POOL",
1.1 paf 50: "STORE_PARAM",
1.232 paf 51: "PREPARE_TO_CONSTRUCT_OBJECT", "PREPARE_TO_EXPRESSION",
52: "CALL", "CALL__WRITE",
1.49 paf 53:
54: // expression ops: unary
1.148 paf 55: "NEG", "INV", "NOT", "DEF", "IN", "FEXISTS", "DEXISTS",
1.49 paf 56: // expression ops: binary
1.201 paf 57: "SUB", "ADD", "MUL", "DIV", "MOD", "INTDIV",
1.275 paf 58: "BIN_SL", "BIN_SR",
1.56 paf 59: "BIN_AND", "BIN_OR", "BIN_XOR",
60: "LOG_AND", "LOG_OR", "LOG_XOR",
1.49 paf 61: "NUM_LT", "NUM_GT", "NUM_LE", "NUM_GE", "NUM_EQ", "NUM_NE",
1.103 paf 62: "STR_LT", "STR_GT", "STR_LE", "STR_GE", "STR_EQ", "STR_NE",
63: "IS"
1.1 paf 64: };
65:
1.158 parser 66: void va_debug_printf(Pool& pool, const char *fmt,va_list args) {
1.127 paf 67: char buf[MAX_STRING];
68: vsnprintf(buf, MAX_STRING, fmt, args);
69: SAPI::log(pool, "%s", buf);
1.107 paf 70: }
71:
1.158 parser 72: void debug_printf(Pool& pool, const char *fmt, ...) {
1.107 paf 73: va_list args;
74: va_start(args,fmt);
1.158 parser 75: va_debug_printf(pool,fmt,args);
1.107 paf 76: va_end(args);
1.105 paf 77: }
78:
1.158 parser 79: void debug_dump(Pool& pool, int level, const Array& ops) {
1.190 parser 80: Array_iter i(ops);
81: while(i.has_next()) {
1.23 paf 82: Operation op;
1.190 parser 83: op.cast=i.next();
1.1 paf 84:
1.86 paf 85: if(op.code==OP_VALUE || op.code==OP_STRING__WRITE) {
1.190 parser 86: Value *value=static_cast<Value *>(i.next());
1.158 parser 87: debug_printf(pool,
1.133 paf 88: "%*s%s"
89: " \"%s\" %s",
90: level*4, "", opcode_name[op.code],
91: value->get_string()->cstr(), value->type());
92: continue;
1.15 paf 93: }
1.158 parser 94: debug_printf(pool, "%*s%s", level*4, "", opcode_name[op.code]);
1.1 paf 95:
1.184 parser 96: switch(op.code) {
97: case OP_CURLY_CODE__STORE_PARAM:
98: case OP_EXPR_CODE__STORE_PARAM:
99: case OP_CURLY_CODE__CONSTRUCT:
1.209 paf 100: case OP_NESTED_CODE:
1.226 paf 101: case OP_OBJECT_POOL:
102: case OP_STRING_POOL:
1.237 paf 103: case OP_CALL:
1.190 parser 104: const Array *local_ops=reinterpret_cast<const Array *>(i.next());
1.158 parser 105: debug_dump(pool, level+1, *local_ops);
1.1 paf 106: }
107: }
108: }
1.157 parser 109: #endif
1.1 paf 110:
1.159 parser 111: #define PUSH(value) stack.push(value)
112: #define POP() static_cast<Value *>(stack.pop())
113: #define POP_NAME() static_cast<Value *>(stack.pop())->as_string()
1.209 paf 114: #define POP_CODE() static_cast<Array *>(stack.pop())
1.159 parser 115:
1.266 paf 116: void Request::execute(const Array& ops) {
1.197 parser 117: // _asm int 3;
1.157 parser 118: #ifdef DEBUG_EXECUTE
1.158 parser 119: debug_printf(pool(), "source----------------------------\n");
120: debug_dump(pool(), 0, ops);
121: debug_printf(pool(), "execution-------------------------\n");
1.157 parser 122: #endif
1.236 paf 123: const String *last_get_element_name=0;
124:
1.190 parser 125: Array_iter i(ops);
126: while(i.has_next()) {
1.23 paf 127: Operation op;
1.190 parser 128: op.cast=i.next();
1.157 parser 129: #ifdef DEBUG_EXECUTE
1.158 parser 130: debug_printf(pool(), "%d:%s", stack.top_index()+1, opcode_name[op.code]);
1.157 parser 131: #endif
1.11 paf 132:
1.197 parser 133: Value *value;
134: Value *a; Value *b;
1.209 paf 135: Array *b_code;
1.23 paf 136: switch(op.code) {
1.51 paf 137: // param in next instruction
1.52 paf 138: case OP_VALUE:
1.32 paf 139: {
1.197 parser 140: value=static_cast<Value *>(i.next());
1.157 parser 141: #ifdef DEBUG_EXECUTE
1.158 parser 142: debug_printf(pool(), " \"%s\" %s", value->get_string()->cstr(), value->type());
1.157 parser 143: #endif
1.52 paf 144: PUSH(value);
1.32 paf 145: break;
146: }
1.66 paf 147: case OP_GET_CLASS:
1.38 paf 148: {
1.130 paf 149: // maybe they do ^class:method[] call, remember the fact
1.215 paf 150: wcontext->set_somebody_entered_some_class();
1.98 paf 151:
1.82 paf 152: const String& name=POP_NAME();
1.197 parser 153: value=static_cast<Value *>(classes().get(name));
1.120 paf 154: if(!value)
1.223 paf 155: throw Exception("parser.runtime",
1.66 paf 156: &name,
1.143 paf 157: "class is undefined");
1.66 paf 158:
1.120 paf 159: PUSH(value);
1.38 paf 160: break;
161: }
1.32 paf 162:
1.51 paf 163: // OP_WITH
1.187 parser 164: case OP_WITH_ROOT:
165: {
1.257 paf 166: PUSH(method_frame);
1.187 parser 167: break;
168: }
1.37 paf 169: case OP_WITH_SELF:
170: {
171: PUSH(self);
172: break;
173: }
1.15 paf 174: case OP_WITH_READ:
175: {
1.24 paf 176: PUSH(rcontext);
1.20 paf 177: break;
178: }
1.37 paf 179: case OP_WITH_WRITE:
1.20 paf 180: {
1.37 paf 181: PUSH(wcontext);
1.20 paf 182: break;
183: }
1.37 paf 184:
1.51 paf 185: // OTHER ACTIONS BUT WITHs
1.80 paf 186: case OP_CONSTRUCT_VALUE:
1.20 paf 187: {
1.197 parser 188: value=POP();
1.82 paf 189: const String& name=POP_NAME();
1.37 paf 190: Value *ncontext=POP();
1.251 paf 191: ncontext->put_element(name, value, false);
1.213 paf 192: break;
193: }
1.80 paf 194: case OP_CONSTRUCT_EXPR:
195: {
1.219 paf 196: // see OP_PREPARE_TO_EXPRESSION
197: wcontext->set_in_expression(false);
198:
1.197 parser 199: value=POP();
1.82 paf 200: const String& name=POP_NAME();
1.80 paf 201: Value *ncontext=POP();
1.251 paf 202: ncontext->put_element(name, value->as_expr_result(), false);
1.80 paf 203: break;
204: }
1.182 parser 205: case OP_CURLY_CODE__CONSTRUCT:
206: {
1.190 parser 207: const Array *local_ops=reinterpret_cast<const Array *>(i.next());
1.182 parser 208: #ifdef DEBUG_EXECUTE
209: debug_printf(pool(), " (%d)\n", local_ops->size());
210: debug_dump(pool(), 1, *local_ops);
211: #endif
212: Junction& j=*NEW Junction(pool(),
1.272 paf 213: *self, 0,
1.257 paf 214: method_frame,
1.240 paf 215: rcontext,
216: wcontext,
1.182 parser 217: local_ops);
1.238 paf 218:
1.197 parser 219: value=NEW VJunction(j);
1.182 parser 220: const String& name=POP_NAME();
221: Value *ncontext=POP();
1.251 paf 222: ncontext->put_element(name, value, false);
1.182 parser 223: break;
224: }
1.209 paf 225: case OP_NESTED_CODE:
226: {
227: Array *local_ops=static_cast<Array *>(i.next());
228: #ifdef DEBUG_EXECUTE
229: debug_printf(pool(), " (%d)\n", local_ops->size());
230: debug_dump(pool(), 1, *local_ops);
231: #endif
232: PUSH(local_ops);
233: break;
234: }
1.108 paf 235: case OP_WRITE_VALUE:
1.13 paf 236: {
1.197 parser 237: value=POP();
1.236 paf 238: write_assign_lang(*value, last_get_element_name);
1.86 paf 239: break;
240: }
1.108 paf 241: case OP_WRITE_EXPR_RESULT:
242: {
1.230 paf 243: value=POP();
244: write_no_lang(*value->as_expr_result());
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.86 paf 251: case OP_STRING__WRITE:
252: {
1.190 parser 253: VString *vstring=static_cast<VString *>(i.next());
1.157 parser 254: #ifdef DEBUG_EXECUTE
1.158 parser 255: debug_printf(pool(), " \"%s\"", vstring->string().cstr());
1.157 parser 256: #endif
1.122 paf 257: write_no_lang(vstring->string());
1.13 paf 258: break;
1.14 paf 259: }
1.13 paf 260:
1.215 paf 261: case OP_GET_ELEMENT_OR_OPERATOR:
262: {
1.236 paf 263: value=get_element(last_get_element_name, true);
1.215 paf 264: PUSH(value);
265: break;
266: }
1.275 paf 267: case OP_GET_ELEMENT_OR_JUNCTION_EXPAND:
268: {
269: value=get_element(last_get_element_name, false, true/* the only user */);
270: PUSH(value);
271: break;
272: }
1.15 paf 273: case OP_GET_ELEMENT:
1.11 paf 274: {
1.236 paf 275: value=get_element(last_get_element_name, false);
1.24 paf 276: PUSH(value);
1.17 paf 277: break;
278: }
279:
1.18 paf 280: case OP_GET_ELEMENT__WRITE:
1.17 paf 281: {
1.236 paf 282: value=get_element(last_get_element_name/*not followed by call, not needed really*/, false);
283: write_assign_lang(*value, last_get_element_name);
1.17 paf 284: break;
285: }
286:
1.32 paf 287:
1.226 paf 288: case OP_OBJECT_POOL:
1.17 paf 289: {
1.226 paf 290: const Array *local_ops=reinterpret_cast<const Array *>(i.next());
291:
1.257 paf 292: WContext *saved_wcontext=wcontext;
293: uchar saved_lang= flang;
1.137 paf 294: flang=String::UL_PASS_APPENDED;
1.267 paf 295: WWrapper local(pool(), 0 /*empty*/, wcontext);
1.226 paf 296: wcontext=&local;
297:
298: execute(*local_ops);
299:
1.228 paf 300: value=&wcontext->result().as_value();
1.257 paf 301: flang=saved_lang;
302: wcontext=saved_wcontext;
1.24 paf 303: PUSH(value);
1.13 paf 304: break;
1.15 paf 305: }
1.13 paf 306:
1.226 paf 307: case OP_STRING_POOL:
1.49 paf 308: {
1.226 paf 309: const Array *local_ops=reinterpret_cast<const Array *>(i.next());
310:
1.257 paf 311: WContext *saved_wcontext=wcontext;
1.267 paf 312: WWrapper local(pool(), 0 /*empty*/, wcontext);
1.226 paf 313: wcontext=&local;
314:
315: execute(*local_ops);
316:
1.49 paf 317: // from "$a $b" part of expression taking only string value,
318: // ignoring any other content of wcontext
1.82 paf 319: const String *string=wcontext->get_string();
1.80 paf 320: Value *value;
321: if(string)
322: value=NEW VString(*string);
323: else
1.167 parser 324: NEW VVoid(pool());
1.257 paf 325: wcontext=saved_wcontext;
1.49 paf 326: PUSH(value);
327: break;
328: }
329:
1.51 paf 330: // CALL
1.237 paf 331: case OP_STORE_PARAM:
1.28 paf 332: {
1.197 parser 333: value=POP();
1.237 paf 334: VMethodFrame *frame=static_cast<VMethodFrame *>(stack.top_value());
335: // this op is executed from CALL local_ops only, so can not check method_frame_to_fill==0
336: frame->store_param(value);
1.28 paf 337: break;
338: }
1.237 paf 339: case OP_CURLY_CODE__STORE_PARAM:
340: case OP_EXPR_CODE__STORE_PARAM:
1.28 paf 341: {
1.237 paf 342: // code
343: const Array *local_ops=reinterpret_cast<const Array *>(i.next());
1.73 paf 344: VMethodFrame *frame=static_cast<VMethodFrame *>(stack.top_value());
1.237 paf 345: #ifdef DEBUG_EXECUTE
346: debug_printf(pool(), " (%d)\n", local_ops->size());
347: debug_dump(pool(), 1, *local_ops);
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"
356: Junction& j=*NEW Junction(pool(),
1.272 paf 357: *self, 0,
1.257 paf 358: method_frame,
1.237 paf 359: rcontext,
360: op.code==OP_EXPR_CODE__STORE_PARAM?0:wcontext,
361: local_ops);
362:
363: value=NEW VJunction(j);
364:
365: // store param
366: // this op is executed from CALL local_ops only, so can not check method_frame_to_fill==0
367: frame->store_param(value);
1.29 paf 368: break;
369: }
370:
1.186 parser 371: case OP_PREPARE_TO_CONSTRUCT_OBJECT:
372: {
1.200 paf 373: wcontext->set_constructing(true);
1.186 parser 374: break;
375: }
1.219 paf 376:
377: case OP_PREPARE_TO_EXPRESSION:
378: {
379: wcontext->set_in_expression(true);
380: break;
381: }
382:
1.186 parser 383: case OP_CALL:
1.232 paf 384: case OP_CALL__WRITE:
1.29 paf 385: {
1.237 paf 386: Array *local_ops=static_cast<Array *>(i.next());
1.157 parser 387: #ifdef DEBUG_EXECUTE
1.237 paf 388: debug_printf(pool(), " (%d)\n", local_ops->size());
389: debug_dump(pool(), 1, *local_ops);
390:
1.158 parser 391: debug_printf(pool(), "->\n");
1.157 parser 392: #endif
1.237 paf 393: value=POP();
394:
395: Junction *junction=value->get_junction();
396: if(!junction)
397: throw Exception("parser.runtime",
398: last_get_element_name,
399: "(%s) not a method or junction, can not call it",
1.276 paf 400: value->type());
1.282 ! paf 401: /* no check needed, code compiled the way that that's impossible
1.276 paf 402: // check:
403: // that this is method-junction, not a code-junction
1.278 paf 404: // [disabling these contstructions:]
405: // $junction{code}
406: // ^junction[]
1.276 paf 407: if(!junction->method)
408: throw Exception("parser.runtime",
409: last_get_element_name,
410: "(%s) is code junction, can not call it",
1.237 paf 411: value->type());
1.282 ! paf 412: */
1.237 paf 413:
1.282 ! paf 414: VMethodFrame frame(pool(), *last_get_element_name, *junction, method_frame/*caller*/);
1.237 paf 415: if(local_ops){ // store param code goes here
416: PUSH(&frame); // argument for *STORE_PARAM ops
417: execute(*local_ops);
418: POP();
419: }
420: frame.fill_unspecified_params();
1.257 paf 421: Value *saved_self=self;
422: VMethodFrame *saved_method_frame=method_frame;
423: Value *saved_rcontext=rcontext;
424: WContext *saved_wcontext=wcontext;
1.45 paf 425:
1.272 paf 426: VStateless_class *called_class=frame.junction.self.get_class();
1.200 paf 427: if(wcontext->get_constructing()) {
428: wcontext->set_constructing(false);
1.237 paf 429: if(frame.junction.method->call_type!=Method::CT_STATIC) {
1.138 paf 430: // this is a constructor call
1.185 parser 431:
432: if(Value *value=called_class->create_new_value(pool())) {
1.204 paf 433: // some stateless_class creatable derivates
1.149 paf 434: self=value;
1.204 paf 435: } else
1.223 paf 436: throw Exception("parser.runtime",
1.237 paf 437: &frame.name(),
1.204 paf 438: "is not a constructor, system class '%s' can be constructed only implicitly",
439: called_class->name().cstr());
440:
1.237 paf 441: frame.write(*self,
1.136 paf 442: String::UL_CLEAN // not used, always an object, not string
1.83 paf 443: );
1.185 parser 444: } else
1.223 paf 445: throw Exception("parser.runtime",
1.237 paf 446: &frame.name(),
1.185 parser 447: "method is static and can not be used as constructor");
1.249 paf 448: } else
1.272 paf 449: self=&frame.junction.self;
1.75 paf 450:
1.272 paf 451: frame.set_self(*self);
1.219 paf 452:
453: // see OP_PREPARE_TO_EXPRESSION
1.237 paf 454: frame.set_in_expression(wcontext->get_in_expression());
1.219 paf 455:
1.237 paf 456: rcontext=wcontext=&frame;
1.47 paf 457: {
1.237 paf 458: const Method& method=*frame.junction.method;
1.134 paf 459: Method::Call_type call_type=
460: called_class==self ? Method::CT_STATIC : Method::CT_DYNAMIC;
461: if(
462: method.call_type==Method::CT_ANY ||
1.197 parser 463: method.call_type==call_type) { // allowed call type?
464: try {
465: if(method.native_code) { // native code?
1.257 paf 466: // method_frame unchanged, so that ^for ^foreach & co may write to locals
1.197 parser 467: method.check_actual_numbered_params(
1.237 paf 468: frame.junction.self,
469: frame.name(), frame.numbered_params());
1.197 parser 470: method.native_code(
471: *this,
1.237 paf 472: frame.name(), frame.numbered_params()); // execute it
1.197 parser 473: } else { // parser code
1.257 paf 474: method_frame=&frame;
1.225 paf 475: // execute it
1.237 paf 476: recoursion_checked_execute(&frame.name(), *method.parser_code);
1.159 parser 477: }
1.197 parser 478: } catch(...) {
479: // record it to stack trace
1.237 paf 480: exception_trace.push((void *)&frame.name());
1.197 parser 481: /*re*/throw;
1.159 parser 482: }
1.197 parser 483: } else
1.223 paf 484: throw Exception("parser.runtime",
1.237 paf 485: &frame.name(),
1.134 paf 486: "is not allowed to be called %s",
487: call_type==Method::CT_STATIC?"statically":"dynamically");
488:
1.47 paf 489: }
1.232 paf 490: StringOrValue result=wcontext->result();
1.45 paf 491:
1.257 paf 492: wcontext=saved_wcontext;
493: rcontext=saved_rcontext;
494: method_frame=saved_method_frame;
495: self=saved_self;
1.220 paf 496:
497: #ifdef DEBUG_STRING_APPENDS_VS_EXPANDS
498: if(const String *s=value->get_string())
499: wcontext_result_size+=s->used_rows()*sizeof(String::Chunk::Row);
500: #endif
1.213 paf 501:
1.232 paf 502: if(op.code==OP_CALL__WRITE) {
1.236 paf 503: write_assign_lang(result, last_get_element_name);
1.232 paf 504: } else { // OP_CALL
505: PUSH(&result.as_value());
506: }
507:
1.157 parser 508: #ifdef DEBUG_EXECUTE
1.158 parser 509: debug_printf(pool(), "<-returned");
1.157 parser 510: #endif
1.49 paf 511: break;
512: }
513:
1.55 paf 514: // expression ops: unary
515: case OP_NEG:
516: {
517: Value *operand=POP();
1.197 parser 518: value=NEW VDouble(pool(), -operand->as_double());
1.55 paf 519: PUSH(value);
520: break;
521: }
522: case OP_INV:
523: {
524: Value *operand=POP();
1.197 parser 525: value=NEW VDouble(pool(), ~operand->as_int());
1.55 paf 526: PUSH(value);
527: break;
528: }
529: case OP_NOT:
530: {
531: Value *operand=POP();
1.197 parser 532: value=NEW VBool(pool(), !operand->as_bool());
1.55 paf 533: PUSH(value);
534: break;
535: }
1.62 paf 536: case OP_DEF:
537: {
538: Value *operand=POP();
1.197 parser 539: value=NEW VBool(pool(), operand->is_defined());
1.62 paf 540: PUSH(value);
541: break;
542: }
543: case OP_IN:
544: {
1.199 paf 545: /// @test String::cmp
1.62 paf 546: Value *operand=POP();
1.110 paf 547: const char *path=operand->as_string().cstr();
1.197 parser 548: value=NEW VBool(pool(),
1.151 paf 549: info.uri && strncmp(path, info.uri, strlen(path))==0);
1.62 paf 550: PUSH(value);
551: break;
552: }
553: case OP_FEXISTS:
554: {
555: Value *operand=POP();
1.197 parser 556: value=NEW VBool(pool(),
1.127 paf 557: file_readable(absolute(operand->as_string())));
1.148 paf 558: PUSH(value);
559: break;
560: }
561: case OP_DEXISTS:
562: {
563: Value *operand=POP();
1.197 parser 564: value=NEW VBool(pool(),
1.148 paf 565: dir_readable(absolute(operand->as_string())));
1.62 paf 566: PUSH(value);
567: break;
568: }
1.55 paf 569:
570: // expression ops: binary
571: case OP_SUB:
1.53 paf 572: {
1.197 parser 573: b=POP(); a=POP();
574: value=NEW VDouble(pool(), a->as_double() - b->as_double());
1.53 paf 575: PUSH(value);
576: break;
577: }
1.55 paf 578: case OP_ADD:
1.53 paf 579: {
1.197 parser 580: b=POP(); a=POP();
581: value=NEW VDouble(pool(), a->as_double() + b->as_double());
1.53 paf 582: PUSH(value);
583: break;
584: }
1.49 paf 585: case OP_MUL:
586: {
1.197 parser 587: b=POP(); a=POP();
588: value=NEW VDouble(pool(), a->as_double() * b->as_double());
1.53 paf 589: PUSH(value);
590: break;
591: }
592: case OP_DIV:
593: {
1.197 parser 594: b=POP(); a=POP();
1.170 parser 595:
596: double a_double=a->as_double();
597: double b_double=b->as_double();
598:
1.171 parser 599: if(b_double == 0) {
600: const String *problem_source=&b->as_string();
1.223 paf 601: throw Exception("number.zerodivision",
1.171 parser 602: problem_source,
1.170 parser 603: "Division by zero");
1.171 parser 604: }
1.170 parser 605:
1.197 parser 606: value=NEW VDouble(pool(), a_double / b_double);
1.54 paf 607: PUSH(value);
608: break;
609: }
1.55 paf 610: case OP_MOD:
611: {
1.197 parser 612: b=POP(); a=POP();
1.170 parser 613:
1.173 parser 614: double a_double=a->as_double();
615: double b_double=b->as_double();
1.170 parser 616:
1.173 parser 617: if(b_double == 0) {
1.171 parser 618: const String *problem_source=&b->as_string();
1.223 paf 619: throw Exception("number.zerodivision",
1.171 parser 620: problem_source,
1.170 parser 621: "Modulus by zero");
1.171 parser 622: }
1.170 parser 623:
1.197 parser 624: value=NEW VDouble(pool(), fmod(a_double, b_double));
1.201 paf 625: PUSH(value);
626: break;
627: }
628: case OP_INTDIV:
629: {
630: b=POP(); a=POP();
631:
632: int a_int=a->as_int();
633: int b_int=b->as_int();
634:
635: if(b_int == 0) {
636: const String *problem_source=&b->as_string();
1.223 paf 637: throw Exception("number.zerodivision",
1.201 paf 638: problem_source,
639: "Division by zero");
640: }
641:
642: value=NEW VInt(pool(), a_int / b_int);
1.55 paf 643: PUSH(value);
644: break;
645: }
1.274 paf 646: case OP_BIN_SL:
647: {
648: b=POP(); a=POP();
649: value=NEW VInt(pool(),
650: a->as_int() <<
651: b->as_int());
652: PUSH(value);
653: break;
654: }
655: case OP_BIN_SR:
656: {
657: b=POP(); a=POP();
658: value=NEW VInt(pool(),
659: a->as_int() >>
660: b->as_int());
661: PUSH(value);
662: break;
663: }
1.55 paf 664: case OP_BIN_AND:
1.54 paf 665: {
1.197 parser 666: b=POP(); a=POP();
1.274 paf 667: value=NEW VInt(pool(),
1.155 parser 668: a->as_int() &
669: b->as_int());
1.54 paf 670: PUSH(value);
671: break;
672: }
1.55 paf 673: case OP_BIN_OR:
1.54 paf 674: {
1.197 parser 675: b=POP(); a=POP();
1.274 paf 676: value=NEW VInt(pool(),
1.155 parser 677: a->as_int() |
678: b->as_int());
1.55 paf 679: PUSH(value);
680: break;
681: }
1.56 paf 682: case OP_BIN_XOR:
683: {
1.197 parser 684: b=POP(); a=POP();
1.274 paf 685: value=NEW VInt(pool(),
1.155 parser 686: a->as_int() ^
687: b->as_int());
1.56 paf 688: PUSH(value);
689: break;
690: }
1.55 paf 691: case OP_LOG_AND:
692: {
1.209 paf 693: b_code=POP_CODE(); a=POP();
1.263 paf 694: bool result;
1.209 paf 695: if(a->as_bool()) {
696: execute(*b_code);
697: b=POP();
1.263 paf 698: result=b->as_bool();
1.209 paf 699: } else
1.263 paf 700: result=false;
701: value=NEW VBool(pool(), result);
1.55 paf 702: PUSH(value);
703: break;
704: }
705: case OP_LOG_OR:
706: {
1.209 paf 707: b_code=POP_CODE(); a=POP();
1.263 paf 708: bool result;
1.209 paf 709: if(a->as_bool())
1.263 paf 710: result=true;
1.209 paf 711: else {
712: execute(*b_code);
713: b=POP();
1.263 paf 714: result=b->as_bool();
1.209 paf 715: }
1.263 paf 716: value=NEW VBool(pool(), result);
1.56 paf 717: PUSH(value);
718: break;
719: }
720: case OP_LOG_XOR:
721: {
1.197 parser 722: b=POP(); a=POP();
723: value=NEW VBool(pool(), a->as_bool() ^ b->as_bool());
1.55 paf 724: PUSH(value);
725: break;
726: }
727: case OP_NUM_LT:
728: {
1.197 parser 729: b=POP(); a=POP();
1.263 paf 730: double result=a->as_double() - b->as_double();
731: value=NEW VBool(pool(), result < 0.0);
1.55 paf 732: PUSH(value);
733: break;
734: }
735: case OP_NUM_GT:
736: {
1.197 parser 737: b=POP(); a=POP();
1.263 paf 738: double result=a->as_double() - b->as_double();
739: value=NEW VBool(pool(), result > 0.0);
1.55 paf 740: PUSH(value);
741: break;
742: }
743: case OP_NUM_LE:
744: {
1.197 parser 745: b=POP(); a=POP();
1.263 paf 746: double result=a->as_double() - b->as_double();
747: value=NEW VBool(pool(), result <= 0.0);
1.55 paf 748: PUSH(value);
749: break;
750: }
751: case OP_NUM_GE:
752: {
1.197 parser 753: b=POP(); a=POP();
1.263 paf 754: double result=a->as_double() - b->as_double();
755: value=NEW VBool(pool(), result >= 0.0);
1.55 paf 756: PUSH(value);
757: break;
758: }
759: case OP_NUM_EQ:
760: {
1.197 parser 761: b=POP(); a=POP();
1.263 paf 762: double result=a->as_double() - b->as_double();
763: value=NEW VBool(pool(), result == 0.0);
1.55 paf 764: PUSH(value);
765: break;
766: }
767: case OP_NUM_NE:
768: {
1.197 parser 769: b=POP(); a=POP();
1.263 paf 770: double result=a->as_double() - b->as_double();
771: value=NEW VBool(pool(), result != 0.0);
1.54 paf 772: PUSH(value);
773: break;
774: }
1.58 paf 775: case OP_STR_LT:
776: {
1.197 parser 777: b=POP(); a=POP();
778: value=NEW VBool(pool(), a->as_string() < b->as_string());
1.58 paf 779: PUSH(value);
780: break;
781: }
782: case OP_STR_GT:
783: {
1.197 parser 784: b=POP(); a=POP();
785: value=NEW VBool(pool(), a->as_string() > b->as_string());
1.58 paf 786: PUSH(value);
787: break;
788: }
1.55 paf 789: case OP_STR_LE:
1.58 paf 790: {
1.197 parser 791: b=POP(); a=POP();
792: value=NEW VBool(pool(), a->as_string() <= b->as_string());
1.58 paf 793: PUSH(value);
794: break;
795: }
1.55 paf 796: case OP_STR_GE:
1.54 paf 797: {
1.197 parser 798: b=POP(); a=POP();
799: value=NEW VBool(pool(), a->as_string() >= b->as_string());
1.58 paf 800: PUSH(value);
801: break;
802: }
803: case OP_STR_EQ:
804: {
1.197 parser 805: b=POP(); a=POP();
806: value=NEW VBool(pool(), a->as_string() == b->as_string());
1.58 paf 807: PUSH(value);
808: break;
809: }
810: case OP_STR_NE:
811: {
1.197 parser 812: b=POP(); a=POP();
813: value=NEW VBool(pool(), a->as_string() != b->as_string());
1.103 paf 814: PUSH(value);
815: break;
816: }
817: case OP_IS:
818: {
1.197 parser 819: b=POP(); a=POP();
1.255 paf 820: value=NEW VBool(pool(), a->is(b->as_string().cstr()));
1.49 paf 821: PUSH(value);
1.28 paf 822: break;
823: }
824:
1.11 paf 825: default:
1.223 paf 826: throw Exception(0,
1.67 paf 827: 0,
1.139 paf 828: "invalid opcode %d", op.code);
1.11 paf 829: }
830: }
1.1 paf 831: }
1.17 paf 832:
1.231 paf 833: /// @test cache|prepare junctions
1.275 paf 834: Value *Request::get_element(const String *& remember_name,
1.277 paf 835: bool can_call_operator, bool should_explode_junction) {
1.236 paf 836: const String& name=POP_NAME(); remember_name=&name;
1.24 paf 837: Value *ncontext=POP();
1.216 paf 838: Value *value=0;
1.249 paf 839: if(can_call_operator) {
1.281 paf 840: if(Method* method=main_class.get_method(name)) { // looking operator of that name FIRST
1.181 parser 841: // as if that method were in self and we have normal dynamic method here
842: Junction& junction=*NEW Junction(pool(),
1.281 paf 843: main_class, method, 0,0,0,0);
1.181 parser 844: value=NEW VJunction(junction);
845: }
1.249 paf 846: }
847: if(!value) {
848: if(!wcontext->get_constructing() // not constructing
849: && wcontext->get_somebody_entered_some_class() ) // ^class:method
850: if(VStateless_class *called_class=ncontext->get_class())
851: if(VStateless_class *read_class=rcontext->get_class())
852: if(read_class->derived_from(*called_class)) // current derived from called
853: if(Value *base_object=self->base_object()) { // doing DYNAMIC call
1.256 paf 854: Temp_derived temp_derived(*base_object, 0); // temporarily prevent go-back-down virtual calls
1.252 paf 855: value=base_object->get_element(name, base_object, false); // virtual-up lookup starting from parent
1.249 paf 856: goto _void;
857: }
858: }
1.216 paf 859: if(!value)
1.252 paf 860: value=ncontext->get_element(name, ncontext, false);
1.249 paf 861:
862: _void:
1.275 paf 863: if(value) {
1.277 paf 864: if(should_explode_junction) // process $junction, but leave $junction.xxx as is
1.275 paf 865: value=&process_to_value(*value); // process possible code-junction
866: } else
1.167 parser 867: value=NEW VVoid(pool());
1.63 paf 868:
1.17 paf 869: return value;
1.34 paf 870: }
1.70 paf 871:
1.162 parser 872: /** @param intercept_string
1.116 paf 873: - true:
874: they want result=string value,
875: possible object result goes to wcontext
876: - false:
877: they want any result[string|object]
878: nothing goes to wcontext.
1.125 paf 879: used in @c (expression) params evaluation
1.224 paf 880:
881: using the fact it's either string_ or value_ result requested to speed up checkes
1.116 paf 882: */
1.229 paf 883: StringOrValue Request::process(Value& input_value, bool intercept_string) {
1.228 paf 884: StringOrValue result;
1.224 paf 885: Junction *junction=input_value.get_junction();
1.70 paf 886: if(junction && junction->code) { // is it a code-junction?
1.92 paf 887: // process it
1.157 parser 888: #ifdef DEBUG_EXECUTE
1.158 parser 889: debug_printf(pool(), "ja->\n");
1.157 parser 890: #endif
1.238 paf 891:
1.257 paf 892: if(!junction->method_frame)
1.240 paf 893: throw Exception("parser.runtime",
894: 0,
895: "junction used outside of context");
896:
1.257 paf 897: Value *saved_self=self;
898: VMethodFrame *saved_method_frame=method_frame;
899: Value *saved_rcontext=rcontext;
900: WContext *saved_wcontext=wcontext;
1.240 paf 901:
1.272 paf 902: self=&junction->self;
1.257 paf 903: method_frame=junction->method_frame;
1.240 paf 904: rcontext=junction->rcontext;
1.225 paf 905:
1.109 paf 906: // for expression method params
907: // wcontext is set 0
908: // using the fact in decision "which wwrapper to use"
1.240 paf 909: bool using_code_frame=intercept_string && junction->wcontext;
1.109 paf 910: if(using_code_frame) {
1.71 paf 911: // almost plain wwrapper about junction wcontext,
912: // BUT intercepts string writes
1.268 paf 913: VCodeFrame local(pool(), *junction->wcontext, junction->wcontext);
1.225 paf 914: wcontext=&local;
1.207 paf 915:
1.225 paf 916: // execute it
1.228 paf 917: recoursion_checked_execute(0/*result_name*/, *junction->code);
1.226 paf 918:
1.71 paf 919: // CodeFrame soul:
920: // string writes were intercepted
921: // returning them as the result of getting code-junction
1.229 paf 922: result.set_string(*wcontext->get_string());
1.224 paf 923: } else {
1.225 paf 924: // plain wwrapper
1.267 paf 925: WWrapper local(pool(), 0/*empty*/, wcontext);
1.225 paf 926: wcontext=&local;
927:
928: // execute it
1.228 paf 929: recoursion_checked_execute(0/*result_name*/, *junction->code);
1.226 paf 930:
1.228 paf 931: result=wcontext->result();
1.224 paf 932: }
1.70 paf 933:
1.257 paf 934: wcontext=saved_wcontext;
935: rcontext=saved_rcontext;
936: method_frame=saved_method_frame;
937: self=saved_self;
1.70 paf 938:
1.157 parser 939: #ifdef DEBUG_EXECUTE
1.158 parser 940: debug_printf(pool(), "<-ja returned");
1.157 parser 941: #endif
1.224 paf 942: } else {
1.228 paf 943: result.set_value(input_value);
1.224 paf 944: }
1.228 paf 945: return result;
1.85 paf 946: }
947:
1.257 paf 948: const String& Request::execute_method(VMethodFrame& amethod_frame, const Method& method) {
949: Value *saved_self=self;
950: VMethodFrame *saved_method_frame=method_frame;
951: Value *saved_rcontext=rcontext;
952: WContext *saved_wcontext=wcontext;
1.99 paf 953:
954: // initialize contexts
1.257 paf 955: self=rcontext=wcontext=method_frame=&amethod_frame;
1.99 paf 956:
957: // execute!
958: execute(*method.parser_code);
959:
960: // result
1.242 paf 961: const String& result=wcontext->result().as_string();
1.208 paf 962:
1.257 paf 963: wcontext=saved_wcontext;
964: rcontext=saved_rcontext;
965: method_frame=saved_method_frame;
966: self=saved_self;
1.242 paf 967:
968: // return
969: return result;
1.208 paf 970: }
971:
1.242 paf 972: void Request::execute_method(Value& aself,
973: const Method& method, VString *optional_param,
974: const String **return_string) {
1.257 paf 975: Value *saved_self=self;
976: VMethodFrame *saved_method_frame=method_frame;
977: Value *saved_rcontext=rcontext;
978: WContext *saved_wcontext=wcontext;
1.208 paf 979:
980: // initialize contexts
1.257 paf 981: //method_frame=rcontext=self=&aself;
1.242 paf 982: self=&aself;
1.267 paf 983: // WWrapper local(pool(), &aself, wcontext);
1.242 paf 984: // wcontext=&local;
1.272 paf 985: Junction local_junction(pool(), *self, &method, 0,0,0,0);
1.282 ! paf 986: VMethodFrame local_frame(pool(), method.name, local_junction, method_frame/*caller*/);
1.242 paf 987: if(optional_param && local_frame.can_store_param()) {
988: local_frame.store_param(optional_param);
989: local_frame.fill_unspecified_params();
990: }
1.272 paf 991: local_frame.set_self(*self);
1.257 paf 992: rcontext=wcontext=method_frame=&local_frame;
1.246 paf 993:
994: // prevent non-string writes for better error reporting
995: if(return_string)
996: wcontext->write(local_frame);
1.208 paf 997:
998: // execute!
999: execute(*method.parser_code);
1000:
1001: // result
1.242 paf 1002: const String *result=0;
1.245 paf 1003: if(return_string)
1.242 paf 1004: *return_string=&wcontext->result().as_string();
1.99 paf 1005:
1.257 paf 1006: wcontext=saved_wcontext;
1007: rcontext=saved_rcontext;
1008: method_frame=saved_method_frame;
1009: self=saved_self;
1.242 paf 1010: }
1011:
1012: void Request::execute_nonvirtual_method(VStateless_class& aclass,
1.281 paf 1013: const String& method_name, VString *optional_param,
1.242 paf 1014: const String **return_string,
1015: const Method **return_method) {
1016:
1.281 paf 1017: const Method *method=aclass.get_method(method_name);
1.242 paf 1018: if(return_string)
1019: *return_string=0;
1020: if(return_method)
1021: *return_method=method;
1022:
1023: if(method)
1024: execute_method(aclass, *method, optional_param, return_string);
1.99 paf 1025: }
1026:
1.176 parser 1027: const String *Request::execute_virtual_method(Value& aself,
1.208 paf 1028: const String& method_name) {
1.252 paf 1029: if(Value *value=aself.get_element(method_name, &aself, false))
1.99 paf 1030: if(Junction *junction=value->get_junction())
1.241 paf 1031: if(const Method *method=junction->method) {
1032: const String *result;
1.242 paf 1033: execute_method(aself, *method, 0/*no params*/, &result);
1.241 paf 1034: return result;
1035: }
1.188 parser 1036:
1.176 parser 1037: return 0;
1038: }