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

1.117     paf         1: /** @file
1.118     paf         2:        Parser: executor part of request class.
                      3: 
1.88      paf         4:        Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
1.118     paf         5: 
1.89      paf         6:        Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
1.88      paf         7: 
1.160   ! parser      8:        $Id: execute.C,v 1.159 2001/05/17 15:20:15 parser Exp $
1.1       paf         9: */
                     10: 
1.123     paf        11: #include "pa_config_includes.h"
1.152     paf        12: #include "pa_opcode.h"
1.8       paf        13: #include "pa_array.h" 
1.11      paf        14: #include "pa_request.h"
1.15      paf        15: #include "pa_vstring.h"
1.22      paf        16: #include "pa_vhash.h"
1.23      paf        17: #include "pa_vunknown.h"
1.145     paf        18: #include "pa_vcode_frame.h"
                     19: #include "pa_vmethod_frame.h"
1.38      paf        20: #include "pa_vobject.h"
1.49      paf        21: #include "pa_vdouble.h"
1.54      paf        22: #include "pa_vbool.h"
1.94      paf        23: #include "pa_vtable.h"
1.132     paf        24: #include "pa_vfile.h"
1.144     paf        25: #include "pa_vimage.h"
1.1       paf        26: 
1.157     parser     27: //#define DEBUG_EXECUTE
                     28: 
1.159     parser     29: const uint ANTI_ENDLESS_EXECUTE_RECOURSION=500;
1.24      paf        30: 
1.157     parser     31: #ifdef DEBUG_EXECUTE
1.1       paf        32: char *opcode_name[]={
1.49      paf        33:        // literals
1.109     paf        34:        "VALUE",  "CURLY_CODE__STORE_PARAM",  "EXPR_CODE__STORE_PARAM",
1.49      paf        35: 
                     36:        // actions
1.51      paf        37:        "WITH_SELF",    "WITH_ROOT",    "WITH_READ",    "WITH_WRITE",
1.66      paf        38:        "GET_CLASS",
1.80      paf        39:        "CONSTRUCT_VALUE",  "CONSTRUCT_DOUBLE",
1.108     paf        40:        "WRITE_VALUE",  "WRITE_EXPR_RESULT",  "STRING__WRITE",
1.18      paf        41:        "GET_ELEMENT",  "GET_ELEMENT__WRITE",
1.1       paf        42:        "CREATE_EWPOOL",        "REDUCE_EWPOOL",
                     43:        "CREATE_RWPOOL",        "REDUCE_RWPOOL",
1.49      paf        44:        "CREATE_SWPOOL",        "REDUCE_SWPOOL",
1.1       paf        45:        "GET_METHOD_FRAME",
                     46:        "STORE_PARAM",
1.51      paf        47:        "CALL",
1.49      paf        48: 
                     49:        // expression ops: unary
1.148     paf        50:        "NEG", "INV", "NOT", "DEF", "IN", "FEXISTS", "DEXISTS",
1.49      paf        51:        // expression ops: binary
                     52:        "SUB", "ADD", "MUL", "DIV", "MOD",
1.56      paf        53:        "BIN_AND", "BIN_OR", "BIN_XOR",
                     54:        "LOG_AND", "LOG_OR", "LOG_XOR",
1.49      paf        55:        "NUM_LT", "NUM_GT", "NUM_LE", "NUM_GE", "NUM_EQ", "NUM_NE",
1.103     paf        56:        "STR_LT", "STR_GT", "STR_LE", "STR_GE", "STR_EQ", "STR_NE",
                     57:        "IS"
1.1       paf        58: };
                     59: 
1.158     parser     60: void va_debug_printf(Pool& pool, const char *fmt,va_list args) {
1.127     paf        61:        char buf[MAX_STRING];
                     62:        vsnprintf(buf, MAX_STRING, fmt, args);
                     63:        SAPI::log(pool, "%s", buf);
1.107     paf        64: }
                     65: 
1.158     parser     66: void debug_printf(Pool& pool, const char *fmt, ...) {
1.107     paf        67:     va_list args;
                     68:     va_start(args,fmt);
1.158     parser     69:     va_debug_printf(pool,fmt,args);
1.107     paf        70:     va_end(args);
1.105     paf        71: }
                     72: 
1.158     parser     73: void debug_dump(Pool& pool, int level, const Array& ops) {
1.157     parser     74:        {
1.135     paf        75:                int size=ops.quick_size();
1.158     parser     76:                //debug_printf(pool, "size=%d\n", size);
1.23      paf        77:                for(int i=0; i<size; i++) {
                     78:                        Operation op;
                     79:                        op.cast=ops.quick_get(i);
1.158     parser     80:                        debug_printf(pool, "%8X\n", op.cast);
1.23      paf        81:                }
                     82:        }
                     83: 
1.135     paf        84:        int size=ops.quick_size();
1.158     parser     85:        //debug_printf(pool, "size=%d\n", size);
1.1       paf        86:        for(int i=0; i<size; i++) {
1.23      paf        87:                Operation op;
                     88:                op.cast=ops.quick_get(i);
1.1       paf        89: 
1.86      paf        90:                if(op.code==OP_VALUE || op.code==OP_STRING__WRITE) {
1.52      paf        91:                        Value *value=static_cast<Value *>(ops.quick_get(++i));
1.158     parser     92:                        debug_printf(pool, 
1.133     paf        93:                                "%*s%s"
                     94:                                " \"%s\" %s", 
                     95:                                level*4, "", opcode_name[op.code],
                     96:                                value->get_string()->cstr(), value->type());
                     97:                        continue;
1.15      paf        98:                }
1.158     parser     99:                debug_printf(pool, "%*s%s", level*4, "", opcode_name[op.code]);
1.1       paf       100: 
1.109     paf       101:                if(op.code==OP_CURLY_CODE__STORE_PARAM || op.code==OP_EXPR_CODE__STORE_PARAM) {
1.10      paf       102:                        const Array *local_ops=reinterpret_cast<const Array *>(ops.quick_get(++i));
1.158     parser    103:                        debug_dump(pool, level+1, *local_ops);
1.1       paf       104:                }
                    105:        }
                    106: }
1.157     parser    107: #endif
1.1       paf       108: 
1.159     parser    109: #define PUSH(value) stack.push(value)
                    110: #define POP() static_cast<Value *>(stack.pop())
                    111: #define POP_NAME() static_cast<Value *>(stack.pop())->as_string()
                    112: 
1.32      paf       113: void Request::execute(const Array& ops) {
1.157     parser    114: #ifdef DEBUG_EXECUTE
1.158     parser    115:        debug_printf(pool(), "source----------------------------\n");
                    116:        debug_dump(pool(), 0, ops);
                    117:        debug_printf(pool(), "execution-------------------------\n");
1.157     parser    118: #endif
1.12      paf       119: 
1.135     paf       120:        int size=ops.quick_size();
1.158     parser    121:        //debug_printf(pool(), "size=%d\n", size);
1.11      paf       122:        for(int i=0; i<size; i++) {
1.23      paf       123:                Operation op;
                    124:                op.cast=ops.quick_get(i);
1.157     parser    125: #ifdef DEBUG_EXECUTE
1.158     parser    126:                debug_printf(pool(), "%d:%s", stack.top_index()+1, opcode_name[op.code]);
1.157     parser    127: #endif
1.11      paf       128: 
1.23      paf       129:                switch(op.code) {
1.51      paf       130:                // param in next instruction
1.52      paf       131:                case OP_VALUE:
1.32      paf       132:                        {
1.52      paf       133:                                Value *value=static_cast<Value *>(ops.quick_get(++i));
1.157     parser    134: #ifdef DEBUG_EXECUTE
1.158     parser    135:                                debug_printf(pool(), " \"%s\" %s", value->get_string()->cstr(), value->type());
1.157     parser    136: #endif
1.52      paf       137:                                PUSH(value);
1.32      paf       138:                                break;
                    139:                        }
1.109     paf       140:                case OP_CURLY_CODE__STORE_PARAM:
                    141:                case OP_EXPR_CODE__STORE_PARAM:
1.32      paf       142:                        {
1.73      paf       143:                                VMethodFrame *frame=static_cast<VMethodFrame *>(stack.top_value());
1.65      paf       144:                                // code
1.32      paf       145:                                const Array *local_ops=reinterpret_cast<const Array *>(ops.quick_get(++i));
1.157     parser    146: #ifdef DEBUG_EXECUTE
1.158     parser    147:                                debug_printf(pool(), " (%d)\n", local_ops->size());
                    148:                                debug_dump(pool(), 1, *local_ops);
1.157     parser    149: #endif                         
1.109     paf       150:                                // when they evaluate expression parameter,
                    151:                                // the object expression result
                    152:                                // does not need to be written into calling frame
                    153:                                // it must go into any expressions using that parameter
1.146     paf       154:                                // hence, we zero junction.wcontext here, and later
                    155:                                // in .process we would test that field 
1.109     paf       156:                                // in decision "which wwrapper to use"
1.32      paf       157:                                Junction& j=*NEW Junction(pool(), 
1.45      paf       158:                                        *self, 0, 0,
1.130     paf       159:                                        root, 
1.149     paf       160:                                        rcontext, 
                    161:                                        op.code==OP_EXPR_CODE__STORE_PARAM?0:wcontext, 
1.130     paf       162:                                        local_ops);
1.32      paf       163:                                
                    164:                                Value *value=NEW VJunction(j);
1.65      paf       165: 
                    166:                                // store param
1.92      paf       167:                                frame->store_param(frame->name(), value);
1.32      paf       168:                                break;
                    169:                        }
1.66      paf       170:                case OP_GET_CLASS:
1.38      paf       171:                        {
1.130     paf       172:                                // maybe they do ^class:method[] call, remember the fact
1.98      paf       173:                                wcontext->set_somebody_entered_some_class();
                    174: 
1.82      paf       175:                                const String& name=POP_NAME();
1.120     paf       176:                                Value *value=static_cast<Value *>(classes().get(name));
                    177:                                if(!value) 
1.66      paf       178:                                        THROW(0,0,
                    179:                                                &name,
1.143     paf       180:                                                "class is undefined"); 
1.66      paf       181: 
1.120     paf       182:                                PUSH(value);
1.38      paf       183:                                break;
                    184:                        }
1.32      paf       185:                        
1.51      paf       186:                // OP_WITH
1.37      paf       187:                case OP_WITH_SELF: 
                    188:                        {
                    189:                                PUSH(self);
                    190:                                break;
                    191:                        }
                    192:                case OP_WITH_ROOT: 
1.11      paf       193:                        {
1.37      paf       194:                                PUSH(root);
1.13      paf       195:                                break;
1.11      paf       196:                        }
1.15      paf       197:                case OP_WITH_READ: 
                    198:                        {
1.24      paf       199:                                PUSH(rcontext);
1.20      paf       200:                                break;
                    201:                        }
1.37      paf       202:                case OP_WITH_WRITE: 
1.20      paf       203:                        {
1.37      paf       204:                                PUSH(wcontext);
1.20      paf       205:                                break;
                    206:                        }
1.37      paf       207:                        
1.51      paf       208:                // OTHER ACTIONS BUT WITHs
1.80      paf       209:                case OP_CONSTRUCT_VALUE:
1.20      paf       210:                        {
1.37      paf       211:                                Value *value=POP();
1.82      paf       212:                                const String& name=POP_NAME();
1.37      paf       213:                                Value *ncontext=POP();
1.81      paf       214:                                ncontext->put_element(name, value);
1.37      paf       215:                                value->set_name(name);
1.15      paf       216:                                break;
                    217:                        }
1.80      paf       218:                case OP_CONSTRUCT_EXPR:
                    219:                        {
                    220:                                Value *value=POP();
1.82      paf       221:                                const String& name=POP_NAME();
1.80      paf       222:                                Value *ncontext=POP();
1.128     paf       223:                                ncontext->put_element(name, value->as_expr_result());
1.80      paf       224:                                value->set_name(name);
                    225:                                break;
                    226:                        }
1.108     paf       227:                case OP_WRITE_VALUE:
1.13      paf       228:                        {
1.24      paf       229:                                Value *value=POP();
1.92      paf       230:                                write_assign_lang(*value);
1.150     paf       231: 
                    232:                                // forget the fact they've entered some ^object.method[].
                    233:                                // see OP_GET_ELEMENT
                    234:                                wcontext->clear_somebody_entered_some_object();
1.86      paf       235:                                break;
                    236:                        }
1.108     paf       237:                case OP_WRITE_EXPR_RESULT:
                    238:                        {
                    239:                                Value *value=POP();
1.128     paf       240:                                write_expr_result(*value->as_expr_result());
1.108     paf       241:                                break;
                    242:                        }
1.86      paf       243:                case OP_STRING__WRITE:
                    244:                        {
                    245:                                VString *vstring=static_cast<VString *>(ops.quick_get(++i));
1.157     parser    246: #ifdef DEBUG_EXECUTE
1.158     parser    247:                                debug_printf(pool(), " \"%s\"", vstring->string().cstr());
1.157     parser    248: #endif
1.122     paf       249:                                write_no_lang(vstring->string());
1.13      paf       250:                                break;
1.14      paf       251:                        }
1.13      paf       252:                        
1.15      paf       253:                case OP_GET_ELEMENT:
1.11      paf       254:                        {
1.150     paf       255:                                // maybe they do ^object.method[] call, remember the fact
                    256:                                wcontext->inc_somebody_entered_some_object();
                    257: 
1.17      paf       258:                                Value *value=get_element();
1.24      paf       259:                                PUSH(value);
1.17      paf       260:                                break;
                    261:                        }
                    262: 
1.18      paf       263:                case OP_GET_ELEMENT__WRITE:
1.17      paf       264:                        {
                    265:                                Value *value=get_element();
1.92      paf       266:                                write_assign_lang(*value);
1.17      paf       267:                                break;
                    268:                        }
                    269: 
1.32      paf       270: 
1.17      paf       271:                case OP_CREATE_EWPOOL:
                    272:                        {
1.24      paf       273:                                PUSH(wcontext);
1.137     paf       274:                                PUSH((void *)flang);
                    275:                                flang=String::UL_PASS_APPENDED;
1.138     paf       276:                                wcontext=NEW WWrapper(pool(), 0 /*empty*/, true /*constructing*/);
1.17      paf       277:                                break;
                    278:                        }
                    279:                case OP_REDUCE_EWPOOL:
                    280:                        {
1.36      paf       281:                                Value *value=wcontext->result();
1.137     paf       282:                                flang=static_cast<String::Untaint_lang>(reinterpret_cast<int>(POP()));
1.25      paf       283:                                wcontext=static_cast<WContext *>(POP());
1.24      paf       284:                                PUSH(value);
1.13      paf       285:                                break;
1.15      paf       286:                        }
1.13      paf       287:                        
1.26      paf       288:                case OP_CREATE_RWPOOL:
                    289:                        {
                    290:                                Value *ncontext=POP();
                    291:                                PUSH(rcontext);
1.146     paf       292:                                PUSH(wcontext);
1.26      paf       293:                                rcontext=ncontext;
1.138     paf       294:                                wcontext=NEW WWrapper(pool(), ncontext, false /*not constructing*/);
1.26      paf       295:                                break;
                    296:                        }
                    297:                case OP_REDUCE_RWPOOL:
                    298:                        {
1.82      paf       299:                                const String *string=wcontext->get_string();
1.80      paf       300:                                Value *value;
                    301:                                if(string)
                    302:                                        value=NEW VString(*string);
                    303:                                else
                    304:                                        value=NEW VUnknown(pool());
1.26      paf       305:                                wcontext=static_cast<WContext *>(POP());
                    306:                                rcontext=POP();
                    307:                                PUSH(value);
                    308:                                break;
                    309:                        }
1.49      paf       310:                case OP_CREATE_SWPOOL:
                    311:                        {
                    312:                                PUSH(wcontext);
1.138     paf       313:                                wcontext=NEW WWrapper(pool(), 0 /*empty*/, false /*not constructing*/);
1.49      paf       314:                                break;
                    315:                        }
                    316:                case OP_REDUCE_SWPOOL:
                    317:                        {
                    318:                                // from "$a $b" part of expression taking only string value,
                    319:                                // ignoring any other content of wcontext
1.82      paf       320:                                const String *string=wcontext->get_string();
1.80      paf       321:                                Value *value;
                    322:                                if(string)
                    323:                                        value=NEW VString(*string);
                    324:                                else
                    325:                                        NEW VUnknown(pool());
1.50      paf       326:                                wcontext=static_cast<WContext *>(POP());
1.49      paf       327:                                PUSH(value);
                    328:                                break;
                    329:                        }
                    330: 
1.51      paf       331:                // CALL
1.28      paf       332:                case OP_GET_METHOD_FRAME:
                    333:                        {
1.32      paf       334:                                Value *value=POP();
1.138     paf       335: 
1.111     paf       336:                                // info: 
                    337:                                //      code compiled so that this one's always method-junction, 
                    338:                                //      not a code-junction
1.32      paf       339:                                Junction *junction=value->get_junction();
1.31      paf       340:                                if(!junction)
1.28      paf       341:                                        THROW(0,0,
1.42      paf       342:                                                &value->name(),
1.111     paf       343:                                                "(%s) not a method or junction, can not call it",
1.38      paf       344:                                                        value->type()); 
1.70      paf       345: 
1.138     paf       346:                                bool is_constructor=
                    347:                                        wcontext->constructing() && // constructing?
                    348:                                        wcontext->somebody_entered_some_class(); // ^class:method[..]?
                    349:                                if(is_constructor)
                    350:                                        wcontext->constructing(false);
                    351: 
1.156     parser    352:                                VMethodFrame *frame=NEW VMethodFrame(pool(), value->name(), *junction, is_constructor);
1.28      paf       353:                                PUSH(frame);
                    354:                                break;
                    355:                        }
                    356:                case OP_STORE_PARAM:
                    357:                        {
                    358:                                Value *value=POP();
1.73      paf       359:                                VMethodFrame *frame=static_cast<VMethodFrame *>(stack.top_value());
1.92      paf       360:                                frame->store_param(frame->name(), value);
1.29      paf       361:                                break;
                    362:                        }
                    363: 
                    364:                case OP_CALL:
                    365:                        {
1.157     parser    366: #ifdef DEBUG_EXECUTE
1.158     parser    367:                                debug_printf(pool(), "->\n");
1.157     parser    368: #endif
1.34      paf       369:                                VMethodFrame *frame=static_cast<VMethodFrame *>(POP());
                    370:                                frame->fill_unspecified_params();
1.45      paf       371:                                PUSH(self);  
                    372:                                PUSH(root);  
                    373:                                PUSH(rcontext);  
                    374:                                PUSH(wcontext); 
                    375:                                
1.100     paf       376:                                VStateless_class *called_class=frame->junction.self.get_class();
1.150     paf       377:                                // not ^name.method call and
1.156     parser    378:                                // is context object or class & is it my class or my parent's class and?
1.100     paf       379:                                VStateless_class *read_class=rcontext->get_class();
1.156     parser    380:                                if(!wcontext->somebody_entered_some_object() && 
1.150     paf       381:                                        read_class && read_class->is_or_derived_from(*called_class)) // yes
1.74      paf       382:                                        self=rcontext; // class dynamic call
1.75      paf       383:                                else // no, not me or relative of mine (total stranger)
1.153     paf       384:                                        // were are constructing something and
1.156     parser    385:                                        // our constructor is just method call and
                    386:                                        // not static-only-method call
1.153     paf       387:                                        if(frame->is_constructor && 
1.156     parser    388:                                                wcontext->somebody_entered_some_object()==1 &&
                    389:                                                frame->junction.method->call_type!=Method::CT_STATIC) {
1.138     paf       390:                                                // this is a constructor call
1.149     paf       391:                                                // some stateless_object creatable derivates
                    392:                                                if(Value *value=called_class->create_new_value(pool()))
                    393:                                                        self=value;
1.121     paf       394:                                                else // stateful object
1.94      paf       395:                                                        self=NEW VObject(pool(), *called_class);
1.83      paf       396:                                                frame->write(*self, 
1.136     paf       397:                                                        String::UL_CLEAN  // not used, always an object, not string
1.83      paf       398:                                                );
1.75      paf       399:                                        } else 
                    400:                                                self=&frame->junction.self; // no, static or simple dynamic call
                    401: 
1.45      paf       402:                                frame->set_self(*self);
1.29      paf       403:                                root=rcontext=wcontext=frame;
1.47      paf       404:                                {
1.48      paf       405:                                        // take object or class from any wrappers
1.102     paf       406:                                        // and substitute class alias to the class they are called AS
                    407:                                        Temp_alias temp_alias(*self->get_aliased(), *frame->junction.vclass);
1.68      paf       408: 
1.99      paf       409:                                        const Method& method=*frame->junction.method;
1.134     paf       410:                                        Method::Call_type call_type=
                    411:                                                called_class==self ? Method::CT_STATIC : Method::CT_DYNAMIC;
                    412:                                        if(
                    413:                                                method.call_type==Method::CT_ANY ||
                    414:                                                method.call_type==call_type) // allowed call type?
                    415:                                                if(method.native_code) { // native code?
1.154     paf       416:                                                        method.check_actual_numbered_params(pool(),
1.134     paf       417:                                                                frame->junction.self, 
                    418:                                                                frame->name(), frame->numbered_params());
1.160   ! parser    419:                                                        method.native_code(
1.134     paf       420:                                                                *this, 
                    421:                                                                frame->name(), frame->numbered_params()); // execute it
1.159     parser    422:                                                } else { // parser code
                    423:                                                        if(++anti_endless_execute_recoursion==ANTI_ENDLESS_EXECUTE_RECOURSION) {
                    424:                                                                anti_endless_execute_recoursion=0; // give @exception a chance
                    425:                                                                THROW(0, 0,
1.160   ! parser    426:                                                                        &frame->name(),
1.159     parser    427:                                                                        "endless recursion detected");
                    428:                                                        }
                    429:                                                        
1.134     paf       430:                                                        execute(*method.parser_code); // execute it
1.159     parser    431:                                                        anti_endless_execute_recoursion--;
                    432:                                                }
1.134     paf       433:                                        else
                    434:                                                THROW(0, 0,
                    435:                                                        &frame->name(),
                    436:                                                        "is not allowed to be called %s", 
                    437:                                                                call_type==Method::CT_STATIC?"statically":"dynamically");
                    438: 
1.47      paf       439:                                }
1.36      paf       440:                                Value *value=wcontext->result();
1.45      paf       441: 
                    442:                                wcontext=static_cast<WContext *>(POP());  
                    443:                                rcontext=POP();  
                    444:                                root=POP();  
                    445:                                self=static_cast<VAliased *>(POP());
1.62      paf       446: 
1.61      paf       447:                                PUSH(value);
1.157     parser    448: #ifdef DEBUG_EXECUTE
1.158     parser    449:                                debug_printf(pool(), "<-returned");
1.157     parser    450: #endif
1.49      paf       451:                                break;
                    452:                        }
                    453: 
1.55      paf       454:                // expression ops: unary
                    455:                case OP_NEG:
                    456:                        {
                    457:                                Value *operand=POP();
1.128     paf       458:                                Value *value=NEW VDouble(pool(), -operand->as_double());
1.55      paf       459:                                PUSH(value);
                    460:                                break;
                    461:                        }
                    462:                case OP_INV:
                    463:                        {
                    464:                                Value *operand=POP();
1.155     parser    465:                                Value *value=NEW VDouble(pool(), ~operand->as_int());
1.55      paf       466:                                PUSH(value);
                    467:                                break;
                    468:                        }
                    469:                case OP_NOT:
                    470:                        {
                    471:                                Value *operand=POP();
1.128     paf       472:                                Value *value=NEW VBool(pool(), !operand->as_bool());
1.55      paf       473:                                PUSH(value);
                    474:                                break;
                    475:                        }
1.62      paf       476:                case OP_DEF:
                    477:                        {
                    478:                                Value *operand=POP();
1.128     paf       479:                                Value *value=NEW VBool(pool(), operand->is_defined());
1.62      paf       480:                                PUSH(value);
                    481:                                break;
                    482:                        }
                    483:                case OP_IN:
                    484:                        {
                    485:                                Value *operand=POP();
1.110     paf       486:                                const char *path=operand->as_string().cstr();
1.151     paf       487:                                Value *value=NEW VBool(pool(), 
                    488:                                        info.uri && strncmp(path, info.uri, strlen(path))==0);
1.62      paf       489:                                PUSH(value);
                    490:                                break;
                    491:                        }
                    492:                case OP_FEXISTS:
                    493:                        {
                    494:                                Value *operand=POP();
1.127     paf       495:                                Value *value=NEW VBool(pool(), 
                    496:                                        file_readable(absolute(operand->as_string())));
1.148     paf       497:                                PUSH(value);
                    498:                                break;
                    499:                        }
                    500:                case OP_DEXISTS:
                    501:                        {
                    502:                                Value *operand=POP();
                    503:                                Value *value=NEW VBool(pool(), 
                    504:                                        dir_readable(absolute(operand->as_string())));
1.62      paf       505:                                PUSH(value);
                    506:                                break;
                    507:                        }
1.55      paf       508: 
                    509:                // expression ops: binary
                    510:                case OP_SUB: 
1.53      paf       511:                        {
1.61      paf       512:                                Value *b=POP();  Value *a=POP();
1.128     paf       513:                                Value *value=NEW VDouble(pool(), a->as_double() - b->as_double());
1.53      paf       514:                                PUSH(value);
                    515:                                break;
                    516:                        }
1.55      paf       517:                case OP_ADD: 
1.53      paf       518:                        {
1.61      paf       519:                                Value *b=POP();  Value *a=POP();
1.128     paf       520:                                Value *value=NEW VDouble(pool(), a->as_double() + b->as_double());
1.53      paf       521:                                PUSH(value);
                    522:                                break;
                    523:                        }
1.49      paf       524:                case OP_MUL: 
                    525:                        {
1.61      paf       526:                                Value *b=POP();  Value *a=POP();
1.128     paf       527:                                Value *value=NEW VDouble(pool(), a->as_double() * b->as_double());
1.53      paf       528:                                PUSH(value);
                    529:                                break;
                    530:                        }
                    531:                case OP_DIV: 
                    532:                        {
1.61      paf       533:                                Value *b=POP();  Value *a=POP();
1.128     paf       534:                                Value *value=NEW VDouble(pool(), a->as_double() / b->as_double());
1.54      paf       535:                                PUSH(value);
                    536:                                break;
                    537:                        }
1.55      paf       538:                case OP_MOD: 
                    539:                        {
1.61      paf       540:                                Value *b=POP();  Value *a=POP();
1.78      paf       541:                                Value *value=NEW VDouble(pool(), 
1.155     parser    542:                                        a->as_int() %
                    543:                                        b->as_int());
1.55      paf       544:                                PUSH(value);
                    545:                                break;
                    546:                        }
                    547:                case OP_BIN_AND:
1.54      paf       548:                        {
1.61      paf       549:                                Value *b=POP();  Value *a=POP();
1.78      paf       550:                                Value *value=NEW VDouble(pool(), 
1.155     parser    551:                                        a->as_int() &
                    552:                                        b->as_int());
1.54      paf       553:                                PUSH(value);
                    554:                                break;
                    555:                        }
1.55      paf       556:                case OP_BIN_OR:
1.54      paf       557:                        {
1.61      paf       558:                                Value *b=POP();  Value *a=POP();
1.78      paf       559:                                Value *value=NEW VDouble(pool(), 
1.155     parser    560:                                        a->as_int() |
                    561:                                        b->as_int());
1.55      paf       562:                                PUSH(value);
                    563:                                break;
                    564:                        }
1.56      paf       565:                case OP_BIN_XOR:
                    566:                        {
1.61      paf       567:                                Value *b=POP();  Value *a=POP();
1.78      paf       568:                                Value *value=NEW VDouble(pool(), 
1.155     parser    569:                                        a->as_int() ^
                    570:                                        b->as_int());
1.56      paf       571:                                PUSH(value);
                    572:                                break;
                    573:                        }
1.55      paf       574:                case OP_LOG_AND:
                    575:                        {
1.61      paf       576:                                Value *b=POP();  Value *a=POP();
1.128     paf       577:                                Value *value=NEW VBool(pool(), a->as_bool() && b->as_bool());
1.55      paf       578:                                PUSH(value);
                    579:                                break;
                    580:                        }
                    581:                case OP_LOG_OR:
                    582:                        {
1.61      paf       583:                                Value *b=POP();  Value *a=POP();
1.128     paf       584:                                Value *value=NEW VBool(pool(), a->as_bool() || b->as_bool());
1.56      paf       585:                                PUSH(value);
                    586:                                break;
                    587:                        }
                    588:                case OP_LOG_XOR:
                    589:                        {
1.61      paf       590:                                Value *b=POP();  Value *a=POP();
1.128     paf       591:                                Value *value=NEW VBool(pool(), a->as_bool() ^ b->as_bool());
1.55      paf       592:                                PUSH(value);
                    593:                                break;
                    594:                        }
                    595:                case OP_NUM_LT: 
                    596:                        {
1.61      paf       597:                                Value *b=POP();  Value *a=POP();
1.128     paf       598:                                Value *value=NEW VBool(pool(), a->as_double() < b->as_double());
1.55      paf       599:                                PUSH(value);
                    600:                                break;
                    601:                        }
                    602:                case OP_NUM_GT: 
                    603:                        {
1.61      paf       604:                                Value *b=POP();  Value *a=POP();
1.128     paf       605:                                Value *value=NEW VBool(pool(), a->as_double() > b->as_double());
1.55      paf       606:                                PUSH(value);
                    607:                                break;
                    608:                        }
                    609:                case OP_NUM_LE: 
                    610:                        {
1.61      paf       611:                                Value *b=POP();  Value *a=POP();
1.128     paf       612:                                Value *value=NEW VBool(pool(), a->as_double() <= b->as_double());
1.55      paf       613:                                PUSH(value);
                    614:                                break;
                    615:                        }
                    616:                case OP_NUM_GE: 
                    617:                        {
1.61      paf       618:                                Value *b=POP();  Value *a=POP();
1.128     paf       619:                                Value *value=NEW VBool(pool(), a->as_double() >= b->as_double());
1.55      paf       620:                                PUSH(value);
                    621:                                break;
                    622:                        }
                    623:                case OP_NUM_EQ: 
                    624:                        {
1.61      paf       625:                                Value *b=POP();  Value *a=POP();
1.128     paf       626:                                Value *value=NEW VBool(pool(), a->as_double() == b->as_double());
1.55      paf       627:                                PUSH(value);
                    628:                                break;
                    629:                        }
                    630:                case OP_NUM_NE: 
                    631:                        {
1.61      paf       632:                                Value *b=POP();  Value *a=POP();
1.128     paf       633:                                Value *value=NEW VBool(pool(), a->as_double() != b->as_double());
1.54      paf       634:                                PUSH(value);
                    635:                                break;
                    636:                        }
1.58      paf       637:                case OP_STR_LT: 
                    638:                        {
1.61      paf       639:                                Value *b=POP();  Value *a=POP();
1.78      paf       640:                                Value *value=NEW VBool(pool(), a->as_string() < b->as_string());
1.58      paf       641:                                PUSH(value);
                    642:                                break;
                    643:                        }
                    644:                case OP_STR_GT: 
                    645:                        {
1.61      paf       646:                                Value *b=POP();  Value *a=POP();
1.78      paf       647:                                Value *value=NEW VBool(pool(), a->as_string() > b->as_string());
1.58      paf       648:                                PUSH(value);
                    649:                                break;
                    650:                        }
1.55      paf       651:                case OP_STR_LE: 
1.58      paf       652:                        {
1.61      paf       653:                                Value *b=POP();  Value *a=POP();
1.78      paf       654:                                Value *value=NEW VBool(pool(), a->as_string() <= b->as_string());
1.58      paf       655:                                PUSH(value);
                    656:                                break;
                    657:                        }
1.55      paf       658:                case OP_STR_GE: 
1.54      paf       659:                        {
1.61      paf       660:                                Value *b=POP();  Value *a=POP();
1.78      paf       661:                                Value *value=NEW VBool(pool(), a->as_string() >= b->as_string());
1.58      paf       662:                                PUSH(value);
                    663:                                break;
                    664:                        }
                    665:                case OP_STR_EQ: 
                    666:                        {
1.61      paf       667:                                Value *b=POP();  Value *a=POP();
1.78      paf       668:                                Value *value=NEW VBool(pool(), a->as_string() == b->as_string());
1.58      paf       669:                                PUSH(value);
                    670:                                break;
                    671:                        }
                    672:                case OP_STR_NE: 
                    673:                        {
1.61      paf       674:                                Value *b=POP();  Value *a=POP();
1.78      paf       675:                                Value *value=NEW VBool(pool(), a->as_string() != b->as_string());
1.103     paf       676:                                PUSH(value);
                    677:                                break;
                    678:                        }
                    679:                case OP_IS:
                    680:                        {
                    681:                                Value *b=POP();  Value *a=POP();
                    682:                                Value *value=NEW VBool(pool(), b->as_string() == a->type());
1.49      paf       683:                                PUSH(value);
1.28      paf       684:                                break;
                    685:                        }
                    686: 
1.11      paf       687:                default:
1.67      paf       688:                        THROW(0,0,
                    689:                                0,
1.139     paf       690:                                "invalid opcode %d", op.code); 
1.11      paf       691:                }
                    692:        }
1.1       paf       693: }
1.17      paf       694: 
                    695: Value *Request::get_element() {
1.82      paf       696:        const String& name=POP_NAME();
1.24      paf       697:        Value *ncontext=POP();
1.147     paf       698:        Value *value;
                    699:        if(Method* method=OP.get_method(name)) { // operator?
                    700:                // as if that method were in self and we have normal dynamic method here
                    701:                Junction& junction=*NEW Junction(pool(), 
                    702:                        *self, self->get_class(), method, 0,0,0,0);
                    703:                value=NEW VJunction(junction);
                    704:        } else
                    705:                value=ncontext->get_element(name);
1.76      paf       706:        if(value)
1.92      paf       707:                value=&process(*value, &name); // process possible code-junction
1.76      paf       708:        else {
                    709:                value=NEW VUnknown(pool());
                    710:                value->set_name(name);
                    711:        }
1.63      paf       712: 
1.17      paf       713:        return value;
1.34      paf       714: }
1.70      paf       715: 
1.159     parser    716: /**    intercept_string:
1.116     paf       717:        - true:
                    718:                they want result=string value, 
                    719:                possible object result goes to wcontext
                    720:        - false:
                    721:                they want any result[string|object]
                    722:                nothing goes to wcontext.
1.125     paf       723:                used in @c (expression) params evaluation
1.116     paf       724: */
1.92      paf       725: Value& Request::process(Value& value, const String *name, bool intercept_string) {
1.91      paf       726:        Value *result;
1.70      paf       727:        Junction *junction=value.get_junction();
                    728:        if(junction && junction->code) { // is it a code-junction?
1.92      paf       729:                // process it
1.157     parser    730: #ifdef DEBUG_EXECUTE
1.158     parser    731:                debug_printf(pool(), "ja->\n");
1.157     parser    732: #endif
1.70      paf       733:                PUSH(self);  
                    734:                PUSH(root);  
                    735:                PUSH(rcontext);  
1.129     paf       736:                PUSH(wcontext);
1.70      paf       737:                
1.71      paf       738:                WContext *frame;
1.109     paf       739:                // for expression method params
                    740:                // wcontext is set 0
                    741:                // using the fact in decision "which wwrapper to use"
                    742:                bool using_code_frame=intercept_string && junction->wcontext;
                    743:                if(using_code_frame) {
1.71      paf       744:                        // almost plain wwrapper about junction wcontext, 
                    745:                        // BUT intercepts string writes
                    746:                        frame=NEW VCodeFrame(pool(), *junction->wcontext);  
                    747:                } else {
                    748:                        // plain wwrapper
1.108     paf       749:                        frame=NEW WWrapper(pool(), 0/*empty*/, false/*not constructing*/);
1.71      paf       750:                }
                    751:                
                    752:                wcontext=frame;
1.70      paf       753:                self=&junction->self;
                    754:                root=junction->root;
                    755:                rcontext=junction->rcontext;
                    756:                execute(*junction->code);
1.109     paf       757:                if(using_code_frame) {
1.71      paf       758:                        // CodeFrame soul:
                    759:                        //   string writes were intercepted
                    760:                        //   returning them as the result of getting code-junction
                    761:                        result=NEW VString(*frame->get_string());
                    762:                } else 
                    763:                        result=frame->result();
1.70      paf       764:                
                    765:                wcontext=static_cast<WContext *>(POP());  
                    766:                rcontext=POP();  
                    767:                root=POP();  
                    768:                self=static_cast<VAliased *>(POP());
                    769:                
1.157     parser    770: #ifdef DEBUG_EXECUTE
1.158     parser    771:                debug_printf(pool(), "<-ja returned");
1.157     parser    772: #endif
1.70      paf       773:        } else
1.91      paf       774:                result=&value;
                    775: 
                    776:        if(name)
                    777:                result->set_name(*name);
                    778:        return *result;
1.85      paf       779: }
                    780: 
1.114     paf       781: const String *Request::execute_method(Value& aself, const Method& method, bool return_cstr) {
1.99      paf       782:        PUSH(self);  
                    783:        PUSH(root);  
                    784:        PUSH(rcontext);  
                    785:        PUSH(wcontext);
                    786:        
                    787:        // initialize contexts
                    788:        root=rcontext=self=&aself;
1.138     paf       789:        wcontext=NEW WWrapper(pool(), &aself, false /*not constructing*/);
1.99      paf       790:        
                    791:        // execute!     
                    792:        execute(*method.parser_code);
                    793:        
                    794:        // result
1.114     paf       795:        const String *result;
1.99      paf       796:        if(return_cstr)
1.114     paf       797:                result=&wcontext->as_string();
1.99      paf       798:        else
                    799:                result=0; // ignore result
                    800:        
                    801:        wcontext=static_cast<WContext *>(POP());  
                    802:        rcontext=POP();  
                    803:        root=POP();  
                    804:        self=static_cast<VAliased *>(POP());
                    805:        
                    806:        // return
                    807:        return result;
                    808: }
                    809: 
1.114     paf       810: const String *Request::execute_method(Value& aself, 
1.99      paf       811:                                                          const String& method_name, bool return_cstr) {
                    812:        if(Value *value=aself.get_element(method_name))
                    813:                if(Junction *junction=value->get_junction())
                    814:                        if(const Method *method=junction->method) 
                    815:                                return execute_method(aself, *method, return_cstr);
                    816: 
1.85      paf       817:        return 0;
1.70      paf       818: }

E-mail: