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