|
|
| version 1.45, 2016/07/20 14:51:16 | version 1.52, 2016/09/19 22:27:57 |
|---|---|
| Line 8 | Line 8 |
| #include "pa_vmethod_frame.h" | #include "pa_vmethod_frame.h" |
| #include "pa_request.h" | #include "pa_request.h" |
| #include "pa_vbool.h" | #include "pa_vbool.h" |
| #include "pa_vobject.h" | |
| volatile const char * IDENT_REFLECTION_C="$Id$"; | volatile const char * IDENT_REFLECTION_C="$Id$"; |
| Line 42 DECLARE_CLASS_VAR(reflection, new MRefle | Line 43 DECLARE_CLASS_VAR(reflection, new MRefle |
| static void _create(Request& r, MethodParams& params) { | static void _create(Request& r, MethodParams& params) { |
| const String& class_name=params.as_string(0, "class_name must be string"); | const String& class_name=params.as_string(0, "class_name must be string"); |
| Value* class_value=r.get_class(class_name); | VStateless_class* vclass=r.get_class(class_name); |
| if(!class_value) | if(!vclass) |
| throw Exception(PARSER_RUNTIME, | throw Exception(PARSER_RUNTIME, &class_name, "class is undefined"); |
| &class_name, | |
| "class is undefined"); | |
| const String& constructor_name=params.as_string(1, "constructor_name must be string"); | const String& constructor_name=params.as_string(1, "constructor_name must be string"); |
| Value* constructor_value=class_value->get_element(constructor_name); | Value* constructor_value=vclass->get_element(constructor_name); |
| if(!constructor_value || !constructor_value->get_junction()) | if(!constructor_value || !constructor_value->get_junction()) |
| throw Exception(PARSER_RUNTIME, | throw Exception(PARSER_RUNTIME, &constructor_name, "constructor must be declared in class '%s'", vclass->type()); |
| &constructor_name, | |
| "constructor must be declared in class '%s'", | |
| class_value->type()); | |
| Junction* junction=constructor_value->get_junction(); | Junction* junction=constructor_value->get_junction(); |
| const Method* method=junction->method; | const Method* method=junction->method; |
| Line 69 static void _create(Request& r, MethodPa | Line 65 static void _create(Request& r, MethodPa |
| throw Exception(PARSER_RUNTIME, | throw Exception(PARSER_RUNTIME, |
| &constructor_name, | &constructor_name, |
| "native method of class '%s' is not allowed to be called dynamically", | "native method of class '%s' is not allowed to be called dynamically", |
| class_value->type()); | vclass->type()); |
| if(nparams<method->min_numbered_params_count) | if(nparams<method->min_numbered_params_count) |
| throw Exception(PARSER_RUNTIME, | throw Exception(PARSER_RUNTIME, |
| &constructor_name, | &constructor_name, |
| "native method of class '%s' accepts minimum %d parameter(s) (%d passed)", | "native method of class '%s' accepts minimum %d parameter(s) (%d passed)", |
| class_value->type(), | vclass->type(), |
| method->min_numbered_params_count, | method->min_numbered_params_count, |
| nparams); | nparams); |
| Line 88 static void _create(Request& r, MethodPa | Line 84 static void _create(Request& r, MethodPa |
| throw Exception(PARSER_RUNTIME, | throw Exception(PARSER_RUNTIME, |
| &constructor_name, | &constructor_name, |
| "method of class '%s' accepts maximum %d parameter(s) (%d passed)", | "method of class '%s' accepts maximum %d parameter(s) (%d passed)", |
| class_value->type(), | vclass->type(), |
| max_params_count, | max_params_count, |
| nparams); | nparams); |
| Value &object = r.construct(*class_value, *method); | Value &object = r.construct(*vclass, *method); |
| VConstructorFrame frame(*method, r.get_method_frame(), object); | VConstructorFrame frame(*method, r.get_method_frame(), object); |
| Value* v[100]; | Value* v[100]; |
| Line 109 static void _create(Request& r, MethodPa | Line 105 static void _create(Request& r, MethodPa |
| } | } |
| static void store_vlass_info( | |
| HashStringValue::key_type key, | |
| HashStringValue::value_type value, | |
| HashStringValue* result | |
| ){ | |
| Value* v; | |
| if(value->get_class()) | |
| v=new VString(class_type_methoded); | |
| else | |
| v=VVoid::get(); | |
| result->put(key, v); | |
| } | |
| static void _classes(Request& r, MethodParams&) { | static void _classes(Request& r, MethodParams&) { |
| VHash& result=*new VHash; | VHash& result=*new VHash; |
| r.classes().for_each(store_vlass_info, result.get_hash()); | for(HashString<VStateless_class*>::Iterator i(r.classes()); i; i.next()){ |
| result.hash().put(i.key(), i.value()->get_methods().count()>0 ? new VString(class_type_methoded) : VVoid::get() ); | |
| } | |
| r.write_no_lang(result); | r.write_no_lang(result); |
| } | } |
| Line 133 static Value* get_class(Value* value){ | Line 118 static Value* get_class(Value* value){ |
| if(VStateless_class* result=value->get_class()) | if(VStateless_class* result=value->get_class()) |
| return result; | return result; |
| else | else |
| // classes with fields only, like env & console | // junction |
| return value; | return value; |
| } | } |
| static const String* get_class_name(Value* value){ | static const String* get_class_name(Value* value){ |
| if(VStateless_class* lclass=value->get_class()) | if(VStateless_class* vclass=value->get_class()) |
| return new String(lclass->type()); | return new String(vclass->type()); |
| else | else |
| // classes with fields only, like env & console | // junction |
| return new String(value->type()); | return new String(value->type()); |
| } | } |
| Line 164 static void _class_by_name(Request& r, M | Line 149 static void _class_by_name(Request& r, M |
| } | } |
| static void _base(Request& r, MethodParams& params) { | static void _base(Request& r, MethodParams& params) { |
| if(VStateless_class* lclass=params[0].get_class()) | if(VStateless_class* vclass=params[0].get_class()) |
| if(Value* base=lclass->base()){ | if(Value* base=vclass->base()){ |
| r.write_no_lang(*get_class(base)); | r.write_no_lang(*get_class(base)); |
| return; | return; |
| } | } |
| // classes with fields only, like env & console or without base | // 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) { | static void _base_name(Request& r, MethodParams& params) { |
| if(VStateless_class* lclass=params[0].get_class()) | if(VStateless_class* vclass=params[0].get_class()) |
| if(Value* base=lclass->base()) | if(Value* base=vclass->base()) |
| r.write_no_lang(*get_class_name(base)); | r.write_no_lang(*get_class_name(base)); |
| } | } |
| static void store_method_info( | |
| HashStringMethod::key_type key, | |
| HashStringMethod::value_type method, | |
| HashStringValue* result | |
| ) { | |
| result->put(key, new VString(method->native_code?method_type_native:method_type_parser)); | |
| } | |
| static void _def(Request& r, MethodParams& params) { | static void _def(Request& r, MethodParams& params) { |
| const String& type=params.as_string(0, "type must be string"); | const String& type=params.as_string(0, "type must be string"); |
| if(type == def_class) { | if(type == def_class) { |
| const String& name=params.as_string(1, "name must be string"); | const String& name=params.as_string(1, "name must be string"); |
| // can't use get_class because it will call @autouse[] if the class wasn't loaded | |
| r.write_no_lang(VBool::get(r.classes().get(name)!=0)); | r.write_no_lang(VBool::get(r.classes().get(name)!=0)); |
| } else { | } else { |
| throw Exception(PARSER_RUNTIME, &type, "is invalid type, must be '%s'", def_class.cstr()); | throw Exception(PARSER_RUNTIME, &type, "is invalid type, must be '%s'", def_class.cstr()); |
| Line 201 static void _def(Request& r, MethodParam | Line 179 static void _def(Request& r, MethodParam |
| static void _methods(Request& r, MethodParams& params) { | static void _methods(Request& r, MethodParams& params) { |
| const String& class_name=params.as_string(0, "class_name must be string"); | const String& class_name=params.as_string(0, "class_name must be string"); |
| Value* class_value=r.get_class(class_name); | VStateless_class* vclass=r.get_class(class_name); |
| if(!class_value) | if(!vclass) |
| throw Exception(PARSER_RUNTIME, | throw Exception(PARSER_RUNTIME, &class_name, "class is undefined"); |
| &class_name, | |
| "class is undefined"); | |
| VHash& result=*new VHash; | VHash& result=*new VHash; |
| if(VStateless_class* lclass=class_value->get_class()) { | for(HashStringMethod::Iterator i(vclass->get_methods()); i; i.next()){ |
| HashStringMethod methods=lclass->get_methods(); | result.hash().put(i.key(), new VString(i.value()->native_code ? method_type_native : method_type_parser)); |
| methods.for_each(store_method_info, result.get_hash()); | |
| } else { | |
| // class which does not have methods (env, console, etc) | |
| } | } |
| r.write_no_lang(result); | r.write_no_lang(result); |
| } | } |
| static VJunction &method_junction(Value &self, Method &method, const String *name=0){ | |
| if(method.native_code) | |
| throw Exception(PARSER_RUNTIME, name, "method must not be native"); | |
| if(!(dynamic_cast<VObject*>(&self) || dynamic_cast<VClass*>(&self))) | |
| throw Exception(PARSER_RUNTIME, 0, "self must be parser object or class"); | |
| return *method.get_vjunction(self); | |
| } | |
| static void _method(Request& r, MethodParams& params) { | static void _method(Request& r, MethodParams& params) { |
| Value& o=params.as_no_junction(0, "first param must be object or class, not junction"); | Value &source=*params.get(0); |
| if(Junction *j=source.get_junction()){ | |
| if(Method* method=const_cast<Method*>(j->method)){ | |
| Value& self=params.count()>1 ? params.as_no_junction(1, "self must be object, not junction") : r.get_method_frame()->caller()->self(); | |
| r.write_no_lang(method_junction(self, *method)); | |
| return; | |
| } | |
| throw Exception(PARSER_RUNTIME, 0, "param must be method junction"); | |
| } | |
| if(params.count()==1) | |
| throw Exception(PARSER_RUNTIME, 0, "method name must be specified"); | |
| const String& name=params.as_string(1, "method name must be string"); | const String& name=params.as_string(1, "method name must be string"); |
| if(VStateless_class* lclass=o.get_class()) { | if(VStateless_class* vclass=source.get_class()) { |
| if(Method* method=lclass->get_method(name)) | if(Method* method=vclass->get_method(name)){ |
| r.write_no_lang(*method->get_vjunction(o)); | 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) ); |
| } else { | return; |
| // class which does not have methods (env, console, etc) | } |
| } | } |
| r.write_value(*VVoid::get()); | |
| } | } |
| static void _fields(Request& r, MethodParams& params) { | static void _fields(Request& r, MethodParams& params) { |
| Line 239 static void _fields(Request& r, MethodPa | Line 237 static void _fields(Request& r, MethodPa |
| } | } |
| static void _fields_reference(Request& r, MethodParams& params) { | static void _fields_reference(Request& r, MethodParams& params) { |
| Value& o=params.as_no_junction(0, "param must be object or class, not junction"); | Value& o=params.as_no_junction(0, "param must be object or hash, not junction"); |
| if(HashStringValue* fields=o.get_fields_reference()) | if(HashStringValue* fields=o.get_fields_reference()) |
| r.write_no_lang(*new VHashReference(*fields)); | r.write_no_lang(*new VHashReference(*fields)); |
| else | else |
| throw Exception(PARSER_RUNTIME, 0, "param must be object or class"); | throw Exception(PARSER_RUNTIME, 0, "param must be object or hash"); |
| } | } |
| static void _field(Request& r, MethodParams& params) { | static void _field(Request& r, MethodParams& params) { |
| Line 258 static void _field(Request& r, MethodPar | Line 256 static void _field(Request& r, MethodPar |
| static void _method_info(Request& r, MethodParams& params) { | static void _method_info(Request& r, MethodParams& params) { |
| const String& class_name=params.as_string(0, "class_name must be string"); | const String& class_name=params.as_string(0, "class_name must be string"); |
| Value* class_value=r.get_class(class_name); | VStateless_class* vclass=r.get_class(class_name); |
| if(!class_value) | if(!vclass) |
| throw Exception(PARSER_RUNTIME, &class_name, "class is undefined"); | throw Exception(PARSER_RUNTIME, &class_name, "class is undefined"); |
| VStateless_class* lclass=class_value->get_class(); | |
| if(!lclass) | |
| throw Exception(PARSER_RUNTIME, &class_name, "class does not have methods"); | |
| const String& method_name=params.as_string(1, "method_name must be string"); | const String& method_name=params.as_string(1, "method_name must be string"); |
| Method* method=lclass->get_method(method_name); | Method* method=vclass->get_method(method_name); |
| if(!method) | if(!method) |
| throw Exception(PARSER_RUNTIME, &method_name, "method not found in class %s", class_name.cstr()); | throw Exception(PARSER_RUNTIME, &method_name, "method not found in class '%s'", vclass->type()); |
| VHash& result=*new VHash; | VHash& result=*new VHash; |
| HashStringValue* hash=result.get_hash(); | HashStringValue* hash=result.get_hash(); |
| VStateless_class* c=lclass; | VStateless_class* c=vclass; |
| Method* base_method; | Method* base_method; |
| if(c->base() && (base_method=c->base()->get_method(method_name))){ | if(c->base() && (base_method=c->base()->get_method(method_name))){ |
| c=c->base()->get_class(); | c=c->base()->get_class(); |
| Line 405 MReflection::MReflection(): Methoded("re | Line 399 MReflection::MReflection(): Methoded("re |
| add_native_method("methods", Method::CT_STATIC, _methods, 1, 1); | add_native_method("methods", Method::CT_STATIC, _methods, 1, 1); |
| // ^reflection:method[object or class;method_name] | // ^reflection:method[object or class;method_name] |
| add_native_method("method", Method::CT_STATIC, _method, 2, 2); | add_native_method("method", Method::CT_STATIC, _method, 1, 3); |
| // ^reflection:method_info[class_name;method_name] | // ^reflection:method_info[class_name;method_name] |
| add_native_method("method_info", Method::CT_STATIC, _method_info, 2, 2); | add_native_method("method_info", Method::CT_STATIC, _method_info, 2, 2); |