Annotation of parser3/src/types/pa_vstateless_class.C, revision 1.67

1.8       paf         1: /**    @file
                      2:        Parser: stateless class.
                      3: 
1.64      moko        4:        Copyright (c) 2001-2023 Art. Lebedev Studio (http://www.artlebedev.com)
                      5:        Authors: Konstantin Morshnev <moko@design.ru>, Alexandr Petrosian <paf@design.ru>\
1.14      paf         6: */
1.8       paf         7: 
1.2       paf         8: #include "pa_vstateless_class.h"
1.29      misha       9: #include "pa_vstring.h"
1.30      misha      10: #include "pa_vbool.h"
1.55      moko       11: #include "pa_symbols.h"
1.45      moko       12: #include "pa_request.h"
1.30      misha      13: 
1.67    ! moko       14: volatile const char * IDENT_PA_VSTATELESS_CLASS_C="$Id: pa_vstateless_class.C,v 1.66 2024/09/07 15:01:38 moko Exp $" IDENT_PA_VSTATELESS_CLASS_H IDENT_PA_METHOD_H;
1.63      moko       15: 
                     16: bool VStateless_class::gall_vars_local=false;
1.41      misha      17: 
1.48      moko       18: override Value& VStateless_class::as_expr_result() {
1.35      misha      19:        return VBool::get(as_bool());
1.30      misha      20: }
1.17      paf        21: 
1.43      misha      22: /// @TODO why?! request must be different ptr from global [used in VStateless_class.set_method]
                     23: void VStateless_class::set_method(const String& aname, Method* amethod) {
1.20      paf        24:        if(flocked)
1.58      moko       25:                throw Exception(PARSER_RUNTIME, &aname, "can not add method to system class (maybe you have forgotten .CLASS in ^process[$caller.CLASS]{...}?)");
1.20      paf        26: 
1.58      moko       27:        if(fderived.count() && aname != auto_method_name) {
1.43      misha      28:                Method *omethod=fmethods.get(aname);
1.67    ! moko       29:                for(ArrayClass::Iterator i(fderived); i; ) {
1.43      misha      30:                        VStateless_class *c=i.next();
                     31:                        if(c->fmethods.get(aname)==omethod)
1.44      misha      32:                                c->real_set_method(aname, amethod);
1.43      misha      33:                }
                     34:        }
1.44      misha      35:        real_set_method(aname, amethod); 
                     36: }
                     37: 
                     38: void VStateless_class::real_set_method(const String& aname, Method* amethod) {
1.56      moko       39: #ifdef SYMBOLS_CACHING
                     40:        // instance() as called from static constuctors
1.55      moko       41:        Symbols::instance().add(aname);
1.56      moko       42: #endif
1.44      misha      43:        fmethods.put(aname, amethod);
1.59      moko       44:        if(amethod)
                     45:                amethod->name=&aname;
1.17      paf        46: }
1.2       paf        47: 
                     48: void VStateless_class::add_native_method(
1.20      paf        49:        const char* cstr_name,
1.7       paf        50:        Method::Call_type call_type,
1.20      paf        51:        NativeCodePtr native_code,
1.38      misha      52:        int min_numbered_params_count, 
1.40      misha      53:        int max_numbered_params_count,
                     54:        Method::Call_optimization
                     55: #ifdef OPTIMIZE_CALL
                     56:                call_optimization
                     57: #endif
                     58:        ) {
1.2       paf        59: 
1.43      misha      60:        Method* method=new Method(
1.7       paf        61:                call_type,
1.2       paf        62:                min_numbered_params_count, max_numbered_params_count,
                     63:                0/*params_names*/, 0/*locals_names*/,
1.40      misha      64:                0/*parser_code*/, native_code, false/*all_vars_local*/
                     65: #ifdef OPTIMIZE_RESULT
                     66:                , Method::RO_USE_WCONTEXT
                     67: #endif
                     68: #ifdef OPTIMIZE_CALL
                     69:                , call_optimization
                     70: #endif
                     71:                );
1.39      misha      72: 
1.43      misha      73:        set_method(*new String(cstr_name), method);
1.16      paf        74: }
                     75: 
1.51      misha      76: /// VStateless_class: $method
1.42      misha      77: Value* VStateless_class::get_element(Value& aself, const String& aname) {
1.51      misha      78: #ifndef OPTIMIZE_BYTECODE_GET_ELEMENT__SPECIAL
1.16      paf        79:        // $CLASS
1.57      moko       80:        if(SYMBOLS_EQ(aname,CLASS_SYMBOL))
1.16      paf        81:                return this;
1.36      misha      82: 
1.29      misha      83:        // $CLASS_NAME
1.57      moko       84:        if(SYMBOLS_EQ(aname,CLASS_NAME_SYMBOL))
1.53      moko       85:                return new VString(*new String(type()));
1.51      misha      86: #endif
1.36      misha      87: 
1.16      paf        88:        // $method=junction(self+class+method)
1.49      misha      89:        if(Method* method=get_method(aname))
                     90:                return method->get_vjunction(aself);
1.18      paf        91: 
1.42      misha      92:        return 0;
                     93: }
                     94: 
                     95: Value* VStateless_class::get_scalar(Value& aself){
                     96:        if(fscalar)
                     97:                return new VJunction(aself, fscalar, true /*getter*/);
1.16      paf        98:        return 0;
1.23      paf        99: }
                    100: 
1.42      misha     101: void VStateless_class::set_scalar(Method* amethod){
                    102:        fscalar=amethod;
1.2       paf       103: }
1.32      misha     104: 
                    105: Value* VStateless_class::get_default_getter(Value& aself, const String& aname){
1.46      moko      106:        if(fdefault_getter && aself.is_enabled_default_getter())
1.32      misha     107:                return new VJunction(aself, fdefault_getter, true /*getter*/, (String*)&aname);
                    108:        return 0;
                    109: }
                    110: 
                    111: void VStateless_class::set_default_getter(Method* amethod){
                    112:        fdefault_getter=amethod;
                    113: }
                    114: 
1.46      moko      115: bool VStateless_class::has_default_getter(){
                    116:        return fdefault_getter != NULL;
                    117: }
                    118: 
                    119: VJunction* VStateless_class::get_default_setter(Value& aself, const String& aname){
1.50      moko      120:        if(fdefault_setter && aself.is_enabled_default_setter())
1.46      moko      121:                return new VJunction(aself, fdefault_setter, false /*setter*/, (String*)&aname);
                    122:        return 0;
                    123: }
                    124: 
                    125: void VStateless_class::set_default_setter(Method* amethod){
                    126:        fdefault_setter=amethod;
                    127: }
                    128: 
                    129: bool VStateless_class::has_default_setter(){
                    130:        return fdefault_setter != NULL;
                    131: }
                    132: 
1.42      misha     133: void VStateless_class::set_base(VStateless_class* abase){
                    134:        if(abase){
1.61      moko      135:                abase->add_derived(*this);
1.42      misha     136:                fbase=abase;
                    137: 
1.45      moko      138:                bool no_auto = fmethods.get(auto_method_name) == NULL;
1.42      misha     139:                // we assume there is no derivatives at this point
                    140:                fmethods.merge_dont_replace(abase->fmethods);
1.65      moko      141:                // we don't want to inherit @auto (issue #75) unless we should (feature #1233)
                    142:                if (no_auto){
                    143:                        Method* mauto = fmethods.get(auto_method_name);
                    144:                        if(mauto != NULL && mauto->params_count < 2)
                    145:                                fmethods.remove(auto_method_name);
                    146:                }
1.42      misha     147: 
                    148:                if(fbase->fscalar && !fscalar)
                    149:                        fscalar=fbase->fscalar;
                    150:                if(fbase->fdefault_getter && !fdefault_getter)
                    151:                        fdefault_getter=fbase->fdefault_getter;
1.46      moko      152:                if(fbase->fdefault_setter && !fdefault_setter)
                    153:                        fdefault_setter=fbase->fdefault_setter;
1.42      misha     154:        }
1.33      misha     155: }
                    156: 
1.42      misha     157: void VStateless_class::add_derived(VStateless_class &aclass){
1.61      moko      158:        if(this == &aclass)
                    159:                throw Exception(PARSER_RUNTIME, 0, "circular class inheritance detected in class '%s'", type());
1.42      misha     160:        fderived+=&aclass;
                    161:        if (fbase)
                    162:                fbase->add_derived(aclass);
1.34      misha     163: }

E-mail: