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

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

E-mail: