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