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