Annotation of parser3/src/classes/reflection.C, revision 1.30
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.30 ! misha 12: volatile const char * IDENT_REFLECTION_C="$Id: reflection.C,v 1.29 2013/03/14 03:14:42 misha 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.29 misha 27: static const String method_extra_param("extra_param");
1.2 misha 28:
1.30 ! misha 29: static const String def_class("class");
! 30:
1.1 misha 31: // class
32:
33: class MReflection: public Methoded {
34: public:
35: MReflection();
36: };
37:
38: // global variable
39:
40: DECLARE_CLASS_VAR(reflection, new MReflection, 0);
41:
42: // methods
43:
1.2 misha 44:
45: static void _create(Request& r, MethodParams& params) {
1.1 misha 46: const String& class_name=params.as_string(0, "class_name must be string");
1.11 misha 47: Value* class_value=r.get_class(class_name);
1.1 misha 48:
49: if(!class_value)
50: throw Exception(PARSER_RUNTIME,
51: &class_name,
52: "class is undefined");
53:
54: const String& constructor_name=params.as_string(1, "constructor_name must be string");
1.6 misha 55: Value* constructor_value=class_value->get_element(constructor_name);
1.1 misha 56:
1.10 misha 57: if(!constructor_value || !constructor_value->get_junction())
1.1 misha 58: throw Exception(PARSER_RUNTIME,
59: &constructor_name,
60: "constructor must be declared in class '%s'",
1.2 misha 61: class_value->get_class()->name_cstr());
1.1 misha 62:
63: Junction* junction=constructor_value->get_junction();
1.2 misha 64: const Method* method=junction->method;
65:
66: int nparams=params.count()-2;
67: int max_params_count;
68:
69: if(method->native_code){
70: if(method->call_type==Method::CT_STATIC)
71: throw Exception(PARSER_RUNTIME,
72: &constructor_name,
73: "native method of class '%s' (%s) is not allowed to be called dynamically",
74: class_value->get_class()->name_cstr(),
75: class_value->type());
76:
77: if(nparams<method->min_numbered_params_count)
78: throw Exception(PARSER_RUNTIME,
79: &constructor_name,
80: "native method of class '%s' (%s) accepts minimum %d parameter(s) (%d passed)",
81: class_value->get_class()->name_cstr(),
82: class_value->type(),
83: method->min_numbered_params_count,
84: nparams);
85:
86: max_params_count=method->max_numbered_params_count;
87: } else {
1.12 misha 88: max_params_count=method->params_names?method->params_names->count():0;
1.2 misha 89: }
90:
91: if(nparams>max_params_count)
92: throw Exception(PARSER_RUNTIME,
93: &constructor_name,
94: "method of class '%s' (%s) accepts maximum %d parameter(s) (%d passed)",
95: class_value->get_class()->name_cstr(),
96: class_value->type(),
97: max_params_count,
98: nparams);
1.1 misha 99:
1.17 moko 100: Value &object = r.construct(*class_value, *method);
101: VConstructorFrame frame(*method, r.get_method_frame(), object);
1.1 misha 102:
1.3 misha 103: Value* v[100];
1.1 misha 104: if(nparams>0){
1.2 misha 105: for(int i=0; i<nparams; i++)
1.1 misha 106: v[i]=&r.process_to_value(params[i+2]);
107: frame.store_params((Value**)&v, nparams);
108: } else {
109: frame.empty_params();
110: }
1.17 moko 111: r.op_call(frame);
1.18 moko 112: object.enable_default_setter();
1.21 moko 113: r.write_pass_lang(frame.result());
1.1 misha 114: }
115:
1.2 misha 116:
1.3 misha 117: static void store_vlass_info(
118: HashStringValue::key_type key,
119: HashStringValue::value_type value,
120: HashStringValue* result
121: ){
122: Value* v;
123: if(value->get_class())
124: v=new VString(class_type_methoded);
125: else
126: v=VVoid::get();
127: result->put(key, v);
128: }
129:
130: static void _classes(Request& r, MethodParams&) {
131: VHash& result=*new VHash;
132: r.classes().for_each(store_vlass_info, result.get_hash());
133: r.write_no_lang(result);
134: }
135:
136:
137: static Value* get_class(Value* value){
138: if(VStateless_class* result=value->get_class())
139: return result;
140: else
141: // classes with fields only, like env & console
142: return value;
143: }
144:
145: static const String* get_class_name(Value* value){
146: if(VStateless_class* lclass=value->get_class())
147: return &lclass->name();
148: else
149: // classes with fields only, like env & console
150: return new String(value->type());
151: }
152:
153:
1.1 misha 154: static void _class(Request& r, MethodParams& params) {
1.3 misha 155: r.write_no_lang(*get_class(¶ms[0]));
1.1 misha 156: }
157:
1.2 misha 158:
1.1 misha 159: static void _class_name(Request& r, MethodParams& params) {
1.3 misha 160: r.write_no_lang(*get_class_name(¶ms[0]));
1.1 misha 161: }
162:
163:
1.2 misha 164: static void _base(Request& r, MethodParams& params) {
1.3 misha 165: if(VStateless_class* lclass=params[0].get_class())
166: if(Value* base=lclass->base()){
167: r.write_no_lang(*get_class(base));
168: return;
169: }
170:
171: // classes with fields only, like env & console or without base
172: r.write_no_lang(*VVoid::get());
1.2 misha 173: }
1.1 misha 174:
175:
1.2 misha 176: static void _base_name(Request& r, MethodParams& params) {
1.3 misha 177: if(VStateless_class* lclass=params[0].get_class())
1.2 misha 178: if(Value* base=lclass->base())
1.3 misha 179: r.write_no_lang(*get_class_name(base));
1.2 misha 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,
1.8 misha 185: HashStringValue* result
1.2 misha 186: ) {
1.8 misha 187: result->put(key, new VString(method->native_code?method_type_native:method_type_parser));
1.1 misha 188: }
189:
1.30 ! misha 190: static void _def(Request& r, MethodParams& params) {
! 191: const String& type=params.as_string(0, "type must be string");
! 192: if(type == def_class) {
! 193: const String& name=params.as_string(1, "name must be string");
! 194: r.write_no_lang(VBool::get(r.get_class(name)!=0));
! 195: } else {
! 196: throw Exception(PARSER_RUNTIME, &type, "is invalid type, must be '%s'", def_class);
! 197: }
! 198: }
! 199:
1.1 misha 200: static void _methods(Request& r, MethodParams& params) {
201: const String& class_name=params.as_string(0, "class_name must be string");
1.11 misha 202: Value* class_value=r.get_class(class_name);
1.1 misha 203: if(!class_value)
204: throw Exception(PARSER_RUNTIME,
205: &class_name,
206: "class is undefined");
207:
208: VHash& result=*new VHash;
1.28 misha 209: if(VStateless_class* lclass=class_value->get_class()) {
1.7 misha 210: HashStringMethod methods=lclass->get_methods();
1.8 misha 211: methods.for_each(store_method_info, result.get_hash());
1.1 misha 212: } else {
1.3 misha 213: // class which does not have methods (env, console, etc)
1.1 misha 214: }
215: r.write_no_lang(result);
216: }
217:
1.28 misha 218: static void _method(Request& r, MethodParams& params) {
219: Value& o=params.as_no_junction(0, "first param must be object or class, not junction");
220: const String& name=params.as_string(1, "method name must be string");
221:
222: if(VStateless_class* lclass=o.get_class()) {
223: if(Method* method=lclass->get_method(name))
224: r.write_no_lang(*method->get_vjunction(o));
225: } else {
226: // class which does not have methods (env, console, etc)
227: }
228: }
229:
1.13 misha 230: static void _fields(Request& r, MethodParams& params) {
1.28 misha 231: Value& o=params.as_no_junction(0, "param must be object or class, not junction");
232:
233: if(HashStringValue* fields=o.get_fields()) {
1.13 misha 234: VHash& result=*new VHash(*fields);
235: r.write_no_lang(result);
236: } else
237: r.write_no_lang(*new VHash());
238: }
1.2 misha 239:
1.28 misha 240: static void _field(Request& r, MethodParams& params) {
241: Value& o=params.as_no_junction(0, "first param must be object or class, not junction");
242: const String& name=params.as_string(1, "field name must be string");
243:
244: if(HashStringValue* fields=o.get_fields())
245: if(Value* value=fields->get(name))
246: r.write_no_lang(*value);
247: }
248:
1.8 misha 249: static void _method_info(Request& r, MethodParams& params) {
1.2 misha 250: const String& class_name=params.as_string(0, "class_name must be string");
1.11 misha 251: Value* class_value=r.get_class(class_name);
1.2 misha 252: if(!class_value)
253: throw Exception(PARSER_RUNTIME,
254: &class_name,
255: "class is undefined");
256:
257: VStateless_class* lclass=class_value->get_class();
258: if(!lclass)
259: throw Exception(PARSER_RUNTIME,
260: &class_name,
261: "class does not have methods");
262:
263: const String& method_name=params.as_string(1, "method_name must be string");
264: Method* method=lclass->get_method(method_name);
265: if(!method)
266: throw Exception(PARSER_RUNTIME,
267: &method_name,
268: "method not found in class %s",
269: class_name.cstr());
270:
271: VHash& result=*new VHash;
272: HashStringValue* hash=result.get_hash();
1.8 misha 273:
274: VStateless_class* c=lclass;
1.23 misha 275: Method* base_method;
276: if(c->base() && (base_method=c->base()->get_method(method_name))){
1.8 misha 277: c=c->base()->get_class();
1.23 misha 278: while(c->base() && base_method==c->base()->get_method(method_name))
279: c=c->base()->get_class();
280: hash->put((base_method==method) ? method_inherited : method_overridden, new VString(c->name()));
281: }
1.8 misha 282:
1.29 misha 283: Value* call_type=0;
284: switch(method->call_type){
285: case Method::CT_DYNAMIC:
286: call_type=new VString(method_call_type_dynamic);
287: break;
288: case Method::CT_STATIC:
289: call_type=new VString(method_call_type_static);
290: break;
291: }
292: if(call_type)
293: hash->put(method_call_type, call_type);
294:
1.2 misha 295: if(method->native_code){
296: // native code
297: hash->put(method_min_params, new VInt(method->min_numbered_params_count));
298: hash->put(method_max_params, new VInt(method->max_numbered_params_count));
299: } else {
300: // parser code
1.16 misha 301: const String* filespec = r.get_method_filename(method);
302: if( filespec )
303: hash->put("file", new VString(*filespec));
1.29 misha 304:
305: hash->put(method_max_params, new VInt(method->params_names ? method->params_names->count() : 0));
306:
1.2 misha 307: if(method->params_names)
308: for(size_t i=0; i<method->params_names->count(); i++)
309: hash->put(String::Body::Format(i), new VString(*method->params_names->get(i)));
1.29 misha 310:
311: if(method->extra_params)
312: hash->put(method_extra_param, new VString(*method->extra_params));
1.2 misha 313: }
314:
315: r.write_no_lang(result);
316: }
317:
1.9 misha 318: static void _dynamical(Request& r, MethodParams& params) {
319: if(params.count()){
320: r.write_no_lang(VBool::get(params[0].get_class() != ¶ms[0]));
321: } else {
322: VMethodFrame* caller=r.get_method_frame()->caller();
323: r.write_no_lang(VBool::get(caller && caller->get_class() != &caller->self()));
324: }
325: }
326:
1.19 moko 327: static void _copy(Request& r, MethodParams& params) {
328: HashStringValue* src=params.as_no_junction(0, "source must not be code").get_hash();
329:
330: if(src==NULL)
331: throw Exception(PARSER_RUNTIME, 0, "source must have hash representation");
332:
333: Value& dst=params.as_no_junction(1, "destination must not be code");
334:
335: for(HashStringValue::Iterator i(*src); i; i.next())
1.20 moko 336: r.put_element(dst, *new String(i.key(), String::L_TAINTED), i.value());
1.19 moko 337: }
338:
1.25 moko 339: static void _uid(Request& r, MethodParams& params) {
340: Value& obj=params.as_no_junction(0, "object must not be code");
341:
342: char local_buf[MAX_NUMBER];
343: int size=snprintf(local_buf, sizeof(local_buf), "%p", &obj);
344:
345: r.write_pass_lang(*new String(pa_strdup(local_buf, (size_t)size), String::L_CLEAN, size));
346: }
347:
1.27 moko 348: static void _delete(Request&, MethodParams& params) {
1.26 misha 349: const String& key=params.as_string(1, "field name must be string");
350: if(HashStringValue* fields=params[0].get_fields()){
351: fields->remove(key);
352: }
353: }
354:
355:
1.1 misha 356: // constructor
357: MReflection::MReflection(): Methoded("reflection") {
1.2 misha 358: // ^reflection:create[class_name;constructor_name[;param1[;param2[;...]]]]
359: add_native_method("create", Method::CT_STATIC, _create, 2, 102);
1.1 misha 360:
1.3 misha 361: // ^reflection:classes[]
362: add_native_method("classes", Method::CT_STATIC, _classes, 0, 0);
363:
1.1 misha 364: // ^reflection:class[object]
365: add_native_method("class", Method::CT_STATIC, _class, 1, 1);
366:
367: // ^reflection:class_name[object]
368: add_native_method("class_name", Method::CT_STATIC, _class_name, 1, 1);
369:
1.2 misha 370: // ^reflection:base_class[object]
371: add_native_method("base", Method::CT_STATIC, _base, 1, 1);
372:
373: // ^reflection:base_class_name[object]
374: add_native_method("base_name", Method::CT_STATIC, _base_name, 1, 1);
375:
1.30 ! misha 376: // ^reflection:def[class|...;name]
! 377: add_native_method("def", Method::CT_STATIC, _def, 2, 2);
! 378:
1.2 misha 379: // ^reflection:methods[class_name]
1.1 misha 380: add_native_method("methods", Method::CT_STATIC, _methods, 1, 1);
1.2 misha 381:
1.28 misha 382: // ^reflection:method[object or class;method_name]
383: add_native_method("method", Method::CT_STATIC, _method, 2, 2);
384:
385: // ^reflection:method_info[class_name;method_name]
386: add_native_method("method_info", Method::CT_STATIC, _method_info, 2, 2);
387:
1.13 misha 388: // ^reflection:fields[object or class]
389: add_native_method("fields", Method::CT_STATIC, _fields, 1, 1);
390:
1.28 misha 391: // ^reflection:field[object or class;field_name]
392: add_native_method("field", Method::CT_STATIC, _field, 2, 2);
1.9 misha 393:
394: // ^reflection:dynamical[[object or class, caller if absent]]
395: add_native_method("dynamical", Method::CT_STATIC, _dynamical, 0, 1);
1.19 moko 396:
397: // ^reflection:copy[src;dst]
398: add_native_method("copy", Method::CT_STATIC, _copy, 2, 2);
1.25 moko 399:
400: // ^reflection:uid[object or class]
401: add_native_method("uid", Method::CT_STATIC, _uid, 1, 1);
1.26 misha 402:
403: // ^reflection:delete[object or class;field_name]
404: add_native_method("delete", Method::CT_STATIC, _delete, 2, 2);
1.1 misha 405: }
E-mail: