Annotation of parser3/src/main/execute.C, revision 1.298

1.117     paf         1: /** @file
1.118     paf         2:        Parser: executor part of request class.
                      3: 
1.297     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.298   ! paf         8: static const char* IDENT_EXECUTE_C="$Date: 2003/07/24 11:31:23 $";
1.1       paf         9: 
1.152     paf        10: #include "pa_opcode.h"
1.8       paf        11: #include "pa_array.h" 
1.11      paf        12: #include "pa_request.h"
1.15      paf        13: #include "pa_vstring.h"
1.22      paf        14: #include "pa_vhash.h"
1.167     parser     15: #include "pa_vvoid.h"
1.145     paf        16: #include "pa_vcode_frame.h"
                     17: #include "pa_vmethod_frame.h"
1.38      paf        18: #include "pa_vobject.h"
1.49      paf        19: #include "pa_vdouble.h"
1.54      paf        20: #include "pa_vbool.h"
1.94      paf        21: #include "pa_vtable.h"
1.132     paf        22: #include "pa_vfile.h"
1.144     paf        23: #include "pa_vimage.h"
1.194     parser     24: #include "pa_wwrapper.h"
1.1       paf        25: 
1.233     paf        26: //#define DEBUG_EXECUTE
1.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.297     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.297     paf        60:        SAPI::log(sapi_info, "%s", buf);
1.107     paf        61: }
                     62: 
1.297     paf        63: void debug_printf(SAPI_Info& sapi_info, const char* fmt, ...) {
1.107     paf        64:     va_list args;
1.297     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.297     paf        70: void debug_dump(SAPI_Info& sapi_info, int level, ArrayOperation& ops) {
                     71:        Array_iterator<Operation> i(ops);
1.190     parser     72:        while(i.has_next()) {
1.297     paf        73:                OPCODE opcode=i.next().code;
1.1       paf        74: 
1.297     paf        75:                if(opcode==OP_VALUE || opcode==OP_STRING__WRITE) {
                     76:                        Operation::Origin origin=i.next().origin;
                     77:                        Value& value=*i.next().value;
                     78:                        debug_printf(sapi_info, 
1.133     paf        79:                                "%*s%s"
                     80:                                " \"%s\" %s", 
1.297     paf        81:                                level*4, "", opcode_name[opcode],
                     82:                                value.get_string()->cstr(), value.type());
1.133     paf        83:                        continue;
1.15      paf        84:                }
1.297     paf        85:                debug_printf(sapi_info, "%*s%s", level*4, "", opcode_name[opcode]);
1.1       paf        86: 
1.297     paf        87:                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.297     paf        95:                case OP_CALL__WRITE:
                     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.297     paf       103: // Request
                    104: 
                    105: void Request::execute(ArrayOperation& ops) {
                    106:        register Stack<StackItem>& stack=this->stack; // helps a lot on MSVC: 'esi'
1.159     parser    107: 
1.297     paf       108:        const String* debug_name=0;  Operation::Origin debug_origin;  //bool is_debug_junction=false;
                    109:        try{
1.157     parser    110: #ifdef DEBUG_EXECUTE
1.297     paf       111:        debug_printf(sapi_info, "source----------------------------\n");
                    112:        debug_dump(sapi_info, 0, ops);
                    113:        debug_printf(sapi_info, "execution-------------------------\n");
1.157     parser    114: #endif
1.297     paf       115:        for(Array_iterator<Operation> i(ops); i.has_next(); ) {
                    116:                if(get_interrupted()) {
                    117:                        set_interrupted(false);
1.293     paf       118:                        throw Exception("parser.interrupted",
                    119:                                0,
                    120:                                "execution stopped");
1.297     paf       121:                }
                    122:                OPCODE opcode=i.next().code;
1.293     paf       123: 
1.157     parser    124: #ifdef DEBUG_EXECUTE
1.297     paf       125:                debug_printf(sapi_info, "%d:%s", stack.top_index()+1, opcode_name[opcode]);
1.157     parser    126: #endif
1.11      paf       127: 
1.297     paf       128:                switch(opcode) {
1.51      paf       129:                // param in next instruction
1.52      paf       130:                case OP_VALUE:
1.32      paf       131:                        {
1.297     paf       132:                                debug_origin=i.next().origin;
                    133:                                Value& value=*i.next().value;
1.157     parser    134: #ifdef DEBUG_EXECUTE
1.297     paf       135:                                debug_printf(sapi_info, " \"%s\" %s", value.get_string()->cstr(), value.type());
1.157     parser    136: #endif
1.297     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.297     paf       145:                                const String& name=stack.pop().string();
                    146:                                Value* value=classes().get(name);
1.120     paf       147:                                if(!value) 
1.223     paf       148:                                        throw Exception("parser.runtime",
1.66      paf       149:                                                &name,
1.143     paf       150:                                                "class is undefined"); 
1.66      paf       151: 
1.297     paf       152:                                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.297     paf       159:                                stack.push(*method_frame);
1.187     parser    160:                                break;
                    161:                        }
1.37      paf       162:                case OP_WITH_SELF: 
                    163:                        {
1.297     paf       164:                                stack.push(get_self());
1.37      paf       165:                                break;
                    166:                        }
1.15      paf       167:                case OP_WITH_READ: 
                    168:                        {
1.297     paf       169:                                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",
                    176:                                                0,
                    177:                                                "$.name outside of $name[...]");
                    178: 
1.297     paf       179:                                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.297     paf       186:                                Value& value=stack.pop().value();
                    187:                                const String& name=stack.pop().string();  debug_name=&name;
                    188:                                Value& ncontext=stack.pop().value();
                    189:                                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.297     paf       197:                                Value& value=stack.pop().value();
                    198:                                const String& name=stack.pop().string();  debug_name=&name;
                    199:                                Value& ncontext=stack.pop().value();
                    200:                                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.297     paf       205:                                ArrayOperation& local_ops=*i.next().ops;
1.182     parser    206: #ifdef DEBUG_EXECUTE
1.297     paf       207:                                debug_printf(sapi_info, " (%d)\n", local_ops.count());
                    208:                                debug_dump(sapi_info, 1, local_ops);
1.182     parser    209: #endif                         
1.297     paf       210:                                Value& value=*new VJunction(new Junction( 
                    211:                                        get_self(), 0,
1.257     paf       212:                                        method_frame, 
1.240     paf       213:                                        rcontext, 
                    214:                                        wcontext, 
1.297     paf       215:                                        &local_ops));
1.238     paf       216: 
1.297     paf       217:                                const String& name=stack.pop().string();  debug_name=&name;
                    218:                                Value& ncontext=stack.pop().value();
                    219:                                ncontext.put_element(name, &value, false);
1.182     parser    220:                                break;
                    221:                        }
1.209     paf       222:                case OP_NESTED_CODE:
                    223:                        {
1.297     paf       224:                                ArrayOperation& local_ops=*i.next().ops;
1.209     paf       225: #ifdef DEBUG_EXECUTE
1.297     paf       226:                                debug_printf(sapi_info, " (%d)\n", local_ops.count());
                    227:                                debug_dump(sapi_info, 1, local_ops);
1.209     paf       228: #endif                         
1.297     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.297     paf       234:                                Value& value=stack.pop().value();
                    235:                                write_assign_lang(value);
1.86      paf       236:                                break;
                    237:                        }
1.108     paf       238:                case OP_WRITE_EXPR_RESULT:
                    239:                        {
1.297     paf       240:                                Value& value=stack.pop().value();
                    241:                                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.297     paf       250:                                i.next(); // ignore origin
                    251:                                Value* value=i.next().value;
1.157     parser    252: #ifdef DEBUG_EXECUTE
1.297     paf       253:                                debug_printf(sapi_info, " \"%s\"", value->get_string()->cstr());
1.157     parser    254: #endif
1.297     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.297     paf       261:                                const String& name=stack.pop().string();  debug_name=&name;
                    262:                                Value& ncontext=stack.pop().value();
                    263:                                Value& value=get_element(ncontext, name, true);
                    264:                                stack.push(value);
1.215     paf       265:                                break;
                    266:                        }
1.15      paf       267:                case OP_GET_ELEMENT:
1.11      paf       268:                        {
1.297     paf       269:                                const String& name=stack.pop().string();  debug_name=&name;
                    270:                                Value& ncontext=stack.pop().value();
                    271:                                Value& value=get_element(ncontext, name, false);
                    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.297     paf       278:                                const String& name=stack.pop().string();  debug_name=&name;
                    279:                                Value& ncontext=stack.pop().value();
                    280:                                Value& value=get_element(ncontext, name, false);
                    281:                                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.297     paf       288:                                ArrayOperation& local_ops=*i.next().ops;
1.226     paf       289:                                
1.257     paf       290:                                WContext *saved_wcontext=wcontext;
1.297     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.297     paf       296:                                execute(local_ops);
1.226     paf       297: 
1.297     paf       298:                                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.297     paf       306:                                ArrayOperation& local_ops=*i.next().ops;
1.226     paf       307: 
1.257     paf       308:                                WContext *saved_wcontext=wcontext;
1.297     paf       309:                                WWrapper local(0 /*empty*/, wcontext);
1.226     paf       310:                                wcontext=&local;
                    311: 
1.297     paf       312:                                execute(local_ops);
1.226     paf       313: 
1.297     paf       314:                                Value* value;
1.49      paf       315:                                // from "$a $b" part of expression taking only string value,
                    316:                                // ignoring any other content of wcontext
1.297     paf       317:                                if(const String* string=wcontext->get_string())
                    318:                                        value=new VString(*string);
1.80      paf       319:                                else
1.297     paf       320:                                        value=new VVoid;
                    321:                                stack.push(*value);
                    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.297     paf       330:                                Value& value=stack.pop().value();
                    331:                                VMethodFrame& frame=stack.upper_value().method_frame();
                    332:                                // this op is executed from CALL local_ops only, so may skip the check "method_frame_to_fill==0"
                    333:                                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.297     paf       340:                                ArrayOperation& local_ops=*i.next().ops;
                    341:                                VMethodFrame& frame=stack.upper_value().method_frame();
1.237     paf       342: #ifdef DEBUG_EXECUTE
1.297     paf       343:                                debug_printf(sapi_info, " (%d)\n", local_ops.count());
                    344:                                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.297     paf       353:                                Value& value=*new VJunction(new Junction(
                    354:                                        get_self(), 0,
1.257     paf       355:                                        method_frame, 
1.237     paf       356:                                        rcontext, 
1.297     paf       357:                                        opcode==OP_EXPR_CODE__STORE_PARAM?0:wcontext, 
                    358:                                        &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.297     paf       361:                                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.297     paf       380:                                //is_debug_junction=true;
                    381:                                ArrayOperation* local_ops=i.next().ops;
1.157     parser    382: #ifdef DEBUG_EXECUTE
1.297     paf       383:                                debug_printf(sapi_info, " (%d)\n", local_ops?local_ops->count():0);
                    384:                                if(local_ops)
                    385:                                        debug_dump(sapi_info, 1, *local_ops);
1.237     paf       386: 
1.297     paf       387:                                debug_printf(sapi_info, "->\n");
1.157     parser    388: #endif
1.297     paf       389:                                Value& value=stack.pop().value();
1.237     paf       390: 
1.297     paf       391:                                Junction* junction=value.get_junction();
                    392:                                if(!junction) {
                    393:                                        if(value.is("void"))
                    394:                                                throw Exception("parser.runtime",
                    395:                                                        0,
                    396:                                                        "undefined method");
                    397:                                        else
                    398:                                                throw Exception("parser.runtime",
                    399:                                                        0,
                    400:                                                        "is '%s', not a method or junction, can not call it",
                    401:                                                                value.type());
                    402:                                }
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.297     paf       411:                                                
                    412:                                                "is '%s', it is code junction, can not call it",
                    413:                                                        value.type());
1.282     paf       414:                                */
1.237     paf       415: 
1.297     paf       416:                                VMethodFrame frame(*junction, method_frame/*caller*/);
1.237     paf       417:                                if(local_ops){ // store param code goes here
1.297     paf       418:                                        stack.push(frame); // argument for *STORE_PARAM ops
1.237     paf       419:                                        execute(*local_ops);
1.297     paf       420:                                        stack.pop().value();
1.237     paf       421:                                }
                    422:                                frame.fill_unspecified_params();
1.257     paf       423:                                VMethodFrame *saved_method_frame=method_frame;
1.297     paf       424:                                Value* saved_rcontext=rcontext;
1.257     paf       425:                                WContext *saved_wcontext=wcontext;
1.45      paf       426:                                
1.297     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.297     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.297     paf       439:                                                                0, //&frame.name(),
1.204     paf       440:                                                                "is not a constructor, system class '%s' can be constructed only implicitly", 
1.297     paf       441:                                                                        called_class.name().cstr());
1.204     paf       442: 
1.285     paf       443:                                                frame.write(*new_self, 
1.297     paf       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.297     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.285     paf       451:                                        new_self=&frame.junction.self;
1.75      paf       452: 
1.285     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.297     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.297     paf       466:                                                method_frame=&frame;
                    467:                                                if(method.native_code) { // native code?
                    468:                                                        method.check_actual_numbered_params(
                    469:                                                                frame.junction.self, 
                    470:                                                                /*frame.name(), */frame.numbered_params());
                    471:                                                        method.native_code(
                    472:                                                                *this, 
                    473:                                                                *frame.numbered_params()); // execute it
                    474:                                                } else // parser code, execute it
                    475:                                                        recoursion_checked_execute(/*frame.name(), */*method.parser_code);
1.197     parser    476:                                        } else
1.223     paf       477:                                                throw Exception("parser.runtime",
1.297     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.220     paf       488: 
1.297     paf       489:                                if(opcode==OP_CALL__WRITE) {
                    490:                                        write_assign_lang(result);
1.232     paf       491:                                } else { // OP_CALL
1.297     paf       492:                                        stack.push(result.as_value());
1.232     paf       493:                                }
                    494:                                
1.157     parser    495: #ifdef DEBUG_EXECUTE
1.297     paf       496:                                debug_printf(sapi_info, "<-returned");
1.157     parser    497: #endif
1.297     paf       498:                                //is_debug_junction=false;
1.49      paf       499:                                break;
                    500:                        }
                    501: 
1.55      paf       502:                // expression ops: unary
                    503:                case OP_NEG:
                    504:                        {
1.297     paf       505:                                Value& a=stack.pop().value();
                    506:                                Value& value=*new VDouble(-a.as_double());
                    507:                                stack.push(value);
1.55      paf       508:                                break;
                    509:                        }
                    510:                case OP_INV:
                    511:                        {
1.297     paf       512:                                Value& a=stack.pop().value();
                    513:                                Value& value=*new VDouble(~a.as_int());
                    514:                                stack.push(value);
1.55      paf       515:                                break;
                    516:                        }
                    517:                case OP_NOT:
                    518:                        {
1.297     paf       519:                                Value& a=stack.pop().value();
                    520:                                Value& value=*new VBool(!a.as_bool());
                    521:                                stack.push(value);
1.55      paf       522:                                break;
                    523:                        }
1.62      paf       524:                case OP_DEF:
                    525:                        {
1.297     paf       526:                                Value& a=stack.pop().value();
                    527:                                Value& value=*new VBool(a.is_defined());
                    528:                                stack.push(value);
1.62      paf       529:                                break;
                    530:                        }
                    531:                case OP_IN:
                    532:                        {
1.297     paf       533:                                Value& a=stack.pop().value();
                    534:                                const String& path=a.as_string();
                    535:                                Value& value=*new VBool(request_info.uri && *request_info.uri && path.this_starts(request_info.uri));
                    536:                                stack.push(value);
1.62      paf       537:                                break;
                    538:                        }
                    539:                case OP_FEXISTS:
                    540:                        {
1.297     paf       541:                                Value& a=stack.pop().value();
                    542:                                Value& value=*new VBool(file_readable(absolute(a.as_string())));
                    543:                                stack.push(value);
1.148     paf       544:                                break;
                    545:                        }
                    546:                case OP_DEXISTS:
                    547:                        {
1.297     paf       548:                                Value& a=stack.pop().value();
                    549:                                Value& value=*new VBool(dir_readable(absolute(a.as_string())));
                    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.297     paf       557:                                Value& b=stack.pop().value();  Value& a=stack.pop().value();
                    558:                                Value& value=*new VDouble(a.as_double() - b.as_double());
                    559:                                stack.push(value);
1.53      paf       560:                                break;
                    561:                        }
1.55      paf       562:                case OP_ADD: 
1.53      paf       563:                        {
1.297     paf       564:                                Value& b=stack.pop().value();  Value& a=stack.pop().value();
                    565:                                Value& value=*new VDouble(a.as_double() + b.as_double());
                    566:                                stack.push(value);
1.53      paf       567:                                break;
                    568:                        }
1.49      paf       569:                case OP_MUL: 
                    570:                        {
1.297     paf       571:                                Value& b=stack.pop().value();  Value& a=stack.pop().value();
                    572:                                Value& value=*new VDouble(a.as_double() * b.as_double());
                    573:                                stack.push(value);
1.53      paf       574:                                break;
                    575:                        }
                    576:                case OP_DIV: 
                    577:                        {
1.297     paf       578:                                Value& b=stack.pop().value();  Value& a=stack.pop().value();
1.170     parser    579: 
1.297     paf       580:                                double a_double=a.as_double();
                    581:                                double b_double=b.as_double();
1.170     parser    582: 
1.171     parser    583:                                if(b_double == 0) {
1.297     paf       584:                                        //const String* problem_source=b.as_string();
1.223     paf       585:                                        throw Exception("number.zerodivision",
1.297     paf       586:                                                0, //problem_source,
1.170     parser    587:                                                "Division by zero");
1.171     parser    588:                                }
1.170     parser    589: 
1.297     paf       590:                                Value& value=*new VDouble(a_double / b_double);
                    591:                                stack.push(value);
1.54      paf       592:                                break;
                    593:                        }
1.55      paf       594:                case OP_MOD: 
                    595:                        {
1.297     paf       596:                                Value& b=stack.pop().value();  Value& a=stack.pop().value();
1.170     parser    597: 
1.297     paf       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.297     paf       602:                                        //const String* problem_source=b.as_string();
1.223     paf       603:                                        throw Exception("number.zerodivision",
1.297     paf       604:                                                0, //problem_source,
1.170     parser    605:                                                "Modulus by zero");
1.171     parser    606:                                }
1.170     parser    607: 
1.297     paf       608:                                Value& value=*new VDouble(fmod(a_double, b_double));
                    609:                                stack.push(value);
1.201     paf       610:                                break;
                    611:                        }
                    612:                case OP_INTDIV:
                    613:                        {
1.297     paf       614:                                Value& b=stack.pop().value();  Value& a=stack.pop().value();
1.201     paf       615: 
1.297     paf       616:                                int a_int=a.as_int();
                    617:                                int b_int=b.as_int();
1.201     paf       618: 
                    619:                                if(b_int == 0) {
1.297     paf       620:                                        //const String* problem_source=b.as_string();
1.223     paf       621:                                        throw Exception("number.zerodivision",
1.297     paf       622:                                                0, //problem_source,
1.201     paf       623:                                                "Division by zero");
                    624:                                }
                    625: 
1.297     paf       626:                                Value& value=*new VInt(a_int / b_int);
                    627:                                stack.push(value);
1.55      paf       628:                                break;
                    629:                        }
1.274     paf       630:                case OP_BIN_SL:
                    631:                        {
1.297     paf       632:                                Value& b=stack.pop().value();  Value& a=stack.pop().value();
                    633:                                Value& value=*new VInt(
                    634:                                        a.as_int() <<
                    635:                                        b.as_int());
                    636:                                stack.push(value);
1.274     paf       637:                                break;
                    638:                        }
                    639:                case OP_BIN_SR:
                    640:                        {
1.297     paf       641:                                Value& b=stack.pop().value();  Value& a=stack.pop().value();
                    642:                                Value& value=*new VInt(
                    643:                                        a.as_int() >>
                    644:                                        b.as_int());
                    645:                                stack.push(value);
1.274     paf       646:                                break;
                    647:                        }
1.55      paf       648:                case OP_BIN_AND:
1.54      paf       649:                        {
1.297     paf       650:                                Value& b=stack.pop().value();  Value& a=stack.pop().value();
                    651:                                Value& value=*new VInt(
                    652:                                        a.as_int() &
                    653:                                        b.as_int());
                    654:                                stack.push(value);
1.54      paf       655:                                break;
                    656:                        }
1.55      paf       657:                case OP_BIN_OR:
1.54      paf       658:                        {
1.297     paf       659:                                Value& b=stack.pop().value();  Value& a=stack.pop().value();
                    660:                                Value& value=*new VInt(
                    661:                                        a.as_int() |
                    662:                                        b.as_int());
                    663:                                stack.push(value);
1.55      paf       664:                                break;
                    665:                        }
1.56      paf       666:                case OP_BIN_XOR:
                    667:                        {
1.297     paf       668:                                Value& b=stack.pop().value();  Value& a=stack.pop().value();
                    669:                                Value& value=*new VInt(
                    670:                                        a.as_int() ^
                    671:                                        b.as_int());
                    672:                                stack.push(value);
1.56      paf       673:                                break;
                    674:                        }
1.55      paf       675:                case OP_LOG_AND:
                    676:                        {
1.297     paf       677:                                ArrayOperation& local_ops=stack.pop().ops();  Value& a=stack.pop().value();
1.263     paf       678:                                bool result;
1.297     paf       679:                                if(a.as_bool()) {
                    680:                                        execute(local_ops);
                    681:                                        Value& b=stack.pop().value();
                    682:                                        result=b.as_bool();
1.209     paf       683:                                } else
1.263     paf       684:                                        result=false;
1.297     paf       685:                                Value& value=*new VBool(result);
                    686:                                stack.push(value);
1.55      paf       687:                                break;
                    688:                        }
                    689:                case OP_LOG_OR:
                    690:                        {
1.297     paf       691:                                ArrayOperation& local_ops=stack.pop().ops();  Value& a=stack.pop().value();
1.263     paf       692:                                bool result;
1.297     paf       693:                                if(a.as_bool()) 
1.263     paf       694:                                        result=true;
1.209     paf       695:                                else {
1.297     paf       696:                                        execute(local_ops);
                    697:                                        Value& b=stack.pop().value();
                    698:                                        result=b.as_bool();
1.209     paf       699:                                }
1.297     paf       700:                                Value& value=*new VBool(result);
                    701:                                stack.push(value);
1.56      paf       702:                                break;
                    703:                        }
                    704:                case OP_LOG_XOR:
                    705:                        {
1.297     paf       706:                                Value& b=stack.pop().value();  Value& a=stack.pop().value();
                    707:                                Value& value=*new VBool(a.as_bool() ^ b.as_bool());
                    708:                                stack.push(value);
1.55      paf       709:                                break;
                    710:                        }
                    711:                case OP_NUM_LT: 
                    712:                        {
1.297     paf       713:                                volatile double b_double=stack.pop().value().as_double();
                    714:                                volatile double a_double=stack.pop().value().as_double();
                    715:                                Value& value=*new VBool(a_double<b_double);
                    716:                                stack.push(value);
1.55      paf       717:                                break;
                    718:                        }
                    719:                case OP_NUM_GT: 
                    720:                        {
1.297     paf       721:                                volatile double b_double=stack.pop().value().as_double();
                    722:                                volatile double a_double=stack.pop().value().as_double();
                    723:                                Value& value=*new VBool(a_double>b_double);
                    724:                                stack.push(value);
1.55      paf       725:                                break;
                    726:                        }
                    727:                case OP_NUM_LE: 
                    728:                        {
1.297     paf       729:                                volatile double b_double=stack.pop().value().as_double();
                    730:                                volatile double a_double=stack.pop().value().as_double();
                    731:                                Value& value=*new VBool(a_double<=b_double);
                    732:                                stack.push(value);
1.55      paf       733:                                break;
                    734:                        }
                    735:                case OP_NUM_GE: 
                    736:                        {
1.297     paf       737:                                volatile double b_double=stack.pop().value().as_double();
                    738:                                volatile double a_double=stack.pop().value().as_double();
                    739:                                Value& value=*new VBool(a_double>=b_double);
                    740:                                stack.push(value);
1.55      paf       741:                                break;
                    742:                        }
                    743:                case OP_NUM_EQ: 
                    744:                        {
1.297     paf       745:                                volatile double b_double=stack.pop().value().as_double();
                    746:                                volatile double a_double=stack.pop().value().as_double();
                    747:                                Value& value=*new VBool(a_double==b_double);
                    748:                                stack.push(value);
1.55      paf       749:                                break;
                    750:                        }
                    751:                case OP_NUM_NE: 
                    752:                        {
1.297     paf       753:                                volatile double b_double=stack.pop().value().as_double();
                    754:                                volatile double a_double=stack.pop().value().as_double();
                    755:                                Value& value=*new VBool(a_double!=b_double);
                    756:                                stack.push(value);
1.54      paf       757:                                break;
                    758:                        }
1.58      paf       759:                case OP_STR_LT: 
                    760:                        {
1.297     paf       761:                                Value& b=stack.pop().value();  Value& a=stack.pop().value();
                    762:                                Value& value=*new VBool(a.as_string() < b.as_string());
                    763:                                stack.push(value);
1.58      paf       764:                                break;
                    765:                        }
                    766:                case OP_STR_GT: 
                    767:                        {
1.297     paf       768:                                Value& b=stack.pop().value();  Value& a=stack.pop().value();
                    769:                                Value& value=*new VBool(a.as_string() > b.as_string());
                    770:                                stack.push(value);
1.58      paf       771:                                break;
                    772:                        }
1.55      paf       773:                case OP_STR_LE: 
1.58      paf       774:                        {
1.297     paf       775:                                Value& b=stack.pop().value();  Value& a=stack.pop().value();
                    776:                                Value& value=*new VBool(a.as_string() <= b.as_string());
                    777:                                stack.push(value);
1.58      paf       778:                                break;
                    779:                        }
1.55      paf       780:                case OP_STR_GE: 
1.54      paf       781:                        {
1.297     paf       782:                                Value& b=stack.pop().value();  Value& a=stack.pop().value();
                    783:                                Value& value=*new VBool(a.as_string() >= b.as_string());
                    784:                                stack.push(value);
1.58      paf       785:                                break;
                    786:                        }
                    787:                case OP_STR_EQ: 
                    788:                        {
1.297     paf       789:                                Value& b=stack.pop().value();  Value& a=stack.pop().value();
                    790:                                Value& value=*new VBool(a.as_string() == b.as_string());
                    791:                                stack.push(value);
1.58      paf       792:                                break;
                    793:                        }
                    794:                case OP_STR_NE: 
                    795:                        {
1.297     paf       796:                                Value& b=stack.pop().value();  Value& a=stack.pop().value();
                    797:                                Value& value=*new VBool(a.as_string() != b.as_string());
                    798:                                stack.push(value);
1.103     paf       799:                                break;
                    800:                        }
                    801:                case OP_IS:
                    802:                        {
1.297     paf       803:                                Value& b=stack.pop().value();  Value& a=stack.pop().value();
                    804:                                Value& value=*new VBool(a.is(b.as_string().cstr()));
                    805:                                stack.push(value);
1.28      paf       806:                                break;
                    807:                        }
                    808: 
1.11      paf       809:                default:
1.223     paf       810:                        throw Exception(0,
1.67      paf       811:                                0,
1.297     paf       812:                                "invalid opcode %d", opcode); 
1.11      paf       813:                }
                    814:        }
1.297     paf       815:        } catch(...) {
                    816:                // record it to stack trace
                    817:                //if(is_debug_junction)
                    818:                        if(debug_name)
                    819:                                exception_trace.push(Trace(debug_name, debug_origin));
                    820:                rethrow;
                    821:        }
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.297     paf       828: Value& Request::get_element(Value& ncontext, const String& name, bool can_call_operator) {
                    829:        Value* value=0;
1.249     paf       830:        if(can_call_operator) {
1.297     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.297     paf       838:                        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.297     paf       841:                                                if(Value* base=get_self().base()) { // doing DYNAMIC call
1.290     paf       842:                                                        Temp_derived temp_derived(*base, 0); // temporarily prevent go-back-down virtual calls
                    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.297     paf       848:                value=ncontext.get_element(name, ncontext, false);
1.291     paf       849: 
                    850:        if(value && wcontext->get_constructing())
1.297     paf       851:                if(Junction* junction=value->get_junction()) {
                    852:                        if(junction->self.get_class()!=&ncontext)
1.291     paf       853:                                throw Exception("parser.runtime",
                    854:                                        &name,
1.297     paf       855:                                        "constructor must be declared in class '%s'", 
                    856:                                                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.294     paf       861:                value=&process_to_value(*value); // process possible code-junction
1.284     paf       862:        else
1.297     paf       863:                value=new VVoid;
1.63      paf       864: 
1.297     paf       865:        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.229     paf       879: StringOrValue Request::process(Value& input_value, bool intercept_string) {
1.228     paf       880:        StringOrValue result;
1.297     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.297     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",
                    890:                                0,
                    891:                                "junction used outside of context");
                    892: 
1.257     paf       893:                VMethodFrame *saved_method_frame=method_frame;  
1.297     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.297     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.297     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.229     paf       916:                        result.set_string(*wcontext->get_string());
1.224     paf       917:                } else {
1.225     paf       918:                        // plain wwrapper
1.297     paf       919:                        WWrapper local(0/*empty*/, wcontext);
1.225     paf       920:                        wcontext=&local;
                    921:                
                    922:                        // execute it
1.297     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.297     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.298   ! paf       941: StringOrValue Request::execute_method(VMethodFrame& amethod_frame, const Method& method) {
1.257     paf       942:        VMethodFrame *saved_method_frame=method_frame;  
1.297     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.298   ! paf       953:        StringOrValue result=wcontext->result();
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.297     paf       963: const String* Request::execute_method(Value& aself, 
                    964:                                      const Method& method, VString* optional_param,
                    965:                                      bool do_return_string) {
1.257     paf       966:        VMethodFrame *saved_method_frame=method_frame;  
1.297     paf       967:        Value* saved_rcontext=rcontext;  
1.257     paf       968:        WContext *saved_wcontext=wcontext;
1.208     paf       969:        
1.297     paf       970:        Junction local_junction(aself, &method, 0,0,0, 0);
                    971:        VMethodFrame local_frame(local_junction, method_frame/*caller*/);
1.242     paf       972:        if(optional_param && local_frame.can_store_param()) {
1.297     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.297     paf       980:        if(do_return_string)
1.246     paf       981:                wcontext->write(local_frame);
1.208     paf       982:        
                    983:        // execute!     
                    984:        execute(*method.parser_code);
                    985:        
                    986:        // result
1.297     paf       987:        const String* result=0;
                    988:        if(do_return_string)
                    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.297     paf       994: 
                    995:        return result;
1.242     paf       996: }
                    997: 
1.297     paf       998: Request::Execute_nonvirtual_method_result 
                    999: Request::execute_nonvirtual_method(VStateless_class& aclass, 
                   1000:                                   const String& method_name, VString* optional_param,
                   1001:                                   bool do_return_string) {
                   1002:        Execute_nonvirtual_method_result result;
                   1003:        result.method=aclass.get_method(method_name);
                   1004:        if(result.method)
                   1005:                result.string=execute_method(aclass, *result.method, optional_param, 
                   1006:                        do_return_string);
                   1007:        return result;
1.99      paf      1008: }
                   1009: 
1.297     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())
                   1014:                        if(const Method *method=junction->method) 
                   1015:                                return execute_method(aself, *method, 0/*no params*/, true);
1.188     parser   1016:                        
1.176     parser   1017:        return 0;
                   1018: }

E-mail: