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

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.330   ! misha       8: static const char * const IDENT_EXECUTE_C="$Date: 2009-04-29 03:26:50 $";
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
                    469:                                // this op is executed from CALL local_ops only, so can not check method_frame_to_fill==0
1.329     misha     470:                                stack.push(value);
1.29      paf       471:                                break;
                    472:                        }
                    473: 
1.322     misha     474:                case OP::OP_PREPARE_TO_CONSTRUCT_OBJECT:
1.186     parser    475:                        {
1.200     paf       476:                                wcontext->set_constructing(true);
1.186     parser    477:                                break;
                    478:                        }
1.219     paf       479: 
1.322     misha     480:                case OP::OP_PREPARE_TO_EXPRESSION:
1.219     paf       481:                        {
                    482:                                wcontext->set_in_expression(true);
                    483:                                break;
                    484:                        }
                    485: 
1.322     misha     486:                case OP::OP_CALL:
                    487:                case OP::OP_CALL__WRITE:
1.29      paf       488:                        {
1.297     paf       489:                                //is_debug_junction=true;
                    490:                                ArrayOperation* local_ops=i.next().ops;
1.157     parser    491: #ifdef DEBUG_EXECUTE
1.297     paf       492:                                debug_printf(sapi_info, " (%d)\n", local_ops?local_ops->count():0);
                    493:                                if(local_ops)
                    494:                                        debug_dump(sapi_info, 1, *local_ops);
1.237     paf       495: 
1.297     paf       496:                                debug_printf(sapi_info, "->\n");
1.157     parser    497: #endif
1.297     paf       498:                                Value& value=stack.pop().value();
1.237     paf       499: 
1.297     paf       500:                                Junction* junction=value.get_junction();
                    501:                                if(!junction) {
                    502:                                        if(value.is("void"))
1.316     misha     503:                                                throw Exception(PARSER_RUNTIME,
1.297     paf       504:                                                        0,
                    505:                                                        "undefined method");
                    506:                                        else
1.316     misha     507:                                                throw Exception(PARSER_RUNTIME,
1.297     paf       508:                                                        0,
                    509:                                                        "is '%s', not a method or junction, can not call it",
                    510:                                                                value.type());
                    511:                                }
1.282     paf       512:                                /* no check needed, code compiled the way that that's impossible
1.276     paf       513:                                // check: 
                    514:                                //      that this is method-junction, not a code-junction
1.278     paf       515:                                // [disabling these contstructions:]
                    516:                                // $junction{code}
                    517:                                //  ^junction[]
1.276     paf       518:                                if(!junction->method)
1.316     misha     519:                                        throw Exception(PARSER_RUNTIME,
1.297     paf       520:                                                "is '%s', it is code junction, can not call it",
                    521:                                                        value.type());
1.282     paf       522:                                */
1.237     paf       523: 
1.321     misha     524:                                VMethodFrame frame(*junction, method_frame);
1.330   ! misha     525:                                WContext* result_wcontext;
        !           526: 
1.321     misha     527:                                if(local_ops){ // store param code goes here
1.329     misha     528:                                        size_t first = stack.top_index();
1.321     misha     529:                                        execute(*local_ops);
1.329     misha     530:                                        frame.store_params((Value**)stack.ptr(first), stack.top_index()-first);
1.330   ! misha     531:                                        result_wcontext=op_call(frame);
1.329     misha     532:                                        stack.set_top_index(first);
1.330   ! misha     533:                                } else {
        !           534:                                        frame.empty_params();
        !           535:                                        result_wcontext=op_call(frame);
1.321     misha     536:                                }
1.330   ! misha     537: 
1.322     misha     538:                                if(opcode==OP::OP_CALL__WRITE) {
1.321     misha     539:                                        write_assign_lang(result_wcontext->result());
                    540:                                } else { // OP_CALL
                    541:                                        stack.push(result_wcontext->result().as_value());
1.237     paf       542:                                }
1.220     paf       543: 
1.157     parser    544: #ifdef DEBUG_EXECUTE
1.297     paf       545:                                debug_printf(sapi_info, "<-returned");
1.157     parser    546: #endif
1.297     paf       547:                                //is_debug_junction=false;
1.327     misha     548: 
                    549:                                if(get_skip())
                    550:                                        return;
                    551:                                if(get_interrupted()) {
                    552:                                        set_interrupted(false);
                    553:                                        throw Exception("parser.interrupted",
                    554:                                                0,
                    555:                                                "execution stopped");
                    556:                                }
1.49      paf       557:                                break;
                    558:                        }
                    559: 
1.55      paf       560:                // expression ops: unary
1.322     misha     561:                case OP::OP_NEG:
1.55      paf       562:                        {
1.297     paf       563:                                Value& a=stack.pop().value();
                    564:                                Value& value=*new VDouble(-a.as_double());
                    565:                                stack.push(value);
1.55      paf       566:                                break;
                    567:                        }
1.322     misha     568:                case OP::OP_INV:
1.55      paf       569:                        {
1.297     paf       570:                                Value& a=stack.pop().value();
                    571:                                Value& value=*new VDouble(~a.as_int());
                    572:                                stack.push(value);
1.55      paf       573:                                break;
                    574:                        }
1.322     misha     575:                case OP::OP_NOT:
1.55      paf       576:                        {
1.297     paf       577:                                Value& a=stack.pop().value();
1.327     misha     578:                                Value& value=VBool::get(!a.as_bool());
1.297     paf       579:                                stack.push(value);
1.55      paf       580:                                break;
                    581:                        }
1.322     misha     582:                case OP::OP_DEF:
1.62      paf       583:                        {
1.297     paf       584:                                Value& a=stack.pop().value();
1.327     misha     585:                                Value& value=VBool::get(a.is_defined());
1.297     paf       586:                                stack.push(value);
1.62      paf       587:                                break;
                    588:                        }
1.322     misha     589:                case OP::OP_IN:
1.62      paf       590:                        {
1.297     paf       591:                                Value& a=stack.pop().value();
                    592:                                const String& path=a.as_string();
1.327     misha     593:                                Value& value=VBool::get(request_info.uri && *request_info.uri && path.this_starts(request_info.uri));
1.297     paf       594:                                stack.push(value);
1.62      paf       595:                                break;
                    596:                        }
1.322     misha     597:                case OP::OP_FEXISTS:
1.62      paf       598:                        {
1.297     paf       599:                                Value& a=stack.pop().value();
1.327     misha     600:                                Value& value=VBool::get(file_exist(absolute(a.as_string())));
1.297     paf       601:                                stack.push(value);
1.148     paf       602:                                break;
                    603:                        }
1.322     misha     604:                case OP::OP_DEXISTS:
1.148     paf       605:                        {
1.297     paf       606:                                Value& a=stack.pop().value();
1.327     misha     607:                                Value& value=VBool::get(dir_exists(absolute(a.as_string())));
1.297     paf       608:                                stack.push(value);
1.62      paf       609:                                break;
                    610:                        }
1.55      paf       611: 
                    612:                // expression ops: binary
1.322     misha     613:                case OP::OP_SUB: 
1.53      paf       614:                        {
1.297     paf       615:                                Value& b=stack.pop().value();  Value& a=stack.pop().value();
                    616:                                Value& value=*new VDouble(a.as_double() - b.as_double());
                    617:                                stack.push(value);
1.53      paf       618:                                break;
                    619:                        }
1.322     misha     620:                case OP::OP_ADD: 
1.53      paf       621:                        {
1.297     paf       622:                                Value& b=stack.pop().value();  Value& a=stack.pop().value();
                    623:                                Value& value=*new VDouble(a.as_double() + b.as_double());
                    624:                                stack.push(value);
1.53      paf       625:                                break;
                    626:                        }
1.322     misha     627:                case OP::OP_MUL: 
1.49      paf       628:                        {
1.297     paf       629:                                Value& b=stack.pop().value();  Value& a=stack.pop().value();
                    630:                                Value& value=*new VDouble(a.as_double() * b.as_double());
                    631:                                stack.push(value);
1.53      paf       632:                                break;
                    633:                        }
1.322     misha     634:                case OP::OP_DIV: 
1.53      paf       635:                        {
1.297     paf       636:                                Value& b=stack.pop().value();  Value& a=stack.pop().value();
1.170     parser    637: 
1.297     paf       638:                                double a_double=a.as_double();
                    639:                                double b_double=b.as_double();
1.170     parser    640: 
1.171     parser    641:                                if(b_double == 0) {
1.297     paf       642:                                        //const String* problem_source=b.as_string();
1.223     paf       643:                                        throw Exception("number.zerodivision",
1.297     paf       644:                                                0, //problem_source,
1.170     parser    645:                                                "Division by zero");
1.171     parser    646:                                }
1.170     parser    647: 
1.297     paf       648:                                Value& value=*new VDouble(a_double / b_double);
                    649:                                stack.push(value);
1.54      paf       650:                                break;
                    651:                        }
1.322     misha     652:                case OP::OP_MOD: 
1.55      paf       653:                        {
1.297     paf       654:                                Value& b=stack.pop().value();  Value& a=stack.pop().value();
1.170     parser    655: 
1.297     paf       656:                                double a_double=a.as_double();
                    657:                                double b_double=b.as_double();
1.170     parser    658: 
1.173     parser    659:                                if(b_double == 0) {
1.297     paf       660:                                        //const String* problem_source=b.as_string();
1.223     paf       661:                                        throw Exception("number.zerodivision",
1.297     paf       662:                                                0, //problem_source,
1.170     parser    663:                                                "Modulus by zero");
1.171     parser    664:                                }
1.170     parser    665: 
1.297     paf       666:                                Value& value=*new VDouble(fmod(a_double, b_double));
                    667:                                stack.push(value);
1.201     paf       668:                                break;
                    669:                        }
1.322     misha     670:                case OP::OP_INTDIV:
1.201     paf       671:                        {
1.297     paf       672:                                Value& b=stack.pop().value();  Value& a=stack.pop().value();
1.201     paf       673: 
1.297     paf       674:                                int a_int=a.as_int();
                    675:                                int b_int=b.as_int();
1.201     paf       676: 
                    677:                                if(b_int == 0) {
1.297     paf       678:                                        //const String* problem_source=b.as_string();
1.223     paf       679:                                        throw Exception("number.zerodivision",
1.297     paf       680:                                                0, //problem_source,
1.201     paf       681:                                                "Division by zero");
                    682:                                }
                    683: 
1.297     paf       684:                                Value& value=*new VInt(a_int / b_int);
                    685:                                stack.push(value);
1.55      paf       686:                                break;
                    687:                        }
1.322     misha     688:                case OP::OP_BIN_SL:
1.274     paf       689:                        {
1.297     paf       690:                                Value& b=stack.pop().value();  Value& a=stack.pop().value();
                    691:                                Value& value=*new VInt(
                    692:                                        a.as_int() <<
                    693:                                        b.as_int());
                    694:                                stack.push(value);
1.274     paf       695:                                break;
                    696:                        }
1.322     misha     697:                case OP::OP_BIN_SR:
1.274     paf       698:                        {
1.297     paf       699:                                Value& b=stack.pop().value();  Value& a=stack.pop().value();
                    700:                                Value& value=*new VInt(
                    701:                                        a.as_int() >>
                    702:                                        b.as_int());
                    703:                                stack.push(value);
1.274     paf       704:                                break;
                    705:                        }
1.322     misha     706:                case OP::OP_BIN_AND:
1.54      paf       707:                        {
1.297     paf       708:                                Value& b=stack.pop().value();  Value& a=stack.pop().value();
                    709:                                Value& value=*new VInt(
                    710:                                        a.as_int() &
                    711:                                        b.as_int());
                    712:                                stack.push(value);
1.54      paf       713:                                break;
                    714:                        }
1.322     misha     715:                case OP::OP_BIN_OR:
1.54      paf       716:                        {
1.297     paf       717:                                Value& b=stack.pop().value();  Value& a=stack.pop().value();
                    718:                                Value& value=*new VInt(
                    719:                                        a.as_int() |
                    720:                                        b.as_int());
                    721:                                stack.push(value);
1.55      paf       722:                                break;
                    723:                        }
1.322     misha     724:                case OP::OP_BIN_XOR:
1.56      paf       725:                        {
1.297     paf       726:                                Value& b=stack.pop().value();  Value& a=stack.pop().value();
                    727:                                Value& value=*new VInt(
                    728:                                        a.as_int() ^
                    729:                                        b.as_int());
                    730:                                stack.push(value);
1.56      paf       731:                                break;
                    732:                        }
1.322     misha     733:                case OP::OP_LOG_AND:
1.55      paf       734:                        {
1.297     paf       735:                                ArrayOperation& local_ops=stack.pop().ops();  Value& a=stack.pop().value();
1.263     paf       736:                                bool result;
1.297     paf       737:                                if(a.as_bool()) {
                    738:                                        execute(local_ops);
                    739:                                        Value& b=stack.pop().value();
                    740:                                        result=b.as_bool();
1.209     paf       741:                                } else
1.263     paf       742:                                        result=false;
1.327     misha     743:                                Value& value=VBool::get(result);
1.297     paf       744:                                stack.push(value);
1.55      paf       745:                                break;
                    746:                        }
1.322     misha     747:                case OP::OP_LOG_OR:
1.55      paf       748:                        {
1.297     paf       749:                                ArrayOperation& local_ops=stack.pop().ops();  Value& a=stack.pop().value();
1.263     paf       750:                                bool result;
1.297     paf       751:                                if(a.as_bool()) 
1.263     paf       752:                                        result=true;
1.209     paf       753:                                else {
1.297     paf       754:                                        execute(local_ops);
                    755:                                        Value& b=stack.pop().value();
                    756:                                        result=b.as_bool();
1.209     paf       757:                                }
1.327     misha     758:                                Value& value=VBool::get(result);
1.297     paf       759:                                stack.push(value);
1.56      paf       760:                                break;
                    761:                        }
1.322     misha     762:                case OP::OP_LOG_XOR:
1.56      paf       763:                        {
1.297     paf       764:                                Value& b=stack.pop().value();  Value& a=stack.pop().value();
1.327     misha     765:                                Value& value=VBool::get(a.as_bool() ^ b.as_bool());
1.297     paf       766:                                stack.push(value);
1.55      paf       767:                                break;
                    768:                        }
1.322     misha     769:                case OP::OP_NUM_LT: 
1.55      paf       770:                        {
1.297     paf       771:                                volatile double b_double=stack.pop().value().as_double();
                    772:                                volatile double a_double=stack.pop().value().as_double();
1.327     misha     773:                                Value& value=VBool::get(a_double<b_double);
1.297     paf       774:                                stack.push(value);
1.55      paf       775:                                break;
                    776:                        }
1.322     misha     777:                case OP::OP_NUM_GT: 
1.55      paf       778:                        {
1.297     paf       779:                                volatile double b_double=stack.pop().value().as_double();
                    780:                                volatile double a_double=stack.pop().value().as_double();
1.327     misha     781:                                Value& value=VBool::get(a_double>b_double);
1.297     paf       782:                                stack.push(value);
1.55      paf       783:                                break;
                    784:                        }
1.322     misha     785:                case OP::OP_NUM_LE: 
1.55      paf       786:                        {
1.297     paf       787:                                volatile double b_double=stack.pop().value().as_double();
                    788:                                volatile double a_double=stack.pop().value().as_double();
1.327     misha     789:                                Value& value=VBool::get(a_double<=b_double);
1.297     paf       790:                                stack.push(value);
1.55      paf       791:                                break;
                    792:                        }
1.322     misha     793:                case OP::OP_NUM_GE: 
1.55      paf       794:                        {
1.297     paf       795:                                volatile double b_double=stack.pop().value().as_double();
                    796:                                volatile double a_double=stack.pop().value().as_double();
1.327     misha     797:                                Value& value=VBool::get(a_double>=b_double);
1.297     paf       798:                                stack.push(value);
1.55      paf       799:                                break;
                    800:                        }
1.322     misha     801:                case OP::OP_NUM_EQ: 
1.55      paf       802:                        {
1.297     paf       803:                                volatile double b_double=stack.pop().value().as_double();
                    804:                                volatile double a_double=stack.pop().value().as_double();
1.327     misha     805:                                Value& value=VBool::get(a_double==b_double);
1.297     paf       806:                                stack.push(value);
1.55      paf       807:                                break;
                    808:                        }
1.322     misha     809:                case OP::OP_NUM_NE: 
1.55      paf       810:                        {
1.297     paf       811:                                volatile double b_double=stack.pop().value().as_double();
                    812:                                volatile double a_double=stack.pop().value().as_double();
1.327     misha     813:                                Value& value=VBool::get(a_double!=b_double);
1.297     paf       814:                                stack.push(value);
1.54      paf       815:                                break;
                    816:                        }
1.322     misha     817:                case OP::OP_STR_LT: 
1.58      paf       818:                        {
1.297     paf       819:                                Value& b=stack.pop().value();  Value& a=stack.pop().value();
1.327     misha     820:                                Value& value=VBool::get(a.as_string() < b.as_string());
1.297     paf       821:                                stack.push(value);
1.58      paf       822:                                break;
                    823:                        }
1.322     misha     824:                case OP::OP_STR_GT: 
1.58      paf       825:                        {
1.297     paf       826:                                Value& b=stack.pop().value();  Value& a=stack.pop().value();
1.327     misha     827:                                Value& value=VBool::get(a.as_string() > b.as_string());
1.297     paf       828:                                stack.push(value);
1.58      paf       829:                                break;
                    830:                        }
1.322     misha     831:                case OP::OP_STR_LE: 
1.58      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_string() <= b.as_string());
1.297     paf       835:                                stack.push(value);
1.58      paf       836:                                break;
                    837:                        }
1.322     misha     838:                case OP::OP_STR_GE: 
1.54      paf       839:                        {
1.297     paf       840:                                Value& b=stack.pop().value();  Value& a=stack.pop().value();
1.327     misha     841:                                Value& value=VBool::get(a.as_string() >= b.as_string());
1.297     paf       842:                                stack.push(value);
1.58      paf       843:                                break;
                    844:                        }
1.322     misha     845:                case OP::OP_STR_EQ: 
1.58      paf       846:                        {
1.297     paf       847:                                Value& b=stack.pop().value();  Value& a=stack.pop().value();
1.327     misha     848:                                Value& value=VBool::get(a.as_string() == b.as_string());
1.297     paf       849:                                stack.push(value);
1.58      paf       850:                                break;
                    851:                        }
1.322     misha     852:                case OP::OP_STR_NE: 
1.58      paf       853:                        {
1.297     paf       854:                                Value& b=stack.pop().value();  Value& a=stack.pop().value();
1.327     misha     855:                                Value& value=VBool::get(a.as_string() != b.as_string());
1.297     paf       856:                                stack.push(value);
1.103     paf       857:                                break;
                    858:                        }
1.322     misha     859:                case OP::OP_IS:
1.103     paf       860:                        {
1.297     paf       861:                                Value& b=stack.pop().value();  Value& a=stack.pop().value();
1.327     misha     862:                                Value& value=VBool::get(a.is(b.as_string().cstr()));
1.297     paf       863:                                stack.push(value);
1.28      paf       864:                                break;
                    865:                        }
                    866: 
1.11      paf       867:                default:
1.223     paf       868:                        throw Exception(0,
1.67      paf       869:                                0,
1.297     paf       870:                                "invalid opcode %d", opcode); 
1.11      paf       871:                }
                    872:        }
1.306     paf       873:        } catch(const Exception&) {
1.297     paf       874:                // record it to stack trace
1.301     paf       875:                if(debug_name)
                    876:                        exception_trace.push(Trace(debug_name, debug_origin));
1.297     paf       877:                rethrow;
                    878:        }
1.1       paf       879: }
1.17      paf       880: 
1.330   ! misha     881: #define SAVE_CONTEXT                                                           \
        !           882:        VMethodFrame *saved_method_frame=method_frame;  \
        !           883:        Value* saved_rcontext=rcontext;                                 \
        !           884:        WContext *saved_wcontext=wcontext;
        !           885: 
        !           886: #define RESTORE_CONTEXT                                        \
        !           887:        wcontext=saved_wcontext;                        \
        !           888:        rcontext=saved_rcontext;                        \
        !           889:        method_frame=saved_method_frame;
        !           890: 
1.319     misha     891: WContext* Request::op_call(VMethodFrame& frame){
1.330   ! misha     892:        SAVE_CONTEXT
1.319     misha     893:                        
                    894:        VStateless_class& called_class=*frame.junction.self.get_class();
                    895:        Value* new_self;
                    896:        if(wcontext->get_constructing()) {
                    897:                wcontext->set_constructing(false);
                    898:                new_self=&get_self();
                    899:                if(frame.junction.method->call_type!=Method::CT_STATIC) {
                    900:                        // this is a constructor call
                    901:                        HashStringValue& new_object_fields=*new HashStringValue();
                    902:                        if(Value* value=called_class.create_new_value(fpool, new_object_fields)) {
                    903:                                // some stateless_class creatable derivates
                    904:                                new_self=value;
                    905:                        } else 
                    906:                                throw Exception(PARSER_RUNTIME,
                    907:                                        0, //&frame.name(),
                    908:                                        "is not a constructor, system class '%s' can be constructed only implicitly", 
                    909:                                                called_class.name().cstr());
                    910: 
                    911:                        frame.write(*new_self, 
                    912:                                String::L_CLEAN  // not used, always an object, not string
                    913:                        );
                    914:                } else
                    915:                        throw Exception(PARSER_RUNTIME,
                    916:                                0, //&frame.name(),
                    917:                                "method is static and can not be used as constructor");
                    918:        } else
                    919:                new_self=&frame.junction.self;
                    920: 
                    921:        frame.set_self(*new_self);
                    922: 
                    923:        // see OP_PREPARE_TO_EXPRESSION
                    924:        frame.set_in_expression(wcontext->get_in_expression());
                    925:                                
                    926:        rcontext=wcontext=&frame;
                    927:        {
                    928:                const Method& method=*frame.junction.method;
                    929:                Method::Call_type call_type=&called_class==new_self ? Method::CT_STATIC : Method::CT_DYNAMIC;
                    930:                if(
                    931:                        method.call_type==Method::CT_ANY ||
                    932:                        method.call_type==call_type) { // allowed call type?
                    933:                        method_frame=&frame;
                    934:                        if(method.native_code) { // native code?
                    935:                                method.check_actual_numbered_params(
                    936:                                        frame.junction.self, 
                    937:                                        /*frame.name(), */frame.numbered_params());
                    938:                                method.native_code(
                    939:                                        *this, 
                    940:                                        *frame.numbered_params()); // execute it
                    941:                        } else // parser code, execute it
                    942:                                recoursion_checked_execute(*method.parser_code);
                    943:                } else
                    944:                        throw Exception(PARSER_RUNTIME,
                    945:                                0, //&frame.name(),
                    946:                                "is not allowed to be called %s", 
                    947:                                        call_type==Method::CT_STATIC?"statically":"dynamically");
                    948:        }
                    949: 
                    950:        WContext *result_wcontext=wcontext;
                    951:        // StringOrValue result=wcontext->result();
                    952: 
1.330   ! misha     953:        RESTORE_CONTEXT
1.319     misha     954: 
                    955:        return result_wcontext;
                    956: }
                    957: 
                    958: 
1.292     paf       959: /**
                    960:        @todo cache|prepare junctions
                    961:        @bug ^superbase:method would dynamically call ^base:method if there is any
                    962: */
1.330   ! misha     963: Value& Request::get_element(Value& ncontext, const String& name) {
        !           964:        if(!wcontext->get_constructing() // not constructing
        !           965:                && wcontext->get_somebody_entered_some_class() ) // ^class:method
1.297     paf       966:                        if(VStateless_class *called_class=ncontext.get_class())
1.249     paf       967:                                if(VStateless_class *read_class=rcontext->get_class())
                    968:                                        if(read_class->derived_from(*called_class)) // current derived from called
1.297     paf       969:                                                if(Value* base=get_self().base()) { // doing DYNAMIC call
1.330   ! misha     970:                                                        Temp_derived temp_derived(*base, 0); // temporarily prevent go-back-down virtual calls 
        !           971:                                                        Value *value=base->get_element(name, *base, true); // virtual-up lookup starting from parent
        !           972:                                                        return *(value ? &process_to_value(*value) : VVoid::get());
1.249     paf       973:                                                }
1.330   ! misha     974: 
        !           975:        Value* value=ncontext.get_element(name, ncontext, true);
1.291     paf       976: 
                    977:        if(value && wcontext->get_constructing())
1.330   ! misha     978:                if(Junction* junction=value->get_junction())
1.297     paf       979:                        if(junction->self.get_class()!=&ncontext)
1.316     misha     980:                                throw Exception(PARSER_RUNTIME,
1.291     paf       981:                                        &name,
1.297     paf       982:                                        "constructor must be declared in class '%s'", 
                    983:                                                ncontext.get_class()->name_cstr());
1.63      paf       984: 
1.330   ! misha     985:        return *(value ? &process_to_value(*value) : VVoid::get());
1.309     paf       986: }
                    987: 
1.329     misha     988: void Request::put_element(Value& ncontext, const String& name, Value* value) {
1.309     paf       989:        // put_element can return property-setting-junction
1.329     misha     990:        if(const VJunction* vjunction=ncontext.put_element(ncontext, name, value, false))
1.312     paf       991:                if(vjunction!=PUT_ELEMENT_REPLACED_ELEMENT) {
1.309     paf       992:                        // process it
1.329     misha     993:                        VMethodFrame frame(vjunction->junction(), method_frame/*caller*/);
                    994:                        int param_count=frame.method_params_count();
                    995: 
1.309     paf       996:                        if(param_count!=1)
1.316     misha     997:                                throw Exception(PARSER_RUNTIME,
1.309     paf       998:                                        0,
                    999:                                        "setter method must have ONE parameter (has %d parameters)", param_count);
                   1000: 
1.329     misha    1001:                        frame.store_params(&value, 1);
1.309     paf      1002:                        frame.set_self(frame.junction.self);
                   1003: 
1.330   ! misha    1004:                        SAVE_CONTEXT
1.309     paf      1005: 
                   1006:                        rcontext=wcontext=&frame;
                   1007:                        method_frame=&frame;
                   1008: 
1.311     paf      1009:                        // prevent non-string writes for better error reporting [setters are not expected to return anything]
1.309     paf      1010:                        wcontext->write(*method_frame);
                   1011: 
                   1012:                        recoursion_checked_execute(*frame.junction.method->parser_code); // parser code, execute it
                   1013:                        // we don't need it StringOrValue result=wcontext->result();
                   1014: 
1.330   ! misha    1015:                        RESTORE_CONTEXT
1.309     paf      1016:                }
1.34      paf      1017: }
1.70      paf      1018: 
1.162     parser   1019: /**    @param intercept_string
1.116     paf      1020:        - true:
                   1021:                they want result=string value, 
                   1022:                possible object result goes to wcontext
                   1023:        - false:
                   1024:                they want any result[string|object]
                   1025:                nothing goes to wcontext.
1.125     paf      1026:                used in @c (expression) params evaluation
1.224     paf      1027: 
                   1028:     using the fact it's either string_ or value_ result requested to speed up checkes
1.116     paf      1029: */
1.229     paf      1030: StringOrValue Request::process(Value& input_value, bool intercept_string) {
1.297     paf      1031:        Junction* junction=input_value.get_junction();
1.307     paf      1032:        if(junction) {
                   1033:                if(junction->is_getter) { // is it a getter-junction?
1.329     misha    1034:                        VMethodFrame frame(*junction, method_frame/*caller*/);
                   1035:                        int param_count=frame.method_params_count();
                   1036: 
1.320     misha    1037:                        if(junction->auto_name){ // default getter
                   1038:                                if(param_count>1)
                   1039:                                        throw Exception(PARSER_RUNTIME,
                   1040:                                                0,
                   1041:                                                "default getter method can't have more then 1 parameter (has %d parameters)", param_count);
                   1042:                        } else if(param_count)
1.316     misha    1043:                                throw Exception(PARSER_RUNTIME,
1.307     paf      1044:                                        0,
1.320     misha    1045:                                        "getter method must have no parameters (has %d parameters)", param_count);
1.307     paf      1046: 
1.329     misha    1047:                        if(junction->auto_name && param_count){
                   1048:                                Value *param=new VString(*junction->auto_name);
                   1049:                                frame.store_params(&param, 1);
1.330   ! misha    1050:                        } else {
        !          1051:                                frame.empty_params();
1.329     misha    1052:                        }
1.320     misha    1053: 
1.307     paf      1054:                        frame.set_self(frame.junction.self);
                   1055: 
1.330   ! misha    1056:                        SAVE_CONTEXT
1.307     paf      1057: 
                   1058:                        rcontext=wcontext=&frame;
                   1059:                        method_frame=&frame;
                   1060: 
                   1061:                        recoursion_checked_execute(*frame.junction.method->parser_code); // parser code, execute it
                   1062:                        StringOrValue result=wcontext->result();
                   1063: 
1.330   ! misha    1064:                        RESTORE_CONTEXT
1.307     paf      1065: 
                   1066:                        return result;
                   1067:                }
                   1068: 
                   1069:                if(junction->code) { // is it a code-junction?
                   1070:                        // process it
                   1071:                        StringOrValue result;
1.157     parser   1072: #ifdef DEBUG_EXECUTE
1.307     paf      1073:                        debug_printf(sapi_info, "ja->\n");
1.157     parser   1074: #endif
1.238     paf      1075: 
1.307     paf      1076:                        if(!junction->method_frame)
1.316     misha    1077:                                throw Exception(PARSER_RUNTIME,
1.240     paf      1078:                                0,
                   1079:                                "junction used outside of context");
                   1080: 
1.330   ! misha    1081:                        SAVE_CONTEXT
1.307     paf      1082: 
                   1083:                        method_frame=junction->method_frame;
                   1084:                        rcontext=junction->rcontext;
                   1085: 
                   1086:                        // for expression method params
                   1087:                        // wcontext is set 0
                   1088:                        // using the fact in decision "which wwrapper to use"
                   1089:                        bool using_code_frame=intercept_string && junction->wcontext;
                   1090:                        if(using_code_frame) {
1.318     misha    1091:                                // almost plain wwrapper about junction wcontext 
                   1092: 
                   1093:                                VCodeFrame local(*junction->wcontext);
1.307     paf      1094:                                wcontext=&local;
                   1095: 
                   1096:                                // execute it
                   1097:                                recoursion_checked_execute(*junction->code);
                   1098: 
                   1099:                                // CodeFrame soul:
1.318     misha    1100:                                result=wcontext->result();
1.307     paf      1101:                        } else {
                   1102:                                // plain wwrapper
                   1103:                                WWrapper local(0/*empty*/, wcontext);
                   1104:                                wcontext=&local;
                   1105: 
                   1106:                                // execute it
                   1107:                                recoursion_checked_execute(*junction->code);
                   1108: 
                   1109:                                result=wcontext->result();
                   1110:                        }
                   1111: 
1.330   ! misha    1112:                        RESTORE_CONTEXT
1.207     paf      1113: 
1.157     parser   1114: #ifdef DEBUG_EXECUTE
1.307     paf      1115:                        debug_printf(sapi_info, "<-ja returned");
1.157     parser   1116: #endif
1.307     paf      1117:                        return result;
                   1118:                }
                   1119: 
                   1120:                // it is then method-junction, do not explode it
                   1121:                // just return it as we do for usual objects
                   1122:        }       
                   1123: 
                   1124:        return input_value;
1.85      paf      1125: }
                   1126: 
1.298     paf      1127: StringOrValue Request::execute_method(VMethodFrame& amethod_frame, const Method& method) {
1.330   ! misha    1128:        SAVE_CONTEXT
1.99      paf      1129:        
                   1130:        // initialize contexts
1.286     paf      1131:        rcontext=wcontext=method_frame=&amethod_frame;
1.99      paf      1132:        
                   1133:        // execute!     
                   1134:        execute(*method.parser_code);
                   1135:        
                   1136:        // result
1.298     paf      1137:        StringOrValue result=wcontext->result();
1.208     paf      1138:        
1.330   ! misha    1139:        RESTORE_CONTEXT
1.242     paf      1140:        
                   1141:        // return
                   1142:        return result;
1.208     paf      1143: }
                   1144: 
1.297     paf      1145: const String* Request::execute_method(Value& aself, 
1.329     misha    1146:                                        const Method& method, Value* optional_param,
                   1147:                                        bool do_return_string) {
1.317     misha    1148: 
1.330   ! misha    1149:        SAVE_CONTEXT
1.208     paf      1150:        
1.307     paf      1151:        Junction local_junction(aself, &method);
1.321     misha    1152:        VMethodFrame local_frame(local_junction, method_frame/*caller*/);
1.329     misha    1153:        if(optional_param && local_frame.method_params_count()>0) {
                   1154:                local_frame.store_params(&optional_param, 1);
1.330   ! misha    1155:        } else {
        !          1156:                local_frame.empty_params();
1.242     paf      1157:        }
1.285     paf      1158:        local_frame.set_self(aself);
1.257     paf      1159:        rcontext=wcontext=method_frame=&local_frame; 
1.246     paf      1160: 
                   1161:        // prevent non-string writes for better error reporting
1.297     paf      1162:        if(do_return_string)
1.246     paf      1163:                wcontext->write(local_frame);
1.208     paf      1164:        
                   1165:        // execute!     
                   1166:        execute(*method.parser_code);
                   1167:        
                   1168:        // result
1.297     paf      1169:        const String* result=0;
                   1170:        if(do_return_string)
                   1171:                result=&wcontext->result().as_string();
1.99      paf      1172:        
1.330   ! misha    1173:        RESTORE_CONTEXT
1.297     paf      1174: 
                   1175:        return result;
1.242     paf      1176: }
                   1177: 
1.297     paf      1178: Request::Execute_nonvirtual_method_result 
                   1179: Request::execute_nonvirtual_method(VStateless_class& aclass, 
1.330   ! misha    1180:                                        const String& method_name,
        !          1181:                                        VString* optional_param,
        !          1182:                                        bool do_return_string) {
1.297     paf      1183:        Execute_nonvirtual_method_result result;
                   1184:        result.method=aclass.get_method(method_name);
                   1185:        if(result.method)
1.317     misha    1186:                result.string=execute_method(aclass, *result.method, optional_param, do_return_string);
1.297     paf      1187:        return result;
1.99      paf      1188: }
                   1189: 
1.297     paf      1190: const String* Request::execute_virtual_method(Value& aself, 
1.330   ! misha    1191:                                        const String& method_name) {
1.297     paf      1192:        if(Value* value=aself.get_element(method_name, aself, false))
                   1193:                if(Junction* junction=value->get_junction())
                   1194:                        if(const Method *method=junction->method) 
                   1195:                                return execute_method(aself, *method, 0/*no params*/, true);
1.188     parser   1196:                        
1.176     parser   1197:        return 0;
                   1198: }

E-mail: