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

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

E-mail: