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