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

1.117     paf         1: /** @file
1.118     paf         2:        Parser: executor part of request class.
                      3: 
1.324     misha       4:        Copyright (c) 2001-2009 ArtLebedev Group (http://www.artlebedev.com)
1.218     paf         5:        Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.243     paf         6: */
1.118     paf         7: 
1.329   ! misha       8: static const char * const IDENT_EXECUTE_C="$Date: 2009-04-28 11:11:11 $";
1.1       paf         9: 
1.152     paf        10: #include "pa_opcode.h"
1.8       paf        11: #include "pa_array.h" 
1.11      paf        12: #include "pa_request.h"
1.15      paf        13: #include "pa_vstring.h"
1.22      paf        14: #include "pa_vhash.h"
1.167     parser     15: #include "pa_vvoid.h"
1.145     paf        16: #include "pa_vcode_frame.h"
1.321     misha      17: #include "pa_vmethod_frame.h"
1.38      paf        18: #include "pa_vobject.h"
1.49      paf        19: #include "pa_vdouble.h"
1.54      paf        20: #include "pa_vbool.h"
1.94      paf        21: #include "pa_vtable.h"
1.132     paf        22: #include "pa_vfile.h"
1.144     paf        23: #include "pa_vimage.h"
1.194     parser     24: #include "pa_wwrapper.h"
1.1       paf        25: 
1.233     paf        26: //#define DEBUG_EXECUTE
1.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: #ifdef OPTIMIZE_BYTECODE_GET_CLASS
                     37:        "VALUE__GET_CLASS",
                     38: #else
1.66      paf        39:        "GET_CLASS",
1.328     misha      40: #endif
1.182     parser     41:        "CONSTRUCT_VALUE", "CONSTRUCT_EXPR", "CURLY_CODE__CONSTRUCT",
1.108     paf        42:        "WRITE_VALUE",  "WRITE_EXPR_RESULT",  "STRING__WRITE",
1.328     misha      43: #ifdef OPTIMIZE_BYTECODE_GET_ELEMENT
                     44:        "VALUE__GET_ELEMENT_OR_OPERATOR",
                     45: #else
                     46:        "GET_ELEMENT_OR_OPERATOR",
                     47: #endif
                     48:        "GET_ELEMENT",
                     49: #ifdef OPTIMIZE_BYTECODE_GET_ELEMENT
                     50:        "VALUE__GET_ELEMENT",
                     51: #endif
                     52:        "GET_ELEMENT__WRITE",
                     53: #ifdef OPTIMIZE_BYTECODE_GET_ELEMENT
                     54:        "VALUE__GET_ELEMENT__WRITE",
                     55: #endif
1.232     paf        56:        "OBJECT_POOL",  "STRING_POOL",
                     57:        "PREPARE_TO_CONSTRUCT_OBJECT",  "PREPARE_TO_EXPRESSION", 
                     58:        "CALL", "CALL__WRITE",
1.49      paf        59: 
                     60:        // expression ops: unary
1.148     paf        61:        "NEG", "INV", "NOT", "DEF", "IN", "FEXISTS", "DEXISTS",
1.49      paf        62:        // expression ops: binary
1.201     paf        63:        "SUB", "ADD", "MUL", "DIV", "MOD", "INTDIV",
1.275     paf        64:        "BIN_SL", "BIN_SR",
1.56      paf        65:        "BIN_AND", "BIN_OR", "BIN_XOR",
                     66:        "LOG_AND", "LOG_OR", "LOG_XOR",
1.49      paf        67:        "NUM_LT", "NUM_GT", "NUM_LE", "NUM_GE", "NUM_EQ", "NUM_NE",
1.103     paf        68:        "STR_LT", "STR_GT", "STR_LE", "STR_GE", "STR_EQ", "STR_NE",
                     69:        "IS"
1.1       paf        70: };
                     71: 
1.297     paf        72: void va_debug_printf(SAPI_Info& sapi_info, const char* fmt,va_list args) {
1.127     paf        73:        char buf[MAX_STRING];
                     74:        vsnprintf(buf, MAX_STRING, fmt, args);
1.297     paf        75:        SAPI::log(sapi_info, "%s", buf);
1.107     paf        76: }
                     77: 
1.297     paf        78: void debug_printf(SAPI_Info& sapi_info, const char* fmt, ...) {
1.107     paf        79:     va_list args;
1.297     paf        80:     va_start(args, fmt);
                     81:     va_debug_printf(sapi_info, fmt, args);
1.107     paf        82:     va_end(args);
1.105     paf        83: }
                     84: 
1.297     paf        85: void debug_dump(SAPI_Info& sapi_info, int level, ArrayOperation& ops) {
                     86:        Array_iterator<Operation> i(ops);
1.190     parser     87:        while(i.has_next()) {
1.323     misha      88:                OP::OPCODE opcode=i.next().code;
1.1       paf        89: 
1.328     misha      90:                if(
                     91:                        opcode==OP::OP_VALUE
                     92:                        || opcode==OP::OP_STRING__WRITE
                     93: #ifdef OPTIMIZE_BYTECODE_GET_CLASS
                     94:                        || opcode==OP::OP_VALUE__GET_CLASS
                     95: #endif
                     96: #ifdef OPTIMIZE_BYTECODE_GET_ELEMENT
                     97:                        || opcode==OP::OP_VALUE__GET_ELEMENT
                     98:                        || opcode==OP::OP_VALUE__GET_ELEMENT__WRITE
                     99:                        || opcode==OP::OP_VALUE__GET_ELEMENT_OR_OPERATOR
                    100: #endif
                    101:                ) {
1.297     paf       102:                        Operation::Origin origin=i.next().origin;
                    103:                        Value& value=*i.next().value;
                    104:                        debug_printf(sapi_info, 
1.133     paf       105:                                "%*s%s"
                    106:                                " \"%s\" %s", 
1.297     paf       107:                                level*4, "", opcode_name[opcode],
                    108:                                value.get_string()->cstr(), value.type());
1.133     paf       109:                        continue;
1.15      paf       110:                }
1.297     paf       111:                debug_printf(sapi_info, "%*s%s", level*4, "", opcode_name[opcode]);
1.1       paf       112: 
1.297     paf       113:                switch(opcode) {
1.323     misha     114:                case OP::OP_CURLY_CODE__STORE_PARAM: 
                    115:                case OP::OP_EXPR_CODE__STORE_PARAM:
                    116:                case OP::OP_CURLY_CODE__CONSTRUCT:
                    117:                case OP::OP_NESTED_CODE:
                    118:                case OP::OP_OBJECT_POOL:  
                    119:                case OP::OP_STRING_POOL:
                    120:                case OP::OP_CALL:
                    121:                case OP::OP_CALL__WRITE:
1.297     paf       122:                        if(ArrayOperation* local_ops=i.next().ops)
                    123:                                debug_dump(sapi_info, level+1, *local_ops);
1.1       paf       124:                }
                    125:        }
                    126: }
1.157     parser    127: #endif
1.1       paf       128: 
1.297     paf       129: // Request
                    130: 
                    131: void Request::execute(ArrayOperation& ops) {
                    132:        register Stack<StackItem>& stack=this->stack; // helps a lot on MSVC: 'esi'
1.159     parser    133: 
1.304     paf       134:        const String* debug_name=0;  Operation::Origin debug_origin={0, 0, 0};
1.297     paf       135:        try{
1.157     parser    136: #ifdef DEBUG_EXECUTE
1.297     paf       137:        debug_printf(sapi_info, "source----------------------------\n");
                    138:        debug_dump(sapi_info, 0, ops);
                    139:        debug_printf(sapi_info, "execution-------------------------\n");
1.157     parser    140: #endif
1.297     paf       141:        for(Array_iterator<Operation> i(ops); i.has_next(); ) {
1.322     misha     142:                OP::OPCODE opcode=i.next().code;
1.293     paf       143: 
1.157     parser    144: #ifdef DEBUG_EXECUTE
1.297     paf       145:                debug_printf(sapi_info, "%d:%s", stack.top_index()+1, opcode_name[opcode]);
1.157     parser    146: #endif
1.11      paf       147: 
1.297     paf       148:                switch(opcode) {
1.51      paf       149:                // param in next instruction
1.322     misha     150:                case OP::OP_VALUE:
1.32      paf       151:                        {
1.297     paf       152:                                debug_origin=i.next().origin;
                    153:                                Value& value=*i.next().value;
1.157     parser    154: #ifdef DEBUG_EXECUTE
1.297     paf       155:                                debug_printf(sapi_info, " \"%s\" %s", value.get_string()->cstr(), value.type());
1.157     parser    156: #endif
1.297     paf       157:                                stack.push(value);
1.32      paf       158:                                break;
                    159:                        }
1.328     misha     160: 
                    161: #ifdef OPTIMIZE_BYTECODE_GET_CLASS
                    162:                case OP::OP_VALUE__GET_CLASS:
                    163:                        {
                    164:                                // maybe they do ^class:method[] call, remember the fact
                    165:                                wcontext->set_somebody_entered_some_class();
                    166: 
                    167:                                debug_origin=i.next().origin;
                    168:                                Value& value=*i.next().value;
                    169:                                const String& name=*value.get_string();
                    170: 
                    171: #ifdef DEBUG_EXECUTE
                    172:                                debug_printf(sapi_info, " \"%s\" ", name.cstr());
                    173: #endif
                    174: 
                    175:                                Value* class_value=classes().get(name);
                    176:                                if(!class_value)
                    177:                                        throw Exception(PARSER_RUNTIME,
                    178:                                                &name,
                    179:                                                "class is undefined"); 
                    180: 
                    181:                                stack.push(*class_value);
                    182:                                break;
                    183:                        }
                    184: #else
1.322     misha     185:                case OP::OP_GET_CLASS:
1.38      paf       186:                        {
1.130     paf       187:                                // maybe they do ^class:method[] call, remember the fact
1.215     paf       188:                                wcontext->set_somebody_entered_some_class();
1.98      paf       189: 
1.297     paf       190:                                const String& name=stack.pop().string();
                    191:                                Value* value=classes().get(name);
1.120     paf       192:                                if(!value) 
1.316     misha     193:                                        throw Exception(PARSER_RUNTIME,
1.66      paf       194:                                                &name,
1.143     paf       195:                                                "class is undefined"); 
1.66      paf       196: 
1.297     paf       197:                                stack.push(*value);
1.38      paf       198:                                break;
                    199:                        }
1.328     misha     200: #endif
1.51      paf       201:                // OP_WITH
1.322     misha     202:                case OP::OP_WITH_ROOT:
1.187     parser    203:                        {
1.297     paf       204:                                stack.push(*method_frame);
1.187     parser    205:                                break;
                    206:                        }
1.322     misha     207:                case OP::OP_WITH_SELF: 
1.37      paf       208:                        {
1.297     paf       209:                                stack.push(get_self());
1.37      paf       210:                                break;
                    211:                        }
1.322     misha     212:                case OP::OP_WITH_READ: 
1.15      paf       213:                        {
1.297     paf       214:                                stack.push(*rcontext);
1.20      paf       215:                                break;
                    216:                        }
1.322     misha     217:                case OP::OP_WITH_WRITE: 
1.20      paf       218:                        {
1.287     paf       219:                                if(wcontext==method_frame)
1.316     misha     220:                                        throw Exception(PARSER_RUNTIME,
1.287     paf       221:                                                0,
                    222:                                                "$.name outside of $name[...]");
                    223: 
1.297     paf       224:                                stack.push(*wcontext);
1.20      paf       225:                                break;
                    226:                        }
1.37      paf       227:                        
1.51      paf       228:                // OTHER ACTIONS BUT WITHs
1.322     misha     229:                case OP::OP_CONSTRUCT_VALUE:
1.20      paf       230:                        {
1.297     paf       231:                                Value& value=stack.pop().value();
                    232:                                const String& name=stack.pop().string();  debug_name=&name;
                    233:                                Value& ncontext=stack.pop().value();
1.329   ! misha     234:                                put_element(ncontext, name, &value);
1.308     paf       235: 
1.213     paf       236:                                break;
                    237:                        }
1.322     misha     238:                case OP::OP_CONSTRUCT_EXPR:
1.80      paf       239:                        {
1.219     paf       240:                                // see OP_PREPARE_TO_EXPRESSION
                    241:                                wcontext->set_in_expression(false);
                    242: 
1.308     paf       243:                                Value& expr=stack.pop().value();
1.297     paf       244:                                const String& name=stack.pop().string();  debug_name=&name;
                    245:                                Value& ncontext=stack.pop().value();
1.308     paf       246:                                Value& value=expr.as_expr_result();
1.329   ! misha     247:                                put_element(ncontext, name, &value);
1.80      paf       248:                                break;
                    249:                        }
1.322     misha     250:                case OP::OP_CURLY_CODE__CONSTRUCT:
1.182     parser    251:                        {
1.297     paf       252:                                ArrayOperation& local_ops=*i.next().ops;
1.182     parser    253: #ifdef DEBUG_EXECUTE
1.297     paf       254:                                debug_printf(sapi_info, " (%d)\n", local_ops.count());
                    255:                                debug_dump(sapi_info, 1, local_ops);
1.326     misha     256: #endif
                    257:                                VJunction& value=*new VJunction(
1.297     paf       258:                                        get_self(), 0,
1.257     paf       259:                                        method_frame, 
1.240     paf       260:                                        rcontext, 
                    261:                                        wcontext, 
1.312     paf       262:                                        &local_ops);
1.238     paf       263: 
1.326     misha     264:                                wcontext->attach_junction(&value);
                    265: 
1.297     paf       266:                                const String& name=stack.pop().string();  debug_name=&name;
                    267:                                Value& ncontext=stack.pop().value();
1.312     paf       268:                                if(const VJunction* vjunction=ncontext.put_element(ncontext, name, &value, false))
                    269:                                        if(vjunction!=PUT_ELEMENT_REPLACED_ELEMENT)
1.316     misha     270:                                                throw Exception(PARSER_RUNTIME,
1.308     paf       271:                                                        0,
                    272:                                                        "property value can not be code, use [] or () brackets");
                    273:                                        
1.182     parser    274:                                break;
                    275:                        }
1.322     misha     276:                case OP::OP_NESTED_CODE:
1.209     paf       277:                        {
1.297     paf       278:                                ArrayOperation& local_ops=*i.next().ops;
1.209     paf       279: #ifdef DEBUG_EXECUTE
1.297     paf       280:                                debug_printf(sapi_info, " (%d)\n", local_ops.count());
                    281:                                debug_dump(sapi_info, 1, local_ops);
1.209     paf       282: #endif                         
1.297     paf       283:                                stack.push(local_ops);
1.209     paf       284:                                break;
                    285:                        }
1.322     misha     286:                case OP::OP_WRITE_VALUE:
1.13      paf       287:                        {
1.297     paf       288:                                Value& value=stack.pop().value();
                    289:                                write_assign_lang(value);
1.86      paf       290:                                break;
                    291:                        }
1.322     misha     292:                case OP::OP_WRITE_EXPR_RESULT:
1.108     paf       293:                        {
1.297     paf       294:                                Value& value=stack.pop().value();
                    295:                                write_no_lang(value.as_expr_result());
1.230     paf       296: 
                    297:                                // must be after write(result) and 
1.219     paf       298:                                // see OP_PREPARE_TO_EXPRESSION
                    299:                                wcontext->set_in_expression(false);
1.108     paf       300:                                break;
                    301:                        }
1.322     misha     302:                case OP::OP_STRING__WRITE:
1.86      paf       303:                        {
1.297     paf       304:                                i.next(); // ignore origin
                    305:                                Value* value=i.next().value;
1.157     parser    306: #ifdef DEBUG_EXECUTE
1.297     paf       307:                                debug_printf(sapi_info, " \"%s\"", value->get_string()->cstr());
1.157     parser    308: #endif
1.297     paf       309:                                write_no_lang(*value->get_string());
1.13      paf       310:                                break;
1.14      paf       311:                        }
1.328     misha     312: 
                    313: #ifdef OPTIMIZE_BYTECODE_GET_ELEMENT
                    314:                case OP::OP_VALUE__GET_ELEMENT_OR_OPERATOR:
                    315:                        {
                    316:                                debug_origin=i.next().origin;
                    317:                                const String& name=*i.next().value->get_string();  debug_name=&name;
                    318: 
                    319: #ifdef DEBUG_EXECUTE
                    320:                                debug_printf(sapi_info, " \"%s\" ", name.cstr());
                    321: #endif
                    322: 
                    323:                                Value& value=get_element(*rcontext, name, true);
                    324:                                stack.push(value);
                    325:                                break;
                    326:                        }
                    327: #else
1.322     misha     328:                case OP::OP_GET_ELEMENT_OR_OPERATOR:
1.215     paf       329:                        {
1.297     paf       330:                                const String& name=stack.pop().string();  debug_name=&name;
                    331:                                Value& ncontext=stack.pop().value();
                    332:                                Value& value=get_element(ncontext, name, true);
                    333:                                stack.push(value);
1.215     paf       334:                                break;
                    335:                        }
1.328     misha     336: #endif
                    337: 
1.322     misha     338:                case OP::OP_GET_ELEMENT:
1.11      paf       339:                        {
1.297     paf       340:                                const String& name=stack.pop().string();  debug_name=&name;
                    341:                                Value& ncontext=stack.pop().value();
                    342:                                Value& value=get_element(ncontext, name, false);
                    343:                                stack.push(value);
1.17      paf       344:                                break;
                    345:                        }
                    346: 
1.328     misha     347: #ifdef OPTIMIZE_BYTECODE_GET_ELEMENT
                    348:                case OP::OP_VALUE__GET_ELEMENT:
                    349:                        {
                    350:                                debug_origin=i.next().origin;
                    351:                                const String& name=*i.next().value->get_string(); debug_name=&name;
                    352: 
                    353: #ifdef DEBUG_EXECUTE
                    354:                                debug_printf(sapi_info, " \"%s\" ", name.cstr());
                    355: #endif
                    356: 
                    357:                                Value& value=get_element(*rcontext, name, false);
                    358:                                stack.push(value);
                    359:                                break;
                    360:                        }
                    361: #endif
                    362: 
                    363: 
1.322     misha     364:                case OP::OP_GET_ELEMENT__WRITE:
1.17      paf       365:                        {
1.297     paf       366:                                const String& name=stack.pop().string();  debug_name=&name;
                    367:                                Value& ncontext=stack.pop().value();
                    368:                                Value& value=get_element(ncontext, name, false);
                    369:                                write_assign_lang(value);
1.17      paf       370:                                break;
                    371:                        }
                    372: 
1.328     misha     373: #ifdef OPTIMIZE_BYTECODE_GET_ELEMENT
                    374:                case OP::OP_VALUE__GET_ELEMENT__WRITE:
                    375:                        {
                    376:                                debug_origin=i.next().origin;
                    377:                                const String& name=*i.next().value->get_string(); debug_name=&name;
                    378: 
                    379: #ifdef DEBUG_EXECUTE
                    380:                                debug_printf(sapi_info, " \"%s\" ", name.cstr());
                    381: #endif
                    382: 
                    383:                                Value& value=get_element(*rcontext, name, false);
                    384:                                write_assign_lang(value);
                    385:                                break;
                    386:                        }
                    387: #endif
1.32      paf       388: 
1.322     misha     389:                case OP::OP_OBJECT_POOL:
1.17      paf       390:                        {
1.297     paf       391:                                ArrayOperation& local_ops=*i.next().ops;
1.226     paf       392:                                
1.257     paf       393:                                WContext *saved_wcontext=wcontext;
1.297     paf       394:                                String::Language saved_lang=flang;
                    395:                                flang=String::L_PASS_APPENDED;
                    396:                                WWrapper local(0/*empty*/, wcontext);
1.226     paf       397:                                wcontext=&local;
                    398: 
1.297     paf       399:                                execute(local_ops);
1.226     paf       400: 
1.297     paf       401:                                stack.push(wcontext->result().as_value());
1.257     paf       402:                                flang=saved_lang;
                    403:                                wcontext=saved_wcontext;
1.13      paf       404:                                break;
1.15      paf       405:                        }
1.13      paf       406:                        
1.322     misha     407:                case OP::OP_STRING_POOL:
1.49      paf       408:                        {
1.297     paf       409:                                ArrayOperation& local_ops=*i.next().ops;
1.226     paf       410: 
1.257     paf       411:                                WContext *saved_wcontext=wcontext;
1.297     paf       412:                                WWrapper local(0 /*empty*/, wcontext);
1.226     paf       413:                                wcontext=&local;
                    414: 
1.297     paf       415:                                execute(local_ops);
1.226     paf       416: 
1.297     paf       417:                                Value* value;
1.49      paf       418:                                // from "$a $b" part of expression taking only string value,
                    419:                                // ignoring any other content of wcontext
1.297     paf       420:                                if(const String* string=wcontext->get_string())
                    421:                                        value=new VString(*string);
1.80      paf       422:                                else
1.324     misha     423:                                        value=VVoid::get();
1.297     paf       424:                                stack.push(*value);
                    425: 
1.257     paf       426:                                wcontext=saved_wcontext;
1.49      paf       427:                                break;
                    428:                        }
                    429: 
1.51      paf       430:                // CALL
1.322     misha     431:                case OP::OP_CURLY_CODE__STORE_PARAM:
                    432:                case OP::OP_EXPR_CODE__STORE_PARAM:
1.28      paf       433:                        {
1.237     paf       434:                                // code
1.297     paf       435:                                ArrayOperation& local_ops=*i.next().ops;
1.237     paf       436: #ifdef DEBUG_EXECUTE
1.297     paf       437:                                debug_printf(sapi_info, " (%d)\n", local_ops.count());
                    438:                                debug_dump(sapi_info, 1, local_ops);
1.237     paf       439: #endif                         
                    440:                                // when they evaluate expression parameter,
                    441:                                // the object expression result
                    442:                                // does not need to be written into calling frame
                    443:                                // it must go into any expressions using that parameter
                    444:                                // hence, we zero junction.wcontext here, and later
                    445:                                // in .process we would test that field 
                    446:                                // in decision "which wwrapper to use"
1.326     misha     447:                                VJunction& value=*new VJunction(
1.297     paf       448:                                        get_self(), 0,
1.257     paf       449:                                        method_frame, 
1.237     paf       450:                                        rcontext, 
1.322     misha     451:                                        opcode==OP::OP_EXPR_CODE__STORE_PARAM?0:wcontext, 
1.312     paf       452:                                        &local_ops);
1.326     misha     453: #ifdef USE_DESTRUCTORS
                    454:                                value.set_temporal(true);
                    455: #else
                    456:                                if (opcode!=OP::OP_EXPR_CODE__STORE_PARAM)
                    457: #endif
                    458:                                wcontext->attach_junction(&value);
1.237     paf       459:                                // store param
                    460:                                // this op is executed from CALL local_ops only, so can not check method_frame_to_fill==0
1.329   ! misha     461:                                stack.push(value);
1.29      paf       462:                                break;
                    463:                        }
                    464: 
1.322     misha     465:                case OP::OP_PREPARE_TO_CONSTRUCT_OBJECT:
1.186     parser    466:                        {
1.200     paf       467:                                wcontext->set_constructing(true);
1.186     parser    468:                                break;
                    469:                        }
1.219     paf       470: 
1.322     misha     471:                case OP::OP_PREPARE_TO_EXPRESSION:
1.219     paf       472:                        {
                    473:                                wcontext->set_in_expression(true);
                    474:                                break;
                    475:                        }
                    476: 
1.322     misha     477:                case OP::OP_CALL:
                    478:                case OP::OP_CALL__WRITE:
1.29      paf       479:                        {
1.297     paf       480:                                //is_debug_junction=true;
                    481:                                ArrayOperation* local_ops=i.next().ops;
1.157     parser    482: #ifdef DEBUG_EXECUTE
1.297     paf       483:                                debug_printf(sapi_info, " (%d)\n", local_ops?local_ops->count():0);
                    484:                                if(local_ops)
                    485:                                        debug_dump(sapi_info, 1, *local_ops);
1.237     paf       486: 
1.297     paf       487:                                debug_printf(sapi_info, "->\n");
1.157     parser    488: #endif
1.297     paf       489:                                Value& value=stack.pop().value();
1.237     paf       490: 
1.297     paf       491:                                Junction* junction=value.get_junction();
                    492:                                if(!junction) {
                    493:                                        if(value.is("void"))
1.316     misha     494:                                                throw Exception(PARSER_RUNTIME,
1.297     paf       495:                                                        0,
                    496:                                                        "undefined method");
                    497:                                        else
1.316     misha     498:                                                throw Exception(PARSER_RUNTIME,
1.297     paf       499:                                                        0,
                    500:                                                        "is '%s', not a method or junction, can not call it",
                    501:                                                                value.type());
                    502:                                }
1.282     paf       503:                                /* no check needed, code compiled the way that that's impossible
1.276     paf       504:                                // check: 
                    505:                                //      that this is method-junction, not a code-junction
1.278     paf       506:                                // [disabling these contstructions:]
                    507:                                // $junction{code}
                    508:                                //  ^junction[]
1.276     paf       509:                                if(!junction->method)
1.316     misha     510:                                        throw Exception(PARSER_RUNTIME,
1.297     paf       511:                                                "is '%s', it is code junction, can not call it",
                    512:                                                        value.type());
1.282     paf       513:                                */
1.237     paf       514: 
1.321     misha     515:                                VMethodFrame frame(*junction, method_frame);
                    516:                                if(local_ops){ // store param code goes here
1.329   ! misha     517:                                        size_t first = stack.top_index();
1.321     misha     518:                                        execute(*local_ops);
1.329   ! misha     519:                                        frame.store_params((Value**)stack.ptr(first), stack.top_index()-first);
        !           520:                                        stack.set_top_index(first);
1.321     misha     521:                                }
                    522:                                WContext* result_wcontext=op_call(frame);
1.322     misha     523:                                if(opcode==OP::OP_CALL__WRITE) {
1.321     misha     524:                                        write_assign_lang(result_wcontext->result());
                    525:                                } else { // OP_CALL
                    526:                                        stack.push(result_wcontext->result().as_value());
1.237     paf       527:                                }
1.220     paf       528: 
1.157     parser    529: #ifdef DEBUG_EXECUTE
1.297     paf       530:                                debug_printf(sapi_info, "<-returned");
1.157     parser    531: #endif
1.297     paf       532:                                //is_debug_junction=false;
1.327     misha     533: 
                    534:                                if(get_skip())
                    535:                                        return;
                    536:                                if(get_interrupted()) {
                    537:                                        set_interrupted(false);
                    538:                                        throw Exception("parser.interrupted",
                    539:                                                0,
                    540:                                                "execution stopped");
                    541:                                }
1.49      paf       542:                                break;
                    543:                        }
                    544: 
1.55      paf       545:                // expression ops: unary
1.322     misha     546:                case OP::OP_NEG:
1.55      paf       547:                        {
1.297     paf       548:                                Value& a=stack.pop().value();
                    549:                                Value& value=*new VDouble(-a.as_double());
                    550:                                stack.push(value);
1.55      paf       551:                                break;
                    552:                        }
1.322     misha     553:                case OP::OP_INV:
1.55      paf       554:                        {
1.297     paf       555:                                Value& a=stack.pop().value();
                    556:                                Value& value=*new VDouble(~a.as_int());
                    557:                                stack.push(value);
1.55      paf       558:                                break;
                    559:                        }
1.322     misha     560:                case OP::OP_NOT:
1.55      paf       561:                        {
1.297     paf       562:                                Value& a=stack.pop().value();
1.327     misha     563:                                Value& value=VBool::get(!a.as_bool());
1.297     paf       564:                                stack.push(value);
1.55      paf       565:                                break;
                    566:                        }
1.322     misha     567:                case OP::OP_DEF:
1.62      paf       568:                        {
1.297     paf       569:                                Value& a=stack.pop().value();
1.327     misha     570:                                Value& value=VBool::get(a.is_defined());
1.297     paf       571:                                stack.push(value);
1.62      paf       572:                                break;
                    573:                        }
1.322     misha     574:                case OP::OP_IN:
1.62      paf       575:                        {
1.297     paf       576:                                Value& a=stack.pop().value();
                    577:                                const String& path=a.as_string();
1.327     misha     578:                                Value& value=VBool::get(request_info.uri && *request_info.uri && path.this_starts(request_info.uri));
1.297     paf       579:                                stack.push(value);
1.62      paf       580:                                break;
                    581:                        }
1.322     misha     582:                case OP::OP_FEXISTS:
1.62      paf       583:                        {
1.297     paf       584:                                Value& a=stack.pop().value();
1.327     misha     585:                                Value& value=VBool::get(file_exist(absolute(a.as_string())));
1.297     paf       586:                                stack.push(value);
1.148     paf       587:                                break;
                    588:                        }
1.322     misha     589:                case OP::OP_DEXISTS:
1.148     paf       590:                        {
1.297     paf       591:                                Value& a=stack.pop().value();
1.327     misha     592:                                Value& value=VBool::get(dir_exists(absolute(a.as_string())));
1.297     paf       593:                                stack.push(value);
1.62      paf       594:                                break;
                    595:                        }
1.55      paf       596: 
                    597:                // expression ops: binary
1.322     misha     598:                case OP::OP_SUB: 
1.53      paf       599:                        {
1.297     paf       600:                                Value& b=stack.pop().value();  Value& a=stack.pop().value();
                    601:                                Value& value=*new VDouble(a.as_double() - b.as_double());
                    602:                                stack.push(value);
1.53      paf       603:                                break;
                    604:                        }
1.322     misha     605:                case OP::OP_ADD: 
1.53      paf       606:                        {
1.297     paf       607:                                Value& b=stack.pop().value();  Value& a=stack.pop().value();
                    608:                                Value& value=*new VDouble(a.as_double() + b.as_double());
                    609:                                stack.push(value);
1.53      paf       610:                                break;
                    611:                        }
1.322     misha     612:                case OP::OP_MUL: 
1.49      paf       613:                        {
1.297     paf       614:                                Value& b=stack.pop().value();  Value& a=stack.pop().value();
                    615:                                Value& value=*new VDouble(a.as_double() * b.as_double());
                    616:                                stack.push(value);
1.53      paf       617:                                break;
                    618:                        }
1.322     misha     619:                case OP::OP_DIV: 
1.53      paf       620:                        {
1.297     paf       621:                                Value& b=stack.pop().value();  Value& a=stack.pop().value();
1.170     parser    622: 
1.297     paf       623:                                double a_double=a.as_double();
                    624:                                double b_double=b.as_double();
1.170     parser    625: 
1.171     parser    626:                                if(b_double == 0) {
1.297     paf       627:                                        //const String* problem_source=b.as_string();
1.223     paf       628:                                        throw Exception("number.zerodivision",
1.297     paf       629:                                                0, //problem_source,
1.170     parser    630:                                                "Division by zero");
1.171     parser    631:                                }
1.170     parser    632: 
1.297     paf       633:                                Value& value=*new VDouble(a_double / b_double);
                    634:                                stack.push(value);
1.54      paf       635:                                break;
                    636:                        }
1.322     misha     637:                case OP::OP_MOD: 
1.55      paf       638:                        {
1.297     paf       639:                                Value& b=stack.pop().value();  Value& a=stack.pop().value();
1.170     parser    640: 
1.297     paf       641:                                double a_double=a.as_double();
                    642:                                double b_double=b.as_double();
1.170     parser    643: 
1.173     parser    644:                                if(b_double == 0) {
1.297     paf       645:                                        //const String* problem_source=b.as_string();
1.223     paf       646:                                        throw Exception("number.zerodivision",
1.297     paf       647:                                                0, //problem_source,
1.170     parser    648:                                                "Modulus by zero");
1.171     parser    649:                                }
1.170     parser    650: 
1.297     paf       651:                                Value& value=*new VDouble(fmod(a_double, b_double));
                    652:                                stack.push(value);
1.201     paf       653:                                break;
                    654:                        }
1.322     misha     655:                case OP::OP_INTDIV:
1.201     paf       656:                        {
1.297     paf       657:                                Value& b=stack.pop().value();  Value& a=stack.pop().value();
1.201     paf       658: 
1.297     paf       659:                                int a_int=a.as_int();
                    660:                                int b_int=b.as_int();
1.201     paf       661: 
                    662:                                if(b_int == 0) {
1.297     paf       663:                                        //const String* problem_source=b.as_string();
1.223     paf       664:                                        throw Exception("number.zerodivision",
1.297     paf       665:                                                0, //problem_source,
1.201     paf       666:                                                "Division by zero");
                    667:                                }
                    668: 
1.297     paf       669:                                Value& value=*new VInt(a_int / b_int);
                    670:                                stack.push(value);
1.55      paf       671:                                break;
                    672:                        }
1.322     misha     673:                case OP::OP_BIN_SL:
1.274     paf       674:                        {
1.297     paf       675:                                Value& b=stack.pop().value();  Value& a=stack.pop().value();
                    676:                                Value& value=*new VInt(
                    677:                                        a.as_int() <<
                    678:                                        b.as_int());
                    679:                                stack.push(value);
1.274     paf       680:                                break;
                    681:                        }
1.322     misha     682:                case OP::OP_BIN_SR:
1.274     paf       683:                        {
1.297     paf       684:                                Value& b=stack.pop().value();  Value& a=stack.pop().value();
                    685:                                Value& value=*new VInt(
                    686:                                        a.as_int() >>
                    687:                                        b.as_int());
                    688:                                stack.push(value);
1.274     paf       689:                                break;
                    690:                        }
1.322     misha     691:                case OP::OP_BIN_AND:
1.54      paf       692:                        {
1.297     paf       693:                                Value& b=stack.pop().value();  Value& a=stack.pop().value();
                    694:                                Value& value=*new VInt(
                    695:                                        a.as_int() &
                    696:                                        b.as_int());
                    697:                                stack.push(value);
1.54      paf       698:                                break;
                    699:                        }
1.322     misha     700:                case OP::OP_BIN_OR:
1.54      paf       701:                        {
1.297     paf       702:                                Value& b=stack.pop().value();  Value& a=stack.pop().value();
                    703:                                Value& value=*new VInt(
                    704:                                        a.as_int() |
                    705:                                        b.as_int());
                    706:                                stack.push(value);
1.55      paf       707:                                break;
                    708:                        }
1.322     misha     709:                case OP::OP_BIN_XOR:
1.56      paf       710:                        {
1.297     paf       711:                                Value& b=stack.pop().value();  Value& a=stack.pop().value();
                    712:                                Value& value=*new VInt(
                    713:                                        a.as_int() ^
                    714:                                        b.as_int());
                    715:                                stack.push(value);
1.56      paf       716:                                break;
                    717:                        }
1.322     misha     718:                case OP::OP_LOG_AND:
1.55      paf       719:                        {
1.297     paf       720:                                ArrayOperation& local_ops=stack.pop().ops();  Value& a=stack.pop().value();
1.263     paf       721:                                bool result;
1.297     paf       722:                                if(a.as_bool()) {
                    723:                                        execute(local_ops);
                    724:                                        Value& b=stack.pop().value();
                    725:                                        result=b.as_bool();
1.209     paf       726:                                } else
1.263     paf       727:                                        result=false;
1.327     misha     728:                                Value& value=VBool::get(result);
1.297     paf       729:                                stack.push(value);
1.55      paf       730:                                break;
                    731:                        }
1.322     misha     732:                case OP::OP_LOG_OR:
1.55      paf       733:                        {
1.297     paf       734:                                ArrayOperation& local_ops=stack.pop().ops();  Value& a=stack.pop().value();
1.263     paf       735:                                bool result;
1.297     paf       736:                                if(a.as_bool()) 
1.263     paf       737:                                        result=true;
1.209     paf       738:                                else {
1.297     paf       739:                                        execute(local_ops);
                    740:                                        Value& b=stack.pop().value();
                    741:                                        result=b.as_bool();
1.209     paf       742:                                }
1.327     misha     743:                                Value& value=VBool::get(result);
1.297     paf       744:                                stack.push(value);
1.56      paf       745:                                break;
                    746:                        }
1.322     misha     747:                case OP::OP_LOG_XOR:
1.56      paf       748:                        {
1.297     paf       749:                                Value& b=stack.pop().value();  Value& a=stack.pop().value();
1.327     misha     750:                                Value& value=VBool::get(a.as_bool() ^ b.as_bool());
1.297     paf       751:                                stack.push(value);
1.55      paf       752:                                break;
                    753:                        }
1.322     misha     754:                case OP::OP_NUM_LT: 
1.55      paf       755:                        {
1.297     paf       756:                                volatile double b_double=stack.pop().value().as_double();
                    757:                                volatile double a_double=stack.pop().value().as_double();
1.327     misha     758:                                Value& value=VBool::get(a_double<b_double);
1.297     paf       759:                                stack.push(value);
1.55      paf       760:                                break;
                    761:                        }
1.322     misha     762:                case OP::OP_NUM_GT: 
1.55      paf       763:                        {
1.297     paf       764:                                volatile double b_double=stack.pop().value().as_double();
                    765:                                volatile double a_double=stack.pop().value().as_double();
1.327     misha     766:                                Value& value=VBool::get(a_double>b_double);
1.297     paf       767:                                stack.push(value);
1.55      paf       768:                                break;
                    769:                        }
1.322     misha     770:                case OP::OP_NUM_LE: 
1.55      paf       771:                        {
1.297     paf       772:                                volatile double b_double=stack.pop().value().as_double();
                    773:                                volatile double a_double=stack.pop().value().as_double();
1.327     misha     774:                                Value& value=VBool::get(a_double<=b_double);
1.297     paf       775:                                stack.push(value);
1.55      paf       776:                                break;
                    777:                        }
1.322     misha     778:                case OP::OP_NUM_GE: 
1.55      paf       779:                        {
1.297     paf       780:                                volatile double b_double=stack.pop().value().as_double();
                    781:                                volatile double a_double=stack.pop().value().as_double();
1.327     misha     782:                                Value& value=VBool::get(a_double>=b_double);
1.297     paf       783:                                stack.push(value);
1.55      paf       784:                                break;
                    785:                        }
1.322     misha     786:                case OP::OP_NUM_EQ: 
1.55      paf       787:                        {
1.297     paf       788:                                volatile double b_double=stack.pop().value().as_double();
                    789:                                volatile double a_double=stack.pop().value().as_double();
1.327     misha     790:                                Value& value=VBool::get(a_double==b_double);
1.297     paf       791:                                stack.push(value);
1.55      paf       792:                                break;
                    793:                        }
1.322     misha     794:                case OP::OP_NUM_NE: 
1.55      paf       795:                        {
1.297     paf       796:                                volatile double b_double=stack.pop().value().as_double();
                    797:                                volatile double a_double=stack.pop().value().as_double();
1.327     misha     798:                                Value& value=VBool::get(a_double!=b_double);
1.297     paf       799:                                stack.push(value);
1.54      paf       800:                                break;
                    801:                        }
1.322     misha     802:                case OP::OP_STR_LT: 
1.58      paf       803:                        {
1.297     paf       804:                                Value& b=stack.pop().value();  Value& a=stack.pop().value();
1.327     misha     805:                                Value& value=VBool::get(a.as_string() < b.as_string());
1.297     paf       806:                                stack.push(value);
1.58      paf       807:                                break;
                    808:                        }
1.322     misha     809:                case OP::OP_STR_GT: 
1.58      paf       810:                        {
1.297     paf       811:                                Value& b=stack.pop().value();  Value& a=stack.pop().value();
1.327     misha     812:                                Value& value=VBool::get(a.as_string() > b.as_string());
1.297     paf       813:                                stack.push(value);
1.58      paf       814:                                break;
                    815:                        }
1.322     misha     816:                case OP::OP_STR_LE: 
1.58      paf       817:                        {
1.297     paf       818:                                Value& b=stack.pop().value();  Value& a=stack.pop().value();
1.327     misha     819:                                Value& value=VBool::get(a.as_string() <= b.as_string());
1.297     paf       820:                                stack.push(value);
1.58      paf       821:                                break;
                    822:                        }
1.322     misha     823:                case OP::OP_STR_GE: 
1.54      paf       824:                        {
1.297     paf       825:                                Value& b=stack.pop().value();  Value& a=stack.pop().value();
1.327     misha     826:                                Value& value=VBool::get(a.as_string() >= b.as_string());
1.297     paf       827:                                stack.push(value);
1.58      paf       828:                                break;
                    829:                        }
1.322     misha     830:                case OP::OP_STR_EQ: 
1.58      paf       831:                        {
1.297     paf       832:                                Value& b=stack.pop().value();  Value& a=stack.pop().value();
1.327     misha     833:                                Value& value=VBool::get(a.as_string() == b.as_string());
1.297     paf       834:                                stack.push(value);
1.58      paf       835:                                break;
                    836:                        }
1.322     misha     837:                case OP::OP_STR_NE: 
1.58      paf       838:                        {
1.297     paf       839:                                Value& b=stack.pop().value();  Value& a=stack.pop().value();
1.327     misha     840:                                Value& value=VBool::get(a.as_string() != b.as_string());
1.297     paf       841:                                stack.push(value);
1.103     paf       842:                                break;
                    843:                        }
1.322     misha     844:                case OP::OP_IS:
1.103     paf       845:                        {
1.297     paf       846:                                Value& b=stack.pop().value();  Value& a=stack.pop().value();
1.327     misha     847:                                Value& value=VBool::get(a.is(b.as_string().cstr()));
1.297     paf       848:                                stack.push(value);
1.28      paf       849:                                break;
                    850:                        }
                    851: 
1.11      paf       852:                default:
1.223     paf       853:                        throw Exception(0,
1.67      paf       854:                                0,
1.297     paf       855:                                "invalid opcode %d", opcode); 
1.11      paf       856:                }
                    857:        }
1.306     paf       858:        } catch(const Exception&) {
1.297     paf       859:                // record it to stack trace
1.301     paf       860:                if(debug_name)
                    861:                        exception_trace.push(Trace(debug_name, debug_origin));
1.297     paf       862:                rethrow;
                    863:        }
1.1       paf       864: }
1.17      paf       865: 
1.319     misha     866: WContext* Request::op_call(VMethodFrame& frame){
                    867:        VMethodFrame *saved_method_frame=method_frame;
                    868:        Value* saved_rcontext=rcontext;
                    869:        WContext *saved_wcontext=wcontext;
                    870:                        
                    871:        VStateless_class& called_class=*frame.junction.self.get_class();
                    872:        Value* new_self;
                    873:        if(wcontext->get_constructing()) {
                    874:                wcontext->set_constructing(false);
                    875:                new_self=&get_self();
                    876:                if(frame.junction.method->call_type!=Method::CT_STATIC) {
                    877:                        // this is a constructor call
                    878:                        HashStringValue& new_object_fields=*new HashStringValue();
                    879:                        if(Value* value=called_class.create_new_value(fpool, new_object_fields)) {
                    880:                                // some stateless_class creatable derivates
                    881:                                new_self=value;
                    882:                        } else 
                    883:                                throw Exception(PARSER_RUNTIME,
                    884:                                        0, //&frame.name(),
                    885:                                        "is not a constructor, system class '%s' can be constructed only implicitly", 
                    886:                                                called_class.name().cstr());
                    887: 
                    888:                        frame.write(*new_self, 
                    889:                                String::L_CLEAN  // not used, always an object, not string
                    890:                        );
                    891:                } else
                    892:                        throw Exception(PARSER_RUNTIME,
                    893:                                0, //&frame.name(),
                    894:                                "method is static and can not be used as constructor");
                    895:        } else
                    896:                new_self=&frame.junction.self;
                    897: 
                    898:        frame.set_self(*new_self);
                    899: 
                    900:        // see OP_PREPARE_TO_EXPRESSION
                    901:        frame.set_in_expression(wcontext->get_in_expression());
                    902:                                
                    903:        rcontext=wcontext=&frame;
                    904:        {
                    905:                const Method& method=*frame.junction.method;
                    906:                Method::Call_type call_type=&called_class==new_self ? Method::CT_STATIC : Method::CT_DYNAMIC;
                    907:                if(
                    908:                        method.call_type==Method::CT_ANY ||
                    909:                        method.call_type==call_type) { // allowed call type?
                    910:                        method_frame=&frame;
                    911:                        if(method.native_code) { // native code?
                    912:                                method.check_actual_numbered_params(
                    913:                                        frame.junction.self, 
                    914:                                        /*frame.name(), */frame.numbered_params());
                    915:                                method.native_code(
                    916:                                        *this, 
                    917:                                        *frame.numbered_params()); // execute it
                    918:                        } else // parser code, execute it
                    919:                                recoursion_checked_execute(*method.parser_code);
                    920:                } else
                    921:                        throw Exception(PARSER_RUNTIME,
                    922:                                0, //&frame.name(),
                    923:                                "is not allowed to be called %s", 
                    924:                                        call_type==Method::CT_STATIC?"statically":"dynamically");
                    925:        }
                    926: 
                    927:        WContext *result_wcontext=wcontext;
                    928:        // StringOrValue result=wcontext->result();
                    929: 
                    930:        wcontext=saved_wcontext;
                    931:        rcontext=saved_rcontext;
                    932:        method_frame=saved_method_frame;
                    933: 
                    934:        return result_wcontext;
                    935: }
                    936: 
                    937: 
1.292     paf       938: /**
                    939:        @todo cache|prepare junctions
                    940:        @bug ^superbase:method would dynamically call ^base:method if there is any
                    941: */
1.297     paf       942: Value& Request::get_element(Value& ncontext, const String& name, bool can_call_operator) {
                    943:        Value* value=0;
1.249     paf       944:        if(can_call_operator) {
1.325     misha     945:                if(Method* method=main_class.get_method(name)){ // looking operator of that name FIRST
                    946:                        if(!method->junction_template)
                    947:                                method->junction_template=new VJunction(main_class, method);
                    948:                        return *method->junction_template;
                    949:                }
1.249     paf       950:        }
                    951:        if(!value) {
                    952:                if(!wcontext->get_constructing() // not constructing
                    953:                        && wcontext->get_somebody_entered_some_class() ) // ^class:method
1.297     paf       954:                        if(VStateless_class *called_class=ncontext.get_class())
1.249     paf       955:                                if(VStateless_class *read_class=rcontext->get_class())
                    956:                                        if(read_class->derived_from(*called_class)) // current derived from called
1.297     paf       957:                                                if(Value* base=get_self().base()) { // doing DYNAMIC call
1.290     paf       958:                                                        Temp_derived temp_derived(*base, 0); // temporarily prevent go-back-down virtual calls
1.320     misha     959:                                                        value=base->get_element(name, *base, true); // virtual-up lookup starting from parent
1.289     paf       960:                                                        goto value_ready;
1.249     paf       961:                                                }
                    962:        }
1.216     paf       963:        if(!value)
1.320     misha     964:                value=ncontext.get_element(name, ncontext, true);
1.291     paf       965: 
                    966:        if(value && wcontext->get_constructing())
1.297     paf       967:                if(Junction* junction=value->get_junction()) {
                    968:                        if(junction->self.get_class()!=&ncontext)
1.316     misha     969:                                throw Exception(PARSER_RUNTIME,
1.291     paf       970:                                        &name,
1.297     paf       971:                                        "constructor must be declared in class '%s'", 
                    972:                                                ncontext.get_class()->name_cstr());
1.291     paf       973:                }
1.249     paf       974: 
1.289     paf       975: value_ready:
1.284     paf       976:        if(value)
1.294     paf       977:                value=&process_to_value(*value); // process possible code-junction
1.284     paf       978:        else
1.324     misha     979:                value=VVoid::get();
1.63      paf       980: 
1.297     paf       981:        return *value;
1.309     paf       982: }
                    983: 
