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