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

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

E-mail: