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

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

E-mail: