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

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.284   ! paf         8: static const char* IDENT_EXECUTE_C="$Date: 2002/10/15 10:05:00 $";
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:                        {
                    170:                                PUSH(self);
                    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.272     paf       212:                                        *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.272     paf       350:                                        *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:                                Value *saved_self=self; 
                    415:                                VMethodFrame *saved_method_frame=method_frame;
                    416:                                Value *saved_rcontext=rcontext;
                    417:                                WContext *saved_wcontext=wcontext;
1.45      paf       418:                                
1.272     paf       419:                                VStateless_class *called_class=frame.junction.self.get_class();
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.149     paf       427:                                                        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.237     paf       434:                                                frame.write(*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.272     paf       442:                                        self=&frame.junction.self;
1.75      paf       443: 
1.272     paf       444:                                frame.set_self(*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=
                    453:                                                called_class==self ? Method::CT_STATIC : Method::CT_DYNAMIC;
                    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;
                    485:                                self=saved_self;
1.220     paf       486: 
                    487: #ifdef DEBUG_STRING_APPENDS_VS_EXPANDS
                    488:                                if(const String *s=value->get_string())
                    489:                                        wcontext_result_size+=s->used_rows()*sizeof(String::Chunk::Row);
                    490: #endif
1.213     paf       491: 
1.232     paf       492:                                if(op.code==OP_CALL__WRITE) {
1.236     paf       493:                                        write_assign_lang(result, last_get_element_name);
1.232     paf       494:                                } else { // OP_CALL
                    495:                                        PUSH(&result.as_value());
                    496:                                }
                    497:                                
1.157     parser    498: #ifdef DEBUG_EXECUTE
1.158     parser    499:                                debug_printf(pool(), "<-returned");
1.157     parser    500: #endif
1.49      paf       501:                                break;
                    502:                        }
                    503: 
1.55      paf       504:                // expression ops: unary
                    505:                case OP_NEG:
                    506:                        {
                    507:                                Value *operand=POP();
1.197     parser    508:                                value=NEW VDouble(pool(), -operand->as_double());
1.55      paf       509:                                PUSH(value);
                    510:                                break;
                    511:                        }
                    512:                case OP_INV:
                    513:                        {
                    514:                                Value *operand=POP();
1.197     parser    515:                                value=NEW VDouble(pool(), ~operand->as_int());
1.55      paf       516:                                PUSH(value);
                    517:                                break;
                    518:                        }
                    519:                case OP_NOT:
                    520:                        {
                    521:                                Value *operand=POP();
1.197     parser    522:                                value=NEW VBool(pool(), !operand->as_bool());
1.55      paf       523:                                PUSH(value);
                    524:                                break;
                    525:                        }
1.62      paf       526:                case OP_DEF:
                    527:                        {
                    528:                                Value *operand=POP();
1.197     parser    529:                                value=NEW VBool(pool(), operand->is_defined());
1.62      paf       530:                                PUSH(value);
                    531:                                break;
                    532:                        }
                    533:                case OP_IN:
                    534:                        {
1.199     paf       535:                                /// @test String::cmp
1.62      paf       536:                                Value *operand=POP();
1.110     paf       537:                                const char *path=operand->as_string().cstr();
1.197     parser    538:                                value=NEW VBool(pool(), 
1.151     paf       539:                                        info.uri && strncmp(path, info.uri, strlen(path))==0);
1.62      paf       540:                                PUSH(value);
                    541:                                break;
                    542:                        }
                    543:                case OP_FEXISTS:
                    544:                        {
                    545:                                Value *operand=POP();
1.197     parser    546:                                value=NEW VBool(pool(), 
1.127     paf       547:                                        file_readable(absolute(operand->as_string())));
1.148     paf       548:                                PUSH(value);
                    549:                                break;
                    550:                        }
                    551:                case OP_DEXISTS:
                    552:                        {
                    553:                                Value *operand=POP();
1.197     parser    554:                                value=NEW VBool(pool(), 
1.148     paf       555:                                        dir_readable(absolute(operand->as_string())));
1.62      paf       556:                                PUSH(value);
                    557:                                break;
                    558:                        }
1.55      paf       559: 
                    560:                // expression ops: binary
                    561:                case OP_SUB: 
1.53      paf       562:                        {
1.197     parser    563:                                b=POP();  a=POP();
                    564:                                value=NEW VDouble(pool(), a->as_double() - b->as_double());
1.53      paf       565:                                PUSH(value);
                    566:                                break;
                    567:                        }
1.55      paf       568:                case OP_ADD: 
1.53      paf       569:                        {
1.197     parser    570:                                b=POP();  a=POP();
                    571:                                value=NEW VDouble(pool(), a->as_double() + b->as_double());
1.53      paf       572:                                PUSH(value);
                    573:                                break;
                    574:                        }
1.49      paf       575:                case OP_MUL: 
                    576:                        {
1.197     parser    577:                                b=POP();  a=POP();
                    578:                                value=NEW VDouble(pool(), a->as_double() * b->as_double());
1.53      paf       579:                                PUSH(value);
                    580:                                break;
                    581:                        }
                    582:                case OP_DIV: 
                    583:                        {
1.197     parser    584:                                b=POP();  a=POP();
1.170     parser    585: 
                    586:                                double a_double=a->as_double();
                    587:                                double b_double=b->as_double();
                    588: 
1.171     parser    589:                                if(b_double == 0) {
                    590:                                        const String *problem_source=&b->as_string();
1.223     paf       591:                                        throw Exception("number.zerodivision",
1.171     parser    592:                                                problem_source,
1.170     parser    593:                                                "Division by zero");
1.171     parser    594:                                }
1.170     parser    595: 
1.197     parser    596:                                value=NEW VDouble(pool(), a_double / b_double);
1.54      paf       597:                                PUSH(value);
                    598:                                break;
                    599:                        }
1.55      paf       600:                case OP_MOD: 
                    601:                        {
1.197     parser    602:                                b=POP();  a=POP();
1.170     parser    603: 
1.173     parser    604:                                double a_double=a->as_double();
                    605:                                double b_double=b->as_double();
1.170     parser    606: 
1.173     parser    607:                                if(b_double == 0) {
1.171     parser    608:                                        const String *problem_source=&b->as_string();
1.223     paf       609:                                        throw Exception("number.zerodivision",
1.171     parser    610:                                                problem_source,
1.170     parser    611:                                                "Modulus by zero");
1.171     parser    612:                                }
1.170     parser    613: 
1.197     parser    614:                                value=NEW VDouble(pool(), fmod(a_double, b_double));
1.201     paf       615:                                PUSH(value);
                    616:                                break;
                    617:                        }
                    618:                case OP_INTDIV:
                    619:                        {
                    620:                                b=POP();  a=POP();
                    621: 
                    622:                                int a_int=a->as_int();
                    623:                                int b_int=b->as_int();
                    624: 
                    625:                                if(b_int == 0) {
                    626:                                        const String *problem_source=&b->as_string();
1.223     paf       627:                                        throw Exception("number.zerodivision",
1.201     paf       628:                                                problem_source,
                    629:                                                "Division by zero");
                    630:                                }
                    631: 
                    632:                                value=NEW VInt(pool(), a_int / b_int);
1.55      paf       633:                                PUSH(value);
                    634:                                break;
                    635:                        }
1.274     paf       636:                case OP_BIN_SL:
                    637:                        {
                    638:                                b=POP();  a=POP();
                    639:                                value=NEW VInt(pool(), 
                    640:                                        a->as_int() <<
                    641:                                        b->as_int());
                    642:                                PUSH(value);
                    643:                                break;
                    644:                        }
                    645:                case OP_BIN_SR:
                    646:                        {
                    647:                                b=POP();  a=POP();
                    648:                                value=NEW VInt(pool(), 
                    649:                                        a->as_int() >>
                    650:                                        b->as_int());
                    651:                                PUSH(value);
                    652:                                break;
                    653:                        }
1.55      paf       654:                case OP_BIN_AND:
1.54      paf       655:                        {
1.197     parser    656:                                b=POP();  a=POP();
1.274     paf       657:                                value=NEW VInt(pool(), 
1.155     parser    658:                                        a->as_int() &
                    659:                                        b->as_int());
1.54      paf       660:                                PUSH(value);
                    661:                                break;
                    662:                        }
1.55      paf       663:                case OP_BIN_OR:
1.54      paf       664:                        {
1.197     parser    665:                                b=POP();  a=POP();
1.274     paf       666:                                value=NEW VInt(pool(), 
1.155     parser    667:                                        a->as_int() |
                    668:                                        b->as_int());
1.55      paf       669:                                PUSH(value);
                    670:                                break;
                    671:                        }
1.56      paf       672:                case OP_BIN_XOR:
                    673:                        {
1.197     parser    674:                                b=POP();  a=POP();
1.274     paf       675:                                value=NEW VInt(pool(), 
1.155     parser    676:                                        a->as_int() ^
                    677:                                        b->as_int());
1.56      paf       678:                                PUSH(value);
                    679:                                break;
                    680:                        }
1.55      paf       681:                case OP_LOG_AND:
                    682:                        {
1.209     paf       683:                                b_code=POP_CODE();  a=POP();
1.263     paf       684:                                bool result;
1.209     paf       685:                                if(a->as_bool()) {
                    686:                                        execute(*b_code);
                    687:                                        b=POP();
1.263     paf       688:                                        result=b->as_bool();
1.209     paf       689:                                } else
1.263     paf       690:                                        result=false;
                    691:                                value=NEW VBool(pool(), result);
1.55      paf       692:                                PUSH(value);
                    693:                                break;
                    694:                        }
                    695:                case OP_LOG_OR:
                    696:                        {
1.209     paf       697:                                b_code=POP_CODE();  a=POP();
1.263     paf       698:                                bool result;
1.209     paf       699:                                if(a->as_bool()) 
1.263     paf       700:                                        result=true;
1.209     paf       701:                                else {
                    702:                                        execute(*b_code);
                    703:                                        b=POP();
1.263     paf       704:                                        result=b->as_bool();
1.209     paf       705:                                }
1.263     paf       706:                                value=NEW VBool(pool(), result);
1.56      paf       707:                                PUSH(value);
                    708:                                break;
                    709:                        }
                    710:                case OP_LOG_XOR:
                    711:                        {
1.197     parser    712:                                b=POP();  a=POP();
                    713:                                value=NEW VBool(pool(), a->as_bool() ^ b->as_bool());
1.55      paf       714:                                PUSH(value);
                    715:                                break;
                    716:                        }
                    717:                case OP_NUM_LT: 
                    718:                        {
1.197     parser    719:                                b=POP();  a=POP();
1.263     paf       720:                                double result=a->as_double() - b->as_double();
                    721:                                value=NEW VBool(pool(), result < 0.0);
1.55      paf       722:                                PUSH(value);
                    723:                                break;
                    724:                        }
                    725:                case OP_NUM_GT: 
                    726:                        {
1.197     parser    727:                                b=POP();  a=POP();
1.263     paf       728:                                double result=a->as_double() - b->as_double();
                    729:                                value=NEW VBool(pool(), result > 0.0);
1.55      paf       730:                                PUSH(value);
                    731:                                break;
                    732:                        }
                    733:                case OP_NUM_LE: 
                    734:                        {
1.197     parser    735:                                b=POP();  a=POP();
1.263     paf       736:                                double result=a->as_double() - b->as_double();
                    737:                                value=NEW VBool(pool(), result <= 0.0);
1.55      paf       738:                                PUSH(value);
                    739:                                break;
                    740:                        }
                    741:                case OP_NUM_GE: 
                    742:                        {
1.197     parser    743:                                b=POP();  a=POP();
1.263     paf       744:                                double result=a->as_double() - b->as_double();
                    745:                                value=NEW VBool(pool(), result >= 0.0);
1.55      paf       746:                                PUSH(value);
                    747:                                break;
                    748:                        }
                    749:                case OP_NUM_EQ: 
                    750:                        {
1.197     parser    751:                                b=POP();  a=POP();
1.263     paf       752:                                double result=a->as_double() - b->as_double();
                    753:                                value=NEW VBool(pool(), result == 0.0);
1.55      paf       754:                                PUSH(value);
                    755:                                break;
                    756:                        }
                    757:                case OP_NUM_NE: 
                    758:                        {
1.197     parser    759:                                b=POP();  a=POP();
1.263     paf       760:                                double result=a->as_double() - b->as_double();
                    761:                                value=NEW VBool(pool(), result != 0.0);
1.54      paf       762:                                PUSH(value);
                    763:                                break;
                    764:                        }
1.58      paf       765:                case OP_STR_LT: 
                    766:                        {
1.197     parser    767:                                b=POP();  a=POP();
                    768:                                value=NEW VBool(pool(), a->as_string() < b->as_string());
1.58      paf       769:                                PUSH(value);
                    770:                                break;
                    771:                        }
                    772:                case OP_STR_GT: 
                    773:                        {
1.197     parser    774:                                b=POP();  a=POP();
                    775:                                value=NEW VBool(pool(), a->as_string() > b->as_string());
1.58      paf       776:                                PUSH(value);
                    777:                                break;
                    778:                        }
1.55      paf       779:                case OP_STR_LE: 
1.58      paf       780:                        {
1.197     parser    781:                                b=POP();  a=POP();
                    782:                                value=NEW VBool(pool(), a->as_string() <= b->as_string());
1.58      paf       783:                                PUSH(value);
                    784:                                break;
                    785:                        }
1.55      paf       786:                case OP_STR_GE: 
1.54      paf       787:                        {
1.197     parser    788:                                b=POP();  a=POP();
                    789:                                value=NEW VBool(pool(), a->as_string() >= b->as_string());
1.58      paf       790:                                PUSH(value);
                    791:                                break;
                    792:                        }
                    793:                case OP_STR_EQ: 
                    794:                        {
1.197     parser    795:                                b=POP();  a=POP();
                    796:                                value=NEW VBool(pool(), a->as_string() == b->as_string());
1.58      paf       797:                                PUSH(value);
                    798:                                break;
                    799:                        }
                    800:                case OP_STR_NE: 
                    801:                        {
1.197     parser    802:                                b=POP();  a=POP();
                    803:                                value=NEW VBool(pool(), a->as_string() != b->as_string());
1.103     paf       804:                                PUSH(value);
                    805:                                break;
                    806:                        }
                    807:                case OP_IS:
                    808:                        {
1.197     parser    809:                                b=POP();  a=POP();
1.255     paf       810:                                value=NEW VBool(pool(), a->is(b->as_string().cstr()));
1.49      paf       811:                                PUSH(value);
1.28      paf       812:                                break;
                    813:                        }
                    814: 
1.11      paf       815:                default:
1.223     paf       816:                        throw Exception(0,
1.67      paf       817:                                0,
1.139     paf       818:                                "invalid opcode %d", op.code); 
1.11      paf       819:                }
                    820:        }
1.1       paf       821: }
1.17      paf       822: 
1.231     paf       823: /// @test cache|prepare junctions 
1.284   ! paf       824: Value *Request::get_element(const String *& remember_name, bool can_call_operator) {
1.236     paf       825:        const String& name=POP_NAME();  remember_name=&name;
1.24      paf       826:        Value *ncontext=POP();
1.216     paf       827:        Value *value=0;
1.249     paf       828:        if(can_call_operator) {
1.281     paf       829:                if(Method* method=main_class.get_method(name)) { // looking operator of that name FIRST
1.181     parser    830:                        // as if that method were in self and we have normal dynamic method here
                    831:                        Junction& junction=*NEW Junction(pool(), 
1.281     paf       832:                                main_class, method, 0,0,0,0);
1.181     parser    833:                        value=NEW VJunction(junction);
                    834:                }
1.249     paf       835:        }
                    836:        if(!value) {
                    837:                if(!wcontext->get_constructing() // not constructing
                    838:                        && wcontext->get_somebody_entered_some_class() ) // ^class:method
                    839:                        if(VStateless_class *called_class=ncontext->get_class())
                    840:                                if(VStateless_class *read_class=rcontext->get_class())
                    841:                                        if(read_class->derived_from(*called_class)) // current derived from called
                    842:                                                if(Value *base_object=self->base_object()) { // doing DYNAMIC call
1.256     paf       843:                                                        Temp_derived temp_derived(*base_object, 0); // temporarily prevent go-back-down virtual calls
1.252     paf       844:                                                        value=base_object->get_element(name, base_object, false); // virtual-up lookup starting from parent
1.249     paf       845:                                                        goto _void;
                    846:                                                }
                    847:        }
1.216     paf       848:        if(!value)
1.252     paf       849:                value=ncontext->get_element(name, ncontext, false);
1.249     paf       850: 
                    851: _void:
1.284   ! paf       852:        if(value)
1.275     paf       853:                        value=&process_to_value(*value); // process possible code-junction
1.284   ! paf       854:        else
1.167     parser    855:                value=NEW VVoid(pool());
1.63      paf       856: 
1.17      paf       857:        return value;
1.34      paf       858: }
1.70      paf       859: 
1.162     parser    860: /**    @param intercept_string
1.116     paf       861:        - true:
                    862:                they want result=string value, 
                    863:                possible object result goes to wcontext
                    864:        - false:
                    865:                they want any result[string|object]
                    866:                nothing goes to wcontext.
1.125     paf       867:                used in @c (expression) params evaluation
1.224     paf       868: 
                    869:     using the fact it's either string_ or value_ result requested to speed up checkes
1.116     paf       870: */
1.229     paf       871: StringOrValue Request::process(Value& input_value, bool intercept_string) {
1.228     paf       872:        StringOrValue result;
1.224     paf       873:        Junction *junction=input_value.get_junction();
1.70      paf       874:        if(junction && junction->code) { // is it a code-junction?
1.92      paf       875:                // process it
1.157     parser    876: #ifdef DEBUG_EXECUTE
1.158     parser    877:                debug_printf(pool(), "ja->\n");
1.157     parser    878: #endif
1.238     paf       879: 
1.257     paf       880:                if(!junction->method_frame)
1.240     paf       881:                        throw Exception("parser.runtime",
                    882:                                0,
                    883:                                "junction used outside of context");
                    884: 
1.257     paf       885:                Value *saved_self=self; 
                    886:                VMethodFrame *saved_method_frame=method_frame;  
                    887:                Value *saved_rcontext=rcontext;  
                    888:                WContext *saved_wcontext=wcontext;
1.240     paf       889:                
1.272     paf       890:                self=&junction->self;
1.257     paf       891:                method_frame=junction->method_frame;
1.240     paf       892:                rcontext=junction->rcontext;
1.225     paf       893: 
1.109     paf       894:                // for expression method params
                    895:                // wcontext is set 0
                    896:                // using the fact in decision "which wwrapper to use"
1.240     paf       897:                bool using_code_frame=intercept_string && junction->wcontext;
1.109     paf       898:                if(using_code_frame) {
1.71      paf       899:                        // almost plain wwrapper about junction wcontext, 
                    900:                        // BUT intercepts string writes
1.268     paf       901:                        VCodeFrame local(pool(), *junction->wcontext, junction->wcontext);
1.225     paf       902:                        wcontext=&local;
1.207     paf       903: 
1.225     paf       904:                        // execute it
1.228     paf       905:                        recoursion_checked_execute(0/*result_name*/, *junction->code);
1.226     paf       906:                        
1.71      paf       907:                        // CodeFrame soul:
                    908:                        //   string writes were intercepted
                    909:                        //   returning them as the result of getting code-junction
1.229     paf       910:                        result.set_string(*wcontext->get_string());
1.224     paf       911:                } else {
1.225     paf       912:                        // plain wwrapper
1.267     paf       913:                        WWrapper local(pool(), 0/*empty*/, wcontext);
1.225     paf       914:                        wcontext=&local;
                    915:                
                    916:                        // execute it
1.228     paf       917:                        recoursion_checked_execute(0/*result_name*/, *junction->code);
1.226     paf       918:                
1.228     paf       919:                        result=wcontext->result();
1.224     paf       920:                }
1.70      paf       921:                
1.257     paf       922:                wcontext=saved_wcontext;
                    923:                rcontext=saved_rcontext;
                    924:                method_frame=saved_method_frame;
                    925:                self=saved_self;
1.70      paf       926:                
1.157     parser    927: #ifdef DEBUG_EXECUTE
1.158     parser    928:                debug_printf(pool(), "<-ja returned");
1.157     parser    929: #endif
1.224     paf       930:        } else {
1.228     paf       931:                result.set_value(input_value);
1.224     paf       932:        }
1.228     paf       933:        return result;
1.85      paf       934: }
                    935: 
1.257     paf       936: const String& Request::execute_method(VMethodFrame& amethod_frame, const Method& method) {
                    937:        Value *saved_self=self; 
                    938:        VMethodFrame *saved_method_frame=method_frame;  
                    939:        Value *saved_rcontext=rcontext;  
                    940:        WContext *saved_wcontext=wcontext;
1.99      paf       941:        
                    942:        // initialize contexts
1.257     paf       943:        self=rcontext=wcontext=method_frame=&amethod_frame;
1.99      paf       944:        
                    945:        // execute!     
                    946:        execute(*method.parser_code);
                    947:        
                    948:        // result
1.242     paf       949:        const String& result=wcontext->result().as_string();
1.208     paf       950:        
1.257     paf       951:        wcontext=saved_wcontext;
                    952:        rcontext=saved_rcontext;
                    953:        method_frame=saved_method_frame;
                    954:        self=saved_self;
1.242     paf       955:        
                    956:        // return
                    957:        return result;
1.208     paf       958: }
                    959: 
1.242     paf       960: void Request::execute_method(Value& aself, 
                    961:                                                         const Method& method, VString *optional_param,
                    962:                                                         const String **return_string) {
1.257     paf       963:        Value *saved_self=self; 
                    964:        VMethodFrame *saved_method_frame=method_frame;  
                    965:        Value *saved_rcontext=rcontext;  
                    966:        WContext *saved_wcontext=wcontext;
1.208     paf       967:        
                    968:        // initialize contexts
1.257     paf       969:        //method_frame=rcontext=self=&aself;
1.242     paf       970:        self=&aself;    
1.267     paf       971: //     WWrapper local(pool(), &aself, wcontext);
1.242     paf       972: //     wcontext=&local; 
1.272     paf       973:        Junction local_junction(pool(), *self, &method, 0,0,0,0);
1.282     paf       974:        VMethodFrame local_frame(pool(), method.name, local_junction, method_frame/*caller*/);
1.242     paf       975:        if(optional_param && local_frame.can_store_param()) {
                    976:                local_frame.store_param(optional_param);
                    977:                local_frame.fill_unspecified_params();
                    978:        }
1.272     paf       979:        local_frame.set_self(*self);
1.257     paf       980:        rcontext=wcontext=method_frame=&local_frame; 
1.246     paf       981: 
                    982:        // prevent non-string writes for better error reporting
                    983:        if(return_string)
                    984:                wcontext->write(local_frame);
1.208     paf       985:        
                    986:        // execute!     
                    987:        execute(*method.parser_code);
                    988:        
                    989:        // result
1.242     paf       990:        const String *result=0;
1.245     paf       991:        if(return_string)
1.242     paf       992:                *return_string=&wcontext->result().as_string();
1.99      paf       993:        
1.257     paf       994:        wcontext=saved_wcontext;
                    995:        rcontext=saved_rcontext;
                    996:        method_frame=saved_method_frame;
                    997:        self=saved_self;
1.242     paf       998: }
                    999: 
                   1000: void Request::execute_nonvirtual_method(VStateless_class& aclass, 
1.281     paf      1001:                                                                                                 const String& method_name, VString *optional_param,
1.242     paf      1002:                                                                                                 const String **return_string,
                   1003:                                                                                                 const Method **return_method) {
                   1004: 
1.281     paf      1005:        const Method *method=aclass.get_method(method_name);
1.242     paf      1006:        if(return_string)
                   1007:                *return_string=0;
                   1008:        if(return_method)
                   1009:                *return_method=method;
                   1010: 
                   1011:        if(method)
                   1012:                execute_method(aclass, *method, optional_param, return_string);
1.99      paf      1013: }
                   1014: 
1.176     parser   1015: const String *Request::execute_virtual_method(Value& aself, 
1.208     paf      1016:                                                                                          const String& method_name) {
1.252     paf      1017:        if(Value *value=aself.get_element(method_name, &aself, false))
1.99      paf      1018:                if(Junction *junction=value->get_junction())
1.241     paf      1019:                        if(const Method *method=junction->method) {
                   1020:                                const String *result;
1.242     paf      1021:                                execute_method(aself, *method, 0/*no params*/, &result);
1.241     paf      1022:                                return result;
                   1023:                        }
1.188     parser   1024:                        
1.176     parser   1025:        return 0;
                   1026: }

E-mail: