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