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