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