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

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

E-mail: