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

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

E-mail: