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

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.109   ! moko       11: #define IDENT_PA_VMETHOD_FRAME_H "$Id: pa_vmethod_frame.h,v 1.108 2016/10/06 19:41:36 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
                     25: 
                     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.82      misha      37:                                delete *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.107     moko       59:                if(!value.get_junction())
                     60:                        throw Exception(PARSER_RUNTIME, 0, "%s (parameter #%d)", msg, 1+index);
                     61:                return value;
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.107     moko       67:                if(value.get_junction())
                     68:                        throw Exception(PARSER_RUNTIME, 0, "%s (parameter #%d)", msg, 1+index);
                     69:                return value;
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.107     moko       75:                if(value.is_evaluated_expr()){
                     76:                        return value;
1.62      misha      77:                } else {
1.107     moko       78:                        if(!value.get_junction())
                     79:                                throw Exception(PARSER_RUNTIME, 0, "%s (parameter #%d)", msg, 1+index);
                     80:                        return value;
1.62      misha      81:                }
                     82:        }
1.107     moko       83: 
1.46      paf        84:        /// handy expression auto-processing to double
1.107     moko       85:        double as_double(int index, const char* msg, Request& r) {
1.108     moko       86:                Value& value=get(index);
1.107     moko       87:                if(value.is_evaluated_expr())
                     88:                        return value.as_double();
                     89:                return get_processed(value, msg, index, r).as_double();
1.46      paf        90:        }
1.107     moko       91: 
1.46      paf        92:        /// handy expression auto-processing to int
1.107     moko       93:        int as_int(int index, const char* msg, Request& r) {
1.108     moko       94:                Value& value=get(index);
1.107     moko       95:                if(value.is_evaluated_expr())
                     96:                        return value.as_int();
                     97:                return get_processed(value, msg, index, r).as_int();
1.46      paf        98:        }
1.107     moko       99: 
1.46      paf       100:        /// handy expression auto-processing to bool
1.107     moko      101:        bool as_bool(int index, const char* msg, Request& r) {
1.108     moko      102:                Value& value=get(index);
1.107     moko      103:                if(value.is_evaluated_expr())
                    104:                        return value.as_bool();
                    105:                return get_processed(value, msg, index, r).as_bool();
1.46      paf       106:        }
1.107     moko      107: 
1.46      paf       108:        /// handy string ensurer
1.109   ! moko      109:        const String& as_string(int index, const char* msg) {
1.46      paf       110:                return as_no_junction(index, msg).as_string();
                    111:        }
1.107     moko      112: 
1.93      misha     113:        /// handy hash ensurers
                    114:        HashStringValue* as_hash(int index, const char* name=0);
1.107     moko      115: 
1.93      misha     116:        /// handy table ensurer
                    117:        Table* as_table(int index, const char* name=0);
                    118: 
1.46      paf       119: private:
                    120: 
1.78      misha     121:        Value **felements;
                    122:        size_t fused;
                    123: 
1.107     moko      124:        Value& get_processed(Value& value, const char* msg, int index, Request& r);
1.46      paf       125: 
                    126: };
                    127: 
1.4       paf       128: /**    Method frame write context
                    129:        accepts values written by method code
                    130:        also handles method parameters and local variables
1.3       paf       131: */
1.46      paf       132: class VMethodFrame: public WContext {
1.63      misha     133: protected:
1.46      paf       134:        VMethodFrame *fcaller;
                    135: 
1.83      misha     136:        HashString<Value*>* my; /*OR*/ MethodParams fnumbered_params;
1.87      moko      137:        Value& fself;
1.46      paf       138: 
1.64      misha     139:        typedef const VJunction* (VMethodFrame::*put_element_t)(const String& aname, Value* avalue);
                    140:        put_element_t put_element_impl;
1.46      paf       141: 
1.1       paf       142: public: // Value
                    143: 
1.46      paf       144:        override const char* type() const { return "method_frame"; }
1.20      paf       145: 
                    146:        /// VMethodFrame: $result | parent get_string(=accumulated fstring)
1.109   ! moko      147:        override const String* get_string() {
1.20      paf       148:                // check the $result value
1.46      paf       149:                Value* result=get_result_variable();
1.20      paf       150:                // if we have one, return it's string value, else return as usual: accumulated fstring or fvalue
                    151:                return result ? result->get_string() : WContext::get_string();
                    152:        }
                    153:        
1.40      paf       154:        /// VMethodFrame: my or self_transparent or $caller
1.109   ! moko      155:        override Value* get_element(const String& aname) {
1.103     moko      156:                if(SYMBOLS_EQ(aname,CALLER_SYMBOL))
1.40      paf       157:                        return caller();
1.42      paf       158: 
1.103     moko      159:                if(SYMBOLS_EQ(aname,SELF_SYMBOL))
1.67      misha     160:                        return &self();
                    161: 
1.66      misha     162:                Value* result;
                    163:                if(my && (result=my->get(aname)))
                    164:                        return result;
                    165: 
1.84      misha     166:                if(result=self().get_element(aname))
1.66      misha     167:                        return result;
1.40      paf       168: 
                    169:                return 0;
1.1       paf       170:        }
                    171: 
1.4       paf       172:        /// VMethodFrame: self_transparent
1.46      paf       173:        override VStateless_class* get_class() { return self().get_class(); }
1.44      paf       174: 
                    175:        /// VMethodFrame: self_transparent
1.85      misha     176:        override VStateless_class* base() { return self().base(); }
1.1       paf       177: 
1.64      misha     178:        /// VMethodFrame: my or self_transparent
1.94      moko      179:        override const VJunction* put_element(const String& aname, Value* avalue) {
1.64      misha     180:                return (this->*put_element_impl)(aname, avalue);
                    181:        }
                    182: 
1.69      misha     183:        /// VMethodFrame: appends a fstring to result
                    184:        override void write(const String& astring, String::Language alang) {
1.79      misha     185: #ifdef OPTIMIZE_RESULT
1.87      moko      186:                switch (method.result_optimization){
1.97      moko      187:                        case Method::RO_USE_RESULT:
1.79      misha     188:                                return;
                    189:                        case Method::RO_UNKNOWN:
                    190:                                if(get_result_variable()){
1.87      moko      191:                                        ((Method *)&method)->result_optimization=Method::RO_USE_RESULT;
1.79      misha     192:                                        return;
                    193:                                }
1.97      moko      194:                        case Method::RO_USE_WCONTEXT: break;
1.69      misha     195:                }
1.79      misha     196: #endif
                    197:                WContext::write(astring, alang);
1.69      misha     198:        }
                    199: 
1.64      misha     200: private:
                    201: 
                    202:        const VJunction* put_element_local(const String& aname, Value* avalue){
                    203:                set_my_variable(aname, *avalue);
                    204:                return PUT_ELEMENT_REPLACED_ELEMENT;
                    205:        }
                    206: 
                    207:        const VJunction* put_element_global(const String& aname, Value* avalue){
                    208:                if(my && my->put_replaced(aname, avalue))
                    209:                        return PUT_ELEMENT_REPLACED_ELEMENT;
1.94      moko      210:                return self().put_element(aname, avalue);
1.64      misha     211:        }
                    212: 
1.30      paf       213: public: // WContext
1.1       paf       214: 
1.105     moko      215:        override Value& result() {
1.80      misha     216:                if(my){
                    217:                        // check the $result value
                    218:                        Value* result_value=get_result_variable();
                    219:                        // if we have one, return it, else return as usual: accumulated fstring or fvalue
                    220:                        if(result_value)
1.105     moko      221:                                return *result_value;
1.79      misha     222: #ifdef OPTIMIZE_RESULT
1.87      moko      223:                        if(method.result_optimization==Method::RO_USE_RESULT)
1.105     moko      224:                                return *VVoid::get();
1.87      moko      225:                        ((Method *)&method)->result_optimization=Method::RO_USE_WCONTEXT;
1.79      misha     226: #ifdef OPTIMIZE_CALL // nested as CO_WITHOUT_WCONTEXT assumes that $result not used
1.87      moko      227:                        ((Method *)&method)->call_optimization=Method::CO_WITHOUT_WCONTEXT;
1.79      misha     228: #endif
                    229: #endif
1.80      misha     230:                }
1.79      misha     231:                return WContext::result();
1.1       paf       232:        }
                    233: 
1.85      misha     234:        override void write(Value& avalue) {
                    235:                WContext::write(avalue);
1.46      paf       236:        }
                    237: 
1.1       paf       238: public: // usage
                    239: 
1.87      moko      240:        VMethodFrame(const Method& amethod, VMethodFrame *acaller, Value& aself);
1.1       paf       241: 
1.40      paf       242:        VMethodFrame *caller() { return fcaller; }
1.25      paf       243: 
1.76      misha     244: #ifdef USE_DESTRUCTORS
1.75      misha     245:        ~VMethodFrame(){
                    246:                if(my){
                    247:                        delete my;
                    248:                }
                    249:        }
1.76      misha     250: #endif
1.75      misha     251: 
1.87      moko      252:        Value& self() { return fself; }
1.1       paf       253: 
1.77      misha     254:        size_t method_params_count() {
                    255:                return method.params_names ? method.params_names->count():0;
1.27      paf       256:        }
1.77      misha     257: 
                    258:        void store_params(Value **params, size_t count) {
1.92      moko      259:                if(my) {
                    260:                        size_t param_count=method.params_names ? method.params_names->count():0;
1.89      moko      261:                        size_t i=0;
                    262: 
                    263:                        if(count>param_count){
                    264:                                if(method.extra_params){
                    265:                                        for(; i<param_count; i++) {
                    266:                                                const String& fname=*(*method.params_names)[i];
                    267:                                                set_my_variable(fname, *params[i]);
                    268:                                        }
                    269: 
                    270:                                        VHash& vargs=*new VHash();
                    271:                                        HashStringValue& args = vargs.hash();
                    272: 
                    273:                                        for(; i<count; i++) {
                    274:                                                args.put(format(args.count(), 0), params[i]);
                    275:                                        }
1.86      misha     276: 
1.89      moko      277:                                        set_my_variable(*method.extra_params, vargs);
                    278:                                        return;
                    279:                                } else
1.109   ! moko      280:                                throw Exception(PARSER_RUNTIME, 0, "method of %s accepts maximum %d parameter(s) (%d present)", self().type(), param_count, count);
1.89      moko      281:                        }
1.78      misha     282: 
1.86      misha     283:                        for(; i<count; i++) {
1.77      misha     284:                                const String& fname=*(*method.params_names)[i];
                    285:                                set_my_variable(fname, *params[i]);
                    286:                        }
                    287: 
                    288:                        for(; i<param_count; i++) {
                    289:                                const String& fname=*(*method.params_names)[i];
1.68      misha     290:                                my->put(fname, VVoid::get());
1.1       paf       291:                        }
1.86      misha     292:                } else
1.77      misha     293:                        fnumbered_params.store_params(params,count);
1.1       paf       294:        }
                    295: 
1.78      misha     296:        void empty_params(){
                    297:                if(method.params_names){
                    298:                        size_t param_count=method.params_names->count();
1.91      moko      299:                        if(param_count>0){
1.104     moko      300:                                my->put(*(*method.params_names)[0], VString::empty());
                    301:                                for(size_t i=1; i<param_count; i++)
                    302:                                        my->put(*(*method.params_names)[i], VVoid::get());
1.78      misha     303:                        }
                    304:                }
                    305:        }
                    306: 
1.72      misha     307:        MethodParams* numbered_params() { return &fnumbered_params; }
1.18      paf       308: 
1.63      misha     309: protected:
1.18      paf       310: 
1.46      paf       311:        void set_my_variable(const String& fname, Value& value) {
                    312:                my->put(fname, &value); // remember param
1.25      paf       313:        }
                    314: 
1.46      paf       315:        Value* get_result_variable();
1.1       paf       316: 
                    317: public:
                    318:        
1.87      moko      319:        const Method& method;
1.1       paf       320: 
1.41      paf       321: };
                    322: 
1.85      misha     323: class VConstructorFrame: public VMethodFrame {
                    324: public:
1.87      moko      325:        VConstructorFrame(const Method& amethod, VMethodFrame *acaller, Value& aself) : VMethodFrame(amethod, acaller, aself) {
                    326:                // prevent non-string writes for better error reporting [constructors are not expected to return anything]
                    327:                VMethodFrame::write(aself);
                    328:        }
1.85      misha     329: 
                    330:        override void write(const String& /*astring*/, String::Language /*alang*/) {}
                    331: };
                    332: 
1.1       paf       333: #endif

E-mail: