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

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.230   ! paf         7:        $Id: execute.C,v 1.229 2002/04/15 11:33:31 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.227     paf        26: //#define DEBUG_EXECUTE
1.221     paf        27: //#define DEBUG_STRING_APPENDS_VS_EXPANDS
1.220     paf        28: 
                     29: 
                     30: #ifdef DEBUG_STRING_APPENDS_VS_EXPANDS
                     31: ulong wcontext_result_size=0;
                     32: #endif
                     33: 
                     34: 
1.157     parser     35: 
                     36: #ifdef DEBUG_EXECUTE
1.1       paf        37: char *opcode_name[]={
1.49      paf        38:        // literals
1.109     paf        39:        "VALUE",  "CURLY_CODE__STORE_PARAM",  "EXPR_CODE__STORE_PARAM",
1.209     paf        40:        "NESTED_CODE",
1.49      paf        41: 
                     42:        // actions
1.187     parser     43:        "WITH_ROOT",    "WITH_SELF",    "WITH_READ",    "WITH_WRITE",
1.66      paf        44:        "GET_CLASS",
1.182     parser     45:        "CONSTRUCT_VALUE", "CONSTRUCT_EXPR", "CURLY_CODE__CONSTRUCT",
1.108     paf        46:        "WRITE_VALUE",  "WRITE_EXPR_RESULT",  "STRING__WRITE",
1.215     paf        47:        "GET_ELEMENT_OR_OPERATOR", "GET_ELEMENT",       "GET_ELEMENT__WRITE",
1.226     paf        48:        "OBJECT_POOL",  
                     49:        "STRING_POOL",
1.1       paf        50:        "GET_METHOD_FRAME",
                     51:        "STORE_PARAM",
1.219     paf        52:        "PREPARE_TO_CONSTRUCT_OBJECT",  "PREPARE_TO_EXPRESSION", "CALL",
1.49      paf        53: 
                     54:        // expression ops: unary
1.148     paf        55:        "NEG", "INV", "NOT", "DEF", "IN", "FEXISTS", "DEXISTS",
1.49      paf        56:        // expression ops: binary
1.201     paf        57:        "SUB", "ADD", "MUL", "DIV", "MOD", "INTDIV",
1.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.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.223     paf       182:                                        throw Exception("parser.runtime",
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.230   ! paf       277:                                value=POP();
        !           278:                                write_no_lang(*value->as_expr_result());
        !           279: 
        !           280:                                // must be after write(result) and 
1.219     paf       281:                                // see OP_PREPARE_TO_EXPRESSION
                    282:                                wcontext->set_in_expression(false);
1.108     paf       283:                                break;
                    284:                        }
1.86      paf       285:                case OP_STRING__WRITE:
                    286:                        {
1.190     parser    287:                                VString *vstring=static_cast<VString *>(i.next());
1.157     parser    288: #ifdef DEBUG_EXECUTE
1.158     parser    289:                                debug_printf(pool(), " \"%s\"", vstring->string().cstr());
1.157     parser    290: #endif
1.122     paf       291:                                write_no_lang(vstring->string());
1.13      paf       292:                                break;
1.14      paf       293:                        }
1.13      paf       294:                        
1.215     paf       295:                case OP_GET_ELEMENT_OR_OPERATOR:
                    296:                        {
                    297:                                // maybe they do ^object.method[] call, remember the fact
                    298:                                wcontext->set_somebody_entered_some_object(true);
                    299: 
                    300:                                //_asm int 3;
                    301:                                value=get_element(true);
                    302:                                PUSH(value);
                    303:                                break;
                    304:                        }
1.15      paf       305:                case OP_GET_ELEMENT:
1.11      paf       306:                        {
1.150     paf       307:                                // maybe they do ^object.method[] call, remember the fact
1.200     paf       308:                                wcontext->set_somebody_entered_some_object(true);
1.150     paf       309: 
1.215     paf       310:                                //_asm int 3;
                    311:                                value=get_element(false);
1.24      paf       312:                                PUSH(value);
1.17      paf       313:                                break;
                    314:                        }
                    315: 
1.18      paf       316:                case OP_GET_ELEMENT__WRITE:
1.17      paf       317:                        {
1.215     paf       318:                                value=get_element(false);
1.92      paf       319:                                write_assign_lang(*value);
1.17      paf       320:                                break;
                    321:                        }
                    322: 
1.32      paf       323: 
1.226     paf       324:                case OP_OBJECT_POOL:
1.17      paf       325:                        {
1.226     paf       326:                                const Array *local_ops=reinterpret_cast<const Array *>(i.next());
                    327:                                
1.24      paf       328:                                PUSH(wcontext);
1.137     paf       329:                                PUSH((void *)flang);
                    330:                                flang=String::UL_PASS_APPENDED;
1.226     paf       331:                                WWrapper local(pool(), 0 /*empty*/);
                    332:                                wcontext=&local;
                    333: 
                    334:                                execute(*local_ops);
                    335: 
1.228     paf       336:                                value=&wcontext->result().as_value();
1.137     paf       337:                                flang=static_cast<String::Untaint_lang>(reinterpret_cast<int>(POP()));
1.25      paf       338:                                wcontext=static_cast<WContext *>(POP());
1.24      paf       339:                                PUSH(value);
1.13      paf       340:                                break;
1.15      paf       341:                        }
1.13      paf       342:                        
1.226     paf       343:                case OP_STRING_POOL:
1.49      paf       344:                        {
1.226     paf       345:                                const Array *local_ops=reinterpret_cast<const Array *>(i.next());
                    346: 
1.49      paf       347:                                PUSH(wcontext);
1.226     paf       348:                                WWrapper local(pool(), 0 /*empty*/);
                    349:                                wcontext=&local;
                    350: 
                    351:                                execute(*local_ops);
                    352: 
1.49      paf       353:                                // from "$a $b" part of expression taking only string value,
                    354:                                // ignoring any other content of wcontext
1.82      paf       355:                                const String *string=wcontext->get_string();
1.80      paf       356:                                Value *value;
                    357:                                if(string)
                    358:                                        value=NEW VString(*string);
                    359:                                else
1.167     parser    360:                                        NEW VVoid(pool());
1.50      paf       361:                                wcontext=static_cast<WContext *>(POP());
1.49      paf       362:                                PUSH(value);
                    363:                                break;
                    364:                        }
                    365: 
1.51      paf       366:                // CALL
1.28      paf       367:                case OP_GET_METHOD_FRAME:
                    368:                        {
1.197     parser    369:                                value=POP();
1.138     paf       370: 
1.111     paf       371:                                // info: 
                    372:                                //      code compiled so that this one's always method-junction, 
                    373:                                //      not a code-junction
1.32      paf       374:                                Junction *junction=value->get_junction();
1.31      paf       375:                                if(!junction)
1.223     paf       376:                                        throw Exception("parser.runtime",
1.42      paf       377:                                                &value->name(),
1.111     paf       378:                                                "(%s) not a method or junction, can not call it",
1.38      paf       379:                                                        value->type()); 
1.70      paf       380: 
1.185     parser    381:                                VMethodFrame *frame=NEW VMethodFrame(pool(), value->name(), *junction);
1.28      paf       382:                                PUSH(frame);
                    383:                                break;
                    384:                        }
                    385:                case OP_STORE_PARAM:
                    386:                        {
1.197     parser    387:                                value=POP();
1.73      paf       388:                                VMethodFrame *frame=static_cast<VMethodFrame *>(stack.top_value());
1.92      paf       389:                                frame->store_param(frame->name(), value);
1.29      paf       390:                                break;
                    391:                        }
                    392: 
1.186     parser    393:                case OP_PREPARE_TO_CONSTRUCT_OBJECT:
                    394:                        {
1.200     paf       395:                                wcontext->set_constructing(true);
1.186     parser    396:                                break;
                    397:                        }
1.219     paf       398: 
                    399:                case OP_PREPARE_TO_EXPRESSION:
                    400:                        {
                    401:                                wcontext->set_in_expression(true);
                    402:                                break;
                    403:                        }
                    404: 
1.186     parser    405:                case OP_CALL:
1.29      paf       406:                        {
1.157     parser    407: #ifdef DEBUG_EXECUTE
1.158     parser    408:                                debug_printf(pool(), "->\n");
1.157     parser    409: #endif
1.34      paf       410:                                VMethodFrame *frame=static_cast<VMethodFrame *>(POP());
                    411:                                frame->fill_unspecified_params();
1.45      paf       412:                                PUSH(self);  
                    413:                                PUSH(root);  
                    414:                                PUSH(rcontext);  
                    415:                                PUSH(wcontext); 
                    416:                                
1.100     paf       417:                                VStateless_class *called_class=frame->junction.self.get_class();
1.200     paf       418:                                if(wcontext->get_constructing()) {
                    419:                                        wcontext->set_constructing(false);
1.185     parser    420:                                        if(frame->junction.method->call_type!=Method::CT_STATIC) {
1.138     paf       421:                                                // this is a constructor call
1.185     parser    422: 
                    423:                                                if(Value *value=called_class->create_new_value(pool())) {
1.204     paf       424:                                                        // some stateless_class creatable derivates
1.149     paf       425:                                                        self=value;
1.204     paf       426:                                                } else 
1.223     paf       427:                                                        throw Exception("parser.runtime",
1.204     paf       428:                                                                &frame->name(),
                    429:                                                                "is not a constructor, system class '%s' can be constructed only implicitly", 
                    430:                                                                called_class->name().cstr());
                    431: 
1.83      paf       432:                                                frame->write(*self, 
1.136     paf       433:                                                        String::UL_CLEAN  // not used, always an object, not string
1.83      paf       434:                                                );
1.185     parser    435:                                        } else
1.223     paf       436:                                                throw Exception("parser.runtime",
1.185     parser    437:                                                        &frame->name(),
                    438:                                                        "method is static and can not be used as constructor");
                    439:                                } else {
                    440:                                        // this is not constructor call
                    441: 
                    442:                                        // not ^name.method call, name:method call; and
                    443:                                        // is context object or class & is it my class or my parent's class and?
                    444:                                        VStateless_class *read_class=rcontext->get_class();
                    445:                                        if(
1.200     paf       446:                                                !(wcontext->get_somebody_entered_some_object() &&
                    447:                                                !wcontext->get_somebody_entered_some_class()) && 
1.185     parser    448:                                                read_class && read_class->is_or_derived_from(*called_class)) // yes
                    449:                                                self=rcontext; // dynamic call
                    450:                                        else // no, not me or relative of mine (=total stranger)
                    451:                                                self=&frame->junction.self; // static call
                    452:                                }
1.75      paf       453: 
1.45      paf       454:                                frame->set_self(*self);
1.219     paf       455: 
                    456:                                // see OP_PREPARE_TO_EXPRESSION
                    457:                                frame->set_in_expression(wcontext->get_in_expression());
                    458:                                
1.202     paf       459:                                rcontext=wcontext=frame;
1.47      paf       460:                                {
1.48      paf       461:                                        // take object or class from any wrappers
1.102     paf       462:                                        // and substitute class alias to the class they are called AS
                    463:                                        Temp_alias temp_alias(*self->get_aliased(), *frame->junction.vclass);
1.68      paf       464: 
1.99      paf       465:                                        const Method& method=*frame->junction.method;
1.134     paf       466:                                        Method::Call_type call_type=
                    467:                                                called_class==self ? Method::CT_STATIC : Method::CT_DYNAMIC;
                    468:                                        if(
                    469:                                                method.call_type==Method::CT_ANY ||
1.197     parser    470:                                                method.call_type==call_type) { // allowed call type?
                    471:                                                try {
                    472:                                                        if(method.native_code) { // native code?
1.202     paf       473:                                                                // root unchanged, so that ^for ^foreach & co may write to locals
1.197     parser    474:                                                                method.check_actual_numbered_params(
                    475:                                                                        frame->junction.self, 
                    476:                                                                        frame->name(), frame->numbered_params());
                    477:                                                                method.native_code(
                    478:                                                                        *this, 
                    479:                                                                        frame->name(), frame->numbered_params()); // execute it
                    480:                                                        } else { // parser code
1.202     paf       481:                                                                root=frame;
1.225     paf       482:                                                                // execute it
                    483:                                                                recoursion_checked_execute(&frame->name(), *method.parser_code);
1.159     parser    484:                                                        }
1.197     parser    485:                                                } catch(...) {
                    486:                                                        // record it to stack trace
1.222     paf       487:                                                        exception_trace.push((void *)&frame->name());
1.197     parser    488:                                                        /*re*/throw;
1.159     parser    489:                                                }
1.197     parser    490:                                        } else
1.223     paf       491:                                                throw Exception("parser.runtime",
1.134     paf       492:                                                        &frame->name(),
                    493:                                                        "is not allowed to be called %s", 
                    494:                                                                call_type==Method::CT_STATIC?"statically":"dynamically");
                    495: 
1.47      paf       496:                                }
1.228     paf       497:                                value=&wcontext->result().as_value(); // todo CALL_WRITE & VString removal
1.45      paf       498: 
                    499:                                wcontext=static_cast<WContext *>(POP());  
                    500:                                rcontext=POP();  
                    501:                                root=POP();  
                    502:                                self=static_cast<VAliased *>(POP());
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.61      paf       509:                                PUSH(value);
1.157     parser    510: #ifdef DEBUG_EXECUTE
1.158     parser    511:                                debug_printf(pool(), "<-returned");
1.157     parser    512: #endif
1.49      paf       513:                                break;
                    514:                        }
                    515: 
1.55      paf       516:                // expression ops: unary
                    517:                case OP_NEG:
                    518:                        {
                    519:                                Value *operand=POP();
1.197     parser    520:                                value=NEW VDouble(pool(), -operand->as_double());
1.55      paf       521:                                PUSH(value);
                    522:                                break;
                    523:                        }
                    524:                case OP_INV:
                    525:                        {
                    526:                                Value *operand=POP();
1.197     parser    527:                                value=NEW VDouble(pool(), ~operand->as_int());
1.55      paf       528:                                PUSH(value);
                    529:                                break;
                    530:                        }
                    531:                case OP_NOT:
                    532:                        {
                    533:                                Value *operand=POP();
1.197     parser    534:                                value=NEW VBool(pool(), !operand->as_bool());
1.55      paf       535:                                PUSH(value);
                    536:                                break;
                    537:                        }
1.62      paf       538:                case OP_DEF:
                    539:                        {
                    540:                                Value *operand=POP();
1.197     parser    541:                                value=NEW VBool(pool(), operand->is_defined());
1.62      paf       542:                                PUSH(value);
                    543:                                break;
                    544:                        }
                    545:                case OP_IN:
                    546:                        {
1.199     paf       547:                                /// @test String::cmp
1.62      paf       548:                                Value *operand=POP();
1.110     paf       549:                                const char *path=operand->as_string().cstr();
1.197     parser    550:                                value=NEW VBool(pool(), 
1.151     paf       551:                                        info.uri && strncmp(path, info.uri, strlen(path))==0);
1.62      paf       552:                                PUSH(value);
                    553:                                break;
                    554:                        }
                    555:                case OP_FEXISTS:
                    556:                        {
                    557:                                Value *operand=POP();
1.197     parser    558:                                value=NEW VBool(pool(), 
1.127     paf       559:                                        file_readable(absolute(operand->as_string())));
1.148     paf       560:                                PUSH(value);
                    561:                                break;
                    562:                        }
                    563:                case OP_DEXISTS:
                    564:                        {
                    565:                                Value *operand=POP();
1.197     parser    566:                                value=NEW VBool(pool(), 
1.148     paf       567:                                        dir_readable(absolute(operand->as_string())));
1.62      paf       568:                                PUSH(value);
                    569:                                break;
                    570:                        }
1.55      paf       571: 
                    572:                // expression ops: binary
                    573:                case OP_SUB: 
1.53      paf       574:                        {
1.197     parser    575:                                b=POP();  a=POP();
                    576:                                value=NEW VDouble(pool(), a->as_double() - b->as_double());
1.53      paf       577:                                PUSH(value);
                    578:                                break;
                    579:                        }
1.55      paf       580:                case OP_ADD: 
1.53      paf       581:                        {
1.197     parser    582:                                b=POP();  a=POP();
                    583:                                value=NEW VDouble(pool(), a->as_double() + b->as_double());
1.53      paf       584:                                PUSH(value);
                    585:                                break;
                    586:                        }
1.49      paf       587:                case OP_MUL: 
                    588:                        {
1.197     parser    589:                                b=POP();  a=POP();
                    590:                                value=NEW VDouble(pool(), a->as_double() * b->as_double());
1.53      paf       591:                                PUSH(value);
                    592:                                break;
                    593:                        }
                    594:                case OP_DIV: 
                    595:                        {
1.197     parser    596:                                b=POP();  a=POP();
1.170     parser    597: 
                    598:                                double a_double=a->as_double();
                    599:                                double b_double=b->as_double();
                    600: 
1.171     parser    601:                                if(b_double == 0) {
                    602:                                        const String *problem_source=&b->as_string();
                    603: #ifndef NO_STRING_ORIGIN
                    604:                                        if(!problem_source->origin().file)
1.172     parser    605:                                                problem_source=&b->name();
1.171     parser    606: #endif
1.223     paf       607:                                        throw Exception("number.zerodivision",
1.171     parser    608:                                                problem_source,
1.170     parser    609:                                                "Division by zero");
1.171     parser    610:                                }
1.170     parser    611: 
1.197     parser    612:                                value=NEW VDouble(pool(), a_double / b_double);
1.54      paf       613:                                PUSH(value);
                    614:                                break;
                    615:                        }
1.55      paf       616:                case OP_MOD: 
                    617:                        {
1.197     parser    618:                                b=POP();  a=POP();
1.170     parser    619: 
1.173     parser    620:                                double a_double=a->as_double();
                    621:                                double b_double=b->as_double();
1.170     parser    622: 
1.173     parser    623:                                if(b_double == 0) {
1.171     parser    624:                                        const String *problem_source=&b->as_string();
                    625: #ifndef NO_STRING_ORIGIN
                    626:                                        if(!problem_source->origin().file)
1.172     parser    627:                                                problem_source=&b->name();
1.171     parser    628: #endif
1.223     paf       629:                                        throw Exception("number.zerodivision",
1.171     parser    630:                                                problem_source,
1.170     parser    631:                                                "Modulus by zero");
1.171     parser    632:                                }
1.170     parser    633: 
1.197     parser    634:                                value=NEW VDouble(pool(), fmod(a_double, b_double));
1.201     paf       635:                                PUSH(value);
                    636:                                break;
                    637:                        }
                    638:                case OP_INTDIV:
                    639:                        {
                    640:                                b=POP();  a=POP();
                    641: 
                    642:                                int a_int=a->as_int();
                    643:                                int b_int=b->as_int();
                    644: 
                    645:                                if(b_int == 0) {
                    646:                                        const String *problem_source=&b->as_string();
                    647: #ifndef NO_STRING_ORIGIN
                    648:                                        if(!problem_source->origin().file)
                    649:                                                problem_source=&b->name();
                    650: #endif
1.223     paf       651:                                        throw Exception("number.zerodivision",
1.201     paf       652:                                                problem_source,
                    653:                                                "Division by zero");
                    654:                                }
                    655: 
                    656:                                value=NEW VInt(pool(), a_int / b_int);
1.55      paf       657:                                PUSH(value);
                    658:                                break;
                    659:                        }
                    660:                case OP_BIN_AND:
1.54      paf       661:                        {
1.197     parser    662:                                b=POP();  a=POP();
                    663:                                value=NEW VDouble(pool(), 
1.155     parser    664:                                        a->as_int() &
                    665:                                        b->as_int());
1.54      paf       666:                                PUSH(value);
                    667:                                break;
                    668:                        }
1.55      paf       669:                case OP_BIN_OR:
1.54      paf       670:                        {
1.197     parser    671:                                b=POP();  a=POP();
                    672:                                value=NEW VDouble(pool(), 
1.155     parser    673:                                        a->as_int() |
                    674:                                        b->as_int());
1.55      paf       675:                                PUSH(value);
                    676:                                break;
                    677:                        }
1.56      paf       678:                case OP_BIN_XOR:
                    679:                        {
1.197     parser    680:                                b=POP();  a=POP();
                    681:                                value=NEW VDouble(pool(), 
1.155     parser    682:                                        a->as_int() ^
                    683:                                        b->as_int());
1.56      paf       684:                                PUSH(value);
                    685:                                break;
                    686:                        }
1.55      paf       687:                case OP_LOG_AND:
                    688:                        {
1.209     paf       689:                                b_code=POP_CODE();  a=POP();
                    690:                                bool result;
                    691:                                if(a->as_bool()) {
                    692:                                        execute(*b_code);
                    693:                                        b=POP();
                    694:                                        result=b->as_bool();
                    695:                                } else
                    696:                                        result=false;
                    697:                                value=NEW VBool(pool(), result);
1.55      paf       698:                                PUSH(value);
                    699:                                break;
                    700:                        }
                    701:                case OP_LOG_OR:
                    702:                        {
1.209     paf       703:                                b_code=POP_CODE();  a=POP();
                    704:                                bool result;
                    705:                                if(a->as_bool()) 
                    706:                                        result=true;
                    707:                                else {
                    708:                                        execute(*b_code);
                    709:                                        b=POP();
                    710:                                        result=b->as_bool();
                    711:                                }
                    712:                                value=NEW VBool(pool(), result);
1.56      paf       713:                                PUSH(value);
                    714:                                break;
                    715:                        }
                    716:                case OP_LOG_XOR:
                    717:                        {
1.197     parser    718:                                b=POP();  a=POP();
                    719:                                value=NEW VBool(pool(), a->as_bool() ^ b->as_bool());
1.55      paf       720:                                PUSH(value);
                    721:                                break;
                    722:                        }
                    723:                case OP_NUM_LT: 
                    724:                        {
1.197     parser    725:                                b=POP();  a=POP();
                    726:                                value=NEW VBool(pool(), a->as_double() < b->as_double());
1.55      paf       727:                                PUSH(value);
                    728:                                break;
                    729:                        }
                    730:                case OP_NUM_GT: 
                    731:                        {
1.197     parser    732:                                b=POP();  a=POP();
                    733:                                value=NEW VBool(pool(), a->as_double() > b->as_double());
1.55      paf       734:                                PUSH(value);
                    735:                                break;
                    736:                        }
                    737:                case OP_NUM_LE: 
                    738:                        {
1.197     parser    739:                                b=POP();  a=POP();
                    740:                                value=NEW VBool(pool(), a->as_double() <= b->as_double());
1.55      paf       741:                                PUSH(value);
                    742:                                break;
                    743:                        }
                    744:                case OP_NUM_GE: 
                    745:                        {
1.197     parser    746:                                b=POP();  a=POP();
                    747:                                value=NEW VBool(pool(), a->as_double() >= b->as_double());
1.55      paf       748:                                PUSH(value);
                    749:                                break;
                    750:                        }
                    751:                case OP_NUM_EQ: 
                    752:                        {
1.197     parser    753:                                b=POP();  a=POP();
                    754:                                value=NEW VBool(pool(), a->as_double() == b->as_double());
1.55      paf       755:                                PUSH(value);
                    756:                                break;
                    757:                        }
                    758:                case OP_NUM_NE: 
                    759:                        {
1.197     parser    760:                                b=POP();  a=POP();
                    761:                                value=NEW VBool(pool(), a->as_double() != b->as_double());
1.54      paf       762:                                PUSH(value);
                    763:                                break;
                    764:                        }
1.58      paf       765:                case OP_STR_LT: 
                    766:                        {
1.197     parser    767:                                b=POP();  a=POP();
                    768:                                value=NEW VBool(pool(), a->as_string() < b->as_string());
1.58      paf       769:                                PUSH(value);
                    770:                                break;
                    771:                        }
                    772:                case OP_STR_GT: 
                    773:                        {
1.197     parser    774:                                b=POP();  a=POP();
                    775:                                value=NEW VBool(pool(), a->as_string() > b->as_string());
1.58      paf       776:                                PUSH(value);
                    777:                                break;
                    778:                        }
1.55      paf       779:                case OP_STR_LE: 
1.58      paf       780:                        {
1.197     parser    781:                                b=POP();  a=POP();
                    782:                                value=NEW VBool(pool(), a->as_string() <= b->as_string());
1.58      paf       783:                                PUSH(value);
                    784:                                break;
                    785:                        }
1.55      paf       786:                case OP_STR_GE: 
1.54      paf       787:                        {
1.197     parser    788:                                b=POP();  a=POP();
                    789:                                value=NEW VBool(pool(), a->as_string() >= b->as_string());
1.58      paf       790:                                PUSH(value);
                    791:                                break;
                    792:                        }
                    793:                case OP_STR_EQ: 
                    794:                        {
1.197     parser    795:                                b=POP();  a=POP();
                    796:                                value=NEW VBool(pool(), a->as_string() == b->as_string());
1.58      paf       797:                                PUSH(value);
                    798:                                break;
                    799:                        }
                    800:                case OP_STR_NE: 
                    801:                        {
1.197     parser    802:                                b=POP();  a=POP();
                    803:                                value=NEW VBool(pool(), a->as_string() != b->as_string());
1.103     paf       804:                                PUSH(value);
                    805:                                break;
                    806:                        }
                    807:                case OP_IS:
                    808:                        {
1.191     parser    809:                                //_asm int 3;
1.197     parser    810:                                b=POP();  a=POP();
                    811:                                value=NEW VBool(pool(), b->as_string() == a->type());
1.49      paf       812:                                PUSH(value);
1.28      paf       813:                                break;
                    814:                        }
                    815: 
1.11      paf       816:                default:
1.223     paf       817:                        throw Exception(0,
1.67      paf       818:                                0,
1.139     paf       819:                                "invalid opcode %d", op.code); 
1.11      paf       820:                }
                    821:        }
1.1       paf       822: }
1.17      paf       823: 
1.215     paf       824: Value *Request::get_element(bool can_call_operator) {
1.82      paf       825:        const String& name=POP_NAME();
1.24      paf       826:        Value *ncontext=POP();
1.216     paf       827:        Value *value=0;
                    828:        if(can_call_operator)
                    829:                if(Method* method=OP.get_method(name)) { // looking operator of that name FIRST
1.181     parser    830:                        // as if that method were in self and we have normal dynamic method here
                    831:                        Junction& junction=*NEW Junction(pool(), 
1.202     paf       832:                                *root, self->get_class(), method, 0,0,0,0);
1.181     parser    833:                        value=NEW VJunction(junction);
                    834:                }
1.216     paf       835:        if(!value)
                    836:                value=ncontext->get_element(name);
1.229     paf       837:        if(value) {
                    838:                value=&process_to_value(*value); // process possible code-junction
                    839:                value->update_name(name);
                    840:        } else {
1.167     parser    841:                value=NEW VVoid(pool());
1.76      paf       842:                value->set_name(name);
                    843:        }
1.63      paf       844: 
1.17      paf       845:        return value;
1.34      paf       846: }
1.70      paf       847: 
1.162     parser    848: /**    @param intercept_string
1.116     paf       849:        - true:
                    850:                they want result=string value, 
                    851:                possible object result goes to wcontext
                    852:        - false:
                    853:                they want any result[string|object]
                    854:                nothing goes to wcontext.
1.125     paf       855:                used in @c (expression) params evaluation
1.224     paf       856: 
                    857:     using the fact it's either string_ or value_ result requested to speed up checkes
1.116     paf       858: */
1.229     paf       859: StringOrValue Request::process(Value& input_value, bool intercept_string) {
1.228     paf       860:        StringOrValue result;
1.224     paf       861:        Junction *junction=input_value.get_junction();
1.70      paf       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.225     paf       872:                self=&junction->self;
                    873:                root=junction->root;
                    874:                rcontext=junction->rcontext;
                    875: 
1.109     paf       876:                // for expression method params
                    877:                // wcontext is set 0
                    878:                // using the fact in decision "which wwrapper to use"
                    879:                bool using_code_frame=intercept_string && junction->wcontext;
                    880:                if(using_code_frame) {
1.71      paf       881:                        // almost plain wwrapper about junction wcontext, 
                    882:                        // BUT intercepts string writes
1.225     paf       883:                        VCodeFrame local(pool(), *junction->wcontext);  
                    884:                        wcontext=&local;
1.207     paf       885: 
1.225     paf       886:                        // execute it
1.228     paf       887:                        recoursion_checked_execute(0/*result_name*/, *junction->code);
1.226     paf       888:                        
1.71      paf       889:                        // CodeFrame soul:
                    890:                        //   string writes were intercepted
                    891:                        //   returning them as the result of getting code-junction
1.229     paf       892:                        result.set_string(*wcontext->get_string());
1.224     paf       893:                } else {
1.225     paf       894:                        // plain wwrapper
                    895:                        WWrapper local(pool(), 0/*empty*/);
                    896:                        wcontext=&local;
                    897:                
                    898:                        // execute it
1.228     paf       899:                        recoursion_checked_execute(0/*result_name*/, *junction->code);
1.226     paf       900:                
1.228     paf       901:                        result=wcontext->result();
1.224     paf       902:                }
1.70      paf       903:                
                    904:                wcontext=static_cast<WContext *>(POP());  
                    905:                rcontext=POP();  
                    906:                root=POP();  
                    907:                self=static_cast<VAliased *>(POP());
                    908:                
1.157     parser    909: #ifdef DEBUG_EXECUTE
1.158     parser    910:                debug_printf(pool(), "<-ja returned");
1.157     parser    911: #endif
1.224     paf       912:        } else {
1.228     paf       913:                result.set_value(input_value);
1.224     paf       914:        }
1.228     paf       915:        return result;
1.85      paf       916: }
                    917: 
1.208     paf       918: const String *Request::execute_method(Value& aself, const Method& method,
                    919:                bool return_cstr) {
1.99      paf       920:        PUSH(self);  
                    921:        PUSH(root);  
                    922:        PUSH(rcontext);  
                    923:        PUSH(wcontext);
                    924:        
                    925:        // initialize contexts
                    926:        root=rcontext=self=&aself;
1.226     paf       927:        WWrapper local(pool(), &aself);
                    928:        wcontext=&local; 
1.99      paf       929:        
                    930:        // execute!     
                    931:        execute(*method.parser_code);
                    932:        
                    933:        // result
1.208     paf       934:        const String *result=return_cstr ? &wcontext->as_string() : 0;
                    935:        
                    936:        wcontext=static_cast<WContext *>(POP());  
                    937:        rcontext=POP();  
                    938:        root=POP();  
                    939:        self=static_cast<VAliased *>(POP());
                    940:        
                    941:        // return
                    942:        return result;
                    943: }
                    944: 
                    945: const String& Request::execute_method(VMethodFrame& amethodFrame, const Method& method) {
                    946:        PUSH(self);  
                    947:        PUSH(root);  
                    948:        PUSH(rcontext);  
                    949:        PUSH(wcontext);
                    950:        
                    951:        // initialize contexts
                    952:        root=rcontext=self=&amethodFrame;
                    953:        wcontext=&amethodFrame;
                    954:        
                    955:        // execute!     
                    956:        execute(*method.parser_code);
                    957:        
                    958:        // result
                    959:        const String& result=wcontext->as_string();
1.99      paf       960:        
                    961:        wcontext=static_cast<WContext *>(POP());  
                    962:        rcontext=POP();  
                    963:        root=POP();  
                    964:        self=static_cast<VAliased *>(POP());
                    965:        
                    966:        // return
                    967:        return result;
                    968: }
                    969: 
1.176     parser    970: const String *Request::execute_virtual_method(Value& aself, 
1.208     paf       971:                                                                                          const String& method_name) {
1.99      paf       972:        if(Value *value=aself.get_element(method_name))
                    973:                if(Junction *junction=value->get_junction())
                    974:                        if(const Method *method=junction->method) 
1.208     paf       975:                                return execute_method(aself, *method, true /*return_cstr*/);
1.188     parser    976:                        
1.176     parser    977:        return 0;
                    978: }
                    979: 
1.178     parser    980: const String *Request::execute_nonvirtual_method(VStateless_class& aclass, 
1.208     paf       981:                                                                                                 const String& method_name,
1.176     parser    982:                                                                                                 bool return_cstr) {
1.178     parser    983:        if(const Method *method=aclass.get_method(method_name))
1.179     parser    984:                return execute_method(aclass, *method, return_cstr);
1.99      paf       985: 
1.85      paf       986:        return 0;
1.70      paf       987: }

E-mail: