Annotation of parser3/src/main/execute.C, revision 1.295.2.8
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.8! paf 8: static const char* IDENT_EXECUTE_C="$Date: 2003/02/03 09:17:47 $";
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.295.2.7 paf 103: local_ops=i.next().ops;
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.295.2.7 paf 122: debug_printf("source----------------------------\n");
123: debug_dump(0, ops);
124: debug_printf("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.295.2.7 paf 137: debug_printf("%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.295.2.7 paf 155: debug_printf(" \"%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.7 paf 227: debug_printf(" (%d)\n", local_ops->count());
228: debug_dump(1, *local_ops);
1.182 parser 229: #endif
1.295.2.6 paf 230: value=ValuePtr(new VJunction(JunctionPtr(new Junction(
1.295.2.7 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.7 paf 246: debug_printf(" (%d)\n", local_ops->count());
247: debug_dump(1, *local_ops);
1.209 paf 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.295.2.7 paf 261: write_no_lang(value->as_expr_result());
1.230 paf 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.295.2.7 paf 270: value=i.next().value;
1.157 parser 271: #ifdef DEBUG_EXECUTE
1.295.2.7 paf 272: debug_printf(" \"%s\"", value->get_string(0).cstr());
1.157 parser 273: #endif
1.295.2.7 paf 274: write_no_lang(*value->get_string(0));
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);
1.295.2.7 paf 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.295.2.7 paf 301: local_ops=i.next().ops;
1.226 paf 302:
1.257 paf 303: WContext *saved_wcontext=wcontext;
304: uchar saved_lang= flang;
1.137 paf 305: flang=String::UL_PASS_APPENDED;
1.295.2.7 paf 306: WWrapper local(ValuePtr(0)/*empty*/, wcontext);
1.226 paf 307: wcontext=&local;
308:
309: execute(*local_ops);
310:
1.295.2.7 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.295.2.7 paf 320: local_ops=i.next().ops;
1.226 paf 321:
1.257 paf 322: WContext *saved_wcontext=wcontext;
1.295.2.7 paf 323: WWrapper local(ValuePtr(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.295.2.7 paf 330: if(StringPtr string=wcontext->get_string(&pool()))
331: value=ValuePtr(new VString(string));
1.80 paf 332: else
1.295.2.7 paf 333: value=ValuePtr(new VVoid);
334:
1.257 paf 335: wcontext=saved_wcontext;
1.295.2.6 paf 336: stack.push(value);
1.49 paf 337: break;
338: }
339:
1.51 paf 340: // CALL
1.237 paf 341: case OP_STORE_PARAM:
1.28 paf 342: {
1.295.2.6 paf 343: value=stack.pop().value;
1.295.2.7 paf 344: VMethodFrame *frame=static_cast<VMethodFrame *>(stack.top_value().unknown);
345: // this op is executed from CALL local_ops only, so may skip the check "method_frame_to_fill==0"
1.237 paf 346: frame->store_param(value);
1.28 paf 347: break;
348: }
1.237 paf 349: case OP_CURLY_CODE__STORE_PARAM:
350: case OP_EXPR_CODE__STORE_PARAM:
1.28 paf 351: {
1.237 paf 352: // code
1.295.2.7 paf 353: local_ops=i.next().ops;
354: VMethodFrame *frame=static_cast<VMethodFrame *>(stack.top_value().unknown);
1.237 paf 355: #ifdef DEBUG_EXECUTE
1.295.2.7 paf 356: debug_printf(" (%d)\n", local_ops->count());
357: debug_dump(1, *local_ops);
1.237 paf 358: #endif
359: // when they evaluate expression parameter,
360: // the object expression result
361: // does not need to be written into calling frame
362: // it must go into any expressions using that parameter
363: // hence, we zero junction.wcontext here, and later
364: // in .process we would test that field
365: // in decision "which wwrapper to use"
1.295.2.7 paf 366: value=ValuePtr(new VJunction(JunctionPtr(new Junction(
367: get_self(), 0,
1.257 paf 368: method_frame,
1.237 paf 369: rcontext,
370: op.code==OP_EXPR_CODE__STORE_PARAM?0:wcontext,
1.295.2.7 paf 371: local_ops))));
1.237 paf 372: // store param
373: // this op is executed from CALL local_ops only, so can not check method_frame_to_fill==0
374: frame->store_param(value);
1.29 paf 375: break;
376: }
377:
1.186 parser 378: case OP_PREPARE_TO_CONSTRUCT_OBJECT:
379: {
1.200 paf 380: wcontext->set_constructing(true);
1.186 parser 381: break;
382: }
1.219 paf 383:
384: case OP_PREPARE_TO_EXPRESSION:
385: {
386: wcontext->set_in_expression(true);
387: break;
388: }
389:
1.186 parser 390: case OP_CALL:
1.232 paf 391: case OP_CALL__WRITE:
1.29 paf 392: {
1.295.2.7 paf 393: local_ops=i.next().ops;
1.157 parser 394: #ifdef DEBUG_EXECUTE
1.295.2.7 paf 395: debug_printf(" (%d)\n", local_ops->count());
396: debug_dump(1, *local_ops);
1.237 paf 397:
1.295.2.7 paf 398: debug_printf("->\n");
1.157 parser 399: #endif
1.295.2.6 paf 400: value=stack.pop().value;
1.237 paf 401:
1.295.2.7 paf 402: JunctionPtr junction=value->get_junction();
1.237 paf 403: if(!junction)
404: throw Exception("parser.runtime",
405: last_get_element_name,
406: "(%s) not a method or junction, can not call it",
1.276 paf 407: value->type());
1.282 paf 408: /* no check needed, code compiled the way that that's impossible
1.276 paf 409: // check:
410: // that this is method-junction, not a code-junction
1.278 paf 411: // [disabling these contstructions:]
412: // $junction{code}
413: // ^junction[]
1.276 paf 414: if(!junction->method)
415: throw Exception("parser.runtime",
416: last_get_element_name,
417: "(%s) is code junction, can not call it",
1.237 paf 418: value->type());
1.282 paf 419: */
1.237 paf 420:
1.295.2.7 paf 421: VMethodFrame frame(pool(), last_get_element_name, *junction, method_frame/*caller*/);
1.237 paf 422: if(local_ops){ // store param code goes here
1.295.2.6 paf 423: stack.push(&frame); // argument for *STORE_PARAM ops
1.237 paf 424: execute(*local_ops);
1.295.2.6 paf 425: stack.pop().value;
1.237 paf 426: }
427: frame.fill_unspecified_params();
1.257 paf 428: VMethodFrame *saved_method_frame=method_frame;
1.295.2.6 paf 429: Value* saved_rcontext=rcontext;
1.257 paf 430: WContext *saved_wcontext=wcontext;
1.45 paf 431:
1.295.2.7 paf 432: VStateless_class *called_class=frame.junction.self->get_class();
433: ValuePtr new_self=get_self();
1.200 paf 434: if(wcontext->get_constructing()) {
435: wcontext->set_constructing(false);
1.237 paf 436: if(frame.junction.method->call_type!=Method::CT_STATIC) {
1.138 paf 437: // this is a constructor call
1.185 parser 438:
1.295.2.2 paf 439: if(ValuePtr value=called_class->create_new_value()) {
1.204 paf 440: // some stateless_class creatable derivates
1.285 paf 441: new_self=value;
1.204 paf 442: } else
1.223 paf 443: throw Exception("parser.runtime",
1.295.2.7 paf 444: frame.name(),
1.204 paf 445: "is not a constructor, system class '%s' can be constructed only implicitly",
1.295.2.7 paf 446: called_class->name()->cstr());
1.204 paf 447:
1.295.2.7 paf 448: frame.write(new_self,
1.136 paf 449: String::UL_CLEAN // not used, always an object, not string
1.83 paf 450: );
1.185 parser 451: } else
1.223 paf 452: throw Exception("parser.runtime",
1.295.2.7 paf 453: frame.name(),
1.185 parser 454: "method is static and can not be used as constructor");
1.249 paf 455: } else
1.295.2.7 paf 456: new_self=frame.junction.self;
1.75 paf 457:
1.295.2.7 paf 458: frame.set_self(new_self);
1.219 paf 459:
460: // see OP_PREPARE_TO_EXPRESSION
1.237 paf 461: frame.set_in_expression(wcontext->get_in_expression());
1.219 paf 462:
1.237 paf 463: rcontext=wcontext=&frame;
1.47 paf 464: {
1.237 paf 465: const Method& method=*frame.junction.method;
1.134 paf 466: Method::Call_type call_type=
1.295.2.7 paf 467: called_class==new_self.get() ? Method::CT_STATIC : Method::CT_DYNAMIC;
1.134 paf 468: if(
469: method.call_type==Method::CT_ANY ||
1.197 parser 470: method.call_type==call_type) { // allowed call type?
471: try {
1.283 paf 472: method_frame=&frame;
1.197 parser 473: if(method.native_code) { // native code?
474: method.check_actual_numbered_params(
1.295.2.7 paf 475: *frame.junction.self,
1.237 paf 476: frame.name(), frame.numbered_params());
1.197 parser 477: method.native_code(
478: *this,
1.237 paf 479: frame.name(), frame.numbered_params()); // execute it
1.283 paf 480: } else // parser code, execute it
1.295.2.7 paf 481: recoursion_checked_execute(frame.name(), *method.parser_code);
1.197 parser 482: } catch(...) {
483: // record it to stack trace
1.295.2.7 paf 484: exception_trace.push(frame.name());
1.295.2.5 paf 485: rethrow;
1.159 parser 486: }
1.197 parser 487: } else
1.223 paf 488: throw Exception("parser.runtime",
1.295.2.7 paf 489: frame.name(),
1.134 paf 490: "is not allowed to be called %s",
491: call_type==Method::CT_STATIC?"statically":"dynamically");
492:
1.47 paf 493: }
1.232 paf 494: StringOrValue result=wcontext->result();
1.45 paf 495:
1.257 paf 496: wcontext=saved_wcontext;
497: rcontext=saved_rcontext;
498: method_frame=saved_method_frame;
1.220 paf 499:
500: #ifdef DEBUG_STRING_APPENDS_VS_EXPANDS
501: if(const String *s=value->get_string())
502: wcontext_result_size+=s->used_rows()*sizeof(String::Chunk::Row);
503: #endif
1.213 paf 504:
1.232 paf 505: if(op.code==OP_CALL__WRITE) {
1.236 paf 506: write_assign_lang(result, last_get_element_name);
1.232 paf 507: } else { // OP_CALL
1.295.2.6 paf 508: stack.push(&result.as_value());
1.232 paf 509: }
510:
1.157 parser 511: #ifdef DEBUG_EXECUTE
1.295.2.7 paf 512: debug_printf("<-returned");
1.157 parser 513: #endif
1.49 paf 514: break;
515: }
516:
1.55 paf 517: // expression ops: unary
518: case OP_NEG:
519: {
1.295.2.7 paf 520: a=stack.pop().value;
521: value=ValuePtr(new VDouble(-a->as_double()));
1.295.2.6 paf 522: stack.push(value);
1.55 paf 523: break;
524: }
525: case OP_INV:
526: {
1.295.2.7 paf 527: a=stack.pop().value;
528: value=ValuePtr(new VDouble(~a->as_int()));
1.295.2.6 paf 529: stack.push(value);
1.55 paf 530: break;
531: }
532: case OP_NOT:
533: {
1.295.2.7 paf 534: a=stack.pop().value;
535: value=ValuePtr(new VBool(!a->as_bool()));
1.295.2.6 paf 536: stack.push(value);
1.55 paf 537: break;
538: }
1.62 paf 539: case OP_DEF:
540: {
1.295.2.7 paf 541: a=stack.pop().value;
542: value=ValuePtr(new VBool(a->is_defined()));
1.295.2.6 paf 543: stack.push(value);
1.62 paf 544: break;
545: }
546: case OP_IN:
547: {
1.199 paf 548: /// @test String::cmp
1.295.2.7 paf 549: a=stack.pop().value;
550: CharPtr path=a->as_string(&pool())->cstr();
551: value=ValuePtr(new VBool(
552: request_info.uri && strncmp(path, request_info.uri, strlen(path))==0));
1.295.2.6 paf 553: stack.push(value);
1.62 paf 554: break;
555: }
556: case OP_FEXISTS:
557: {
1.295.2.7 paf 558: a=stack.pop().value;
559: value=ValuePtr(new VBool(
560: file_readable(absolute(a->as_string(&pool())))));
1.295.2.6 paf 561: stack.push(value);
1.148 paf 562: break;
563: }
564: case OP_DEXISTS:
565: {
1.295.2.7 paf 566: a=stack.pop().value;
567: value=ValuePtr(new VBool(
568: dir_readable(absolute(a->as_string(&pool())))));
1.295.2.6 paf 569: stack.push(value);
1.62 paf 570: break;
571: }
1.55 paf 572:
573: // expression ops: binary
574: case OP_SUB:
1.53 paf 575: {
1.295.2.6 paf 576: b=stack.pop().value; a=stack.pop().value;
1.295.2.7 paf 577: value=ValuePtr(new VDouble(a->as_double() - b->as_double()));
1.295.2.6 paf 578: stack.push(value);
1.53 paf 579: break;
580: }
1.55 paf 581: case OP_ADD:
1.53 paf 582: {
1.295.2.6 paf 583: b=stack.pop().value; a=stack.pop().value;
1.295.2.7 paf 584: value=ValuePtr(new VDouble(a->as_double() + b->as_double()));
1.295.2.6 paf 585: stack.push(value);
1.53 paf 586: break;
587: }
1.49 paf 588: case OP_MUL:
589: {
1.295.2.6 paf 590: b=stack.pop().value; a=stack.pop().value;
1.295.2.7 paf 591: value=ValuePtr(new VDouble(a->as_double() * b->as_double()));
1.295.2.6 paf 592: stack.push(value);
1.53 paf 593: break;
594: }
595: case OP_DIV:
596: {
1.295.2.6 paf 597: b=stack.pop().value; a=stack.pop().value;
1.170 parser 598:
599: double a_double=a->as_double();
600: double b_double=b->as_double();
601:
1.171 parser 602: if(b_double == 0) {
1.295.2.7 paf 603: StringPtr problem_source=b->as_string(&pool());
1.223 paf 604: throw Exception("number.zerodivision",
1.171 parser 605: problem_source,
1.170 parser 606: "Division by zero");
1.171 parser 607: }
1.170 parser 608:
1.295.2.7 paf 609: value=ValuePtr(new VDouble(a_double / b_double));
1.295.2.6 paf 610: stack.push(value);
1.54 paf 611: break;
612: }
1.55 paf 613: case OP_MOD:
614: {
1.295.2.6 paf 615: b=stack.pop().value; a=stack.pop().value;
1.170 parser 616:
1.173 parser 617: double a_double=a->as_double();
618: double b_double=b->as_double();
1.170 parser 619:
1.173 parser 620: if(b_double == 0) {
1.295.2.7 paf 621: StringPtr problem_source=b->as_string(&pool());
1.223 paf 622: throw Exception("number.zerodivision",
1.171 parser 623: problem_source,
1.170 parser 624: "Modulus by zero");
1.171 parser 625: }
1.170 parser 626:
1.295.2.7 paf 627: value=ValuePtr(new VDouble(fmod(a_double, b_double)));
1.295.2.6 paf 628: stack.push(value);
1.201 paf 629: break;
630: }
631: case OP_INTDIV:
632: {
1.295.2.6 paf 633: b=stack.pop().value; a=stack.pop().value;
1.201 paf 634:
635: int a_int=a->as_int();
636: int b_int=b->as_int();
637:
638: if(b_int == 0) {
1.295.2.7 paf 639: StringPtr problem_source=b->as_string(&pool());
1.223 paf 640: throw Exception("number.zerodivision",
1.201 paf 641: problem_source,
642: "Division by zero");
643: }
644:
1.295.2.7 paf 645: value=ValuePtr(new VInt(a_int / b_int));
1.295.2.6 paf 646: stack.push(value);
1.55 paf 647: break;
648: }
1.274 paf 649: case OP_BIN_SL:
650: {
1.295.2.6 paf 651: b=stack.pop().value; a=stack.pop().value;
1.295.2.7 paf 652: value=ValuePtr(new VInt(
1.274 paf 653: a->as_int() <<
1.295.2.7 paf 654: b->as_int()));
1.295.2.6 paf 655: stack.push(value);
1.274 paf 656: break;
657: }
658: case OP_BIN_SR:
659: {
1.295.2.6 paf 660: b=stack.pop().value; a=stack.pop().value;
1.295.2.7 paf 661: value=ValuePtr(new VInt(
1.274 paf 662: a->as_int() >>
1.295.2.7 paf 663: b->as_int()));
1.295.2.6 paf 664: stack.push(value);
1.274 paf 665: break;
666: }
1.55 paf 667: case OP_BIN_AND:
1.54 paf 668: {
1.295.2.6 paf 669: b=stack.pop().value; a=stack.pop().value;
1.295.2.7 paf 670: value=ValuePtr(new VInt(
1.155 parser 671: a->as_int() &
1.295.2.7 paf 672: b->as_int()));
1.295.2.6 paf 673: stack.push(value);
1.54 paf 674: break;
675: }
1.55 paf 676: case OP_BIN_OR:
1.54 paf 677: {
1.295.2.6 paf 678: b=stack.pop().value; a=stack.pop().value;
1.295.2.7 paf 679: value=ValuePtr(new VInt(
1.155 parser 680: a->as_int() |
1.295.2.7 paf 681: b->as_int()));
1.295.2.6 paf 682: stack.push(value);
1.55 paf 683: break;
684: }
1.56 paf 685: case OP_BIN_XOR:
686: {
1.295.2.6 paf 687: b=stack.pop().value; a=stack.pop().value;
1.295.2.7 paf 688: value=ValuePtr(new VInt(
1.155 parser 689: a->as_int() ^
1.295.2.7 paf 690: b->as_int()));
1.295.2.6 paf 691: stack.push(value);
1.56 paf 692: break;
693: }
1.55 paf 694: case OP_LOG_AND:
695: {
1.295.2.7 paf 696: local_ops=stack.pop().ops; a=stack.pop().value;
1.263 paf 697: bool result;
1.209 paf 698: if(a->as_bool()) {
1.295.2.7 paf 699: execute(*local_ops);
1.295.2.6 paf 700: b=stack.pop().value;
1.263 paf 701: result=b->as_bool();
1.209 paf 702: } else
1.263 paf 703: result=false;
1.295.2.7 paf 704: value=ValuePtr(new VBool(result));
1.295.2.6 paf 705: stack.push(value);
1.55 paf 706: break;
707: }
708: case OP_LOG_OR:
709: {
1.295.2.7 paf 710: local_ops=stack.pop().ops; a=stack.pop().value;
1.263 paf 711: bool result;
1.209 paf 712: if(a->as_bool())
1.263 paf 713: result=true;
1.209 paf 714: else {
1.295.2.7 paf 715: execute(*local_ops);
1.295.2.6 paf 716: b=stack.pop().value;
1.263 paf 717: result=b->as_bool();
1.209 paf 718: }
1.295.2.7 paf 719: value=ValuePtr(new VBool(result));
1.295.2.6 paf 720: stack.push(value);
1.56 paf 721: break;
722: }
723: case OP_LOG_XOR:
724: {
1.295.2.6 paf 725: b=stack.pop().value; a=stack.pop().value;
1.295.2.7 paf 726: value=ValuePtr(new VBool(a->as_bool() ^ b->as_bool()));
1.295.2.6 paf 727: stack.push(value);
1.55 paf 728: break;
729: }
730: case OP_NUM_LT:
731: {
1.295.2.6 paf 732: b=stack.pop().value; a=stack.pop().value;
1.263 paf 733: double result=a->as_double() - b->as_double();
1.295.2.7 paf 734: value=ValuePtr(new VBool(result < 0.0));
1.295.2.6 paf 735: stack.push(value);
1.55 paf 736: break;
737: }
738: case OP_NUM_GT:
739: {
1.295.2.6 paf 740: b=stack.pop().value; a=stack.pop().value;
1.263 paf 741: double result=a->as_double() - b->as_double();
1.295.2.7 paf 742: value=ValuePtr(new VBool(result > 0.0));
1.295.2.6 paf 743: stack.push(value);
1.55 paf 744: break;
745: }
746: case OP_NUM_LE:
747: {
1.295.2.6 paf 748: b=stack.pop().value; a=stack.pop().value;
1.263 paf 749: double result=a->as_double() - b->as_double();
1.295.2.7 paf 750: value=ValuePtr(new VBool(result <= 0.0));
1.295.2.6 paf 751: stack.push(value);
1.55 paf 752: break;
753: }
754: case OP_NUM_GE:
755: {
1.295.2.6 paf 756: b=stack.pop().value; a=stack.pop().value;
1.263 paf 757: double result=a->as_double() - b->as_double();
1.295.2.7 paf 758: value=ValuePtr(new VBool(result >= 0.0));
1.295.2.6 paf 759: stack.push(value);
1.55 paf 760: break;
761: }
762: case OP_NUM_EQ:
763: {
1.295.2.6 paf 764: b=stack.pop().value; a=stack.pop().value;
1.263 paf 765: double result=a->as_double() - b->as_double();
1.295.2.7 paf 766: value=ValuePtr(new VBool(result == 0.0));
1.295.2.6 paf 767: stack.push(value);
1.55 paf 768: break;
769: }
770: case OP_NUM_NE:
771: {
1.295.2.6 paf 772: b=stack.pop().value; a=stack.pop().value;
1.263 paf 773: double result=a->as_double() - b->as_double();
1.295.2.7 paf 774: value=ValuePtr(new VBool(result != 0.0));
1.295.2.6 paf 775: stack.push(value);
1.54 paf 776: break;
777: }
1.58 paf 778: case OP_STR_LT:
779: {
1.295.2.6 paf 780: b=stack.pop().value; a=stack.pop().value;
1.295.2.7 paf 781: value=ValuePtr(new VBool(a->as_string(&pool()) < b->as_string(&pool())));
1.295.2.6 paf 782: stack.push(value);
1.58 paf 783: break;
784: }
785: case OP_STR_GT:
786: {
1.295.2.6 paf 787: b=stack.pop().value; a=stack.pop().value;
1.295.2.7 paf 788: value=ValuePtr(new VBool(a->as_string(&pool()) > b->as_string(&pool())));
1.295.2.6 paf 789: stack.push(value);
1.58 paf 790: break;
791: }
1.55 paf 792: case OP_STR_LE:
1.58 paf 793: {
1.295.2.6 paf 794: b=stack.pop().value; a=stack.pop().value;
1.295.2.7 paf 795: value=ValuePtr(new VBool(a->as_string(&pool()) <= b->as_string(&pool())));
1.295.2.6 paf 796: stack.push(value);
1.58 paf 797: break;
798: }
1.55 paf 799: case OP_STR_GE:
1.54 paf 800: {
1.295.2.6 paf 801: b=stack.pop().value; a=stack.pop().value;
1.295.2.7 paf 802: value=ValuePtr(new VBool(a->as_string(&pool()) >= b->as_string(&pool())));
1.295.2.6 paf 803: stack.push(value);
1.58 paf 804: break;
805: }
806: case OP_STR_EQ:
807: {
1.295.2.6 paf 808: b=stack.pop().value; a=stack.pop().value;
1.295.2.7 paf 809: value=ValuePtr(new VBool(a->as_string(&pool()) == b->as_string(&pool())));
1.295.2.6 paf 810: stack.push(value);
1.58 paf 811: break;
812: }
813: case OP_STR_NE:
814: {
1.295.2.6 paf 815: b=stack.pop().value; a=stack.pop().value;
1.295.2.7 paf 816: value=ValuePtr(new VBool(a->as_string(&pool()) != b->as_string(&pool())));
1.295.2.6 paf 817: stack.push(value);
1.103 paf 818: break;
819: }
820: case OP_IS:
821: {
1.295.2.6 paf 822: b=stack.pop().value; a=stack.pop().value;
1.295.2.7 paf 823: value=ValuePtr(new VBool(a->is(b->as_string(&pool())->cstr())));
1.295.2.6 paf 824: stack.push(value);
1.28 paf 825: break;
826: }
827:
1.11 paf 828: default:
1.295.2.7 paf 829: throw Exception(Exception::undefined_type,
830: Exception::undefined_source,
1.139 paf 831: "invalid opcode %d", op.code);
1.11 paf 832: }
833: }
1.1 paf 834: }
1.17 paf 835:
1.292 paf 836: /**
837: @todo cache|prepare junctions
838: @bug ^superbase:method would dynamically call ^base:method if there is any
839: */
1.295.2.7 paf 840: ValuePtr Request::get_element(StringPtr& remember_name, bool can_call_operator) {
841: StringPtr name=stack.pop().string; remember_name=name;
842: Value* ncontext=stack.pop().value.get();
843: ValuePtr value(0);
1.249 paf 844: if(can_call_operator) {
1.295.2.7 paf 845: if(Method* method=main_class->get_method(name)) // looking operator of that name FIRST
846: value=ValuePtr(new VJunction(JunctionPtr(new Junction(
847: main_class, method, 0,0,0, ArrayOperationPtr(0)))));
1.249 paf 848: }
849: if(!value) {
850: if(!wcontext->get_constructing() // not constructing
851: && wcontext->get_somebody_entered_some_class() ) // ^class:method
852: if(VStateless_class *called_class=ncontext->get_class())
853: if(VStateless_class *read_class=rcontext->get_class())
854: if(read_class->derived_from(*called_class)) // current derived from called
1.295.2.7 paf 855: if(ValuePtr base=get_self()->base()) { // doing DYNAMIC call
856: Temp_derived temp_derived(*base, VObjectPtr(0)); // temporarily prevent go-back-down virtual calls
1.290 paf 857: value=base->get_element(name, *base, false); // virtual-up lookup starting from parent
1.289 paf 858: goto value_ready;
1.249 paf 859: }
860: }
1.216 paf 861: if(!value)
1.290 paf 862: value=ncontext->get_element(name, *ncontext, false);
1.291 paf 863:
864: if(value && wcontext->get_constructing())
1.295.2.7 paf 865: if(JunctionPtr junction=value->get_junction()) {
866: if(junction->self->get_class()!=ncontext)
1.291 paf 867: throw Exception("parser.runtime",
1.295.2.7 paf 868: name,
1.291 paf 869: "constructor must be declared in class %s", ncontext->get_class()->name_cstr());
870: }
1.249 paf 871:
1.289 paf 872: value_ready:
1.284 paf 873: if(value)
1.295.2.7 paf 874: value=process_to_value(value); // process possible code-junction
1.284 paf 875: else
1.295.2.7 paf 876: value=ValuePtr(new VVoid);
1.63 paf 877:
1.17 paf 878: return value;
1.34 paf 879: }
1.70 paf 880:
1.162 parser 881: /** @param intercept_string
1.116 paf 882: - true:
883: they want result=string value,
884: possible object result goes to wcontext
885: - false:
886: they want any result[string|object]
887: nothing goes to wcontext.
1.125 paf 888: used in @c (expression) params evaluation
1.224 paf 889:
890: using the fact it's either string_ or value_ result requested to speed up checkes
1.116 paf 891: */
1.295.2.7 paf 892: StringOrValue Request::process(ValuePtr input_value, bool intercept_string) {
1.228 paf 893: StringOrValue result;
1.295.2.7 paf 894: JunctionPtr junction=input_value->get_junction();
1.70 paf 895: if(junction && junction->code) { // is it a code-junction?
1.92 paf 896: // process it
1.157 parser 897: #ifdef DEBUG_EXECUTE
1.295.2.7 paf 898: debug_printf("ja->\n");
1.157 parser 899: #endif
1.238 paf 900:
1.257 paf 901: if(!junction->method_frame)
1.240 paf 902: throw Exception("parser.runtime",
1.295.2.7 paf 903: Exception::undefined_source,
1.240 paf 904: "junction used outside of context");
905:
1.257 paf 906: VMethodFrame *saved_method_frame=method_frame;
1.295.2.6 paf 907: Value* saved_rcontext=rcontext;
1.257 paf 908: WContext *saved_wcontext=wcontext;
1.240 paf 909:
1.257 paf 910: method_frame=junction->method_frame;
1.240 paf 911: rcontext=junction->rcontext;
1.225 paf 912:
1.109 paf 913: // for expression method params
914: // wcontext is set 0
915: // using the fact in decision "which wwrapper to use"
1.240 paf 916: bool using_code_frame=intercept_string && junction->wcontext;
1.109 paf 917: if(using_code_frame) {
1.71 paf 918: // almost plain wwrapper about junction wcontext,
919: // BUT intercepts string writes
1.295.2.7 paf 920: VCodeFrame local(*junction->wcontext, junction->wcontext);
1.225 paf 921: wcontext=&local;
1.207 paf 922:
1.225 paf 923: // execute it
1.295.2.7 paf 924: recoursion_checked_execute(Exception::undefined_source/*result_name*/, *junction->code);
1.226 paf 925:
1.71 paf 926: // CodeFrame soul:
927: // string writes were intercepted
928: // returning them as the result of getting code-junction
1.295.2.7 paf 929: result.set_string(wcontext->get_string(&pool()));
1.224 paf 930: } else {
1.225 paf 931: // plain wwrapper
1.295.2.7 paf 932: WWrapper local(ValuePtr(0)/*empty*/, wcontext);
1.225 paf 933: wcontext=&local;
934:
935: // execute it
1.295.2.7 paf 936: recoursion_checked_execute(Exception::undefined_source/*result_name*/, *junction->code);
1.226 paf 937:
1.228 paf 938: result=wcontext->result();
1.224 paf 939: }
1.70 paf 940:
1.257 paf 941: wcontext=saved_wcontext;
942: rcontext=saved_rcontext;
943: method_frame=saved_method_frame;
1.70 paf 944:
1.157 parser 945: #ifdef DEBUG_EXECUTE
1.295.2.7 paf 946: debug_printf("<-ja returned");
1.157 parser 947: #endif
1.224 paf 948: } else {
1.228 paf 949: result.set_value(input_value);
1.224 paf 950: }
1.228 paf 951: return result;
1.85 paf 952: }
953:
1.295.2.7 paf 954: StringPtr Request::execute_method(VMethodFrame& amethod_frame, const Method& method) {
1.257 paf 955: VMethodFrame *saved_method_frame=method_frame;
1.295.2.6 paf 956: Value* saved_rcontext=rcontext;
1.257 paf 957: WContext *saved_wcontext=wcontext;
1.99 paf 958:
959: // initialize contexts
1.286 paf 960: rcontext=wcontext=method_frame=&amethod_frame;
1.99 paf 961:
962: // execute!
963: execute(*method.parser_code);
964:
965: // result
1.295.2.7 paf 966: StringPtr result=wcontext->result().as_string(&pool());
1.208 paf 967:
1.257 paf 968: wcontext=saved_wcontext;
969: rcontext=saved_rcontext;
970: method_frame=saved_method_frame;
1.242 paf 971:
972: // return
973: return result;
1.208 paf 974: }
975:
1.295.2.7 paf 976: StringPtr Request::execute_method(ValuePtr aself,
977: const Method& method, VStringPtr optional_param,
978: bool do_return_string) {
1.257 paf 979: VMethodFrame *saved_method_frame=method_frame;
1.295.2.6 paf 980: Value* saved_rcontext=rcontext;
1.257 paf 981: WContext *saved_wcontext=wcontext;
1.208 paf 982:
1.295.2.7 paf 983: Junction local_junction(aself, &method, 0,0,0, ArrayOperationPtr(0));
1.282 paf 984: VMethodFrame local_frame(pool(), method.name, local_junction, method_frame/*caller*/);
1.242 paf 985: if(optional_param && local_frame.can_store_param()) {
986: local_frame.store_param(optional_param);
987: local_frame.fill_unspecified_params();
988: }
1.285 paf 989: local_frame.set_self(aself);
1.257 paf 990: rcontext=wcontext=method_frame=&local_frame;
1.246 paf 991:
992: // prevent non-string writes for better error reporting
1.295.2.7 paf 993: if(do_return_string)
994: wcontext->write(pool(), ValuePtr(new VVoid));
1.208 paf 995:
996: // execute!
997: execute(*method.parser_code);
998:
999: // result
1.295.2.7 paf 1000: StringPtr result(0);
1001: if(do_return_string)
1002: result=wcontext->result().as_string(&pool());
1.99 paf 1003:
1.257 paf 1004: wcontext=saved_wcontext;
1005: rcontext=saved_rcontext;
1006: method_frame=saved_method_frame;
1.295.2.7 paf 1007:
1008: return result;
1.242 paf 1009: }
1010:
1.295.2.8! paf 1011: StringPtr Request::execute_nonvirtual_method(VStateless_class& aclass,
1.295.2.5 paf 1012: StringPtr method_name, VStringPtr optional_param,
1.295.2.7 paf 1013: bool do_return_string,
1.242 paf 1014: const Method **return_method) {
1015:
1.295.2.8! paf 1016: const Method *method=aclass.get_method(method_name);
1.295.2.7 paf 1017: StringPtr result(0);
1.242 paf 1018: if(return_method)
1019: *return_method=method;
1020:
1021: if(method)
1.295.2.8! paf 1022: return execute_method(ValuePtr(&aclass), *method, optional_param, do_return_string);
1.295.2.7 paf 1023: else
1024: return 0;
1.99 paf 1025: }
1026:
1.295.2.7 paf 1027: StringPtr Request::execute_virtual_method(ValuePtr aself,
1.295.2.5 paf 1028: StringPtr method_name) {
1.295.2.7 paf 1029: if(ValuePtr value=aself->get_element(method_name, *aself, false))
1030: if(JunctionPtr junction=value->get_junction())
1031: if(const Method *method=junction->method)
1032: return execute_method(aself, *method, VStringPtr(0)/*no params*/, true);
1.188 parser 1033:
1.176 parser 1034: return 0;
1035: }
E-mail: