|
|
| version 1.35, 2015/10/25 22:24:05 | version 1.43, 2016/07/13 15:03:51 |
|---|---|
| Line 1 | Line 1 |
| /** @file | /** @file |
| Parser: @b reflection parser class. | Parser: @b reflection parser class. |
| Copyright (c) 2001-2012 Art. Lebedev Studio (http://www.artlebedev.com) | Copyright (c) 2001-2015 Art. Lebedev Studio (http://www.artlebedev.com) |
| Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru) | Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru) |
| */ | */ |
| Line 19 static const String method_type_parser(" | Line 19 static const String method_type_parser(" |
| static const String method_call_type("call_type"); | static const String method_call_type("call_type"); |
| static const String method_inherited("inherited"); | static const String method_inherited("inherited"); |
| static const String method_overridden("overridden"); | static const String method_overridden("overridden"); |
| static const String method_call_type_static("static"); | |
| static const String method_call_type_dynamic("dynamic"); | |
| static const String method_min_params("min_params"); | static const String method_min_params("min_params"); |
| static const String method_max_params("max_params"); | static const String method_max_params("max_params"); |
| Line 37 public: | Line 35 public: |
| // global variable | // global variable |
| DECLARE_CLASS_VAR(reflection, new MReflection, 0); | DECLARE_CLASS_VAR(reflection, new MReflection); |
| // methods | // methods |
| Line 58 static void _create(Request& r, MethodPa | Line 56 static void _create(Request& r, MethodPa |
| throw Exception(PARSER_RUNTIME, | throw Exception(PARSER_RUNTIME, |
| &constructor_name, | &constructor_name, |
| "constructor must be declared in class '%s'", | "constructor must be declared in class '%s'", |
| class_value->get_class()->name_cstr()); | 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 70 static void _create(Request& r, MethodPa | Line 68 static void _create(Request& r, MethodPa |
| if(method->call_type==Method::CT_STATIC) | if(method->call_type==Method::CT_STATIC) |
| throw Exception(PARSER_RUNTIME, | throw Exception(PARSER_RUNTIME, |
| &constructor_name, | &constructor_name, |
| "native method of class '%s' (%s) is not allowed to be called dynamically", | "native method of class '%s' is not allowed to be called dynamically", |
| class_value->get_class()->name_cstr(), | |
| class_value->type()); | class_value->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' (%s) accepts minimum %d parameter(s) (%d passed)", | "native method of class '%s' accepts minimum %d parameter(s) (%d passed)", |
| class_value->get_class()->name_cstr(), | |
| class_value->type(), | class_value->type(), |
| method->min_numbered_params_count, | method->min_numbered_params_count, |
| nparams); | nparams); |
| Line 91 static void _create(Request& r, MethodPa | Line 87 static void _create(Request& r, MethodPa |
| if(nparams>max_params_count) | if(nparams>max_params_count) |
| throw Exception(PARSER_RUNTIME, | throw Exception(PARSER_RUNTIME, |
| &constructor_name, | &constructor_name, |
| "method of class '%s' (%s) accepts maximum %d parameter(s) (%d passed)", | "method of class '%s' accepts maximum %d parameter(s) (%d passed)", |
| class_value->get_class()->name_cstr(), | |
| class_value->type(), | class_value->type(), |
| max_params_count, | max_params_count, |
| nparams); | nparams); |
| Line 144 static Value* get_class(Value* value){ | Line 139 static Value* get_class(Value* 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* lclass=value->get_class()) |
| return &lclass->name(); | return new String(lclass->type()); |
| else | else |
| // classes with fields only, like env & console | // classes with fields only, like env & console |
| return new String(value->type()); | return new String(value->type()); |
| Line 198 static void _def(Request& r, MethodParam | Line 193 static void _def(Request& r, MethodParam |
| 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"); |
| r.write_no_lang(VBool::get(r.get_class(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 284 static void _method_info(Request& r, Met | Line 279 static void _method_info(Request& r, Met |
| c=c->base()->get_class(); | c=c->base()->get_class(); |
| while(c->base() && base_method==c->base()->get_method(method_name)) | while(c->base() && base_method==c->base()->get_method(method_name)) |
| c=c->base()->get_class(); | c=c->base()->get_class(); |
| hash->put((base_method==method) ? method_inherited : method_overridden, new VString(c->name())); | hash->put((base_method==method) ? method_inherited : method_overridden, new VString(*new String(c->type()))); |
| } | } |
| Value* call_type=0; | Value* call_type=0; |
| switch(method->call_type){ | switch(method->call_type){ |
| case Method::CT_DYNAMIC: | case Method::CT_DYNAMIC: |
| call_type=new VString(method_call_type_dynamic); | call_type=new VString(Symbols::DYNAMIC_SYMBOL); |
| break; | break; |
| case Method::CT_STATIC: | case Method::CT_STATIC: |
| call_type=new VString(method_call_type_static); | call_type=new VString(Symbols::STATIC_SYMBOL); |
| break; | break; |
| case Method::CT_ANY: | case Method::CT_ANY: |
| break; | break; |
| Line 334 static void _dynamical(Request& r, Metho | Line 329 static void _dynamical(Request& r, Metho |
| } | } |
| static void _is(Request& r, MethodParams& params) { | static void _is(Request& r, MethodParams& params) { |
| const String& type=params.as_string(0, "class name must be string"); | const String& name=params.as_string(0, "element name must be string"); |
| const String& name=params.as_string(params.count()-1, "element name must be string"); | const String& type=params.as_string(1, "class name must be string"); |
| Value &context=params.count()==3 ? params.as_no_junction(1, "context must not be code") : *(r.get_method_frame()->caller()); | Value *context=params.count()==3 ? &(params.as_no_junction(2, "context must not be code")) : r.get_method_frame()->caller(); |
| Value *value=context.get_element(name); | Value *value=context ? context->get_element(name) : 0; |
| if(value) { | if(value) { |
| if(type == "code" || type == "method") { | if(type == "code" || type == "method") { |
| Line 347 static void _is(Request& r, MethodParams | Line 342 static void _is(Request& r, MethodParams |
| r.write_no_lang(VBool::get( value->is(type.cstr()) )); | r.write_no_lang(VBool::get( value->is(type.cstr()) )); |
| } | } |
| } else | } else |
| r.write_no_lang(VBool::get(false)); | r.write_no_lang(VBool::get(type == "void")); |
| } | } |
| static void _copy(Request& r, MethodParams& params) { | static void _copy(Request& r, MethodParams& params) { |
| Line 423 MReflection::MReflection(): Methoded("re | Line 418 MReflection::MReflection(): Methoded("re |
| // ^reflection:dynamical[[object or class, caller if absent]] | // ^reflection:dynamical[[object or class, caller if absent]] |
| add_native_method("dynamical", Method::CT_STATIC, _dynamical, 0, 1); | add_native_method("dynamical", Method::CT_STATIC, _dynamical, 0, 1); |
| // ^reflection:is[class_name|code|method;[context;]element_name] | // ^reflection:is[element_name;class_name|code|method[;context]] |
| add_native_method("is", Method::CT_STATIC, _is, 2, 3); | add_native_method("is", Method::CT_STATIC, _is, 2, 3); |
| // ^reflection:copy[src;dst] | // ^reflection:copy[src;dst] |