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

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

E-mail: