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