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