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

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

E-mail: