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