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

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.243     paf         6: */
1.118     paf         7: 
1.255   ! paf         8: static const char* IDENT_EXECUTE_C="$Date: 2002/08/15 07:53:06 $";
1.1       paf         9: 
1.152     paf        10: #include "pa_opcode.h"
1.8       paf        11: #include "pa_array.h" 
1.11      paf        12: #include "pa_request.h"
1.15      paf        13: #include "pa_vstring.h"
1.22      paf        14: #include "pa_vhash.h"
1.167     parser     15: #include "pa_vvoid.h"
1.145     paf        16: #include "pa_vcode_frame.h"
                     17: #include "pa_vmethod_frame.h"
1.38      paf        18: #include "pa_vobject.h"
1.49      paf        19: #include "pa_vdouble.h"
1.54      paf        20: #include "pa_vbool.h"
1.94      paf        21: #include "pa_vtable.h"
1.132     paf        22: #include "pa_vfile.h"
1.144     paf        23: #include "pa_vimage.h"
1.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.251     paf       234:                                ncontext->put_element(name, value, false);
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.251     paf       245:                                ncontext->put_element(name, value->as_expr_result(), false);
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();
1.251     paf       266:                                ncontext->put_element(name, value, false);
1.182     parser    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:                                //_asm int 3;
1.236     paf       308:                                value=get_element(last_get_element_name, true);
1.215     paf       309:                                PUSH(value);
                    310:                                break;
                    311:                        }
1.15      paf       312:                case OP_GET_ELEMENT:
1.11      paf       313:                        {
1.215     paf       314:                                //_asm int 3;
1.236     paf       315:                                value=get_element(last_get_element_name, false);
1.24      paf       316:                                PUSH(value);
1.17      paf       317:                                break;
                    318:                        }
                    319: 
1.18      paf       320:                case OP_GET_ELEMENT__WRITE:
1.17      paf       321:                        {
1.236     paf       322:                                value=get_element(last_get_element_name/*not followed by call, not needed really*/, false);
                    323:                                write_assign_lang(*value, last_get_element_name);
1.17      paf       324:                                break;
                    325:                        }
                    326: 
1.32      paf       327: 
1.226     paf       328:                case OP_OBJECT_POOL:
1.17      paf       329:                        {
1.226     paf       330:                                const Array *local_ops=reinterpret_cast<const Array *>(i.next());
                    331:                                
1.24      paf       332:                                PUSH(wcontext);
1.137     paf       333:                                PUSH((void *)flang);
                    334:                                flang=String::UL_PASS_APPENDED;
1.226     paf       335:                                WWrapper local(pool(), 0 /*empty*/);
                    336:                                wcontext=&local;
                    337: 
                    338:                                execute(*local_ops);
                    339: 
1.228     paf       340:                                value=&wcontext->result().as_value();
1.137     paf       341:                                flang=static_cast<String::Untaint_lang>(reinterpret_cast<int>(POP()));
1.25      paf       342:                                wcontext=static_cast<WContext *>(POP());
1.24      paf       343:                                PUSH(value);
1.13      paf       344:                                break;
1.15      paf       345:                        }
1.13      paf       346:                        
1.226     paf       347:                case OP_STRING_POOL:
1.49      paf       348:                        {
1.226     paf       349:                                const Array *local_ops=reinterpret_cast<const Array *>(i.next());
                    350: 
1.49      paf       351:                                PUSH(wcontext);
1.226     paf       352:                                WWrapper local(pool(), 0 /*empty*/);
                    353:                                wcontext=&local;
                    354: 
                    355:                                execute(*local_ops);
                    356: 
1.49      paf       357:                                // from "$a $b" part of expression taking only string value,
                    358:                                // ignoring any other content of wcontext
1.82      paf       359:                                const String *string=wcontext->get_string();
1.80      paf       360:                                Value *value;
                    361:                                if(string)
                    362:                                        value=NEW VString(*string);
                    363:                                else
1.167     parser    364:                                        NEW VVoid(pool());
1.50      paf       365:                                wcontext=static_cast<WContext *>(POP());
1.49      paf       366:                                PUSH(value);
                    367:                                break;
                    368:                        }
                    369: 
1.51      paf       370:                // CALL
1.237     paf       371:                case OP_STORE_PARAM:
1.28      paf       372:                        {
1.197     parser    373:                                value=POP();
1.237     paf       374:                                VMethodFrame *frame=static_cast<VMethodFrame *>(stack.top_value());
                    375:                                // this op is executed from CALL local_ops only, so can not check method_frame_to_fill==0
                    376:                                frame->store_param(value);
1.28      paf       377:                                break;
                    378:                        }
1.237     paf       379:                case OP_CURLY_CODE__STORE_PARAM:
                    380:                case OP_EXPR_CODE__STORE_PARAM:
1.28      paf       381:                        {
1.237     paf       382:                                // code
                    383:                                const Array *local_ops=reinterpret_cast<const Array *>(i.next());
1.73      paf       384:                                VMethodFrame *frame=static_cast<VMethodFrame *>(stack.top_value());
1.237     paf       385: #ifdef DEBUG_EXECUTE
                    386:                                debug_printf(pool(), " (%d)\n", local_ops->size());
                    387:                                debug_dump(pool(), 1, *local_ops);
                    388: #endif                         
                    389:                                // when they evaluate expression parameter,
                    390:                                // the object expression result
                    391:                                // does not need to be written into calling frame
                    392:                                // it must go into any expressions using that parameter
                    393:                                // hence, we zero junction.wcontext here, and later
                    394:                                // in .process we would test that field 
                    395:                                // in decision "which wwrapper to use"
                    396:                                Junction& j=*NEW Junction(pool(), 
                    397:                                        *self, 0, 0,
                    398:                                        root, 
                    399:                                        rcontext, 
                    400:                                        op.code==OP_EXPR_CODE__STORE_PARAM?0:wcontext, 
                    401:                                        local_ops);
                    402:                                
                    403:                                value=NEW VJunction(j);
                    404: 
                    405:                                // store param
                    406:                                // this op is executed from CALL local_ops only, so can not check method_frame_to_fill==0
                    407:                                frame->store_param(value);
1.29      paf       408:                                break;
                    409:                        }
                    410: 
1.186     parser    411:                case OP_PREPARE_TO_CONSTRUCT_OBJECT:
                    412:                        {
1.200     paf       413:                                wcontext->set_constructing(true);
1.186     parser    414:                                break;
                    415:                        }
1.219     paf       416: 
                    417:                case OP_PREPARE_TO_EXPRESSION:
                    418:                        {
                    419:                                wcontext->set_in_expression(true);
                    420:                                break;
                    421:                        }
                    422: 
