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

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

E-mail: