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

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

E-mail: