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

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

E-mail: