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