|
|
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.276 ! paf 8: static const char* IDENT_EXECUTE_C="$Date: 2002/09/20 09:26:32 $";
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: Junction *junction=value->get_junction();
396: if(!junction)
397: throw Exception("parser.runtime",
398: last_get_element_name,
399: "(%s) not a method or junction, can not call it",
1.276 ! paf 400: value->type());
! 401: // check:
! 402: // that this is method-junction, not a code-junction
! 403: if(!junction->method)
! 404: throw Exception("parser.runtime",
! 405: last_get_element_name,
! 406: "(%s) is code junction, can not call it",
1.237 paf 407: value->type());
408:
409: VMethodFrame frame(pool(), *last_get_element_name, *junction);
410: if(local_ops){ // store param code goes here
411: PUSH(&frame); // argument for *STORE_PARAM ops
412: execute(*local_ops);
413: POP();
414: }
415: frame.fill_unspecified_params();
1.257 paf 416: Value *saved_self=self;
417: VMethodFrame *saved_method_frame=method_frame;
418: Value *saved_rcontext=rcontext;
419: WContext *saved_wcontext=wcontext;
1.45 paf 420:
1.272 paf 421: VStateless_class *called_class=frame.junction.self.get_class();
1.200 paf 422: if(wcontext->get_constructing()) {
423: wcontext->set_constructing(false);
1.237 paf 424: if(frame.junction.method->call_type!=Method::CT_STATIC) {
1.138 paf 425: // this is a constructor call
1.185 parser 426:
427: if(Value *value=called_class->create_new_value(pool())) {
1.204 paf 428: // some stateless_class creatable derivates
1.149 paf 429: self=value;
1.204 paf 430: } else
1.223 paf 431: throw Exception("parser.runtime",
1.237 paf 432: &frame.name(),
1.204 paf 433: "is not a constructor, system class '%s' can be constructed only implicitly",
434: called_class->name().cstr());
435:
1.237 paf 436: frame.write(*self,
1.136 paf 437: String::UL_CLEAN // not used, always an object, not string
1.83 paf 438: );
1.185 parser 439: } else
1.223 paf 440: throw Exception("parser.runtime",
1.237 paf 441: &frame.name(),
1.185 parser 442: "method is static and can not be used as constructor");
1.249 paf 443: } else
1.272 paf 444: self=&frame.junction.self;
1.75 paf 445:
1.272 paf 446: frame.set_self(*self);
1.219 paf 447:
448: // see OP_PREPARE_TO_EXPRESSION
1.237 paf 449: frame.set_in_expression(wcontext->get_in_expression());
1.219 paf 450:
1.237 paf 451: rcontext=wcontext=&frame;
1.47 paf 452: {
1.237 paf 453: const Method& method=*frame.junction.method;
1.134 paf 454: Method::Call_type call_type=
455: called_class==self ? Method::CT_STATIC : Method::CT_DYNAMIC;
456: if(
457: method.call_type==Method::CT_ANY ||
1.197 parser 458: method.call_type==call_type) { // allowed call type?
459: try {
460: if(method.native_code) { // native code?
1.257 paf 461: // method_frame unchanged, so that ^for ^foreach & co may write to locals
1.197 parser 462: method.check_actual_numbered_params(
1.237 paf 463: frame.junction.self,
464: frame.name(), frame.numbered_params());
1.197 parser 465: method.native_code(
466: *this,
1.237 paf 467: frame.name(), frame.numbered_params()); // execute it
1.197 parser 468: } else { // parser code
1.257 paf 469: method_frame=&frame;
1.225 paf 470: // execute it
1.237 paf 471: recoursion_checked_execute(&frame.name(), *method.parser_code);
1.159 parser 472: }
1.197 parser 473: } catch(...) {
474: // record it to stack trace
1.237 paf 475: exception_trace.push((void *)&frame.name());
1.197 parser 476: /*re*/throw;
1.159 parser 477: }
1.197 parser 478: } else
1.223 paf 479: throw Exception("parser.runtime",
1.237 paf 480: &frame.name(),
1.134 paf 481: "is not allowed to be called %s",
482: call_type==Method::CT_STATIC?"statically":"dynamically");
483:
1.47 paf 484: }
1.232 paf 485: StringOrValue result=wcontext->result();
1.45 paf 486:
1.257 paf 487: wcontext=saved_wcontext;
488: rcontext=saved_rcontext;
489: method_frame=saved_method_frame;
490: self=saved_self;
1.220 paf 491:
492: #ifdef DEBUG_STRING_APPENDS_VS_EXPANDS
493: if(const String *s=value->get_string())
494: wcontext_result_size+=s->used_rows()*sizeof(String::Chunk::Row);
495: #endif
1.213 paf 496:
1.232 paf 497: if(op.code==OP_CALL__WRITE) {
1.236 paf 498: write_assign_lang(result, last_get_element_name);
1.232 paf 499: } else { // OP_CALL
500: PUSH(&result.as_value());
501: }
502:
1.157 parser 503: #ifdef DEBUG_EXECUTE
1.158 parser 504: debug_printf(pool(), "<-returned");
1.157 parser 505: #endif
1.49 paf 506: break;
507: }
508:
1.55 paf 509: // expression ops: unary
510: case OP_NEG:
511: {
512: Value *operand=POP();
1.197 parser 513: value=NEW VDouble(pool(), -operand->as_double());
1.55 paf 514: PUSH(value);
515: break;
516: }
517: case OP_INV:
518: {
519: Value *operand=POP();
1.197 parser 520: value=NEW VDouble(pool(), ~operand->as_int());
1.55 paf 521: PUSH(value);
522: break;
523: }
524: case OP_NOT:
525: {
526: Value *operand=POP();
1.197 parser 527: value=NEW VBool(pool(), !operand->as_bool());
1.55 paf 528: PUSH(value);
529: break;
530: }
1.62 paf 531: case OP_DEF:
532: {
533: Value *operand=POP();
1.197 parser 534: value=NEW VBool(pool(), operand->is_defined());
1.62 paf 535: PUSH(value);
536: break;
537: }
538: case OP_IN:
539: {
1.199 paf 540: /// @test String::cmp
1.62 paf 541: Value *operand=POP();
1.110 paf 542: const char *path=operand->as_string().cstr();
1.197 parser 543: value=NEW VBool(pool(),
1.151 paf 544: info.uri && strncmp(path, info.uri, strlen(path))==0);
1.62 paf 545: PUSH(value);
546: break;
547: }
548: case OP_FEXISTS:
549: {
550: Value *operand=POP();
1.197 parser 551: value=NEW VBool(pool(),
1.127 paf 552: file_readable(absolute(operand->as_string())));
1.148 paf 553: PUSH(value);
554: break;
555: }
556: case OP_DEXISTS:
557: {
558: Value *operand=POP();
1.197 parser 559: value=NEW VBool(pool(),
1.148 paf 560: dir_readable(absolute(operand->as_string())));
1.62 paf 561: PUSH(value);
562: break;
563: }
1.55 paf 564:
565: // expression ops: binary
566: case OP_SUB:
1.53 paf 567: {
1.197 parser 568: b=POP(); a=POP();
569: value=NEW VDouble(pool(), a->as_double() - b->as_double());
1.53 paf 570: PUSH(value);
571: break;
572: }
1.55 paf 573: case OP_ADD:
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.49 paf 580: case OP_MUL:
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: }
587: case OP_DIV:
588: {
1.197 parser 589: b=POP(); a=POP();
1.170 parser 590:
591: double a_double=a->as_double();
592: double b_double=b->as_double();
593:
1.171 parser 594: if(b_double == 0) {
595: const String *problem_source=&b->as_string();
1.223 paf 596: throw Exception("number.zerodivision",
1.171 parser 597: problem_source,
1.170 parser 598: "Division by zero");
1.171 parser 599: }
1.170 parser 600:
1.197 parser 601: value=NEW VDouble(pool(), a_double / b_double);
1.54 paf 602: PUSH(value);
603: break;
604: }
1.55 paf 605: case OP_MOD:
606: {
1.197 parser 607: b=POP(); a=POP();
1.170 parser 608:
1.173 parser 609: double a_double=a->as_double();
610: double b_double=b->as_double();
1.170 parser 611:
1.173 parser 612: if(b_double == 0) {
1.171 parser 613: const String *problem_source=&b->as_string();
1.223 paf 614: throw Exception("number.zerodivision",
1.171 parser 615: problem_source,
1.170 parser 616: "Modulus by zero");
1.171 parser 617: }
1.170 parser 618:
1.197 parser 619: value=NEW VDouble(pool(), fmod(a_double, b_double));
1.201 paf 620: PUSH(value);
621: break;
622: }
623: case OP_INTDIV:
624: {
625: b=POP(); a=POP();
626:
627: int a_int=a->as_int();
628: int b_int=b->as_int();
629:
630: if(b_int == 0) {
631: const String *problem_source=&b->as_string();
1.223 paf 632: throw Exception("number.zerodivision",
1.201 paf 633: problem_source,
634: "Division by zero");
635: }
636:
637: value=NEW VInt(pool(), a_int / b_int);
1.55 paf 638: PUSH(value);
639: break;
640: }
1.274 paf 641: case OP_BIN_SL:
642: {
643: b=POP(); a=POP();
644: value=NEW VInt(pool(),
645: a->as_int() <<
646: b->as_int());
647: PUSH(value);
648: break;
649: }
650: case OP_BIN_SR:
651: {
652: b=POP(); a=POP();
653: value=NEW VInt(pool(),
654: a->as_int() >>
655: b->as_int());
656: PUSH(value);
657: break;
658: }
1.55 paf 659: case OP_BIN_AND:
1.54 paf 660: {
1.197 parser 661: b=POP(); a=POP();
1.274 paf 662: value=NEW VInt(pool(),
1.155 parser 663: a->as_int() &
664: b->as_int());
1.54 paf 665: PUSH(value);
666: break;
667: }
1.55 paf 668: case OP_BIN_OR:
1.54 paf 669: {
1.197 parser 670: b=POP(); a=POP();
1.274 paf 671: value=NEW VInt(pool(),
1.155 parser 672: a->as_int() |
673: b->as_int());
1.55 paf 674: PUSH(value);
675: break;
676: }
1.56 paf 677: case OP_BIN_XOR:
678: {
1.197 parser 679: b=POP(); a=POP();
1.274 paf 680: value=NEW VInt(pool(),
1.155 parser 681: a->as_int() ^
682: b->as_int());
1.56 paf 683: PUSH(value);
684: break;
685: }
1.55 paf 686: case OP_LOG_AND:
687: {
1.209 paf 688: b_code=POP_CODE(); a=POP();
1.263 paf 689: bool result;
1.209 paf 690: if(a->as_bool()) {
691: execute(*b_code);
692: b=POP();
1.263 paf 693: result=b->as_bool();
1.209 paf 694: } else
1.263 paf 695: result=false;
696: value=NEW VBool(pool(), result);
1.55 paf 697: PUSH(value);
698: break;
699: }
700: case OP_LOG_OR:
701: {
1.209 paf 702: b_code=POP_CODE(); a=POP();
1.263 paf 703: bool result;
1.209 paf 704: if(a->as_bool())
1.263 paf 705: result=true;
1.209 paf 706: else {
707: execute(*b_code);
708: b=POP();
1.263 paf 709: result=b->as_bool();
1.209 paf 710: }
1.263 paf 711: value=NEW VBool(pool(), result);
1.56 paf 712: PUSH(value);
713: break;
714: }
715: case OP_LOG_XOR:
716: {
1.197 parser 717: b=POP(); a=POP();
718: value=NEW VBool(pool(), a->as_bool() ^ b->as_bool());
1.55 paf 719: PUSH(value);
720: break;
721: }
722: case OP_NUM_LT:
723: {
1.197 parser 724: b=POP(); a=POP();
1.263 paf 725: double result=a->as_double() - b->as_double();
726: value=NEW VBool(pool(), result < 0.0);
1.55 paf 727: PUSH(value);
728: break;
729: }
730: case OP_NUM_GT:
731: {
1.197 parser 732: b=POP(); a=POP();
1.263 paf 733: double result=a->as_double() - b->as_double();
734: value=NEW VBool(pool(), result > 0.0);
1.55 paf 735: PUSH(value);
736: break;
737: }
738: case OP_NUM_LE:
739: {
1.197 parser 740: b=POP(); a=POP();
1.263 paf 741: double result=a->as_double() - b->as_double();
742: value=NEW VBool(pool(), result <= 0.0);
1.55 paf 743: PUSH(value);
744: break;
745: }
746: case OP_NUM_GE:
747: {
1.197 parser 748: b=POP(); a=POP();
1.263 paf 749: double result=a->as_double() - b->as_double();
750: value=NEW VBool(pool(), result >= 0.0);
1.55 paf 751: PUSH(value);
752: break;
753: }
754: case OP_NUM_EQ:
755: {
1.197 parser 756: b=POP(); a=POP();
1.263 paf 757: double result=a->as_double() - b->as_double();
758: value=NEW VBool(pool(), result == 0.0);
1.55 paf 759: PUSH(value);
760: break;
761: }
762: case OP_NUM_NE:
763: {
1.197 parser 764: b=POP(); a=POP();
1.263 paf 765: double result=a->as_double() - b->as_double();
766: value=NEW VBool(pool(), result != 0.0);
1.54 paf 767: PUSH(value);
768: break;
769: }
1.58 paf 770: case OP_STR_LT:
771: {
1.197 parser 772: b=POP(); a=POP();
773: value=NEW VBool(pool(), a->as_string() < b->as_string());
1.58 paf 774: PUSH(value);
775: break;
776: }
777: case OP_STR_GT:
778: {
1.197 parser 779: b=POP(); a=POP();
780: value=NEW VBool(pool(), a->as_string() > b->as_string());
1.58 paf 781: PUSH(value);
782: break;
783: }
1.55 paf 784: case OP_STR_LE:
1.58 paf 785: {
1.197 parser 786: b=POP(); a=POP();
787: value=NEW VBool(pool(), a->as_string() <= b->as_string());
1.58 paf 788: PUSH(value);
789: break;
790: }
1.55 paf 791: case OP_STR_GE:
1.54 paf 792: {
1.197 parser 793: b=POP(); a=POP();
794: value=NEW VBool(pool(), a->as_string() >= b->as_string());
1.58 paf 795: PUSH(value);
796: break;
797: }
798: case OP_STR_EQ:
799: {
1.197 parser 800: b=POP(); a=POP();
801: value=NEW VBool(pool(), a->as_string() == b->as_string());
1.58 paf 802: PUSH(value);
803: break;
804: }
805: case OP_STR_NE:
806: {
1.197 parser 807: b=POP(); a=POP();
808: value=NEW VBool(pool(), a->as_string() != b->as_string());
1.103 paf 809: PUSH(value);
810: break;
811: }
812: case OP_IS:
813: {
1.197 parser 814: b=POP(); a=POP();
1.255 paf 815: value=NEW VBool(pool(), a->is(b->as_string().cstr()));
1.49 paf 816: PUSH(value);
1.28 paf 817: break;
818: }
819:
1.11 paf 820: default:
1.223 paf 821: throw Exception(0,
1.67 paf 822: 0,
1.139 paf 823: "invalid opcode %d", op.code);
1.11 paf 824: }
825: }
1.1 paf 826: }
1.17 paf 827:
1.231 paf 828: /// @test cache|prepare junctions
1.275 paf 829: Value *Request::get_element(const String *& remember_name,
830: bool can_call_operator, bool sould_explode_junction) {
1.236 paf 831: const String& name=POP_NAME(); remember_name=&name;
1.24 paf 832: Value *ncontext=POP();
1.216 paf 833: Value *value=0;
1.249 paf 834: if(can_call_operator) {
1.216 paf 835: if(Method* method=OP.get_method(name)) { // looking operator of that name FIRST
1.181 parser 836: // as if that method were in self and we have normal dynamic method here
837: Junction& junction=*NEW Junction(pool(),
1.273 paf 838: *method_frame, method, 0,0,0,0);
1.181 parser 839: value=NEW VJunction(junction);
840: }
1.249 paf 841: }
842: if(!value) {
843: if(!wcontext->get_constructing() // not constructing
844: && wcontext->get_somebody_entered_some_class() ) // ^class:method
845: if(VStateless_class *called_class=ncontext->get_class())
846: if(VStateless_class *read_class=rcontext->get_class())
847: if(read_class->derived_from(*called_class)) // current derived from called
848: if(Value *base_object=self->base_object()) { // doing DYNAMIC call
1.256 paf 849: Temp_derived temp_derived(*base_object, 0); // temporarily prevent go-back-down virtual calls
1.252 paf 850: value=base_object->get_element(name, base_object, false); // virtual-up lookup starting from parent
1.249 paf 851: goto _void;
852: }
853: }
1.216 paf 854: if(!value)
1.252 paf 855: value=ncontext->get_element(name, ncontext, false);
1.249 paf 856:
857: _void:
1.275 paf 858: if(value) {
859: if(sould_explode_junction) // process $junction, but leave $junction.xxx as is
860: value=&process_to_value(*value); // process possible code-junction
861: } else
1.167 parser 862: value=NEW VVoid(pool());
1.63 paf 863:
1.17 paf 864: return value;
1.34 paf 865: }
1.70 paf 866:
1.162 parser 867: /** @param intercept_string
1.116 paf 868: - true:
869: they want result=string value,
870: possible object result goes to wcontext
871: - false:
872: they want any result[string|object]
873: nothing goes to wcontext.
1.125 paf 874: used in @c (expression) params evaluation
1.224 paf 875:
876: using the fact it's either string_ or value_ result requested to speed up checkes
1.116 paf 877: */
1.229 paf 878: StringOrValue Request::process(Value& input_value, bool intercept_string) {
1.228 paf 879: StringOrValue result;
1.224 paf 880: Junction *junction=input_value.get_junction();
1.70 paf 881: if(junction && junction->code) { // is it a code-junction?
1.92 paf 882: // process it
1.157 parser 883: #ifdef DEBUG_EXECUTE
1.158 parser 884: debug_printf(pool(), "ja->\n");
1.157 parser 885: #endif
1.238 paf 886:
1.257 paf 887: if(!junction->method_frame)
1.240 paf 888: throw Exception("parser.runtime",
889: 0,
890: "junction used outside of context");
891:
1.257 paf 892: Value *saved_self=self;
893: VMethodFrame *saved_method_frame=method_frame;
894: Value *saved_rcontext=rcontext;
895: WContext *saved_wcontext=wcontext;
1.240 paf 896:
1.272 paf 897: self=&junction->self;
1.257 paf 898: method_frame=junction->method_frame;
1.240 paf 899: rcontext=junction->rcontext;
1.225 paf 900:
1.109 paf 901: // for expression method params
902: // wcontext is set 0
903: // using the fact in decision "which wwrapper to use"
1.240 paf 904: bool using_code_frame=intercept_string && junction->wcontext;
1.109 paf 905: if(using_code_frame) {
1.71 paf 906: // almost plain wwrapper about junction wcontext,
907: // BUT intercepts string writes
1.268 paf 908: VCodeFrame local(pool(), *junction->wcontext, junction->wcontext);
1.225 paf 909: wcontext=&local;
1.207 paf 910:
1.225 paf 911: // execute it
1.228 paf 912: recoursion_checked_execute(0/*result_name*/, *junction->code);
1.226 paf 913:
1.71 paf 914: // CodeFrame soul:
915: // string writes were intercepted
916: // returning them as the result of getting code-junction
1.229 paf 917: result.set_string(*wcontext->get_string());
1.224 paf 918: } else {
1.225 paf 919: // plain wwrapper
1.267 paf 920: WWrapper local(pool(), 0/*empty*/, wcontext);
1.225 paf 921: wcontext=&local;
922:
923: // execute it
1.228 paf 924: recoursion_checked_execute(0/*result_name*/, *junction->code);
1.226 paf 925:
1.228 paf 926: result=wcontext->result();
1.224 paf 927: }
1.70 paf 928:
1.257 paf 929: wcontext=saved_wcontext;
930: rcontext=saved_rcontext;
931: method_frame=saved_method_frame;
932: self=saved_self;
1.70 paf 933:
1.157 parser 934: #ifdef DEBUG_EXECUTE
1.158 parser 935: debug_printf(pool(), "<-ja returned");
1.157 parser 936: #endif
1.224 paf 937: } else {
1.228 paf 938: result.set_value(input_value);
1.224 paf 939: }
1.228 paf 940: return result;
1.85 paf 941: }
942:
1.257 paf 943: const String& Request::execute_method(VMethodFrame& amethod_frame, const Method& method) {
944: Value *saved_self=self;
945: VMethodFrame *saved_method_frame=method_frame;
946: Value *saved_rcontext=rcontext;
947: WContext *saved_wcontext=wcontext;
1.99 paf 948:
949: // initialize contexts
1.257 paf 950: self=rcontext=wcontext=method_frame=&amethod_frame;
1.99 paf 951:
952: // execute!
953: execute(*method.parser_code);
954:
955: // result
1.242 paf 956: const String& result=wcontext->result().as_string();
1.208 paf 957:
1.257 paf 958: wcontext=saved_wcontext;
959: rcontext=saved_rcontext;
960: method_frame=saved_method_frame;
961: self=saved_self;
1.242 paf 962:
963: // return
964: return result;
1.208 paf 965: }
966:
1.242 paf 967: void Request::execute_method(Value& aself,
968: const Method& method, VString *optional_param,
969: const String **return_string) {
1.257 paf 970: Value *saved_self=self;
971: VMethodFrame *saved_method_frame=method_frame;
972: Value *saved_rcontext=rcontext;
973: WContext *saved_wcontext=wcontext;
1.208 paf 974:
975: // initialize contexts
1.257 paf 976: //method_frame=rcontext=self=&aself;
1.242 paf 977: self=&aself;
1.267 paf 978: // WWrapper local(pool(), &aself, wcontext);
1.242 paf 979: // wcontext=&local;
1.272 paf 980: Junction local_junction(pool(), *self, &method, 0,0,0,0);
1.242 paf 981: VMethodFrame local_frame(pool(), method.name, local_junction);
982: if(optional_param && local_frame.can_store_param()) {
983: local_frame.store_param(optional_param);
984: local_frame.fill_unspecified_params();
985: }
1.272 paf 986: local_frame.set_self(*self);
1.257 paf 987: rcontext=wcontext=method_frame=&local_frame;
1.246 paf 988:
989: // prevent non-string writes for better error reporting
990: if(return_string)
991: wcontext->write(local_frame);
1.208 paf 992:
993: // execute!
994: execute(*method.parser_code);
995:
996: // result
1.242 paf 997: const String *result=0;
1.245 paf 998: if(return_string)
1.242 paf 999: *return_string=&wcontext->result().as_string();
1.99 paf 1000:
1.257 paf 1001: wcontext=saved_wcontext;
1002: rcontext=saved_rcontext;
1003: method_frame=saved_method_frame;
1004: self=saved_self;
1.242 paf 1005: }
1006:
1007: void Request::execute_nonvirtual_method(VStateless_class& aclass,
1008: const String& method_name, VString *optional_param,
1009: const String **return_string,
1010: const Method **return_method) {
1011:
1012: const Method *method=aclass.get_method(method_name);
1013: if(return_string)
1014: *return_string=0;
1015: if(return_method)
1016: *return_method=method;
1017:
1018: if(method)
1019: execute_method(aclass, *method, optional_param, return_string);
1.99 paf 1020: }
1021:
1.176 parser 1022: const String *Request::execute_virtual_method(Value& aself,
1.208 paf 1023: const String& method_name) {
1.252 paf 1024: if(Value *value=aself.get_element(method_name, &aself, false))
1.99 paf 1025: if(Junction *junction=value->get_junction())
1.241 paf 1026: if(const Method *method=junction->method) {
1027: const String *result;
1.242 paf 1028: execute_method(aself, *method, 0/*no params*/, &result);
1.241 paf 1029: return result;
1030: }
1.188 parser 1031:
1.176 parser 1032: return 0;
1033: }