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