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