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