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

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

E-mail: