|
|
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.226 ! paf 7: $Id: execute.C,v 1.225 2002/04/11 13:33:39 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.226 ! 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.208 paf 335: value=&wcontext->result();
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:
! 346:
1.49 paf 347: PUSH(wcontext);
1.226 ! paf 348: WWrapper local(pool(), 0 /*empty*/);
! 349: wcontext=&local;
! 350:
! 351: execute(*local_ops);
! 352:
1.49 paf 353: // from "$a $b" part of expression taking only string value,
354: // ignoring any other content of wcontext
1.82 paf 355: const String *string=wcontext->get_string();
1.80 paf 356: Value *value;
357: if(string)
358: value=NEW VString(*string);
359: else
1.167 parser 360: NEW VVoid(pool());
1.50 paf 361: wcontext=static_cast<WContext *>(POP());
1.49 paf 362: PUSH(value);
363: break;
364: }
365:
1.51 paf 366: // CALL
1.28 paf 367: case OP_GET_METHOD_FRAME:
368: {
1.197 parser 369: value=POP();
1.138 paf 370:
1.111 paf 371: // info:
372: // code compiled so that this one's always method-junction,
373: // not a code-junction
1.32 paf 374: Junction *junction=value->get_junction();
1.31 paf 375: if(!junction)
1.223 paf 376: throw Exception("parser.runtime",
1.42 paf 377: &value->name(),
1.111 paf 378: "(%s) not a method or junction, can not call it",
1.38 paf 379: value->type());
1.70 paf 380:
1.185 parser 381: VMethodFrame *frame=NEW VMethodFrame(pool(), value->name(), *junction);
1.28 paf 382: PUSH(frame);
383: break;
384: }
385: case OP_STORE_PARAM:
386: {
1.197 parser 387: value=POP();
1.73 paf 388: VMethodFrame *frame=static_cast<VMethodFrame *>(stack.top_value());
1.92 paf 389: frame->store_param(frame->name(), value);
1.29 paf 390: break;
391: }
392:
1.186 parser 393: case OP_PREPARE_TO_CONSTRUCT_OBJECT:
394: {
1.200 paf 395: wcontext->set_constructing(true);
1.186 parser 396: break;
397: }
1.219 paf 398:
399: case OP_PREPARE_TO_EXPRESSION:
400: {
401: wcontext->set_in_expression(true);
402: break;
403: }
404:
1.186 parser 405: case OP_CALL:
1.29 paf 406: {
1.157 parser 407: #ifdef DEBUG_EXECUTE
1.158 parser 408: debug_printf(pool(), "->\n");
1.157 parser 409: #endif
1.34 paf 410: VMethodFrame *frame=static_cast<VMethodFrame *>(POP());
411: frame->fill_unspecified_params();
1.45 paf 412: PUSH(self);
413: PUSH(root);
414: PUSH(rcontext);
415: PUSH(wcontext);
416:
1.100 paf 417: VStateless_class *called_class=frame->junction.self.get_class();
1.200 paf 418: if(wcontext->get_constructing()) {
419: wcontext->set_constructing(false);
1.185 parser 420: if(frame->junction.method->call_type!=Method::CT_STATIC) {
1.138 paf 421: // this is a constructor call
1.185 parser 422:
423: if(Value *value=called_class->create_new_value(pool())) {
1.204 paf 424: // some stateless_class creatable derivates
1.149 paf 425: self=value;
1.204 paf 426: } else
1.223 paf 427: throw Exception("parser.runtime",
1.204 paf 428: &frame->name(),
429: "is not a constructor, system class '%s' can be constructed only implicitly",
430: called_class->name().cstr());
431:
1.83 paf 432: frame->write(*self,
1.136 paf 433: String::UL_CLEAN // not used, always an object, not string
1.83 paf 434: );
1.185 parser 435: } else
1.223 paf 436: throw Exception("parser.runtime",
1.185 parser 437: &frame->name(),
438: "method is static and can not be used as constructor");
439: } else {
440: // this is not constructor call
441:
442: // not ^name.method call, name:method call; and
443: // is context object or class & is it my class or my parent's class and?
444: VStateless_class *read_class=rcontext->get_class();
445: if(
1.200 paf 446: !(wcontext->get_somebody_entered_some_object() &&
447: !wcontext->get_somebody_entered_some_class()) &&
1.185 parser 448: read_class && read_class->is_or_derived_from(*called_class)) // yes
449: self=rcontext; // dynamic call
450: else // no, not me or relative of mine (=total stranger)
451: self=&frame->junction.self; // static call
452: }
1.75 paf 453:
1.45 paf 454: frame->set_self(*self);
1.219 paf 455:
456: // see OP_PREPARE_TO_EXPRESSION
457: frame->set_in_expression(wcontext->get_in_expression());
458:
1.202 paf 459: rcontext=wcontext=frame;
1.47 paf 460: {
1.48 paf 461: // take object or class from any wrappers
1.102 paf 462: // and substitute class alias to the class they are called AS
463: Temp_alias temp_alias(*self->get_aliased(), *frame->junction.vclass);
1.68 paf 464:
1.99 paf 465: const Method& method=*frame->junction.method;
1.134 paf 466: Method::Call_type call_type=
467: called_class==self ? Method::CT_STATIC : Method::CT_DYNAMIC;
468: if(
469: method.call_type==Method::CT_ANY ||
1.197 parser 470: method.call_type==call_type) { // allowed call type?
471: try {
472: if(method.native_code) { // native code?
1.202 paf 473: // root unchanged, so that ^for ^foreach & co may write to locals
1.197 parser 474: method.check_actual_numbered_params(
475: frame->junction.self,
476: frame->name(), frame->numbered_params());
477: method.native_code(
478: *this,
479: frame->name(), frame->numbered_params()); // execute it
480: } else { // parser code
1.202 paf 481: root=frame;
1.225 paf 482: // execute it
483: recoursion_checked_execute(&frame->name(), *method.parser_code);
1.159 parser 484: }
1.197 parser 485: } catch(...) {
486: // record it to stack trace
1.222 paf 487: exception_trace.push((void *)&frame->name());
1.197 parser 488: /*re*/throw;
1.159 parser 489: }
1.197 parser 490: } else
1.223 paf 491: throw Exception("parser.runtime",
1.134 paf 492: &frame->name(),
493: "is not allowed to be called %s",
494: call_type==Method::CT_STATIC?"statically":"dynamically");
495:
1.47 paf 496: }
1.208 paf 497: value=&wcontext->result();
1.45 paf 498:
499: wcontext=static_cast<WContext *>(POP());
500: rcontext=POP();
501: root=POP();
502: self=static_cast<VAliased *>(POP());
1.220 paf 503:
504: #ifdef DEBUG_STRING_APPENDS_VS_EXPANDS
505: if(const String *s=value->get_string())
506: wcontext_result_size+=s->used_rows()*sizeof(String::Chunk::Row);
507: #endif
1.213 paf 508:
1.61 paf 509: PUSH(value);
1.157 parser 510: #ifdef DEBUG_EXECUTE
1.158 parser 511: debug_printf(pool(), "<-returned");
1.157 parser 512: #endif
1.49 paf 513: break;
514: }
515:
1.55 paf 516: // expression ops: unary
517: case OP_NEG:
518: {
519: Value *operand=POP();
1.197 parser 520: value=NEW VDouble(pool(), -operand->as_double());
1.55 paf 521: PUSH(value);
522: break;
523: }
524: case OP_INV:
525: {
526: Value *operand=POP();
1.197 parser 527: value=NEW VDouble(pool(), ~operand->as_int());
1.55 paf 528: PUSH(value);
529: break;
530: }
531: case OP_NOT:
532: {
533: Value *operand=POP();
1.197 parser 534: value=NEW VBool(pool(), !operand->as_bool());
1.55 paf 535: PUSH(value);
536: break;
537: }
1.62 paf 538: case OP_DEF:
539: {
540: Value *operand=POP();
1.197 parser 541: value=NEW VBool(pool(), operand->is_defined());
1.62 paf 542: PUSH(value);
543: break;
544: }
545: case OP_IN:
546: {
1.199 paf 547: /// @test String::cmp
1.62 paf 548: Value *operand=POP();
1.110 paf 549: const char *path=operand->as_string().cstr();
1.197 parser 550: value=NEW VBool(pool(),
1.151 paf 551: info.uri && strncmp(path, info.uri, strlen(path))==0);
1.62 paf 552: PUSH(value);
553: break;
554: }
555: case OP_FEXISTS:
556: {
557: Value *operand=POP();
1.197 parser 558: value=NEW VBool(pool(),
1.127 paf 559: file_readable(absolute(operand->as_string())));
1.148 paf 560: PUSH(value);
561: break;
562: }
563: case OP_DEXISTS:
564: {
565: Value *operand=POP();
1.197 parser 566: value=NEW VBool(pool(),
1.148 paf 567: dir_readable(absolute(operand->as_string())));
1.62 paf 568: PUSH(value);
569: break;
570: }
1.55 paf 571:
572: // expression ops: binary
573: case OP_SUB:
1.53 paf 574: {
1.197 parser 575: b=POP(); a=POP();
576: value=NEW VDouble(pool(), a->as_double() - b->as_double());
1.53 paf 577: PUSH(value);
578: break;
579: }
1.55 paf 580: case OP_ADD:
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.49 paf 587: case OP_MUL:
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: }
594: case OP_DIV:
595: {
1.197 parser 596: b=POP(); a=POP();
1.170 parser 597:
598: double a_double=a->as_double();
599: double b_double=b->as_double();
600:
1.171 parser 601: if(b_double == 0) {
602: const String *problem_source=&b->as_string();
603: #ifndef NO_STRING_ORIGIN
604: if(!problem_source->origin().file)
1.172 parser 605: problem_source=&b->name();
1.171 parser 606: #endif
1.223 paf 607: throw Exception("number.zerodivision",
1.171 parser 608: problem_source,
1.170 parser 609: "Division by zero");
1.171 parser 610: }
1.170 parser 611:
1.197 parser 612: value=NEW VDouble(pool(), a_double / b_double);
1.54 paf 613: PUSH(value);
614: break;
615: }
1.55 paf 616: case OP_MOD:
617: {
1.197 parser 618: b=POP(); a=POP();
1.170 parser 619:
1.173 parser 620: double a_double=a->as_double();
621: double b_double=b->as_double();
1.170 parser 622:
1.173 parser 623: if(b_double == 0) {
1.171 parser 624: const String *problem_source=&b->as_string();
625: #ifndef NO_STRING_ORIGIN
626: if(!problem_source->origin().file)
1.172 parser 627: problem_source=&b->name();
1.171 parser 628: #endif
1.223 paf 629: throw Exception("number.zerodivision",
1.171 parser 630: problem_source,
1.170 parser 631: "Modulus by zero");
1.171 parser 632: }
1.170 parser 633:
1.197 parser 634: value=NEW VDouble(pool(), fmod(a_double, b_double));
1.201 paf 635: PUSH(value);
636: break;
637: }
638: case OP_INTDIV:
639: {
640: b=POP(); a=POP();
641:
642: int a_int=a->as_int();
643: int b_int=b->as_int();
644:
645: if(b_int == 0) {
646: const String *problem_source=&b->as_string();
647: #ifndef NO_STRING_ORIGIN
648: if(!problem_source->origin().file)
649: problem_source=&b->name();
650: #endif
1.223 paf 651: throw Exception("number.zerodivision",
1.201 paf 652: problem_source,
653: "Division by zero");
654: }
655:
656: value=NEW VInt(pool(), a_int / b_int);
1.55 paf 657: PUSH(value);
658: break;
659: }
660: case OP_BIN_AND:
1.54 paf 661: {
1.197 parser 662: b=POP(); a=POP();
663: value=NEW VDouble(pool(),
1.155 parser 664: a->as_int() &
665: b->as_int());
1.54 paf 666: PUSH(value);
667: break;
668: }
1.55 paf 669: case OP_BIN_OR:
1.54 paf 670: {
1.197 parser 671: b=POP(); a=POP();
672: value=NEW VDouble(pool(),
1.155 parser 673: a->as_int() |
674: b->as_int());
1.55 paf 675: PUSH(value);
676: break;
677: }
1.56 paf 678: case OP_BIN_XOR:
679: {
1.197 parser 680: b=POP(); a=POP();
681: value=NEW VDouble(pool(),
1.155 parser 682: a->as_int() ^
683: b->as_int());
1.56 paf 684: PUSH(value);
685: break;
686: }
1.55 paf 687: case OP_LOG_AND:
688: {
1.209 paf 689: b_code=POP_CODE(); a=POP();
690: bool result;
691: if(a->as_bool()) {
692: execute(*b_code);
693: b=POP();
694: result=b->as_bool();
695: } else
696: result=false;
697: value=NEW VBool(pool(), result);
1.55 paf 698: PUSH(value);
699: break;
700: }
701: case OP_LOG_OR:
702: {
1.209 paf 703: b_code=POP_CODE(); a=POP();
704: bool result;
705: if(a->as_bool())
706: result=true;
707: else {
708: execute(*b_code);
709: b=POP();
710: result=b->as_bool();
711: }
712: value=NEW VBool(pool(), result);
1.56 paf 713: PUSH(value);
714: break;
715: }
716: case OP_LOG_XOR:
717: {
1.197 parser 718: b=POP(); a=POP();
719: value=NEW VBool(pool(), a->as_bool() ^ b->as_bool());
1.55 paf 720: PUSH(value);
721: break;
722: }
723: case OP_NUM_LT:
724: {
1.197 parser 725: b=POP(); a=POP();
726: value=NEW VBool(pool(), a->as_double() < b->as_double());
1.55 paf 727: PUSH(value);
728: break;
729: }
730: case OP_NUM_GT:
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_LE:
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_GE:
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_EQ:
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_NE:
759: {
1.197 parser 760: b=POP(); a=POP();
761: value=NEW VBool(pool(), a->as_double() != b->as_double());
1.54 paf 762: PUSH(value);
763: break;
764: }
1.58 paf 765: case OP_STR_LT:
766: {
1.197 parser 767: b=POP(); a=POP();
768: value=NEW VBool(pool(), a->as_string() < b->as_string());
1.58 paf 769: PUSH(value);
770: break;
771: }
772: case OP_STR_GT:
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: }
1.55 paf 779: case OP_STR_LE:
1.58 paf 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_GE:
1.54 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: }
793: case OP_STR_EQ:
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_NE:
801: {
1.197 parser 802: b=POP(); a=POP();
803: value=NEW VBool(pool(), a->as_string() != b->as_string());
1.103 paf 804: PUSH(value);
805: break;
806: }
807: case OP_IS:
808: {
1.191 parser 809: //_asm int 3;
1.197 parser 810: b=POP(); a=POP();
811: value=NEW VBool(pool(), b->as_string() == a->type());
1.49 paf 812: PUSH(value);
1.28 paf 813: break;
814: }
815:
1.11 paf 816: default:
1.223 paf 817: throw Exception(0,
1.67 paf 818: 0,
1.139 paf 819: "invalid opcode %d", op.code);
1.11 paf 820: }
821: }
1.1 paf 822: }
1.17 paf 823:
1.215 paf 824: Value *Request::get_element(bool can_call_operator) {
1.82 paf 825: const String& name=POP_NAME();
1.24 paf 826: Value *ncontext=POP();
1.216 paf 827: Value *value=0;
828: if(can_call_operator)
829: if(Method* method=OP.get_method(name)) { // looking operator of that name FIRST
1.181 parser 830: // as if that method were in self and we have normal dynamic method here
831: Junction& junction=*NEW Junction(pool(),
1.202 paf 832: *root, self->get_class(), method, 0,0,0,0);
1.181 parser 833: value=NEW VJunction(junction);
834: }
1.216 paf 835: if(!value)
836: value=ncontext->get_element(name);
1.76 paf 837: if(value)
1.224 paf 838: value=&process_to_value(*value, &name); // process possible code-junction
1.76 paf 839: else {
1.167 parser 840: value=NEW VVoid(pool());
1.76 paf 841: value->set_name(name);
842: }
1.63 paf 843:
1.17 paf 844: return value;
1.34 paf 845: }
1.70 paf 846:
1.162 parser 847: /** @param intercept_string
1.116 paf 848: - true:
849: they want result=string value,
850: possible object result goes to wcontext
851: - false:
852: they want any result[string|object]
853: nothing goes to wcontext.
1.125 paf 854: used in @c (expression) params evaluation
1.224 paf 855:
856: using the fact it's either string_ or value_ result requested to speed up checkes
1.116 paf 857: */
1.224 paf 858: void Request::process_internal(
859: Value& input_value, const String *result_name,
860: bool intercept_string,
1.226 ! paf 861: const String **string_result, Value **value_result,
! 862: void (*postexecute)(void *info), void *postexecute_info) {
1.224 paf 863: Junction *junction=input_value.get_junction();
1.70 paf 864: if(junction && junction->code) { // is it a code-junction?
1.92 paf 865: // process it
1.157 parser 866: #ifdef DEBUG_EXECUTE
1.158 parser 867: debug_printf(pool(), "ja->\n");
1.157 parser 868: #endif
1.70 paf 869: PUSH(self);
870: PUSH(root);
871: PUSH(rcontext);
1.129 paf 872: PUSH(wcontext);
1.70 paf 873:
1.225 paf 874: self=&junction->self;
875: root=junction->root;
876: rcontext=junction->rcontext;
877:
1.109 paf 878: // for expression method params
879: // wcontext is set 0
880: // using the fact in decision "which wwrapper to use"
881: bool using_code_frame=intercept_string && junction->wcontext;
882: if(using_code_frame) {
1.71 paf 883: // almost plain wwrapper about junction wcontext,
884: // BUT intercepts string writes
1.225 paf 885: VCodeFrame local(pool(), *junction->wcontext);
886: wcontext=&local;
1.207 paf 887:
1.225 paf 888: // execute it
889: recoursion_checked_execute(result_name, *junction->code);
1.226 ! paf 890:
! 891: // maybe-postexecute something
! 892: if(postexecute)
! 893: postexecute(postexecute_info);
1.207 paf 894:
1.71 paf 895: // CodeFrame soul:
896: // string writes were intercepted
897: // returning them as the result of getting code-junction
1.224 paf 898: if(string_result)
1.225 paf 899: *string_result=wcontext->get_string();
900: else {
901: *value_result=NEW VString(*wcontext->get_string());
902: if(result_name)
903: (*value_result)->set_name(*result_name);
904: }
1.224 paf 905: } else {
1.225 paf 906: // plain wwrapper
907: WWrapper local(pool(), 0/*empty*/);
908: wcontext=&local;
909:
910: // execute it
911: recoursion_checked_execute(result_name, *junction->code);
912:
1.226 ! paf 913: // maybe-postexecute something
! 914: if(postexecute)
! 915: postexecute(postexecute_info);
! 916:
1.224 paf 917: if(string_result)
1.225 paf 918: *string_result=&wcontext->result().as_string();
1.224 paf 919: else {
1.225 paf 920: *value_result=&wcontext->result();
1.224 paf 921: if(result_name)
922: (*value_result)->set_name(*result_name);
923: }
924: }
1.70 paf 925:
926: wcontext=static_cast<WContext *>(POP());
927: rcontext=POP();
928: root=POP();
929: self=static_cast<VAliased *>(POP());
930:
1.157 parser 931: #ifdef DEBUG_EXECUTE
1.158 parser 932: debug_printf(pool(), "<-ja returned");
1.157 parser 933: #endif
1.224 paf 934: } else {
935: if(string_result) // they asked for string_result
936: *string_result=&input_value.as_string();
937: else {// they asked for value_result
938: *value_result=&input_value;
939: if(result_name)
940: (*value_result)->set_name(*result_name);
941: }
942: }
1.85 paf 943: }
944:
1.208 paf 945: const String *Request::execute_method(Value& aself, const Method& method,
946: bool return_cstr) {
1.99 paf 947: PUSH(self);
948: PUSH(root);
949: PUSH(rcontext);
950: PUSH(wcontext);
951:
952: // initialize contexts
953: root=rcontext=self=&aself;
1.226 ! paf 954: WWrapper local(pool(), &aself);
! 955: wcontext=&local;
1.99 paf 956:
957: // execute!
958: execute(*method.parser_code);
959:
960: // result
1.208 paf 961: const String *result=return_cstr ? &wcontext->as_string() : 0;
962:
963: wcontext=static_cast<WContext *>(POP());
964: rcontext=POP();
965: root=POP();
966: self=static_cast<VAliased *>(POP());
967:
968: // return
969: return result;
970: }
971:
972: const String& Request::execute_method(VMethodFrame& amethodFrame, const Method& method) {
973: PUSH(self);
974: PUSH(root);
975: PUSH(rcontext);
976: PUSH(wcontext);
977:
978: // initialize contexts
979: root=rcontext=self=&amethodFrame;
980: wcontext=&amethodFrame;
981:
982: // execute!
983: execute(*method.parser_code);
984:
985: // result
986: const String& result=wcontext->as_string();
1.99 paf 987:
988: wcontext=static_cast<WContext *>(POP());
989: rcontext=POP();
990: root=POP();
991: self=static_cast<VAliased *>(POP());
992:
993: // return
994: return result;
995: }
996:
1.176 parser 997: const String *Request::execute_virtual_method(Value& aself,
1.208 paf 998: const String& method_name) {
1.99 paf 999: if(Value *value=aself.get_element(method_name))
1000: if(Junction *junction=value->get_junction())
1001: if(const Method *method=junction->method)
1.208 paf 1002: return execute_method(aself, *method, true /*return_cstr*/);
1.188 parser 1003:
1.176 parser 1004: return 0;
1005: }
1006:
1.178 parser 1007: const String *Request::execute_nonvirtual_method(VStateless_class& aclass,
1.208 paf 1008: const String& method_name,
1.176 parser 1009: bool return_cstr) {
1.178 parser 1010: if(const Method *method=aclass.get_method(method_name))
1.179 parser 1011: return execute_method(aclass, *method, return_cstr);
1.99 paf 1012:
1.85 paf 1013: return 0;
1.70 paf 1014: }