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

1.3       paf         1: /** @file
1.5       paf         2:        Parser: @b method_frame write context
1.3       paf         3: 
1.98      moko        4:        Copyright (c) 2001-2015 Art. Lebedev Studio (http://www.artlebedev.com)
1.22      paf         5:        Author: Alexandr Petrosian <paf@design.ru> (http://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.120   ! moko       11: #define IDENT_PA_VMETHOD_FRAME_H "$Id: pa_vmethod_frame.h,v 1.119 2016/11/29 15:20:18 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.109     moko       29:        MethodParams() : felements(0), fused(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.77      misha      44:                fused=count;
                     45:        }
                     46: 
1.78      misha      47:        inline size_t count() const { return fused; }
                     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.46      paf        98:        /// handy expression auto-processing to bool
1.107     moko       99:        bool as_bool(int index, const char* msg, Request& r) {
1.108     moko      100:                Value& value=get(index);
1.107     moko      101:                if(value.is_evaluated_expr())
                    102:                        return value.as_bool();
                    103:                return get_processed(value, msg, index, r).as_bool();
1.46      paf       104:        }
1.107     moko      105: 
1.46      paf       106:        /// handy string ensurer
1.109     moko      107:        const String& as_string(int index, const char* msg) {
1.46      paf       108:                return as_no_junction(index, msg).as_string();
                    109:        }
1.107     moko      110: 
1.93      misha     111:        /// handy hash ensurers
                    112:        HashStringValue* as_hash(int index, const char* name=0);
1.107     moko      113: 
1.93      misha     114:        /// handy table ensurer
                    115:        Table* as_table(int index, const char* name=0);
                    116: 
1.46      paf       117: private:
                    118: 
1.78      misha     119:        Value **felements;
                    120:        size_t fused;
                    121: 
1.107     moko      122:        Value& get_processed(Value& value, const char* msg, int index, Request& r);
1.46      paf       123: 
                    124: };
                    125: 
1.114     moko      126: /**
                    127:        Method frame write context
1.4       paf       128:        accepts values written by method code
1.3       paf       129: */
1.114     moko      130: 
1.46      paf       131: class VMethodFrame: public WContext {
1.63      misha     132: protected:
1.46      paf       133:        VMethodFrame *fcaller;
1.114     moko      134:        Value& fself;
                    135: 
                    136: public: // Value
                    137: 
                    138:        override const char* type() const { return "method_frame"; }
                    139: 
                    140:        /// VMethodFrame: self_transparent
                    141:        override VStateless_class* get_class() { return self().get_class(); }
                    142: 
                    143:        /// VMethodFrame: self_transparent
                    144:        override VStateless_class* base() { return self().base(); }
                    145: 
                    146: public: // usage
                    147: 
                    148:        VMethodFrame(const Method& amethod, VMethodFrame *acaller, Value& aself):
                    149:                WContext(0 /* no parent, junctions can be reattached only up to VMethodFrame */),
                    150:                fcaller(acaller),
                    151:                fself(aself),
                    152:                method(amethod) {
                    153:        }
                    154: 
                    155:        VMethodFrame *caller() { return fcaller; }
                    156: 
                    157:        Value& self() { return fself; }
                    158: 
                    159:        void check_call_type(){
1.115     moko      160:                if(method.call_type==Method::CT_ANY)
                    161:                        return;
                    162: 
1.114     moko      163:                Method::Call_type call_type=self().get_class()==&self() ? Method::CT_STATIC : Method::CT_DYNAMIC;
1.115     moko      164: 
                    165:                 if(method.call_type!=call_type) // call type not allowed?
1.114     moko      166:                        throw Exception(PARSER_RUNTIME, 0, "is not allowed to be called %s", call_type==Method::CT_STATIC ? "statically" : "dynamically");
                    167:        }
                    168: 
                    169: public:
                    170: 
                    171:        const Method& method;
                    172: };
1.46      paf       173: 
                    174: 
1.114     moko      175: class VNativeMethodFrame: public VMethodFrame {
                    176: protected:
                    177:        MethodParams fnumbered_params;
1.46      paf       178: 
1.114     moko      179: public: // usage
                    180: 
                    181:        VNativeMethodFrame(const Method& amethod, VMethodFrame *acaller, Value& aself) : VMethodFrame(amethod, acaller, aself){}
                    182: 
                    183:        void store_params(Value **params, size_t count) {
                    184:                fnumbered_params.store_params(params, count);
                    185:                method.check_actual_numbered_params(self(), &fnumbered_params);
                    186:        }
                    187: 
                    188:        void empty_params() {
                    189:                method.check_actual_numbered_params(self(), &fnumbered_params);
                    190:        }
                    191: 
                    192:        void call(Request &r);
                    193: 
                    194: };
                    195: 
                    196: 
                    197: /**
                    198:        Handles named parameters and local variables
                    199: */
                    200: 
                    201: class VParserMethodFrame: public VMethodFrame {
                    202: protected:
                    203:        HashString<Value*> my;
                    204: 
                    205: public: // Value
1.20      paf       206: 
1.114     moko      207:        /// VParserMethodFrame: $result | parent get_string(=accumulated fstring)
1.109     moko      208:        override const String* get_string() {
1.20      paf       209:                // check the $result value
1.46      paf       210:                Value* result=get_result_variable();
1.20      paf       211:                // if we have one, return it's string value, else return as usual: accumulated fstring or fvalue
                    212:                return result ? result->get_string() : WContext::get_string();
                    213:        }
1.114     moko      214: 
                    215:        /// VParserMethodFrame: my or self_transparent or $caller
1.109     moko      216:        override Value* get_element(const String& aname) {
1.103     moko      217:                if(SYMBOLS_EQ(aname,CALLER_SYMBOL))
1.118     moko      218:                        return get_caller_wrapper();
1.42      paf       219: 
1.103     moko      220:                if(SYMBOLS_EQ(aname,SELF_SYMBOL))
1.67      misha     221:                        return &self();
                    222: 
1.114     moko      223:                if(Value* result=my.get(aname))
1.66      misha     224:                        return result;
                    225: 
1.114     moko      226:                if(Value* result=self().get_element(aname))
1.66      misha     227:                        return result;
1.40      paf       228: 
                    229:                return 0;
1.1       paf       230:        }
                    231: 
1.114     moko      232:        /// VParserMethodFrame: my or self_transparent
1.94      moko      233:        override const VJunction* put_element(const String& aname, Value* avalue) {
1.114     moko      234:                if(my.put_replaced(aname, avalue))
                    235:                        return PUT_ELEMENT_REPLACED_ELEMENT;
                    236:                return self().put_element(aname, avalue);
1.64      misha     237:        }
                    238: 
1.114     moko      239: public: // WContext
                    240: 
                    241:        /// VParserMethodFrame: skip write when RO_USE_RESULT
1.112     moko      242:        override void write(const String& astring) {
1.79      misha     243: #ifdef OPTIMIZE_RESULT
1.87      moko      244:                switch (method.result_optimization){
1.97      moko      245:                        case Method::RO_USE_RESULT:
1.79      misha     246:                                return;
                    247:                        case Method::RO_UNKNOWN:
                    248:                                if(get_result_variable()){
1.87      moko      249:                                        ((Method *)&method)->result_optimization=Method::RO_USE_RESULT;
1.79      misha     250:                                        return;
                    251:                                }
1.97      moko      252:                        case Method::RO_USE_WCONTEXT: break;
1.69      misha     253:                }
1.79      misha     254: #endif
1.112     moko      255:                WContext::write(astring);
1.69      misha     256:        }
                    257: 
1.114     moko      258:        override void write(Value& avalue) {
                    259:                WContext::write(avalue);
1.64      misha     260:        }
                    261: 
1.119     moko      262:        override ValueRef result() {
1.114     moko      263:                // check the $result value
                    264:                Value* result_value=get_result_variable();
                    265:                // if we have one, return it, else return as usual: accumulated fstring or fvalue
                    266:                if(result_value)
1.120   ! moko      267:                        return result_value;
1.79      misha     268: #ifdef OPTIMIZE_RESULT
1.114     moko      269:                if(method.result_optimization==Method::RO_USE_RESULT)
1.120   ! moko      270:                        return VVoid::get();
1.114     moko      271:                ((Method *)&method)->result_optimization=Method::RO_USE_WCONTEXT;
1.79      misha     272: #ifdef OPTIMIZE_CALL // nested as CO_WITHOUT_WCONTEXT assumes that $result not used
1.114     moko      273:                ((Method *)&method)->call_optimization=Method::CO_WITHOUT_WCONTEXT;
1.79      misha     274: #endif
                    275: #endif
                    276:                return WContext::result();
1.1       paf       277:        }
                    278: 
                    279: public: // usage
                    280: 
1.114     moko      281:        VParserMethodFrame(const Method& amethod, VMethodFrame *acaller, Value& aself);
1.1       paf       282: 
1.114     moko      283:        void store_params(Value **params, size_t count) {
                    284:                size_t param_count=method.params_count;
                    285:                size_t i=0;
1.25      paf       286: 
1.114     moko      287:                if(count>param_count){
                    288:                        if(method.extra_params){
                    289:                                for(; i<param_count; i++) {
                    290:                                        const String& fname=*(*method.params_names)[i];
                    291:                                        set_my_variable(fname, *params[i]);
                    292:                                }
1.75      misha     293: 
1.114     moko      294:                                VHash& vargs=*new VHash();
                    295:                                HashStringValue& args = vargs.hash();
1.1       paf       296: 
1.114     moko      297:                                for(; i<count; i++) {
                    298:                                        args.put(format(args.count(), 0), params[i]);
                    299:                                }
1.77      misha     300: 
1.114     moko      301:                                set_my_variable(*method.extra_params, vargs);
                    302:                                return;
                    303:                        } else
                    304:                                throw Exception(PARSER_RUNTIME, 0, "method of %s accepts maximum %d parameter(s) (%d present)", self().type(), param_count, count);
                    305:                }
1.86      misha     306: 
1.114     moko      307:                for(; i<count; i++) {
                    308:                        const String& fname=*(*method.params_names)[i];
                    309:                        set_my_variable(fname, *params[i]);
                    310:                }
1.78      misha     311: 
1.114     moko      312:                for(; i<param_count; i++) {
                    313:                        const String& fname=*(*method.params_names)[i];
                    314:                        my.put(fname, VVoid::get());
                    315:                }
1.1       paf       316:        }
                    317: 
1.78      misha     318:        void empty_params(){
1.114     moko      319:                size_t param_count=method.params_count;
                    320:                if(param_count>0){
                    321:                        my.put(*(*method.params_names)[0], VString::empty());
                    322:                        for(size_t i=1; i<param_count; i++)
                    323:                                my.put(*(*method.params_names)[i], VVoid::get());
1.78      misha     324:                }
                    325:        }
                    326: 
1.114     moko      327:        void call(Request &r);
1.18      paf       328: 
1.63      misha     329: protected:
1.18      paf       330: 
1.46      paf       331:        void set_my_variable(const String& fname, Value& value) {
1.114     moko      332:                my.put(fname, &value); // remember param
1.25      paf       333:        }
                    334: 
1.46      paf       335:        Value* get_result_variable();
1.1       paf       336: 
1.118     moko      337:        Value* get_caller_wrapper();
                    338: 
1.114     moko      339: };
                    340: 
                    341: 
                    342: class VLocalParserMethodFrame: public VParserMethodFrame {
                    343: public: // Value
                    344: 
                    345:        override const VJunction* put_element(const String& aname, Value* avalue){
                    346:                set_my_variable(aname, *avalue);
                    347:                return PUT_ELEMENT_REPLACED_ELEMENT;
                    348:        }
                    349: 
                    350: public: // usage
                    351: 
                    352:        VLocalParserMethodFrame(const Method& amethod, VMethodFrame *acaller, Value& aself) : VParserMethodFrame(amethod, acaller, aself) {}
1.1       paf       353: 
1.41      paf       354: };
                    355: 
1.114     moko      356: 
                    357: template<typename Parent> class VExpressionFrame: public Parent {
1.113     moko      358: public:
1.114     moko      359:        VExpressionFrame(const Method& amethod, VMethodFrame *acaller, Value& aself) : Parent(amethod, acaller, aself) {}
1.113     moko      360: 
1.117     moko      361:        /// in expressions only strings are written as strings
1.113     moko      362:        override void write_as_string(Value& avalue) {
1.117     moko      363:                if(avalue.is_string())
                    364:                        Parent::write(*avalue.get_string());
                    365:                else
                    366:                        Parent::write(avalue);
1.113     moko      367:        }
                    368: };
                    369: 
                    370: 
1.114     moko      371: template<typename Parent> class VConstructorFrame: public Parent {
1.85      misha     372: public:
1.114     moko      373:        VConstructorFrame(const Method& amethod, VMethodFrame *acaller, Value& aself) : Parent(amethod, acaller, aself) {
1.87      moko      374:                // prevent non-string writes for better error reporting [constructors are not expected to return anything]
                    375:                VMethodFrame::write(aself);
                    376:        }
1.85      misha     377: 
1.113     moko      378:        override void write(const String& /*astring*/) {}
1.85      misha     379: };
                    380: 
1.114     moko      381: 
                    382: #define METHOD_FRAME_ACTION(method, caller, self, action)                                      \
                    383:        if((method).native_code){                                                               \
                    384:                VNativeMethodFrame frame(method, caller, self);                                 \
                    385:                action;                                                                         \
                    386:        } else {                                                                                \
                    387:                if((method).all_vars_local){                                                    \
                    388:                        VLocalParserMethodFrame frame(method, caller, self);                    \
                    389:                        action;                                                                 \
                    390:                } else {                                                                        \
                    391:                        VParserMethodFrame frame(method, caller, self);                         \
                    392:                        action;                                                                 \
                    393:                }                                                                               \
                    394:        }
                    395: 
                    396: #define EXPRESSION_FRAME_ACTION(method, caller, self, action)                                  \
                    397:        if((method).native_code){                                                               \
                    398:                VExpressionFrame<VNativeMethodFrame> frame(method, caller, self);               \
                    399:                action;                                                                         \
                    400:        } else {                                                                                \
                    401:                if((method).all_vars_local){                                                    \
                    402:                        VExpressionFrame<VLocalParserMethodFrame> frame(method, caller, self);  \
                    403:                        action;                                                                 \
                    404:                } else {                                                                        \
                    405:                        VExpressionFrame<VParserMethodFrame> frame(method, caller, self);       \
                    406:                        action;                                                                 \
                    407:                }                                                                               \
                    408:        }
                    409: 
                    410: #define CONSTRUCTOR_FRAME_ACTION(method, caller, self, action)                                 \
                    411:        if((method).native_code){                                                               \
                    412:                VConstructorFrame<VNativeMethodFrame> frame(method, caller, self);              \
                    413:                action;                                                                         \
                    414:        } else {                                                                                \
                    415:                if((method).all_vars_local){                                                    \
                    416:                        VConstructorFrame<VLocalParserMethodFrame> frame(method, caller, self); \
                    417:                        action;                                                                 \
                    418:                } else {                                                                        \
                    419:                        VConstructorFrame<VParserMethodFrame> frame(method, caller, self);      \
                    420:                        action;                                                                 \
                    421:                }                                                                               \
                    422:        }
                    423: 
1.1       paf       424: #endif

E-mail: