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