Annotation of parser3/src/types/pa_vmethod_frame.h, revision 1.141

1.3       paf         1: /** @file
1.5       paf         2:        Parser: @b method_frame write context
1.3       paf         3: 
1.133     moko        4:        Copyright (c) 2001-2024 Art. Lebedev Studio (http://www.artlebedev.com)
1.129     moko        5:        Authors: Konstantin Morshnev <moko@design.ru>, Alexandr Petrosian <paf@design.ru>
1.1       paf         6: */
                      7: 
                      8: #ifndef PA_VMETHOD_FRAME_H
                      9: #define PA_VMETHOD_FRAME_H
1.28      paf        10: 
1.141   ! moko       11: #define IDENT_PA_VMETHOD_FRAME_H "$Id: pa_vmethod_frame.h,v 1.140 2024/12/10 02:40:53 moko Exp $"
1.1       paf        12: 
1.101     moko       13: #include "pa_symbols.h"
1.1       paf        14: #include "pa_wcontext.h"
1.12      parser     15: #include "pa_vvoid.h"
1.1       paf        16: #include "pa_vjunction.h"
                     17: 
1.46      paf        18: // forwards
                     19: 
                     20: class Request;
                     21: 
                     22: /**
                     23:        @b method parameters passed in this array.
                     24:        contains handy typecast ad junction/not junction ensurers
1.114     moko       25: */
1.46      paf        26: 
1.78      misha      27: class MethodParams {
1.46      paf        28: public:
1.130     moko       29:        MethodParams() : felements(0), fsize(0) {}
1.78      misha      30: 
1.82      misha      31: #ifdef USE_DESTRUCTORS
1.109     moko       32:        ~MethodParams() {
1.82      misha      33:                Value **flast=felements+count();
1.95      moko       34:                for(Value **current=felements;current<flast;current++){
                     35:                        Junction* junction=(*current)->get_junction();
                     36:                        if (junction && junction->code)
1.111     moko       37:                                delete (VJunction*)(*current);
1.95      moko       38:                }
1.82      misha      39:        }
                     40: #endif
                     41: 
1.109     moko       42:        void store_params(Value **params, size_t count) {
1.78      misha      43:                felements=params;
1.130     moko       44:                fsize=count;
1.77      misha      45:        }
                     46: 
1.130     moko       47:        inline size_t count() const { return fsize; }
1.78      misha      48: 
1.108     moko       49:        inline Value& get(size_t index) const {
1.78      misha      50:                assert(index<count());
1.108     moko       51:                return *felements[index];
1.78      misha      52:        }
                     53: 
1.108     moko       54:        inline Value& operator[] (size_t index) { return get(index); }
1.46      paf        55: 
                     56:        /// handy is-value-a-junction ensurer
1.109     moko       57:        Value& as_junction(int index, const char* msg) {
1.108     moko       58:                Value& value=get(index);
1.110     moko       59:                if(value.get_junction())
                     60:                        return value;
                     61:                throw Exception(PARSER_RUNTIME, 0, "%s (parameter #%d)", msg, 1+index);
1.46      paf        62:        }
1.107     moko       63: 
1.46      paf        64:        /// handy value-is-not-a-junction ensurer
1.109     moko       65:        Value& as_no_junction(int index, const char* msg) {
1.108     moko       66:                Value& value=get(index);
1.110     moko       67:                if(!value.get_junction())
                     68:                        return value;
                     69:                throw Exception(PARSER_RUNTIME, 0, "%s (parameter #%d)", msg, 1+index);
1.46      paf        70:        }
1.107     moko       71: 
1.62      misha      72:        /// handy is-value-a-junction ensurer or can be auto-processed
1.109     moko       73:        Value& as_expression(int index, const char* msg) {
1.108     moko       74:                Value& value=get(index);
1.110     moko       75:                if(value.is_evaluated_expr())
1.107     moko       76:                        return value;
1.110     moko       77:                if(value.get_junction())
1.107     moko       78:                        return value;
1.110     moko       79:                throw Exception(PARSER_RUNTIME, 0, "%s (parameter #%d)", msg, 1+index);
1.62      misha      80:        }
1.107     moko       81: 
1.46      paf        82:        /// handy expression auto-processing to double
1.107     moko       83:        double as_double(int index, const char* msg, Request& r) {
1.108     moko       84:                Value& value=get(index);
1.107     moko       85:                if(value.is_evaluated_expr())
                     86:                        return value.as_double();
                     87:                return get_processed(value, msg, index, r).as_double();
1.46      paf        88:        }
1.107     moko       89: 
1.46      paf        90:        /// handy expression auto-processing to int
1.107     moko       91:        int as_int(int index, const char* msg, Request& r) {
1.108     moko       92:                Value& value=get(index);
1.107     moko       93:                if(value.is_evaluated_expr())
                     94:                        return value.as_int();
                     95:                return get_processed(value, msg, index, r).as_int();
1.46      paf        96:        }
1.107     moko       97: 
1.132     moko       98:        /// handy param auto-processing to index
                     99:        int as_index(int index, size_t count, Request& r) {
                    100:                if(get(index).is_string()) {
                    101:                        const String& svalue=*get(index).get_string();
                    102:                        if(svalue == "last")
                    103:                                return count-1;
                    104:                        else if(svalue != "first")
                    105:                                throw Exception(PARSER_RUNTIME, &svalue, "index must be 'first', 'last' or expression");
                    106:                        return 0;
                    107:                } else {
                    108:                        int result=as_int(index, "index must be 'first', 'last' or expression", r);
                    109:                        if(result < 0)
                    110:                                result+=count;
                    111:                        return result;
                    112:                }
                    113:        }
                    114: 
1.46      paf       115:        /// handy expression auto-processing to bool
1.107     moko      116:        bool as_bool(int index, const char* msg, Request& r) {
1.108     moko      117:                Value& value=get(index);
1.107     moko      118:                if(value.is_evaluated_expr())
                    119:                        return value.as_bool();
                    120:                return get_processed(value, msg, index, r).as_bool();
1.46      paf       121:        }
1.107     moko      122: 
1.46      paf       123:        /// handy string ensurer
1.109     moko      124:        const String& as_string(int index, const char* msg) {
1.46      paf       125:                return as_no_junction(index, msg).as_string();
                    126:        }
1.107     moko      127: 
1.134     moko      128:        /// handy file name ensurer
1.141   ! moko      129:        const String& as_file_name(int index);
        !           130: 
        !           131:        /// handy file name from string or file
        !           132:        const String& as_file_spec(int index);
1.134     moko      133: 
1.93      misha     134:        /// handy hash ensurers
                    135:        HashStringValue* as_hash(int index, const char* name=0);
1.107     moko      136: 
1.93      misha     137:        /// handy table ensurer
                    138:        Table* as_table(int index, const char* name=0);
                    139: 
1.46      paf       140: private:
                    141: 
1.78      misha     142:        Value **felements;
1.130     moko      143:        size_t fsize;
1.78      misha     144: 
1.107     moko      145:        Value& get_processed(Value& value, const char* msg, int index, Request& r);
1.46      paf       146: 
                    147: };
                    148: 
1.114     moko      149: /**
                    150:        Method frame write context
1.4       paf       151:        accepts values written by method code
1.3       paf       152: */
1.114     moko      153: 
1.46      paf       154: class VMethodFrame: public WContext {
1.63      misha     155: protected:
1.46      paf       156:        VMethodFrame *fcaller;
1.114     moko      157:        Value& fself;
                    158: 
                    159: public: // Value
                    160: 
                    161:        override const char* type() const { return "method_frame"; }
                    162: 
                    163:        /// VMethodFrame: self_transparent
                    164:        override VStateless_class* get_class() { return self().get_class(); }
                    165: 
                    166:        /// VMethodFrame: self_transparent
                    167:        override VStateless_class* base() { return self().base(); }
                    168: 
                    169: public: // usage
                    170: 
                    171:        VMethodFrame(const Method& amethod, VMethodFrame *acaller, Value& aself):
                    172:                WContext(0 /* no parent, junctions can be reattached only up to VMethodFrame */),
                    173:                fcaller(acaller),
                    174:                fself(aself),
                    175:                method(amethod) {
                    176:        }
                    177: 
                    178:        VMethodFrame *caller() { return fcaller; }
                    179: 
                    180:        Value& self() { return fself; }
                    181: 
                    182:        void check_call_type(){
1.115     moko      183:                if(method.call_type==Method::CT_ANY)
                    184:                        return;
                    185: 
1.114     moko      186:                Method::Call_type call_type=self().get_class()==&self() ? Method::CT_STATIC : Method::CT_DYNAMIC;
1.115     moko      187: 
                    188:                 if(method.call_type!=call_type) // call type not allowed?
1.121     moko      189:                        throw Exception(PARSER_RUNTIME, method.name, "method of '%s' is not allowed to be called %s", 
                    190:                                self().type(), call_type==Method::CT_STATIC ? "statically" : "dynamically");
1.114     moko      191:        }
                    192: 
                    193: public:
                    194: 
                    195:        const Method& method;
                    196: };
1.46      paf       197: 
                    198: 
1.114     moko      199: class VNativeMethodFrame: public VMethodFrame {
                    200: protected:
                    201:        MethodParams fnumbered_params;
1.46      paf       202: 
1.114     moko      203: public: // usage
                    204: 
                    205:        VNativeMethodFrame(const Method& amethod, VMethodFrame *acaller, Value& aself) : VMethodFrame(amethod, acaller, aself){}
                    206: 
1.122     moko      207:        // params should be declared outside of *_FRAME_ACTION as MethodParams destructor will be called in ~VNativeMethodFrame
1.114     moko      208:        void store_params(Value **params, size_t count) {
                    209:                fnumbered_params.store_params(params, count);
                    210:                method.check_actual_numbered_params(self(), &fnumbered_params);
                    211:        }
                    212: 
                    213:        void empty_params() {
                    214:                method.check_actual_numbered_params(self(), &fnumbered_params);
                    215:        }
                    216: 
                    217:        void call(Request &r);
                    218: 
                    219: };
                    220: 
                    221: 
                    222: /**
                    223:        Handles named parameters and local variables
                    224: */
                    225: 
                    226: class VParserMethodFrame: public VMethodFrame {
                    227: public: // Value
1.20      paf       228: 
1.114     moko      229:        /// VParserMethodFrame: $result | parent get_string(=accumulated fstring)
1.109     moko      230:        override const String* get_string() {
1.139     moko      231:                // if we have $result, return it's string value, else return as usual: accumulated fstring or fvalue
                    232:                return my_result ? my_result->get_string() : WContext::get_string();
1.20      paf       233:        }
1.114     moko      234: 
                    235:        /// VParserMethodFrame: my or self_transparent or $caller
1.109     moko      236:        override Value* get_element(const String& aname) {
1.139     moko      237:                if(SYMBOLS_EQ(aname,RESULT_SYMBOL))
                    238:                        return my_result ? my_result : VVoid::get();
                    239: 
1.103     moko      240:                if(SYMBOLS_EQ(aname,CALLER_SYMBOL))
1.118     moko      241:                        return get_caller_wrapper();
1.42      paf       242: 
1.103     moko      243:                if(SYMBOLS_EQ(aname,SELF_SYMBOL))
1.67      misha     244:                        return &self();
                    245: 
1.114     moko      246:                if(Value* result=my.get(aname))
1.66      misha     247:                        return result;
                    248: 
1.114     moko      249:                if(Value* result=self().get_element(aname))
1.66      misha     250:                        return result;
1.40      paf       251: 
                    252:                return 0;
1.1       paf       253:        }
                    254: 
1.114     moko      255:        /// VParserMethodFrame: my or self_transparent
1.94      moko      256:        override const VJunction* put_element(const String& aname, Value* avalue) {
1.139     moko      257:                if(SYMBOLS_EQ(aname,RESULT_SYMBOL)){
                    258:                        my_result=avalue;
                    259: #ifdef OPTIMIZE_RESULT
1.140     moko      260:                        // This check is only for maintaining consistency. Even if we change the mode from RO_USE_WCONTEXT, it will not work because
                    261:                        // CO_WITHOUT_WCONTEXT is also active. In this mode writes go directly to the parent context and result() is never called.
                    262:                        // If we also change CO_WITHOUT_WCONTEXT, the current method call will still continue writing to the parent context and
                    263:                        // result() will not be called. Only subsequent method calls will use $result.
                    264:                        // The current behavior is more predictable: the first method call determines result_optimization, and subsequent calls never change it.
                    265:                        if(method.result_optimization==Method::RO_UNKNOWN)
                    266:                                ((Method *)&method)->result_optimization=Method::RO_USE_RESULT;
1.139     moko      267: #endif
                    268:                        return 0;
                    269:                }
1.114     moko      270:                if(my.put_replaced(aname, avalue))
1.126     moko      271:                        return 0;
1.114     moko      272:                return self().put_element(aname, avalue);
1.64      misha     273:        }
                    274: 
1.114     moko      275: public: // WContext
                    276: 
                    277:        /// VParserMethodFrame: skip write when RO_USE_RESULT
1.112     moko      278:        override void write(const String& astring) {
1.79      misha     279: #ifdef OPTIMIZE_RESULT
1.125     moko      280:                if(method.result_optimization != Method::RO_USE_RESULT)
1.79      misha     281: #endif
1.125     moko      282:                        WContext::write(astring);
1.69      misha     283:        }
                    284: 
1.114     moko      285:        override void write(Value& avalue) {
                    286:                WContext::write(avalue);
1.64      misha     287:        }
                    288: 
1.119     moko      289:        override ValueRef result() {
1.139     moko      290:                // if we have $result, return it, else return as usual: accumulated fstring or fvalue
                    291:                if(my_result)
                    292:                        return my_result;
1.79      misha     293: #ifdef OPTIMIZE_RESULT
1.114     moko      294:                if(method.result_optimization==Method::RO_USE_RESULT)
1.120     moko      295:                        return VVoid::get();
1.140     moko      296: 
                    297:                // Due to call optimization, the following code is called only once.
                    298:                // CO_WITHOUT_WCONTEXT means subsequent writes will be to the parent context, and the result() won't be called.
                    299: 
1.114     moko      300:                ((Method *)&method)->result_optimization=Method::RO_USE_WCONTEXT;
1.79      misha     301: #ifdef OPTIMIZE_CALL // nested as CO_WITHOUT_WCONTEXT assumes that $result not used
1.114     moko      302:                ((Method *)&method)->call_optimization=Method::CO_WITHOUT_WCONTEXT;
1.79      misha     303: #endif
                    304: #endif
                    305:                return WContext::result();
1.1       paf       306:        }
                    307: 
                    308: public: // usage
                    309: 
1.123     moko      310:        HashString<Value*> my; // public for ^stack[]
1.139     moko      311:        Value* my_result;
1.123     moko      312: 
1.114     moko      313:        VParserMethodFrame(const Method& amethod, VMethodFrame *acaller, Value& aself);
1.1       paf       314: 
1.114     moko      315:        void store_params(Value **params, size_t count) {
                    316:                size_t param_count=method.params_count;
                    317:                size_t i=0;
1.25      paf       318: 
1.114     moko      319:                if(count>param_count){
1.136     moko      320: 
                    321:                        for(; i<param_count; i++) {
                    322:                                const String& fname=*(*method.params_names)[i];
1.138     moko      323:                                set_my_variable(fname, params[i]);
1.136     moko      324:                        }
                    325: 
1.114     moko      326:                        if(method.extra_params){
                    327:                                VHash& vargs=*new VHash();
                    328:                                HashStringValue& args = vargs.hash();
1.1       paf       329: 
1.114     moko      330:                                for(; i<count; i++) {
1.131     moko      331:                                        args.put(pa_uitoa(args.count()), params[i]);
1.114     moko      332:                                }
1.77      misha     333: 
1.138     moko      334:                                set_my_variable(*method.extra_params, &vargs);
1.136     moko      335:                        } else if(method.named_params){
                    336:                                if(count!=param_count+1)
                    337:                                        throw Exception(PARSER_RUNTIME, method.name, "method of '%s' accepts maximum %d parameter(s) (%d present)", self().type(), param_count+1, count);
                    338: 
                    339:                                HashStringValue* named_args = params[i]->as_hash("named parameter");
                    340:                                size_t named_count=method.named_params->count();
                    341:                                for(i=0; i<named_count; i++) {
                    342:                                        const String& fname=*(*method.named_params)[i];
                    343:                                        Value *arg=named_args ? named_args->get(fname) : NULL;
1.138     moko      344:                                        set_my_variable(fname, arg ? arg : VVoid::get());
1.136     moko      345:                                }
1.114     moko      346:                        } else
1.121     moko      347:                                throw Exception(PARSER_RUNTIME, method.name, "method of '%s' accepts maximum %d parameter(s) (%d present)", self().type(), param_count, count);
1.135     moko      348:                } else {
1.136     moko      349: 
                    350:                        for(; i<count; i++) {
                    351:                                const String& fname=*(*method.params_names)[i];
1.138     moko      352:                                set_my_variable(fname, params[i]);
1.136     moko      353:                        }
                    354: 
                    355:                        for(; i<param_count; i++) {
                    356:                                const String& fname=*(*method.params_names)[i];
1.138     moko      357:                                set_my_variable(fname, VVoid::get());
1.136     moko      358:                        }
                    359: 
1.135     moko      360:                        if(method.extra_params){
1.138     moko      361:                                set_my_variable(*method.extra_params, VVoid::get());
1.136     moko      362:                        } else if(method.named_params){
                    363:                                size_t named_count=method.named_params->count();
                    364:                                for(i=0; i<named_count; i++) {
                    365:                                        const String& fname=*(*method.named_params)[i];
1.138     moko      366:                                        set_my_variable(fname, VVoid::get());
1.136     moko      367:                                }
1.135     moko      368:                        }
1.114     moko      369:                }
1.1       paf       370:        }
                    371: 
1.78      misha     372:        void empty_params(){
1.114     moko      373:                size_t param_count=method.params_count;
                    374:                if(param_count>0){
1.138     moko      375:                        set_my_variable(*(*method.params_names)[0], VString::empty());
1.114     moko      376:                        for(size_t i=1; i<param_count; i++)
1.138     moko      377:                                set_my_variable(*(*method.params_names)[i], VVoid::get());
1.78      misha     378:                }
1.137     moko      379:                if(method.extra_params){
1.138     moko      380:                        set_my_variable(*method.extra_params, VVoid::get());
1.137     moko      381:                } else if(method.named_params){
                    382:                        size_t named_count=method.named_params->count();
                    383:                        for(int i=0; i<named_count; i++) {
                    384:                                const String& fname=*(*method.named_params)[i];
1.138     moko      385:                                set_my_variable(fname, VVoid::get());
1.137     moko      386:                        }
                    387:                }
1.78      misha     388:        }
                    389: 
1.114     moko      390:        void call(Request &r);
1.18      paf       391: 
1.63      misha     392: protected:
1.18      paf       393: 
1.139     moko      394:        inline void set_my_variable(const String& aname, Value* value) {
                    395:                if(SYMBOLS_EQ(aname,RESULT_SYMBOL)){
                    396:                        my_result=value;
                    397: #ifdef OPTIMIZE_RESULT
                    398:                        ((Method *)&method)->result_optimization=Method::RO_USE_RESULT;
                    399: #endif
                    400:                        return;
                    401:                }
                    402:                my.put(aname, value); // remember param
1.25      paf       403:        }
                    404: 
1.118     moko      405:        Value* get_caller_wrapper();
                    406: 
1.114     moko      407: };
                    408: 
                    409: 
                    410: class VLocalParserMethodFrame: public VParserMethodFrame {
                    411: public: // Value
                    412: 
                    413:        override const VJunction* put_element(const String& aname, Value* avalue){
1.138     moko      414:                set_my_variable(aname, avalue);
1.126     moko      415:                return 0;
1.114     moko      416:        }
                    417: 
                    418: public: // usage
                    419: 
                    420:        VLocalParserMethodFrame(const Method& amethod, VMethodFrame *acaller, Value& aself) : VParserMethodFrame(amethod, acaller, aself) {}
1.1       paf       421: 
1.41      paf       422: };
                    423: 
1.114     moko      424: 
                    425: template<typename Parent> class VExpressionFrame: public Parent {
1.113     moko      426: public:
1.114     moko      427:        VExpressionFrame(const Method& amethod, VMethodFrame *acaller, Value& aself) : Parent(amethod, acaller, aself) {}
1.113     moko      428: 
1.117     moko      429:        /// in expressions only strings are written as strings
1.113     moko      430:        override void write_as_string(Value& avalue) {
1.117     moko      431:                if(avalue.is_string())
                    432:                        Parent::write(*avalue.get_string());
                    433:                else
                    434:                        Parent::write(avalue);
1.113     moko      435:        }
                    436: };
                    437: 
                    438: 
1.114     moko      439: template<typename Parent> class VConstructorFrame: public Parent {
1.85      misha     440: public:
1.114     moko      441:        VConstructorFrame(const Method& amethod, VMethodFrame *acaller, Value& aself) : Parent(amethod, acaller, aself) {
1.87      moko      442:                // prevent non-string writes for better error reporting [constructors are not expected to return anything]
                    443:                VMethodFrame::write(aself);
                    444:        }
1.85      misha     445: 
1.113     moko      446:        override void write(const String& /*astring*/) {}
1.85      misha     447: };
                    448: 
1.114     moko      449: 
                    450: #define METHOD_FRAME_ACTION(method, caller, self, action)                                      \
                    451:        if((method).native_code){                                                               \
                    452:                VNativeMethodFrame frame(method, caller, self);                                 \
                    453:                action;                                                                         \
                    454:        } else {                                                                                \
                    455:                if((method).all_vars_local){                                                    \
                    456:                        VLocalParserMethodFrame frame(method, caller, self);                    \
                    457:                        action;                                                                 \
                    458:                } else {                                                                        \
                    459:                        VParserMethodFrame frame(method, caller, self);                         \
                    460:                        action;                                                                 \
                    461:                }                                                                               \
                    462:        }
                    463: 
                    464: #define EXPRESSION_FRAME_ACTION(method, caller, self, action)                                  \
                    465:        if((method).native_code){                                                               \
                    466:                VExpressionFrame<VNativeMethodFrame> frame(method, caller, self);               \
                    467:                action;                                                                         \
                    468:        } else {                                                                                \
                    469:                if((method).all_vars_local){                                                    \
1.128     moko      470:                        VLocalParserMethodFrame frame(method, caller, self);                    \
1.114     moko      471:                        action;                                                                 \
                    472:                } else {                                                                        \
1.128     moko      473:                        VParserMethodFrame frame(method, caller, self);                         \
1.114     moko      474:                        action;                                                                 \
                    475:                }                                                                               \
                    476:        }
                    477: 
                    478: #define CONSTRUCTOR_FRAME_ACTION(method, caller, self, action)                                 \
                    479:        if((method).native_code){                                                               \
                    480:                VConstructorFrame<VNativeMethodFrame> frame(method, caller, self);              \
                    481:                action;                                                                         \
                    482:        } else {                                                                                \
                    483:                if((method).all_vars_local){                                                    \
                    484:                        VConstructorFrame<VLocalParserMethodFrame> frame(method, caller, self); \
                    485:                        action;                                                                 \
                    486:                } else {                                                                        \
                    487:                        VConstructorFrame<VParserMethodFrame> frame(method, caller, self);      \
                    488:                        action;                                                                 \
                    489:                }                                                                               \
                    490:        }
                    491: 
1.1       paf       492: #endif

E-mail: