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

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

E-mail: