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

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

E-mail: