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

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

E-mail: