Annotation of parser3/src/classes/reflection.C, revision 1.72
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.72 ! moko 13: volatile const char * IDENT_REFLECTION_C="$Id: reflection.C,v 1.71 2016/11/30 21:30:24 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.59 moko 20: static const String method_name("name");
1.71 moko 21: static const String method_class_name("class");
1.5 misha 22: static const String method_call_type("call_type");
1.8 misha 23: static const String method_inherited("inherited");
1.23 misha 24: static const String method_overridden("overridden");
1.2 misha 25:
1.3 misha 26: static const String method_min_params("min_params");
27: static const String method_max_params("max_params");
1.29 misha 28: static const String method_extra_param("extra_param");
1.2 misha 29:
1.30 misha 30: static const String def_class("class");
31:
1.1 misha 32: // class
33:
34: class MReflection: public Methoded {
35: public:
36: MReflection();
37: };
38:
39: // global variable
40:
1.40 moko 41: DECLARE_CLASS_VAR(reflection, new MReflection);
1.1 misha 42:
43: // methods
44:
1.2 misha 45:
46: static void _create(Request& r, MethodParams& params) {
1.72 ! moko 47: const Method* method;
! 48: const String& class_name=params.as_string(0, "class name must be string");
1.48 moko 49: VStateless_class* vclass=r.get_class(class_name);
1.1 misha 50:
1.48 moko 51: if(!vclass)
52: throw Exception(PARSER_RUNTIME, &class_name, "class is undefined");
1.1 misha 53:
1.72 ! moko 54: const String& constructor_name=params.as_string(1, "constructor name must be string");
1.1 misha 55:
1.72 ! moko 56: if(!(method=vclass->get_method(constructor_name)))
! 57: throw Exception(PARSER_RUNTIME, &constructor_name, "constructor not found in class '%s'", vclass->type());
1.1 misha 58:
1.72 ! moko 59: Value &object = r.construct(*vclass, *method);
1.2 misha 60:
61: int nparams=params.count()-2;
1.1 misha 62:
1.65 moko 63: CONSTRUCTOR_FRAME_ACTION(*method, r.get_method_frame(), object, {
64: Value* v[100];
65: if(nparams>0){
66: for(int i=0; i<nparams; i++)
67: v[i]=&r.process(params[i+2]);
68: frame.store_params((Value**)&v, nparams);
69: } else {
70: frame.empty_params();
71: }
72: r.call(frame);
73: object.enable_default_setter();
74: r.write(frame.result());
75: });
1.1 misha 76: }
77:
1.2 misha 78:
1.3 misha 79: static void _classes(Request& r, MethodParams&) {
80: VHash& result=*new VHash;
1.47 moko 81: for(HashString<VStateless_class*>::Iterator i(r.classes()); i; i.next()){
82: result.hash().put(i.key(), i.value()->get_methods().count()>0 ? new VString(class_type_methoded) : VVoid::get() );
83: }
1.64 moko 84: r.write(result);
1.3 misha 85: }
86:
87:
1.53 moko 88: static Value& get_class(Value& value){
89: if(VStateless_class* result=value.get_class())
90: return *result;
91: else {
92: // we can't return code junction to outside as it's stack value
93: if(Junction *j=value.get_junction())
94: if(j->code)
95: throw Exception(PARSER_RUNTIME, 0, "param must not be code junction");
96: // method junction
1.3 misha 97: return value;
1.53 moko 98: }
1.3 misha 99: }
100:
1.53 moko 101: static const String& get_class_name(Value& value){
102: return *new String(get_class(value).type());
1.3 misha 103: }
104:
105:
1.1 misha 106: static void _class(Request& r, MethodParams& params) {
1.64 moko 107: r.write(get_class(params[0]));
1.1 misha 108: }
109:
1.2 misha 110:
1.1 misha 111: static void _class_name(Request& r, MethodParams& params) {
1.64 moko 112: r.write(get_class_name(params[0]));
1.1 misha 113: }
114:
1.34 moko 115: static void _class_by_name(Request& r, MethodParams& params) {
116: const String& class_name=params.as_string(0, "class_name must be string");
117: Value* class_value=r.get_class(class_name);
118: if(!class_value)
119: throw Exception(PARSER_RUNTIME, &class_name, "class is undefined");
1.64 moko 120: r.write(*class_value);
1.34 moko 121: }
1.1 misha 122:
1.2 misha 123: static void _base(Request& r, MethodParams& params) {
1.49 moko 124: if(VStateless_class* vclass=params[0].get_class())
125: if(Value* base=vclass->base()){
1.64 moko 126: r.write(get_class(*base));
1.3 misha 127: return;
128: }
129:
130: // classes with fields only, like env & console or without base
1.52 moko 131: r.write_value(*VVoid::get());
1.2 misha 132: }
1.1 misha 133:
134:
1.2 misha 135: static void _base_name(Request& r, MethodParams& params) {
1.49 moko 136: if(VStateless_class* vclass=params[0].get_class())
137: if(Value* base=vclass->base())
1.64 moko 138: r.write(get_class_name(*base));
1.2 misha 139: }
1.1 misha 140:
1.30 misha 141: static void _def(Request& r, MethodParams& params) {
142: const String& type=params.as_string(0, "type must be string");
143: if(type == def_class) {
144: const String& name=params.as_string(1, "name must be string");
1.47 moko 145: // can't use get_class because it will call @autouse[] if the class wasn't loaded
1.64 moko 146: r.write(VBool::get(r.classes().get(name)!=0));
1.30 misha 147: } else {
1.31 misha 148: throw Exception(PARSER_RUNTIME, &type, "is invalid type, must be '%s'", def_class.cstr());
1.30 misha 149: }
150: }
151:
1.1 misha 152: static void _methods(Request& r, MethodParams& params) {
153: const String& class_name=params.as_string(0, "class_name must be string");
1.49 moko 154: VStateless_class* vclass=r.get_class(class_name);
155: if(!vclass)
156: throw Exception(PARSER_RUNTIME, &class_name, "class is undefined");
1.1 misha 157:
158: VHash& result=*new VHash;
1.49 moko 159: for(HashStringMethod::Iterator i(vclass->get_methods()); i; i.next()){
160: result.hash().put(i.key(), new VString(i.value()->native_code ? method_type_native : method_type_parser));
1.1 misha 161: }
1.49 moko 162:
1.64 moko 163: r.write(result);
1.1 misha 164: }
165:
1.57 moko 166: static VJunction &method_junction(Value &self, Method &method){
1.50 moko 167: if(method.native_code)
1.57 moko 168: throw Exception(PARSER_RUNTIME, method.name, "method must not be native");
1.50 moko 169:
1.51 moko 170: if(!(dynamic_cast<VObject*>(&self) || dynamic_cast<VClass*>(&self)))
171: throw Exception(PARSER_RUNTIME, 0, "self must be parser object or class");
1.50 moko 172:
173: return *method.get_vjunction(self);
174: }
175:
1.28 misha 176: static void _method(Request& r, MethodParams& params) {
1.62 moko 177: Value &source=params[0];
1.50 moko 178:
179: if(Junction *j=source.get_junction()){
180: if(Method* method=const_cast<Method*>(j->method)){
181: Value& self=params.count()>1 ? params.as_no_junction(1, "self must be object, not junction") : r.get_method_frame()->caller()->self();
1.64 moko 182: r.write(method_junction(self, *method));
1.50 moko 183: return;
184: }
1.51 moko 185: throw Exception(PARSER_RUNTIME, 0, "param must be method junction");
1.50 moko 186: }
187:
188: if(params.count()==1)
1.51 moko 189: throw Exception(PARSER_RUNTIME, 0, "method name must be specified");
1.50 moko 190:
1.28 misha 191: const String& name=params.as_string(1, "method name must be string");
192:
1.50 moko 193: if(VStateless_class* vclass=source.get_class()) {
194: if(Method* method=vclass->get_method(name)){
1.64 moko 195: r.write( params.count()>2 ? method_junction(params.as_no_junction(2, "self must be object, not junction"), *method) : *method->get_vjunction(source) );
1.50 moko 196: return;
197: }
1.28 misha 198: }
1.52 moko 199: r.write_value(*VVoid::get());
1.28 misha 200: }
201:
1.13 misha 202: static void _fields(Request& r, MethodParams& params) {
1.28 misha 203: Value& o=params.as_no_junction(0, "param must be object or class, not junction");
204:
1.44 moko 205: if(HashStringValue* fields=o.get_fields())
1.64 moko 206: r.write(*new VHash(*fields));
1.44 moko 207: else
1.64 moko 208: r.write(*new VHash());
1.13 misha 209: }
1.2 misha 210:
1.44 moko 211: static void _fields_reference(Request& r, MethodParams& params) {
1.46 moko 212: Value& o=params.as_no_junction(0, "param must be object or hash, not junction");
1.44 moko 213:
1.45 moko 214: if(HashStringValue* fields=o.get_fields_reference())
1.64 moko 215: r.write(*new VHashReference(*fields));
1.44 moko 216: else
1.46 moko 217: throw Exception(PARSER_RUNTIME, 0, "param must be object or hash");
1.44 moko 218: }
219:
1.28 misha 220: static void _field(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, "field name must be string");
223:
224: if(HashStringValue* fields=o.get_fields())
225: if(Value* value=fields->get(name))
1.64 moko 226: r.write(*value);
1.28 misha 227: }
228:
1.8 misha 229: static void _method_info(Request& r, MethodParams& params) {
1.59 moko 230: const Method* method;
1.66 moko 231:
232: VHash& result=*new VHash;
233: HashStringValue* hash=result.get_hash();
1.2 misha 234:
1.62 moko 235: if(Junction *j=params[0].get_junction()){
1.59 moko 236: if(!(method=j->method))
237: throw Exception(PARSER_RUNTIME, 0, "param must be class name or method junction");
1.66 moko 238:
239: hash->put(method_name, new VString(*method->name));
1.71 moko 240: hash->put(method_class_name, new VString(*new String(j->self.type())));
241:
1.59 moko 242: } else {
243: const String& class_name=params.as_string(0, "param must be class name or method junction");
1.66 moko 244: VStateless_class* vclass=r.get_class(class_name);
245:
246: if(!vclass)
1.59 moko 247: throw Exception(PARSER_RUNTIME, &class_name, "class is undefined");
248:
249: if(params.count()==1)
250: throw Exception(PARSER_RUNTIME, 0, "method name must be specified");
251:
252: const String& method_name=params.as_string(1, "method name must be string");
253: if(!(method=vclass->get_method(method_name)))
254: throw Exception(PARSER_RUNTIME, &method_name, "method not found in class '%s'", vclass->type());
1.2 misha 255:
1.66 moko 256: Method* base_method;
257: if(vclass && vclass->base() && (base_method=vclass->base()->get_method(*method->name))){
258: VStateless_class* c=vclass->base()->get_class();
259: while(c->base() && base_method==c->base()->get_method(*method->name))
260: c=c->base()->get_class();
261: hash->put((base_method==method) ? method_inherited : method_overridden, new VString(*new String(c->type())));
262: }
1.23 misha 263: }
1.8 misha 264:
1.29 misha 265: Value* call_type=0;
266: switch(method->call_type){
267: case Method::CT_DYNAMIC:
1.42 moko 268: call_type=new VString(Symbols::DYNAMIC_SYMBOL);
1.29 misha 269: break;
270: case Method::CT_STATIC:
1.42 moko 271: call_type=new VString(Symbols::STATIC_SYMBOL);
1.29 misha 272: break;
1.32 moko 273: case Method::CT_ANY:
274: break;
1.29 misha 275: }
276: if(call_type)
277: hash->put(method_call_type, call_type);
278:
1.2 misha 279: if(method->native_code){
280: // native code
281: hash->put(method_min_params, new VInt(method->min_numbered_params_count));
282: hash->put(method_max_params, new VInt(method->max_numbered_params_count));
283: } else {
284: // parser code
1.68 moko 285: const String* filespec = r.get_method_filespec(method);
1.16 misha 286: if( filespec )
287: hash->put("file", new VString(*filespec));
1.29 misha 288:
289: hash->put(method_max_params, new VInt(method->params_names ? method->params_names->count() : 0));
290:
1.2 misha 291: if(method->params_names)
292: for(size_t i=0; i<method->params_names->count(); i++)
293: hash->put(String::Body::Format(i), new VString(*method->params_names->get(i)));
1.29 misha 294:
295: if(method->extra_params)
296: hash->put(method_extra_param, new VString(*method->extra_params));
1.2 misha 297: }
298:
1.64 moko 299: r.write(result);
1.2 misha 300: }
301:
1.67 moko 302: static void _filename(Request& r, MethodParams& params) {
1.68 moko 303: if(Junction *j=params[0].get_junction()){
304: if(const Method* method=j->method){
305: if(!method->native_code)
306: if(const String* filespec = r.get_method_filespec(method))
307: r.write(*new VString(*filespec));
308: return;
309: }
310: throw Exception(PARSER_RUNTIME, 0, "param must be object, class or method junction");
311: }
1.67 moko 312:
1.68 moko 313: if(VClass* vclass = dynamic_cast<VClass*>(params[0].get_class())){
314: r.write(*new VString(vclass->get_filespec()));
1.67 moko 315: }
316: }
317:
1.9 misha 318: static void _dynamical(Request& r, MethodParams& params) {
319: if(params.count()){
1.64 moko 320: r.write(VBool::get(params[0].get_class() != ¶ms[0]));
1.9 misha 321: } else {
322: VMethodFrame* caller=r.get_method_frame()->caller();
1.64 moko 323: r.write(VBool::get(caller && caller->get_class() != &caller->self()));
1.9 misha 324: }
325: }
326:
1.35 moko 327: static void _is(Request& r, MethodParams& params) {
1.39 moko 328: const String& name=params.as_string(0, "element name must be string");
329: const String& type=params.as_string(1, "class name must be string");
330: Value *context=params.count()==3 ? &(params.as_no_junction(2, "context must not be code")) : r.get_method_frame()->caller();
1.37 moko 331: Value *value=context ? context->get_element(name) : 0;
1.35 moko 332:
333: if(value) {
334: if(type == "code" || type == "method") {
335: Junction *junction=value->get_junction();
1.64 moko 336: r.write(VBool::get(junction && ((junction->code==0) ^ (type == "code"))) );
1.35 moko 337: } else {
1.64 moko 338: r.write(VBool::get( value->is(type.cstr()) ));
1.35 moko 339: }
340: } else
1.64 moko 341: r.write(VBool::get(type == "void"));
1.35 moko 342: }
343:
1.19 moko 344: static void _copy(Request& r, MethodParams& params) {
345: HashStringValue* src=params.as_no_junction(0, "source must not be code").get_hash();
346:
1.44 moko 347: if(src==NULL)
1.19 moko 348: throw Exception(PARSER_RUNTIME, 0, "source must have hash representation");
349:
350: Value& dst=params.as_no_junction(1, "destination must not be code");
351:
352: for(HashStringValue::Iterator i(*src); i; i.next())
1.20 moko 353: r.put_element(dst, *new String(i.key(), String::L_TAINTED), i.value());
1.19 moko 354: }
355:
1.25 moko 356: static void _uid(Request& r, MethodParams& params) {
357: Value& obj=params.as_no_junction(0, "object must not be code");
358:
359: char local_buf[MAX_NUMBER];
360: int size=snprintf(local_buf, sizeof(local_buf), "%p", &obj);
361:
1.64 moko 362: r.write(*new String(String::C(pa_strdup(local_buf, (size_t)size), size)));
1.25 moko 363: }
364:
1.27 moko 365: static void _delete(Request&, MethodParams& params) {
1.58 moko 366: Value* v=¶ms.as_no_junction(0, "param must be object or class, not junction");
1.26 misha 367: const String& key=params.as_string(1, "field name must be string");
1.58 moko 368:
369: if(VObject* o=dynamic_cast<VObject*>(v)){
370: o->get_fields()->remove(key);
371: } else if(VClass* c=dynamic_cast<VClass*>(v)){
372: HashStringProperty &p=*c->get_properties();
373: if(Property* property=p.get(key))
374: if(property->value)
375: p.remove(key);
1.26 misha 376: }
377: }
378:
1.55 moko 379: static void _mixin(Request& r, MethodParams& params) {
380: Value& vsource=params.as_no_junction(0, "source must not be code");
381:
382: Value* vtarget=0;
383: const String *name=0;
384: bool copy_methods=true;
385: bool copy_fields=true;
1.60 moko 386: bool overwrite=false;
1.55 moko 387:
388: if(params.count()>1)
389: if(HashStringValue* options=params.as_hash(1, "mixin options")) {
390: int valid_options=0;
391: if(vtarget=options->get("to")) {
392: valid_options++;
393: }
394: if(Value* vname=options->get("name")) {
395: name=&vname->as_string();
396: valid_options++;
397: }
398: if(Value* vmethods=options->get("methods")) {
1.61 moko 399: copy_methods=r.process(*vmethods).as_bool();
1.55 moko 400: valid_options++;
401: }
402: if(Value* vfields=options->get("fields")) {
1.61 moko 403: copy_fields=r.process(*vfields).as_bool();
1.55 moko 404: valid_options++;
405: }
406: if(Value* voverwrite=options->get("overwrite")) {
1.61 moko 407: overwrite=r.process(*voverwrite).as_bool();
1.55 moko 408: valid_options++;
409: }
410: if(valid_options!=options->count())
411: throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
412: }
413:
414: if(!vtarget)
415: vtarget=&r.get_method_frame()->caller()->self();
416:
417: VClass* source=dynamic_cast<VClass*>(vsource.get_class());
418: VClass* target=dynamic_cast<VClass*>(vtarget->get_class());
419:
420: if(!source)
421: throw Exception(PARSER_RUNTIME, 0, "source must be parser object or class");
422: if(!target)
423: throw Exception(PARSER_RUNTIME, 0, "destination must be parser object or class");
424:
425: if(name){
426: if(copy_methods)
427: if(Method* method=source->get_method(*name))
428: if(overwrite || !target->get_method(*name)){
1.59 moko 429: target->set_method(*name, new Method(*method));
1.55 moko 430: }
431:
432: if(copy_fields)
433: if(Property* property=source->get_properties()->get(*name))
434: if(property->value && (overwrite || !target->get_properties()->get(*name))){
435: target->put_element(*target, *name, property->value);
436: }
437:
438: } else {
439: if(copy_methods)
440: for(HashStringMethod::Iterator i(source->get_methods()); i; i.next()){
441: if(overwrite || !target->get_method(i.key()))
1.59 moko 442: target->set_method(*i.value()->name, new Method(*i.value()));
1.55 moko 443: }
444: if(copy_fields)
445: for(HashStringProperty::Iterator i(*source->get_properties()); i; i.next()){
446: if(i.value()->value && ( overwrite || !target->get_properties()->get(i.key()) ))
447: target->put_element(*target, *new String(i.key(), String::L_TAINTED), i.value()->value);
448: }
449: }
450: }
451:
1.26 misha 452:
1.1 misha 453: // constructor
454: MReflection::MReflection(): Methoded("reflection") {
1.2 misha 455: // ^reflection:create[class_name;constructor_name[;param1[;param2[;...]]]]
1.72 ! moko 456: add_native_method("create", Method::CT_STATIC, _create, 1, 101);
1.1 misha 457:
1.3 misha 458: // ^reflection:classes[]
459: add_native_method("classes", Method::CT_STATIC, _classes, 0, 0);
460:
1.1 misha 461: // ^reflection:class[object]
462: add_native_method("class", Method::CT_STATIC, _class, 1, 1);
463:
464: // ^reflection:class_name[object]
465: add_native_method("class_name", Method::CT_STATIC, _class_name, 1, 1);
466:
1.34 moko 467: // ^reflection:class_by_name[class_name]
468: add_native_method("class_by_name", Method::CT_STATIC, _class_by_name, 1, 1);
469:
1.2 misha 470: // ^reflection:base_class[object]
471: add_native_method("base", Method::CT_STATIC, _base, 1, 1);
472:
473: // ^reflection:base_class_name[object]
474: add_native_method("base_name", Method::CT_STATIC, _base_name, 1, 1);
475:
1.30 misha 476: // ^reflection:def[class|...;name]
477: add_native_method("def", Method::CT_STATIC, _def, 2, 2);
478:
1.2 misha 479: // ^reflection:methods[class_name]
1.1 misha 480: add_native_method("methods", Method::CT_STATIC, _methods, 1, 1);
1.2 misha 481:
1.54 moko 482: // ^reflection:method[object or class;method_name[;self]]
483: // ^reflection:method[junction[;self]]
1.50 moko 484: add_native_method("method", Method::CT_STATIC, _method, 1, 3);
1.28 misha 485:
486: // ^reflection:method_info[class_name;method_name]
1.59 moko 487: // ^reflection:method_info[junction]
488: add_native_method("method_info", Method::CT_STATIC, _method_info, 1, 2);
1.28 misha 489:
1.67 moko 490: // ^reflection:filename[object or class]
491: add_native_method("filename", Method::CT_STATIC, _filename, 1, 1);
492:
1.13 misha 493: // ^reflection:fields[object or class]
494: add_native_method("fields", Method::CT_STATIC, _fields, 1, 1);
495:
1.44 moko 496: // ^reflection:fields_reference[object]
497: add_native_method("fields_reference", Method::CT_STATIC, _fields_reference, 1, 1);
498:
1.28 misha 499: // ^reflection:field[object or class;field_name]
500: add_native_method("field", Method::CT_STATIC, _field, 2, 2);
1.9 misha 501:
502: // ^reflection:dynamical[[object or class, caller if absent]]
503: add_native_method("dynamical", Method::CT_STATIC, _dynamical, 0, 1);
1.19 moko 504:
1.39 moko 505: // ^reflection:is[element_name;class_name|code|method[;context]]
1.35 moko 506: add_native_method("is", Method::CT_STATIC, _is, 2, 3);
507:
1.19 moko 508: // ^reflection:copy[src;dst]
509: add_native_method("copy", Method::CT_STATIC, _copy, 2, 2);
1.25 moko 510:
511: // ^reflection:uid[object or class]
512: add_native_method("uid", Method::CT_STATIC, _uid, 1, 1);
1.26 misha 513:
514: // ^reflection:delete[object or class;field_name]
515: add_native_method("delete", Method::CT_STATIC, _delete, 2, 2);
1.54 moko 516:
517: // ^reflection:mixin[object or class or junction;options]
1.55 moko 518: add_native_method("mixin", Method::CT_STATIC, _mixin, 1, 2);
1.54 moko 519:
1.1 misha 520: }
E-mail: