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