Annotation of parser3/src/classes/reflection.C, revision 1.28
1.1 misha 1: /** @file
2: Parser: @b reflection parser class.
3:
1.24 moko 4: Copyright (c) 2001-2012 Art. Lebedev Studio (http://www.artlebedev.com)
1.1 misha 5: Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
6: */
7:
8: #include "pa_vmethod_frame.h"
9: #include "pa_request.h"
10: #include "pa_vbool.h"
11:
1.28 ! misha 12: volatile const char * IDENT_REFLECTION_C="$Id: reflection.C,v 1.27 2012-05-28 19:47:52 moko Exp $";
1.24 moko 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.8 misha 20: static const String method_inherited("inherited");
1.23 misha 21: static const String method_overridden("overridden");
1.3 misha 22: static const String method_call_type_static("static");
23: static const String method_call_type_dynamic("dynamic");
1.2 misha 24:
1.3 misha 25: static const String method_min_params("min_params");
26: static const String method_max_params("max_params");
1.2 misha 27:
1.1 misha 28: // class
29:
30: class MReflection: public Methoded {
31: public:
32: MReflection();
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");
1.11 misha 44: Value* class_value=r.get_class(class_name);
1.1 misha 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.10 misha 54: if(!constructor_value || !constructor_value->get_junction())
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 {
1.12 misha 85: max_params_count=method->params_names?method->params_names->count():0;
1.2 misha 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:
1.17 moko 97: Value &object = r.construct(*class_value, *method);
98: VConstructorFrame frame(*method, r.get_method_frame(), object);
1.1 misha 99:
1.3 misha 100: Value* v[100];
1.1 misha 101: if(nparams>0){
1.2 misha 102: for(int i=0; i<nparams; i++)
1.1 misha 103: v[i]=&r.process_to_value(params[i+2]);
104: frame.store_params((Value**)&v, nparams);
105: } else {
106: frame.empty_params();
107: }
1.17 moko 108: r.op_call(frame);
1.18 moko 109: object.enable_default_setter();
1.21 moko 110: r.write_pass_lang(frame.result());
1.1 misha 111: }
112:
1.2 misha 113:
1.3 misha 114: static void store_vlass_info(
115: HashStringValue::key_type key,
116: HashStringValue::value_type value,
117: HashStringValue* result
118: ){
119: Value* v;
120: if(value->get_class())
121: v=new VString(class_type_methoded);
122: else
123: v=VVoid::get();
124: result->put(key, v);
125: }
126:
127: static void _classes(Request& r, MethodParams&) {
128: VHash& result=*new VHash;
129: r.classes().for_each(store_vlass_info, result.get_hash());
130: r.write_no_lang(result);
131: }
132:
133:
134: static Value* get_class(Value* value){
135: if(VStateless_class* result=value->get_class())
136: return result;
137: else
138: // classes with fields only, like env & console
139: return value;
140: }
141:
142: static const String* get_class_name(Value* value){
143: if(VStateless_class* lclass=value->get_class())
144: return &lclass->name();
145: else
146: // classes with fields only, like env & console
147: return new String(value->type());
148: }
149:
150:
1.1 misha 151: static void _class(Request& r, MethodParams& params) {
1.3 misha 152: r.write_no_lang(*get_class(¶ms[0]));
1.1 misha 153: }
154:
1.2 misha 155:
1.1 misha 156: static void _class_name(Request& r, MethodParams& params) {
1.3 misha 157: r.write_no_lang(*get_class_name(¶ms[0]));
1.1 misha 158: }
159:
160:
1.2 misha 161: static void _base(Request& r, MethodParams& params) {
1.3 misha 162: if(VStateless_class* lclass=params[0].get_class())
163: if(Value* base=lclass->base()){
164: r.write_no_lang(*get_class(base));
165: return;
166: }
167:
168: // classes with fields only, like env & console or without base
169: r.write_no_lang(*VVoid::get());
1.2 misha 170: }
1.1 misha 171:
172:
1.2 misha 173: static void _base_name(Request& r, MethodParams& params) {
1.3 misha 174: if(VStateless_class* lclass=params[0].get_class())
1.2 misha 175: if(Value* base=lclass->base())
1.3 misha 176: r.write_no_lang(*get_class_name(base));
1.2 misha 177: }
1.1 misha 178:
1.2 misha 179: static void store_method_info(
1.7 misha 180: HashStringMethod::key_type key,
181: HashStringMethod::value_type method,
1.8 misha 182: HashStringValue* result
1.2 misha 183: ) {
1.8 misha 184: result->put(key, new VString(method->native_code?method_type_native:method_type_parser));
1.1 misha 185: }
186:
187: static void _methods(Request& r, MethodParams& params) {
188: const String& class_name=params.as_string(0, "class_name must be string");
1.11 misha 189: Value* class_value=r.get_class(class_name);
1.1 misha 190: if(!class_value)
191: throw Exception(PARSER_RUNTIME,
192: &class_name,
193: "class is undefined");
194:
195: VHash& result=*new VHash;
1.28 ! misha 196: if(VStateless_class* lclass=class_value->get_class()) {
1.7 misha 197: HashStringMethod methods=lclass->get_methods();
1.8 misha 198: methods.for_each(store_method_info, result.get_hash());
1.1 misha 199: } else {
1.3 misha 200: // class which does not have methods (env, console, etc)
1.1 misha 201: }
202: r.write_no_lang(result);
203: }
204:
1.28 ! misha 205: static void _method(Request& r, MethodParams& params) {
! 206: Value& o=params.as_no_junction(0, "first param must be object or class, not junction");
! 207: const String& name=params.as_string(1, "method name must be string");
! 208:
! 209: if(VStateless_class* lclass=o.get_class()) {
! 210: if(Method* method=lclass->get_method(name))
! 211: r.write_no_lang(*method->get_vjunction(o));
! 212: } else {
! 213: // class which does not have methods (env, console, etc)
! 214: }
! 215: }
! 216:
1.13 misha 217: static void _fields(Request& r, MethodParams& params) {
1.28 ! misha 218: Value& o=params.as_no_junction(0, "param must be object or class, not junction");
! 219:
! 220: if(HashStringValue* fields=o.get_fields()) {
1.13 misha 221: VHash& result=*new VHash(*fields);
222: r.write_no_lang(result);
223: } else
224: r.write_no_lang(*new VHash());
225: }
1.2 misha 226:
1.28 ! misha 227: static void _field(Request& r, MethodParams& params) {
! 228: Value& o=params.as_no_junction(0, "first param must be object or class, not junction");
! 229: const String& name=params.as_string(1, "field name must be string");
! 230:
! 231: if(HashStringValue* fields=o.get_fields())
! 232: if(Value* value=fields->get(name))
! 233: r.write_no_lang(*value);
! 234: }
! 235:
1.8 misha 236: static void _method_info(Request& r, MethodParams& params) {
1.2 misha 237: const String& class_name=params.as_string(0, "class_name must be string");
1.11 misha 238: Value* class_value=r.get_class(class_name);
1.2 misha 239: if(!class_value)
240: throw Exception(PARSER_RUNTIME,
241: &class_name,
242: "class is undefined");
243:
244: VStateless_class* lclass=class_value->get_class();
245: if(!lclass)
246: throw Exception(PARSER_RUNTIME,
247: &class_name,
248: "class does not have methods");
249:
250: const String& method_name=params.as_string(1, "method_name must be string");
251: Method* method=lclass->get_method(method_name);
252: if(!method)
253: throw Exception(PARSER_RUNTIME,
254: &method_name,
255: "method not found in class %s",
256: class_name.cstr());
257:
258: VHash& result=*new VHash;
259: HashStringValue* hash=result.get_hash();
1.8 misha 260:
261: VStateless_class* c=lclass;
1.23 misha 262: Method* base_method;
263: if(c->base() && (base_method=c->base()->get_method(method_name))){
1.8 misha 264: c=c->base()->get_class();
1.23 misha 265: while(c->base() && base_method==c->base()->get_method(method_name))
266: c=c->base()->get_class();
267: hash->put((base_method==method) ? method_inherited : method_overridden, new VString(c->name()));
268: }
1.8 misha 269:
1.2 misha 270: if(method->native_code){
271: // native code
272: hash->put(method_min_params, new VInt(method->min_numbered_params_count));
273: hash->put(method_max_params, new VInt(method->max_numbered_params_count));
1.5 misha 274: Value* call_type=0;
1.2 misha 275: switch(method->call_type){
276: case Method::CT_DYNAMIC:
1.4 misha 277: call_type=new VString(method_call_type_dynamic);
1.2 misha 278: break;
279: case Method::CT_STATIC:
1.4 misha 280: call_type=new VString(method_call_type_static);
1.2 misha 281: break;
282: }
1.5 misha 283: if(call_type)
284: hash->put(method_call_type, call_type);
1.2 misha 285: } else {
286: // parser code
1.16 misha 287: const String* filespec = r.get_method_filename(method);
288: if( filespec )
289: hash->put("file", new VString(*filespec));
1.2 misha 290: if(method->params_names)
291: for(size_t i=0; i<method->params_names->count(); i++)
292: hash->put(String::Body::Format(i), new VString(*method->params_names->get(i)));
293: }
294:
295: r.write_no_lang(result);
296: }
297:
1.9 misha 298: static void _dynamical(Request& r, MethodParams& params) {
299: if(params.count()){
300: r.write_no_lang(VBool::get(params[0].get_class() != ¶ms[0]));
301: } else {
302: VMethodFrame* caller=r.get_method_frame()->caller();
303: r.write_no_lang(VBool::get(caller && caller->get_class() != &caller->self()));
304: }
305: }
306:
1.19 moko 307: static void _copy(Request& r, MethodParams& params) {
308: HashStringValue* src=params.as_no_junction(0, "source must not be code").get_hash();
309:
310: if(src==NULL)
311: throw Exception(PARSER_RUNTIME, 0, "source must have hash representation");
312:
313: Value& dst=params.as_no_junction(1, "destination must not be code");
314:
315: for(HashStringValue::Iterator i(*src); i; i.next())
1.20 moko 316: r.put_element(dst, *new String(i.key(), String::L_TAINTED), i.value());
1.19 moko 317: }
318:
1.25 moko 319: static void _uid(Request& r, MethodParams& params) {
320: Value& obj=params.as_no_junction(0, "object must not be code");
321:
322: char local_buf[MAX_NUMBER];
323: int size=snprintf(local_buf, sizeof(local_buf), "%p", &obj);
324:
325: r.write_pass_lang(*new String(pa_strdup(local_buf, (size_t)size), String::L_CLEAN, size));
326: }
327:
1.27 moko 328: static void _delete(Request&, MethodParams& params) {
1.26 misha 329: const String& key=params.as_string(1, "field name must be string");
330: if(HashStringValue* fields=params[0].get_fields()){
331: fields->remove(key);
332: }
333: }
334:
335:
1.1 misha 336: // constructor
337: MReflection::MReflection(): Methoded("reflection") {
1.2 misha 338: // ^reflection:create[class_name;constructor_name[;param1[;param2[;...]]]]
339: add_native_method("create", Method::CT_STATIC, _create, 2, 102);
1.1 misha 340:
1.3 misha 341: // ^reflection:classes[]
342: add_native_method("classes", Method::CT_STATIC, _classes, 0, 0);
343:
1.1 misha 344: // ^reflection:class[object]
345: add_native_method("class", Method::CT_STATIC, _class, 1, 1);
346:
347: // ^reflection:class_name[object]
348: add_native_method("class_name", Method::CT_STATIC, _class_name, 1, 1);
349:
1.2 misha 350: // ^reflection:base_class[object]
351: add_native_method("base", Method::CT_STATIC, _base, 1, 1);
352:
353: // ^reflection:base_class_name[object]
354: add_native_method("base_name", Method::CT_STATIC, _base_name, 1, 1);
355:
356: // ^reflection:methods[class_name]
1.1 misha 357: add_native_method("methods", Method::CT_STATIC, _methods, 1, 1);
1.2 misha 358:
1.28 ! misha 359: // ^reflection:method[object or class;method_name]
! 360: add_native_method("method", Method::CT_STATIC, _method, 2, 2);
! 361:
! 362: // ^reflection:method_info[class_name;method_name]
! 363: add_native_method("method_info", Method::CT_STATIC, _method_info, 2, 2);
! 364:
1.13 misha 365: // ^reflection:fields[object or class]
366: add_native_method("fields", Method::CT_STATIC, _fields, 1, 1);
367:
1.28 ! misha 368: // ^reflection:field[object or class;field_name]
! 369: add_native_method("field", Method::CT_STATIC, _field, 2, 2);
1.9 misha 370:
371: // ^reflection:dynamical[[object or class, caller if absent]]
372: add_native_method("dynamical", Method::CT_STATIC, _dynamical, 0, 1);
1.19 moko 373:
374: // ^reflection:copy[src;dst]
375: add_native_method("copy", Method::CT_STATIC, _copy, 2, 2);
1.25 moko 376:
377: // ^reflection:uid[object or class]
378: add_native_method("uid", Method::CT_STATIC, _uid, 1, 1);
1.26 misha 379:
380: // ^reflection:delete[object or class;field_name]
381: add_native_method("delete", Method::CT_STATIC, _delete, 2, 2);
1.1 misha 382: }
E-mail: