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