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

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

E-mail: