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