Annotation of parser3/src/classes/reflection.C, revision 1.5
1.1 misha 1: /** @file
2: Parser: @b reflection parser class.
3:
4: Copyright (c) 2001-2009 ArtLebedev Group (http://www.artlebedev.com)
5: Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
6: */
7:
1.5 ! misha 8: static const char * const IDENT_REFLECTION_C="$Date: 2009-07-29 05:07:36 $";
1.1 misha 9:
10: #include "pa_vmethod_frame.h"
11: #include "pa_request.h"
12: #include "pa_vbool.h"
13:
1.3 misha 14: static const String class_type_methoded("methoded");
1.2 misha 15:
1.3 misha 16: static const String method_type_native("native");
17: static const String method_type_parser("parser");
1.2 misha 18:
1.5 ! misha 19: static const String method_call_type("call_type");
1.3 misha 20: static const String method_call_type_static("static");
21: static const String method_call_type_dynamic("dynamic");
1.2 misha 22:
1.3 misha 23: static const String method_min_params("min_params");
24: static const String method_max_params("max_params");
1.2 misha 25:
1.1 misha 26: // class
27:
28: class MReflection: public Methoded {
29: public:
30: MReflection();
31: public: // Methoded
32: bool used_directly() { return true; }
33: };
34:
35: // global variable
36:
37: DECLARE_CLASS_VAR(reflection, new MReflection, 0);
38:
39: // methods
40:
1.2 misha 41:
42: static void _create(Request& r, MethodParams& params) {
1.1 misha 43: const String& class_name=params.as_string(0, "class_name must be string");
44: Value* class_value=r.classes().get(class_name);
45:
46: if(!class_value)
47: throw Exception(PARSER_RUNTIME,
48: &class_name,
49: "class is undefined");
50:
51: const String& constructor_name=params.as_string(1, "constructor_name must be string");
52: Value* constructor_value=class_value->get_element(constructor_name, *class_value, true);
53:
1.2 misha 54: if(!constructor_value || !constructor_value->get_junction() || constructor_value->get_junction()->self.get_class()!=class_value)
1.1 misha 55: throw Exception(PARSER_RUNTIME,
56: &constructor_name,
57: "constructor must be declared in class '%s'",
1.2 misha 58: class_value->get_class()->name_cstr());
1.1 misha 59:
60: Junction* junction=constructor_value->get_junction();
1.2 misha 61: const Method* method=junction->method;
62:
63: int nparams=params.count()-2;
64: int max_params_count;
65:
66: if(method->native_code){
67: if(method->call_type==Method::CT_STATIC)
68: throw Exception(PARSER_RUNTIME,
69: &constructor_name,
70: "native method of class '%s' (%s) is not allowed to be called dynamically",
71: class_value->get_class()->name_cstr(),
72: class_value->type());
73:
74: if(nparams<method->min_numbered_params_count)
75: throw Exception(PARSER_RUNTIME,
76: &constructor_name,
77: "native method of class '%s' (%s) accepts minimum %d parameter(s) (%d passed)",
78: class_value->get_class()->name_cstr(),
79: class_value->type(),
80: method->min_numbered_params_count,
81: nparams);
82:
83: max_params_count=method->max_numbered_params_count;
84: } else {
85: max_params_count=method->params_names->count();
86: }
87:
88: if(nparams>max_params_count)
89: throw Exception(PARSER_RUNTIME,
90: &constructor_name,
91: "method of class '%s' (%s) accepts maximum %d parameter(s) (%d passed)",
92: class_value->get_class()->name_cstr(),
93: class_value->type(),
94: max_params_count,
95: nparams);
1.1 misha 96:
97: VMethodFrame frame(*junction, r.get_method_frame());
98:
1.3 misha 99: Value* v[100];
1.1 misha 100: if(nparams>0){
1.2 misha 101: for(int i=0; i<nparams; i++)
1.1 misha 102: v[i]=&r.process_to_value(params[i+2]);
103: frame.store_params((Value**)&v, nparams);
104: } else {
105: frame.empty_params();
106: }
107: r.op_call(frame, true/*constructing*/);
108: r.write_pass_lang(frame.result());
109: }
110:
1.2 misha 111:
1.3 misha 112: static void store_vlass_info(
113: HashStringValue::key_type key,
114: HashStringValue::value_type value,
115: HashStringValue* result
116: ){
117: Value* v;
118: if(value->get_class())
119: v=new VString(class_type_methoded);
120: else
121: v=VVoid::get();
122: result->put(key, v);
123: }
124:
125: static void _classes(Request& r, MethodParams&) {
126: VHash& result=*new VHash;
127: r.classes().for_each(store_vlass_info, result.get_hash());
128: r.write_no_lang(result);
129: }
130:
131:
132: static Value* get_class(Value* value){
133: if(VStateless_class* result=value->get_class())
134: return result;
135: else
136: // classes with fields only, like env & console
137: return value;
138: }
139:
140: static const String* get_class_name(Value* value){
141: if(VStateless_class* lclass=value->get_class())
142: return &lclass->name();
143: else
144: // classes with fields only, like env & console
145: return new String(value->type());
146: }
147:
148:
1.1 misha 149: static void _class(Request& r, MethodParams& params) {
1.3 misha 150: r.write_no_lang(*get_class(¶ms[0]));
1.1 misha 151: }
152:
1.2 misha 153:
1.1 misha 154: static void _class_name(Request& r, MethodParams& params) {
1.3 misha 155: r.write_no_lang(*get_class_name(¶ms[0]));
1.1 misha 156: }
157:
158:
1.2 misha 159: static void _base(Request& r, MethodParams& params) {
1.3 misha 160: if(VStateless_class* lclass=params[0].get_class())
161: if(Value* base=lclass->base()){
162: r.write_no_lang(*get_class(base));
163: return;
164: }
165:
166: // classes with fields only, like env & console or without base
167: r.write_no_lang(*VVoid::get());
1.2 misha 168: }
1.1 misha 169:
170:
1.2 misha 171: static void _base_name(Request& r, MethodParams& params) {
1.3 misha 172: if(VStateless_class* lclass=params[0].get_class())
1.2 misha 173: if(Value* base=lclass->base())
1.3 misha 174: r.write_no_lang(*get_class_name(base));
1.2 misha 175: }
1.1 misha 176:
177:
1.2 misha 178: static void store_method_info(
179: HashString<Method*>::key_type key,
180: HashString<Method*>::value_type method,
181: HashStringValue* result
182: ) {
183: result->put(key, new VString(method->native_code?method_type_native:method_type_parser));
1.1 misha 184: }
185:
186: static void _methods(Request& r, MethodParams& params) {
187: const String& class_name=params.as_string(0, "class_name must be string");
188: Value* class_value=r.classes().get(class_name);
189: if(!class_value)
190: throw Exception(PARSER_RUNTIME,
191: &class_name,
192: "class is undefined");
193:
194: VHash& result=*new VHash;
195: if(VStateless_class* lclass=class_value->get_class()){
196: HashString<Method*> methods=lclass->get_methods();
197: methods.for_each(store_method_info, result.get_hash());
198: } else {
1.3 misha 199: // class which does not have methods (env, console, etc)
1.1 misha 200: }
201: r.write_no_lang(result);
202: }
203:
1.2 misha 204:
205: static void _method_params(Request& r, MethodParams& params) {
206: const String& class_name=params.as_string(0, "class_name must be string");
207: Value* class_value=r.classes().get(class_name);
208: if(!class_value)
209: throw Exception(PARSER_RUNTIME,
210: &class_name,
211: "class is undefined");
212:
213: VStateless_class* lclass=class_value->get_class();
214: if(!lclass)
215: throw Exception(PARSER_RUNTIME,
216: &class_name,
217: "class does not have methods");
218:
219: const String& method_name=params.as_string(1, "method_name must be string");
220: Method* method=lclass->get_method(method_name);
221: if(!method)
222: throw Exception(PARSER_RUNTIME,
223: &method_name,
224: "method not found in class %s",
225: class_name.cstr());
226:
227: VHash& result=*new VHash;
228: HashStringValue* hash=result.get_hash();
229: if(method->native_code){
230: // native code
231: hash->put(method_min_params, new VInt(method->min_numbered_params_count));
232: hash->put(method_max_params, new VInt(method->max_numbered_params_count));
1.5 ! misha 233: Value* call_type=0;
1.2 misha 234: switch(method->call_type){
235: case Method::CT_DYNAMIC:
1.4 misha 236: call_type=new VString(method_call_type_dynamic);
1.2 misha 237: break;
238: case Method::CT_STATIC:
1.4 misha 239: call_type=new VString(method_call_type_static);
1.2 misha 240: break;
241: }
1.5 ! misha 242: if(call_type)
! 243: hash->put(method_call_type, call_type);
1.2 misha 244: } else {
245: // parser code
246: if(method->params_names)
247: for(size_t i=0; i<method->params_names->count(); i++)
248: hash->put(String::Body::Format(i), new VString(*method->params_names->get(i)));
249: }
250:
251: r.write_no_lang(result);
252: }
253:
1.1 misha 254: // constructor
255: MReflection::MReflection(): Methoded("reflection") {
1.2 misha 256: // ^reflection:create[class_name;constructor_name[;param1[;param2[;...]]]]
257: add_native_method("create", Method::CT_STATIC, _create, 2, 102);
1.1 misha 258:
1.3 misha 259: // ^reflection:classes[]
260: add_native_method("classes", Method::CT_STATIC, _classes, 0, 0);
261:
1.1 misha 262: // ^reflection:class[object]
263: add_native_method("class", Method::CT_STATIC, _class, 1, 1);
264:
265: // ^reflection:class_name[object]
266: add_native_method("class_name", Method::CT_STATIC, _class_name, 1, 1);
267:
1.2 misha 268: // ^reflection:base_class[object]
269: add_native_method("base", Method::CT_STATIC, _base, 1, 1);
270:
271: // ^reflection:base_class_name[object]
272: add_native_method("base_name", Method::CT_STATIC, _base_name, 1, 1);
273:
274: // ^reflection:methods[class_name]
1.1 misha 275: add_native_method("methods", Method::CT_STATIC, _methods, 1, 1);
1.2 misha 276:
277: // ^reflection:method_params[class_name;method_name]
278: add_native_method("method_params", Method::CT_STATIC, _method_params, 2, 2);
1.1 misha 279: }
E-mail: