--- parser3/src/classes/reflection.C 2016/09/13 22:30:30 1.50 +++ parser3/src/classes/reflection.C 2016/09/23 15:56:51 1.54 @@ -10,7 +10,7 @@ #include "pa_vbool.h" #include "pa_vobject.h" -volatile const char * IDENT_REFLECTION_C="$Id: reflection.C,v 1.50 2016/09/13 22:30:30 moko Exp $"; +volatile const char * IDENT_REFLECTION_C="$Id: reflection.C,v 1.54 2016/09/23 15:56:51 moko Exp $"; static const String class_type_methoded("methoded"); @@ -114,30 +114,31 @@ static void _classes(Request& r, MethodP } -static Value* get_class(Value* value){ - if(VStateless_class* result=value->get_class()) - return result; - else - // junction +static Value& get_class(Value& value){ + if(VStateless_class* result=value.get_class()) + return *result; + else { + // we can't return code junction to outside as it's stack value + if(Junction *j=value.get_junction()) + if(j->code) + throw Exception(PARSER_RUNTIME, 0, "param must not be code junction"); + // method junction return value; + } } -static const String* get_class_name(Value* value){ - if(VStateless_class* vclass=value->get_class()) - return new String(vclass->type()); - else - // junction - return new String(value->type()); +static const String& get_class_name(Value& value){ + return *new String(get_class(value).type()); } static void _class(Request& r, MethodParams& params) { - r.write_no_lang(*get_class(¶ms[0])); + r.write_no_lang(get_class(params[0])); } static void _class_name(Request& r, MethodParams& params) { - r.write_no_lang(*get_class_name(¶ms[0])); + r.write_no_lang(get_class_name(params[0])); } static void _class_by_name(Request& r, MethodParams& params) { @@ -151,19 +152,19 @@ static void _class_by_name(Request& r, M static void _base(Request& r, MethodParams& params) { if(VStateless_class* vclass=params[0].get_class()) if(Value* base=vclass->base()){ - r.write_no_lang(*get_class(base)); + r.write_no_lang(get_class(*base)); return; } // classes with fields only, like env & console or without base - r.write_no_lang(*VVoid::get()); + r.write_value(*VVoid::get()); } static void _base_name(Request& r, MethodParams& params) { if(VStateless_class* vclass=params[0].get_class()) if(Value* base=vclass->base()) - r.write_no_lang(*get_class_name(base)); + r.write_no_lang(get_class_name(*base)); } static void _def(Request& r, MethodParams& params) { @@ -193,10 +194,10 @@ static void _methods(Request& r, MethodP static VJunction &method_junction(Value &self, Method &method, const String *name=0){ if(method.native_code) - throw Exception(PARSER_RUNTIME, name, "method should not be native"); + throw Exception(PARSER_RUNTIME, name, "method must not be native"); -// if(dynamic_cast(&self)) -// throw Exception(PARSER_RUNTIME, 0, "self should be parser object"); + if(!(dynamic_cast(&self) || dynamic_cast(&self))) + throw Exception(PARSER_RUNTIME, 0, "self must be parser object or class"); return *method.get_vjunction(self); } @@ -210,22 +211,21 @@ static void _method(Request& r, MethodPa r.write_no_lang(method_junction(self, *method)); return; } - throw Exception(PARSER_RUNTIME, 0, "param should be method junction"); + throw Exception(PARSER_RUNTIME, 0, "param must be method junction"); } if(params.count()==1) - throw Exception(PARSER_RUNTIME, 0, "method name should be specified"); + throw Exception(PARSER_RUNTIME, 0, "method name must be specified"); const String& name=params.as_string(1, "method name must be string"); - Value& self=params.count()>2 ? params.as_no_junction(2, "self must be object, not junction") : source; if(VStateless_class* vclass=source.get_class()) { if(Method* method=vclass->get_method(name)){ - r.write_no_lang(method_junction(self, *method, &name)); + 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) ); return; } } - throw Exception(PARSER_RUNTIME, &name, "method not found in class '%s'", source.type()); + r.write_value(*VVoid::get()); } static void _fields(Request& r, MethodParams& params) { @@ -399,7 +399,8 @@ MReflection::MReflection(): Methoded("re // ^reflection:methods[class_name] add_native_method("methods", Method::CT_STATIC, _methods, 1, 1); - // ^reflection:method[object or class;method_name] + // ^reflection:method[object or class;method_name[;self]] + // ^reflection:method[junction[;self]] add_native_method("method", Method::CT_STATIC, _method, 1, 3); // ^reflection:method_info[class_name;method_name] @@ -428,4 +429,8 @@ MReflection::MReflection(): Methoded("re // ^reflection:delete[object or class;field_name] add_native_method("delete", Method::CT_STATIC, _delete, 2, 2); + + // ^reflection:mixin[object or class or junction;options] + // add_native_method("mixin", Method::CT_STATIC, _mixin, 1, 2); + }