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

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

E-mail: