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