Diff for /parser3/src/classes/reflection.C between versions 1.73 and 1.83

version 1.73, 2016/12/01 23:42:35 version 1.83, 2017/02/07 22:00:29
Line 1 Line 1
 /** @file  /** @file
         Parser: @b reflection parser class.          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 <paf@design.ru> (http://paf.design.ru)          Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
 */  */
   
Line 51  static void _create(Request& r, MethodPa Line 51  static void _create(Request& r, MethodPa
         const String* class_name=0;          const String* class_name=0;
         const String* constructor_name=0;          const String* constructor_name=0;
   
         Value& voptions=params.as_no_junction(0, "params must not be code");          Value& voptions=params.as_no_junction(0, "param must not be code");
         if(HashStringValue* options=voptions.get_hash()) {          if(HashStringValue* options=voptions.get_hash()) {
                 int valid_options=0;                  int valid_options=0;
                 if(Value* vclass_name=options->get("class")) {                  if(Value* vclass_name=options->get("class")) {
Line 78  static void _create(Request& r, MethodPa Line 78  static void _create(Request& r, MethodPa
   
                 params_offset=1;                  params_offset=1;
         } else {          } else {
                 class_name=&params.as_string(0, "param must be string or hash");                  class_name=&params.as_string(0, "param must not be code");
   
                 if(params.count()==1)                  if(params.count()==1)
                         throw Exception(PARSER_RUNTIME, 0, "constructor name must be specified");                          throw Exception(PARSER_RUNTIME, 0, "constructor name must be specified");
Line 102  static void _create(Request& r, MethodPa Line 102  static void _create(Request& r, MethodPa
         if(nparams>MAX_CREATE_PARAMS)          if(nparams>MAX_CREATE_PARAMS)
                 throw Exception(PARSER_RUNTIME, 0, "arguments count should not exceed %d", MAX_CREATE_PARAMS);                  throw Exception(PARSER_RUNTIME, 0, "arguments count should not exceed %d", MAX_CREATE_PARAMS);
   
           Value* args[MAX_CREATE_PARAMS];
         CONSTRUCTOR_FRAME_ACTION(*method, r.get_method_frame(), object, {          CONSTRUCTOR_FRAME_ACTION(*method, r.get_method_frame(), object, {
                 Value* v[MAX_CREATE_PARAMS];  
                 if(nparams>0){                  if(nparams>0){
                         if(params_hash){                          if(params_hash){
                                 for(HashStringValue::Iterator i(*params_hash); i; i.next() )                                  int i=0;
                                         v[i]=i.value();                                  for(HashStringValue::Iterator h(*params_hash); h; h.next())
                                           args[i++]=h.value();
                         } else {                          } else {
                                 for(int i=0; i<nparams; i++)                                  for(int i=0; i<nparams; i++)
                                         v[i]=&r.process(params[i+params_offset]);                                          args[i]=&r.process(params[i+params_offset]);
                         }                          }
                         frame.store_params((Value**)&v, nparams);                          frame.store_params((Value**)&args, nparams);
                 } else {                  } else {
                         frame.empty_params();                          frame.empty_params();
                 }                  }
Line 435  static void _mixin(Request& r, MethodPar Line 436  static void _mixin(Request& r, MethodPar
         if(params.count()>1)          if(params.count()>1)
                 if(HashStringValue* options=params.as_hash(1, "mixin options")) {                  if(HashStringValue* options=params.as_hash(1, "mixin options")) {
                         int valid_options=0;                          int valid_options=0;
                         if(vtarget=options->get("to")) {                          for(HashStringValue::Iterator i(*options); i; i.next() ){
                                 valid_options++;                                  String::Body key=i.key();
                         }                                  Value* value=i.value();
                         if(Value* vname=options->get("name")) {                                  if(key == "to") {
                                 name=&vname->as_string();                                          vtarget=value;
                                 valid_options++;                                          valid_options++;
                         }                                  } else if(key == "name") {
                         if(Value* vmethods=options->get("methods")) {                                          name=&value->as_string();
                                 copy_methods=r.process(*vmethods).as_bool();                                          valid_options++;
                                 valid_options++;                                  } else if(key == "methods") {
                         }                                          copy_methods=r.process(*value).as_bool();
                         if(Value* vfields=options->get("fields")) {                                          valid_options++;
                                 copy_fields=r.process(*vfields).as_bool();                                  } else if(key == "fields") {
                                 valid_options++;                                          copy_fields=r.process(*value).as_bool();
                         }                                          valid_options++;
                         if(Value* voverwrite=options->get("overwrite")) {                                  } else if(key == "overwrite") {
                                 overwrite=r.process(*voverwrite).as_bool();                                          overwrite=r.process(*value).as_bool();
                                 valid_options++;                                          valid_options++;
                                   }
                         }                          }
                         if(valid_options!=options->count())                          if(valid_options!=options->count())
                                 throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);                                  throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
Line 496  static void _mixin(Request& r, MethodPar Line 498  static void _mixin(Request& r, MethodPar
         }          }
 }  }
   
   String::Language get_untaint_lang(const String& lang_name); // op.C
   
   static void _tainting(Request& r, MethodParams& params) {
           const String& str=params.as_string(params.count()-1, "param must be string");
           String::Language lang = String::L_UNSPECIFIED;
           bool optimized=false;
   
           if(params.count()==2){
                   const String& slang=params.as_string(0, "language name must be string");
                   if(slang == "optimized")
                           optimized=true;
                   else if(slang == "tainted")
                           lang=String::L_TAINTED;
                   else lang=get_untaint_lang(slang);
           }
   
           if(!str.is_empty()){
                   char *visual=str.visualize_langs();
   
                   if(optimized){
                           for(char *c=visual; *c; c++)
                                   *c = *c<0 ? '+':'-';
                   } else if(lang != String::L_UNSPECIFIED){
                           for(char *c=visual; *c; c++)
                                   *c = *c==lang ? '+':'-';
                   } else {
                           for(char *c=visual; *c; c++)
                                   *c = *c & 0x7F;
                   }
   
                   r.write(*new String(visual));
           }
   }
   
   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; i<method.params_names->count(); 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<VParserMethodFrame*>(caller))
                                                           for(HashString<Value*>::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  // constructor
 MReflection::MReflection(): Methoded("reflection") {  MReflection::MReflection(): Methoded("reflection") {
Line 535  MReflection::MReflection(): Methoded("re Line 672  MReflection::MReflection(): Methoded("re
         // ^reflection:method_info[junction]          // ^reflection:method_info[junction]
         add_native_method("method_info", Method::CT_STATIC, _method_info, 1, 2);          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);          add_native_method("filename", Method::CT_STATIC, _filename, 1, 1);
   
         // ^reflection:fields[object or class]          // ^reflection:fields[object or class]
Line 565  MReflection::MReflection(): Methoded("re Line 702  MReflection::MReflection(): Methoded("re
         // ^reflection:mixin[object or class or junction;options]          // ^reflection:mixin[object or class or junction;options]
         add_native_method("mixin", Method::CT_STATIC, _mixin, 1, 2);          add_native_method("mixin", Method::CT_STATIC, _mixin, 1, 2);
   
           // ^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);
   
 }  }

Removed from v.1.73  
changed lines
  Added in v.1.83


E-mail: