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

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

E-mail: