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

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.332   ! misha       8: static const char * const IDENT_EXECUTE_C="$Date: 2009-05-01 11:41:14 $";
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
1.330     misha     322:                                if(Method* method=main_class.get_method(name)){ // looking operator of that name FIRST
                    323:                                        if(!method->junction_template) method->junction_template=new VJunction(main_class, method);
                    324:                                        stack.push(*method->junction_template);
                    325:                                        break;
                    326:                                }
                    327:                                Value& value=get_element(*rcontext, name);
1.328     misha     328:                                stack.push(value);
                    329:                                break;
                    330:                        }
                    331: #else
1.322     misha     332:                case OP::OP_GET_ELEMENT_OR_OPERATOR:
1.215     paf       333:                        {
1.297     paf       334:                                const String& name=stack.pop().string();  debug_name=&name;
                    335:                                Value& ncontext=stack.pop().value();
1.330     misha     336:                                if(Method* method=main_class.get_method(name)){ // looking operator of that name FIRST
                    337:                                        if(!method->junction_template) method->junction_template=new VJunction(main_class, method);
                    338:                                        stack.push(*method->junction_template);
                    339:                                        break;
                    340:                                }
                    341:                                Value& value=get_element(ncontext, name);
1.297     paf       342:                                stack.push(value);
1.215     paf       343:                                break;
                    344:                        }
1.328     misha     345: #endif
                    346: 
1.322     misha     347:                case OP::OP_GET_ELEMENT:
1.11      paf       348:                        {
1.297     paf       349:                                const String& name=stack.pop().string();  debug_name=&name;
                    350:                                Value& ncontext=stack.pop().value();
1.330     misha     351:                                Value& value=get_element(ncontext, name);
1.297     paf       352:                                stack.push(value);
1.17      paf       353:                                break;
                    354:                        }
                    355: 
1.328     misha     356: #ifdef OPTIMIZE_BYTECODE_GET_ELEMENT
                    357:                case OP::OP_VALUE__GET_ELEMENT:
                    358:                        {
                    359:                                debug_origin=i.next().origin;
                    360:                                const String& name=*i.next().value->get_string(); debug_name=&name;
                    361: 
                    362: #ifdef DEBUG_EXECUTE
                    363:                                debug_printf(sapi_info, " \"%s\" ", name.cstr());
                    364: #endif
                    365: 
1.330     misha     366:                                Value& value=get_element(*rcontext, name);
1.328     misha     367:                                stack.push(value);
                    368:                                break;
                    369:                        }
                    370: #endif
                    371: 
                    372: 
1.322     misha     373:                case OP::OP_GET_ELEMENT__WRITE:
1.17      paf       374:                        {
1.297     paf       375:                                const String& name=stack.pop().string();  debug_name=&name;
                    376:                                Value& ncontext=stack.pop().value();
1.330     misha     377:                                Value& value=get_element(ncontext, name);
1.297     paf       378:                                write_assign_lang(value);
1.17      paf       379:                                break;
                    380:                        }
                    381: 
1.328     misha     382: #ifdef OPTIMIZE_BYTECODE_GET_ELEMENT
                    383:                case OP::OP_VALUE__GET_ELEMENT__WRITE:
                    384:                        {
                    385:                                debug_origin=i.next().origin;
                    386:                                const String& name=*i.next().value->get_string(); debug_name=&name;
                    387: 
                    388: #ifdef DEBUG_EXECUTE
                    389:                                debug_printf(sapi_info, " \"%s\" ", name.cstr());
                    390: #endif
                    391: 
1.330     misha     392:                                Value& value=get_element(*rcontext, name);
1.328     misha     393:                                write_assign_lang(value);
                    394:                                break;
                    395:                        }
                    396: #endif
1.32      paf       397: 
1.322     misha     398:                case OP::OP_OBJECT_POOL:
1.17      paf       399:                        {
1.297     paf       400:                                ArrayOperation& local_ops=*i.next().ops;
1.226     paf       401:                                
1.257     paf       402:                                WContext *saved_wcontext=wcontext;
1.297     paf       403:                                String::Language saved_lang=flang;
                    404:                                flang=String::L_PASS_APPENDED;
                    405:                                WWrapper local(0/*empty*/, wcontext);
1.226     paf       406:                                wcontext=&local;
                    407: 
1.297     paf       408:                                execute(local_ops);
1.226     paf       409: 
1.297     paf       410:                                stack.push(wcontext->result().as_value());
1.257     paf       411:                                flang=saved_lang;
                    412:                                wcontext=saved_wcontext;
1.13      paf       413:                                break;
1.15      paf       414:                        }
1.13      paf       415:                        
1.322     misha     416:                case OP::OP_STRING_POOL:
1.49      paf       417:                        {
1.297     paf       418:                                ArrayOperation& local_ops=*i.next().ops;
1.226     paf       419: 
1.257     paf       420:                                WContext *saved_wcontext=wcontext;
1.297     paf       421:                                WWrapper local(0 /*empty*/, wcontext);
1.226     paf       422:                                wcontext=&local;
                    423: 
1.297     paf       424:                                execute(local_ops);
1.226     paf       425: 
1.297     paf       426:                                Value* value;
1.49      paf       427:                                // from "$a $b" part of expression taking only string value,
                    428:                                // ignoring any other content of wcontext
1.297     paf       429:                                if(const String* string=wcontext->get_string())
                    430:                                        value=new VString(*string);
1.80      paf       431:                                else
1.324     misha     432:                                        value=VVoid::get();
1.297     paf       433:                                stack.push(*value);
                    434: 
1.257     paf       435:                                wcontext=saved_wcontext;
1.49      paf       436:                                break;
                    437:                        }
                    438: 
1.51      paf       439:                // CALL
1.322     misha     440:                case OP::OP_CURLY_CODE__STORE_PARAM:
                    441:                case OP::OP_EXPR_CODE__STORE_PARAM:
1.28      paf       442:                        {
1.237     paf       443:                                // code
1.297     paf       444:                                ArrayOperation& local_ops=*i.next().ops;
1.237     paf       445: #ifdef DEBUG_EXECUTE
1.297     paf       446:                                debug_printf(sapi_info, " (%d)\n", local_ops.count());
                    447:                                debug_dump(sapi_info, 1, local_ops);
1.237     paf       448: #endif                         
                    449:                                // when they evaluate expression parameter,
                    450:                                // the object expression result
                    451:                                // does not need to be written into calling frame
                    452:                                // it must go into any expressions using that parameter
                    453:                                // hence, we zero junction.wcontext here, and later
                    454:                                // in .process we would test that field 
                    455:                                // in decision "which wwrapper to use"
1.326     misha     456:                                VJunction& value=*new VJunction(
1.297     paf       457:                                        get_self(), 0,
1.257     paf       458:                                        method_frame, 
1.237     paf       459:                                        rcontext, 
1.322     misha     460:                                        opcode==OP::OP_EXPR_CODE__STORE_PARAM?0:wcontext, 
1.312     paf       461:                                        &local_ops);
1.326     misha     462: #ifdef USE_DESTRUCTORS
                    463:                                value.set_temporal(true);
                    464: #else
                    465:                                if (opcode!=OP::OP_EXPR_CODE__STORE_PARAM)
                    466: #endif
                    467:                                wcontext->attach_junction(&value);
1.237     paf       468:                                // store param
1.329     misha     469:                                stack.push(value);
1.29      paf       470:                                break;
                    471:                        }
                    472: 
1.322     misha     473:                case OP::OP_PREPARE_TO_CONSTRUCT_OBJECT:
1.186     parser    474:                        {
1.200     paf       475:                                wcontext->set_constructing(true);
1.186     parser    476:                                break;
                    477:                        }
1.219     paf       478: 
1.322     misha     479:                case OP::OP_PREPARE_TO_EXPRESSION:
1.219     paf       480:                        {
                    481:                                wcontext->set_in_expression(true);
                    482:                                break;
                    483:                        }
                    484: 
1.322     misha     485:                case OP::OP_CALL:
1.29      paf       486:                        {
1.297     paf       487:                                ArrayOperation* local_ops=i.next().ops;
1.157     parser    488: #ifdef DEBUG_EXECUTE
1.297     paf       489:                                debug_printf(sapi_info, " (%d)\n", local_ops?local_ops->count():0);
                    490:                                if(local_ops)
                    491:                                        debug_dump(sapi_info, 1, *local_ops);
1.237     paf       492: 
1.297     paf       493:                                debug_printf(sapi_info, "->\n");
1.157     parser    494: #endif
1.297     paf       495:                                Value& value=stack.pop().value();
1.237     paf       496: 
1.297     paf       497:                                Junction* junction=value.get_junction();
                    498:                                if(!junction) {
                    499:                                        if(value.is("void"))
1.316     misha     500:                                                throw Exception(PARSER_RUNTIME,
1.297     paf       501:                                                        0,
                    502:                                                        "undefined method");
                    503:                                        else
1.316     misha     504:                                                throw Exception(PARSER_RUNTIME,
1.297     paf       505:                                                        0,
                    506:                                                        "is '%s', not a method or junction, can not call it",
                    507:                                                                value.type());
                    508:                                }
1.237     paf       509: 
1.321     misha     510:                                VMethodFrame frame(*junction, method_frame);
1.330     misha     511: 
1.321     misha     512:                                if(local_ops){ // store param code goes here
1.329     misha     513:                                        size_t first = stack.top_index();
1.321     misha     514:                                        execute(*local_ops);
1.329     misha     515:                                        frame.store_params((Value**)stack.ptr(first), stack.top_index()-first);
1.332   ! misha     516:                                        op_call(frame);
1.329     misha     517:                                        stack.set_top_index(first);
1.330     misha     518:                                } else {
                    519:                                        frame.empty_params();
1.332   ! misha     520:                                        op_call(frame);
1.321     misha     521:                                }
1.330     misha     522: 
1.332   ! misha     523:                                stack.push(frame.result().as_value());
        !           524: 
        !           525: #ifdef DEBUG_EXECUTE
        !           526:                                debug_printf(sapi_info, "<-returned");
        !           527: #endif
        !           528:                                if(get_skip())
        !           529:                                        return;
        !           530:                                if(get_interrupted()) {
        !           531:                                        set_interrupted(false);
        !           532:                                        throw Exception("parser.interrupted",
        !           533:                                                0,
        !           534:                                                "execution stopped");
1.237     paf       535:                                }
1.332   ! misha     536:                                break;
        !           537:                        }
1.220     paf       538: 
1.332   ! misha     539:                case OP::OP_CALL__WRITE:
        !           540:                        {
        !           541:                                ArrayOperation* local_ops=i.next().ops;
        !           542: #ifdef DEBUG_EXECUTE
        !           543:                                debug_printf(sapi_info, " (%d)\n", local_ops?local_ops->count():0);
        !           544:                                if(local_ops)
        !           545:                                        debug_dump(sapi_info, 1, *local_ops);
        !           546: 
        !           547:                                debug_printf(sapi_info, "->\n");
        !           548: #endif
        !           549:                                Value& value=stack.pop().value();
        !           550: 
        !           551:                                Junction* junction=value.get_junction();
        !           552:                                if(!junction) {
        !           553:                                        if(value.is("void"))
        !           554:                                                throw Exception(PARSER_RUNTIME,
        !           555:                                                        0,
        !           556:                                                        "undefined method");
        !           557:                                        else
        !           558:                                                throw Exception(PARSER_RUNTIME,
        !           559:                                                        0,
        !           560:                                                        "is '%s', not a method or junction, can not call it",
        !           561:                                                        value.type());
        !           562:                                }
        !           563: 
        !           564: #ifdef OPTIMIZE_CALL
        !           565:                                const Method& method=*junction->method;
        !           566:                                if (!wcontext->get_constructing() && method.call_optimization==Method::CO_WITHOUT_FRAME){
        !           567:                                        if(local_ops){ // store param code goes here
        !           568:                                                size_t first = stack.top_index();
        !           569:                                                execute(*local_ops);
        !           570: 
        !           571:                                                MethodParams method_params;
        !           572:                                                method_params.store_params((Value**)stack.ptr(first), stack.top_index()-first);
        !           573:                                                method.check_actual_numbered_params(junction->self, &method_params);
        !           574:                                                method.native_code(*this, method_params); // execute it
        !           575: 
        !           576:                                                stack.set_top_index(first);
        !           577:                                        } else {
        !           578:                                                MethodParams method_params;
        !           579:                                                method.check_actual_numbered_params(junction->self, &method_params);
        !           580:                                                method.native_code(*this, method_params); // execute it
        !           581:                                        }
        !           582:                                } else if (!wcontext->get_constructing() && method.call_optimization==Method::CO_WITHOUT_WCONTEXT){
        !           583:                                        VMethodFrame frame(*junction, method_frame);
        !           584:                                        if(local_ops){ // store param code goes here
        !           585:                                                size_t first = stack.top_index();
        !           586:                                                execute(*local_ops);
        !           587: 
        !           588:                                                frame.store_params((Value**)stack.ptr(first), stack.top_index()-first);
        !           589:                                                op_call_write(frame);
        !           590: 
        !           591:                                                stack.set_top_index(first);
        !           592:                                        } else {
        !           593:                                                frame.empty_params();
        !           594:                                                op_call_write(frame);
        !           595:                                        }
        !           596:                                } else 
        !           597: #endif // OPTIMIZE_CALL
        !           598:                                {
        !           599:                                        VMethodFrame frame(*junction, method_frame);
        !           600:                                        if(local_ops){ // store param code goes here
        !           601:                                                size_t first = stack.top_index();
        !           602:                                                execute(*local_ops);
        !           603: 
        !           604:                                                frame.store_params((Value**)stack.ptr(first), stack.top_index()-first);
        !           605:                                                op_call(frame);
        !           606: 
        !           607:                                                stack.set_top_index(first);
        !           608:                                        } else {
        !           609:                                                frame.empty_params();
        !           610:                                                op_call(frame);
        !           611:                                        }
        !           612:                                        write_assign_lang(frame.result());
        !           613:                                }
1.157     parser    614: #ifdef DEBUG_EXECUTE
1.297     paf       615:                                debug_printf(sapi_info, "<-returned");
1.157     parser    616: #endif
1.327     misha     617: 
                    618:                                if(get_skip())
                    619:                                        return;
                    620:                                if(get_interrupted()) {
                    621:                                        set_interrupted(false);
                    622:                                        throw Exception("parser.interrupted",
                    623:                                                0,
                    624:                                                "execution stopped");
                    625:                                }
1.49      paf       626:                                break;
                    627:                        }
                    628: 
1.55      paf       629:                // expression ops: unary
1.322     misha     630:                case OP::OP_NEG:
1.55      paf       631:                        {
1.297     paf       632:                                Value& a=stack.pop().value();
                    633:                                Value& value=*new VDouble(-a.as_double());
                    634:                                stack.push(value);
1.55      paf       635:                                break;
                    636:                        }
1.322     misha     637:                case OP::OP_INV:
1.55      paf       638:                        {
1.297     paf       639:                                Value& a=stack.pop().value();
                    640:                                Value& value=*new VDouble(~a.as_int());
                    641:                                stack.push(value);
1.55      paf       642:                                break;
                    643:                        }
1.322     misha     644:                case OP::OP_NOT:
1.55      paf       645:                        {
1.297     paf       646:                                Value& a=stack.pop().value();
1.327     misha     647:                                Value& value=VBool::get(!a.as_bool());
1.297     paf       648:                                stack.push(value);
1.55      paf       649:                                break;
                    650:                        }
1.322     misha     651:                case OP::OP_DEF:
1.62      paf       652:                        {
1.297     paf       653:                                Value& a=stack.pop().value();
1.327     misha     654:                                Value& value=VBool::get(a.is_defined());
1.297     paf       655:                                stack.push(value);
1.62      paf       656:                                break;
                    657:                        }
1.322     misha     658:                case OP::OP_IN:
1.62      paf       659:                        {
1.297     paf       660:                                Value& a=stack.pop().value();
                    661:                                const String& path=a.as_string();
1.327     misha     662:                                Value& value=VBool::get(request_info.uri && *request_info.uri && path.this_starts(request_info.uri));
1.297     paf       663:                                stack.push(value);
1.62      paf       664:                                break;
                    665:                        }
1.322     misha     666:                case OP::OP_FEXISTS:
1.62      paf       667:                        {
1.297     paf       668:                                Value& a=stack.pop().value();
1.327     misha     669:                                Value& value=VBool::get(file_exist(absolute(a.as_string())));
1.297     paf       670:                                stack.push(value);
1.148     paf       671:                                break;
                    672:                        }
1.322     misha     673:                case OP::OP_DEXISTS:
1.148     paf       674:                        {
1.297     paf       675:                                Value& a=stack.pop().value();
1.327     misha     676:                                Value& value=VBool::get(dir_exists(absolute(a.as_string())));
1.297     paf       677:                                stack.push(value);
1.62      paf       678:                                break;
                    679:                        }
1.55      paf       680: 
                    681:                // expression ops: binary
1.322     misha     682:                case OP::OP_SUB: 
1.53      paf       683:                        {
1.297     paf       684:                                Value& b=stack.pop().value();  Value& a=stack.pop().value();
                    685:                                Value& value=*new VDouble(a.as_double() - b.as_double());
                    686:                                stack.push(value);
1.53      paf       687:                                break;
                    688:                        }
1.322     misha     689:                case OP::OP_ADD: 
1.53      paf       690:                        {
1.297     paf       691:                                Value& b=stack.pop().value();  Value& a=stack.pop().value();
                    692:                                Value& value=*new VDouble(a.as_double() + b.as_double());
                    693:                                stack.push(value);
1.53      paf       694:                                break;
                    695:                        }
1.322     misha     696:                case OP::OP_MUL: 
1.49      paf       697:                        {
1.297     paf       698:                                Value& b=stack.pop().value();  Value& a=stack.pop().value();
                    699:                                Value& value=*new VDouble(a.as_double() * b.as_double());
                    700:                                stack.push(value);
1.53      paf       701:                                break;
                    702:                        }
1.322     misha     703:                case OP::OP_DIV: 
1.53      paf       704:                        {
1.297     paf       705:                                Value& b=stack.pop().value();  Value& a=stack.pop().value();
1.170     parser    706: 
1.297     paf       707:                                double a_double=a.as_double();
                    708:                                double b_double=b.as_double();
1.170     parser    709: 
1.171     parser    710:                                if(b_double == 0) {
1.297     paf       711:                                        //const String* problem_source=b.as_string();
1.223     paf       712:                                        throw Exception("number.zerodivision",
1.297     paf       713:                                                0, //problem_source,
1.170     parser    714:                                                "Division by zero");
1.171     parser    715:                                }
1.170     parser    716: 
1.297     paf       717:                                Value& value=*new VDouble(a_double / b_double);
                    718:                                stack.push(value);
1.54      paf       719:                                break;
                    720:                        }
1.322     misha     721:                case OP::OP_MOD: 
1.55      paf       722:                        {
1.297     paf       723:                                Value& b=stack.pop().value();  Value& a=stack.pop().value();
1.170     parser    724: 
1.297     paf       725:                                double a_double=a.as_double();
                    726:                                double b_double=b.as_double();
1.170     parser    727: 
1.173     parser    728:                                if(b_double == 0) {
1.297     paf       729:                                        //const String* problem_source=b.as_string();
1.223     paf       730:                                        throw Exception("number.zerodivision",
1.297     paf       731:                                                0, //problem_source,
1.170     parser    732:                                                "Modulus by zero");
1.171     parser    733:                                }
1.170     parser    734: 
1.297     paf       735:                                Value& value=*new VDouble(fmod(a_double, b_double));
                    736:                                stack.push(value);
1.201     paf       737:                                break;
                    738:                        }
1.322     misha     739:                case OP::OP_INTDIV:
1.201     paf       740:                        {
1.297     paf       741:                                Value& b=stack.pop().value();  Value& a=stack.pop().value();
1.201     paf       742: 
1.297     paf       743:                                int a_int=a.as_int();
                    744:                                int b_int=b.as_int();
1.201     paf       745: 
                    746:                                if(b_int == 0) {
1.297     paf       747:                                        //const String* problem_source=b.as_string();
1.223     paf       748:                                        throw Exception("number.zerodivision",
1.297     paf       749:                                                0, //problem_source,
1.201     paf       750:                                                "Division by zero");
                    751:                                }
                    752: 
1.297     paf       753:                                Value& value=*new VInt(a_int / b_int);
                    754:                                stack.push(value);
1.55      paf       755:                                break;
                    756:                        }
1.322     misha     757:                case OP::OP_BIN_SL:
1.274     paf       758:                        {
1.297     paf       759:                                Value& b=stack.pop().value();  Value& a=stack.pop().value();
                    760:                                Value& value=*new VInt(
                    761:                                        a.as_int() <<
                    762:                                        b.as_int());
                    763:                                stack.push(value);
1.274     paf       764:                                break;
                    765:                        }
1.322     misha     766:                case OP::OP_BIN_SR:
1.274     paf       767:                        {
1.297     paf       768:                                Value& b=stack.pop().value();  Value& a=stack.pop().value();
                    769:                                Value& value=*new VInt(
                    770:                                        a.as_int() >>
                    771:                                        b.as_int());
                    772:                                stack.push(value);
1.274     paf       773:                                break;
                    774:                        }
1.322     misha     775:                case OP::OP_BIN_AND:
1.54      paf       776:                        {
1.297     paf       777:                                Value& b=stack.pop().value();  Value& a=stack.pop().value();
                    778:                                Value& value=*new VInt(
                    779:                                        a.as_int() &
                    780:                                        b.as_int());
                    781:                                stack.push(value);
1.54      paf       782:                                break;
                    783:                        }
1.322     misha     784:                case OP::OP_BIN_OR:
1.54      paf       785:                        {
1.297     paf       786:                                Value& b=stack.pop().value();  Value& a=stack.pop().value();
                    787:                                Value& value=*new VInt(
                    788:                                        a.as_int() |
                    789:                                        b.as_int());
                    790:                                stack.push(value);
1.55      paf       791:                                break;
                    792:                        }
1.322     misha     793:                case OP::OP_BIN_XOR:
1.56      paf       794:                        {
1.297     paf       795:                                Value& b=stack.pop().value();  Value& a=stack.pop().value();
                    796:                                Value& value=*new VInt(
                    797:                                        a.as_int() ^
                    798:                                        b.as_int());
                    799:                                stack.push(value);
1.56      paf       800:                                break;
                    801:                        }
1.322     misha     802:                case OP::OP_LOG_AND:
1.55      paf       803:                        {
1.297     paf       804:                                ArrayOperation& local_ops=stack.pop().ops();  Value& a=stack.pop().value();
1.263     paf       805:                                bool result;
1.297     paf       806:                                if(a.as_bool()) {
                    807:                                        execute(local_ops);
                    808:                                        Value& b=stack.pop().value();
                    809:                                        result=b.as_bool();
1.209     paf       810:                                } else
1.263     paf       811:                                        result=false;
1.327     misha     812:                                Value& value=VBool::get(result);
1.297     paf       813:                                stack.push(value);
1.55      paf       814:                                break;
                    815:                        }
1.322     misha     816:                case OP::OP_LOG_OR:
1.55      paf       817:                        {
1.297     paf       818:                                ArrayOperation& local_ops=stack.pop().ops();  Value& a=stack.pop().value();
1.263     paf       819:                                bool result;
1.297     paf       820:                                if(a.as_bool()) 
1.263     paf       821:                                        result=true;
1.209     paf       822:                                else {
1.297     paf       823:                                        execute(local_ops);
                    824:                                        Value& b=stack.pop().value();
                    825:                                        result=b.as_bool();
1.209     paf       826:                                }
1.327     misha     827:                                Value& value=VBool::get(result);
1.297     paf       828:                                stack.push(value);
1.56      paf       829:                                break;
                    830:                        }
1.322     misha     831:                case OP::OP_LOG_XOR:
1.56      paf       832:                        {
1.297     paf       833:                                Value& b=stack.pop().value();  Value& a=stack.pop().value();
1.327     misha     834:                                Value& value=VBool::get(a.as_bool() ^ b.as_bool());
1.297     paf       835:                                stack.push(value);
1.55      paf       836:                                break;
                    837:                        }
1.322     misha     838:                case OP::OP_NUM_LT: 
1.55      paf       839:                        {
1.297     paf       840:                                volatile double b_double=stack.pop().value().as_double();
                    841:                                volatile double a_double=stack.pop().value().as_double();
1.327     misha     842:                                Value& value=VBool::get(a_double<b_double);
1.297     paf       843:                                stack.push(value);
1.55      paf       844:                                break;
                    845:                        }
1.322     misha     846:                case OP::OP_NUM_GT: 
1.55      paf       847:                        {
1.297     paf       848:                                volatile double b_double=stack.pop().value().as_double();
                    849:                                volatile double a_double=stack.pop().value().as_double();
1.327     misha     850:                                Value& value=VBool::get(a_double>b_double);
1.297     paf       851:                                stack.push(value);
1.55      paf       852:                                break;
                    853:                        }
1.322     misha     854:                case OP::OP_NUM_LE: 
1.55      paf       855:                        {
1.297     paf       856:                                volatile double b_double=stack.pop().value().as_double();
                    857:                                volatile double a_double=stack.pop().value().as_double();
1.327     misha     858:                                Value& value=VBool::get(a_double<=b_double);
1.297     paf       859:                                stack.push(value);
1.55      paf       860:                                break;
                    861:                        }
1.322     misha     862:                case OP::OP_NUM_GE: 
1.55      paf       863:                        {
1.297     paf       864:                                volatile double b_double=stack.pop().value().as_double();
                    865:                                volatile double a_double=stack.pop().value().as_double();
1.327     misha     866:                                Value& value=VBool::get(a_double>=b_double);
1.297     paf       867:                                stack.push(value);
1.55      paf       868:                                break;
                    869:                        }
1.322     misha     870:                case OP::OP_NUM_EQ: 
1.55      paf       871:                        {
1.297     paf       872:                                volatile double b_double=stack.pop().value().as_double();
                    873:                                volatile double a_double=stack.pop().value().as_double();
1.327     misha     874:                                Value& value=VBool::get(a_double==b_double);
1.297     paf       875:                                stack.push(value);
1.55      paf       876:                                break;
                    877:                        }
1.322     misha     878:                case OP::OP_NUM_NE: 
1.55      paf       879:                        {
1.297     paf       880:                                volatile double b_double=stack.pop().value().as_double();
                    881:                                volatile double a_double=stack.pop().value().as_double();
1.327     misha     882:                                Value& value=VBool::get(a_double!=b_double);
1.297     paf       883:                                stack.push(value);
1.54      paf       884:                                break;
                    885:                        }
1.322     misha     886:                case OP::OP_STR_LT: 
1.58      paf       887:                        {
1.297     paf       888:                                Value& b=stack.pop().value();  Value& a=stack.pop().value();
1.327     misha     889:                                Value& value=VBool::get(a.as_string() < b.as_string());
1.297     paf       890:                                stack.push(value);
1.58      paf       891:                                break;
                    892:                        }
1.322     misha     893:                case OP::OP_STR_GT: 
1.58      paf       894:                        {
1.297     paf       895:                                Value& b=stack.pop().value();  Value& a=stack.pop().value();
1.327     misha     896:                                Value& value=VBool::get(a.as_string() > b.as_string());
1.297     paf       897:                                stack.push(value);
1.58      paf       898:                                break;
                    899:                        }
1.322     misha     900:                case OP::OP_STR_LE: 
1.58      paf       901:                        {
1.297     paf       902:                                Value& b=stack.pop().value();  Value& a=stack.pop().value();
1.327     misha     903:                                Value& value=VBool::get(a.as_string() <= b.as_string());
1.297     paf       904:                                stack.push(value);
1.58      paf       905:                                break;
                    906:                        }
1.322     misha     907:                case OP::OP_STR_GE: 
1.54      paf       908:                        {
1.297     paf       909:                                Value& b=stack.pop().value();  Value& a=stack.pop().value();
1.327     misha     910:                                Value& value=VBool::get(a.as_string() >= b.as_string());
1.297     paf       911:                                stack.push(value);
1.58      paf       912:                                break;
                    913:                        }
1.322     misha     914:                case OP::OP_STR_EQ: 
1.58      paf       915:                        {
1.297     paf       916:                                Value& b=stack.pop().value();  Value& a=stack.pop().value();
1.327     misha     917:                                Value& value=VBool::get(a.as_string() == b.as_string());
1.297     paf       918:                                stack.push(value);
1.58      paf       919:                                break;
                    920:                        }
1.322     misha     921:                case OP::OP_STR_NE: 
1.58      paf       922:                        {
1.297     paf       923:                                Value& b=stack.pop().value();  Value& a=stack.pop().value();
1.327     misha     924:                                Value& value=VBool::get(a.as_string() != b.as_string());
1.297     paf       925:                                stack.push(value);
1.103     paf       926:                                break;
                    927:                        }
1.322     misha     928:                case OP::OP_IS:
1.103     paf       929:                        {
1.297     paf       930:                                Value& b=stack.pop().value();  Value& a=stack.pop().value();
1.327     misha     931:                                Value& value=VBool::get(a.is(b.as_string().cstr()));
1.297     paf       932:                                stack.push(value);
1.28      paf       933:                                break;
                    934:                        }
                    935: 
1.11      paf       936:                default:
1.223     paf       937:                        throw Exception(0,
1.67      paf       938:                                0,
1.297     paf       939:                                "invalid opcode %d", opcode); 
1.11      paf       940:                }
                    941:        }
1.306     paf       942:        } catch(const Exception&) {
1.297     paf       943:                // record it to stack trace
1.301     paf       944:                if(debug_name)
                    945:                        exception_trace.push(Trace(debug_name, debug_origin));
1.297     paf       946:                rethrow;
                    947:        }
1.1       paf       948: }
1.17      paf       949: 
1.330     misha     950: #define SAVE_CONTEXT                                                           \
                    951:        VMethodFrame *saved_method_frame=method_frame;  \
                    952:        Value* saved_rcontext=rcontext;                                 \
                    953:        WContext *saved_wcontext=wcontext;
                    954: 
                    955: #define RESTORE_CONTEXT                                        \
                    956:        wcontext=saved_wcontext;                        \
                    957:        rcontext=saved_rcontext;                        \
                    958:        method_frame=saved_method_frame;
                    959: 
1.332   ! misha     960: void Request::op_call(VMethodFrame& frame){
        !           961:        const Junction &junction=frame.junction;
        !           962:        VStateless_class& called_class=*junction.self.get_class();
1.319     misha     963:        Value* new_self;
1.332   ! misha     964: 
1.319     misha     965:        if(wcontext->get_constructing()) {
                    966:                wcontext->set_constructing(false);
1.332   ! misha     967:                if(junction.method->call_type!=Method::CT_STATIC) {
1.319     misha     968:                        // this is a constructor call
                    969:                        HashStringValue& new_object_fields=*new HashStringValue();
1.332   ! misha     970:                        if(new_self=called_class.create_new_value(fpool, new_object_fields)) {
1.319     misha     971:                                // some stateless_class creatable derivates
                    972:                        } else 
                    973:                                throw Exception(PARSER_RUNTIME,
                    974:                                        0, //&frame.name(),
                    975:                                        "is not a constructor, system class '%s' can be constructed only implicitly", 
                    976:                                                called_class.name().cstr());
                    977: 
                    978:                        frame.write(*new_self, 
                    979:                                String::L_CLEAN  // not used, always an object, not string
                    980:                        );
                    981:                } else
                    982:                        throw Exception(PARSER_RUNTIME,
                    983:                                0, //&frame.name(),
                    984:                                "method is static and can not be used as constructor");
1.332   ! misha     985:        } else 
        !           986:                new_self=&junction.self;
1.319     misha     987: 
                    988:        frame.set_self(*new_self);
                    989: 
                    990:        // see OP_PREPARE_TO_EXPRESSION
                    991:        frame.set_in_expression(wcontext->get_in_expression());
                    992:                                
1.332   ! misha     993:        SAVE_CONTEXT
        !           994: 
1.319     misha     995:        rcontext=wcontext=&frame;
1.332   ! misha     996:        method_frame=&frame;
1.319     misha     997: 
1.332   ! misha     998:        const Method& method=*junction.method;
        !           999:        Method::Call_type call_type=&called_class==new_self ? Method::CT_STATIC : Method::CT_DYNAMIC;
1.319     misha    1000: 
1.332   ! misha    1001:        if(method.call_type==Method::CT_ANY || method.call_type==call_type) { // allowed call type?
        !          1002:                if(method.native_code) { // native code?
        !          1003:                        method.check_actual_numbered_params(junction.self, frame.numbered_params());
        !          1004:                        method.native_code(*this, *frame.numbered_params()); // execute it
        !          1005:                } else // parser code, execute it
        !          1006:                        recoursion_checked_execute(*method.parser_code);
        !          1007:        } else
        !          1008:                throw Exception(PARSER_RUNTIME,
        !          1009:                        0,
        !          1010:                        "is not allowed to be called %s", 
        !          1011:                        call_type==Method::CT_STATIC?"statically":"dynamically");
        !          1012:  
1.330     misha    1013:        RESTORE_CONTEXT
1.332   ! misha    1014:        //return &frame;
        !          1015: }
        !          1016: 
        !          1017: void Request::op_call_write(VMethodFrame& frame){
        !          1018:        const Junction &junction=frame.junction;
        !          1019: 
        !          1020:        frame.set_self(junction.self);
        !          1021: 
        !          1022:        VMethodFrame *saved_method_frame=method_frame;
        !          1023:        Value* saved_rcontext=rcontext;
1.319     misha    1024: 
1.332   ! misha    1025:        rcontext=&frame;
        !          1026:        method_frame=&frame;
        !          1027: 
        !          1028:        const Method& method=*junction.method;
        !          1029:        Method::Call_type call_type=junction.self.get_class()==&junction.self ? Method::CT_STATIC : Method::CT_DYNAMIC;
        !          1030: 
        !          1031:        if( method.call_type==Method::CT_ANY || method.call_type==call_type) { // allowed call type?
        !          1032:                method.check_actual_numbered_params(junction.self, frame.numbered_params());
        !          1033:                method.native_code(*this, *frame.numbered_params()); // execute it
        !          1034:        } else
        !          1035:                throw Exception(PARSER_RUNTIME,
        !          1036:                        0,
        !          1037:                        "is not allowed to be called %s", 
        !          1038:                        call_type==Method::CT_STATIC?"statically":"dynamically");
        !          1039:        
        !          1040:        rcontext=saved_rcontext;
        !          1041:        method_frame=saved_method_frame;
1.319     misha    1042: }
                   1043: 
1.292     paf      1044: /**
                   1045:        @bug ^superbase:method would dynamically call ^base:method if there is any
                   1046: */
1.330     misha    1047: Value& Request::get_element(Value& ncontext, const String& name) {
                   1048:        if(!wcontext->get_constructing() // not constructing
                   1049:                && wcontext->get_somebody_entered_some_class() ) // ^class:method
1.297     paf      1050:                        if(VStateless_class *called_class=ncontext.get_class())
1.249     paf      1051:                                if(VStateless_class *read_class=rcontext->get_class())
                   1052:                                        if(read_class->derived_from(*called_class)) // current derived from called
1.297     paf      1053:                                                if(Value* base=get_self().base()) { // doing DYNAMIC call
1.330     misha    1054:                                                        Temp_derived temp_derived(*base, 0); // temporarily prevent go-back-down virtual calls 
                   1055:                                                        Value *value=base->get_element(name, *base, true); // virtual-up lookup starting from parent
                   1056:                                                        return *(value ? &process_to_value(*value) : VVoid::get());
1.249     paf      1057:                                                }
1.330     misha    1058: 
                   1059:        Value* value=ncontext.get_element(name, ncontext, true);
1.291     paf      1060: 
                   1061:        if(value && wcontext->get_constructing())
1.330     misha    1062:                if(Junction* junction=value->get_junction())
1.297     paf      1063:                        if(junction->self.get_class()!=&ncontext)
1.316     misha    1064:                                throw Exception(PARSER_RUNTIME,
1.291     paf      1065:                                        &name,
1.297     paf      1066:                                        "constructor must be declared in class '%s'", 
                   1067:                                                ncontext.get_class()->name_cstr());
1.63      paf      1068: 
1.330     misha    1069:        return *(value ? &process_to_value(*value) : VVoid::get());
1.309     paf      1070: }
                   1071: 
1.329     misha    1072: void Request::put_element(Value& ncontext, const String& name, Value* value) {
1.309     paf      1073:        // put_element can return property-setting-junction
1.329     misha    1074:        if(const VJunction* vjunction=ncontext.put_element(ncontext, name, value, false))
1.312     paf      1075:                if(vjunction!=PUT_ELEMENT_REPLACED_ELEMENT) {
1.309     paf      1076:                        // process it
1.329     misha    1077:                        VMethodFrame frame(vjunction->junction(), method_frame/*caller*/);
                   1078:                        int param_count=frame.method_params_count();
                   1079: 
1.309     paf      1080:                        if(param_count!=1)
1.316     misha    1081:                                throw Exception(PARSER_RUNTIME,
1.309     paf      1082:                                        0,
                   1083:                                        "setter method must have ONE parameter (has %d parameters)", param_count);
                   1084: 
1.329     misha    1085:                        frame.store_params(&value, 1);
1.309     paf      1086:                        frame.set_self(frame.junction.self);
                   1087: 
1.330     misha    1088:                        SAVE_CONTEXT
1.309     paf      1089: 
                   1090:                        rcontext=wcontext=&frame;
                   1091:                        method_frame=&frame;
                   1092: 
1.311     paf      1093:                        // prevent non-string writes for better error reporting [setters are not expected to return anything]
1.309     paf      1094:                        wcontext->write(*method_frame);
                   1095: 
                   1096:                        recoursion_checked_execute(*frame.junction.method->parser_code); // parser code, execute it
                   1097:                        // we don't need it StringOrValue result=wcontext->result();
                   1098: 
1.330     misha    1099:                        RESTORE_CONTEXT
1.309     paf      1100:                }
1.34      paf      1101: }
1.70      paf      1102: 
1.162     parser   1103: /**    @param intercept_string
1.116     paf      1104:        - true:
                   1105:                they want result=string value, 
                   1106:                possible object result goes to wcontext
                   1107:        - false:
                   1108:                they want any result[string|object]
                   1109:                nothing goes to wcontext.
1.125     paf      1110:                used in @c (expression) params evaluation
1.224     paf      1111: 
                   1112:     using the fact it's either string_ or value_ result requested to speed up checkes
1.116     paf      1113: */
1.229     paf      1114: StringOrValue Request::process(Value& input_value, bool intercept_string) {
1.297     paf      1115:        Junction* junction=input_value.get_junction();
1.307     paf      1116:        if(junction) {
                   1117:                if(junction->is_getter) { // is it a getter-junction?
1.329     misha    1118:                        VMethodFrame frame(*junction, method_frame/*caller*/);
                   1119:                        int param_count=frame.method_params_count();
                   1120: 
1.331     misha    1121:                        if(param_count){
                   1122:                                if(junction->auto_name){ // default getter
                   1123:                                        if(param_count==1){
                   1124:                                                Value *param=new VString(*junction->auto_name);
                   1125:                                                frame.store_params(&param, 1);
                   1126:                                        } else
                   1127:                                                throw Exception(PARSER_RUNTIME,
                   1128:                                                        0,
                   1129:                                                        "default getter method can't have more then 1 parameter (has %d parameters)", param_count);
                   1130:                                } else
1.320     misha    1131:                                        throw Exception(PARSER_RUNTIME,
                   1132:                                                0,
1.331     misha    1133:                                                "getter method must have no parameters (has %d parameters)", param_count);
                   1134:                        } // no need for else frame.empty_params()
1.320     misha    1135: 
1.307     paf      1136:                        frame.set_self(frame.junction.self);
                   1137: 
1.330     misha    1138:                        SAVE_CONTEXT
1.307     paf      1139: 
                   1140:                        rcontext=wcontext=&frame;
                   1141:                        method_frame=&frame;
                   1142: 
                   1143:                        recoursion_checked_execute(*frame.junction.method->parser_code); // parser code, execute it
                   1144:                        StringOrValue result=wcontext->result();
                   1145: 
1.330     misha    1146:                        RESTORE_CONTEXT
1.307     paf      1147: 
                   1148:                        return result;
                   1149:                }
                   1150: 
                   1151:                if(junction->code) { // is it a code-junction?
                   1152:                        // process it
                   1153:                        StringOrValue result;
1.157     parser   1154: #ifdef DEBUG_EXECUTE
1.307     paf      1155:                        debug_printf(sapi_info, "ja->\n");
1.157     parser   1156: #endif
1.238     paf      1157: 
1.307     paf      1158:                        if(!junction->method_frame)
1.316     misha    1159:                                throw Exception(PARSER_RUNTIME,
1.240     paf      1160:                                0,
                   1161:                                "junction used outside of context");
                   1162: 
1.330     misha    1163:                        SAVE_CONTEXT
1.307     paf      1164: 
                   1165:                        method_frame=junction->method_frame;
                   1166:                        rcontext=junction->rcontext;
                   1167: 
                   1168:                        // for expression method params
                   1169:                        // wcontext is set 0
                   1170:                        // using the fact in decision "which wwrapper to use"
                   1171:                        bool using_code_frame=intercept_string && junction->wcontext;
                   1172:                        if(using_code_frame) {
1.318     misha    1173:                                // almost plain wwrapper about junction wcontext 
                   1174: 
                   1175:                                VCodeFrame local(*junction->wcontext);
1.307     paf      1176:                                wcontext=&local;
                   1177: 
                   1178:                                // execute it
                   1179:                                recoursion_checked_execute(*junction->code);
                   1180: 
                   1181:                                // CodeFrame soul:
1.318     misha    1182:                                result=wcontext->result();
1.307     paf      1183:                        } else {
                   1184:                                // plain wwrapper
                   1185:                                WWrapper local(0/*empty*/, wcontext);
                   1186:                                wcontext=&local;
                   1187: 
                   1188:                                // execute it
                   1189:                                recoursion_checked_execute(*junction->code);
                   1190: 
                   1191:                                result=wcontext->result();
                   1192:                        }
                   1193: 
1.330     misha    1194:                        RESTORE_CONTEXT
1.207     paf      1195: 
1.157     parser   1196: #ifdef DEBUG_EXECUTE
1.307     paf      1197:                        debug_printf(sapi_info, "<-ja returned");
1.157     parser   1198: #endif
1.307     paf      1199:                        return result;
                   1200:                }
                   1201: 
                   1202:                // it is then method-junction, do not explode it
                   1203:                // just return it as we do for usual objects
                   1204:        }       
                   1205: 
                   1206:        return input_value;
1.85      paf      1207: }
                   1208: 
1.332   ! misha    1209: void Request::process_write(Value& input_value) {
        !          1210:        Junction* junction=input_value.get_junction();
        !          1211:        if(junction) {
        !          1212:                if(junction->is_getter) { // is it a getter-junction?
        !          1213:                        VMethodFrame frame(*junction, method_frame/*caller*/);
        !          1214:                        int param_count=frame.method_params_count();
        !          1215: 
        !          1216:                        if(param_count){
        !          1217:                                if(junction->auto_name){ // default getter
        !          1218:                                        if(param_count==1){
        !          1219:                                                Value *param=new VString(*junction->auto_name);
        !          1220:                                                frame.store_params(&param, 1);
        !          1221:                                        } else 
        !          1222:                                                throw Exception(PARSER_RUNTIME,
        !          1223:                                                        0,
        !          1224:                                                        "default getter method can't have more then 1 parameter (has %d parameters)", param_count);
        !          1225:                                } else 
        !          1226:                                        throw Exception(PARSER_RUNTIME,
        !          1227:                                                0,
        !          1228:                                                "getter method must have no parameters (has %d parameters)", param_count);
        !          1229:                        } // no need for else frame.empty_params()
        !          1230: 
        !          1231:                        frame.set_self(frame.junction.self);
        !          1232: 
        !          1233:                        SAVE_CONTEXT
        !          1234: 
        !          1235:                        rcontext=wcontext=&frame;
        !          1236:                        method_frame=&frame;
        !          1237: 
        !          1238:                        recoursion_checked_execute(*frame.junction.method->parser_code); // parser code, execute it
        !          1239: 
        !          1240:                        RESTORE_CONTEXT
        !          1241: 
        !          1242:                        write_pass_lang(frame.result());
        !          1243:                        return;
        !          1244:                }
        !          1245: 
        !          1246:                if(junction->code) { // is it a code-junction?
        !          1247:                                                        // process it
        !          1248: #ifdef DEBUG_EXECUTE
        !          1249:                        debug_printf(sapi_info, "ja->\n");
        !          1250: #endif
        !          1251:                        if(!junction->method_frame)
        !          1252:                                throw Exception(PARSER_RUNTIME,
        !          1253:                                        0,
        !          1254:                                        "junction used outside of context");
        !          1255: 
        !          1256:                        SAVE_CONTEXT
        !          1257: 
        !          1258:                        method_frame=junction->method_frame;
        !          1259:                        rcontext=junction->rcontext;
        !          1260: 
        !          1261:                        // for expression method params
        !          1262:                        // wcontext is set 0
        !          1263:                        // using the fact in decision "which wwrapper to use"
        !          1264: #ifdef OPTIMIZE_CALL
        !          1265:                        if(wcontext==junction->wcontext){
        !          1266:                                // no wrappers for wcontext
        !          1267:                                recoursion_checked_execute(*junction->code);
        !          1268:                                RESTORE_CONTEXT
        !          1269: 
        !          1270:                        } else
        !          1271: #endif
        !          1272:                        if(junction->wcontext) {
        !          1273:                                // almost plain wwrapper about junction wcontext 
        !          1274:                                VCodeFrame local(*junction->wcontext);
        !          1275:                                wcontext=&local;
        !          1276: 
        !          1277:                                // execute it
        !          1278:                                recoursion_checked_execute(*junction->code);
        !          1279:                                RESTORE_CONTEXT
        !          1280:                                write_pass_lang(local.result());
        !          1281:                        } else {
        !          1282:                                // plain wwrapper
        !          1283:                                WWrapper local(0/*empty*/, wcontext);
        !          1284:                                wcontext=&local;
        !          1285: 
        !          1286:                                // execute it
        !          1287:                                recoursion_checked_execute(*junction->code);
        !          1288:                                RESTORE_CONTEXT
        !          1289:                                write_pass_lang(local.result());
        !          1290:                        }
        !          1291: #ifdef DEBUG_EXECUTE
        !          1292:                        debug_printf(sapi_info, "<-ja returned");
        !          1293: #endif
        !          1294:                        return;
        !          1295:                }
        !          1296: 
        !          1297:                // it is then method-junction, do not explode it
        !          1298:                // just return it as we do for usual objects
        !          1299:        }
        !          1300: 
        !          1301:        write_pass_lang(input_value);
        !          1302: }
        !          1303: 
1.298     paf      1304: StringOrValue Request::execute_method(VMethodFrame& amethod_frame, const Method& method) {
1.330     misha    1305:        SAVE_CONTEXT
1.99      paf      1306:        
                   1307:        // initialize contexts
1.286     paf      1308:        rcontext=wcontext=method_frame=&amethod_frame;
1.99      paf      1309:        
                   1310:        // execute!     
                   1311:        execute(*method.parser_code);
                   1312:        
                   1313:        // result
1.298     paf      1314:        StringOrValue result=wcontext->result();
1.208     paf      1315:        
1.330     misha    1316:        RESTORE_CONTEXT
1.242     paf      1317:        
                   1318:        // return
                   1319:        return result;
1.208     paf      1320: }
                   1321: 
1.297     paf      1322: const String* Request::execute_method(Value& aself, 
1.329     misha    1323:                                        const Method& method, Value* optional_param,
                   1324:                                        bool do_return_string) {
1.317     misha    1325: 
1.330     misha    1326:        SAVE_CONTEXT
1.208     paf      1327:        
1.307     paf      1328:        Junction local_junction(aself, &method);
1.321     misha    1329:        VMethodFrame local_frame(local_junction, method_frame/*caller*/);
1.329     misha    1330:        if(optional_param && local_frame.method_params_count()>0) {
                   1331:                local_frame.store_params(&optional_param, 1);
1.330     misha    1332:        } else {
                   1333:                local_frame.empty_params();
1.242     paf      1334:        }
1.285     paf      1335:        local_frame.set_self(aself);
1.257     paf      1336:        rcontext=wcontext=method_frame=&local_frame; 
1.246     paf      1337: 
                   1338:        // prevent non-string writes for better error reporting
1.297     paf      1339:        if(do_return_string)
1.246     paf      1340:                wcontext->write(local_frame);
1.208     paf      1341:        
                   1342:        // execute!     
                   1343:        execute(*method.parser_code);
                   1344:        
                   1345:        // result
1.297     paf      1346:        const String* result=0;
                   1347:        if(do_return_string)
                   1348:                result=&wcontext->result().as_string();
1.99      paf      1349:        
1.330     misha    1350:        RESTORE_CONTEXT
1.297     paf      1351: 
                   1352:        return result;
1.242     paf      1353: }
                   1354: 
1.297     paf      1355: Request::Execute_nonvirtual_method_result 
                   1356: Request::execute_nonvirtual_method(VStateless_class& aclass, 
1.330     misha    1357:                                        const String& method_name,
                   1358:                                        VString* optional_param,
                   1359:                                        bool do_return_string) {
1.297     paf      1360:        Execute_nonvirtual_method_result result;
                   1361:        result.method=aclass.get_method(method_name);
                   1362:        if(result.method)
1.317     misha    1363:                result.string=execute_method(aclass, *result.method, optional_param, do_return_string);
1.297     paf      1364:        return result;
1.99      paf      1365: }
                   1366: 
1.297     paf      1367: const String* Request::execute_virtual_method(Value& aself, 
1.330     misha    1368:                                        const String& method_name) {
1.297     paf      1369:        if(Value* value=aself.get_element(method_name, aself, false))
                   1370:                if(Junction* junction=value->get_junction())
                   1371:                        if(const Method *method=junction->method) 
                   1372:                                return execute_method(aself, *method, 0/*no params*/, true);
1.188     parser   1373:                        
1.176     parser   1374:        return 0;
                   1375: }

E-mail: