Annotation of parser3/src/types/pa_vobject.C, revision 1.58

1.1       paf         1: /**    @file
                      2:        Parser: @b object class impl.
                      3: 
1.58    ! moko        4:        Copyright (c) 2001-2026 Art. Lebedev Studio (https://www.artlebedev.com)
1.51      moko        5:        Authors: Konstantin Morshnev <moko@design.ru>, Alexandr Petrosian <paf@design.ru>
1.1       paf         6: */
                      7: 
                      8: #include "pa_vobject.h"
1.7       paf         9: #include "pa_vhash.h"
                     10: #include "pa_vtable.h"
1.25      misha      11: #include "pa_vstring.h"
1.26      misha      12: #include "pa_vmethod_frame.h"
                     13: #include "pa_request.h"
1.1       paf        14: 
1.58    ! moko       15: volatile const char * IDENT_PA_VOBJECT_C="$Id: pa_vobject.C,v 1.57 2024/11/04 03:53:26 moko Exp $" IDENT_PA_VOBJECT_H;
1.26      misha      16: 
1.37      moko       17: Value* VObject::get_scalar_value(const char* as_something) const {
1.26      misha      18:        VObject* unconst_this=const_cast<VObject*>(this);
1.29      misha      19:        if(Value* scalar=fclass.get_scalar(*unconst_this))
1.26      misha      20:                if(Junction* junction=scalar->get_junction())
                     21:                        if(const Method *method=junction->method){
1.47      moko       22:                                if(method->params_count>1)
1.55      moko       23:                                        throw Exception(PARSER_RUNTIME, 0, "scalar getter method can't have more than 1 parameter (has %d parameters)", method->params_count);
1.47      moko       24:                                METHOD_FRAME_ACTION(*method, 0 /*no caller*/, *unconst_this, {
                     25:                                        Value *param;
                     26:                                        if(method->params_count==1){
1.30      misha      27:                                                param=new VString(*new String(as_something));
                     28:                                                frame.store_params(&param, 1);
1.47      moko       29:                                        } /* no need for else frame.empty_params() */
                     30:                                        pa_thread_request().call(frame);
                     31:                                        return &frame.result();
                     32:                                });
1.26      misha      33:                        }
                     34:        return 0;
                     35: }
1.5       paf        36: 
1.54      moko       37: bool VObject::is(const char* atype) {
                     38:        return fclass.is(atype);
1.5       paf        39: }
1.1       paf        40: 
1.7       paf        41: bool VObject::is_defined() const {
1.56      moko       42:        Temp_recursion go_down(pa_thread_request());
1.30      misha      43:        if(Value* value=get_scalar_value("def"))
1.40      misha      44:                return value->as_bool();
1.29      misha      45:        return Value::is_defined();
1.7       paf        46: }
1.29      misha      47: 
1.35      moko       48: Value& VObject::as_expr_result() {
1.56      moko       49:        Temp_recursion go_down(pa_thread_request());
1.30      misha      50:        if(Value* value=get_scalar_value("expression"))
1.26      misha      51:                return value->as_expr_result();
1.29      misha      52:        return Value::as_expr_result();
1.7       paf        53: }
1.29      misha      54: 
1.7       paf        55: int VObject::as_int() const {
1.56      moko       56:        Temp_recursion go_down(pa_thread_request());
1.30      misha      57:        if(Value* value=get_scalar_value("int"))
1.26      misha      58:                return value->as_int();
1.29      misha      59:        return Value::as_int();
1.7       paf        60: }
1.29      misha      61: 
1.26      misha      62: double VObject::as_double() const {
1.56      moko       63:        Temp_recursion go_down(pa_thread_request());
1.30      misha      64:        if(Value* value=get_scalar_value("double"))
1.26      misha      65:                return value->as_double();
1.29      misha      66:        return Value::as_double();
1.7       paf        67: }
1.29      misha      68: 
1.7       paf        69: bool VObject::as_bool() const { 
1.56      moko       70:        Temp_recursion go_down(pa_thread_request());
1.30      misha      71:        if(Value* value=get_scalar_value("bool"))
1.26      misha      72:                return value->as_bool();
1.29      misha      73:        return Value::as_bool();
1.7       paf        74: }
1.29      misha      75: 
1.53      moko       76: VFile* VObject::as_vfile() {
1.56      moko       77:        Temp_recursion go_down(pa_thread_request());
1.30      misha      78:        if(Value* value=get_scalar_value("file"))
1.53      moko       79:                return value->as_vfile();
                     80:        return Value::as_vfile();
1.7       paf        81: }
                     82: 
1.13      paf        83: HashStringValue* VObject::get_hash() {
1.56      moko       84:        Temp_recursion go_down(pa_thread_request());
1.30      misha      85:        if(Value* value=get_scalar_value("hash"))
1.29      misha      86:                return value->get_hash();
                     87:        return &ffields;
                     88: }
1.7       paf        89: 
                     90: Table *VObject::get_table() {
1.56      moko       91:        Temp_recursion go_down(pa_thread_request());
1.30      misha      92:        if(Value* value=get_scalar_value("table"))
1.29      misha      93:                return value->get_table();
                     94:        return Value::get_table();
1.7       paf        95: }
1.1       paf        96: 
1.29      misha      97: Value* VObject::get_element(const String& aname) {
1.32      moko       98:        // object field
1.13      paf        99:        if(Value* result=ffields.get(aname))
1.8       paf       100:                return result;
                    101: 
1.32      moko      102:        // class method or property, or _object_ default getter
                    103:        return fclass.get_element(*this, aname);
1.1       paf       104: }
1.19      paf       105: 
1.43      moko      106: #ifdef FEATURE_GET_ELEMENT4CALL
                    107: // get_element copy to remove extra virtual call
                    108: Value* VObject::get_element4call(const String& aname) {
                    109:        // object field
                    110:        if(Value* result=ffields.get(aname))
                    111:                return result;
                    112: 
                    113:        // class method or property, or _object_ default getter
1.52      moko      114:        if(Value* result=fclass.get_element(*this, aname))
                    115:                return result;
                    116: 
                    117:        return bark("%s method not found", &aname);
1.43      moko      118: }
                    119: #endif
                    120: 
1.38      moko      121: const VJunction* VObject::put_element(const String& aname, Value* avalue){
1.45      moko      122:        // class setter
1.38      moko      123:        if(const VJunction* result=fclass.put_element_replace_only(*this, aname, avalue))
1.49      moko      124:                return result == PUT_ELEMENT_REPLACED_FIELD ? 0 : result;
1.32      moko      125:        
1.39      moko      126:        // object field or default setter, avoiding virtual is_enabled_default_setter call
                    127:        if (state & IS_SETTER_ACTIVE){
1.32      moko      128:                return ffields.put_replaced(aname, avalue) ? 0 : fclass.get_default_setter(*this, aname); 
                    129:        } else {
                    130:                ffields.put(aname, avalue);
                    131:        }
1.29      misha     132:        return 0;
1.1       paf       133: }
1.36      moko      134: 
                    135: const String* VObject::get_json_string(Json_options& options){
                    136:        if(options.default_method){
1.42      moko      137:                return default_method_2_json_string(*options.default_method, options);
1.36      moko      138:        }
1.42      moko      139:        return options.hash_json_string(get_hash());
1.36      moko      140: }

E-mail: