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