|
|
| version 1.50, 2016/09/13 22:30:30 | version 1.57, 2016/09/26 20:38:33 |
|---|---|
| Line 114 static void _classes(Request& r, MethodP | Line 114 static void _classes(Request& r, MethodP |
| } | } |
| static Value* get_class(Value* value){ | 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 { |
| // junction | // 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; | return value; |
| } | |
| } | } |
| static const String* get_class_name(Value* value){ | static const String& get_class_name(Value& value){ |
| if(VStateless_class* vclass=value->get_class()) | return *new String(get_class(value).type()); |
| return new String(vclass->type()); | |
| else | |
| // junction | |
| return new String(value->type()); | |
| } | } |
| static void _class(Request& r, MethodParams& params) { | 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) { | 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) { | static void _class_by_name(Request& r, MethodParams& params) { |
| Line 151 static void _class_by_name(Request& r, M | Line 152 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* vclass=params[0].get_class()) | if(VStateless_class* vclass=params[0].get_class()) |
| if(Value* base=vclass->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* vclass=params[0].get_class()) | if(VStateless_class* vclass=params[0].get_class()) |
| if(Value* base=vclass->base()) | 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) { | static void _def(Request& r, MethodParams& params) { |
| Line 191 static void _methods(Request& r, MethodP | Line 192 static void _methods(Request& r, MethodP |
| r.write_no_lang(result); | r.write_no_lang(result); |
| } | } |
| static VJunction &method_junction(Value &self, Method &method, const String *name=0){ | static VJunction &method_junction(Value &self, Method &method){ |
| if(method.native_code) | if(method.native_code) |
| throw Exception(PARSER_RUNTIME, name, "method should not be native"); | throw Exception(PARSER_RUNTIME, method.name, "method must not be native"); |
| // if(dynamic_cast<VObject*>(&self)) | if(!(dynamic_cast<VObject*>(&self) || dynamic_cast<VClass*>(&self))) |
| // throw Exception(PARSER_RUNTIME, 0, "self should be parser object"); | throw Exception(PARSER_RUNTIME, 0, "self must be parser object or class"); |
| return *method.get_vjunction(self); | return *method.get_vjunction(self); |
| } | } |
| Line 210 static void _method(Request& r, MethodPa | Line 211 static void _method(Request& r, MethodPa |
| r.write_no_lang(method_junction(self, *method)); | r.write_no_lang(method_junction(self, *method)); |
| return; | 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) | 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"); | 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(VStateless_class* vclass=source.get_class()) { |
| if(Method* method=vclass->get_method(name)){ | 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) : *method->get_vjunction(source) ); |
| return; | 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) { | static void _fields(Request& r, MethodParams& params) { |
| Line 369 static void _delete(Request&, MethodPara | Line 369 static void _delete(Request&, MethodPara |
| } | } |
| } | } |
| static void _mixin(Request& r, MethodParams& params) { | |
| Value& vsource=params.as_no_junction(0, "source must not be code"); | |
| Value* vtarget=0; | |
| const String *name=0; | |
| bool copy_methods=true; | |
| bool copy_fields=true; | |
| bool overwrite=true; | |
| if(params.count()>1) | |
| if(HashStringValue* options=params.as_hash(1, "mixin options")) { | |
| int valid_options=0; | |
| if(vtarget=options->get("to")) { | |
| valid_options++; | |
| } | |
| if(Value* vname=options->get("name")) { | |
| name=&vname->as_string(); | |
| valid_options++; | |
| } | |
| if(Value* vmethods=options->get("methods")) { | |
| copy_methods=r.process_to_value(*vmethods).as_bool(); | |
| valid_options++; | |
| } | |
| if(Value* vfields=options->get("fields")) { | |
| copy_fields=r.process_to_value(*vfields).as_bool(); | |
| valid_options++; | |
| } | |
| if(Value* voverwrite=options->get("overwrite")) { | |
| overwrite=r.process_to_value(*voverwrite).as_bool(); | |
| valid_options++; | |
| } | |
| if(valid_options!=options->count()) | |
| throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION); | |
| } | |
| if(!vtarget) | |
| vtarget=&r.get_method_frame()->caller()->self(); | |
| VClass* source=dynamic_cast<VClass*>(vsource.get_class()); | |
| VClass* target=dynamic_cast<VClass*>(vtarget->get_class()); | |
| if(!source) | |
| throw Exception(PARSER_RUNTIME, 0, "source must be parser object or class"); | |
| if(!target) | |
| throw Exception(PARSER_RUNTIME, 0, "destination must be parser object or class"); | |
| if(name){ | |
| if(copy_methods) | |
| if(Method* method=source->get_method(*name)) | |
| if(overwrite || !target->get_method(*name)){ | |
| target->set_method(*name, method); | |
| } | |
| if(copy_fields) | |
| if(Property* property=source->get_properties()->get(*name)) | |
| if(property->value && (overwrite || !target->get_properties()->get(*name))){ | |
| target->put_element(*target, *name, property->value); | |
| } | |
| } else { | |
| if(copy_methods) | |
| for(HashStringMethod::Iterator i(source->get_methods()); i; i.next()){ | |
| if(overwrite || !target->get_method(i.key())) | |
| target->set_method(*i.value()->name, i.value()); | |
| } | |
| if(copy_fields) | |
| for(HashStringProperty::Iterator i(*source->get_properties()); i; i.next()){ | |
| if(i.value()->value && ( overwrite || !target->get_properties()->get(i.key()) )) | |
| target->put_element(*target, *new String(i.key(), String::L_TAINTED), i.value()->value); | |
| } | |
| } | |
| } | |
| // constructor | // constructor |
| MReflection::MReflection(): Methoded("reflection") { | MReflection::MReflection(): Methoded("reflection") { |
| Line 399 MReflection::MReflection(): Methoded("re | Line 472 MReflection::MReflection(): Methoded("re |
| // ^reflection:methods[class_name] | // ^reflection:methods[class_name] |
| 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[;self]] |
| // ^reflection:method[junction[;self]] | |
| add_native_method("method", Method::CT_STATIC, _method, 1, 3); | add_native_method("method", Method::CT_STATIC, _method, 1, 3); |
| // ^reflection:method_info[class_name;method_name] | // ^reflection:method_info[class_name;method_name] |
| Line 428 MReflection::MReflection(): Methoded("re | Line 502 MReflection::MReflection(): Methoded("re |
| // ^reflection:delete[object or class;field_name] | // ^reflection:delete[object or class;field_name] |
| add_native_method("delete", Method::CT_STATIC, _delete, 2, 2); | 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); | |
| } | } |