1.186     parser    423:                case OP_CALL:
1.232     paf       424:                case OP_CALL__WRITE:
1.29      paf       425:                        {
1.237     paf       426:                                Array *local_ops=static_cast<Array *>(i.next());
1.157     parser    427: #ifdef DEBUG_EXECUTE
1.237     paf       428:                                debug_printf(pool(), " (%d)\n", local_ops->size());
                    429:                                debug_dump(pool(), 1, *local_ops);
                    430: 
1.158     parser    431:                                debug_printf(pool(), "->\n");
1.157     parser    432: #endif
1.237     paf       433:                                value=POP();
                    434: 
                    435:                                // info: 
                    436:                                //      code compiled so that this one's always method-junction, 
                    437:                                //      not a code-junction
                    438:                                Junction *junction=value->get_junction();
                    439:                                if(!junction)
                    440:                                        throw Exception("parser.runtime",
                    441:                                                last_get_element_name, 
                    442:                                                "(%s) not a method or junction, can not call it",
                    443:                                                        value->type());
                    444: 
                    445:                                VMethodFrame frame(pool(), *last_get_element_name, *junction);
                    446:                                if(local_ops){ // store param code goes here
                    447:                                        PUSH(&frame); // argument for *STORE_PARAM ops
                    448:                                        execute(*local_ops);
                    449:                                        POP();
                    450:                                }
                    451:                                frame.fill_unspecified_params();
1.45      paf       452:                                PUSH(self);  
                    453:                                PUSH(root);  
                    454:                                PUSH(rcontext);  
                    455:                                PUSH(wcontext); 
                    456:                                
1.237     paf       457:                                VStateless_class *called_class=frame.junction.self.get_class();
1.200     paf       458:                                if(wcontext->get_constructing()) {
                    459:                                        wcontext->set_constructing(false);
1.237     paf       460:                                        if(frame.junction.method->call_type!=Method::CT_STATIC) {
1.138     paf       461:                                                // this is a constructor call
1.185     parser    462: 
                    463:                                                if(Value *value=called_class->create_new_value(pool())) {
1.204     paf       464:                                                        // some stateless_class creatable derivates
1.149     paf       465:                                                        self=value;
1.204     paf       466:                                                } else 
1.223     paf       467:                                                        throw Exception("parser.runtime",
1.237     paf       468:                                                                &frame.name(),
1.204     paf       469:                                                                "is not a constructor, system class '%s' can be constructed only implicitly", 
                    470:                                                                called_class->name().cstr());
                    471: 
1.237     paf       472:                                                frame.write(*self, 
1.136     paf       473:                                                        String::UL_CLEAN  // not used, always an object, not string
1.83      paf       474:                                                );
1.185     parser    475:                                        } else
1.223     paf       476:                                                throw Exception("parser.runtime",
1.237     paf       477:                                                        &frame.name(),
1.185     parser    478:                                                        "method is static and can not be used as constructor");
1.249     paf       479:                                } else
1.250     paf       480:                                        self=&frame.junction.self;
1.75      paf       481: 
1.237     paf       482:                                frame.set_self(*self);
1.219     paf       483: 
                    484:                                // see OP_PREPARE_TO_EXPRESSION
1.237     paf       485:                                frame.set_in_expression(wcontext->get_in_expression());
1.219     paf       486:                                
1.237     paf       487:                                rcontext=wcontext=&frame;
1.47      paf       488:                                {
1.237     paf       489:                                        const Method& method=*frame.junction.method;
1.134     paf       490:                                        Method::Call_type call_type=
                    491:                                                called_class==self ? Method::CT_STATIC : Method::CT_DYNAMIC;
                    492:                                        if(
                    493:                                                method.call_type==Method::CT_ANY ||
1.197     parser    494:                                                method.call_type==call_type) { // allowed call type?
                    495:                                                try {
                    496:                                                        if(method.native_code) { // native code?
1.202     paf       497:                                                                // root unchanged, so that ^for ^foreach & co may write to locals
1.197     parser    498:                                                                method.check_actual_numbered_params(
1.237     paf       499:                                                                        frame.junction.self, 
                    500:                                                                        frame.name(), frame.numbered_params());
1.197     parser    501:                                                                method.native_code(
                    502:                                                                        *this, 
1.237     paf       503:                                                                        frame.name(), frame.numbered_params()); // execute it
1.197     parser    504:                                                        } else { // parser code
1.237     paf       505:                                                                root=&frame;
1.225     paf       506:                                                                // execute it
1.237     paf       507:                                                                recoursion_checked_execute(&frame.name(), *method.parser_code);
1.159     parser    508:                                                        }
1.197     parser    509:                                                } catch(...) {
                    510:                                                        // record it to stack trace
1.237     paf       511:                                                        exception_trace.push((void *)&frame.name());
1.197     parser    512:                                                        /*re*/throw;
1.159     parser    513:                                                }
1.197     parser    514:                                        } else
1.223     paf       515:                                                throw Exception("parser.runtime",
1.237     paf       516:                                                        &frame.name(),
1.134     paf       517:                                                        "is not allowed to be called %s", 
                    518:                                                                call_type==Method::CT_STATIC?"statically":"dynamically");
                    519: 
1.47      paf       520:                                }
1.232     paf       521:                                StringOrValue result=wcontext->result();
1.45      paf       522: 
                    523:                                wcontext=static_cast<WContext *>(POP());  
                    524:                                rcontext=POP();  
                    525:                                root=POP();  
1.248     paf       526:                                self=static_cast<Value *>(POP());
1.220     paf       527: 
                    528: #ifdef DEBUG_STRING_APPENDS_VS_EXPANDS
                    529:                                if(const String *s=value->get_string())
                    530:                                        wcontext_result_size+=s->used_rows()*sizeof(String::Chunk::Row);
                    531: #endif
1.213     paf       532: 
1.232     paf       533:                                if(op.code==OP_CALL__WRITE) {
1.236     paf       534:                                        write_assign_lang(result, last_get_element_name);
1.232     paf       535:                                } else { // OP_CALL
                    536:                                        PUSH(&result.as_value());
                    537:                                }
                    538:                                
1.157     parser    539: #ifdef DEBUG_EXECUTE
1.158     parser    540:                                debug_printf(pool(), "<-returned");
1.157     parser    541: #endif
1.49      paf       542:                                break;
                    543:                        }
                    544: 
1.55      paf       545:                // expression ops: unary
                    546:                case OP_NEG:
                    547:                        {
                    548:                                Value *operand=POP();
1.197     parser    549:                                value=NEW VDouble(pool(), -operand->as_double());
1.55      paf       550:                                PUSH(value);
                    551:                                break;
                    552:                        }
                    553:                case OP_INV:
                    554:                        {
                    555:                                Value *operand=POP();
1.197     parser    556:                                value=NEW VDouble(pool(), ~operand->as_int());
1.55      paf       557:                                PUSH(value);
                    558:                                break;
                    559:                        }
                    560:                case OP_NOT:
                    561:                        {
                    562:                                Value *operand=POP();
1.197     parser    563:                                value=NEW VBool(pool(), !operand->as_bool());
1.55      paf       564:                                PUSH(value);
                    565:                                break;
                    566:                        }
1.62      paf       567:                case OP_DEF:
                    568:                        {
                    569:                                Value *operand=POP();
1.197     parser    570:                                value=NEW VBool(pool(), operand->is_defined());
1.62      paf       571:                                PUSH(value);
                    572:                                break;
                    573:                        }
                    574:                case OP_IN:
                    575:                        {
1.199     paf       576:                                /// @test String::cmp
1.62      paf       577:                                Value *operand=POP();
1.110     paf       578:                                const char *path=operand->as_string().cstr();
1.197     parser    579:                                value=NEW VBool(pool(), 
1.151     paf       580:                                        info.uri && strncmp(path, info.uri, strlen(path))==0);
1.62      paf       581:                                PUSH(value);
                    582:                                break;
                    583:                        }
                    584:                case OP_FEXISTS:
                    585:                        {
                    586:                                Value *operand=POP();
1.197     parser    587:                                value=NEW VBool(pool(), 
1.127     paf       588:                                        file_readable(absolute(operand->as_string())));
1.148     paf       589:                                PUSH(value);
                    590:                                break;
                    591:                        }
                    592:                case OP_DEXISTS:
                    593:                        {
                    594:                                Value *operand=POP();
1.197     parser    595:                                value=NEW VBool(pool(), 
1.148     paf       596:                                        dir_readable(absolute(operand->as_string())));
1.62      paf       597:                                PUSH(value);
                    598:                                break;
                    599:                        }
1.55      paf       600: 
                    601:                // expression ops: binary
                    602:                case OP_SUB: 
1.53      paf       603:                        {
1.197     parser    604:                                b=POP();  a=POP();
                    605:                                value=NEW VDouble(pool(), a->as_double() - b->as_double());
1.53      paf       606:                                PUSH(value);
                    607:                                break;
                    608:                        }
1.55      paf       609:                case OP_ADD: 
1.53      paf       610:                        {
1.197     parser    611:                                b=POP();  a=POP();
                    612:                                value=NEW VDouble(pool(), a->as_double() + b->as_double());
1.53      paf       613:                                PUSH(value);
                    614:                                break;
                    615:                        }
1.49      paf       616:                case OP_MUL: 
                    617:                        {
1.197     parser    618:                                b=POP();  a=POP();
                    619:                                value=NEW VDouble(pool(), a->as_double() * b->as_double());
1.53      paf       620:                                PUSH(value);
                    621:                                break;
                    622:                        }
                    623:                case OP_DIV: 
                    624:                        {
1.197     parser    625:                                b=POP();  a=POP();
1.170     parser    626: 
                    627:                                double a_double=a->as_double();
                    628:                                double b_double=b->as_double();
                    629: 
1.171     parser    630:                                if(b_double == 0) {
                    631:                                        const String *problem_source=&b->as_string();
1.223     paf       632:                                        throw Exception("number.zerodivision",
1.171     parser    633:                                                problem_source,
1.170     parser    634:                                                "Division by zero");
1.171     parser    635:                                }
1.170     parser    636: 
1.197     parser    637:                                value=NEW VDouble(pool(), a_double / b_double);
1.54      paf       638:                                PUSH(value);
                    639:                                break;
                    640:                        }
1.55      paf       641:                case OP_MOD: 
                    642:                        {
1.197     parser    643:                                b=POP();  a=POP();
1.170     parser    644: 
1.173     parser    645:                                double a_double=a->as_double();
                    646:                                double b_double=b->as_double();
1.170     parser    647: 
1.173     parser    648:                                if(b_double == 0) {
1.171     parser    649:                                        const String *problem_source=&b->as_string();
1.223     paf       650:                                        throw Exception("number.zerodivision",
1.171     parser    651:                                                problem_source,
1.170     parser    652:                                                "Modulus by zero");
1.171     parser    653:                                }
1.170     parser    654: 
1.197     parser    655:                                value=NEW VDouble(pool(), fmod(a_double, b_double));
1.201     paf       656:                                PUSH(value);
                    657:                                break;
                    658:                        }
                    659:                case OP_INTDIV:
                    660:                        {
                    661:                                b=POP();  a=POP();
                    662: 
                    663:                                int a_int=a->as_int();
                    664:                                int b_int=b->as_int();
                    665: 
                    666:                                if(b_int == 0) {
                    667:                                        const String *problem_source=&b->as_string();
1.223     paf       668:                                        throw Exception("number.zerodivision",
1.201     paf       669:                                                problem_source,
                    670:                                                "Division by zero");
                    671:                                }
                    672: 
                    673:                                value=NEW VInt(pool(), a_int / b_int);
1.55      paf       674:                                PUSH(value);
                    675:                                break;
                    676:                        }
                    677:                case OP_BIN_AND:
1.54      paf       678:                        {
1.197     parser    679:                                b=POP();  a=POP();
                    680:                                value=NEW VDouble(pool(), 
1.155     parser    681:                                        a->as_int() &
                    682:                                        b->as_int());
1.54      paf       683:                                PUSH(value);
                    684:                                break;
                    685:                        }
1.55      paf       686:                case OP_BIN_OR:
1.54      paf       687:                        {
1.197     parser    688:                                b=POP();  a=POP();
                    689:                                value=NEW VDouble(pool(), 
1.155     parser    690:                                        a->as_int() |
                    691:                                        b->as_int());
1.55      paf       692:                                PUSH(value);
                    693:                                break;
                    694:                        }
1.56      paf       695:                case OP_BIN_XOR:
                    696:                        {
1.197     parser    697:                                b=POP();  a=POP();
                    698:                                value=NEW VDouble(pool(), 
1.155     parser    699:                                        a->as_int() ^
                    700:                                        b->as_int());
1.56      paf       701:                                PUSH(value);
                    702:                                break;
                    703:                        }
1.55      paf       704:                case OP_LOG_AND:
                    705:                        {
1.209     paf       706:                                b_code=POP_CODE();  a=POP();
                    707:                                bool result;
                    708:                                if(a->as_bool()) {
                    709:                                        execute(*b_code);
                    710:                                        b=POP();
                    711:                                        result=b->as_bool();
                    712:                                } else
                    713:                                        result=false;
                    714:                                value=NEW VBool(pool(), result);
1.55      paf       715:                                PUSH(value);
                    716:                                break;
                    717:                        }
                    718:                case OP_LOG_OR:
                    719:                        {
1.209     paf       720:                                b_code=POP_CODE();  a=POP();
                    721:                                bool result;
                    722:                                if(a->as_bool()) 
                    723:                                        result=true;
                    724:                                else {
                    725:                                        execute(*b_code);
                    726:                                        b=POP();
                    727:                                        result=b->as_bool();
                    728:                                }
                    729:                                value=NEW VBool(pool(), result);
1.56      paf       730:                                PUSH(value);
                    731:                                break;
                    732:                        }
                    733:                case OP_LOG_XOR:
                    734:                        {
1.197     parser    735:                                b=POP();  a=POP();
                    736:                                value=NEW VBool(pool(), a->as_bool() ^ b->as_bool());
1.55      paf       737:                                PUSH(value);
                    738:                                break;
                    739:                        }
                    740:                case OP_NUM_LT: 
                    741:                        {
1.197     parser    742:                                b=POP();  a=POP();
                    743:                                value=NEW VBool(pool(), a->as_double() < b->as_double());
1.55      paf       744:                                PUSH(value);
                    745:                                break;
                    746:                        }
                    747:                case OP_NUM_GT: 
                    748:                        {
1.197     parser    749:                                b=POP();  a=POP();
                    750:                                value=NEW VBool(pool(), a->as_double() > b->as_double());
1.55      paf       751:                                PUSH(value);
                    752:                                break;
                    753:                        }
                    754:                case OP_NUM_LE: 
                    755:                        {
1.197     parser    756:                                b=POP();  a=POP();
                    757:                                value=NEW VBool(pool(), a->as_double() <= b->as_double());
1.55      paf       758:                                PUSH(value);
                    759:                                break;
                    760:                        }
                    761:                case OP_NUM_GE: 
                    762:                        {
1.197     parser    763:                                b=POP();  a=POP();
                    764:                                value=NEW VBool(pool(), a->as_double() >= b->as_double());
1.55      paf       765:                                PUSH(value);
                    766:                                break;
                    767:                        }
                    768:                case OP_NUM_EQ: 
                    769:                        {
1.197     parser    770:                                b=POP();  a=POP();
                    771:                                value=NEW VBool(pool(), a->as_double() == b->as_double());
1.55      paf       772:                                PUSH(value);
                    773:                                break;
                    774:                        }
                    775:                case OP_NUM_NE: 
                    776:                        {
1.197     parser    777:                                b=POP();  a=POP();
                    778:                                value=NEW VBool(pool(), a->as_double() != b->as_double());
1.54      paf       779:                                PUSH(value);
                    780:                                break;
                    781:                        }
