From 0cd28199e50039e9425f10b880c436d3ecacde0b Mon Sep 17 00:00:00 2001 From: mame Date: Thu, 6 Dec 2018 06:40:54 +0000 Subject: load.c (RubyVM.resolve_feature_path): New method. [Feature #15230] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66237 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- NEWS | 7 +++++++ load.c | 36 ++++++++++++++++++++++++++++++++++++ vm.c | 4 ++++ 3 files changed, 47 insertions(+) diff --git a/NEWS b/NEWS index c471a8abbc..d0c4830f76 100644 --- a/NEWS +++ b/NEWS @@ -266,6 +266,13 @@ sufficient information, see the ChangeLog file or Redmine * Range#step now returns an instance of Enumerator::ArithmeticSequence class rather than one of Enumerator class. +[RubyVM] + + [New methods] + + * RubyVM.resolve_feature_path identifies the file that will be loaded by + "require(feature)". [experimental] [Feature #15230] + [RubyVM::AbstractSyntaxTree] [New methods] diff --git a/load.c b/load.c index ddde2baf3b..46367358ca 100644 --- a/load.c +++ b/load.c @@ -942,6 +942,42 @@ load_ext(VALUE path) return (VALUE)dln_load(RSTRING_PTR(path)); } +/* + * call-seq: + * RubyVM.resolve_feature_path(feature) -> [:rb or :so, path] + * + * Identifies the file that will be loaded by "require(feature)". + * This API is experimental and just for internal. + * + * RubyVM.resolve_feature_path("set") + * #=> [:rb, "/path/to/feature.rb"] + */ + +VALUE +rb_resolve_feature_path(VALUE klass, VALUE fname) +{ + VALUE path; + int found; + VALUE sym; + + fname = rb_get_path_check(fname, 0); + path = rb_str_encode_ospath(fname); + found = search_required(path, &path, 0); + + switch (found) { + case 'r': + sym = ID2SYM(rb_intern("rb")); + break; + case 's': + sym = ID2SYM(rb_intern("so")); + break; + default: + load_failed(fname); + } + + return rb_ary_new_from_args(2, sym, path); +} + /* * returns * 0: if already loaded (false) diff --git a/vm.c b/vm.c index e86f4650f0..5920f25fc4 100644 --- a/vm.c +++ b/vm.c @@ -2869,6 +2869,8 @@ static VALUE usage_analysis_operand_stop(VALUE self); static VALUE usage_analysis_register_stop(VALUE self); #endif +VALUE rb_resolve_feature_path(VALUE klass, VALUE fname); + void Init_VM(void) { @@ -3171,6 +3173,8 @@ Init_VM(void) /* vm_backtrace.c */ Init_vm_backtrace(); + + rb_define_singleton_method(rb_cRubyVM, "resolve_feature_path", rb_resolve_feature_path, 1); } void -- cgit v1.2.3