Annotation of parser3/src/types/pa_vstateless_class.C, revision 1.64
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.64 ! moko 14: volatile const char * IDENT_PA_VSTATELESS_CLASS_C="$Id: pa_vstateless_class.C,v 1.63 2021/01/02 23:01:11 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);
29: Array_iterator<VStateless_class *> i(fderived);
30: while(i.has_next()) {
31: VStateless_class *c=i.next();
32: if(c->fmethods.get(aname)==omethod)
1.44 misha 33: c->real_set_method(aname, amethod);
1.43 misha 34: }
35: }
1.44 misha 36: real_set_method(aname, amethod);
37: }
38:
39: void VStateless_class::real_set_method(const String& aname, Method* amethod) {
1.56 moko 40: #ifdef SYMBOLS_CACHING
41: // instance() as called from static constuctors
1.55 moko 42: Symbols::instance().add(aname);
1.56 moko 43: #endif
1.44 misha 44: fmethods.put(aname, amethod);
1.59 moko 45: if(amethod)
46: amethod->name=&aname;
1.17 paf 47: }
1.2 paf 48:
49: void VStateless_class::add_native_method(
1.20 paf 50: const char* cstr_name,
1.7 paf 51: Method::Call_type call_type,
1.20 paf 52: NativeCodePtr native_code,
1.38 misha 53: int min_numbered_params_count,
1.40 misha 54: int max_numbered_params_count,
55: Method::Call_optimization
56: #ifdef OPTIMIZE_CALL
57: call_optimization
58: #endif
59: ) {
1.2 paf 60:
1.43 misha 61: Method* method=new Method(
1.7 paf 62: call_type,
1.2 paf 63: min_numbered_params_count, max_numbered_params_count,
64: 0/*params_names*/, 0/*locals_names*/,
1.40 misha 65: 0/*parser_code*/, native_code, false/*all_vars_local*/
66: #ifdef OPTIMIZE_RESULT
67: , Method::RO_USE_WCONTEXT
68: #endif
69: #ifdef OPTIMIZE_CALL
70: , call_optimization
71: #endif
72: );
1.39 misha 73:
1.43 misha 74: set_method(*new String(cstr_name), method);
1.16 paf 75: }
76:
1.51 misha 77: /// VStateless_class: $method
1.42 misha 78: Value* VStateless_class::get_element(Value& aself, const String& aname) {
1.51 misha 79: #ifndef OPTIMIZE_BYTECODE_GET_ELEMENT__SPECIAL
1.16 paf 80: // $CLASS
1.57 moko 81: if(SYMBOLS_EQ(aname,CLASS_SYMBOL))
1.16 paf 82: return this;
1.36 misha 83:
1.29 misha 84: // $CLASS_NAME
1.57 moko 85: if(SYMBOLS_EQ(aname,CLASS_NAME_SYMBOL))
1.53 moko 86: return new VString(*new String(type()));
1.51 misha 87: #endif
1.36 misha 88:
1.16 paf 89: // $method=junction(self+class+method)
1.49 misha 90: if(Method* method=get_method(aname))
91: return method->get_vjunction(aself);
1.18 paf 92:
1.42 misha 93: return 0;
94: }
95:
96: Value* VStateless_class::get_scalar(Value& aself){
97: if(fscalar)
98: return new VJunction(aself, fscalar, true /*getter*/);
1.16 paf 99: return 0;
1.23 paf 100: }
101:
1.42 misha 102: void VStateless_class::set_scalar(Method* amethod){
103: fscalar=amethod;
1.2 paf 104: }
1.32 misha 105:
106: Value* VStateless_class::get_default_getter(Value& aself, const String& aname){
1.46 moko 107: if(fdefault_getter && aself.is_enabled_default_getter())
1.32 misha 108: return new VJunction(aself, fdefault_getter, true /*getter*/, (String*)&aname);
109: return 0;
110: }
111:
112: void VStateless_class::set_default_getter(Method* amethod){
113: fdefault_getter=amethod;
114: }
115:
1.46 moko 116: bool VStateless_class::has_default_getter(){
117: return fdefault_getter != NULL;
118: }
119:
120: VJunction* VStateless_class::get_default_setter(Value& aself, const String& aname){
1.50 moko 121: if(fdefault_setter && aself.is_enabled_default_setter())
1.46 moko 122: return new VJunction(aself, fdefault_setter, false /*setter*/, (String*)&aname);
123: return 0;
124: }
125:
126: void VStateless_class::set_default_setter(Method* amethod){
127: fdefault_setter=amethod;
128: }
129:
130: bool VStateless_class::has_default_setter(){
131: return fdefault_setter != NULL;
132: }
133:
1.42 misha 134: void VStateless_class::set_base(VStateless_class* abase){
135: if(abase){
1.61 moko 136: abase->add_derived(*this);
1.42 misha 137: fbase=abase;
138:
1.45 moko 139: bool no_auto = fmethods.get(auto_method_name) == NULL;
1.42 misha 140: // we assume there is no derivatives at this point
141: fmethods.merge_dont_replace(abase->fmethods);
1.45 moko 142: // we don't want to inherit @auto (issue #75)
143: if (no_auto) fmethods.remove(auto_method_name);
1.42 misha 144:
145: if(fbase->fscalar && !fscalar)
146: fscalar=fbase->fscalar;
147: if(fbase->fdefault_getter && !fdefault_getter)
148: fdefault_getter=fbase->fdefault_getter;
1.46 moko 149: if(fbase->fdefault_setter && !fdefault_setter)
150: fdefault_setter=fbase->fdefault_setter;
1.42 misha 151: }
1.33 misha 152: }
153:
1.42 misha 154: void VStateless_class::add_derived(VStateless_class &aclass){
1.61 moko 155: if(this == &aclass)
156: throw Exception(PARSER_RUNTIME, 0, "circular class inheritance detected in class '%s'", type());
1.42 misha 157: fderived+=&aclass;
158: if (fbase)
159: fbase->add_derived(aclass);
1.34 misha 160: }
E-mail: