|
|
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.257 ! paf 8: static const char* IDENT_EXECUTE_C="$Date: 2002/08/21 10:18:35 $";
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();
661: bool result;
662: if(a->as_bool()) {
663: execute(*b_code);
664: b=POP();
665: result=b->as_bool();
666: } else
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();
675: bool result;
676: if(a->as_bool())
677: result=true;
678: else {
679: execute(*b_code);
680: b=POP();
681: result=b->as_bool();
682: }
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();
697: value=NEW VBool(pool(), a->as_double() < b->as_double());
1.55 paf 698: PUSH(value);
699: break;
700: }
701: case OP_NUM_GT:
702: {
1.197 parser 703: b=POP(); a=POP();
704: value=NEW VBool(pool(), a->as_double() > b->as_double());
1.55 paf 705: PUSH(value);
706: break;
707: }
708: case OP_NUM_LE:
709: {
1.197 parser 710: b=POP(); a=POP();
711: value=NEW VBool(pool(), a->as_double() <= b->as_double());
1.55 paf 712: PUSH(value);
713: break;
714: }
715: case OP_NUM_GE:
716: {
1.197 parser 717: b=POP(); a=POP();
718: value=NEW VBool(pool(), a->as_double() >= b->as_double());
1.55 paf 719: PUSH(value);
720: break;
721: }
722: case OP_NUM_EQ:
723: {
1.197 parser 724: b=POP(); a=POP();
725: value=NEW VBool(pool(), a->as_double() == b->as_double());
1.55 paf 726: PUSH(value);
727: break;
728: }
729: case OP_NUM_NE:
730: {
1.197 parser 731: b=POP(); a=POP();
732: value=NEW VBool(pool(), a->as_double() != b->as_double());
1.54 paf 733: PUSH(value);
734: break;
735: }
1.58 paf 736: case OP_STR_LT:
737: {
1.197 parser 738: b=POP(); a=POP();
739: value=NEW VBool(pool(), a->as_string() < b->as_string());
1.58 paf 740: PUSH(value);
741: break;
742: }
743: case OP_STR_GT:
744: {
1.197 parser 745: b=POP(); a=POP();
746: value=NEW VBool(pool(), a->as_string() > b->as_string());
1.58 paf 747: PUSH(value);
748: break;
749: }
1.55 paf 750: case OP_STR_LE:
1.58 paf 751: {
1.197 parser 752: b=POP(); a=POP();
753: value=NEW VBool(pool(), a->as_string() <= b->as_string());
1.58 paf 754: PUSH(value);
755: break;
756: }
1.55 paf 757: case OP_STR_GE:
1.54 paf 758: {
1.197 parser 759: b=POP(); a=POP();
760: value=NEW VBool(pool(), a->as_string() >= b->as_string());
1.58 paf 761: PUSH(value);
762: break;
763: }
764: case OP_STR_EQ:
765: {
1.197 parser 766: b=POP(); a=POP();
767: value=NEW VBool(pool(), a->as_string() == b->as_string());
1.58 paf 768: PUSH(value);
769: break;
770: }
771: case OP_STR_NE:
772: {
1.197 parser 773: b=POP(); a=POP();
774: value=NEW VBool(pool(), a->as_string() != b->as_string());
1.103 paf 775: PUSH(value);
776: break;
777: }
778: case OP_IS:
779: {
1.197 parser 780: b=POP(); a=POP();
1.255 paf 781: value=NEW VBool(pool(), a->is(b->as_string().cstr()));
1.49 paf 782: PUSH(value);
1.28 paf 783: break;
784: }
785:
1.11 paf 786: default:
1.223 paf 787: throw Exception(0,
1.67 paf 788: 0,
1.139 paf 789: "invalid opcode %d", op.code);
1.11 paf 790: }
791: }
1.1 paf 792: }
1.17 paf 793:
1.231 paf 794: /// @test cache|prepare junctions
1.236 paf 795: Value *Request::get_element(const String *& remember_name, bool can_call_operator) {
796: const String& name=POP_NAME(); remember_name=&name;
1.24 paf 797: Value *ncontext=POP();
1.216 paf 798: Value *value=0;
1.249 paf 799: if(can_call_operator) {
1.216 paf 800: if(Method* method=OP.get_method(name)) { // looking operator of that name FIRST
1.181 parser 801: // as if that method were in self and we have normal dynamic method here
802: Junction& junction=*NEW Junction(pool(),
1.257 ! paf 803: *method_frame, self->get_class(), method, 0,0,0,0);
1.181 parser 804: value=NEW VJunction(junction);
805: }
1.249 paf 806: }
807: if(!value) {
808: if(!wcontext->get_constructing() // not constructing
809: && wcontext->get_somebody_entered_some_class() ) // ^class:method
810: if(VStateless_class *called_class=ncontext->get_class())
811: if(VStateless_class *read_class=rcontext->get_class())
812: if(read_class->derived_from(*called_class)) // current derived from called
813: if(Value *base_object=self->base_object()) { // doing DYNAMIC call
1.256 paf 814: Temp_derived temp_derived(*base_object, 0); // temporarily prevent go-back-down virtual calls
1.252 paf 815: value=base_object->get_element(name, base_object, false); // virtual-up lookup starting from parent
1.249 paf 816: goto _void;
817: }
818: }
1.216 paf 819: if(!value)
1.252 paf 820: value=ncontext->get_element(name, ncontext, false);
1.249 paf 821:
822: _void:
1.236 paf 823: if(value)
1.229 paf 824: value=&process_to_value(*value); // process possible code-junction
1.236 paf 825: else
1.167 parser 826: value=NEW VVoid(pool());
1.63 paf 827:
1.17 paf 828: return value;
1.34 paf 829: }
1.70 paf 830:
1.162 parser 831: /** @param intercept_string
1.116 paf 832: - true:
833: they want result=string value,
834: possible object result goes to wcontext
835: - false:
836: they want any result[string|object]
837: nothing goes to wcontext.
1.125 paf 838: used in @c (expression) params evaluation
1.224 paf 839:
840: using the fact it's either string_ or value_ result requested to speed up checkes
1.116 paf 841: */
1.229 paf 842: StringOrValue Request::process(Value& input_value, bool intercept_string) {
1.228 paf 843: StringOrValue result;
1.224 paf 844: Junction *junction=input_value.get_junction();
1.70 paf 845: if(junction && junction->code) { // is it a code-junction?
1.92 paf 846: // process it
1.157 parser 847: #ifdef DEBUG_EXECUTE
1.158 parser 848: debug_printf(pool(), "ja->\n");
1.157 parser 849: #endif
1.238 paf 850:
1.257 ! paf 851: if(!junction->method_frame)
1.240 paf 852: throw Exception("parser.runtime",
853: 0,
854: "junction used outside of context");
855:
1.257 ! paf 856: Value *saved_self=self;
! 857: VMethodFrame *saved_method_frame=method_frame;
! 858: Value *saved_rcontext=rcontext;
! 859: WContext *saved_wcontext=wcontext;
1.240 paf 860:
1.225 paf 861: self=&junction->self;
1.257 ! paf 862: method_frame=junction->method_frame;
1.240 paf 863: rcontext=junction->rcontext;
1.225 paf 864:
1.109 paf 865: // for expression method params
866: // wcontext is set 0
867: // using the fact in decision "which wwrapper to use"
1.240 paf 868: bool using_code_frame=intercept_string && junction->wcontext;
1.109 paf 869: if(using_code_frame) {
1.71 paf 870: // almost plain wwrapper about junction wcontext,
871: // BUT intercepts string writes
1.240 paf 872: VCodeFrame local(pool(), *junction->wcontext);
1.225 paf 873: wcontext=&local;
1.207 paf 874:
1.225 paf 875: // execute it
1.228 paf 876: recoursion_checked_execute(0/*result_name*/, *junction->code);
1.226 paf 877:
1.71 paf 878: // CodeFrame soul:
879: // string writes were intercepted
880: // returning them as the result of getting code-junction
1.229 paf 881: result.set_string(*wcontext->get_string());
1.224 paf 882: } else {
1.225 paf 883: // plain wwrapper
884: WWrapper local(pool(), 0/*empty*/);
885: wcontext=&local;
886:
887: // execute it
1.228 paf 888: recoursion_checked_execute(0/*result_name*/, *junction->code);
1.226 paf 889:
1.228 paf 890: result=wcontext->result();
1.224 paf 891: }
1.70 paf 892:
1.257 ! paf 893: wcontext=saved_wcontext;
! 894: rcontext=saved_rcontext;
! 895: method_frame=saved_method_frame;
! 896: self=saved_self;
1.70 paf 897:
1.157 parser 898: #ifdef DEBUG_EXECUTE
1.158 parser 899: debug_printf(pool(), "<-ja returned");
1.157 parser 900: #endif
1.224 paf 901: } else {
1.228 paf 902: result.set_value(input_value);
1.224 paf 903: }
1.228 paf 904: return result;
1.85 paf 905: }
906:
1.257 ! paf 907: const String& Request::execute_method(VMethodFrame& amethod_frame, const Method& method) {
! 908: Value *saved_self=self;
! 909: VMethodFrame *saved_method_frame=method_frame;
! 910: Value *saved_rcontext=rcontext;
! 911: WContext *saved_wcontext=wcontext;
1.99 paf 912:
913: // initialize contexts
1.257 ! paf 914: self=rcontext=wcontext=method_frame=&amethod_frame;
1.99 paf 915:
916: // execute!
917: execute(*method.parser_code);
918:
919: // result
1.242 paf 920: const String& result=wcontext->result().as_string();
1.208 paf 921:
1.257 ! paf 922: wcontext=saved_wcontext;
! 923: rcontext=saved_rcontext;
! 924: method_frame=saved_method_frame;
! 925: self=saved_self;
1.242 paf 926:
927: // return
928: return result;
1.208 paf 929: }
930:
1.242 paf 931: void Request::execute_method(Value& aself,
932: const Method& method, VString *optional_param,
933: const String **return_string) {
1.257 ! paf 934: Value *saved_self=self;
! 935: VMethodFrame *saved_method_frame=method_frame;
! 936: Value *saved_rcontext=rcontext;
! 937: WContext *saved_wcontext=wcontext;
1.208 paf 938:
939: // initialize contexts
1.257 ! paf 940: //method_frame=rcontext=self=&aself;
1.242 paf 941: self=&aself;
942: // WWrapper local(pool(), &aself);
943: // wcontext=&local;
944: Junction local_junction(pool(), *self, self->get_class(), &method, 0,0,0,0);
945: VMethodFrame local_frame(pool(), method.name, local_junction);
946: if(optional_param && local_frame.can_store_param()) {
947: local_frame.store_param(optional_param);
948: local_frame.fill_unspecified_params();
949: }
950: local_frame.set_self(*self);
1.257 ! paf 951: rcontext=wcontext=method_frame=&local_frame;
1.246 paf 952:
953: // prevent non-string writes for better error reporting
954: if(return_string)
955: wcontext->write(local_frame);
1.208 paf 956:
957: // execute!
958: execute(*method.parser_code);
959:
960: // result
1.242 paf 961: const String *result=0;
1.245 paf 962: if(return_string)
1.242 paf 963: *return_string=&wcontext->result().as_string();
1.99 paf 964:
1.257 ! paf 965: wcontext=saved_wcontext;
! 966: rcontext=saved_rcontext;
! 967: method_frame=saved_method_frame;
! 968: self=saved_self;
1.242 paf 969: }
970:
971: void Request::execute_nonvirtual_method(VStateless_class& aclass,
972: const String& method_name, VString *optional_param,
973: const String **return_string,
974: const Method **return_method) {
975:
976: const Method *method=aclass.get_method(method_name);
977: if(return_string)
978: *return_string=0;
979: if(return_method)
980: *return_method=method;
981:
982: if(method)
983: execute_method(aclass, *method, optional_param, return_string);
1.99 paf 984: }
985:
1.176 parser 986: const String *Request::execute_virtual_method(Value& aself,
1.208 paf 987: const String& method_name) {
1.252 paf 988: if(Value *value=aself.get_element(method_name, &aself, false))
1.99 paf 989: if(Junction *junction=value->get_junction())
1.241 paf 990: if(const Method *method=junction->method) {
991: const String *result;
1.242 paf 992: execute_method(aself, *method, 0/*no params*/, &result);
1.241 paf 993: return result;
994: }
1.188 parser 995:
1.176 parser 996: return 0;
997: }