1.329   ! misha     984: void Request::put_element(Value& ncontext, const String& name, Value* value) {
1.309     paf       985:        // put_element can return property-setting-junction
1.329   ! misha     986:        if(const VJunction* vjunction=ncontext.put_element(ncontext, name, value, false))
1.312     paf       987:                if(vjunction!=PUT_ELEMENT_REPLACED_ELEMENT) {
1.309     paf       988:                        // process it
1.329   ! misha     989:                        VMethodFrame frame(vjunction->junction(), method_frame/*caller*/);
        !           990:                        int param_count=frame.method_params_count();
        !           991: 
1.309     paf       992:                        if(param_count!=1)
1.316     misha     993:                                throw Exception(PARSER_RUNTIME,
1.309     paf       994:                                        0,
                    995:                                        "setter method must have ONE parameter (has %d parameters)", param_count);
                    996: 
1.329   ! misha     997:                        frame.store_params(&value, 1);
1.309     paf       998:                        frame.set_self(frame.junction.self);
                    999: 
                   1000:                        VMethodFrame *saved_method_frame=method_frame;
                   1001:                        Value* saved_rcontext=rcontext;
                   1002:                        WContext *saved_wcontext=wcontext;
                   1003: 
                   1004:                        rcontext=wcontext=&frame;
                   1005:                        method_frame=&frame;
                   1006: 
1.311     paf      1007:                        // prevent non-string writes for better error reporting [setters are not expected to return anything]
1.309     paf      1008:                        wcontext->write(*method_frame);
                   1009: 
                   1010:                        recoursion_checked_execute(*frame.junction.method->parser_code); // parser code, execute it
                   1011:                        // we don't need it StringOrValue result=wcontext->result();
                   1012: 
                   1013:                        method_frame=saved_method_frame;
                   1014:                        wcontext=saved_wcontext;
                   1015:                        rcontext=saved_rcontext;
                   1016:                }
1.34      paf      1017: }
1.70      paf      1018: 
1.162     parser   1019: /**    @param intercept_string
1.116     paf      1020:        - true:
                   1021:                they want result=string value, 
                   1022:                possible object result goes to wcontext
                   1023:        - false:
                   1024:                they want any result[string|object]
                   1025:                nothing goes to wcontext.
1.125     paf      1026:                used in @c (expression) params evaluation
1.224     paf      1027: 
                   1028:     using the fact it's either string_ or value_ result requested to speed up checkes
1.116     paf      1029: */
1.229     paf      1030: StringOrValue Request::process(Value& input_value, bool intercept_string) {
1.297     paf      1031:        Junction* junction=input_value.get_junction();
1.307     paf      1032:        if(junction) {
                   1033:                if(junction->is_getter) { // is it a getter-junction?
1.329   ! misha    1034:                        VMethodFrame frame(*junction, method_frame/*caller*/);
        !          1035:                        int param_count=frame.method_params_count();
        !          1036: 
1.320     misha    1037:                        if(junction->auto_name){ // default getter
                   1038:                                if(param_count>1)
                   1039:                                        throw Exception(PARSER_RUNTIME,
                   1040:                                                0,
                   1041:                                                "default getter method can't have more then 1 parameter (has %d parameters)", param_count);
                   1042:                        } else if(param_count)
1.316     misha    1043:                                throw Exception(PARSER_RUNTIME,
1.307     paf      1044:                                        0,
1.320     misha    1045:                                        "getter method must have no parameters (has %d parameters)", param_count);
1.307     paf      1046: 
1.329   ! misha    1047:                        if(junction->auto_name && param_count){
        !          1048:                                Value *param=new VString(*junction->auto_name);
        !          1049:                                frame.store_params(&param, 1);
        !          1050:                        }
1.320     misha    1051: 
1.307     paf      1052:                        frame.set_self(frame.junction.self);
                   1053: 
                   1054:                        VMethodFrame *saved_method_frame=method_frame;
                   1055:                        Value* saved_rcontext=rcontext;
                   1056:                        WContext *saved_wcontext=wcontext;
                   1057: 
                   1058:                        rcontext=wcontext=&frame;
                   1059:                        method_frame=&frame;
                   1060: 
                   1061:                        recoursion_checked_execute(*frame.junction.method->parser_code); // parser code, execute it
                   1062:                        StringOrValue result=wcontext->result();
                   1063: 
                   1064:                        method_frame=saved_method_frame;
                   1065:                        wcontext=saved_wcontext;
                   1066:                        rcontext=saved_rcontext;
                   1067: 
                   1068:                        return result;
                   1069:                }
                   1070: 
                   1071:                if(junction->code) { // is it a code-junction?
                   1072:                        // process it
                   1073:                        StringOrValue result;
1.157     parser   1074: #ifdef DEBUG_EXECUTE
1.307     paf      1075:                        debug_printf(sapi_info, "ja->\n");
1.157     parser   1076: #endif
1.238     paf      1077: 
1.307     paf      1078:                        if(!junction->method_frame)
1.316     misha    1079:                                throw Exception(PARSER_RUNTIME,
1.240     paf      1080:                                0,
                   1081:                                "junction used outside of context");
                   1082: 
1.307     paf      1083:                        VMethodFrame *saved_method_frame=method_frame;  
                   1084:                        Value* saved_rcontext=rcontext;  
                   1085:                        WContext *saved_wcontext=wcontext;
                   1086: 
                   1087:                        method_frame=junction->method_frame;
                   1088:                        rcontext=junction->rcontext;
                   1089: 
                   1090:                        // for expression method params
                   1091:                        // wcontext is set 0
                   1092:                        // using the fact in decision "which wwrapper to use"
                   1093:                        bool using_code_frame=intercept_string && junction->wcontext;
                   1094:                        if(using_code_frame) {
1.318     misha    1095:                                // almost plain wwrapper about junction wcontext 
                   1096: 
                   1097:                                VCodeFrame local(*junction->wcontext);
1.307     paf      1098:                                wcontext=&local;
                   1099: 
                   1100:                                // execute it
                   1101:                                recoursion_checked_execute(*junction->code);
                   1102: 
                   1103:                                // CodeFrame soul:
1.318     misha    1104:                                result=wcontext->result();
1.307     paf      1105:                        } else {
                   1106:                                // plain wwrapper
                   1107:                                WWrapper local(0/*empty*/, wcontext);
                   1108:                                wcontext=&local;
                   1109: 
                   1110:                                // execute it
                   1111:                                recoursion_checked_execute(*junction->code);
                   1112: 
                   1113:                                result=wcontext->result();
                   1114:                        }
                   1115: 
                   1116:                        wcontext=saved_wcontext;
                   1117:                        rcontext=saved_rcontext;
                   1118:                        method_frame=saved_method_frame;
1.207     paf      1119: 
1.157     parser   1120: #ifdef DEBUG_EXECUTE
1.307     paf      1121:                        debug_printf(sapi_info, "<-ja returned");
1.157     parser   1122: #endif
1.307     paf      1123:                        return result;
                   1124:                }
                   1125: 
                   1126:                // it is then method-junction, do not explode it
                   1127:                // just return it as we do for usual objects
                   1128:        }       
                   1129: 
                   1130:        return input_value;
1.85      paf      1131: }
                   1132: 
1.298     paf      1133: StringOrValue Request::execute_method(VMethodFrame& amethod_frame, const Method& method) {
1.257     paf      1134:        VMethodFrame *saved_method_frame=method_frame;  
1.297     paf      1135:        Value* saved_rcontext=rcontext;  
1.257     paf      1136:        WContext *saved_wcontext=wcontext;
1.99      paf      1137:        
                   1138:        // initialize contexts
1.286     paf      1139:        rcontext=wcontext=method_frame=&amethod_frame;
1.99      paf      1140:        
                   1141:        // execute!     
                   1142:        execute(*method.parser_code);
                   1143:        
                   1144:        // result
1.298     paf      1145:        StringOrValue result=wcontext->result();
1.208     paf      1146:        
1.257     paf      1147:        wcontext=saved_wcontext;
                   1148:        rcontext=saved_rcontext;
                   1149:        method_frame=saved_method_frame;
1.242     paf      1150:        
                   1151:        // return
                   1152:        return result;
1.208     paf      1153: }
                   1154: 
1.297     paf      1155: const String* Request::execute_method(Value& aself, 
1.329   ! misha    1156:                                        const Method& method, Value* optional_param,
        !          1157:                                        bool do_return_string) {
1.317     misha    1158: 
1.257     paf      1159:        VMethodFrame *saved_method_frame=method_frame;  
1.297     paf      1160:        Value* saved_rcontext=rcontext;  
1.257     paf      1161:        WContext *saved_wcontext=wcontext;
1.208     paf      1162:        
1.307     paf      1163:        Junction local_junction(aself, &method);
1.321     misha    1164:        VMethodFrame local_frame(local_junction, method_frame/*caller*/);
1.329   ! misha    1165:        if(optional_param && local_frame.method_params_count()>0) {
        !          1166:                local_frame.store_params(&optional_param, 1);
1.242     paf      1167:        }
1.285     paf      1168:        local_frame.set_self(aself);
1.257     paf      1169:        rcontext=wcontext=method_frame=&local_frame; 
1.246     paf      1170: 
                   1171:        // prevent non-string writes for better error reporting
1.297     paf      1172:        if(do_return_string)
1.246     paf      1173:                wcontext->write(local_frame);
1.208     paf      1174:        
                   1175:        // execute!     
                   1176:        execute(*method.parser_code);
                   1177:        
                   1178:        // result
1.297     paf      1179:        const String* result=0;
                   1180:        if(do_return_string)
                   1181:                result=&wcontext->result().as_string();
1.99      paf      1182:        
1.257     paf      1183:        wcontext=saved_wcontext;
                   1184:        rcontext=saved_rcontext;
                   1185:        method_frame=saved_method_frame;
1.297     paf      1186: 
                   1187:        return result;
1.242     paf      1188: }
                   1189: 
1.297     paf      1190: Request::Execute_nonvirtual_method_result 
                   1191: Request::execute_nonvirtual_method(VStateless_class& aclass, 
1.317     misha    1192:                                   const String& method_name,
                   1193:                                   VString* optional_param,
1.297     paf      1194:                                   bool do_return_string) {
                   1195:        Execute_nonvirtual_method_result result;
                   1196:        result.method=aclass.get_method(method_name);
                   1197:        if(result.method)
1.317     misha    1198:                result.string=execute_method(aclass, *result.method, optional_param, do_return_string);
1.297     paf      1199:        return result;
1.99      paf      1200: }
                   1201: 
1.297     paf      1202: const String* Request::execute_virtual_method(Value& aself, 
                   1203:                                              const String& method_name) {
                   1204:        if(Value* value=aself.get_element(method_name, aself, false))
                   1205:                if(Junction* junction=value->get_junction())
                   1206:                        if(const Method *method=junction->method) 
                   1207:                                return execute_method(aself, *method, 0/*no params*/, true);
1.188     parser   1208:                        
1.176     parser   1209:        return 0;
                   1210: }

E-mail: