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

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

E-mail: