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

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.188   ! parser      8: static const char *RCSId="$Id: execute.C,v 1.187 2001/07/26 14:15:45 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.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.187     parser    187:                case OP_WITH_ROOT:
                    188:                        {
                    189:                                PUSH(root);
                    190:                                break;
                    191:                        }
1.37      paf       192:                case OP_WITH_SELF: 
                    193:                        {
                    194:                                PUSH(self);
                    195:                                break;
                    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.186     parser    298:                                wcontext=NEW WWrapper(pool(), 0 /*empty*/);
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.186     parser    313:                                wcontext=NEW WWrapper(pool(), 0 /*empty*/);
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.185     parser    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.185     parser    346:                                VMethodFrame *frame=NEW VMethodFrame(pool(), value->name(), *junction);
1.28      paf       347:                                PUSH(frame);
                    348:                                break;
                    349:                        }
                    350:                case OP_STORE_PARAM:
                    351:                        {
                    352:                                Value *value=POP();
1.73      paf       353:                                VMethodFrame *frame=static_cast<VMethodFrame *>(stack.top_value());
1.92      paf       354:                                frame->store_param(frame->name(), value);
1.29      paf       355:                                break;
                    356:                        }
                    357: 
1.186     parser    358:                case OP_PREPARE_TO_CONSTRUCT_OBJECT:
                    359:                        {
                    360:                                wcontext->constructing(true);
                    361:                                break;
                    362:                        }
                    363:                case OP_CALL:
1.29      paf       364:                        {
1.157     parser    365: #ifdef DEBUG_EXECUTE
1.158     parser    366:                                debug_printf(pool(), "->\n");
1.157     parser    367: #endif
1.34      paf       368:                                VMethodFrame *frame=static_cast<VMethodFrame *>(POP());
                    369:                                frame->fill_unspecified_params();
1.45      paf       370:                                PUSH(self);  
                    371:                                PUSH(root);  
                    372:                                PUSH(rcontext);  
                    373:                                PUSH(wcontext); 
                    374:                                
1.100     paf       375:                                VStateless_class *called_class=frame->junction.self.get_class();
1.186     parser    376:                                if(wcontext->constructing()) {
                    377:                                        wcontext->constructing(false);
1.185     parser    378:                                        if(frame->junction.method->call_type!=Method::CT_STATIC) {
1.138     paf       379:                                                // this is a constructor call
1.185     parser    380: 
                    381:                                                if(Value *value=called_class->create_new_value(pool())) {
                    382:                                                        // some stateless_object creatable derivates
1.149     paf       383:                                                        self=value;
1.185     parser    384:                                                } else {
                    385:                                                        // stateful object
1.94      paf       386:                                                        self=NEW VObject(pool(), *called_class);
1.185     parser    387:                                                }
1.83      paf       388:                                                frame->write(*self, 
1.136     paf       389:                                                        String::UL_CLEAN  // not used, always an object, not string
1.83      paf       390:                                                );
1.185     parser    391:                                        } else
                    392:                                                THROW(0, 0,
                    393:                                                        &frame->name(),
                    394:                                                        "method is static and can not be used as constructor");
                    395:                                } else {
                    396:                                        // this is not constructor call
                    397: 
                    398:                                        // not ^name.method call, name:method call; and
                    399:                                        // is context object or class & is it my class or my parent's class and?
                    400:                                        VStateless_class *read_class=rcontext->get_class();
                    401:                                        if(
                    402:                                                !(wcontext->somebody_entered_some_object() &&
                    403:                                                !wcontext->somebody_entered_some_class()) && 
                    404:                                                read_class && read_class->is_or_derived_from(*called_class)) // yes
                    405:                                                self=rcontext; // dynamic call
                    406:                                        else // no, not me or relative of mine (=total stranger)
                    407:                                                self=&frame->junction.self; // static call
                    408:                                }
1.75      paf       409: 
1.45      paf       410:                                frame->set_self(*self);
1.29      paf       411:                                root=rcontext=wcontext=frame;
1.47      paf       412:                                {
1.48      paf       413:                                        // take object or class from any wrappers
1.102     paf       414:                                        // and substitute class alias to the class they are called AS
                    415:                                        Temp_alias temp_alias(*self->get_aliased(), *frame->junction.vclass);
1.68      paf       416: 
1.99      paf       417:                                        const Method& method=*frame->junction.method;
1.134     paf       418:                                        Method::Call_type call_type=
                    419:                                                called_class==self ? Method::CT_STATIC : Method::CT_DYNAMIC;
                    420:                                        if(
                    421:                                                method.call_type==Method::CT_ANY ||
                    422:                                                method.call_type==call_type) // allowed call type?
                    423:                                                if(method.native_code) { // native code?
1.154     paf       424:                                                        method.check_actual_numbered_params(pool(),
1.134     paf       425:                                                                frame->junction.self, 
                    426:                                                                frame->name(), frame->numbered_params());
1.160     parser    427:                                                        method.native_code(
1.134     paf       428:                                                                *this, 
                    429:                                                                frame->name(), frame->numbered_params()); // execute it
1.159     parser    430:                                                } else { // parser code
                    431:                                                        if(++anti_endless_execute_recoursion==ANTI_ENDLESS_EXECUTE_RECOURSION) {
                    432:                                                                anti_endless_execute_recoursion=0; // give @exception a chance
                    433:                                                                THROW(0, 0,
1.160     parser    434:                                                                        &frame->name(),
1.159     parser    435:                                                                        "endless recursion detected");
                    436:                                                        }
                    437:                                                        
1.134     paf       438:                                                        execute(*method.parser_code); // execute it
1.159     parser    439:                                                        anti_endless_execute_recoursion--;
                    440:                                                }
1.134     paf       441:                                        else
                    442:                                                THROW(0, 0,
                    443:                                                        &frame->name(),
                    444:                                                        "is not allowed to be called %s", 
                    445:                                                                call_type==Method::CT_STATIC?"statically":"dynamically");
                    446: 
1.47      paf       447:                                }
1.36      paf       448:                                Value *value=wcontext->result();
1.45      paf       449: 
                    450:                                wcontext=static_cast<WContext *>(POP());  
                    451:                                rcontext=POP();  
                    452:                                root=POP();  
                    453:                                self=static_cast<VAliased *>(POP());
1.62      paf       454: 
1.61      paf       455:                                PUSH(value);
1.157     parser    456: #ifdef DEBUG_EXECUTE
1.158     parser    457:                                debug_printf(pool(), "<-returned");
1.157     parser    458: #endif
1.49      paf       459:                                break;
                    460:                        }
                    461: 
1.55      paf       462:                // expression ops: unary
                    463:                case OP_NEG:
                    464:                        {
                    465:                                Value *operand=POP();
1.128     paf       466:                                Value *value=NEW VDouble(pool(), -operand->as_double());
1.55      paf       467:                                PUSH(value);
                    468:                                break;
                    469:                        }
                    470:                case OP_INV:
                    471:                        {
                    472:                                Value *operand=POP();
1.155     parser    473:                                Value *value=NEW VDouble(pool(), ~operand->as_int());
1.55      paf       474:                                PUSH(value);
                    475:                                break;
                    476:                        }
                    477:                case OP_NOT:
                    478:                        {
                    479:                                Value *operand=POP();
1.128     paf       480:                                Value *value=NEW VBool(pool(), !operand->as_bool());
1.55      paf       481:                                PUSH(value);
                    482:                                break;
                    483:                        }
1.62      paf       484:                case OP_DEF:
                    485:                        {
                    486:                                Value *operand=POP();
1.128     paf       487:                                Value *value=NEW VBool(pool(), operand->is_defined());
1.62      paf       488:                                PUSH(value);
                    489:                                break;
                    490:                        }
                    491:                case OP_IN:
                    492:                        {
                    493:                                Value *operand=POP();
1.110     paf       494:                                const char *path=operand->as_string().cstr();
1.151     paf       495:                                Value *value=NEW VBool(pool(), 
                    496:                                        info.uri && strncmp(path, info.uri, strlen(path))==0);
1.62      paf       497:                                PUSH(value);
                    498:                                break;
                    499:                        }
                    500:                case OP_FEXISTS:
                    501:                        {
                    502:                                Value *operand=POP();
1.127     paf       503:                                Value *value=NEW VBool(pool(), 
                    504:                                        file_readable(absolute(operand->as_string())));
1.148     paf       505:                                PUSH(value);
                    506:                                break;
                    507:                        }
                    508:                case OP_DEXISTS:
                    509:                        {
                    510:                                Value *operand=POP();
                    511:                                Value *value=NEW VBool(pool(), 
                    512:                                        dir_readable(absolute(operand->as_string())));
1.62      paf       513:                                PUSH(value);
                    514:                                break;
                    515:                        }
1.55      paf       516: 
                    517:                // expression ops: binary
                    518:                case OP_SUB: 
1.53      paf       519:                        {
1.61      paf       520:                                Value *b=POP();  Value *a=POP();
1.128     paf       521:                                Value *value=NEW VDouble(pool(), a->as_double() - b->as_double());
1.53      paf       522:                                PUSH(value);
                    523:                                break;
                    524:                        }
1.55      paf       525:                case OP_ADD: 
1.53      paf       526:                        {
1.61      paf       527:                                Value *b=POP();  Value *a=POP();
1.128     paf       528:                                Value *value=NEW VDouble(pool(), a->as_double() + b->as_double());
1.53      paf       529:                                PUSH(value);
                    530:                                break;
                    531:                        }
1.49      paf       532:                case OP_MUL: 
                    533:                        {
1.61      paf       534:                                Value *b=POP();  Value *a=POP();
1.128     paf       535:                                Value *value=NEW VDouble(pool(), a->as_double() * b->as_double());
1.53      paf       536:                                PUSH(value);
                    537:                                break;
                    538:                        }
                    539:                case OP_DIV: 
                    540:                        {
1.61      paf       541:                                Value *b=POP();  Value *a=POP();
1.170     parser    542: 
                    543:                                double a_double=a->as_double();
                    544:                                double b_double=b->as_double();
                    545: 
1.171     parser    546:                                if(b_double == 0) {
                    547:                                        const String *problem_source=&b->as_string();
                    548: #ifndef NO_STRING_ORIGIN
                    549:                                        if(!problem_source->origin().file)
1.172     parser    550:                                                problem_source=&b->name();
1.171     parser    551: #endif
1.170     parser    552:                                        THROW(0, 0,
1.171     parser    553:                                                problem_source,
1.170     parser    554:                                                "Division by zero");
1.171     parser    555:                                }
1.170     parser    556: 
1.171     parser    557:                                Value *value=NEW VDouble(pool(), a_double / b_double);
1.54      paf       558:                                PUSH(value);
                    559:                                break;
                    560:                        }
1.55      paf       561:                case OP_MOD: 
                    562:                        {
1.61      paf       563:                                Value *b=POP();  Value *a=POP();
1.170     parser    564: 
1.173     parser    565:                                double a_double=a->as_double();
                    566:                                double b_double=b->as_double();
1.170     parser    567: 
1.173     parser    568:                                if(b_double == 0) {
1.171     parser    569:                                        const String *problem_source=&b->as_string();
                    570: #ifndef NO_STRING_ORIGIN
                    571:                                        if(!problem_source->origin().file)
1.172     parser    572:                                                problem_source=&b->name();
1.171     parser    573: #endif
1.170     parser    574:                                        THROW(0, 0,
1.171     parser    575:                                                problem_source,
1.170     parser    576:                                                "Modulus by zero");
1.171     parser    577:                                }
1.170     parser    578: 
1.173     parser    579:                                Value *value=NEW VDouble(pool(), fmod(a_double, b_double));
1.55      paf       580:                                PUSH(value);
                    581:                                break;
                    582:                        }
                    583:                case OP_BIN_AND:
1.54      paf       584:                        {
1.61      paf       585:                                Value *b=POP();  Value *a=POP();
1.78      paf       586:                                Value *value=NEW VDouble(pool(), 
1.155     parser    587:                                        a->as_int() &
                    588:                                        b->as_int());
1.54      paf       589:                                PUSH(value);
                    590:                                break;
                    591:                        }
1.55      paf       592:                case OP_BIN_OR:
1.54      paf       593:                        {
1.61      paf       594:                                Value *b=POP();  Value *a=POP();
1.78      paf       595:                                Value *value=NEW VDouble(pool(), 
1.155     parser    596:                                        a->as_int() |
                    597:                                        b->as_int());
1.55      paf       598:                                PUSH(value);
                    599:                                break;
                    600:                        }
1.56      paf       601:                case OP_BIN_XOR:
                    602:                        {
1.61      paf       603:                                Value *b=POP();  Value *a=POP();
1.78      paf       604:                                Value *value=NEW VDouble(pool(), 
1.155     parser    605:                                        a->as_int() ^
                    606:                                        b->as_int());
1.56      paf       607:                                PUSH(value);
                    608:                                break;
                    609:                        }
1.55      paf       610:                case OP_LOG_AND:
                    611:                        {
1.61      paf       612:                                Value *b=POP();  Value *a=POP();
1.128     paf       613:                                Value *value=NEW VBool(pool(), a->as_bool() && b->as_bool());
1.55      paf       614:                                PUSH(value);
                    615:                                break;
                    616:                        }
                    617:                case OP_LOG_OR:
                    618:                        {
1.61      paf       619:                                Value *b=POP();  Value *a=POP();
1.128     paf       620:                                Value *value=NEW VBool(pool(), a->as_bool() || b->as_bool());
1.56      paf       621:                                PUSH(value);
                    622:                                break;
                    623:                        }
                    624:                case OP_LOG_XOR:
                    625:                        {
1.61      paf       626:                                Value *b=POP();  Value *a=POP();
1.128     paf       627:                                Value *value=NEW VBool(pool(), a->as_bool() ^ b->as_bool());
1.55      paf       628:                                PUSH(value);
                    629:                                break;
                    630:                        }
                    631:                case OP_NUM_LT: 
                    632:                        {
1.61      paf       633:                                Value *b=POP();  Value *a=POP();
1.128     paf       634:                                Value *value=NEW VBool(pool(), a->as_double() < b->as_double());
1.55      paf       635:                                PUSH(value);
                    636:                                break;
                    637:                        }
                    638:                case OP_NUM_GT: 
                    639:                        {
1.61      paf       640:                                Value *b=POP();  Value *a=POP();
1.128     paf       641:                                Value *value=NEW VBool(pool(), a->as_double() > b->as_double());
1.55      paf       642:                                PUSH(value);
                    643:                                break;
                    644:                        }
                    645:                case OP_NUM_LE: 
                    646:                        {
1.61      paf       647:                                Value *b=POP();  Value *a=POP();
1.128     paf       648:                                Value *value=NEW VBool(pool(), a->as_double() <= b->as_double());
1.55      paf       649:                                PUSH(value);
                    650:                                break;
                    651:                        }
                    652:                case OP_NUM_GE: 
                    653:                        {
1.61      paf       654:                                Value *b=POP();  Value *a=POP();
1.128     paf       655:                                Value *value=NEW VBool(pool(), a->as_double() >= b->as_double());
1.55      paf       656:                                PUSH(value);
                    657:                                break;
                    658:                        }
                    659:                case OP_NUM_EQ: 
                    660:                        {
1.61      paf       661:                                Value *b=POP();  Value *a=POP();
1.128     paf       662:                                Value *value=NEW VBool(pool(), a->as_double() == b->as_double());
1.55      paf       663:                                PUSH(value);
                    664:                                break;
                    665:                        }
                    666:                case OP_NUM_NE: 
                    667:                        {
1.61      paf       668:                                Value *b=POP();  Value *a=POP();
1.128     paf       669:                                Value *value=NEW VBool(pool(), a->as_double() != b->as_double());
1.54      paf       670:                                PUSH(value);
                    671:                                break;
                    672:                        }
1.58      paf       673:                case OP_STR_LT: 
                    674:                        {
1.61      paf       675:                                Value *b=POP();  Value *a=POP();
1.78      paf       676:                                Value *value=NEW VBool(pool(), a->as_string() < b->as_string());
1.58      paf       677:                                PUSH(value);
                    678:                                break;
                    679:                        }
                    680:                case OP_STR_GT: 
                    681:                        {
1.61      paf       682:                                Value *b=POP();  Value *a=POP();
1.78      paf       683:                                Value *value=NEW VBool(pool(), a->as_string() > b->as_string());
1.58      paf       684:                                PUSH(value);
                    685:                                break;
                    686:                        }
1.55      paf       687:                case OP_STR_LE: 
1.58      paf       688:                        {
1.61      paf       689:                                Value *b=POP();  Value *a=POP();
1.78      paf       690:                                Value *value=NEW VBool(pool(), a->as_string() <= b->as_string());
1.58      paf       691:                                PUSH(value);
                    692:                                break;
                    693:                        }
1.55      paf       694:                case OP_STR_GE: 
1.54      paf       695:                        {
1.61      paf       696:                                Value *b=POP();  Value *a=POP();
1.78      paf       697:                                Value *value=NEW VBool(pool(), a->as_string() >= b->as_string());
1.58      paf       698:                                PUSH(value);
                    699:                                break;
                    700:                        }
                    701:                case OP_STR_EQ: 
                    702:                        {
1.61      paf       703:                                Value *b=POP();  Value *a=POP();
1.78      paf       704:                                Value *value=NEW VBool(pool(), a->as_string() == b->as_string());
1.58      paf       705:                                PUSH(value);
                    706:                                break;
                    707:                        }
                    708:                case OP_STR_NE: 
                    709:                        {
1.61      paf       710:                                Value *b=POP();  Value *a=POP();
1.78      paf       711:                                Value *value=NEW VBool(pool(), a->as_string() != b->as_string());
1.103     paf       712:                                PUSH(value);
                    713:                                break;
                    714:                        }
                    715:                case OP_IS:
                    716:                        {
                    717:                                Value *b=POP();  Value *a=POP();
                    718:                                Value *value=NEW VBool(pool(), b->as_string() == a->type());
1.49      paf       719:                                PUSH(value);
1.28      paf       720:                                break;
                    721:                        }
                    722: 
1.11      paf       723:                default:
1.67      paf       724:                        THROW(0,0,
                    725:                                0,
1.139     paf       726:                                "invalid opcode %d", op.code); 
1.11      paf       727:                }
                    728:        }
1.1       paf       729: }
1.17      paf       730: 
                    731: Value *Request::get_element() {
1.82      paf       732:        const String& name=POP_NAME();
1.24      paf       733:        Value *ncontext=POP();
1.181     parser    734:        Value *value=ncontext->get_element(name);
                    735:        if(!value)
                    736:                if(Method* method=OP.get_method(name)) { // maybe operator?
                    737:                        // as if that method were in self and we have normal dynamic method here
                    738:                        Junction& junction=*NEW Junction(pool(), 
                    739:                                *self, self->get_class(), method, 0,0,0,0);
                    740:                        value=NEW VJunction(junction);
                    741:                }
1.76      paf       742:        if(value)
1.92      paf       743:                value=&process(*value, &name); // process possible code-junction
1.76      paf       744:        else {
1.167     parser    745:                value=NEW VVoid(pool());
1.76      paf       746:                value->set_name(name);
                    747:        }
1.63      paf       748: 
1.17      paf       749:        return value;
1.34      paf       750: }
1.70      paf       751: 
1.162     parser    752: /**    @param intercept_string
1.116     paf       753:        - true:
                    754:                they want result=string value, 
                    755:                possible object result goes to wcontext
                    756:        - false:
                    757:                they want any result[string|object]
                    758:                nothing goes to wcontext.
1.125     paf       759:                used in @c (expression) params evaluation
1.116     paf       760: */
1.92      paf       761: Value& Request::process(Value& value, const String *name, bool intercept_string) {
1.91      paf       762:        Value *result;
1.70      paf       763:        Junction *junction=value.get_junction();
                    764:        if(junction && junction->code) { // is it a code-junction?
1.92      paf       765:                // process it
1.157     parser    766: #ifdef DEBUG_EXECUTE
1.158     parser    767:                debug_printf(pool(), "ja->\n");
1.157     parser    768: #endif
1.70      paf       769:                PUSH(self);  
                    770:                PUSH(root);  
                    771:                PUSH(rcontext);  
1.129     paf       772:                PUSH(wcontext);
1.70      paf       773:                
1.71      paf       774:                WContext *frame;
1.109     paf       775:                // for expression method params
                    776:                // wcontext is set 0
                    777:                // using the fact in decision "which wwrapper to use"
                    778:                bool using_code_frame=intercept_string && junction->wcontext;
                    779:                if(using_code_frame) {
1.71      paf       780:                        // almost plain wwrapper about junction wcontext, 
                    781:                        // BUT intercepts string writes
                    782:                        frame=NEW VCodeFrame(pool(), *junction->wcontext);  
                    783:                } else {
                    784:                        // plain wwrapper
1.186     parser    785:                        frame=NEW WWrapper(pool(), 0/*empty*/);
1.71      paf       786:                }
                    787:                
1.183     parser    788:                //frame->set_name(value.name());
1.71      paf       789:                wcontext=frame;
1.70      paf       790:                self=&junction->self;
                    791:                root=junction->root;
                    792:                rcontext=junction->rcontext;
                    793:                execute(*junction->code);
1.109     paf       794:                if(using_code_frame) {
1.71      paf       795:                        // CodeFrame soul:
                    796:                        //   string writes were intercepted
                    797:                        //   returning them as the result of getting code-junction
                    798:                        result=NEW VString(*frame->get_string());
                    799:                } else 
                    800:                        result=frame->result();
1.70      paf       801:                
                    802:                wcontext=static_cast<WContext *>(POP());  
                    803:                rcontext=POP();  
                    804:                root=POP();  
                    805:                self=static_cast<VAliased *>(POP());
                    806:                
1.157     parser    807: #ifdef DEBUG_EXECUTE
1.158     parser    808:                debug_printf(pool(), "<-ja returned");
1.157     parser    809: #endif
1.70      paf       810:        } else
1.91      paf       811:                result=&value;
                    812: 
                    813:        if(name)
                    814:                result->set_name(*name);
                    815:        return *result;
1.85      paf       816: }
                    817: 
1.176     parser    818: const String *Request::execute_method(Value& aself, const Method& method, 
                    819:                                                                          bool return_cstr) {
1.99      paf       820:        PUSH(self);  
                    821:        PUSH(root);  
                    822:        PUSH(rcontext);  
                    823:        PUSH(wcontext);
                    824:        
                    825:        // initialize contexts
                    826:        root=rcontext=self=&aself;
1.186     parser    827:        wcontext=NEW WWrapper(pool(), &aself);
1.99      paf       828:        
                    829:        // execute!     
                    830:        execute(*method.parser_code);
                    831:        
                    832:        // result
1.114     paf       833:        const String *result;
1.99      paf       834:        if(return_cstr)
1.114     paf       835:                result=&wcontext->as_string();
1.99      paf       836:        else
                    837:                result=0; // ignore result
                    838:        
                    839:        wcontext=static_cast<WContext *>(POP());  
                    840:        rcontext=POP();  
                    841:        root=POP();  
                    842:        self=static_cast<VAliased *>(POP());
                    843:        
                    844:        // return
                    845:        return result;
                    846: }
                    847: 
1.176     parser    848: const String *Request::execute_virtual_method(Value& aself, 
                    849:                                                                                          const String& method_name, 
                    850:                                                                                          bool return_cstr) {
1.99      paf       851:        if(Value *value=aself.get_element(method_name))
                    852:                if(Junction *junction=value->get_junction())
                    853:                        if(const Method *method=junction->method) 
                    854:                                return execute_method(aself, *method, return_cstr);
1.188   ! parser    855:                        
1.176     parser    856:        return 0;
                    857: }
                    858: 
1.178     parser    859: const String *Request::execute_nonvirtual_method(VStateless_class& aclass, 
1.176     parser    860:                                                                                                 const String& method_name, 
                    861:                                                                                                 bool return_cstr) {
1.178     parser    862:        if(const Method *method=aclass.get_method(method_name))
1.179     parser    863:                return execute_method(aclass, *method, return_cstr);
1.99      paf       864: 
1.85      paf       865:        return 0;
1.70      paf       866: }

E-mail: