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

1.117     paf         1: /** @file
1.118     paf         2:        Parser: executor part of request class.
                      3: 
1.368     moko        4:        Copyright (c) 2001-2012 Art. Lebedev Studio (http://www.artlebedev.com)
1.218     paf         5:        Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.243     paf         6: */
1.118     paf         7: 
1.152     paf         8: #include "pa_opcode.h"
1.8       paf         9: #include "pa_array.h" 
1.11      paf        10: #include "pa_request.h"
1.15      paf        11: #include "pa_vstring.h"
1.22      paf        12: #include "pa_vhash.h"
1.167     parser     13: #include "pa_vvoid.h"
1.145     paf        14: #include "pa_vcode_frame.h"
1.321     misha      15: #include "pa_vmethod_frame.h"
1.38      paf        16: #include "pa_vobject.h"
1.49      paf        17: #include "pa_vdouble.h"
1.54      paf        18: #include "pa_vbool.h"
1.94      paf        19: #include "pa_vtable.h"
1.132     paf        20: #include "pa_vfile.h"
1.144     paf        21: #include "pa_vimage.h"
1.194     parser     22: #include "pa_wwrapper.h"
1.1       paf        23: 
1.373   ! moko       24: volatile const char * IDENT_EXECUTE_C="$Id: execute.C,v 1.372 2015/03/16 09:47:34 misha Exp $" IDENT_PA_OPCODE_H IDENT_PA_OPERATION_H IDENT_PA_VCODE_FRAME_H IDENT_PA_WWRAPPER_H;
1.368     moko       25: 
1.233     paf        26: //#define DEBUG_EXECUTE
1.157     parser     27: 
                     28: #ifdef DEBUG_EXECUTE
1.1       paf        29: char *opcode_name[]={
1.49      paf        30:        // literals
1.109     paf        31:        "VALUE",  "CURLY_CODE__STORE_PARAM",  "EXPR_CODE__STORE_PARAM",
1.209     paf        32:        "NESTED_CODE",
1.49      paf        33: 
                     34:        // actions
1.187     parser     35:        "WITH_ROOT",    "WITH_SELF",    "WITH_READ",    "WITH_WRITE",
1.328     misha      36:        "VALUE__GET_CLASS",
1.182     parser     37:        "CONSTRUCT_VALUE", "CONSTRUCT_EXPR", "CURLY_CODE__CONSTRUCT",
1.108     paf        38:        "WRITE_VALUE",  "WRITE_EXPR_RESULT",  "STRING__WRITE",
1.328     misha      39: #ifdef OPTIMIZE_BYTECODE_GET_ELEMENT
                     40:        "VALUE__GET_ELEMENT_OR_OPERATOR",
                     41: #else
                     42:        "GET_ELEMENT_OR_OPERATOR",
                     43: #endif
                     44:        "GET_ELEMENT",
1.346     misha      45:        "GET_ELEMENT__WRITE",
                     46: #ifdef OPTIMIZE_BYTECODE_GET_ELEMENT
                     47:        "VALUE__GET_ELEMENT",
                     48:        "VALUE__GET_ELEMENT__WRITE",
1.347     misha      49:        "WITH_ROOT__VALUE__GET_ELEMENT",
1.346     misha      50: #endif
1.335     misha      51: #ifdef OPTIMIZE_BYTECODE_GET_OBJECT_ELEMENT
                     52:        "GET_OBJECT_ELEMENT",
                     53:        "GET_OBJECT_ELEMENT__WRITE",
                     54: #endif
                     55: #ifdef OPTIMIZE_BYTECODE_GET_OBJECT_VAR_ELEMENT
                     56:        "GET_OBJECT_VAR_ELEMENT",
                     57:        "GET_OBJECT_VAR_ELEMENT__WRITE",
1.334     misha      58: #endif
1.345     misha      59: #ifdef OPTIMIZE_BYTECODE_GET_SELF_ELEMENT
                     60:        "WITH_SELF__VALUE__GET_ELEMENT",
                     61:        "WITH_SELF__VALUE__GET_ELEMENT__WRITE",
                     62: #endif
1.232     paf        63:        "OBJECT_POOL",  "STRING_POOL",
1.348     misha      64:        "PREPARE_TO_CONSTRUCT_OBJECT",
                     65:        "CONSTRUCT_OBJECT",
                     66:        "CONSTRUCT_OBJECT__WRITE",
                     67:        "PREPARE_TO_EXPRESSION", 
1.232     paf        68:        "CALL", "CALL__WRITE",
1.49      paf        69: 
1.337     misha      70: #ifdef OPTIMIZE_BYTECODE_CONSTRUCT
1.344     misha      71:        "WITH_ROOT__VALUE__CONSTRUCT_EXPR",
                     72:        "WITH_ROOT__VALUE__CONSTRUCT_VALUE",
                     73:        "WITH_WRITE__VALUE__CONSTRUCT_EXPR",
                     74:        "WITH_WRITE__VALUE__CONSTRUCT_VALUE",
1.345     misha      75:        "WITH_SELF__VALUE__CONSTRUCT_EXPR",
                     76:        "WITH_SELF__VALUE__CONSTRUCT_VALUE",
1.337     misha      77: #endif
                     78: 
1.372     misha      79: #ifdef OPTIMIZE_BYTECODE_GET_ELEMENT__SPECIAL
                     80:        "GET_ELEMENT__SPECIAL",
                     81:        "GET_ELEMENT__SPECIAL__WRITE",
                     82: #endif
1.49      paf        83:        // expression ops: unary
1.148     paf        84:        "NEG", "INV", "NOT", "DEF", "IN", "FEXISTS", "DEXISTS",
1.49      paf        85:        // expression ops: binary
1.201     paf        86:        "SUB", "ADD", "MUL", "DIV", "MOD", "INTDIV",
1.275     paf        87:        "BIN_SL", "BIN_SR",
1.56      paf        88:        "BIN_AND", "BIN_OR", "BIN_XOR",
                     89:        "LOG_AND", "LOG_OR", "LOG_XOR",
1.49      paf        90:        "NUM_LT", "NUM_GT", "NUM_LE", "NUM_GE", "NUM_EQ", "NUM_NE",
1.103     paf        91:        "STR_LT", "STR_GT", "STR_LE", "STR_GE", "STR_EQ", "STR_NE",
                     92:        "IS"
1.1       paf        93: };
                     94: 
1.342     misha      95: const char* debug_value_to_cstr(Value& value){
                     96:        const String* string=value.get_string();
                     97: 
                     98:        if(string)
                     99:                return string->cstr();
                    100:        else
                    101:                if(value.is_bool())
                    102:                        return value.as_bool()?"<true>":"<false>";
                    103:                else
                    104:                        return "<value>";
                    105: }
                    106: 
1.297     paf       107: void va_debug_printf(SAPI_Info& sapi_info, const char* fmt,va_list args) {
1.127     paf       108:        char buf[MAX_STRING];
                    109:        vsnprintf(buf, MAX_STRING, fmt, args);
1.297     paf       110:        SAPI::log(sapi_info, "%s", buf);
1.107     paf       111: }
                    112: 
1.297     paf       113: void debug_printf(SAPI_Info& sapi_info, const char* fmt, ...) {
1.334     misha     114:        va_list args;
                    115:        va_start(args, fmt);
                    116:        va_debug_printf(sapi_info, fmt, args);
                    117:        va_end(args);
1.105     paf       118: }
                    119: 
1.297     paf       120: void debug_dump(SAPI_Info& sapi_info, int level, ArrayOperation& ops) {
                    121:        Array_iterator<Operation> i(ops);
1.190     parser    122:        while(i.has_next()) {
1.323     misha     123:                OP::OPCODE opcode=i.next().code;
1.1       paf       124: 
1.337     misha     125: #if defined(OPTIMIZE_BYTECODE_GET_OBJECT_ELEMENT) || defined(OPTIMIZE_BYTECODE_GET_OBJECT_VAR_ELEMENT)
1.334     misha     126:                if(
1.335     misha     127:                        1==0
                    128: #ifdef OPTIMIZE_BYTECODE_GET_OBJECT_ELEMENT
                    129:                        || opcode==OP::OP_GET_OBJECT_ELEMENT
                    130:                        || opcode==OP::OP_GET_OBJECT_ELEMENT__WRITE
                    131: #endif
                    132: #ifdef OPTIMIZE_BYTECODE_GET_OBJECT_VAR_ELEMENT
                    133:                        || opcode==OP::OP_GET_OBJECT_VAR_ELEMENT
                    134:                        || opcode==OP::OP_GET_OBJECT_VAR_ELEMENT__WRITE
                    135: #endif
1.334     misha     136:                ){
                    137:                        i.next(); // skip origin
                    138:                        Value& value1=*i.next().value;
                    139:                        i.next(); // skip origin
                    140:                        Value& value2=*i.next().value;
                    141:                        debug_printf(sapi_info, 
                    142:                                "%*s%s"
                    143:                                " \"%s\" \"%s\"", 
                    144:                                level*4, "", opcode_name[opcode],
1.342     misha     145:                                debug_value_to_cstr(value1), debug_value_to_cstr(value2));
1.334     misha     146:                        continue;
                    147:                }
                    148: #endif
1.348     misha     149:                if(
                    150:                        opcode==OP::OP_CONSTRUCT_OBJECT
                    151:                        || opcode==OP::OP_CONSTRUCT_OBJECT__WRITE
                    152:                ){
                    153:                        i.next(); // skip origin
                    154:                        Value& value1=*i.next().value;
                    155:                        i.next(); // skip origin
                    156:                        Value& value2=*i.next().value;
                    157:                        debug_printf(sapi_info, 
                    158:                                "%*s%s"
                    159:                                " \"%s\" \"%s\"", 
                    160:                                level*4, "", opcode_name[opcode],
                    161:                                debug_value_to_cstr(value1), debug_value_to_cstr(value2));
                    162: 
                    163:                        if(ArrayOperation* local_ops=i.next().ops)
                    164:                                debug_dump(sapi_info, level+1, *local_ops);
                    165:                        continue;
                    166:                }
1.328     misha     167:                if(
                    168:                        opcode==OP::OP_VALUE
                    169:                        || opcode==OP::OP_STRING__WRITE
                    170:                        || opcode==OP::OP_VALUE__GET_CLASS
                    171: #ifdef OPTIMIZE_BYTECODE_GET_ELEMENT
                    172:                        || opcode==OP::OP_VALUE__GET_ELEMENT
                    173:                        || opcode==OP::OP_VALUE__GET_ELEMENT__WRITE
                    174:                        || opcode==OP::OP_VALUE__GET_ELEMENT_OR_OPERATOR
1.347     misha     175:                        || opcode==OP::OP_WITH_ROOT__VALUE__GET_ELEMENT
1.328     misha     176: #endif
1.345     misha     177: #ifdef OPTIMIZE_BYTECODE_GET_SELF_ELEMENT
                    178:                        || opcode==OP::OP_WITH_SELF__VALUE__GET_ELEMENT
                    179:                        || opcode==OP::OP_WITH_SELF__VALUE__GET_ELEMENT__WRITE
                    180: #endif
1.342     misha     181: #ifdef OPTIMIZE_BYTECODE_CONSTRUCT
1.344     misha     182:                        || opcode==OP::OP_WITH_ROOT__VALUE__CONSTRUCT_EXPR
                    183:                        || opcode==OP::OP_WITH_ROOT__VALUE__CONSTRUCT_VALUE
                    184:                        || opcode==OP::OP_WITH_WRITE__VALUE__CONSTRUCT_EXPR
                    185:                        || opcode==OP::OP_WITH_WRITE__VALUE__CONSTRUCT_VALUE
1.345     misha     186:                        || opcode==OP::OP_WITH_SELF__VALUE__CONSTRUCT_EXPR
                    187:                        || opcode==OP::OP_WITH_SELF__VALUE__CONSTRUCT_VALUE
1.342     misha     188: #endif
1.328     misha     189:                ) {
1.297     paf       190:                        Operation::Origin origin=i.next().origin;
                    191:                        Value& value=*i.next().value;
1.342     misha     192: 
1.297     paf       193:                        debug_printf(sapi_info, 
1.133     paf       194:                                "%*s%s"
                    195:                                " \"%s\" %s", 
1.297     paf       196:                                level*4, "", opcode_name[opcode],
1.342     misha     197:                                debug_value_to_cstr(value), value.type());
1.133     paf       198:                        continue;
1.15      paf       199:                }
1.348     misha     200: 
1.297     paf       201:                debug_printf(sapi_info, "%*s%s", level*4, "", opcode_name[opcode]);
1.1       paf       202: 
1.297     paf       203:                switch(opcode) {
1.323     misha     204:                case OP::OP_CURLY_CODE__STORE_PARAM: 
                    205:                case OP::OP_EXPR_CODE__STORE_PARAM:
                    206:                case OP::OP_CURLY_CODE__CONSTRUCT:
                    207:                case OP::OP_NESTED_CODE:
1.346     misha     208:                case OP::OP_OBJECT_POOL:
1.323     misha     209:                case OP::OP_STRING_POOL:
                    210:                case OP::OP_CALL:
                    211:                case OP::OP_CALL__WRITE:
1.297     paf       212:                        if(ArrayOperation* local_ops=i.next().ops)
                    213:                                debug_dump(sapi_info, level+1, *local_ops);
1.1       paf       214:                }
                    215:        }
                    216: }
1.336     misha     217: #define DEBUG_PRINT_STR(str) debug_printf(sapi_info, str);
                    218: #define DEBUG_PRINT_STRING(value) debug_printf(sapi_info, " \"%s\" ", value.cstr());
1.342     misha     219: #define DEBUG_PRINT_VALUE_AND_TYPE(value) debug_printf(sapi_info, " \"%s\" %s", debug_value_to_cstr(value), value.type());
1.336     misha     220: #define DEBUG_PRINT_OPS(local_ops) \
                    221:                                        debug_printf(sapi_info, \
                    222:                                        " (%d)\n", local_ops?local_ops->count():0); \
                    223:                                        if(local_ops) debug_dump(sapi_info, 1, *local_ops);
                    224: 
                    225: #else
                    226: #define DEBUG_PRINT_STR(str)
                    227: #define DEBUG_PRINT_STRING(value)
1.342     misha     228: #define DEBUG_PRINT_VALUE_AND_TYPE(value)
1.336     misha     229: #define DEBUG_PRINT_OPS(local_ops)
1.157     parser    230: #endif
1.1       paf       231: 
1.336     misha     232: 
1.297     paf       233: // Request
                    234: 
                    235: void Request::execute(ArrayOperation& ops) {
                    236:        register Stack<StackItem>& stack=this->stack; // helps a lot on MSVC: 'esi'
1.159     parser    237: 
1.304     paf       238:        const String* debug_name=0;  Operation::Origin debug_origin={0, 0, 0};
1.297     paf       239:        try{
1.157     parser    240: #ifdef DEBUG_EXECUTE
1.297     paf       241:        debug_printf(sapi_info, "source----------------------------\n");
                    242:        debug_dump(sapi_info, 0, ops);
                    243:        debug_printf(sapi_info, "execution-------------------------\n");
1.157     parser    244: #endif
1.297     paf       245:        for(Array_iterator<Operation> i(ops); i.has_next(); ) {
1.322     misha     246:                OP::OPCODE opcode=i.next().code;
1.293     paf       247: 
1.157     parser    248: #ifdef DEBUG_EXECUTE
1.297     paf       249:                debug_printf(sapi_info, "%d:%s", stack.top_index()+1, opcode_name[opcode]);
1.157     parser    250: #endif
1.11      paf       251: 
1.297     paf       252:                switch(opcode) {
1.51      paf       253:                // param in next instruction
1.322     misha     254:                case OP::OP_VALUE:
1.32      paf       255:                        {
1.297     paf       256:                                debug_origin=i.next().origin;
                    257:                                Value& value=*i.next().value;
1.342     misha     258: 
                    259:                                DEBUG_PRINT_VALUE_AND_TYPE(value)
                    260: 
1.297     paf       261:                                stack.push(value);
1.32      paf       262:                                break;
                    263:                        }
1.328     misha     264:                case OP::OP_VALUE__GET_CLASS:
                    265:                        {
                    266:                                // maybe they do ^class:method[] call, remember the fact
                    267:                                wcontext->set_somebody_entered_some_class();
                    268: 
                    269:                                debug_origin=i.next().origin;
1.372     misha     270:                                const String& name=*i.next().value->get_string(); debug_name=&name;
1.328     misha     271: 
1.336     misha     272:                                DEBUG_PRINT_STRING(name)
1.328     misha     273: 
1.358     misha     274:                                Value* class_value=get_class(name);
1.328     misha     275:                                if(!class_value)
                    276:                                        throw Exception(PARSER_RUNTIME,
                    277:                                                &name,
                    278:                                                "class is undefined"); 
                    279: 
                    280:                                stack.push(*class_value);
                    281:                                break;
                    282:                        }
1.372     misha     283: 
                    284: #ifdef OPTIMIZE_BYTECODE_GET_ELEMENT__SPECIAL
                    285:                case OP::OP_GET_ELEMENT__SPECIAL:
                    286:                case OP::OP_GET_ELEMENT__SPECIAL__WRITE:
                    287:                        {
                    288:                                const String& name=stack.pop().string();  debug_name=&name;
                    289:                                Value& ncontext=stack.pop().value();
                    290: 
                    291:                                Value* value=0;
                    292:                                if(VStateless_class* vclass=ncontext.get_class()){
                    293:                                        if(name==class_element_name){
                    294:                                                value=vclass;
                    295:                                        } else if(name==class_name_element_name){
                    296:                                                value=new VString(vclass->name());
                    297:                                        }
                    298:                                } else {
                    299:                                        // Value
                    300:                                        if(name==class_element_name){
                    301:                                                value=&ncontext;
                    302:                                        }else if(name==class_name_element_name){
                    303:                                                value=new VString(*new String(ncontext.type()));
                    304:                                        }
                    305:                                };
                    306:                                if(opcode==OP::OP_GET_ELEMENT__SPECIAL){
                    307:                                        stack.push(*value);
                    308:                                } else {
                    309:                                        write_assign_lang(*value);
                    310:                                }
                    311:                                break;
                    312:                        }
                    313: #endif
                    314: 
1.51      paf       315:                // OP_WITH
1.322     misha     316:                case OP::OP_WITH_ROOT:
1.187     parser    317:                        {
1.297     paf       318:                                stack.push(*method_frame);
1.187     parser    319:                                break;
                    320:                        }
1.336     misha     321:                case OP::OP_WITH_SELF:
1.37      paf       322:                        {
1.297     paf       323:                                stack.push(get_self());
1.37      paf       324:                                break;
                    325:                        }
1.336     misha     326:                case OP::OP_WITH_READ:
1.15      paf       327:                        {
1.297     paf       328:                                stack.push(*rcontext);
1.20      paf       329:                                break;
                    330:                        }
1.336     misha     331:                case OP::OP_WITH_WRITE:
1.20      paf       332:                        {
1.287     paf       333:                                if(wcontext==method_frame)
1.316     misha     334:                                        throw Exception(PARSER_RUNTIME,
1.287     paf       335:                                                0,
                    336:                                                "$.name outside of $name[...]");
                    337: 
1.297     paf       338:                                stack.push(*wcontext);
1.20      paf       339:                                break;
                    340:                        }
1.37      paf       341:                        
1.51      paf       342:                // OTHER ACTIONS BUT WITHs
1.337     misha     343: 
                    344: #ifdef OPTIMIZE_BYTECODE_CONSTRUCT
1.347     misha     345: #define DO_CONSTRUCT(context, vvalue) {                                                 \
                    346:                                debug_origin=i.next().origin;                                           \
                    347:                                const String& name=*i.next().value->get_string();  debug_name=&name;    \
                    348:                                DEBUG_PRINT_STRING(name)                                                \
                    349:                                Value& value=stack.pop().value();                                       \
                    350:                                put_element( context, name, vvalue );                                   \
                    351:                                break;                                                                  \
                    352:                }
                    353: #define DO_CONSTRUCT_VALUE(context) DO_CONSTRUCT(context, &value)
                    354: #define DO_CONSTRUCT_EXPR(context) {                                                    \
                    355:                                wcontext->set_in_expression(false);                                     \
                    356:                                DO_CONSTRUCT(context, &value.as_expr_result())                          \
                    357:                }
                    358: 
1.344     misha     359:                case OP::OP_WITH_WRITE__VALUE__CONSTRUCT_EXPR:
1.347     misha     360:                        {
                    361:                                if(wcontext==method_frame)
                    362:                                        throw Exception(PARSER_RUNTIME, 0, "$.name outside of $name[...]");
                    363:                                DO_CONSTRUCT_EXPR(*wcontext)
                    364:                        }
                    365: 
1.344     misha     366:                case OP::OP_WITH_WRITE__VALUE__CONSTRUCT_VALUE:
1.338     misha     367:                        {
                    368:                                if(wcontext==method_frame)
1.347     misha     369:                                        throw Exception(PARSER_RUNTIME, 0, "$.name outside of $name[...]");
                    370:                                DO_CONSTRUCT_VALUE(*wcontext)
1.338     misha     371:                        }
1.345     misha     372: 
1.347     misha     373:                case OP::OP_WITH_ROOT__VALUE__CONSTRUCT_EXPR:   DO_CONSTRUCT_EXPR(*method_frame)
1.337     misha     374: 
1.347     misha     375:                case OP::OP_WITH_ROOT__VALUE__CONSTRUCT_VALUE:  DO_CONSTRUCT_VALUE(*method_frame)
1.337     misha     376: 
1.347     misha     377:                case OP::OP_WITH_SELF__VALUE__CONSTRUCT_EXPR:   DO_CONSTRUCT_EXPR(get_self())
1.342     misha     378: 
1.347     misha     379:                case OP::OP_WITH_SELF__VALUE__CONSTRUCT_VALUE:  DO_CONSTRUCT_VALUE(get_self())
1.345     misha     380: 
1.340     misha     381: #endif // OPTIMIZE_BYTECODE_CONSTRUCT
1.337     misha     382: 
1.344     misha     383:                case OP::OP_CONSTRUCT_VALUE:
1.337     misha     384:                        {
1.344     misha     385: #ifdef OPTIMIZE_BYTECODE_CONSTRUCT
                    386:                                const String& name=stack.pop().string();  debug_name=&name;
                    387:                                Value& ncontext=stack.pop().value();
                    388:                                Value& value=stack.pop().value();
                    389: #else
                    390:                                Value& value=stack.pop().value();
                    391:                                const String& name=stack.pop().string();  debug_name=&name;
                    392:                                Value& ncontext=stack.pop().value();
                    393: #endif
                    394:                                put_element(ncontext, name, &value);
1.337     misha     395: 
                    396:                                break;
                    397:                        }
                    398: 
1.322     misha     399:                case OP::OP_CONSTRUCT_EXPR:
1.80      paf       400:                        {
1.219     paf       401:                                // see OP_PREPARE_TO_EXPRESSION
                    402:                                wcontext->set_in_expression(false);
                    403: 
1.344     misha     404: #ifdef OPTIMIZE_BYTECODE_CONSTRUCT
                    405:                                const String& name=stack.pop().string();  debug_name=&name;
                    406:                                Value& ncontext=stack.pop().value();
                    407:                                Value& expr=stack.pop().value();
                    408: #else
1.308     paf       409:                                Value& expr=stack.pop().value();
1.297     paf       410:                                const String& name=stack.pop().string();  debug_name=&name;
                    411:                                Value& ncontext=stack.pop().value();
1.344     misha     412: #endif
1.308     paf       413:                                Value& value=expr.as_expr_result();
1.329     misha     414:                                put_element(ncontext, name, &value);
1.80      paf       415:                                break;
                    416:                        }
1.337     misha     417: 
1.322     misha     418:                case OP::OP_CURLY_CODE__CONSTRUCT:
1.182     parser    419:                        {
1.297     paf       420:                                ArrayOperation& local_ops=*i.next().ops;
1.336     misha     421: 
                    422:                                DEBUG_PRINT_OPS((&local_ops))
                    423: 
1.326     misha     424:                                VJunction& value=*new VJunction(
1.297     paf       425:                                        get_self(), 0,
1.257     paf       426:                                        method_frame, 
1.240     paf       427:                                        rcontext, 
                    428:                                        wcontext, 
1.312     paf       429:                                        &local_ops);
1.238     paf       430: 
1.326     misha     431:                                wcontext->attach_junction(&value);
                    432: 
1.297     paf       433:                                const String& name=stack.pop().string();  debug_name=&name;
                    434:                                Value& ncontext=stack.pop().value();
1.371     moko      435:                                if(const VJunction* vjunction=ncontext.put_element(name, &value))
1.312     paf       436:                                        if(vjunction!=PUT_ELEMENT_REPLACED_ELEMENT)
1.316     misha     437:                                                throw Exception(PARSER_RUNTIME,
1.308     paf       438:                                                        0,
                    439:                                                        "property value can not be code, use [] or () brackets");
                    440:                                        
1.182     parser    441:                                break;
                    442:                        }
1.322     misha     443:                case OP::OP_NESTED_CODE:
1.209     paf       444:                        {
1.297     paf       445:                                ArrayOperation& local_ops=*i.next().ops;
1.336     misha     446: 
                    447:                                DEBUG_PRINT_OPS((&local_ops))
                    448: 
1.297     paf       449:                                stack.push(local_ops);
1.209     paf       450:                                break;
                    451:                        }
1.322     misha     452:                case OP::OP_WRITE_VALUE:
1.13      paf       453:                        {
1.297     paf       454:                                Value& value=stack.pop().value();
                    455:                                write_assign_lang(value);
1.86      paf       456:                                break;
                    457:                        }
1.322     misha     458:                case OP::OP_WRITE_EXPR_RESULT:
1.108     paf       459:                        {
1.219     paf       460:                                // see OP_PREPARE_TO_EXPRESSION
                    461:                                wcontext->set_in_expression(false);
1.351     misha     462: 
                    463:                                Value& value=stack.pop().value();
                    464:                                wcontext->write(value.as_expr_result());
1.108     paf       465:                                break;
                    466:                        }
1.322     misha     467:                case OP::OP_STRING__WRITE:
1.86      paf       468:                        {
1.297     paf       469:                                i.next(); // ignore origin
                    470:                                Value* value=i.next().value;
1.336     misha     471: 
                    472:                                const String& string_value=*value->get_string();
                    473: 
                    474:                                DEBUG_PRINT_STRING(string_value)
                    475: 
                    476:                                write_no_lang(string_value);
1.13      paf       477:                                break;
1.14      paf       478:                        }
1.328     misha     479: 
                    480: #ifdef OPTIMIZE_BYTECODE_GET_ELEMENT
                    481:                case OP::OP_VALUE__GET_ELEMENT_OR_OPERATOR:
                    482:                        {
                    483:                                debug_origin=i.next().origin;
                    484:                                const String& name=*i.next().value->get_string();  debug_name=&name;
                    485: 
1.336     misha     486:                                DEBUG_PRINT_STRING(name)
                    487: 
1.330     misha     488:                                if(Method* method=main_class.get_method(name)){ // looking operator of that name FIRST
                    489:                                        if(!method->junction_template) method->junction_template=new VJunction(main_class, method);
                    490:                                        stack.push(*method->junction_template);
                    491:                                        break;
                    492:                                }
                    493:                                Value& value=get_element(*rcontext, name);
1.328     misha     494:                                stack.push(value);
                    495:                                break;
                    496:                        }
                    497: #else
1.322     misha     498:                case OP::OP_GET_ELEMENT_OR_OPERATOR:
1.215     paf       499:                        {
1.297     paf       500:                                const String& name=stack.pop().string();  debug_name=&name;
                    501:                                Value& ncontext=stack.pop().value();
1.330     misha     502:                                if(Method* method=main_class.get_method(name)){ // looking operator of that name FIRST
                    503:                                        if(!method->junction_template) method->junction_template=new VJunction(main_class, method);
                    504:                                        stack.push(*method->junction_template);
                    505:                                        break;
                    506:                                }
                    507:                                Value& value=get_element(ncontext, name);
1.297     paf       508:                                stack.push(value);
1.215     paf       509:                                break;
                    510:                        }
1.328     misha     511: #endif
                    512: 
1.335     misha     513: #ifdef OPTIMIZE_BYTECODE_GET_OBJECT_ELEMENT
                    514:                case OP::OP_GET_OBJECT_ELEMENT:
                    515:                case OP::OP_GET_OBJECT_ELEMENT__WRITE:
1.334     misha     516:                        {
                    517:                                debug_origin=i.next().origin;
                    518:                                const String& context_name=*i.next().value->get_string();  debug_name=&context_name;
1.336     misha     519: 
                    520:                                DEBUG_PRINT_STRING(context_name)
                    521: 
1.334     misha     522:                                Value& object=get_element(*rcontext, context_name);
                    523: 
                    524:                                debug_origin=i.next().origin;
                    525:                                const String& field_name=*i.next().value->get_string();  debug_name=&field_name;
1.336     misha     526: 
                    527:                                DEBUG_PRINT_STRING(field_name)
                    528: 
1.334     misha     529:                                Value& value=get_element(object, field_name);
                    530: 
1.335     misha     531:                                if(opcode==OP::OP_GET_OBJECT_ELEMENT){
                    532:                                        stack.push(value);
                    533:                                } else {
                    534:                                        write_assign_lang(value);
                    535:                                }
                    536:                                break;
                    537:                        }
                    538: #endif
                    539: 
                    540: #ifdef OPTIMIZE_BYTECODE_GET_OBJECT_VAR_ELEMENT
                    541:                case OP::OP_GET_OBJECT_VAR_ELEMENT:
                    542:                case OP::OP_GET_OBJECT_VAR_ELEMENT__WRITE:
                    543:                        {
                    544:                                debug_origin=i.next().origin;
                    545:                                const String& context_name=*i.next().value->get_string();  debug_name=&context_name;
1.336     misha     546: 
                    547:                                DEBUG_PRINT_STRING(context_name)
                    548: 
1.335     misha     549:                                Value& object=get_element(*rcontext, context_name);
                    550: 
                    551:                                debug_origin=i.next().origin;
                    552:                                const String& var_name=*i.next().value->get_string();  debug_name=&var_name;
1.336     misha     553: 
                    554:                                DEBUG_PRINT_STRING(var_name)
                    555: 
1.359     misha     556:                                const String* field=&get_element(*rcontext, var_name).as_string();
1.335     misha     557: 
                    558:                                Value& value=get_element(object, *field);
                    559: 
                    560:                                if(opcode==OP::OP_GET_OBJECT_VAR_ELEMENT){
1.334     misha     561:                                        stack.push(value);
                    562:                                } else {
                    563:                                        write_assign_lang(value);
                    564:                                }
                    565:                                break;
                    566:                        }
                    567: #endif
                    568: 
1.322     misha     569:                case OP::OP_GET_ELEMENT:
1.11      paf       570:                        {
1.297     paf       571:                                const String& name=stack.pop().string();  debug_name=&name;
                    572:                                Value& ncontext=stack.pop().value();
1.330     misha     573:                                Value& value=get_element(ncontext, name);
1.297     paf       574:                                stack.push(value);
1.17      paf       575:                                break;
                    576:                        }
                    577: 
1.346     misha     578:                case OP::OP_GET_ELEMENT__WRITE:
                    579:                        {
                    580:                                const String& name=stack.pop().string();  debug_name=&name;
                    581:                                Value& ncontext=stack.pop().value();
                    582:                                Value& value=get_element(ncontext, name);
                    583:                                write_assign_lang(value);
                    584:                                break;
                    585:                        }
                    586: 
1.328     misha     587: #ifdef OPTIMIZE_BYTECODE_GET_ELEMENT
                    588:                case OP::OP_VALUE__GET_ELEMENT:
                    589:                        {
                    590:                                debug_origin=i.next().origin;
                    591:                                const String& name=*i.next().value->get_string(); debug_name=&name;
                    592: 
1.336     misha     593:                                DEBUG_PRINT_STRING(name)
1.328     misha     594: 
1.330     misha     595:                                Value& value=get_element(*rcontext, name);
1.328     misha     596:                                stack.push(value);
                    597:                                break;
                    598:                        }
                    599: 
                    600:                case OP::OP_VALUE__GET_ELEMENT__WRITE:
                    601:                        {
                    602:                                debug_origin=i.next().origin;
                    603:                                const String& name=*i.next().value->get_string(); debug_name=&name;
                    604: 
1.336     misha     605:                                DEBUG_PRINT_STRING(name)
1.328     misha     606: 
1.330     misha     607:                                Value& value=get_element(*rcontext, name);
1.328     misha     608:                                write_assign_lang(value);
                    609:                                break;
                    610:                        }
1.347     misha     611: 
                    612:                case OP::OP_WITH_ROOT__VALUE__GET_ELEMENT:
                    613:                        {
                    614:                                debug_origin=i.next().origin;
                    615:                                const String& name=*i.next().value->get_string(); debug_name=&name;
                    616: 
                    617:                                DEBUG_PRINT_STRING(name)
                    618: 
                    619:                                Value& value=get_element(*method_frame, name);
                    620:                                stack.push(value);
                    621:                                break;
                    622:                        }
1.328     misha     623: #endif
1.32      paf       624: 
1.345     misha     625: #ifdef OPTIMIZE_BYTECODE_GET_SELF_ELEMENT
                    626:                case OP::OP_WITH_SELF__VALUE__GET_ELEMENT:
                    627:                case OP::OP_WITH_SELF__VALUE__GET_ELEMENT__WRITE:
                    628:                        {
                    629:                                debug_origin=i.next().origin;
                    630:                                const String& name=*i.next().value->get_string(); debug_name=&name;
                    631: 
                    632:                                DEBUG_PRINT_STRING(name)
                    633: 
                    634:                                Value& value=get_element(get_self(), name);
                    635: 
                    636:                                if(opcode==OP::OP_WITH_SELF__VALUE__GET_ELEMENT){
                    637:                                        stack.push(value);
                    638:                                } else {
                    639:                                        write_assign_lang(value);
                    640:                                }
                    641:                                break;
                    642:                        }
                    643: #endif
                    644: 
1.322     misha     645:                case OP::OP_OBJECT_POOL:
1.17      paf       646:                        {
1.297     paf       647:                                ArrayOperation& local_ops=*i.next().ops;
1.226     paf       648:                                
1.257     paf       649:                                WContext *saved_wcontext=wcontext;
1.297     paf       650:                                String::Language saved_lang=flang;
                    651:                                flang=String::L_PASS_APPENDED;
1.348     misha     652: #ifdef OPTIMIZE_SINGLE_STRING_WRITE
1.351     misha     653:                                WObjectPoolWrapper local(wcontext);
1.348     misha     654: #else
1.351     misha     655:                                WWrapper local(wcontext);
1.348     misha     656: #endif
1.226     paf       657:                                wcontext=&local;
                    658: 
1.297     paf       659:                                execute(local_ops);
1.226     paf       660: 
1.297     paf       661:                                stack.push(wcontext->result().as_value());
1.257     paf       662:                                flang=saved_lang;
                    663:                                wcontext=saved_wcontext;
1.13      paf       664:                                break;
1.15      paf       665:                        }
1.13      paf       666:                        
1.322     misha     667:                case OP::OP_STRING_POOL:
1.49      paf       668:                        {
1.297     paf       669:                                ArrayOperation& local_ops=*i.next().ops;
1.226     paf       670: 
1.257     paf       671:                                WContext *saved_wcontext=wcontext;
1.351     misha     672:                                WWrapper local(wcontext);
1.226     paf       673:                                wcontext=&local;
                    674: 
1.297     paf       675:                                execute(local_ops);
1.226     paf       676: 
1.49      paf       677:                                // from "$a $b" part of expression taking only string value,
                    678:                                // ignoring any other content of wcontext
1.367     moko      679:                                const String* string=wcontext->get_string();
                    680:                                Value* value=string ? new VString(*string) : new VString();
1.297     paf       681:                                stack.push(*value);
                    682: 
1.257     paf       683:                                wcontext=saved_wcontext;
1.49      paf       684:                                break;
                    685:                        }
                    686: 
1.51      paf       687:                // CALL
1.322     misha     688:                case OP::OP_CURLY_CODE__STORE_PARAM:
                    689:                case OP::OP_EXPR_CODE__STORE_PARAM:
1.28      paf       690:                        {
1.237     paf       691:                                // code
1.297     paf       692:                                ArrayOperation& local_ops=*i.next().ops;
1.336     misha     693: 
                    694:                                DEBUG_PRINT_OPS((&local_ops))
                    695: 
1.237     paf       696:                                // when they evaluate expression parameter,
                    697:                                // the object expression result
                    698:                                // does not need to be written into calling frame
                    699:                                // it must go into any expressions using that parameter
                    700:                                // hence, we zero junction.wcontext here, and later
                    701:                                // in .process we would test that field 
                    702:                                // in decision "which wwrapper to use"
1.326     misha     703:                                VJunction& value=*new VJunction(
1.297     paf       704:                                        get_self(), 0,
1.257     paf       705:                                        method_frame, 
1.237     paf       706:                                        rcontext, 
1.322     misha     707:                                        opcode==OP::OP_EXPR_CODE__STORE_PARAM?0:wcontext, 
1.312     paf       708:                                        &local_ops);
1.343     misha     709: #ifndef USE_DESTRUCTORS
1.326     misha     710:                                if (opcode!=OP::OP_EXPR_CODE__STORE_PARAM)
1.343     misha     711:                                        wcontext->attach_junction(&value);
1.326     misha     712: #endif
1.237     paf       713:                                // store param
1.329     misha     714:                                stack.push(value);
1.29      paf       715:                                break;
                    716:                        }
                    717: 
1.322     misha     718:                case OP::OP_PREPARE_TO_EXPRESSION:
1.219     paf       719:                        {
                    720:                                wcontext->set_in_expression(true);
                    721:                                break;
                    722:                        }
                    723: 
1.350     misha     724: #define METHOD_FRAME_ACTION(action)                                                                                                    \
                    725:                if(local_ops){                                                                                                                          \
                    726:                        size_t first = stack.top_index();                                                                               \
                    727:                        execute(*local_ops);                                                                                                    \
                    728:                        frame.store_params((Value**)stack.ptr(first), stack.top_index()-first); \
                    729:                        action;                                                                                                                                 \
                    730:                        stack.set_top_index(first);                                                                                             \
                    731:                } else {                                                                                                                                        \
                    732:                        frame.empty_params();                                                                                                   \
                    733:                        action;                                                                                                                                 \
                    734:                }
                    735: 
1.322     misha     736:                case OP::OP_CALL:
1.29      paf       737:                        {
1.297     paf       738:                                ArrayOperation* local_ops=i.next().ops;
1.237     paf       739: 
1.336     misha     740:                                DEBUG_PRINT_OPS(local_ops)
                    741:                                DEBUG_PRINT_STR("->\n")
                    742: 
1.297     paf       743:                                Value& value=stack.pop().value();
1.237     paf       744: 
1.297     paf       745:                                Junction* junction=value.get_junction();
                    746:                                if(!junction) {
                    747:                                        if(value.is("void"))
1.316     misha     748:                                                throw Exception(PARSER_RUNTIME,
1.297     paf       749:                                                        0,
                    750:                                                        "undefined method");
                    751:                                        else
1.316     misha     752:                                                throw Exception(PARSER_RUNTIME,
1.297     paf       753:                                                        0,
                    754:                                                        "is '%s', not a method or junction, can not call it",
                    755:                                                                value.type());
                    756:                                }
1.237     paf       757: 
1.370     moko      758:                                Value *result;
                    759:                                {
                    760:                                        VMethodFrame frame(*junction->method, method_frame, junction->self);
                    761:                                        METHOD_FRAME_ACTION(op_call(frame));
                    762:                                        result=&frame.result().as_value();
                    763:                                        // VMethodFrame desctructor deletes junctions in stack params here
                    764:                                }
                    765:                                stack.push(*result);
1.332     misha     766: 
1.336     misha     767:                                DEBUG_PRINT_STR("<-returned")
                    768: 
1.332     misha     769:                                if(get_skip())
                    770:                                        return;
                    771:                                if(get_interrupted()) {
                    772:                                        set_interrupted(false);
                    773:                                        throw Exception("parser.interrupted",
                    774:                                                0,
                    775:                                                "execution stopped");
1.237     paf       776:                                }
1.332     misha     777:                                break;
                    778:                        }
1.220     paf       779: 
1.332     misha     780:                case OP::OP_CALL__WRITE:
                    781:                        {
                    782:                                ArrayOperation* local_ops=i.next().ops;
                    783: 
1.336     misha     784:                                DEBUG_PRINT_OPS(local_ops)
                    785:                                DEBUG_PRINT_STR("->\n")
                    786: 
1.332     misha     787:                                Value& value=stack.pop().value();
                    788: 
                    789:                                Junction* junction=value.get_junction();
                    790:                                if(!junction) {
                    791:                                        if(value.is("void"))
                    792:                                                throw Exception(PARSER_RUNTIME,
                    793:                                                        0,
                    794:                                                        "undefined method");
                    795:                                        else
                    796:                                                throw Exception(PARSER_RUNTIME,
                    797:                                                        0,
                    798:                                                        "is '%s', not a method or junction, can not call it",
                    799:                                                        value.type());
                    800:                                }
                    801: 
                    802: #ifdef OPTIMIZE_CALL
                    803:                                const Method& method=*junction->method;
1.353     misha     804:                                if(method.call_optimization==Method::CO_WITHOUT_FRAME){
1.332     misha     805:                                        if(local_ops){ // store param code goes here
                    806:                                                size_t first = stack.top_index();
                    807:                                                execute(*local_ops);
                    808: 
                    809:                                                MethodParams method_params;
                    810:                                                method_params.store_params((Value**)stack.ptr(first), stack.top_index()-first);
                    811:                                                method.check_actual_numbered_params(junction->self, &method_params);
                    812:                                                method.native_code(*this, method_params); // execute it
                    813: 
                    814:                                                stack.set_top_index(first);
                    815:                                        } else {
                    816:                                                MethodParams method_params;
                    817:                                                method.check_actual_numbered_params(junction->self, &method_params);
                    818:                                                method.native_code(*this, method_params); // execute it
                    819:                                        }
1.353     misha     820:                                } else if(method.call_optimization==Method::CO_WITHOUT_WCONTEXT){
1.363     moko      821:                                        VMethodFrame frame(method, method_frame, junction->self);
1.350     misha     822:                                        METHOD_FRAME_ACTION(op_call_write(frame));
1.332     misha     823:                                } else 
                    824: #endif // OPTIMIZE_CALL
                    825:                                {
1.363     moko      826:                                        VMethodFrame frame(method, method_frame, junction->self);
                    827:                                        METHOD_FRAME_ACTION(op_call(frame));
1.346     misha     828:                                        write_pass_lang(frame.result());
1.332     misha     829:                                }
1.336     misha     830: 
                    831:                                DEBUG_PRINT_STR("<-returned")
1.327     misha     832: 
                    833:                                if(get_skip())
                    834:                                        return;
                    835:                                if(get_interrupted()) {
                    836:                                        set_interrupted(false);
                    837:                                        throw Exception("parser.interrupted",
                    838:                                                0,
                    839:                                                "execution stopped");
                    840:                                }
1.49      paf       841:                                break;
                    842:                        }
                    843: 
1.348     misha     844:                case OP::OP_CONSTRUCT_OBJECT:
                    845:                case OP::OP_CONSTRUCT_OBJECT__WRITE:
                    846:                        {
                    847:                                debug_origin=i.next().origin;
                    848:                                Value& vclass_name=*i.next().value;
1.360     pretende  849:                                const String& class_name=*vclass_name.get_string(); debug_name=&class_name;
1.348     misha     850: 
                    851:                                DEBUG_PRINT_STRING(class_name)
                    852: 
1.358     misha     853:                                Value* class_value=get_class(class_name);
1.348     misha     854:                                if(!class_value)
                    855:                                        throw Exception(PARSER_RUNTIME,
                    856:                                                &class_name,
                    857:                                                "class is undefined"); 
                    858: 
                    859:                                debug_origin=i.next().origin;
                    860:                                Value& vconstructor_name=*i.next().value;
                    861:                                const String& constructor_name=*vconstructor_name.get_string(); debug_name=&constructor_name;
                    862: 
                    863:                                DEBUG_PRINT_STRING(constructor_name)
                    864: 
1.363     moko      865:                                Junction* constructor_junction=get_element(*class_value, constructor_name).get_junction();
                    866:                                if(!constructor_junction)
1.350     misha     867:                                        throw Exception(PARSER_RUNTIME,
                    868:                                                &constructor_name,
                    869:                                                "constructor must be declared in class '%s'", 
                    870:                                                class_value->get_class()->name_cstr());
                    871: 
1.348     misha     872:                                ArrayOperation* local_ops=i.next().ops;
                    873:                                DEBUG_PRINT_OPS(local_ops)
                    874:                                DEBUG_PRINT_STR("->\n")
                    875: 
1.370     moko      876:                                Value *result;
                    877:                                {
                    878:                                        Value& object=construct(*class_value, *constructor_junction->method);
                    879:                                        VConstructorFrame frame(*constructor_junction->method, method_frame, object);
                    880:                                        METHOD_FRAME_ACTION(op_call(frame));
                    881:                                        object.enable_default_setter();
                    882:                                        result=&frame.result().as_value();
                    883:                                        // VMethodFrame desctructor deletes junctions in stack params here
                    884:                                }
1.355     misha     885: 
1.350     misha     886:                                if(opcode==OP::OP_CONSTRUCT_OBJECT)
1.370     moko      887:                                        stack.push(*result);
1.350     misha     888:                                else
1.370     moko      889:                                        write_pass_lang(*result);
1.348     misha     890: 
                    891:                                DEBUG_PRINT_STR("<-returned")
                    892:                                break;
                    893:                        }
                    894: 
1.55      paf       895:                // expression ops: unary
1.322     misha     896:                case OP::OP_NEG:
1.55      paf       897:                        {
1.297     paf       898:                                Value& a=stack.pop().value();
                    899:                                Value& value=*new VDouble(-a.as_double());
                    900:                                stack.push(value);
1.55      paf       901:                                break;
                    902:                        }
1.322     misha     903:                case OP::OP_INV:
1.55      paf       904:                        {
1.297     paf       905:                                Value& a=stack.pop().value();
                    906:                                Value& value=*new VDouble(~a.as_int());
                    907:                                stack.push(value);
1.55      paf       908:                                break;
                    909:                        }
1.322     misha     910:                case OP::OP_NOT:
1.55      paf       911:                        {
1.297     paf       912:                                Value& a=stack.pop().value();
1.327     misha     913:                                Value& value=VBool::get(!a.as_bool());
1.297     paf       914:                                stack.push(value);
1.55      paf       915:                                break;
                    916:                        }
1.322     misha     917:                case OP::OP_DEF:
1.62      paf       918:                        {
1.297     paf       919:                                Value& a=stack.pop().value();
1.327     misha     920:                                Value& value=VBool::get(a.is_defined());
1.297     paf       921:                                stack.push(value);
1.62      paf       922:                                break;
                    923:                        }
1.322     misha     924:                case OP::OP_IN:
1.62      paf       925:                        {
1.297     paf       926:                                Value& a=stack.pop().value();
                    927:                                const String& path=a.as_string();
1.327     misha     928:                                Value& value=VBool::get(request_info.uri && *request_info.uri && path.this_starts(request_info.uri));
1.297     paf       929:                                stack.push(value);
1.62      paf       930:                                break;
                    931:                        }
1.322     misha     932:                case OP::OP_FEXISTS:
1.62      paf       933:                        {
1.297     paf       934:                                Value& a=stack.pop().value();
1.327     misha     935:                                Value& value=VBool::get(file_exist(absolute(a.as_string())));
1.297     paf       936:                                stack.push(value);
1.148     paf       937:                                break;
                    938:                        }
1.322     misha     939:                case OP::OP_DEXISTS:
1.148     paf       940:                        {
1.297     paf       941:                                Value& a=stack.pop().value();
1.327     misha     942:                                Value& value=VBool::get(dir_exists(absolute(a.as_string())));
1.297     paf       943:                                stack.push(value);
1.62      paf       944:                                break;
                    945:                        }
1.55      paf       946: 
                    947:                // expression ops: binary
1.322     misha     948:                case OP::OP_SUB: 
1.53      paf       949:                        {
1.297     paf       950:                                Value& b=stack.pop().value();  Value& a=stack.pop().value();
                    951:                                Value& value=*new VDouble(a.as_double() - b.as_double());
                    952:                                stack.push(value);
1.53      paf       953:                                break;
                    954:                        }
1.322     misha     955:                case OP::OP_ADD: 
1.53      paf       956:                        {
1.297     paf       957:                                Value& b=stack.pop().value();  Value& a=stack.pop().value();
                    958:                                Value& value=*new VDouble(a.as_double() + b.as_double());
                    959:                                stack.push(value);
1.53      paf       960:                                break;
                    961:                        }
1.322     misha     962:                case OP::OP_MUL: 
1.49      paf       963:                        {
1.297     paf       964:                                Value& b=stack.pop().value();  Value& a=stack.pop().value();
                    965:                                Value& value=*new VDouble(a.as_double() * b.as_double());
                    966:                                stack.push(value);
1.53      paf       967:                                break;
                    968:                        }
1.322     misha     969:                case OP::OP_DIV: 
1.53      paf       970:                        {
1.297     paf       971:                                Value& b=stack.pop().value();  Value& a=stack.pop().value();
1.170     parser    972: 
1.297     paf       973:                                double a_double=a.as_double();
                    974:                                double b_double=b.as_double();
1.170     parser    975: 
1.171     parser    976:                                if(b_double == 0) {
1.297     paf       977:                                        //const String* problem_source=b.as_string();
1.223     paf       978:                                        throw Exception("number.zerodivision",
1.297     paf       979:                                                0, //problem_source,
1.170     parser    980:                                                "Division by zero");
1.171     parser    981:                                }
1.170     parser    982: 
1.297     paf       983:                                Value& value=*new VDouble(a_double / b_double);
                    984:                                stack.push(value);
1.54      paf       985:                                break;
                    986:                        }
1.322     misha     987:                case OP::OP_MOD: 
1.55      paf       988:                        {
1.297     paf       989:                                Value& b=stack.pop().value();  Value& a=stack.pop().value();
1.170     parser    990: 
1.297     paf       991:                                double a_double=a.as_double();
                    992:                                double b_double=b.as_double();
1.170     parser    993: 
1.173     parser    994:                                if(b_double == 0) {
1.297     paf       995:                                        //const String* problem_source=b.as_string();
1.223     paf       996:                                        throw Exception("number.zerodivision",
1.297     paf       997:                                                0, //problem_source,
1.170     parser    998:                                                "Modulus by zero");
1.171     parser    999:                                }
1.170     parser   1000: 
1.297     paf      1001:                                Value& value=*new VDouble(fmod(a_double, b_double));
                   1002:                                stack.push(value);
1.201     paf      1003:                                break;
                   1004:                        }
1.322     misha    1005:                case OP::OP_INTDIV:
1.201     paf      1006:                        {
1.297     paf      1007:                                Value& b=stack.pop().value();  Value& a=stack.pop().value();
1.201     paf      1008: 
1.297     paf      1009:                                int a_int=a.as_int();
                   1010:                                int b_int=b.as_int();
1.201     paf      1011: 
                   1012:                                if(b_int == 0) {
1.297     paf      1013:                                        //const String* problem_source=b.as_string();
1.223     paf      1014:                                        throw Exception("number.zerodivision",
1.297     paf      1015:                                                0, //problem_source,
1.201     paf      1016:                                                "Division by zero");
                   1017:                                }
                   1018: 
1.297     paf      1019:                                Value& value=*new VInt(a_int / b_int);
                   1020:                                stack.push(value);
1.55      paf      1021:                                break;
                   1022:                        }
1.322     misha    1023:                case OP::OP_BIN_SL:
1.274     paf      1024:                        {
1.297     paf      1025:                                Value& b=stack.pop().value();  Value& a=stack.pop().value();
                   1026:                                Value& value=*new VInt(
                   1027:                                        a.as_int() <<
                   1028:                                        b.as_int());
                   1029:                                stack.push(value);
1.274     paf      1030:                                break;
                   1031:                        }
1.322     misha    1032:                case OP::OP_BIN_SR:
1.274     paf      1033:                        {
1.297     paf      1034:                                Value& b=stack.pop().value();  Value& a=stack.pop().value();
                   1035:                                Value& value=*new VInt(
                   1036:                                        a.as_int() >>
                   1037:                                        b.as_int());
                   1038:                                stack.push(value);
1.274     paf      1039:                                break;
                   1040:                        }
1.322     misha    1041:                case OP::OP_BIN_AND:
1.54      paf      1042:                        {
1.297     paf      1043:                                Value& b=stack.pop().value();  Value& a=stack.pop().value();
                   1044:                                Value& value=*new VInt(
                   1045:                                        a.as_int() &
                   1046:                                        b.as_int());
                   1047:                                stack.push(value);
1.54      paf      1048:                                break;
                   1049:                        }
1.322     misha    1050:                case OP::OP_BIN_OR:
1.54      paf      1051:                        {
1.297     paf      1052:                                Value& b=stack.pop().value();  Value& a=stack.pop().value();
                   1053:                                Value& value=*new VInt(
                   1054:                                        a.as_int() |
                   1055:                                        b.as_int());
                   1056:                                stack.push(value);
1.55      paf      1057:                                break;
                   1058:                        }
1.322     misha    1059:                case OP::OP_BIN_XOR:
1.56      paf      1060:                        {
1.297     paf      1061:                                Value& b=stack.pop().value();  Value& a=stack.pop().value();
                   1062:                                Value& value=*new VInt(
                   1063:                                        a.as_int() ^
                   1064:                                        b.as_int());
                   1065:                                stack.push(value);
1.56      paf      1066:                                break;
                   1067:                        }
1.322     misha    1068:                case OP::OP_LOG_AND:
1.55      paf      1069:                        {
1.297     paf      1070:                                ArrayOperation& local_ops=stack.pop().ops();  Value& a=stack.pop().value();
1.263     paf      1071:                                bool result;
1.297     paf      1072:                                if(a.as_bool()) {
                   1073:                                        execute(local_ops);
                   1074:                                        Value& b=stack.pop().value();
                   1075:                                        result=b.as_bool();
1.209     paf      1076:                                } else
1.263     paf      1077:                                        result=false;
1.327     misha    1078:                                Value& value=VBool::get(result);
1.297     paf      1079:                                stack.push(value);
1.55      paf      1080:                                break;
                   1081:                        }
1.322     misha    1082:                case OP::OP_LOG_OR:
1.55      paf      1083:                        {
1.297     paf      1084:                                ArrayOperation& local_ops=stack.pop().ops();  Value& a=stack.pop().value();
1.263     paf      1085:                                bool result;
1.297     paf      1086:                                if(a.as_bool()) 
1.263     paf      1087:                                        result=true;
1.209     paf      1088:                                else {
1.297     paf      1089:                                        execute(local_ops);
                   1090:                                        Value& b=stack.pop().value();
                   1091:                                        result=b.as_bool();
1.209     paf      1092:                                }
1.327     misha    1093:                                Value& value=VBool::get(result);
1.297     paf      1094:                                stack.push(value);
1.56      paf      1095:                                break;
                   1096:                        }
1.322     misha    1097:                case OP::OP_LOG_XOR:
1.56      paf      1098:                        {
1.297     paf      1099:                                Value& b=stack.pop().value();  Value& a=stack.pop().value();
1.327     misha    1100:                                Value& value=VBool::get(a.as_bool() ^ b.as_bool());
1.297     paf      1101:                                stack.push(value);
1.55      paf      1102:                                break;
                   1103:                        }
1.322     misha    1104:                case OP::OP_NUM_LT: 
1.55      paf      1105:                        {
1.297     paf      1106:                                volatile double b_double=stack.pop().value().as_double();
                   1107:                                volatile double a_double=stack.pop().value().as_double();
1.327     misha    1108:                                Value& value=VBool::get(a_double<b_double);
1.297     paf      1109:                                stack.push(value);
1.55      paf      1110:                                break;
                   1111:                        }
1.322     misha    1112:                case OP::OP_NUM_GT: 
1.55      paf      1113:                        {
1.297     paf      1114:                                volatile double b_double=stack.pop().value().as_double();
                   1115:                                volatile double a_double=stack.pop().value().as_double();
1.327     misha    1116:                                Value& value=VBool::get(a_double>b_double);
1.297     paf      1117:                                stack.push(value);
1.55      paf      1118:                                break;
                   1119:                        }
1.322     misha    1120:                case OP::OP_NUM_LE: 
1.55      paf      1121:                        {
1.297     paf      1122:                                volatile double b_double=stack.pop().value().as_double();
                   1123:                                volatile double a_double=stack.pop().value().as_double();
1.327     misha    1124:                                Value& value=VBool::get(a_double<=b_double);
1.297     paf      1125:                                stack.push(value);
1.55      paf      1126:                                break;
                   1127:                        }
1.322     misha    1128:                case OP::OP_NUM_GE: 
1.55      paf      1129:                        {
1.297     paf      1130:                                volatile double b_double=stack.pop().value().as_double();
                   1131:                                volatile double a_double=stack.pop().value().as_double();
1.327     misha    1132:                                Value& value=VBool::get(a_double>=b_double);
1.297     paf      1133:                                stack.push(value);
1.55      paf      1134:                                break;
                   1135:                        }
1.322     misha    1136:                case OP::OP_NUM_EQ: 
1.55      paf      1137:                        {
1.297     paf      1138:                                volatile double b_double=stack.pop().value().as_double();
                   1139:                                volatile double a_double=stack.pop().value().as_double();
1.327     misha    1140:                                Value& value=VBool::get(a_double==b_double);
1.297     paf      1141:                                stack.push(value);
1.55      paf      1142:                                break;
                   1143:                        }
1.322     misha    1144:                case OP::OP_NUM_NE: 
1.55      paf      1145:                        {
1.297     paf      1146:                                volatile double b_double=stack.pop().value().as_double();
                   1147:                                volatile double a_double=stack.pop().value().as_double();
1.327     misha    1148:                                Value& value=VBool::get(a_double!=b_double);
1.297     paf      1149:                                stack.push(value);
1.54      paf      1150:                                break;
                   1151:                        }
1.322     misha    1152:                case OP::OP_STR_LT: 
1.58      paf      1153:                        {
1.297     paf      1154:                                Value& b=stack.pop().value();  Value& a=stack.pop().value();
1.327     misha    1155:                                Value& value=VBool::get(a.as_string() < b.as_string());
1.297     paf      1156:                                stack.push(value);
1.58      paf      1157:                                break;
                   1158:                        }
1.322     misha    1159:                case OP::OP_STR_GT: 
1.58      paf      1160:                        {
1.297     paf      1161:                                Value& b=stack.pop().value();  Value& a=stack.pop().value();
1.327     misha    1162:                                Value& value=VBool::get(a.as_string() > b.as_string());
1.297     paf      1163:                                stack.push(value);
1.58      paf      1164:                                break;
                   1165:                        }
1.322     misha    1166:                case OP::OP_STR_LE: 
1.58      paf      1167:                        {
1.297     paf      1168:                                Value& b=stack.pop().value();  Value& a=stack.pop().value();
1.327     misha    1169:                                Value& value=VBool::get(a.as_string() <= b.as_string());
1.297     paf      1170:                                stack.push(value);
1.58      paf      1171:                                break;
                   1172:                        }
1.322     misha    1173:                case OP::OP_STR_GE: 
1.54      paf      1174:                        {
1.297     paf      1175:                                Value& b=stack.pop().value();  Value& a=stack.pop().value();
1.327     misha    1176:                                Value& value=VBool::get(a.as_string() >= b.as_string());
1.297     paf      1177:                                stack.push(value);
1.58      paf      1178:                                break;
                   1179:                        }
1.322     misha    1180:                case OP::OP_STR_EQ: 
1.58      paf      1181:                        {
1.297     paf      1182:                                Value& b=stack.pop().value();  Value& a=stack.pop().value();
1.327     misha    1183:                                Value& value=VBool::get(a.as_string() == b.as_string());
1.297     paf      1184:                                stack.push(value);
1.58      paf      1185:                                break;
                   1186:                        }
1.322     misha    1187:                case OP::OP_STR_NE: 
1.58      paf      1188:                        {
1.297     paf      1189:                                Value& b=stack.pop().value();  Value& a=stack.pop().value();
1.327     misha    1190:                                Value& value=VBool::get(a.as_string() != b.as_string());
1.297     paf      1191:                                stack.push(value);
1.103     paf      1192:                                break;
                   1193:                        }
1.322     misha    1194:                case OP::OP_IS:
1.103     paf      1195:                        {
1.297     paf      1196:                                Value& b=stack.pop().value();  Value& a=stack.pop().value();
1.327     misha    1197:                                Value& value=VBool::get(a.is(b.as_string().cstr()));
1.297     paf      1198:                                stack.push(value);
1.28      paf      1199:                                break;
                   1200:                        }
                   1201: 
1.11      paf      1202:                default:
1.223     paf      1203:                        throw Exception(0,
1.67      paf      1204:                                0,
1.297     paf      1205:                                "invalid opcode %d", opcode); 
1.11      paf      1206:                }
                   1207:        }
1.306     paf      1208:        } catch(const Exception&) {
1.297     paf      1209:                // record it to stack trace
1.301     paf      1210:                if(debug_name)
                   1211:                        exception_trace.push(Trace(debug_name, debug_origin));
1.297     paf      1212:                rethrow;
                   1213:        }
1.1       paf      1214: }
1.17      paf      1215: 
1.330     misha    1216: #define SAVE_CONTEXT                                                           \
                   1217:        VMethodFrame *saved_method_frame=method_frame;  \
                   1218:        Value* saved_rcontext=rcontext;                                 \
                   1219:        WContext *saved_wcontext=wcontext;
                   1220: 
                   1221: #define RESTORE_CONTEXT                                        \
                   1222:        wcontext=saved_wcontext;                        \
                   1223:        rcontext=saved_rcontext;                        \
                   1224:        method_frame=saved_method_frame;
                   1225: 
1.363     moko     1226: 
                   1227: Value& Request::construct(Value &class_value, const Method &method){
                   1228:        VStateless_class& called_class=*class_value.get_class();
                   1229: 
                   1230:        if(method.call_type!=Method::CT_STATIC) {
                   1231:                // this is a constructor call
                   1232:                if(Value* result=called_class.create_new_value(fpool)) {
1.319     misha    1233:                                // some stateless_class creatable derivates
1.363     moko     1234:                                return *result;
                   1235:                } else 
1.319     misha    1236:                        throw Exception(PARSER_RUNTIME,
                   1237:                                0, //&frame.name(),
1.363     moko     1238:                                "is not a constructor, system class '%s' can be constructed only implicitly", 
                   1239:                                        called_class.name().cstr());
                   1240:        } else
                   1241:                throw Exception(PARSER_RUNTIME,
                   1242:                        0, //&frame.name(),
1.319     misha    1243:                                "method is static and can not be used as constructor");
1.363     moko     1244: }
1.319     misha    1245: 
1.363     moko     1246: void Request::op_call(VMethodFrame& frame){
1.319     misha    1247:        // see OP_PREPARE_TO_EXPRESSION
                   1248:        frame.set_in_expression(wcontext->get_in_expression());
                   1249:                                
1.332     misha    1250:        SAVE_CONTEXT
                   1251: 
1.365     moko     1252:        rcontext=wcontext=method_frame=&frame;
1.319     misha    1253: 
1.363     moko     1254:        Value& self=frame.self();
                   1255:        const Method& method=frame.method;
                   1256:        Method::Call_type call_type=self.get_class()==&self ? Method::CT_STATIC : Method::CT_DYNAMIC;
1.319     misha    1257: 
1.332     misha    1258:        if(method.call_type==Method::CT_ANY || method.call_type==call_type) { // allowed call type?
                   1259:                if(method.native_code) { // native code?
1.363     moko     1260:                        method.check_actual_numbered_params(self, frame.numbered_params());
1.332     misha    1261:                        method.native_code(*this, *frame.numbered_params()); // execute it
                   1262:                } else // parser code, execute it
                   1263:                        recoursion_checked_execute(*method.parser_code);
                   1264:        } else
                   1265:                throw Exception(PARSER_RUNTIME,
                   1266:                        0,
                   1267:                        "is not allowed to be called %s", 
                   1268:                        call_type==Method::CT_STATIC?"statically":"dynamically");
1.334     misha    1269: 
1.330     misha    1270:        RESTORE_CONTEXT
1.332     misha    1271: }
                   1272: 
                   1273: void Request::op_call_write(VMethodFrame& frame){
                   1274:        VMethodFrame *saved_method_frame=method_frame;
                   1275:        Value* saved_rcontext=rcontext;
1.319     misha    1276: 
1.332     misha    1277:        rcontext=&frame;
                   1278:        method_frame=&frame;
                   1279: 
1.363     moko     1280:        Value& self=frame.self();
                   1281:        const Method& method=frame.method;
                   1282:        Method::Call_type call_type=self.get_class()==&self ? Method::CT_STATIC : Method::CT_DYNAMIC;
1.332     misha    1283: 
1.333     misha    1284:        if(method.call_type==Method::CT_ANY || method.call_type==call_type) { // allowed call type?
                   1285:                if(method.native_code) { // native code?
1.363     moko     1286:                        method.check_actual_numbered_params(self, frame.numbered_params());
1.333     misha    1287:                        method.native_code(*this, *frame.numbered_params()); // execute it
                   1288:                } else // parser code, execute it
                   1289:                        recoursion_checked_execute(*method.parser_code);
1.332     misha    1290:        } else
                   1291:                throw Exception(PARSER_RUNTIME,
                   1292:                        0,
                   1293:                        "is not allowed to be called %s", 
                   1294:                        call_type==Method::CT_STATIC?"statically":"dynamically");
                   1295:        
                   1296:        rcontext=saved_rcontext;
                   1297:        method_frame=saved_method_frame;
1.319     misha    1298: }
                   1299: 
1.330     misha    1300: Value& Request::get_element(Value& ncontext, const String& name) {
1.353     misha    1301:        if(wcontext->get_somebody_entered_some_class()) // ^class:method
                   1302:                if(VStateless_class *called_class=ncontext.get_class())
                   1303:                        if(VStateless_class *read_class=rcontext->get_class())
1.354     misha    1304:                                if(read_class->derived_from(*called_class)){ // current derived from called
                   1305:                                        Value *value=called_class->get_element(get_self(), name);
                   1306:                                        return *(value ? &process_to_value(*value) : VVoid::get());
                   1307:                                }
1.330     misha    1308: 
1.354     misha    1309:        Value* value=ncontext.get_element(name);
1.291     paf      1310: 
1.330     misha    1311:        return *(value ? &process_to_value(*value) : VVoid::get());
1.309     paf      1312: }
                   1313: 
1.329     misha    1314: void Request::put_element(Value& ncontext, const String& name, Value* value) {
1.309     paf      1315:        // put_element can return property-setting-junction
1.371     moko     1316:        if(const VJunction* vjunction=ncontext.put_element(name, value))
1.312     paf      1317:                if(vjunction!=PUT_ELEMENT_REPLACED_ELEMENT) {
1.363     moko     1318:                        const Junction& junction = vjunction->junction();
1.364     moko     1319:                        VConstructorFrame frame(*junction.method, method_frame /*caller*/, junction.self);
1.329     misha    1320: 
1.352     misha    1321:                        size_t param_count=frame.method_params_count();
1.364     moko     1322: 
                   1323:                        if(junction.auto_name){ 
                   1324:                                // default setter
                   1325:                                if(param_count!=2)
                   1326:                                        throw Exception(PARSER_RUNTIME,
                   1327:                                                0,
                   1328:                                                "default setter method must have TWO parameters (has %d parameters)", param_count);
                   1329: 
                   1330:                                Value* params[2] = { new VString(*junction.auto_name), value };
                   1331:                                frame.store_params(params, 2);
                   1332: 
                   1333:                                Temp_disable_default_setter temp(junction.self);
1.365     moko     1334:                                execute_method(frame);
1.364     moko     1335:                        } else {
                   1336:                                // setter
                   1337:                                if(param_count!=1)
                   1338:                                        throw Exception(PARSER_RUNTIME,
                   1339:                                                0,
                   1340:                                                "setter method must have ONE parameter (has %d parameters)", param_count);
                   1341: 
                   1342:                                frame.store_params(&value, 1);
1.365     moko     1343:                                execute_method(frame);
1.364     moko     1344:                        }
                   1345:                }
                   1346: }
                   1347: 
                   1348: StringOrValue Request::process_getter(Junction& junction) {
                   1349:        VMethodFrame frame(*junction.method, method_frame/*caller*/, junction.self);
                   1350:        size_t param_count=frame.method_params_count();
                   1351: 
                   1352:        if(junction.auto_name){ 
                   1353:                // default getter
                   1354:                Value *param;
                   1355: 
                   1356:                if(param_count){
                   1357:                        if(param_count>1)
1.316     misha    1358:                                throw Exception(PARSER_RUNTIME,
1.309     paf      1359:                                        0,
1.364     moko     1360:                                        "default getter method can't have more then 1 parameter (has %d parameters)", param_count);
                   1361:                        param=new VString(*junction.auto_name);
                   1362:                        frame.store_params(&param, 1);
                   1363:                } // no need for else frame.empty_params()
                   1364: 
                   1365:                Temp_disable_default_getter temp(junction.self);
1.365     moko     1366:                execute_method(frame);
1.364     moko     1367:        } else {
                   1368:                // getter
                   1369:                if(param_count!=0)
                   1370:                        throw Exception(PARSER_RUNTIME,
                   1371:                                0,
                   1372:                                "getter method must have no parameters (has %d parameters)", param_count);
                   1373:                
                   1374:                // no need for frame.empty_params()
1.365     moko     1375:                execute_method(frame);
1.364     moko     1376:        }
1.309     paf      1377: 
1.364     moko     1378:        return frame.result();
1.34      paf      1379: }
1.70      paf      1380: 
1.162     parser   1381: /**    @param intercept_string
1.116     paf      1382:        - true:
                   1383:                they want result=string value, 
                   1384:                possible object result goes to wcontext
                   1385:        - false:
                   1386:                they want any result[string|object]
                   1387:                nothing goes to wcontext.
1.125     paf      1388:                used in @c (expression) params evaluation
1.224     paf      1389: 
1.334     misha    1390:        using the fact it's either string_ or value_ result requested to speed up checkes
1.116     paf      1391: */
1.364     moko     1392: 
1.229     paf      1393: StringOrValue Request::process(Value& input_value, bool intercept_string) {
1.297     paf      1394:        Junction* junction=input_value.get_junction();
1.307     paf      1395:        if(junction) {
                   1396:                if(junction->is_getter) { // is it a getter-junction?
1.364     moko     1397:                        return process_getter(*junction);
1.307     paf      1398:                }
                   1399: 
                   1400:                if(junction->code) { // is it a code-junction?
                   1401:                        // process it
                   1402:                        StringOrValue result;
1.336     misha    1403: 
                   1404:                        DEBUG_PRINT_STR("ja->\n")
1.238     paf      1405: 
1.307     paf      1406:                        if(!junction->method_frame)
1.316     misha    1407:                                throw Exception(PARSER_RUNTIME,
1.352     misha    1408:                                        0,
                   1409:                                        "junction used outside of context");
1.240     paf      1410: 
1.330     misha    1411:                        SAVE_CONTEXT
1.307     paf      1412: 
                   1413:                        method_frame=junction->method_frame;
                   1414:                        rcontext=junction->rcontext;
                   1415: 
                   1416:                        // for expression method params
                   1417:                        // wcontext is set 0
                   1418:                        // using the fact in decision "which wwrapper to use"
                   1419:                        bool using_code_frame=intercept_string && junction->wcontext;
                   1420:                        if(using_code_frame) {
1.318     misha    1421:                                // almost plain wwrapper about junction wcontext 
                   1422: 
                   1423:                                VCodeFrame local(*junction->wcontext);
1.307     paf      1424:                                wcontext=&local;
                   1425: 
                   1426:                                // execute it
                   1427:                                recoursion_checked_execute(*junction->code);
                   1428: 
                   1429:                                // CodeFrame soul:
1.318     misha    1430:                                result=wcontext->result();
1.307     paf      1431:                        } else {
                   1432:                                // plain wwrapper
1.351     misha    1433:                                WWrapper local(wcontext);
1.307     paf      1434:                                wcontext=&local;
                   1435: 
                   1436:                                // execute it
                   1437:                                recoursion_checked_execute(*junction->code);
                   1438: 
                   1439:                                result=wcontext->result();
                   1440:                        }
                   1441: 
1.330     misha    1442:                        RESTORE_CONTEXT
1.207     paf      1443: 
1.336     misha    1444:                        DEBUG_PRINT_STR("<-ja returned")
                   1445: 
1.307     paf      1446:                        return result;
                   1447:                }
                   1448: 
                   1449:                // it is then method-junction, do not explode it
                   1450:                // just return it as we do for usual objects
                   1451:        }       
                   1452: 
                   1453:        return input_value;
1.85      paf      1454: }
                   1455: 
1.332     misha    1456: void Request::process_write(Value& input_value) {
                   1457:        Junction* junction=input_value.get_junction();
                   1458:        if(junction) {
                   1459:                if(junction->is_getter) { // is it a getter-junction?
1.364     moko     1460:                        write_pass_lang(process_getter(*junction));
1.332     misha    1461:                        return;
                   1462:                }
                   1463: 
                   1464:                if(junction->code) { // is it a code-junction?
                   1465:                                                        // process it
1.336     misha    1466: 
                   1467:                        DEBUG_PRINT_STR("ja->\n")
                   1468: 
1.332     misha    1469:                        if(!junction->method_frame)
                   1470:                                throw Exception(PARSER_RUNTIME,
                   1471:                                        0,
                   1472:                                        "junction used outside of context");
                   1473: 
                   1474:                        SAVE_CONTEXT
                   1475: 
                   1476:                        method_frame=junction->method_frame;
                   1477:                        rcontext=junction->rcontext;
                   1478: 
                   1479:                        // for expression method params
                   1480:                        // wcontext is set 0
                   1481:                        // using the fact in decision "which wwrapper to use"
                   1482: #ifdef OPTIMIZE_CALL
                   1483:                        if(wcontext==junction->wcontext){
                   1484:                                // no wrappers for wcontext
                   1485:                                recoursion_checked_execute(*junction->code);
                   1486:                                RESTORE_CONTEXT
                   1487: 
                   1488:                        } else
                   1489: #endif
                   1490:                        if(junction->wcontext) {
                   1491:                                // almost plain wwrapper about junction wcontext 
                   1492:                                VCodeFrame local(*junction->wcontext);
                   1493:                                wcontext=&local;
                   1494: 
                   1495:                                // execute it
                   1496:                                recoursion_checked_execute(*junction->code);
                   1497:                                RESTORE_CONTEXT
                   1498:                                write_pass_lang(local.result());
                   1499:                        } else {
                   1500:                                // plain wwrapper
1.351     misha    1501:                                WWrapper local(wcontext);
1.332     misha    1502:                                wcontext=&local;
                   1503: 
                   1504:                                // execute it
                   1505:                                recoursion_checked_execute(*junction->code);
                   1506:                                RESTORE_CONTEXT
                   1507:                                write_pass_lang(local.result());
                   1508:                        }
1.336     misha    1509: 
                   1510:                        DEBUG_PRINT_STR("<-ja returned")
                   1511: 
1.332     misha    1512:                        return;
                   1513:                }
                   1514: 
                   1515:                // it is then method-junction, do not explode it
                   1516:                // just return it as we do for usual objects
                   1517:        }
                   1518: 
                   1519:        write_pass_lang(input_value);
                   1520: }
                   1521: 
1.365     moko     1522: void Request::execute_method(VMethodFrame& aframe) {
1.330     misha    1523:        SAVE_CONTEXT
1.99      paf      1524:        
                   1525:        // initialize contexts
1.365     moko     1526:        rcontext=wcontext=method_frame=&aframe;
1.99      paf      1527:        
                   1528:        // execute!     
1.365     moko     1529:        recoursion_checked_execute(*aframe.method.parser_code);
1.208     paf      1530:        
1.330     misha    1531:        RESTORE_CONTEXT
1.208     paf      1532: }
                   1533: 
1.297     paf      1534: const String* Request::execute_method(Value& aself, 
1.329     misha    1535:                                        const Method& method, Value* optional_param,
                   1536:                                        bool do_return_string) {
1.317     misha    1537: 
1.363     moko     1538:        VMethodFrame local_frame(method, method_frame/*caller*/, aself);
1.365     moko     1539: 
1.329     misha    1540:        if(optional_param && local_frame.method_params_count()>0) {
                   1541:                local_frame.store_params(&optional_param, 1);
1.330     misha    1542:        } else {
                   1543:                local_frame.empty_params();
1.242     paf      1544:        }
1.246     paf      1545: 
                   1546:        // prevent non-string writes for better error reporting
1.297     paf      1547:        if(do_return_string)
1.357     misha    1548:                local_frame.write(local_frame);
1.208     paf      1549:        
1.365     moko     1550:        execute_method(local_frame);
1.99      paf      1551:        
1.365     moko     1552:        return do_return_string ? local_frame.get_string() : 0;
1.242     paf      1553: }
                   1554: 
1.297     paf      1555: Request::Execute_nonvirtual_method_result 
                   1556: Request::execute_nonvirtual_method(VStateless_class& aclass, 
1.330     misha    1557:                                        const String& method_name,
                   1558:                                        VString* optional_param,
                   1559:                                        bool do_return_string) {
1.297     paf      1560:        Execute_nonvirtual_method_result result;
                   1561:        result.method=aclass.get_method(method_name);
                   1562:        if(result.method)
1.317     misha    1563:                result.string=execute_method(aclass, *result.method, optional_param, do_return_string);
1.297     paf      1564:        return result;
1.99      paf      1565: }
                   1566: 
1.297     paf      1567: const String* Request::execute_virtual_method(Value& aself, 
1.330     misha    1568:                                        const String& method_name) {
1.354     misha    1569:        if(Value* value=aself.get_element(method_name))
1.297     paf      1570:                if(Junction* junction=value->get_junction())
                   1571:                        if(const Method *method=junction->method) 
                   1572:                                return execute_method(aself, *method, 0/*no params*/, true);
1.188     parser   1573:                        
1.176     parser   1574:        return 0;
                   1575: }
1.362     misha    1576: 
                   1577: const String* Request::get_method_filename(const Method* method){
                   1578:        if(ArrayOperation* code=method->parser_code)
                   1579:                if(code){
                   1580:                        Operation::Origin origin={0, 0, 0};
                   1581:                        Array_iterator<Operation> i(*code);
                   1582:                        while( i.has_next() ){
                   1583:                                switch( i.next().code ){
                   1584:                                        case OP::OP_CURLY_CODE__STORE_PARAM: 
                   1585:                                        case OP::OP_EXPR_CODE__STORE_PARAM:
                   1586:                                        case OP::OP_CURLY_CODE__CONSTRUCT:
                   1587:                                        case OP::OP_NESTED_CODE:
                   1588:                                        case OP::OP_OBJECT_POOL:
                   1589:                                        case OP::OP_STRING_POOL:
                   1590:                                        case OP::OP_CALL:
                   1591:                                        case OP::OP_CALL__WRITE:
                   1592:                                                {
                   1593:                                                        i.next(); // skip local ops
                   1594:                                                        i.next(); // skip next opcode
                   1595:                                                        // continue execution
                   1596:                                                }
                   1597:                                        case OP::OP_CONSTRUCT_OBJECT:
                   1598:                                        case OP::OP_CONSTRUCT_OBJECT__WRITE:
                   1599:                                        case OP::OP_VALUE:
                   1600:                                        case OP::OP_STRING__WRITE:
                   1601:                                        case OP::OP_VALUE__GET_CLASS:
                   1602: #ifdef OPTIMIZE_BYTECODE_GET_OBJECT_ELEMENT
                   1603:                                        case OP::OP_GET_OBJECT_ELEMENT:
                   1604:                                        case OP::OP_GET_OBJECT_ELEMENT__WRITE:
                   1605: #endif
                   1606: #ifdef OPTIMIZE_BYTECODE_GET_OBJECT_VAR_ELEMENT
                   1607:                                        case OP::OP_GET_OBJECT_VAR_ELEMENT:
                   1608:                                        case OP::OP_GET_OBJECT_VAR_ELEMENT__WRITE:
                   1609: #endif
                   1610: #ifdef OPTIMIZE_BYTECODE_GET_ELEMENT
                   1611:                                        case OP::OP_VALUE__GET_ELEMENT:
                   1612:                                        case OP::OP_VALUE__GET_ELEMENT__WRITE:
                   1613:                                        case OP::OP_VALUE__GET_ELEMENT_OR_OPERATOR:
                   1614:                                        case OP::OP_WITH_ROOT__VALUE__GET_ELEMENT:
                   1615: #endif
                   1616: #ifdef OPTIMIZE_BYTECODE_GET_SELF_ELEMENT
                   1617:                                        case OP::OP_WITH_SELF__VALUE__GET_ELEMENT:
                   1618:                                        case OP::OP_WITH_SELF__VALUE__GET_ELEMENT__WRITE:
                   1619: #endif
                   1620: #ifdef OPTIMIZE_BYTECODE_CONSTRUCT
                   1621:                                        case OP::OP_WITH_ROOT__VALUE__CONSTRUCT_EXPR:
                   1622:                                        case OP::OP_WITH_ROOT__VALUE__CONSTRUCT_VALUE:
                   1623:                                        case OP::OP_WITH_WRITE__VALUE__CONSTRUCT_EXPR:
                   1624:                                        case OP::OP_WITH_WRITE__VALUE__CONSTRUCT_VALUE:
                   1625:                                        case OP::OP_WITH_SELF__VALUE__CONSTRUCT_EXPR:
                   1626:                                        case OP::OP_WITH_SELF__VALUE__CONSTRUCT_VALUE:
                   1627: #endif
                   1628:                                                {
                   1629:                                                        origin=i.next().origin;
                   1630:                                                        break;
                   1631:                                                }
1.373   ! moko     1632:                                        default: break;
1.362     misha    1633:                                }
                   1634:                                if(origin.file_no)
                   1635:                                        return get_used_filename(origin.file_no);
                   1636:                        }
                   1637:                }
                   1638:        return 0;
                   1639: }
                   1640: 

E-mail: