Annotation of parser3/src/classes/reflection.C, revision 1.7
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.7 ! misha 8: static const char * const IDENT_REFLECTION_C="$Date: 2009-08-08 13:30:20 $";
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");
1.6 misha 52: Value* constructor_value=class_value->get_element(constructor_name);
1.1 misha 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:
1.7 ! misha 177: struct Store_method_info {
! 178: VStateless_class* base_class;
! 179: HashStringValue* result;
! 180: };
1.1 misha 181:
1.2 misha 182: static void store_method_info(
1.7 ! misha 183: HashStringMethod::key_type key,
! 184: HashStringMethod::value_type method,
! 185: Store_method_info* info
1.2 misha 186: ) {
1.7 ! misha 187: if(!info->base_class || info->base_class->get_method(String(key, String::L_CLEAN)) != method)
! 188: info->result->put(key, new VString(method->native_code?method_type_native:method_type_parser));
1.1 misha 189: }
190:
191: static void _methods(Request& r, MethodParams& params) {
192: const String& class_name=params.as_string(0, "class_name must be string");
193: Value* class_value=r.classes().get(class_name);
194: if(!class_value)
195: throw Exception(PARSER_RUNTIME,
196: &class_name,
197: "class is undefined");
198:
199: VHash& result=*new VHash;
200: if(VStateless_class* lclass=class_value->get_class()){
1.7 ! misha 201: HashStringMethod methods=lclass->get_methods();
! 202: Store_method_info info={lclass->base()?lclass->base()->get_class():0, result.get_hash()};
! 203: methods.for_each(store_method_info, &info);
1.1 misha 204: } else {
1.3 misha 205: // class which does not have methods (env, console, etc)
1.1 misha 206: }
207: r.write_no_lang(result);
208: }
209:
1.2 misha 210:
211: static void _method_params(Request& r, MethodParams& params) {
212: const String& class_name=params.as_string(0, "class_name must be string");
213: Value* class_value=r.classes().get(class_name);
214: if(!class_value)
215: throw Exception(PARSER_RUNTIME,
216: &class_name,
217: "class is undefined");
218:
219: VStateless_class* lclass=class_value->get_class();
220: if(!lclass)
221: throw Exception(PARSER_RUNTIME,
222: &class_name,
223: "class does not have methods");
224:
225: const String& method_name=params.as_string(1, "method_name must be string");
226: Method* method=lclass->get_method(method_name);
227: if(!method)
228: throw Exception(PARSER_RUNTIME,
229: &method_name,
230: "method not found in class %s",
231: class_name.cstr());
232:
233: VHash& result=*new VHash;
234: HashStringValue* hash=result.get_hash();
235: if(method->native_code){
236: // native code
237: hash->put(method_min_params, new VInt(method->min_numbered_params_count));
238: hash->put(method_max_params, new VInt(method->max_numbered_params_count));
1.5 misha 239: Value* call_type=0;
1.2 misha 240: switch(method->call_type){
241: case Method::CT_DYNAMIC:
1.4 misha 242: call_type=new VString(method_call_type_dynamic);
1.2 misha 243: break;
244: case Method::CT_STATIC:
1.4 misha 245: call_type=new VString(method_call_type_static);
1.2 misha 246: break;
247: }
1.5 misha 248: if(call_type)
249: hash->put(method_call_type, call_type);
1.2 misha 250: } else {
251: // parser code
252: if(method->params_names)
253: for(size_t i=0; i<method->params_names->count(); i++)
254: hash->put(String::Body::Format(i), new VString(*method->params_names->get(i)));
255: }
256:
257: r.write_no_lang(result);
258: }
259:
1.1 misha 260: // constructor
261: MReflection::MReflection(): Methoded("reflection") {
1.2 misha 262: // ^reflection:create[class_name;constructor_name[;param1[;param2[;...]]]]
263: add_native_method("create", Method::CT_STATIC, _create, 2, 102);
1.1 misha 264:
1.3 misha 265: // ^reflection:classes[]
266: add_native_method("classes", Method::CT_STATIC, _classes, 0, 0);
267:
1.1 misha 268: // ^reflection:class[object]
269: add_native_method("class", Method::CT_STATIC, _class, 1, 1);
270:
271: // ^reflection:class_name[object]
272: add_native_method("class_name", Method::CT_STATIC, _class_name, 1, 1);
273:
1.2 misha 274: // ^reflection:base_class[object]
275: add_native_method("base", Method::CT_STATIC, _base, 1, 1);
276:
277: // ^reflection:base_class_name[object]
278: add_native_method("base_name", Method::CT_STATIC, _base_name, 1, 1);
279:
280: // ^reflection:methods[class_name]
1.1 misha 281: add_native_method("methods", Method::CT_STATIC, _methods, 1, 1);
1.2 misha 282:
283: // ^reflection:method_params[class_name;method_name]
284: add_native_method("method_params", Method::CT_STATIC, _method_params, 2, 2);
1.1 misha 285: }
E-mail: