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

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

E-mail: