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

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

E-mail: