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

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

E-mail: