--- parser3/src/classes/reflection.C 2016/12/08 21:05:43 1.78 +++ parser3/src/classes/reflection.C 2017/02/15 17:21:28 1.85 @@ -1,7 +1,7 @@ /** @file Parser: @b reflection parser class. - Copyright (c) 2001-2015 Art. Lebedev Studio (http://www.artlebedev.com) + Copyright (c) 2001-2017 Art. Lebedev Studio (http://www.artlebedev.com) Author: Alexandr Petrosian (http://paf.design.ru) */ @@ -10,7 +10,7 @@ #include "pa_vbool.h" #include "pa_vobject.h" -volatile const char * IDENT_REFLECTION_C="$Id: reflection.C,v 1.78 2016/12/08 21:05:43 moko Exp $"; +volatile const char * IDENT_REFLECTION_C="$Id: reflection.C,v 1.85 2017/02/15 17:21:28 moko Exp $"; static const String class_type_methoded("methoded"); @@ -203,9 +203,37 @@ static void _methods(Request& r, MethodP if(!vclass) throw Exception(PARSER_RUNTIME, &class_name, "class is undefined"); + bool reverse=true; + + if(params.count()>1) + if(HashStringValue* options=params.as_hash(1, "methods options")) { + int valid_options=0; + for(HashStringValue::Iterator i(*options); i; i.next() ){ + String::Body key=i.key(); + Value* value=i.value(); + if(key == "reverse") { + reverse=r.process(*value).as_bool(); + valid_options++; + } + } + if(valid_options!=options->count()) + throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION); + } + VHash& result=*new VHash; - for(HashStringMethod::Iterator i(vclass->get_methods()); i; i.next()){ - result.hash().put(i.key(), new VString(i.value()->native_code ? method_type_native : method_type_parser)); + +#ifdef HASH_ORDER + if(reverse){ + for(HashStringMethod::ReverseIterator i(vclass->get_methods()); i; i.prev()){ + result.hash().put(i.key(), new VString(i.value()->native_code ? method_type_native : method_type_parser)); + } + } else { +#else + { +#endif + for(HashStringMethod::Iterator i(vclass->get_methods()); i; i.next()){ + result.hash().put(i.key(), new VString(i.value()->native_code ? method_type_native : method_type_parser)); + } } r.write(result); @@ -436,24 +464,25 @@ static void _mixin(Request& r, MethodPar 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(*vmethods).as_bool(); - valid_options++; - } - if(Value* vfields=options->get("fields")) { - copy_fields=r.process(*vfields).as_bool(); - valid_options++; - } - if(Value* voverwrite=options->get("overwrite")) { - overwrite=r.process(*voverwrite).as_bool(); - valid_options++; + for(HashStringValue::Iterator i(*options); i; i.next() ){ + String::Body key=i.key(); + Value* value=i.value(); + if(key == "to") { + vtarget=value; + valid_options++; + } else if(key == "name") { + name=&value->as_string(); + valid_options++; + } else if(key == "methods") { + copy_methods=r.process(*value).as_bool(); + valid_options++; + } else if(key == "fields") { + copy_fields=r.process(*value).as_bool(); + valid_options++; + } else if(key == "overwrite") { + overwrite=r.process(*value).as_bool(); + valid_options++; + } } if(valid_options!=options->count()) throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION); @@ -531,6 +560,108 @@ static void _tainting(Request& r, Method } } +static void _stack(Request& r, MethodParams& params) { + bool show_args=false; + bool show_locals=false; + + int limit=1000000; + int offset=0; + + if(params.count()>0) + if(HashStringValue* options=params.as_hash(0, "stack options")) { + int valid_options=0; + for(HashStringValue::Iterator i(*options); i; i.next() ){ + String::Body key=i.key(); + Value* value=i.value(); + + if(key == "args") { + show_args=r.process(*value).as_bool(); + valid_options++; + } else if(key == "locals") { + show_locals=r.process(*value).as_bool(); + valid_options++; + } else if(key == "limit") { + limit=r.process(*value).as_int(); + valid_options++; + } else if(key == "offset") { + offset=r.process(*value).as_int(); + valid_options++; + } + } + + if(valid_options!=options->count()) + throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION); + } + + limit+=offset; + + VHash& vresult=*new VHash; + HashStringValue* result=vresult.get_hash(); + int index=1; + VMethodFrame* caller=r.get_method_frame()->caller(); + while(caller && index <= limit){ + if(index>offset){ + VHash& vcurrent=*new VHash; + HashStringValue* current=vcurrent.get_hash(); + + current->put(Symbols::SELF_SYMBOL, &caller->self()); + + const Method& method=caller->method; + + current->put(method_name, new VString(*method.name)); + + if(!method.native_code){ + Operation::Origin origin=r.get_method_origin(&method); + if(origin.file_no){ + current->put("file", new VString(*r.get_used_filespec(origin.file_no))); + current->put("line", new VInt(origin.line)); // no +1 as declaration before first command + } + + if(show_args || show_locals){ + + VHash& vargs=*new VHash; + HashStringValue* args=vargs.get_hash(); + + if(method.params_names){ + for(size_t i=0; icount(); i++){ + const String& pname=*(*method.params_names)[i]; + Value* value=caller->get_element(pname); + args->put(pname, value->get_junction() ? VVoid::get() : value); + } + } + if(method.extra_params) + args->put(*method.extra_params, caller->get_element(*method.extra_params)); + + if(show_args) + current->put("args", &vargs); + + if(show_locals){ + VHash& vlocals=*new VHash; + HashStringValue* locals=vlocals.get_hash(); + + if(VParserMethodFrame* frame=dynamic_cast(caller)) + for(HashString::Iterator h(frame->my); h; h.next()){ + String::Body key=h.key(); + Value* value=h.value(); + if(!args->contains(key) && (key != "result")){ + locals->put(key, value->get_junction() ? VVoid::get() : value); + } + } + + current->put("locals", &vlocals); + } + } + } + + result->put(format(index, 0), &vcurrent); + } + caller=caller->caller(); + index++; + } + + r.write(vresult); +} + // constructor MReflection::MReflection(): Methoded("reflection") { // ^reflection:create[class_name;constructor_name[;param1[;param2[;...]]]] @@ -559,7 +690,7 @@ MReflection::MReflection(): Methoded("re add_native_method("def", Method::CT_STATIC, _def, 2, 2); // ^reflection:methods[class_name] - add_native_method("methods", Method::CT_STATIC, _methods, 1, 1); + add_native_method("methods", Method::CT_STATIC, _methods, 1, 2); // ^reflection:method[object or class;method_name[;self]] // ^reflection:method[junction[;self]] @@ -569,7 +700,7 @@ MReflection::MReflection(): Methoded("re // ^reflection:method_info[junction] add_native_method("method_info", Method::CT_STATIC, _method_info, 1, 2); - // ^reflection:filename[object or class] + // ^reflection:filename[object or class or method] add_native_method("filename", Method::CT_STATIC, _filename, 1, 1); // ^reflection:fields[object or class] @@ -602,4 +733,7 @@ MReflection::MReflection(): Methoded("re // ^reflection:tainting[[language or 'tainted' or 'optimized';]string] add_native_method("tainting", Method::CT_STATIC, _tainting, 1, 2); + // ^reflection:stack[options] + add_native_method("stack", Method::CT_STATIC, _stack, 0, 1); + }