--- parser3/src/classes/reflection.C 2015/10/25 22:27:59 1.36 +++ parser3/src/classes/reflection.C 2016/07/20 14:51:16 1.45 @@ -1,7 +1,7 @@ /** @file 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 (http://paf.design.ru) */ @@ -9,7 +9,7 @@ #include "pa_request.h" #include "pa_vbool.h" -volatile const char * IDENT_REFLECTION_C="$Id: reflection.C,v 1.36 2015/10/25 22:27:59 moko Exp $"; +volatile const char * IDENT_REFLECTION_C="$Id: reflection.C,v 1.45 2016/07/20 14:51:16 moko Exp $"; static const String class_type_methoded("methoded"); @@ -19,8 +19,6 @@ static const String method_type_parser(" static const String method_call_type("call_type"); static const String method_inherited("inherited"); 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_max_params("max_params"); @@ -37,7 +35,7 @@ public: // global variable -DECLARE_CLASS_VAR(reflection, new MReflection, 0); +DECLARE_CLASS_VAR(reflection, new MReflection); // methods @@ -58,7 +56,7 @@ static void _create(Request& r, MethodPa throw Exception(PARSER_RUNTIME, &constructor_name, "constructor must be declared in class '%s'", - class_value->get_class()->name_cstr()); + class_value->type()); Junction* junction=constructor_value->get_junction(); const Method* method=junction->method; @@ -70,15 +68,13 @@ static void _create(Request& r, MethodPa if(method->call_type==Method::CT_STATIC) throw Exception(PARSER_RUNTIME, &constructor_name, - "native method of class '%s' (%s) is not allowed to be called dynamically", - class_value->get_class()->name_cstr(), + "native method of class '%s' is not allowed to be called dynamically", class_value->type()); if(nparamsmin_numbered_params_count) throw Exception(PARSER_RUNTIME, &constructor_name, - "native method of class '%s' (%s) accepts minimum %d parameter(s) (%d passed)", - class_value->get_class()->name_cstr(), + "native method of class '%s' accepts minimum %d parameter(s) (%d passed)", class_value->type(), method->min_numbered_params_count, nparams); @@ -91,8 +87,7 @@ static void _create(Request& r, MethodPa if(nparams>max_params_count) throw Exception(PARSER_RUNTIME, &constructor_name, - "method of class '%s' (%s) accepts maximum %d parameter(s) (%d passed)", - class_value->get_class()->name_cstr(), + "method of class '%s' accepts maximum %d parameter(s) (%d passed)", class_value->type(), max_params_count, nparams); @@ -144,7 +139,7 @@ static Value* get_class(Value* value){ static const String* get_class_name(Value* value){ if(VStateless_class* lclass=value->get_class()) - return &lclass->name(); + return new String(lclass->type()); else // classes with fields only, like env & console return new String(value->type()); @@ -198,7 +193,7 @@ static void _def(Request& r, MethodParam const String& type=params.as_string(0, "type must be string"); if(type == def_class) { 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 { throw Exception(PARSER_RUNTIME, &type, "is invalid type, must be '%s'", def_class.cstr()); } @@ -237,13 +232,21 @@ static void _method(Request& r, MethodPa static void _fields(Request& r, MethodParams& params) { Value& o=params.as_no_junction(0, "param must be object or class, not junction"); - if(HashStringValue* fields=o.get_fields()) { - VHash& result=*new VHash(*fields); - r.write_no_lang(result); - } else + if(HashStringValue* fields=o.get_fields()) + r.write_no_lang(*new VHash(*fields)); + else r.write_no_lang(*new VHash()); } +static void _fields_reference(Request& r, MethodParams& params) { + Value& o=params.as_no_junction(0, "param must be object or class, not junction"); + + if(HashStringValue* fields=o.get_fields_reference()) + r.write_no_lang(*new VHashReference(*fields)); + else + throw Exception(PARSER_RUNTIME, 0, "param must be object or class"); +} + static void _field(Request& r, MethodParams& params) { Value& o=params.as_no_junction(0, "first param must be object or class, not junction"); const String& name=params.as_string(1, "field name must be string"); @@ -257,23 +260,16 @@ static void _method_info(Request& r, Met const String& class_name=params.as_string(0, "class_name must be string"); Value* class_value=r.get_class(class_name); if(!class_value) - 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"); + throw Exception(PARSER_RUNTIME, &class_name, "class does not have methods"); const String& method_name=params.as_string(1, "method_name must be string"); Method* method=lclass->get_method(method_name); 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", class_name.cstr()); VHash& result=*new VHash; HashStringValue* hash=result.get_hash(); @@ -284,16 +280,16 @@ static void _method_info(Request& r, Met c=c->base()->get_class(); while(c->base() && base_method==c->base()->get_method(method_name)) 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; switch(method->call_type){ case Method::CT_DYNAMIC: - call_type=new VString(method_call_type_dynamic); + call_type=new VString(Symbols::DYNAMIC_SYMBOL); break; case Method::CT_STATIC: - call_type=new VString(method_call_type_static); + call_type=new VString(Symbols::STATIC_SYMBOL); break; case Method::CT_ANY: break; @@ -334,10 +330,10 @@ static void _dynamical(Request& r, Metho } 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(params.count()-1, "element 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 *value=context.get_element(name); + const String& name=params.as_string(0, "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(2, "context must not be code")) : r.get_method_frame()->caller(); + Value *value=context ? context->get_element(name) : 0; if(value) { if(type == "code" || type == "method") { @@ -353,7 +349,7 @@ static void _is(Request& r, MethodParams static void _copy(Request& r, MethodParams& params) { HashStringValue* src=params.as_no_junction(0, "source must not be code").get_hash(); - if(src==NULL) + if(src==NULL) throw Exception(PARSER_RUNTIME, 0, "source must have hash representation"); Value& dst=params.as_no_junction(1, "destination must not be code"); @@ -417,13 +413,16 @@ MReflection::MReflection(): Methoded("re // ^reflection:fields[object or class] add_native_method("fields", Method::CT_STATIC, _fields, 1, 1); + // ^reflection:fields_reference[object] + add_native_method("fields_reference", Method::CT_STATIC, _fields_reference, 1, 1); + // ^reflection:field[object or class;field_name] add_native_method("field", Method::CT_STATIC, _field, 2, 2); // ^reflection:dynamical[[object or class, caller if absent]] 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); // ^reflection:copy[src;dst]