Annotation of parser3/src/main/execute.C, revision 1.295.2.27.2.20
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. 0(paf 8:3): static const char* IDENT_EXECUTE_C="$Date: 2003/04/03 10:48:25 $";
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
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.27.2. (paf 70:): void debug_dump(SAPI_Info& sapi_info, 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.27.2. 7(paf 73:3): OPCODE opcode=i.next().code();
1.1 paf 74:
1.295.2.27.2. 7(paf 75:3): if(opcode==OP_VALUE || opcode==OP_STRING__WRITE) {
0(paf 76:3): Operation::Origin origin=i.next().origin;
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. 7(paf 81:3): level*4, "", opcode_name[opcode],
2(paf 82:3): value.get_string()->cstr(), value.type());
1.133 paf 83: continue;
1.15 paf 84: }
1.295.2.27.2. 7(paf 85:3): debug_printf(sapi_info, "%*s%s", level*4, "", opcode_name[opcode]);
1.1 paf 86:
1.295.2.27.2. 7(paf 87:3): switch(opcode) {
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):
0(paf 108:3): const String* debug_name=0; Operation::Origin debug_origin; //bool is_debug_junction=false;
5(paf 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. 0(paf 122:3): OPCODE opcode=i.next().code;
5(paf 123:3):
1.157 parser 124: #ifdef DEBUG_EXECUTE
1.295.2.27.2. 0(paf 125:3): debug_printf(sapi_info, "%d:%s", stack.top_index()+1, opcode_name[opcode]);
1.157 parser 126: #endif
1.11 paf 127:
1.295.2.27.2. 0(paf 128:3): switch(opcode) {
1.51 paf 129: // param in next instruction
1.52 paf 130: case OP_VALUE:
1.32 paf 131: {
1.295.2.27.2. 0(paf 132:3): debug_origin=i.next().origin;
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. 0(paf 250:3): i.next(); // ignore origin
2(paf 251:3): Value* value=i.next().value;
1.157 parser 252: #ifdef DEBUG_EXECUTE
1.295.2.27.2. (paf 253:): debug_printf(sapi_info, " \"%s\"", value->get_string()->cstr());
1.157 parser 254: #endif
1.295.2.27.2. (paf 255:): write_no_lang(*value->get_string());
1.13 paf 256: break;
1.14 paf 257: }
1.13 paf 258:
1.215 paf 259: case OP_GET_ELEMENT_OR_OPERATOR:
260: {
1.295.2.27.2. 5(paf 261:3): const String& name=stack.pop().string(); debug_name=&name;
262:3): Value& ncontext=stack.pop().value();
263:3): Value& value=get_element(ncontext, name, true);
1.295.2.6 paf 264: stack.push(value);
1.215 paf 265: break;
266: }
1.15 paf 267: case OP_GET_ELEMENT:
1.11 paf 268: {
1.295.2.27.2. 5(paf 269:3): const String& name=stack.pop().string(); debug_name=&name;
270:3): Value& ncontext=stack.pop().value();
271:3): Value& value=get_element(ncontext, name, false);
1.295.2.6 paf 272: stack.push(value);
1.17 paf 273: break;
274: }
275:
1.18 paf 276: case OP_GET_ELEMENT__WRITE:
1.17 paf 277: {
1.295.2.27.2. 5(paf 278:3): const String& name=stack.pop().string(); debug_name=&name;
279:3): Value& ncontext=stack.pop().value();
280:3): Value& value=get_element(ncontext, name, false);
2(paf 281:3): write_assign_lang(value);
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.27.2. 2(paf 288:3): ArrayOperation& local_ops=*i.next().ops;
1.226 paf 289:
1.257 paf 290: WContext *saved_wcontext=wcontext;
1.295.2.27.2. (paf 291:): String::Language saved_lang=flang;
292:): flang=String::L_PASS_APPENDED;
293:): WWrapper local(0/*empty*/, wcontext);
1.226 paf 294: wcontext=&local;
295:
1.295.2.27.2. 2(paf 296:3): execute(local_ops);
1.226 paf 297:
1.295.2.27.2. 2(paf 298:3): stack.push(wcontext->result().as_value());
1.257 paf 299: flang=saved_lang;
300: wcontext=saved_wcontext;
1.13 paf 301: break;
1.15 paf 302: }
1.13 paf 303:
1.226 paf 304: case OP_STRING_POOL:
1.49 paf 305: {
1.295.2.27.2. 2(paf 306:3): ArrayOperation& local_ops=*i.next().ops;
1.226 paf 307:
1.257 paf 308: WContext *saved_wcontext=wcontext;
1.295.2.27.2. (paf 309:): WWrapper local(0 /*empty*/, wcontext);
1.226 paf 310: wcontext=&local;
311:
1.295.2.27.2. 2(paf 312:3): execute(local_ops);
1.226 paf 313:
1.295.2.27.2. 2(paf 314:3): Value* value;
1.49 paf 315: // from "$a $b" part of expression taking only string value,
316: // ignoring any other content of wcontext
1.295.2.27.2. (paf 317:): if(const String* string=wcontext->get_string())
318:): value=new VString(*string);
1.80 paf 319: else
1.295.2.27.2. (paf 320:): value=new VVoid;
2(paf 321:3): stack.push(*value);
1.295.2.7 paf 322:
1.257 paf 323: wcontext=saved_wcontext;
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.27.2. 2(paf 330:3): Value& value=stack.pop().value();
331:3): VMethodFrame& frame=stack.top_value().method_frame();
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.295.2.27.2. 2(paf 333:3): 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.27.2. 2(paf 340:3): ArrayOperation& local_ops=*i.next().ops;
341:3): VMethodFrame& frame=stack.top_value().method_frame();
1.237 paf 342: #ifdef DEBUG_EXECUTE
1.295.2.27.2. 2(paf 343:3): debug_printf(sapi_info, " (%d)\n", local_ops.count());
3(paf 344:3): debug_dump(sapi_info, 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.27.2. 2(paf 353:3): Value& value=*new VJunction(new Junction(
1.295.2.7 paf 354: get_self(), 0,
1.257 paf 355: method_frame,
1.237 paf 356: rcontext,
1.295.2.27.2. 0(paf 357:3): opcode==OP_EXPR_CODE__STORE_PARAM?0:wcontext,
2(paf 358:3): &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
1.295.2.27.2. 2(paf 361:3): 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.27.2. 6(paf 380:3): //is_debug_junction=true;
2(paf 381:3): ArrayOperation* local_ops=i.next().ops;
1.157 parser 382: #ifdef DEBUG_EXECUTE
1.295.2.27.2. 3(paf 383:3): debug_printf(sapi_info, " (%d)\n", local_ops?local_ops->count():0);
1.295.2.13 paf 384: if(local_ops)
1.295.2.27.2. (paf 385:): debug_dump(sapi_info, 1, *local_ops);
1.237 paf 386:
1.295.2.11 paf 387: debug_printf(sapi_info, "->\n");
1.157 parser 388: #endif
1.295.2.27.2. 2(paf 389:3): Value& value=stack.pop().value();
1.237 paf 390:
1.295.2.27.2. 2(paf 391:3): Junction* junction=value.get_junction();
8(paf 392:3): if(!junction) {
393:3): if(value.is("void"))
394:3): throw Exception("parser.runtime",
395:3): 0,
396:3): "undefined method");
397:3): else
398:3): throw Exception("parser.runtime",
399:3): 0,
400:3): "is '%s', not a method or junction, can not call it",
401:3): value.type());
402:3): }
1.282 paf 403: /* no check needed, code compiled the way that that's impossible
1.276 paf 404: // check:
405: // that this is method-junction, not a code-junction
1.278 paf 406: // [disabling these contstructions:]
407: // $junction{code}
408: // ^junction[]
1.276 paf 409: if(!junction->method)
410: throw Exception("parser.runtime",
1.295.2.27.2. (paf 411:):
8(paf 412:3): "is '%s', it is code junction, can not call it",
2(paf 413:3): value.type());
1.282 paf 414: */
1.237 paf 415:
1.295.2.27.2. (paf 416:): VMethodFrame frame(*junction, method_frame/*caller*/);
1.237 paf 417: if(local_ops){ // store param code goes here
1.295.2.27.2. 2(paf 418:3): stack.push(frame); // argument for *STORE_PARAM ops
1.237 paf 419: execute(*local_ops);
1.295.2.27.2. 2(paf 420:3): stack.pop().value();
1.237 paf 421: }
422: frame.fill_unspecified_params();
1.257 paf 423: VMethodFrame *saved_method_frame=method_frame;
1.295.2.6 paf 424: Value* saved_rcontext=rcontext;
1.257 paf 425: WContext *saved_wcontext=wcontext;
1.45 paf 426:
1.295.2.27.2. (paf 427:): VStateless_class& called_class=*frame.junction.self.get_class();
428:): Value* new_self=&get_self();
1.200 paf 429: if(wcontext->get_constructing()) {
430: wcontext->set_constructing(false);
1.237 paf 431: if(frame.junction.method->call_type!=Method::CT_STATIC) {
1.138 paf 432: // this is a constructor call
1.185 parser 433:
1.295.2.27.2. (paf 434:): if(Value* value=called_class.create_new_value()) {
1.204 paf 435: // some stateless_class creatable derivates
1.285 paf 436: new_self=value;
1.204 paf 437: } else
1.223 paf 438: throw Exception("parser.runtime",
1.295.2.27.2. (paf 439:): 0, //&frame.name(),
1.204 paf 440: "is not a constructor, system class '%s' can be constructed only implicitly",
1.295.2.27.2. (paf 441:): called_class.name().cstr());
1.204 paf 442:
1.295.2.27.2. (paf 443:): frame.write(*new_self,
444:): String::L_CLEAN // not used, always an object, not string
1.83 paf 445: );
1.185 parser 446: } else
1.223 paf 447: throw Exception("parser.runtime",
1.295.2.27.2. (paf 448:): 0, //&frame.name(),
1.185 parser 449: "method is static and can not be used as constructor");
1.249 paf 450: } else
1.295.2.27.2. (paf 451:): new_self=&frame.junction.self;
1.75 paf 452:
1.295.2.27.2. (paf 453:): frame.set_self(*new_self);
1.219 paf 454:
455: // see OP_PREPARE_TO_EXPRESSION
1.237 paf 456: frame.set_in_expression(wcontext->get_in_expression());
1.219 paf 457:
1.237 paf 458: rcontext=wcontext=&frame;
1.47 paf 459: {
1.237 paf 460: const Method& method=*frame.junction.method;
1.134 paf 461: Method::Call_type call_type=
1.295.2.27.2. (paf 462:): &called_class==new_self ? Method::CT_STATIC : Method::CT_DYNAMIC;
1.134 paf 463: if(
464: method.call_type==Method::CT_ANY ||
1.197 parser 465: method.call_type==call_type) { // allowed call type?
1.295.2.27.2. 5(paf 466:3): method_frame=&frame;
467:3): if(method.native_code) { // native code?
468:3): method.check_actual_numbered_params(
469:3): frame.junction.self,
470:3): /*frame.name(), */frame.numbered_params());
471:3): method.native_code(
472:3): *this,
9(paf 473:3): *frame.numbered_params()); // execute it
5(paf 474:3): } else // parser code, execute it
475:3): recoursion_checked_execute(/*frame.name(), */*method.parser_code);
1.197 parser 476: } else
1.223 paf 477: throw Exception("parser.runtime",
1.295.2.27.2. (paf 478:): 0, //&frame.name(),
1.134 paf 479: "is not allowed to be called %s",
480: call_type==Method::CT_STATIC?"statically":"dynamically");
481:
1.47 paf 482: }
1.232 paf 483: StringOrValue result=wcontext->result();
1.45 paf 484:
1.257 paf 485: wcontext=saved_wcontext;
486: rcontext=saved_rcontext;
487: method_frame=saved_method_frame;
1.213 paf 488:
1.295.2.27.2. 0(paf 489:3): if(opcode==OP_CALL__WRITE) {
(paf 490:): write_assign_lang(result);
1.232 paf 491: } else { // OP_CALL
1.295.2.27.2. 2(paf 492:3): stack.push(result.as_value());
1.232 paf 493: }
494:
1.157 parser 495: #ifdef DEBUG_EXECUTE
1.295.2.11 paf 496: debug_printf(sapi_info, "<-returned");
1.157 parser 497: #endif
1.295.2.27.2. 6(paf 498:3): //is_debug_junction=false;
1.49 paf 499: break;
500: }
501:
1.55 paf 502: // expression ops: unary
503: case OP_NEG:
504: {
1.295.2.27.2. 2(paf 505:3): Value& a=stack.pop().value();
506:3): Value& value=*new VDouble(-a.as_double());
1.295.2.6 paf 507: stack.push(value);
1.55 paf 508: break;
509: }
510: case OP_INV:
511: {
1.295.2.27.2. 2(paf 512:3): Value& a=stack.pop().value();
513:3): Value& value=*new VDouble(~a.as_int());
1.295.2.6 paf 514: stack.push(value);
1.55 paf 515: break;
516: }
517: case OP_NOT:
518: {
1.295.2.27.2. 2(paf 519:3): Value& a=stack.pop().value();
520:3): Value& value=*new VBool(!a.as_bool());
1.295.2.6 paf 521: stack.push(value);
1.55 paf 522: break;
523: }
1.62 paf 524: case OP_DEF:
525: {
1.295.2.27.2. 2(paf 526:3): Value& a=stack.pop().value();
527:3): Value& value=*new VBool(a.is_defined());
1.295.2.6 paf 528: stack.push(value);
1.62 paf 529: break;
530: }
531: case OP_IN:
532: {
1.295.2.27.2. 2(paf 533:3): Value& a=stack.pop().value();
534:3): const String& path=a.as_string();
535:3): Value& value=*new VBool(request_info.uri && *request_info.uri && path.this_starts(request_info.uri));
1.295.2.6 paf 536: stack.push(value);
1.62 paf 537: break;
538: }
539: case OP_FEXISTS:
540: {
1.295.2.27.2. 2(paf 541:3): Value& a=stack.pop().value();
542:3): Value& value=*new VBool(file_readable(absolute(a.as_string())));
1.295.2.6 paf 543: stack.push(value);
1.148 paf 544: break;
545: }
546: case OP_DEXISTS:
547: {
1.295.2.27.2. 2(paf 548:3): Value& a=stack.pop().value();
549:3): Value& value=*new VBool(dir_readable(absolute(a.as_string())));
1.295.2.6 paf 550: stack.push(value);
1.62 paf 551: break;
552: }
1.55 paf 553:
554: // expression ops: binary
555: case OP_SUB:
1.53 paf 556: {
1.295.2.27.2. 2(paf 557:3): Value& b=stack.pop().value(); Value& a=stack.pop().value();
558:3): Value& value=*new VDouble(a.as_double() - b.as_double());
1.295.2.6 paf 559: stack.push(value);
1.53 paf 560: break;
561: }
1.55 paf 562: case OP_ADD:
1.53 paf 563: {
1.295.2.27.2. 2(paf 564:3): Value& b=stack.pop().value(); Value& a=stack.pop().value();
565:3): Value& value=*new VDouble(a.as_double() + b.as_double());
1.295.2.6 paf 566: stack.push(value);
1.53 paf 567: break;
568: }
1.49 paf 569: case OP_MUL:
570: {
1.295.2.27.2. 2(paf 571:3): Value& b=stack.pop().value(); Value& a=stack.pop().value();
572:3): Value& value=*new VDouble(a.as_double() * b.as_double());
1.295.2.6 paf 573: stack.push(value);
1.53 paf 574: break;
575: }
576: case OP_DIV:
577: {
1.295.2.27.2. 2(paf 578:3): Value& b=stack.pop().value(); Value& a=stack.pop().value();
1.170 parser 579:
1.295.2.27.2. 2(paf 580:3): double a_double=a.as_double();
581:3): double b_double=b.as_double();
1.170 parser 582:
1.171 parser 583: if(b_double == 0) {
1.295.2.27.2. 2(paf 584:3): //const String* problem_source=b.as_string();
1.223 paf 585: throw Exception("number.zerodivision",
1.295.2.27.2. (paf 586:): 0, //problem_source,
1.170 parser 587: "Division by zero");
1.171 parser 588: }
1.170 parser 589:
1.295.2.27.2. 2(paf 590:3): Value& value=*new VDouble(a_double / b_double);
1.295.2.6 paf 591: stack.push(value);
1.54 paf 592: break;
593: }
1.55 paf 594: case OP_MOD:
595: {
1.295.2.27.2. 2(paf 596:3): Value& b=stack.pop().value(); Value& a=stack.pop().value();
1.170 parser 597:
1.295.2.27.2. 2(paf 598:3): double a_double=a.as_double();
599:3): double b_double=b.as_double();
1.170 parser 600:
1.173 parser 601: if(b_double == 0) {
1.295.2.27.2. 2(paf 602:3): //const String* problem_source=b.as_string();
1.223 paf 603: throw Exception("number.zerodivision",
1.295.2.27.2. (paf 604:): 0, //problem_source,
1.170 parser 605: "Modulus by zero");
1.171 parser 606: }
1.170 parser 607:
1.295.2.27.2. 2(paf 608:3): Value& value=*new VDouble(fmod(a_double, b_double));
1.295.2.6 paf 609: stack.push(value);
1.201 paf 610: break;
611: }
612: case OP_INTDIV:
613: {
1.295.2.27.2. 2(paf 614:3): Value& b=stack.pop().value(); Value& a=stack.pop().value();
1.201 paf 615:
1.295.2.27.2. 2(paf 616:3): int a_int=a.as_int();
617:3): int b_int=b.as_int();
1.201 paf 618:
619: if(b_int == 0) {
1.295.2.27.2. 2(paf 620:3): //const String* problem_source=b.as_string();
1.223 paf 621: throw Exception("number.zerodivision",
1.295.2.27.2. (paf 622:): 0, //problem_source,
1.201 paf 623: "Division by zero");
624: }
625:
1.295.2.27.2. 2(paf 626:3): Value& value=*new VInt(a_int / b_int);
1.295.2.6 paf 627: stack.push(value);
1.55 paf 628: break;
629: }
1.274 paf 630: case OP_BIN_SL:
631: {
1.295.2.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: }
639: case OP_BIN_SR:
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.274 paf 646: break;
647: }
1.55 paf 648: case OP_BIN_AND:
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.54 paf 655: break;
656: }
1.55 paf 657: case OP_BIN_OR:
1.54 paf 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.55 paf 664: break;
665: }
1.56 paf 666: case OP_BIN_XOR:
667: {
1.295.2.27.2. 2(paf 668:3): Value& b=stack.pop().value(); Value& a=stack.pop().value();
669:3): Value& value=*new VInt(
670:3): a.as_int() ^
671:3): b.as_int());
1.295.2.6 paf 672: stack.push(value);
1.56 paf 673: break;
674: }
1.55 paf 675: case OP_LOG_AND:
676: {
1.295.2.27.2. 2(paf 677:3): ArrayOperation& local_ops=stack.pop().ops(); Value& a=stack.pop().value();
1.263 paf 678: bool result;
1.295.2.27.2. 2(paf 679:3): if(a.as_bool()) {
680:3): execute(local_ops);
681:3): Value& b=stack.pop().value();
682:3): result=b.as_bool();
1.209 paf 683: } else
1.263 paf 684: result=false;
1.295.2.27.2. 2(paf 685:3): Value& value=*new VBool(result);
1.295.2.6 paf 686: stack.push(value);
1.55 paf 687: break;
688: }
689: case OP_LOG_OR:
690: {
1.295.2.27.2. 2(paf 691:3): ArrayOperation& local_ops=stack.pop().ops(); Value& a=stack.pop().value();
1.263 paf 692: bool result;
1.295.2.27.2. 2(paf 693:3): if(a.as_bool())
1.263 paf 694: result=true;
1.209 paf 695: else {
1.295.2.27.2. 2(paf 696:3): execute(local_ops);
697:3): Value& b=stack.pop().value();
698:3): result=b.as_bool();
1.209 paf 699: }
1.295.2.27.2. 2(paf 700:3): Value& value=*new VBool(result);
1.295.2.6 paf 701: stack.push(value);
1.56 paf 702: break;
703: }
704: case OP_LOG_XOR:
705: {
1.295.2.27.2. 2(paf 706:3): Value& b=stack.pop().value(); Value& a=stack.pop().value();
707:3): Value& value=*new VBool(a.as_bool() ^ b.as_bool());
1.295.2.6 paf 708: stack.push(value);
1.55 paf 709: break;
710: }
711: case OP_NUM_LT:
712: {
1.295.2.27.2. 2(paf 713:3): Value& b=stack.pop().value(); Value& a=stack.pop().value();
714:3): double result=a.as_double() - b.as_double();
715:3): Value& value=*new VBool(result < 0.0);
1.295.2.6 paf 716: stack.push(value);
1.55 paf 717: break;
718: }
719: case OP_NUM_GT:
720: {
1.295.2.27.2. 2(paf 721:3): Value& b=stack.pop().value(); Value& a=stack.pop().value();
722:3): double result=a.as_double() - b.as_double();
723:3): Value& value=*new VBool(result > 0.0);
1.295.2.6 paf 724: stack.push(value);
1.55 paf 725: break;
726: }
727: case OP_NUM_LE:
728: {
1.295.2.27.2. 2(paf 729:3): Value& b=stack.pop().value(); Value& a=stack.pop().value();
730:3): double result=a.as_double() - b.as_double();
731:3): Value& value=*new VBool(result <= 0.0);
1.295.2.6 paf 732: stack.push(value);
1.55 paf 733: break;
734: }
735: case OP_NUM_GE:
736: {
1.295.2.27.2. 2(paf 737:3): Value& b=stack.pop().value(); Value& a=stack.pop().value();
738:3): double result=a.as_double() - b.as_double();
739:3): Value& value=*new VBool(result >= 0.0);
1.295.2.6 paf 740: stack.push(value);
1.55 paf 741: break;
742: }
743: case OP_NUM_EQ:
744: {
1.295.2.27.2. 2(paf 745:3): Value& b=stack.pop().value(); Value& a=stack.pop().value();
746:3): double result=a.as_double() - b.as_double();
747:3): Value& value=*new VBool(result == 0.0);
1.295.2.6 paf 748: stack.push(value);
1.55 paf 749: break;
750: }
751: case OP_NUM_NE:
752: {
1.295.2.27.2. 2(paf 753:3): Value& b=stack.pop().value(); Value& a=stack.pop().value();
754:3): double result=a.as_double() - b.as_double();
755:3): Value& value=*new VBool(result != 0.0);
1.295.2.6 paf 756: stack.push(value);
1.54 paf 757: break;
758: }
1.58 paf 759: case OP_STR_LT:
760: {
1.295.2.27.2. 2(paf 761:3): Value& b=stack.pop().value(); Value& a=stack.pop().value();
762:3): Value& value=*new VBool(a.as_string() < b.as_string());
1.295.2.6 paf 763: stack.push(value);
1.58 paf 764: break;
765: }
766: case OP_STR_GT:
767: {
1.295.2.27.2. 2(paf 768:3): Value& b=stack.pop().value(); Value& a=stack.pop().value();
769:3): Value& value=*new VBool(a.as_string() > b.as_string());
1.295.2.6 paf 770: stack.push(value);
1.58 paf 771: break;
772: }
1.55 paf 773: case OP_STR_LE:
1.58 paf 774: {
1.295.2.27.2. 2(paf 775:3): Value& b=stack.pop().value(); Value& a=stack.pop().value();
776:3): Value& value=*new VBool(a.as_string() <= b.as_string());
1.295.2.6 paf 777: stack.push(value);
1.58 paf 778: break;
779: }
1.55 paf 780: case OP_STR_GE:
1.54 paf 781: {
1.295.2.27.2. 2(paf 782:3): Value& b=stack.pop().value(); Value& a=stack.pop().value();
783:3): Value& value=*new VBool(a.as_string() >= b.as_string());
1.295.2.6 paf 784: stack.push(value);
1.58 paf 785: break;
786: }
787: case OP_STR_EQ:
788: {
1.295.2.27.2. 2(paf 789:3): Value& b=stack.pop().value(); Value& a=stack.pop().value();
790:3): Value& value=*new VBool(a.as_string() == b.as_string());
1.295.2.6 paf 791: stack.push(value);
1.58 paf 792: break;
793: }
794: case OP_STR_NE:
795: {
1.295.2.27.2. 2(paf 796:3): Value& b=stack.pop().value(); Value& a=stack.pop().value();
797:3): Value& value=*new VBool(a.as_string() != b.as_string());
1.295.2.6 paf 798: stack.push(value);
1.103 paf 799: break;
800: }
801: case OP_IS:
802: {
1.295.2.27.2. 2(paf 803:3): Value& b=stack.pop().value(); Value& a=stack.pop().value();
804:3): Value& value=*new VBool(a.is(b.as_string().cstr()));
1.295.2.6 paf 805: stack.push(value);
1.28 paf 806: break;
807: }
808:
1.11 paf 809: default:
1.295.2.27.2. (paf 810:): throw Exception(0,
811:): 0,
0(paf 812:3): "invalid opcode %d", opcode);
1.11 paf 813: }
814: }
1.295.2.27.2. 5(paf 815:3): } catch(...) {
816:3): // record it to stack trace
6(paf 817:3): //if(is_debug_junction)
9(paf 818:3): if(debug_name)
0(paf 819:3): exception_trace.push(Trace(debug_name, debug_origin));
5(paf 820:3): rethrow;
821:3): }
1.1 paf 822: }
1.17 paf 823:
1.292 paf 824: /**
825: @todo cache|prepare junctions
826: @bug ^superbase:method would dynamically call ^base:method if there is any
827: */
1.295.2.27.2. 5(paf 828:3): Value& Request::get_element(Value& ncontext, const String& name, bool can_call_operator) {
(paf 829:): Value* value=0;
1.249 paf 830: if(can_call_operator) {
1.295.2.27.2. (paf 831:): if(Method* method=main_class.get_method(name)) // looking operator of that name FIRST
832:): value=new VJunction(new Junction(
833:): main_class, method, 0,0,0, 0));
1.249 paf 834: }
835: if(!value) {
836: if(!wcontext->get_constructing() // not constructing
837: && wcontext->get_somebody_entered_some_class() ) // ^class:method
1.295.2.27.2. 2(paf 838:3): if(VStateless_class *called_class=ncontext.get_class())
1.249 paf 839: if(VStateless_class *read_class=rcontext->get_class())
840: if(read_class->derived_from(*called_class)) // current derived from called
1.295.2.27.2. (paf 841:): if(Value* base=get_self().base()) { // doing DYNAMIC call
1.295.2.27 paf 842: Temp_derived temp_derived(*base, 0); // temporarily prevent go-back-down virtual calls
1.290 paf 843: value=base->get_element(name, *base, false); // virtual-up lookup starting from parent
1.289 paf 844: goto value_ready;
1.249 paf 845: }
846: }
1.216 paf 847: if(!value)
1.295.2.27.2. 2(paf 848:3): value=ncontext.get_element(name, ncontext, false);
1.291 paf 849:
850: if(value && wcontext->get_constructing())
1.295.2.27.2. (paf 851:): if(Junction* junction=value->get_junction()) {
2(paf 852:3): if(junction->self.get_class()!=&ncontext)
1.291 paf 853: throw Exception("parser.runtime",
1.295.2.27.2. (paf 854:): &name,
1.295.2.20 paf 855: "constructor must be declared in class '%s'",
1.295.2.27.2. 2(paf 856:3): ncontext.get_class()->name_cstr());
1.291 paf 857: }
1.249 paf 858:
1.289 paf 859: value_ready:
1.284 paf 860: if(value)
1.295.2.27.2. (paf 861:): value=&process_to_value(*value); // process possible code-junction
1.284 paf 862: else
1.295.2.27.2. (paf 863:): value=new VVoid;
1.63 paf 864:
1.295.2.27.2. 2(paf 865:3): return *value;
1.34 paf 866: }
1.70 paf 867:
1.162 parser 868: /** @param intercept_string
1.116 paf 869: - true:
870: they want result=string value,
871: possible object result goes to wcontext
872: - false:
873: they want any result[string|object]
874: nothing goes to wcontext.
1.125 paf 875: used in @c (expression) params evaluation
1.224 paf 876:
877: using the fact it's either string_ or value_ result requested to speed up checkes
1.116 paf 878: */
1.295.2.27.2. (paf 879:): StringOrValue Request::process(Value& input_value, bool intercept_string) {
1.228 paf 880: StringOrValue result;
1.295.2.27.2. (paf 881:): Junction* junction=input_value.get_junction();
1.70 paf 882: if(junction && junction->code) { // is it a code-junction?
1.92 paf 883: // process it
1.157 parser 884: #ifdef DEBUG_EXECUTE
1.295.2.11 paf 885: debug_printf(sapi_info, "ja->\n");
1.157 parser 886: #endif
1.238 paf 887:
1.257 paf 888: if(!junction->method_frame)
1.240 paf 889: throw Exception("parser.runtime",
1.295.2.27.2. (paf 890:): 0,
1.240 paf 891: "junction used outside of context");
892:
1.257 paf 893: VMethodFrame *saved_method_frame=method_frame;
1.295.2.6 paf 894: Value* saved_rcontext=rcontext;
1.257 paf 895: WContext *saved_wcontext=wcontext;
1.240 paf 896:
1.257 paf 897: method_frame=junction->method_frame;
1.240 paf 898: rcontext=junction->rcontext;
1.225 paf 899:
1.109 paf 900: // for expression method params
901: // wcontext is set 0
902: // using the fact in decision "which wwrapper to use"
1.240 paf 903: bool using_code_frame=intercept_string && junction->wcontext;
1.109 paf 904: if(using_code_frame) {
1.71 paf 905: // almost plain wwrapper about junction wcontext,
906: // BUT intercepts string writes
1.295.2.27.2. (paf 907:): VCodeFrame local(*junction->wcontext, junction->wcontext);
1.225 paf 908: wcontext=&local;
1.207 paf 909:
1.225 paf 910: // execute it
1.295.2.27.2. (paf 911:): recoursion_checked_execute(/*0/*result_name* /, */ *junction->code);
1.226 paf 912:
1.71 paf 913: // CodeFrame soul:
914: // string writes were intercepted
915: // returning them as the result of getting code-junction
1.295.2.27.2. (paf 916:): result.set_string(*wcontext->get_string());
1.224 paf 917: } else {
1.225 paf 918: // plain wwrapper
1.295.2.27.2. (paf 919:): WWrapper local(0/*empty*/, wcontext);
1.225 paf 920: wcontext=&local;
921:
922: // execute it
1.295.2.27.2. (paf 923:): recoursion_checked_execute(/*0/*result_name* /, */ *junction->code);
1.226 paf 924:
1.228 paf 925: result=wcontext->result();
1.224 paf 926: }
1.70 paf 927:
1.257 paf 928: wcontext=saved_wcontext;
929: rcontext=saved_rcontext;
930: method_frame=saved_method_frame;
1.70 paf 931:
1.157 parser 932: #ifdef DEBUG_EXECUTE
1.295.2.11 paf 933: debug_printf(sapi_info, "<-ja returned");
1.157 parser 934: #endif
1.224 paf 935: } else {
1.228 paf 936: result.set_value(input_value);
1.224 paf 937: }
1.228 paf 938: return result;
1.85 paf 939: }
940:
1.295.2.27.2. (paf 941:): const String& Request::execute_method(VMethodFrame& amethod_frame, const Method& method) {
1.257 paf 942: VMethodFrame *saved_method_frame=method_frame;
1.295.2.6 paf 943: Value* saved_rcontext=rcontext;
1.257 paf 944: WContext *saved_wcontext=wcontext;
1.99 paf 945:
946: // initialize contexts
1.286 paf 947: rcontext=wcontext=method_frame=&amethod_frame;
1.99 paf 948:
949: // execute!
950: execute(*method.parser_code);
951:
952: // result
1.295.2.27.2. (paf 953:): const String& result=wcontext->result().as_string();
1.208 paf 954:
1.257 paf 955: wcontext=saved_wcontext;
956: rcontext=saved_rcontext;
957: method_frame=saved_method_frame;
1.242 paf 958:
959: // return
960: return result;
1.208 paf 961: }
962:
1.295.2.27.2. (paf 963:): const String* Request::execute_method(Value& aself,
0(paf 964:3): const Method& method, VString* optional_param,
965:3): bool do_return_string) {
1.257 paf 966: VMethodFrame *saved_method_frame=method_frame;
1.295.2.6 paf 967: Value* saved_rcontext=rcontext;
1.257 paf 968: WContext *saved_wcontext=wcontext;
1.208 paf 969:
1.295.2.27.2. (paf 970:): Junction local_junction(aself, &method, 0,0,0, 0);
0(paf 971:3): VMethodFrame local_frame(local_junction, method_frame/*caller*/);
1.242 paf 972: if(optional_param && local_frame.can_store_param()) {
1.295.2.27.2. (paf 973:): local_frame.store_param(*optional_param);
1.242 paf 974: local_frame.fill_unspecified_params();
975: }
1.285 paf 976: local_frame.set_self(aself);
1.257 paf 977: rcontext=wcontext=method_frame=&local_frame;
1.246 paf 978:
979: // prevent non-string writes for better error reporting
1.295.2.7 paf 980: if(do_return_string)
1.295.2.27.2. (paf 981:): wcontext->write(local_frame);
1.208 paf 982:
983: // execute!
984: execute(*method.parser_code);
985:
986: // result
1.295.2.27.2. (paf 987:): const String* result=0;
1.295.2.7 paf 988: if(do_return_string)
1.295.2.27.2. (paf 989:): result=&wcontext->result().as_string();
1.99 paf 990:
1.257 paf 991: wcontext=saved_wcontext;
992: rcontext=saved_rcontext;
993: method_frame=saved_method_frame;
1.295.2.7 paf 994:
995: return result;
1.242 paf 996: }
997:
1.295.2.16 paf 998: Request::Execute_nonvirtual_method_result
999: Request::execute_nonvirtual_method(VStateless_class& aclass,
1.295.2.27.2. (paf 1000:): const String& method_name, VString* optional_param,
1001:): bool do_return_string) {
1.295.2.16 paf 1002: Execute_nonvirtual_method_result result;
1003: result.method=aclass.get_method(method_name);
1004: if(result.method)
1.295.2.27.2. (paf 1005:): result.string=execute_method(aclass, *result.method, optional_param,
1.295.2.16 paf 1006: do_return_string);
1007: return result;
1.99 paf 1008: }
1009:
1.295.2.27.2. (paf 1010:): const String* Request::execute_virtual_method(Value& aself,
1011:): const String& method_name) {
1012:): if(Value* value=aself.get_element(method_name, aself, false))
1013:): if(Junction* junction=value->get_junction())
1.295.2.7 paf 1014: if(const Method *method=junction->method)
1.295.2.27.2. (paf 1015:): return execute_method(aself, *method, 0/*no params*/, true);
1.188 parser 1016:
1.295.2.27.2. (paf 1017:): return 0;
1.176 parser 1018: }
E-mail: