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