Annotation of parser3/src/main/execute.C, revision 1.295.2.27.2.8
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. (paf 8:): static const char* IDENT_EXECUTE_C="$Date: 2003/03/24 11:52:57 $";
1.1 paf 9:
1.152 paf 10: #include "pa_opcode.h"
1.8 paf 11: #include "pa_array.h"
1.11 paf 12: #include "pa_request.h"
1.15 paf 13: #include "pa_vstring.h"
1.22 paf 14: #include "pa_vhash.h"
1.167 parser 15: #include "pa_vvoid.h"
1.145 paf 16: #include "pa_vcode_frame.h"
17: #include "pa_vmethod_frame.h"
1.38 paf 18: #include "pa_vobject.h"
1.49 paf 19: #include "pa_vdouble.h"
1.54 paf 20: #include "pa_vbool.h"
1.94 paf 21: #include "pa_vtable.h"
1.132 paf 22: #include "pa_vfile.h"
1.144 paf 23: #include "pa_vimage.h"
1.194 parser 24: #include "pa_wwrapper.h"
1.1 paf 25:
1.295.2.17 paf 26: // #define DEBUG_EXECUTE
1.295.2.27 paf 27: #define PA_DEBUG_NO_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.11 paf 74: Operation& op=i.next();
1.1 paf 75:
1.86 paf 76: if(op.code==OP_VALUE || op.code==OP_STRING__WRITE) {
1.295.2.27.2. (paf 77:): Value* value=i.next().value;
1.295.2.11 paf 78: debug_printf(sapi_info,
1.133 paf 79: "%*s%s"
80: " \"%s\" %s",
81: level*4, "", opcode_name[op.code],
1.295.2.27.2. (paf 82:): value->get_string()->cstr().get(), value->type());
1.133 paf 83: continue;
1.15 paf 84: }
1.295.2.11 paf 85: debug_printf(sapi_info, "%*s%s", level*4, "", opcode_name[op.code]);
1.1 paf 86:
1.184 parser 87: switch(op.code) {
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.13 paf 96: if(ArrayOperation* local_ops=i.next().ops.get())
1.295.2.27.2. (paf 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.197 parser 106: // _asm int 3;
1.157 parser 107: #ifdef DEBUG_EXECUTE
1.295.2.11 paf 108: debug_printf(sapi_info, "source----------------------------\n");
1.295.2.27.2. (paf 109:): debug_dump(sapi_info, 0, ops);
1.295.2.11 paf 110: debug_printf(sapi_info, "execution-------------------------\n");
1.157 parser 111: #endif
1.295.2.6 paf 112: Array_iterator<Operation> i(ops);
1.295.2.27.2. (paf 113:): Value* value;
114:): Value* a; Value* b;
1.295.2.18 paf 115:
1.295.2.27.2. (paf 116:): const String* name;
117:): Value* ncontext;
1.295.2.18 paf 118:
1.295.2.27.2. (paf 119:): ArrayOperation* local_ops;
1.295.2.18 paf 120:
1.190 parser 121: while(i.has_next()) {
1.295.2.26 paf 122: if(get_interrupted()) {
123: set_interrupted(false);
1.293 paf 124: throw Exception("parser.interrupted",
1.295.2.27.2. (paf 125:): 0,
1.293 paf 126: "execution stopped");
1.295.2.26 paf 127: }
1.293 paf 128:
1.295.2.27.2. (paf 129:): Operation op=i.next();
1.157 parser 130: #ifdef DEBUG_EXECUTE
1.295.2.11 paf 131: debug_printf(sapi_info, "%d:%s", stack.top_index()+1, opcode_name[op.code]);
1.157 parser 132: #endif
1.11 paf 133:
1.23 paf 134: switch(op.code) {
1.51 paf 135: // param in next instruction
1.52 paf 136: case OP_VALUE:
1.32 paf 137: {
1.295.2.6 paf 138: value=i.next().value;
1.157 parser 139: #ifdef DEBUG_EXECUTE
1.295.2.27.2. (paf 140:): debug_printf(sapi_info, " \"%s\" %s", value->get_string()->cstr().get(), value->type());
1.157 parser 141: #endif
1.295.2.6 paf 142: stack.push(value);
1.32 paf 143: break;
144: }
1.66 paf 145: case OP_GET_CLASS:
1.38 paf 146: {
1.130 paf 147: // maybe they do ^class:method[] call, remember the fact
1.215 paf 148: wcontext->set_somebody_entered_some_class();
1.98 paf 149:
1.295.2.27.2. (paf 150:): name=stack.pop().string();
151:): value=classes().get(*name);
1.120 paf 152: if(!value)
1.223 paf 153: throw Exception("parser.runtime",
1.295.2.27.2. (paf 154:): name,
1.143 paf 155: "class is undefined");
1.66 paf 156:
1.295.2.6 paf 157: stack.push(value);
1.38 paf 158: break;
159: }
1.32 paf 160:
1.51 paf 161: // OP_WITH
1.187 parser 162: case OP_WITH_ROOT:
163: {
1.295.2.27.2. (paf 164:): stack.push(method_frame);
1.187 parser 165: break;
166: }
1.37 paf 167: case OP_WITH_SELF:
168: {
1.295.2.27.2. (paf 169:): stack.push(&get_self());
1.37 paf 170: break;
171: }
1.15 paf 172: case OP_WITH_READ:
173: {
1.295.2.27.2. (paf 174:): stack.push(rcontext);
1.20 paf 175: break;
176: }
1.37 paf 177: case OP_WITH_WRITE:
1.20 paf 178: {
1.287 paf 179: if(wcontext==method_frame)
180: throw Exception("parser.runtime",
1.295.2.27.2. (paf 181:): 0,
1.287 paf 182: "$.name outside of $name[...]");
183:
1.295.2.27.2. (paf 184:): stack.push(wcontext);
1.20 paf 185: break;
186: }
1.37 paf 187:
1.51 paf 188: // OTHER ACTIONS BUT WITHs
1.80 paf 189: case OP_CONSTRUCT_VALUE:
1.20 paf 190: {
1.295.2.6 paf 191: value=stack.pop().value;
1.295.2.27.2. (paf 192:): name=stack.pop().string();
1.295.2.24 paf 193: ncontext=stack.pop().value;
1.295.2.27.2. (paf 194:): ncontext->put_element(*name, value, false);
1.213 paf 195: break;
196: }
1.80 paf 197: case OP_CONSTRUCT_EXPR:
198: {
1.219 paf 199: // see OP_PREPARE_TO_EXPRESSION
200: wcontext->set_in_expression(false);
201:
1.295.2.6 paf 202: value=stack.pop().value;
1.295.2.27.2. (paf 203:): name=stack.pop().string();
1.295.2.24 paf 204: ncontext=stack.pop().value;
1.295.2.27.2. (paf 205:): ncontext->put_element(*name, value->as_expr_result(), false);
1.80 paf 206: break;
207: }
1.182 parser 208: case OP_CURLY_CODE__CONSTRUCT:
209: {
1.295.2.6 paf 210: local_ops=i.next().ops;
1.182 parser 211: #ifdef DEBUG_EXECUTE
1.295.2.11 paf 212: debug_printf(sapi_info, " (%d)\n", local_ops->count());
1.295.2.27.2. (paf 213:): debug_dump(sapi_info, 1, *local_ops);
1.182 parser 214: #endif
1.295.2.27.2. (paf 215:): value=new VJunction(new Junction(
1.295.2.7 paf 216: get_self(), 0,
1.257 paf 217: method_frame,
1.240 paf 218: rcontext,
219: wcontext,
1.295.2.27.2. (paf 220:): local_ops));
1.238 paf 221:
1.295.2.27.2. (paf 222:): name=stack.pop().string();
1.295.2.24 paf 223: ncontext=stack.pop().value;
1.295.2.27.2. (paf 224:): ncontext->put_element(*name, value, false);
1.182 parser 225: break;
226: }
1.209 paf 227: case OP_NESTED_CODE:
228: {
1.295.2.6 paf 229: local_ops=i.next().ops;
1.209 paf 230: #ifdef DEBUG_EXECUTE
1.295.2.11 paf 231: debug_printf(sapi_info, " (%d)\n", local_ops->count());
1.295.2.27.2. (paf 232:): debug_dump(sapi_info, 1, *local_ops);
1.209 paf 233: #endif
1.295.2.6 paf 234: stack.push(local_ops);
1.209 paf 235: break;
236: }
1.108 paf 237: case OP_WRITE_VALUE:
1.13 paf 238: {
1.295.2.6 paf 239: value=stack.pop().value;
1.295.2.27.2. (paf 240:): write_assign_lang(*value);
1.86 paf 241: break;
242: }
1.108 paf 243: case OP_WRITE_EXPR_RESULT:
244: {
1.295.2.6 paf 245: value=stack.pop().value;
1.295.2.27.2. (paf 246:): write_no_lang(*value->as_expr_result());
1.230 paf 247:
248: // must be after write(result) and
1.219 paf 249: // see OP_PREPARE_TO_EXPRESSION
250: wcontext->set_in_expression(false);
1.108 paf 251: break;
252: }
1.86 paf 253: case OP_STRING__WRITE:
254: {
1.295.2.7 paf 255: value=i.next().value;
1.157 parser 256: #ifdef DEBUG_EXECUTE
1.295.2.27.2. (paf 257:): debug_printf(sapi_info, " \"%s\"", value->get_string()->cstr().get());
1.157 parser 258: #endif
1.295.2.27.2. (paf 259:): write_no_lang(*value->get_string());
1.13 paf 260: break;
1.14 paf 261: }
1.13 paf 262:
1.215 paf 263: case OP_GET_ELEMENT_OR_OPERATOR:
264: {
1.295.2.27.2. (paf 265:): value=get_element(true);
1.295.2.6 paf 266: stack.push(value);
1.215 paf 267: break;
268: }
1.15 paf 269: case OP_GET_ELEMENT:
1.11 paf 270: {
1.295.2.27.2. (paf 271:): value=get_element(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. (paf 278:): value=get_element(false);
279:): 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.7 paf 286: 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:
294: execute(*local_ops);
295:
1.295.2.27.2. (paf 296:): value=&wcontext->result().as_value();
1.257 paf 297: flang=saved_lang;
298: wcontext=saved_wcontext;
1.295.2.6 paf 299: stack.push(value);
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.7 paf 305: 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:
311: execute(*local_ops);
312:
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;
1.295.2.7 paf 319:
1.257 paf 320: wcontext=saved_wcontext;
1.295.2.6 paf 321: stack.push(value);
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.6 paf 328: value=stack.pop().value;
1.295.2.27.2. (paf 329:): 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. (paf 331:): 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.7 paf 338: local_ops=i.next().ops;
1.295.2.27.2. (paf 339:): VMethodFrame *frame=stack.top_value().method_frame;
1.237 paf 340: #ifdef DEBUG_EXECUTE
1.295.2.11 paf 341: debug_printf(sapi_info, " (%d)\n", local_ops->count());
1.295.2.27.2. (paf 342:): 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. (paf 351:): 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,
355: op.code==OP_EXPR_CODE__STORE_PARAM?0:wcontext,
1.295.2.27.2. (paf 356:): 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. (paf 359:): 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.7 paf 378: local_ops=i.next().ops;
1.157 parser 379: #ifdef DEBUG_EXECUTE
1.295.2.13 paf 380: debug_printf(sapi_info, " (%d)\n", local_ops?local_ops->count():0);
381: if(local_ops)
1.295.2.27.2. (paf 382:): debug_dump(sapi_info, 1, *local_ops);
1.237 paf 383:
1.295.2.11 paf 384: debug_printf(sapi_info, "->\n");
1.157 parser 385: #endif
1.295.2.6 paf 386: value=stack.pop().value;
1.237 paf 387:
1.295.2.27.2. (paf 388:): Junction* junction=value->get_junction();
1.237 paf 389: if(!junction)
390: throw Exception("parser.runtime",
1.295.2.27.2. (paf 391:): 0,
1.237 paf 392: "(%s) not a method or junction, can not call it",
1.276 paf 393: value->type());
1.282 paf 394: /* no check needed, code compiled the way that that's impossible
1.276 paf 395: // check:
396: // that this is method-junction, not a code-junction
1.278 paf 397: // [disabling these contstructions:]
398: // $junction{code}
399: // ^junction[]
1.276 paf 400: if(!junction->method)
401: throw Exception("parser.runtime",
1.295.2.27.2. (paf 402:):
1.276 paf 403: "(%s) is code junction, can not call it",
1.237 paf 404: value->type());
1.282 paf 405: */
1.237 paf 406:
1.295.2.27.2. (paf 407:): VMethodFrame frame(*junction, method_frame/*caller*/);
1.237 paf 408: if(local_ops){ // store param code goes here
1.295.2.6 paf 409: stack.push(&frame); // argument for *STORE_PARAM ops
1.237 paf 410: execute(*local_ops);
1.295.2.6 paf 411: stack.pop().value;
1.237 paf 412: }
413: frame.fill_unspecified_params();
1.257 paf 414: VMethodFrame *saved_method_frame=method_frame;
1.295.2.6 paf 415: Value* saved_rcontext=rcontext;
1.257 paf 416: WContext *saved_wcontext=wcontext;
1.45 paf 417:
1.295.2.27.2. (paf 418:): VStateless_class& called_class=*frame.junction.self.get_class();
419:): Value* new_self=&get_self();
1.200 paf 420: if(wcontext->get_constructing()) {
421: wcontext->set_constructing(false);
1.237 paf 422: if(frame.junction.method->call_type!=Method::CT_STATIC) {
1.138 paf 423: // this is a constructor call
1.185 parser 424:
1.295.2.27.2. (paf 425:): if(Value* value=called_class.create_new_value()) {
1.204 paf 426: // some stateless_class creatable derivates
1.285 paf 427: new_self=value;
1.204 paf 428: } else
1.223 paf 429: throw Exception("parser.runtime",
1.295.2.27.2. (paf 430:): 0, //&frame.name(),
1.204 paf 431: "is not a constructor, system class '%s' can be constructed only implicitly",
1.295.2.27.2. (paf 432:): called_class.name().cstr());
1.204 paf 433:
1.295.2.27.2. (paf 434:): frame.write(*new_self,
435:): String::L_CLEAN // not used, always an object, not string
1.83 paf 436: );
1.185 parser 437: } else
1.223 paf 438: throw Exception("parser.runtime",
1.295.2.27.2. (paf 439:): 0, //&frame.name(),
1.185 parser 440: "method is static and can not be used as constructor");
1.249 paf 441: } else
1.295.2.27.2. (paf 442:): new_self=&frame.junction.self;
1.75 paf 443:
1.295.2.27.2. (paf 444:): frame.set_self(*new_self);
1.219 paf 445:
446: // see OP_PREPARE_TO_EXPRESSION
1.237 paf 447: frame.set_in_expression(wcontext->get_in_expression());
1.219 paf 448:
1.237 paf 449: rcontext=wcontext=&frame;
1.47 paf 450: {
1.237 paf 451: const Method& method=*frame.junction.method;
1.134 paf 452: Method::Call_type call_type=
1.295.2.27.2. (paf 453:): &called_class==new_self ? Method::CT_STATIC : Method::CT_DYNAMIC;
1.134 paf 454: if(
455: method.call_type==Method::CT_ANY ||
1.197 parser 456: method.call_type==call_type) { // allowed call type?
1.295.2.27 paf 457: #ifndef PA_DEBUG_NO_STACK_TRACE
1.197 parser 458: try {
1.295.2.25 paf 459: #endif
1.283 paf 460: method_frame=&frame;
1.197 parser 461: if(method.native_code) { // native code?
462: method.check_actual_numbered_params(
1.295.2.27.2. (paf 463:): frame.junction.self,
464:): /*frame.name(), */frame.numbered_params());
1.197 parser 465: method.native_code(
466: *this,
1.295.2.27.2. (paf 467:): /*frame.name(), */frame.numbered_params()); // execute it
1.283 paf 468: } else // parser code, execute it
1.295.2.27.2. (paf 469:): recoursion_checked_execute(/*frame.name(), */*method.parser_code);
1.295.2.27 paf 470: #ifndef PA_DEBUG_NO_STACK_TRACE
1.197 parser 471: } catch(...) {
472: // record it to stack trace
1.295.2.7 paf 473: exception_trace.push(frame.name());
1.295.2.5 paf 474: rethrow;
1.159 parser 475: }
1.295.2.25 paf 476: #endif
1.197 parser 477: } else
1.223 paf 478: throw Exception("parser.runtime",
1.295.2.27.2. (paf 479:): 0, //&frame.name(),
1.134 paf 480: "is not allowed to be called %s",
481: call_type==Method::CT_STATIC?"statically":"dynamically");
482:
1.47 paf 483: }
1.232 paf 484: StringOrValue result=wcontext->result();
1.45 paf 485:
1.257 paf 486: wcontext=saved_wcontext;
487: rcontext=saved_rcontext;
488: method_frame=saved_method_frame;
1.213 paf 489:
1.232 paf 490: if(op.code==OP_CALL__WRITE) {
1.295.2.27.2. (paf 491:): write_assign_lang(result);
1.232 paf 492: } else { // OP_CALL
1.295.2.27.2. (paf 493:): stack.push(&result.as_value());
1.232 paf 494: }
495:
1.157 parser 496: #ifdef DEBUG_EXECUTE
1.295.2.11 paf 497: debug_printf(sapi_info, "<-returned");
1.157 parser 498: #endif
1.49 paf 499: break;
500: }
501:
1.55 paf 502: // expression ops: unary
503: case OP_NEG:
504: {
1.295.2.7 paf 505: a=stack.pop().value;
1.295.2.27.2. (paf 506:): 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.7 paf 512: a=stack.pop().value;
1.295.2.27.2. (paf 513:): 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.7 paf 519: a=stack.pop().value;
1.295.2.27.2. (paf 520:): 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.7 paf 526: a=stack.pop().value;
1.295.2.27.2. (paf 527:): 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.7 paf 533: a=stack.pop().value;
1.295.2.27.2. (paf 534:): const String& path=a->as_string();
535:): value=new VBool(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.7 paf 541: a=stack.pop().value;
1.295.2.27.2. (paf 542:): 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.7 paf 548: a=stack.pop().value;
1.295.2.27.2. (paf 549:): 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.6 paf 557: b=stack.pop().value; a=stack.pop().value;
1.295.2.27.2. (paf 558:): 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.6 paf 564: b=stack.pop().value; a=stack.pop().value;
1.295.2.27.2. (paf 565:): 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.6 paf 571: b=stack.pop().value; a=stack.pop().value;
1.295.2.27.2. (paf 572:): 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.6 paf 578: b=stack.pop().value; a=stack.pop().value;
1.170 parser 579:
580: double a_double=a->as_double();
581: double b_double=b->as_double();
582:
1.171 parser 583: if(b_double == 0) {
1.295.2.27.2. (paf 584:): //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. (paf 590:): 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.6 paf 596: b=stack.pop().value; a=stack.pop().value;
1.170 parser 597:
1.173 parser 598: double a_double=a->as_double();
599: double b_double=b->as_double();
1.170 parser 600:
1.173 parser 601: if(b_double == 0) {
1.295.2.27.2. (paf 602:): //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. (paf 608:): 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.6 paf 614: b=stack.pop().value; a=stack.pop().value;
1.201 paf 615:
616: int a_int=a->as_int();
617: int b_int=b->as_int();
618:
619: if(b_int == 0) {
1.295.2.27.2. (paf 620:): //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. (paf 626:): 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.6 paf 632: b=stack.pop().value; a=stack.pop().value;
1.295.2.27.2. (paf 633:): value=new VInt(
1.274 paf 634: a->as_int() <<
1.295.2.27.2. (paf 635:): 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.6 paf 641: b=stack.pop().value; a=stack.pop().value;
1.295.2.27.2. (paf 642:): value=new VInt(
1.274 paf 643: a->as_int() >>
1.295.2.27.2. (paf 644:): 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.6 paf 650: b=stack.pop().value; a=stack.pop().value;
1.295.2.27.2. (paf 651:): value=new VInt(
1.155 parser 652: a->as_int() &
1.295.2.27.2. (paf 653:): 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.6 paf 659: b=stack.pop().value; a=stack.pop().value;
1.295.2.27.2. (paf 660:): value=new VInt(
1.155 parser 661: a->as_int() |
1.295.2.27.2. (paf 662:): 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.6 paf 668: b=stack.pop().value; a=stack.pop().value;
1.295.2.27.2. (paf 669:): value=new VInt(
1.155 parser 670: a->as_int() ^
1.295.2.27.2. (paf 671:): 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.7 paf 677: local_ops=stack.pop().ops; a=stack.pop().value;
1.263 paf 678: bool result;
1.209 paf 679: if(a->as_bool()) {
1.295.2.7 paf 680: execute(*local_ops);
1.295.2.6 paf 681: b=stack.pop().value;
1.263 paf 682: result=b->as_bool();
1.209 paf 683: } else
1.263 paf 684: result=false;
1.295.2.27.2. (paf 685:): 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.7 paf 691: local_ops=stack.pop().ops; a=stack.pop().value;
1.263 paf 692: bool result;
1.209 paf 693: if(a->as_bool())
1.263 paf 694: result=true;
1.209 paf 695: else {
1.295.2.7 paf 696: execute(*local_ops);
1.295.2.6 paf 697: b=stack.pop().value;
1.263 paf 698: result=b->as_bool();
1.209 paf 699: }
1.295.2.27.2. (paf 700:): 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.6 paf 706: b=stack.pop().value; a=stack.pop().value;
1.295.2.27.2. (paf 707:): 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.6 paf 713: b=stack.pop().value; a=stack.pop().value;
1.263 paf 714: double result=a->as_double() - b->as_double();
1.295.2.27.2. (paf 715:): 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.6 paf 721: b=stack.pop().value; a=stack.pop().value;
1.263 paf 722: double result=a->as_double() - b->as_double();
1.295.2.27.2. (paf 723:): 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.6 paf 729: b=stack.pop().value; a=stack.pop().value;
1.263 paf 730: double result=a->as_double() - b->as_double();
1.295.2.27.2. (paf 731:): 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.6 paf 737: b=stack.pop().value; a=stack.pop().value;
1.263 paf 738: double result=a->as_double() - b->as_double();
1.295.2.27.2. (paf 739:): 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.6 paf 745: b=stack.pop().value; a=stack.pop().value;
1.263 paf 746: double result=a->as_double() - b->as_double();
1.295.2.27.2. (paf 747:): 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.6 paf 753: b=stack.pop().value; a=stack.pop().value;
1.263 paf 754: double result=a->as_double() - b->as_double();
1.295.2.27.2. (paf 755:): 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.6 paf 761: b=stack.pop().value; a=stack.pop().value;
1.295.2.27.2. (paf 762:): 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.6 paf 768: b=stack.pop().value; a=stack.pop().value;
1.295.2.27.2. (paf 769:): 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.6 paf 775: b=stack.pop().value; a=stack.pop().value;
1.295.2.27.2. (paf 776:): 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.6 paf 782: b=stack.pop().value; a=stack.pop().value;
1.295.2.27.2. (paf 783:): 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.6 paf 789: b=stack.pop().value; a=stack.pop().value;
1.295.2.27.2. (paf 790:): 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.6 paf 796: b=stack.pop().value; a=stack.pop().value;
1.295.2.27.2. (paf 797:): 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.6 paf 803: b=stack.pop().value; a=stack.pop().value;
1.295.2.27.2. (paf 804:): 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,
1.139 paf 812: "invalid opcode %d", op.code);
1.11 paf 813: }
814: }
1.1 paf 815: }
1.17 paf 816:
1.292 paf 817: /**
818: @todo cache|prepare junctions
819: @bug ^superbase:method would dynamically call ^base:method if there is any
820: */
1.295.2.27.2. (paf 821:): Value* Request::get_element(bool can_call_operator) {
822:): const String& name=*stack.pop().string(); //remember_name=name;
823:): Value* ncontext=stack.pop().value;
824:): Value* value=0;
1.249 paf 825: if(can_call_operator) {
1.295.2.27.2. (paf 826:): if(Method* method=main_class.get_method(name)) // looking operator of that name FIRST
827:): value=new VJunction(new Junction(
828:): main_class, method, 0,0,0, 0));
1.249 paf 829: }
830: if(!value) {
831: if(!wcontext->get_constructing() // not constructing
832: && wcontext->get_somebody_entered_some_class() ) // ^class:method
833: if(VStateless_class *called_class=ncontext->get_class())
834: if(VStateless_class *read_class=rcontext->get_class())
835: if(read_class->derived_from(*called_class)) // current derived from called
1.295.2.27.2. (paf 836:): if(Value* base=get_self().base()) { // doing DYNAMIC call
1.295.2.27 paf 837: Temp_derived temp_derived(*base, 0); // temporarily prevent go-back-down virtual calls
1.290 paf 838: value=base->get_element(name, *base, false); // virtual-up lookup starting from parent
1.289 paf 839: goto value_ready;
1.249 paf 840: }
841: }
1.216 paf 842: if(!value)
1.290 paf 843: value=ncontext->get_element(name, *ncontext, false);
1.291 paf 844:
845: if(value && wcontext->get_constructing())
1.295.2.27.2. (paf 846:): if(Junction* junction=value->get_junction()) {
847:): if(junction->self.get_class()!=ncontext)
1.291 paf 848: throw Exception("parser.runtime",
1.295.2.27.2. (paf 849:): &name,
1.295.2.20 paf 850: "constructor must be declared in class '%s'",
851: ncontext->get_class()->name_cstr());
1.291 paf 852: }
1.249 paf 853:
1.289 paf 854: value_ready:
1.284 paf 855: if(value)
1.295.2.27.2. (paf 856:): value=&process_to_value(*value); // process possible code-junction
1.284 paf 857: else
1.295.2.27.2. (paf 858:): value=new VVoid;
1.63 paf 859:
1.17 paf 860: return value;
1.34 paf 861: }
1.70 paf 862:
1.162 parser 863: /** @param intercept_string
1.116 paf 864: - true:
865: they want result=string value,
866: possible object result goes to wcontext
867: - false:
868: they want any result[string|object]
869: nothing goes to wcontext.
1.125 paf 870: used in @c (expression) params evaluation
1.224 paf 871:
872: using the fact it's either string_ or value_ result requested to speed up checkes
1.116 paf 873: */
1.295.2.27.2. (paf 874:): StringOrValue Request::process(Value& input_value, bool intercept_string) {
1.228 paf 875: StringOrValue result;
1.295.2.27.2. (paf 876:): Junction* junction=input_value.get_junction();
1.70 paf 877: if(junction && junction->code) { // is it a code-junction?
1.92 paf 878: // process it
1.157 parser 879: #ifdef DEBUG_EXECUTE
1.295.2.11 paf 880: debug_printf(sapi_info, "ja->\n");
1.157 parser 881: #endif
1.238 paf 882:
1.257 paf 883: if(!junction->method_frame)
1.240 paf 884: throw Exception("parser.runtime",
1.295.2.27.2. (paf 885:): 0,
1.240 paf 886: "junction used outside of context");
887:
1.257 paf 888: VMethodFrame *saved_method_frame=method_frame;
1.295.2.6 paf 889: Value* saved_rcontext=rcontext;
1.257 paf 890: WContext *saved_wcontext=wcontext;
1.240 paf 891:
1.257 paf 892: method_frame=junction->method_frame;
1.240 paf 893: rcontext=junction->rcontext;
1.225 paf 894:
1.109 paf 895: // for expression method params
896: // wcontext is set 0
897: // using the fact in decision "which wwrapper to use"
1.240 paf 898: bool using_code_frame=intercept_string && junction->wcontext;
1.109 paf 899: if(using_code_frame) {
1.71 paf 900: // almost plain wwrapper about junction wcontext,
901: // BUT intercepts string writes
1.295.2.27.2. (paf 902:): VCodeFrame local(*junction->wcontext, junction->wcontext);
1.225 paf 903: wcontext=&local;
1.207 paf 904:
1.225 paf 905: // execute it
1.295.2.27.2. (paf 906:): recoursion_checked_execute(/*0/*result_name* /, */ *junction->code);
1.226 paf 907:
1.71 paf 908: // CodeFrame soul:
909: // string writes were intercepted
910: // returning them as the result of getting code-junction
1.295.2.27.2. (paf 911:): result.set_string(*wcontext->get_string());
1.224 paf 912: } else {
1.225 paf 913: // plain wwrapper
1.295.2.27.2. (paf 914:): WWrapper local(0/*empty*/, wcontext);
1.225 paf 915: wcontext=&local;
916:
917: // execute it
1.295.2.27.2. (paf 918:): recoursion_checked_execute(/*0/*result_name* /, */ *junction->code);
1.226 paf 919:
1.228 paf 920: result=wcontext->result();
1.224 paf 921: }
1.70 paf 922:
1.257 paf 923: wcontext=saved_wcontext;
924: rcontext=saved_rcontext;
925: method_frame=saved_method_frame;
1.70 paf 926:
1.157 parser 927: #ifdef DEBUG_EXECUTE
1.295.2.11 paf 928: debug_printf(sapi_info, "<-ja returned");
1.157 parser 929: #endif
1.224 paf 930: } else {
1.228 paf 931: result.set_value(input_value);
1.224 paf 932: }
1.228 paf 933: return result;
1.85 paf 934: }
935:
1.295.2.27.2. (paf 936:): const String& Request::execute_method(VMethodFrame& amethod_frame, const Method& method) {
1.257 paf 937: VMethodFrame *saved_method_frame=method_frame;
1.295.2.6 paf 938: Value* saved_rcontext=rcontext;
1.257 paf 939: WContext *saved_wcontext=wcontext;
1.99 paf 940:
941: // initialize contexts
1.286 paf 942: rcontext=wcontext=method_frame=&amethod_frame;
1.99 paf 943:
944: // execute!
945: execute(*method.parser_code);
946:
947: // result
1.295.2.27.2. (paf 948:): const String& result=wcontext->result().as_string();
1.208 paf 949:
1.257 paf 950: wcontext=saved_wcontext;
951: rcontext=saved_rcontext;
952: method_frame=saved_method_frame;
1.242 paf 953:
954: // return
955: return result;
1.208 paf 956: }
957:
1.295.2.27.2. (paf 958:): const String* Request::execute_method(Value& aself,
959:): const Method& method, VString* optional_param,
1.295.2.7 paf 960: bool do_return_string) {
1.257 paf 961: VMethodFrame *saved_method_frame=method_frame;
1.295.2.6 paf 962: Value* saved_rcontext=rcontext;
1.257 paf 963: WContext *saved_wcontext=wcontext;
1.208 paf 964:
1.295.2.27.2. (paf 965:): Junction local_junction(aself, &method, 0,0,0, 0);
966:): VMethodFrame local_frame(/*method.name, */local_junction, method_frame/*caller*/);
1.242 paf 967: if(optional_param && local_frame.can_store_param()) {
1.295.2.27.2. (paf 968:): local_frame.store_param(*optional_param);
1.242 paf 969: local_frame.fill_unspecified_params();
970: }
1.285 paf 971: local_frame.set_self(aself);
1.257 paf 972: rcontext=wcontext=method_frame=&local_frame;
1.246 paf 973:
974: // prevent non-string writes for better error reporting
1.295.2.7 paf 975: if(do_return_string)
1.295.2.27.2. (paf 976:): wcontext->write(local_frame);
1.208 paf 977:
978: // execute!
979: execute(*method.parser_code);
980:
981: // result
1.295.2.27.2. (paf 982:): const String* result=0;
1.295.2.7 paf 983: if(do_return_string)
1.295.2.27.2. (paf 984:): result=&wcontext->result().as_string();
1.99 paf 985:
1.257 paf 986: wcontext=saved_wcontext;
987: rcontext=saved_rcontext;
988: method_frame=saved_method_frame;
1.295.2.7 paf 989:
990: return result;
1.242 paf 991: }
992:
1.295.2.16 paf 993: Request::Execute_nonvirtual_method_result
994: Request::execute_nonvirtual_method(VStateless_class& aclass,
1.295.2.27.2. (paf 995:): const String& method_name, VString* optional_param,
996:): bool do_return_string) {
1.295.2.16 paf 997: Execute_nonvirtual_method_result result;
998: result.method=aclass.get_method(method_name);
999: if(result.method)
1.295.2.27.2. (paf 1000:): result.string=execute_method(aclass, *result.method, optional_param,
1.295.2.16 paf 1001: do_return_string);
1002: return result;
1.99 paf 1003: }
1004:
1.295.2.27.2. (paf 1005:): const String* Request::execute_virtual_method(Value& aself,
1006:): const String& method_name) {
1007:): if(Value* value=aself.get_element(method_name, aself, false))
1008:): if(Junction* junction=value->get_junction())
1.295.2.7 paf 1009: if(const Method *method=junction->method)
1.295.2.27.2. (paf 1010:): return execute_method(aself, *method, 0/*no params*/, true);
1.188 parser 1011:
1.295.2.27.2. (paf 1012:): return 0;
1.176 parser 1013: }
E-mail: