|
|
1.117 paf 1: /** @file
1.118 paf 2: Parser: executor part of request class.
3:
1.88 paf 4: Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
1.118 paf 5:
1.89 paf 6: Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
1.1 paf 7: */
1.169 ! parser 8: static const char *RCSId="$Id: double.C,v 1.31 2001/06/28 07:41:59 parser Exp $";
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.1 paf 24:
1.157 parser 25: //#define DEBUG_EXECUTE
26:
1.159 parser 27: const uint ANTI_ENDLESS_EXECUTE_RECOURSION=500;
1.24 paf 28:
1.157 parser 29: #ifdef DEBUG_EXECUTE
1.1 paf 30: char *opcode_name[]={
1.49 paf 31: // literals
1.109 paf 32: "VALUE", "CURLY_CODE__STORE_PARAM", "EXPR_CODE__STORE_PARAM",
1.49 paf 33:
34: // actions
1.51 paf 35: "WITH_SELF", "WITH_ROOT", "WITH_READ", "WITH_WRITE",
1.66 paf 36: "GET_CLASS",
1.80 paf 37: "CONSTRUCT_VALUE", "CONSTRUCT_DOUBLE",
1.108 paf 38: "WRITE_VALUE", "WRITE_EXPR_RESULT", "STRING__WRITE",
1.18 paf 39: "GET_ELEMENT", "GET_ELEMENT__WRITE",
1.1 paf 40: "CREATE_EWPOOL", "REDUCE_EWPOOL",
41: "CREATE_RWPOOL", "REDUCE_RWPOOL",
1.49 paf 42: "CREATE_SWPOOL", "REDUCE_SWPOOL",
1.1 paf 43: "GET_METHOD_FRAME",
44: "STORE_PARAM",
1.51 paf 45: "CALL",
1.49 paf 46:
47: // expression ops: unary
1.148 paf 48: "NEG", "INV", "NOT", "DEF", "IN", "FEXISTS", "DEXISTS",
1.49 paf 49: // expression ops: binary
50: "SUB", "ADD", "MUL", "DIV", "MOD",
1.56 paf 51: "BIN_AND", "BIN_OR", "BIN_XOR",
52: "LOG_AND", "LOG_OR", "LOG_XOR",
1.49 paf 53: "NUM_LT", "NUM_GT", "NUM_LE", "NUM_GE", "NUM_EQ", "NUM_NE",
1.103 paf 54: "STR_LT", "STR_GT", "STR_LE", "STR_GE", "STR_EQ", "STR_NE",
55: "IS"
1.1 paf 56: };
57:
1.158 parser 58: void va_debug_printf(Pool& pool, const char *fmt,va_list args) {
1.127 paf 59: char buf[MAX_STRING];
60: vsnprintf(buf, MAX_STRING, fmt, args);
61: SAPI::log(pool, "%s", buf);
1.107 paf 62: }
63:
1.158 parser 64: void debug_printf(Pool& pool, const char *fmt, ...) {
1.107 paf 65: va_list args;
66: va_start(args,fmt);
1.158 parser 67: va_debug_printf(pool,fmt,args);
1.107 paf 68: va_end(args);
1.105 paf 69: }
70:
1.158 parser 71: void debug_dump(Pool& pool, int level, const Array& ops) {
1.157 parser 72: {
1.135 paf 73: int size=ops.quick_size();
1.158 parser 74: //debug_printf(pool, "size=%d\n", size);
1.23 paf 75: for(int i=0; i<size; i++) {
76: Operation op;
77: op.cast=ops.quick_get(i);
1.158 parser 78: debug_printf(pool, "%8X\n", op.cast);
1.23 paf 79: }
80: }
81:
1.135 paf 82: int size=ops.quick_size();
1.158 parser 83: //debug_printf(pool, "size=%d\n", size);
1.1 paf 84: for(int i=0; i<size; i++) {
1.23 paf 85: Operation op;
86: op.cast=ops.quick_get(i);
1.1 paf 87:
1.86 paf 88: if(op.code==OP_VALUE || op.code==OP_STRING__WRITE) {
1.52 paf 89: Value *value=static_cast<Value *>(ops.quick_get(++i));
1.158 parser 90: debug_printf(pool,
1.133 paf 91: "%*s%s"
92: " \"%s\" %s",
93: level*4, "", opcode_name[op.code],
94: value->get_string()->cstr(), value->type());
95: continue;
1.15 paf 96: }
1.158 parser 97: debug_printf(pool, "%*s%s", level*4, "", opcode_name[op.code]);
1.1 paf 98:
1.109 paf 99: if(op.code==OP_CURLY_CODE__STORE_PARAM || op.code==OP_EXPR_CODE__STORE_PARAM) {
1.10 paf 100: const Array *local_ops=reinterpret_cast<const Array *>(ops.quick_get(++i));
1.158 parser 101: debug_dump(pool, level+1, *local_ops);
1.1 paf 102: }
103: }
104: }
1.157 parser 105: #endif
1.1 paf 106:
1.159 parser 107: #define PUSH(value) stack.push(value)
108: #define POP() static_cast<Value *>(stack.pop())
109: #define POP_NAME() static_cast<Value *>(stack.pop())->as_string()
110:
1.32 paf 111: void Request::execute(const Array& ops) {
1.157 parser 112: #ifdef DEBUG_EXECUTE
1.158 parser 113: debug_printf(pool(), "source----------------------------\n");
114: debug_dump(pool(), 0, ops);
115: debug_printf(pool(), "execution-------------------------\n");
1.157 parser 116: #endif
1.12 paf 117:
1.135 paf 118: int size=ops.quick_size();
1.158 parser 119: //debug_printf(pool(), "size=%d\n", size);
1.11 paf 120: for(int i=0; i<size; i++) {
1.23 paf 121: Operation op;
122: op.cast=ops.quick_get(i);
1.157 parser 123: #ifdef DEBUG_EXECUTE
1.158 parser 124: debug_printf(pool(), "%d:%s", stack.top_index()+1, opcode_name[op.code]);
1.157 parser 125: #endif
1.11 paf 126:
1.23 paf 127: switch(op.code) {
1.51 paf 128: // param in next instruction
1.52 paf 129: case OP_VALUE:
1.32 paf 130: {
1.52 paf 131: Value *value=static_cast<Value *>(ops.quick_get(++i));
1.157 parser 132: #ifdef DEBUG_EXECUTE
1.158 parser 133: debug_printf(pool(), " \"%s\" %s", value->get_string()->cstr(), value->type());
1.157 parser 134: #endif
1.52 paf 135: PUSH(value);
1.32 paf 136: break;
137: }
1.109 paf 138: case OP_CURLY_CODE__STORE_PARAM:
139: case OP_EXPR_CODE__STORE_PARAM:
1.32 paf 140: {
1.73 paf 141: VMethodFrame *frame=static_cast<VMethodFrame *>(stack.top_value());
1.65 paf 142: // code
1.32 paf 143: const Array *local_ops=reinterpret_cast<const Array *>(ops.quick_get(++i));
1.157 parser 144: #ifdef DEBUG_EXECUTE
1.158 parser 145: debug_printf(pool(), " (%d)\n", local_ops->size());
146: debug_dump(pool(), 1, *local_ops);
1.157 parser 147: #endif
1.109 paf 148: // when they evaluate expression parameter,
149: // the object expression result
150: // does not need to be written into calling frame
151: // it must go into any expressions using that parameter
1.146 paf 152: // hence, we zero junction.wcontext here, and later
153: // in .process we would test that field
1.109 paf 154: // in decision "which wwrapper to use"
1.32 paf 155: Junction& j=*NEW Junction(pool(),
1.45 paf 156: *self, 0, 0,
1.130 paf 157: root,
1.149 paf 158: rcontext,
159: op.code==OP_EXPR_CODE__STORE_PARAM?0:wcontext,
1.130 paf 160: local_ops);
1.32 paf 161:
162: Value *value=NEW VJunction(j);
1.65 paf 163:
164: // store param
1.92 paf 165: frame->store_param(frame->name(), value);
1.32 paf 166: break;
167: }
1.66 paf 168: case OP_GET_CLASS:
1.38 paf 169: {
1.130 paf 170: // maybe they do ^class:method[] call, remember the fact
1.98 paf 171: wcontext->set_somebody_entered_some_class();
172:
1.82 paf 173: const String& name=POP_NAME();
1.120 paf 174: Value *value=static_cast<Value *>(classes().get(name));
175: if(!value)
1.66 paf 176: THROW(0,0,
177: &name,
1.143 paf 178: "class is undefined");
1.66 paf 179:
1.120 paf 180: PUSH(value);
1.38 paf 181: break;
182: }
1.32 paf 183:
1.51 paf 184: // OP_WITH
1.37 paf 185: case OP_WITH_SELF:
186: {
187: PUSH(self);
188: break;
189: }
190: case OP_WITH_ROOT:
1.11 paf 191: {
1.37 paf 192: PUSH(root);
1.13 paf 193: break;
1.11 paf 194: }
1.15 paf 195: case OP_WITH_READ:
196: {
1.24 paf 197: PUSH(rcontext);
1.20 paf 198: break;
199: }
1.37 paf 200: case OP_WITH_WRITE:
1.20 paf 201: {
1.37 paf 202: PUSH(wcontext);
1.20 paf 203: break;
204: }
1.37 paf 205:
1.51 paf 206: // OTHER ACTIONS BUT WITHs
1.80 paf 207: case OP_CONSTRUCT_VALUE:
1.20 paf 208: {
1.37 paf 209: Value *value=POP();
1.82 paf 210: const String& name=POP_NAME();
1.37 paf 211: Value *ncontext=POP();
1.81 paf 212: ncontext->put_element(name, value);
1.37 paf 213: value->set_name(name);
1.15 paf 214: break;
215: }
1.80 paf 216: case OP_CONSTRUCT_EXPR:
217: {
218: Value *value=POP();
1.82 paf 219: const String& name=POP_NAME();
1.80 paf 220: Value *ncontext=POP();
1.128 paf 221: ncontext->put_element(name, value->as_expr_result());
1.80 paf 222: value->set_name(name);
223: break;
224: }
1.108 paf 225: case OP_WRITE_VALUE:
1.13 paf 226: {
1.24 paf 227: Value *value=POP();
1.92 paf 228: write_assign_lang(*value);
1.150 paf 229:
230: // forget the fact they've entered some ^object.method[].
231: // see OP_GET_ELEMENT
232: wcontext->clear_somebody_entered_some_object();
1.86 paf 233: break;
234: }
1.108 paf 235: case OP_WRITE_EXPR_RESULT:
236: {
237: Value *value=POP();
1.128 paf 238: write_expr_result(*value->as_expr_result());
1.108 paf 239: break;
240: }
1.86 paf 241: case OP_STRING__WRITE:
242: {
243: VString *vstring=static_cast<VString *>(ops.quick_get(++i));
1.157 parser 244: #ifdef DEBUG_EXECUTE
1.158 parser 245: debug_printf(pool(), " \"%s\"", vstring->string().cstr());
1.157 parser 246: #endif
1.122 paf 247: write_no_lang(vstring->string());
1.13 paf 248: break;
1.14 paf 249: }
1.13 paf 250:
1.15 paf 251: case OP_GET_ELEMENT:
1.11 paf 252: {
1.150 paf 253: // maybe they do ^object.method[] call, remember the fact
254: wcontext->inc_somebody_entered_some_object();
255:
1.17 paf 256: Value *value=get_element();
1.24 paf 257: PUSH(value);
1.17 paf 258: break;
259: }
260:
1.18 paf 261: case OP_GET_ELEMENT__WRITE:
1.17 paf 262: {
263: Value *value=get_element();
1.92 paf 264: write_assign_lang(*value);
1.17 paf 265: break;
266: }
267:
1.32 paf 268:
1.17 paf 269: case OP_CREATE_EWPOOL:
270: {
1.24 paf 271: PUSH(wcontext);
1.137 paf 272: PUSH((void *)flang);
273: flang=String::UL_PASS_APPENDED;
1.138 paf 274: wcontext=NEW WWrapper(pool(), 0 /*empty*/, true /*constructing*/);
1.17 paf 275: break;
276: }
277: case OP_REDUCE_EWPOOL:
278: {
1.36 paf 279: Value *value=wcontext->result();
1.137 paf 280: flang=static_cast<String::Untaint_lang>(reinterpret_cast<int>(POP()));
1.25 paf 281: wcontext=static_cast<WContext *>(POP());
1.24 paf 282: PUSH(value);
1.13 paf 283: break;
1.15 paf 284: }
1.13 paf 285:
1.26 paf 286: case OP_CREATE_RWPOOL:
287: {
288: Value *ncontext=POP();
289: PUSH(rcontext);
1.146 paf 290: PUSH(wcontext);
1.26 paf 291: rcontext=ncontext;
1.138 paf 292: wcontext=NEW WWrapper(pool(), ncontext, false /*not constructing*/);
1.26 paf 293: break;
294: }
295: case OP_REDUCE_RWPOOL:
296: {
1.82 paf 297: const String *string=wcontext->get_string();
1.80 paf 298: Value *value;
299: if(string)
300: value=NEW VString(*string);
301: else
1.167 parser 302: value=NEW VVoid(pool());
1.26 paf 303: wcontext=static_cast<WContext *>(POP());
304: rcontext=POP();
305: PUSH(value);
306: break;
307: }
1.49 paf 308: case OP_CREATE_SWPOOL:
309: {
310: PUSH(wcontext);
1.138 paf 311: wcontext=NEW WWrapper(pool(), 0 /*empty*/, false /*not constructing*/);
1.49 paf 312: break;
313: }
314: case OP_REDUCE_SWPOOL:
315: {
316: // from "$a $b" part of expression taking only string value,
317: // ignoring any other content of wcontext
1.82 paf 318: const String *string=wcontext->get_string();
1.80 paf 319: Value *value;
320: if(string)
321: value=NEW VString(*string);
322: else
1.167 parser 323: NEW VVoid(pool());
1.50 paf 324: wcontext=static_cast<WContext *>(POP());
1.49 paf 325: PUSH(value);
326: break;
327: }
328:
1.51 paf 329: // CALL
1.28 paf 330: case OP_GET_METHOD_FRAME:
331: {
1.32 paf 332: Value *value=POP();
1.138 paf 333:
1.111 paf 334: // info:
335: // code compiled so that this one's always method-junction,
336: // not a code-junction
1.32 paf 337: Junction *junction=value->get_junction();
1.31 paf 338: if(!junction)
1.28 paf 339: THROW(0,0,
1.42 paf 340: &value->name(),
1.111 paf 341: "(%s) not a method or junction, can not call it",
1.38 paf 342: value->type());
1.70 paf 343:
1.138 paf 344: bool is_constructor=
345: wcontext->constructing() && // constructing?
346: wcontext->somebody_entered_some_class(); // ^class:method[..]?
347: if(is_constructor)
348: wcontext->constructing(false);
349:
1.156 parser 350: VMethodFrame *frame=NEW VMethodFrame(pool(), value->name(), *junction, is_constructor);
1.28 paf 351: PUSH(frame);
352: break;
353: }
354: case OP_STORE_PARAM:
355: {
356: Value *value=POP();
1.73 paf 357: VMethodFrame *frame=static_cast<VMethodFrame *>(stack.top_value());
1.92 paf 358: frame->store_param(frame->name(), value);
1.29 paf 359: break;
360: }
361:
362: case OP_CALL:
363: {
1.157 parser 364: #ifdef DEBUG_EXECUTE
1.158 parser 365: debug_printf(pool(), "->\n");
1.157 parser 366: #endif
1.34 paf 367: VMethodFrame *frame=static_cast<VMethodFrame *>(POP());
368: frame->fill_unspecified_params();
1.45 paf 369: PUSH(self);
370: PUSH(root);
371: PUSH(rcontext);
372: PUSH(wcontext);
373:
1.100 paf 374: VStateless_class *called_class=frame->junction.self.get_class();
1.150 paf 375: // not ^name.method call and
1.156 parser 376: // is context object or class & is it my class or my parent's class and?
1.100 paf 377: VStateless_class *read_class=rcontext->get_class();
1.156 parser 378: if(!wcontext->somebody_entered_some_object() &&
1.150 paf 379: read_class && read_class->is_or_derived_from(*called_class)) // yes
1.74 paf 380: self=rcontext; // class dynamic call
1.75 paf 381: else // no, not me or relative of mine (total stranger)
1.153 paf 382: // were are constructing something and
1.156 parser 383: // our constructor is just method call and
384: // not static-only-method call
1.153 paf 385: if(frame->is_constructor &&
1.156 parser 386: wcontext->somebody_entered_some_object()==1 &&
387: frame->junction.method->call_type!=Method::CT_STATIC) {
1.138 paf 388: // this is a constructor call
1.149 paf 389: // some stateless_object creatable derivates
390: if(Value *value=called_class->create_new_value(pool()))
391: self=value;
1.121 paf 392: else // stateful object
1.94 paf 393: self=NEW VObject(pool(), *called_class);
1.83 paf 394: frame->write(*self,
1.136 paf 395: String::UL_CLEAN // not used, always an object, not string
1.83 paf 396: );
1.75 paf 397: } else
398: self=&frame->junction.self; // no, static or simple dynamic call
399:
1.45 paf 400: frame->set_self(*self);
1.29 paf 401: root=rcontext=wcontext=frame;
1.47 paf 402: {
1.48 paf 403: // take object or class from any wrappers
1.102 paf 404: // and substitute class alias to the class they are called AS
405: Temp_alias temp_alias(*self->get_aliased(), *frame->junction.vclass);
1.68 paf 406:
1.99 paf 407: const Method& method=*frame->junction.method;
1.134 paf 408: Method::Call_type call_type=
409: called_class==self ? Method::CT_STATIC : Method::CT_DYNAMIC;
410: if(
411: method.call_type==Method::CT_ANY ||
412: method.call_type==call_type) // allowed call type?
413: if(method.native_code) { // native code?
1.154 paf 414: method.check_actual_numbered_params(pool(),
1.134 paf 415: frame->junction.self,
416: frame->name(), frame->numbered_params());
1.160 parser 417: method.native_code(
1.134 paf 418: *this,
419: frame->name(), frame->numbered_params()); // execute it
1.159 parser 420: } else { // parser code
421: if(++anti_endless_execute_recoursion==ANTI_ENDLESS_EXECUTE_RECOURSION) {
422: anti_endless_execute_recoursion=0; // give @exception a chance
423: THROW(0, 0,
1.160 parser 424: &frame->name(),
1.159 parser 425: "endless recursion detected");
426: }
427:
1.134 paf 428: execute(*method.parser_code); // execute it
1.159 parser 429: anti_endless_execute_recoursion--;
430: }
1.134 paf 431: else
432: THROW(0, 0,
433: &frame->name(),
434: "is not allowed to be called %s",
435: call_type==Method::CT_STATIC?"statically":"dynamically");
436:
1.47 paf 437: }
1.36 paf 438: Value *value=wcontext->result();
1.45 paf 439:
440: wcontext=static_cast<WContext *>(POP());
441: rcontext=POP();
442: root=POP();
443: self=static_cast<VAliased *>(POP());
1.62 paf 444:
1.61 paf 445: PUSH(value);
1.157 parser 446: #ifdef DEBUG_EXECUTE
1.158 parser 447: debug_printf(pool(), "<-returned");
1.157 parser 448: #endif
1.49 paf 449: break;
450: }
451:
1.55 paf 452: // expression ops: unary
453: case OP_NEG:
454: {
455: Value *operand=POP();
1.128 paf 456: Value *value=NEW VDouble(pool(), -operand->as_double());
1.55 paf 457: PUSH(value);
458: break;
459: }
460: case OP_INV:
461: {
462: Value *operand=POP();
1.155 parser 463: Value *value=NEW VDouble(pool(), ~operand->as_int());
1.55 paf 464: PUSH(value);
465: break;
466: }
467: case OP_NOT:
468: {
469: Value *operand=POP();
1.128 paf 470: Value *value=NEW VBool(pool(), !operand->as_bool());
1.55 paf 471: PUSH(value);
472: break;
473: }
1.62 paf 474: case OP_DEF:
475: {
476: Value *operand=POP();
1.128 paf 477: Value *value=NEW VBool(pool(), operand->is_defined());
1.62 paf 478: PUSH(value);
479: break;
480: }
481: case OP_IN:
482: {
483: Value *operand=POP();
1.110 paf 484: const char *path=operand->as_string().cstr();
1.151 paf 485: Value *value=NEW VBool(pool(),
486: info.uri && strncmp(path, info.uri, strlen(path))==0);
1.62 paf 487: PUSH(value);
488: break;
489: }
490: case OP_FEXISTS:
491: {
492: Value *operand=POP();
1.127 paf 493: Value *value=NEW VBool(pool(),
494: file_readable(absolute(operand->as_string())));
1.148 paf 495: PUSH(value);
496: break;
497: }
498: case OP_DEXISTS:
499: {
500: Value *operand=POP();
501: Value *value=NEW VBool(pool(),
502: dir_readable(absolute(operand->as_string())));
1.62 paf 503: PUSH(value);
504: break;
505: }
1.55 paf 506:
507: // expression ops: binary
508: case OP_SUB:
1.53 paf 509: {
1.61 paf 510: Value *b=POP(); Value *a=POP();
1.128 paf 511: Value *value=NEW VDouble(pool(), a->as_double() - b->as_double());
1.53 paf 512: PUSH(value);
513: break;
514: }
1.55 paf 515: case OP_ADD:
1.53 paf 516: {
1.61 paf 517: Value *b=POP(); Value *a=POP();
1.128 paf 518: Value *value=NEW VDouble(pool(), a->as_double() + b->as_double());
1.53 paf 519: PUSH(value);
520: break;
521: }
1.49 paf 522: case OP_MUL:
523: {
1.61 paf 524: Value *b=POP(); Value *a=POP();
1.128 paf 525: Value *value=NEW VDouble(pool(), a->as_double() * b->as_double());
1.53 paf 526: PUSH(value);
527: break;
528: }
529: case OP_DIV:
530: {
1.61 paf 531: Value *b=POP(); Value *a=POP();
1.128 paf 532: Value *value=NEW VDouble(pool(), a->as_double() / b->as_double());
1.54 paf 533: PUSH(value);
534: break;
535: }
1.55 paf 536: case OP_MOD:
537: {
1.61 paf 538: Value *b=POP(); Value *a=POP();
1.78 paf 539: Value *value=NEW VDouble(pool(),
1.155 parser 540: a->as_int() %
541: b->as_int());
1.55 paf 542: PUSH(value);
543: break;
544: }
545: case OP_BIN_AND:
1.54 paf 546: {
1.61 paf 547: Value *b=POP(); Value *a=POP();
1.78 paf 548: Value *value=NEW VDouble(pool(),
1.155 parser 549: a->as_int() &
550: b->as_int());
1.54 paf 551: PUSH(value);
552: break;
553: }
1.55 paf 554: case OP_BIN_OR:
1.54 paf 555: {
1.61 paf 556: Value *b=POP(); Value *a=POP();
1.78 paf 557: Value *value=NEW VDouble(pool(),
1.155 parser 558: a->as_int() |
559: b->as_int());
1.55 paf 560: PUSH(value);
561: break;
562: }
1.56 paf 563: case OP_BIN_XOR:
564: {
1.61 paf 565: Value *b=POP(); Value *a=POP();
1.78 paf 566: Value *value=NEW VDouble(pool(),
1.155 parser 567: a->as_int() ^
568: b->as_int());
1.56 paf 569: PUSH(value);
570: break;
571: }
1.55 paf 572: case OP_LOG_AND:
573: {
1.61 paf 574: Value *b=POP(); Value *a=POP();
1.128 paf 575: Value *value=NEW VBool(pool(), a->as_bool() && b->as_bool());
1.55 paf 576: PUSH(value);
577: break;
578: }
579: case OP_LOG_OR:
580: {
1.61 paf 581: Value *b=POP(); Value *a=POP();
1.128 paf 582: Value *value=NEW VBool(pool(), a->as_bool() || b->as_bool());
1.56 paf 583: PUSH(value);
584: break;
585: }
586: case OP_LOG_XOR:
587: {
1.61 paf 588: Value *b=POP(); Value *a=POP();
1.128 paf 589: Value *value=NEW VBool(pool(), a->as_bool() ^ b->as_bool());
1.55 paf 590: PUSH(value);
591: break;
592: }
593: case OP_NUM_LT:
594: {
1.61 paf 595: Value *b=POP(); Value *a=POP();
1.128 paf 596: Value *value=NEW VBool(pool(), a->as_double() < b->as_double());
1.55 paf 597: PUSH(value);
598: break;
599: }
600: case OP_NUM_GT:
601: {
1.61 paf 602: Value *b=POP(); Value *a=POP();
1.128 paf 603: Value *value=NEW VBool(pool(), a->as_double() > b->as_double());
1.55 paf 604: PUSH(value);
605: break;
606: }
607: case OP_NUM_LE:
608: {
1.61 paf 609: Value *b=POP(); Value *a=POP();
1.128 paf 610: Value *value=NEW VBool(pool(), a->as_double() <= b->as_double());
1.55 paf 611: PUSH(value);
612: break;
613: }
614: case OP_NUM_GE:
615: {
1.61 paf 616: Value *b=POP(); Value *a=POP();
1.128 paf 617: Value *value=NEW VBool(pool(), a->as_double() >= b->as_double());
1.55 paf 618: PUSH(value);
619: break;
620: }
621: case OP_NUM_EQ:
622: {
1.61 paf 623: Value *b=POP(); Value *a=POP();
1.128 paf 624: Value *value=NEW VBool(pool(), a->as_double() == b->as_double());
1.55 paf 625: PUSH(value);
626: break;
627: }
628: case OP_NUM_NE:
629: {
1.61 paf 630: Value *b=POP(); Value *a=POP();
1.128 paf 631: Value *value=NEW VBool(pool(), a->as_double() != b->as_double());
1.54 paf 632: PUSH(value);
633: break;
634: }
1.58 paf 635: case OP_STR_LT:
636: {
1.61 paf 637: Value *b=POP(); Value *a=POP();
1.78 paf 638: Value *value=NEW VBool(pool(), a->as_string() < b->as_string());
1.58 paf 639: PUSH(value);
640: break;
641: }
642: case OP_STR_GT:
643: {
1.61 paf 644: Value *b=POP(); Value *a=POP();
1.78 paf 645: Value *value=NEW VBool(pool(), a->as_string() > b->as_string());
1.58 paf 646: PUSH(value);
647: break;
648: }
1.55 paf 649: case OP_STR_LE:
1.58 paf 650: {
1.61 paf 651: Value *b=POP(); Value *a=POP();
1.78 paf 652: Value *value=NEW VBool(pool(), a->as_string() <= b->as_string());
1.58 paf 653: PUSH(value);
654: break;
655: }
1.55 paf 656: case OP_STR_GE:
1.54 paf 657: {
1.61 paf 658: Value *b=POP(); Value *a=POP();
1.78 paf 659: Value *value=NEW VBool(pool(), a->as_string() >= b->as_string());
1.58 paf 660: PUSH(value);
661: break;
662: }
663: case OP_STR_EQ:
664: {
1.61 paf 665: Value *b=POP(); Value *a=POP();
1.78 paf 666: Value *value=NEW VBool(pool(), a->as_string() == b->as_string());
1.58 paf 667: PUSH(value);
668: break;
669: }
670: case OP_STR_NE:
671: {
1.61 paf 672: Value *b=POP(); Value *a=POP();
1.78 paf 673: Value *value=NEW VBool(pool(), a->as_string() != b->as_string());
1.103 paf 674: PUSH(value);
675: break;
676: }
677: case OP_IS:
678: {
679: Value *b=POP(); Value *a=POP();
680: Value *value=NEW VBool(pool(), b->as_string() == a->type());
1.49 paf 681: PUSH(value);
1.28 paf 682: break;
683: }
684:
1.11 paf 685: default:
1.67 paf 686: THROW(0,0,
687: 0,
1.139 paf 688: "invalid opcode %d", op.code);
1.11 paf 689: }
690: }
1.1 paf 691: }
1.17 paf 692:
693: Value *Request::get_element() {
1.82 paf 694: const String& name=POP_NAME();
1.24 paf 695: Value *ncontext=POP();
1.166 parser 696: Value *value;
1.147 paf 697: if(Method* method=OP.get_method(name)) { // operator?
698: // as if that method were in self and we have normal dynamic method here
699: Junction& junction=*NEW Junction(pool(),
700: *self, self->get_class(), method, 0,0,0,0);
701: value=NEW VJunction(junction);
702: } else
1.166 parser 703: value=ncontext->get_element(name);
1.76 paf 704: if(value)
1.92 paf 705: value=&process(*value, &name); // process possible code-junction
1.76 paf 706: else {
1.167 parser 707: value=NEW VVoid(pool());
1.76 paf 708: value->set_name(name);
709: }
1.63 paf 710:
1.17 paf 711: return value;
1.34 paf 712: }
1.70 paf 713:
1.162 parser 714: /** @param intercept_string
1.116 paf 715: - true:
716: they want result=string value,
717: possible object result goes to wcontext
718: - false:
719: they want any result[string|object]
720: nothing goes to wcontext.
1.125 paf 721: used in @c (expression) params evaluation
1.116 paf 722: */
1.92 paf 723: Value& Request::process(Value& value, const String *name, bool intercept_string) {
1.91 paf 724: Value *result;
1.70 paf 725: Junction *junction=value.get_junction();
726: if(junction && junction->code) { // is it a code-junction?
1.92 paf 727: // process it
1.157 parser 728: #ifdef DEBUG_EXECUTE
1.158 parser 729: debug_printf(pool(), "ja->\n");
1.157 parser 730: #endif
1.70 paf 731: PUSH(self);
732: PUSH(root);
733: PUSH(rcontext);
1.129 paf 734: PUSH(wcontext);
1.70 paf 735:
1.71 paf 736: WContext *frame;
1.109 paf 737: // for expression method params
738: // wcontext is set 0
739: // using the fact in decision "which wwrapper to use"
740: bool using_code_frame=intercept_string && junction->wcontext;
741: if(using_code_frame) {
1.71 paf 742: // almost plain wwrapper about junction wcontext,
743: // BUT intercepts string writes
744: frame=NEW VCodeFrame(pool(), *junction->wcontext);
745: } else {
746: // plain wwrapper
1.108 paf 747: frame=NEW WWrapper(pool(), 0/*empty*/, false/*not constructing*/);
1.71 paf 748: }
749:
750: wcontext=frame;
1.70 paf 751: self=&junction->self;
752: root=junction->root;
753: rcontext=junction->rcontext;
754: execute(*junction->code);
1.109 paf 755: if(using_code_frame) {
1.71 paf 756: // CodeFrame soul:
757: // string writes were intercepted
758: // returning them as the result of getting code-junction
759: result=NEW VString(*frame->get_string());
760: } else
761: result=frame->result();
1.70 paf 762:
763: wcontext=static_cast<WContext *>(POP());
764: rcontext=POP();
765: root=POP();
766: self=static_cast<VAliased *>(POP());
767:
1.157 parser 768: #ifdef DEBUG_EXECUTE
1.158 parser 769: debug_printf(pool(), "<-ja returned");
1.157 parser 770: #endif
1.70 paf 771: } else
1.91 paf 772: result=&value;
773:
774: if(name)
775: result->set_name(*name);
776: return *result;
1.85 paf 777: }
778:
1.114 paf 779: const String *Request::execute_method(Value& aself, const Method& method, bool return_cstr) {
1.99 paf 780: PUSH(self);
781: PUSH(root);
782: PUSH(rcontext);
783: PUSH(wcontext);
784:
785: // initialize contexts
786: root=rcontext=self=&aself;
1.138 paf 787: wcontext=NEW WWrapper(pool(), &aself, false /*not constructing*/);
1.99 paf 788:
789: // execute!
790: execute(*method.parser_code);
791:
792: // result
1.114 paf 793: const String *result;
1.99 paf 794: if(return_cstr)
1.114 paf 795: result=&wcontext->as_string();
1.99 paf 796: else
797: result=0; // ignore result
798:
799: wcontext=static_cast<WContext *>(POP());
800: rcontext=POP();
801: root=POP();
802: self=static_cast<VAliased *>(POP());
803:
804: // return
805: return result;
806: }
807:
1.114 paf 808: const String *Request::execute_method(Value& aself,
1.99 paf 809: const String& method_name, bool return_cstr) {
810: if(Value *value=aself.get_element(method_name))
811: if(Junction *junction=value->get_junction())
812: if(const Method *method=junction->method)
813: return execute_method(aself, *method, return_cstr);
814:
1.85 paf 815: return 0;
1.70 paf 816: }