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