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