1.58      paf       782:                case OP_STR_LT: 
                    783:                        {
1.197     parser    784:                                b=POP();  a=POP();
                    785:                                value=NEW VBool(pool(), a->as_string() < b->as_string());
1.58      paf       786:                                PUSH(value);
                    787:                                break;
                    788:                        }
                    789:                case OP_STR_GT: 
                    790:                        {
1.197     parser    791:                                b=POP();  a=POP();
                    792:                                value=NEW VBool(pool(), a->as_string() > b->as_string());
1.58      paf       793:                                PUSH(value);
                    794:                                break;
                    795:                        }
1.55      paf       796:                case OP_STR_LE: 
1.58      paf       797:                        {
1.197     parser    798:                                b=POP();  a=POP();
                    799:                                value=NEW VBool(pool(), a->as_string() <= b->as_string());
1.58      paf       800:                                PUSH(value);
                    801:                                break;
                    802:                        }
1.55      paf       803:                case OP_STR_GE: 
1.54      paf       804:                        {
1.197     parser    805:                                b=POP();  a=POP();
                    806:                                value=NEW VBool(pool(), a->as_string() >= b->as_string());
1.58      paf       807:                                PUSH(value);
                    808:                                break;
                    809:                        }
                    810:                case OP_STR_EQ: 
                    811:                        {
1.197     parser    812:                                b=POP();  a=POP();
                    813:                                value=NEW VBool(pool(), a->as_string() == b->as_string());
1.58      paf       814:                                PUSH(value);
                    815:                                break;
                    816:                        }
                    817:                case OP_STR_NE: 
                    818:                        {
1.197     parser    819:                                b=POP();  a=POP();
                    820:                                value=NEW VBool(pool(), a->as_string() != b->as_string());
1.103     paf       821:                                PUSH(value);
                    822:                                break;
                    823:                        }
                    824:                case OP_IS:
                    825:                        {
1.197     parser    826:                                b=POP();  a=POP();
1.255   ! paf       827:                                value=NEW VBool(pool(), a->is(b->as_string().cstr()));
1.49      paf       828:                                PUSH(value);
1.28      paf       829:                                break;
                    830:                        }
                    831: 
1.11      paf       832:                default:
1.223     paf       833:                        throw Exception(0,
1.67      paf       834:                                0,
1.139     paf       835:                                "invalid opcode %d", op.code); 
1.11      paf       836:                }
                    837:        }
1.1       paf       838: }
1.17      paf       839: 
1.231     paf       840: /// @test cache|prepare junctions 
1.236     paf       841: Value *Request::get_element(const String *& remember_name, bool can_call_operator) {
                    842:        const String& name=POP_NAME();  remember_name=&name;
1.24      paf       843:        Value *ncontext=POP();
1.216     paf       844:        Value *value=0;
1.249     paf       845:        if(can_call_operator) {
1.216     paf       846:                if(Method* method=OP.get_method(name)) { // looking operator of that name FIRST
1.181     parser    847:                        // as if that method were in self and we have normal dynamic method here
                    848:                        Junction& junction=*NEW Junction(pool(), 
1.202     paf       849:                                *root, self->get_class(), method, 0,0,0,0);
1.181     parser    850:                        value=NEW VJunction(junction);
                    851:                }
1.249     paf       852:        }
                    853:        if(!value) {
                    854:                if(!wcontext->get_constructing() // not constructing
                    855:                        && wcontext->get_somebody_entered_some_class() ) // ^class:method
                    856:                        if(VStateless_class *called_class=ncontext->get_class())
                    857:                                if(VStateless_class *read_class=rcontext->get_class())
                    858:                                        if(read_class->derived_from(*called_class)) // current derived from called
                    859:                                                if(Value *base_object=self->base_object()) { // doing DYNAMIC call
                    860:                                                        Temp_derived(*base_object, 0); // temporarily prevent go-back-down virtual calls
1.252     paf       861:                                                        value=base_object->get_element(name, base_object, false); // virtual-up lookup starting from parent
1.249     paf       862:                                                        goto _void;
                    863:                                                }
                    864:        }
1.216     paf       865:        if(!value)
1.252     paf       866:                value=ncontext->get_element(name, ncontext, false);
1.249     paf       867: 
                    868: _void:
1.236     paf       869:        if(value)
1.229     paf       870:                value=&process_to_value(*value); // process possible code-junction
1.236     paf       871:        else
1.167     parser    872:                value=NEW VVoid(pool());
1.63      paf       873: 
1.17      paf       874:        return value;
1.34      paf       875: }
1.70      paf       876: 
1.162     parser    877: /**    @param intercept_string
1.116     paf       878:        - true:
                    879:                they want result=string value, 
                    880:                possible object result goes to wcontext
                    881:        - false:
                    882:                they want any result[string|object]
                    883:                nothing goes to wcontext.
1.125     paf       884:                used in @c (expression) params evaluation
1.224     paf       885: 
                    886:     using the fact it's either string_ or value_ result requested to speed up checkes
1.116     paf       887: */
1.229     paf       888: StringOrValue Request::process(Value& input_value, bool intercept_string) {
1.228     paf       889:        StringOrValue result;
1.224     paf       890:        Junction *junction=input_value.get_junction();
1.70      paf       891:        if(junction && junction->code) { // is it a code-junction?
1.92      paf       892:                // process it
1.157     parser    893: #ifdef DEBUG_EXECUTE
1.158     parser    894:                debug_printf(pool(), "ja->\n");
1.157     parser    895: #endif
1.238     paf       896: 
1.240     paf       897:                if(!junction->root)
                    898:                        throw Exception("parser.runtime",
                    899:                                0,
                    900:                                "junction used outside of context");
                    901: 
1.70      paf       902:                PUSH(self);  
                    903:                PUSH(root);  
                    904:                PUSH(rcontext);  
1.129     paf       905:                PUSH(wcontext);
1.240     paf       906:                
1.225     paf       907:                self=&junction->self;
1.240     paf       908:                root=junction->root;
                    909:                rcontext=junction->rcontext;
1.225     paf       910: 
1.109     paf       911:                // for expression method params
                    912:                // wcontext is set 0
                    913:                // using the fact in decision "which wwrapper to use"
1.240     paf       914:                bool using_code_frame=intercept_string && junction->wcontext;
1.109     paf       915:                if(using_code_frame) {
1.71      paf       916:                        // almost plain wwrapper about junction wcontext, 
                    917:                        // BUT intercepts string writes
1.240     paf       918:                        VCodeFrame local(pool(), *junction->wcontext);  
1.225     paf       919:                        wcontext=&local;
1.207     paf       920: 
1.225     paf       921:                        // execute it
1.228     paf       922:                        recoursion_checked_execute(0/*result_name*/, *junction->code);
1.226     paf       923:                        
1.71      paf       924:                        // CodeFrame soul:
                    925:                        //   string writes were intercepted
                    926:                        //   returning them as the result of getting code-junction
1.229     paf       927:                        result.set_string(*wcontext->get_string());
1.224     paf       928:                } else {
1.225     paf       929:                        // plain wwrapper
                    930:                        WWrapper local(pool(), 0/*empty*/);
                    931:                        wcontext=&local;
                    932:                
                    933:                        // execute it
1.228     paf       934:                        recoursion_checked_execute(0/*result_name*/, *junction->code);
1.226     paf       935:                
1.228     paf       936:                        result=wcontext->result();
1.224     paf       937:                }
1.70      paf       938:                
                    939:                wcontext=static_cast<WContext *>(POP());  
                    940:                rcontext=POP();  
                    941:                root=POP();  
1.248     paf       942:                self=static_cast<Value *>(POP());
1.70      paf       943:                
1.157     parser    944: #ifdef DEBUG_EXECUTE
1.158     parser    945:                debug_printf(pool(), "<-ja returned");
1.157     parser    946: #endif
1.224     paf       947:        } else {
1.228     paf       948:                result.set_value(input_value);
1.224     paf       949:        }
1.228     paf       950:        return result;
1.85      paf       951: }
                    952: 
1.242     paf       953: const String& Request::execute_method(VMethodFrame& amethodFrame, const Method& method) {
1.99      paf       954:        PUSH(self);  
                    955:        PUSH(root);  
                    956:        PUSH(rcontext);  
                    957:        PUSH(wcontext);
                    958:        
                    959:        // initialize contexts
1.242     paf       960:        root=rcontext=self=&amethodFrame;
                    961:        wcontext=&amethodFrame;
1.99      paf       962:        
                    963:        // execute!     
                    964:        execute(*method.parser_code);
                    965:        
                    966:        // result
1.242     paf       967:        const String& result=wcontext->result().as_string();
1.208     paf       968:        
                    969:        wcontext=static_cast<WContext *>(POP());  
                    970:        rcontext=POP();  
                    971:        root=POP();  
1.248     paf       972:        self=static_cast<Value *>(POP());
1.242     paf       973:        
                    974:        // return
                    975:        return result;
1.208     paf       976: }
                    977: 
1.242     paf       978: void Request::execute_method(Value& aself, 
                    979:                                                         const Method& method, VString *optional_param,
                    980:                                                         const String **return_string) {
1.208     paf       981:        PUSH(self);  
                    982:        PUSH(root);  
                    983:        PUSH(rcontext);  
                    984:        PUSH(wcontext);
                    985:        
                    986:        // initialize contexts
1.242     paf       987:        //root=rcontext=self=&aself;
                    988:        self=&aself;    
                    989: //     WWrapper local(pool(), &aself);
                    990: //     wcontext=&local; 
                    991:        Junction local_junction(pool(), *self, self->get_class(), &method, 0,0,0,0);
                    992:        VMethodFrame local_frame(pool(), method.name, local_junction);
                    993:        if(optional_param && local_frame.can_store_param()) {
                    994:                local_frame.store_param(optional_param);
                    995:                local_frame.fill_unspecified_params();
                    996:        }
                    997:        local_frame.set_self(*self);
                    998:        root=rcontext=wcontext=&local_frame; 
1.246     paf       999: 
                   1000:        // prevent non-string writes for better error reporting
                   1001:        if(return_string)
                   1002:                wcontext->write(local_frame);
1.208     paf      1003:        
                   1004:        // execute!     
                   1005:        execute(*method.parser_code);
                   1006:        
                   1007:        // result
1.242     paf      1008:        const String *result=0;
1.245     paf      1009:        if(return_string)
1.242     paf      1010:                *return_string=&wcontext->result().as_string();
1.99      paf      1011:        
                   1012:        wcontext=static_cast<WContext *>(POP());  
                   1013:        rcontext=POP();  
                   1014:        root=POP();  
1.248     paf      1015:        self=static_cast<Value *>(POP());
1.242     paf      1016: }
                   1017: 
                   1018: void Request::execute_nonvirtual_method(VStateless_class& aclass, 
                   1019:                                                                                                 const String& method_name, VString *optional_param,
                   1020:                                                                                                 const String **return_string,
                   1021:                                                                                                 const Method **return_method) {
                   1022: 
                   1023:        const Method *method=aclass.get_method(method_name);
                   1024:        if(return_string)
                   1025:                *return_string=0;
                   1026:        if(return_method)
                   1027:                *return_method=method;
                   1028: 
                   1029:        if(method)
                   1030:                execute_method(aclass, *method, optional_param, return_string);
1.99      paf      1031: }
                   1032: 
1.176     parser   1033: const String *Request::execute_virtual_method(Value& aself, 
1.208     paf      1034:                                                                                          const String& method_name) {
1.252     paf      1035:        if(Value *value=aself.get_element(method_name, &aself, false))
1.99      paf      1036:                if(Junction *junction=value->get_junction())
1.241     paf      1037:                        if(const Method *method=junction->method) {
                   1038:                                const String *result;
1.242     paf      1039:                                execute_method(aself, *method, 0/*no params*/, &result);
1.241     paf      1040:                                return result;
                   1041:                        }
1.188     parser   1042:                        
1.176     parser   1043:        return 0;
                   1044: }
                   1045: 

E-mail: