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

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

E-mail: