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

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

E-mail: