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

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

E-mail: