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

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

E-mail: