Project

General

Profile

Actions

Feature #15230

closed

RubyVM.resolve_feature_path

Added by mame (Yusuke Endoh) about 6 years ago. Updated over 5 years ago.

Status:
Closed
Target version:
-
[ruby-core:89431]

Description

I'd like a feature to know what will be loaded by require(feature) without actual loading.

$ ./local/bin/ruby -e 'p RubyVM.resolve_feature_path("set")'
[:r, "/home/mame/work/ruby/local/lib/ruby/2.6.0/set.rb"]

$ ./local/bin/ruby -e 'p RubyVM.resolve_feature_path("etc")'
[:s, "/home/mame/work/ruby/local/lib/ruby/2.6.0/x86_64-linux/etc.so"]

This feature is useful for a static analysis tool of Ruby programs. It might also be useful to check $LOAD_PATH configuration.

I don't think that RubyVM is the best place to have this method, but a good place to experiment the new feature. Kernel#resolve_feature_path looks too aggressive.

diff --git a/load.c b/load.c
index ddde2baf3b..dd609105ee 100644
--- a/load.c
+++ b/load.c
@@ -942,6 +942,26 @@ load_ext(VALUE path)
     return (VALUE)dln_load(RSTRING_PTR(path));
 }
 
+VALUE
+rb_resolve_feature_path(VALUE klass, VALUE fname)
+{
+    VALUE path;
+    int found;
+    char s[2];
+
+    fname = rb_get_path_check(fname, 0);
+    path = rb_str_encode_ospath(fname);
+    found = search_required(path, &path, 0);
+
+    if (!found) {
+        load_failed(fname);
+    }
+
+    s[0] = found;
+    s[1] = 0;
+    return rb_ary_new_from_args(2, ID2SYM(rb_intern2(s, 1)), path);
+}
+
 /*
  * returns
  *  0: if already loaded (false)
diff --git a/vm.c b/vm.c
index fababaa2ec..2a72d16f47 100644
--- a/vm.c
+++ b/vm.c
@@ -2834,6 +2834,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)
 {
@@ -3140,6 +3142,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

Related issues 1 (0 open1 closed)

Related to Ruby master - Feature #15903: Move RubyVM.resolve_feature_path to Kernel.resolve_feature_pathClosedmatz (Yukihiro Matsumoto)Actions
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0