Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jarmo/1360449 to your computer and use it in GitHub Desktop.
Save jarmo/1360449 to your computer and use it in GitHub Desktop.
Ruby: playing with require and load
diff --git a/file.c b/file.c
index 3107895..8bb0839 100644
--- a/file.c
+++ b/file.c
@@ -5117,7 +5117,7 @@ rb_find_file_ext_safe(VALUE *filep, const char *const *ext, int safe_level)
if (!ext[0]) return 0;
if (f[0] == '~') {
- fname = file_expand_path_1(fname);
+ fname = rb_funcall(rb_cFile, rb_intern("expand_path"), 1, fname);
if (safe_level >= 1 && OBJ_TAINTED(fname)) {
rb_raise(rb_eSecurityError, "loading from unsafe file %s", f);
}
@@ -5130,7 +5130,7 @@ rb_find_file_ext_safe(VALUE *filep, const char *const *ext, int safe_level)
if (safe_level >= 1 && !fpath_check(fname)) {
rb_raise(rb_eSecurityError, "loading from unsafe path %s", f);
}
- if (!expanded) fname = file_expand_path_1(fname);
+ if (!expanded) fname = rb_funcall(rb_cFile, rb_intern("expand_path"), 1, fname);
fnlen = RSTRING_LEN(fname);
for (i=0; ext[i]; i++) {
rb_str_cat2(fname, ext[i]);
@@ -5151,7 +5151,6 @@ rb_find_file_ext_safe(VALUE *filep, const char *const *ext, int safe_level)
if (!load_path) return 0;
fname = rb_str_dup(*filep);
- RBASIC(fname)->klass = 0;
fnlen = RSTRING_LEN(fname);
tmp = rb_str_tmp_new(MAXPATHLEN + 2);
for (j=0; ext[j]; j++) {
@@ -5161,14 +5160,14 @@ rb_find_file_ext_safe(VALUE *filep, const char *const *ext, int safe_level)
RB_GC_GUARD(str) = rb_get_path_check(str, safe_level);
if (RSTRING_LEN(str) == 0) continue;
- file_expand_path(fname, str, 0, tmp);
+ tmp = rb_funcall(rb_cFile, rb_intern("expand_path"), 2, fname, str);
if (file_load_ok(RSTRING_PTR(tmp))) {
*filep = copy_path_class(tmp, *filep);
return (int)(j+1);
}
FL_UNSET(tmp, FL_TAINT | FL_UNTRUSTED);
}
- rb_str_set_len(fname, fnlen);
+ rb_str_resize(fname, fnlen);
}
RB_GC_GUARD(load_path);
return 0;
@@ -5188,7 +5187,7 @@ rb_find_file_safe(VALUE path, int safe_level)
int expanded = 0;
if (f[0] == '~') {
- tmp = file_expand_path_1(path);
+ tmp = rb_funcall(rb_cFile, rb_intern("expand_path"), 1, path);
if (safe_level >= 1 && OBJ_TAINTED(tmp)) {
rb_raise(rb_eSecurityError, "loading from unsafe file %s", f);
}
@@ -5203,7 +5202,7 @@ rb_find_file_safe(VALUE path, int safe_level)
}
if (!file_load_ok(f)) return 0;
if (!expanded)
- path = copy_path_class(file_expand_path_1(path), path);
+ path = copy_path_class(rb_funcall(rb_cFile, rb_intern("expand_path"), 1, path), path);
return path;
}
@@ -5220,7 +5219,7 @@ rb_find_file_safe(VALUE path, int safe_level)
VALUE str = RARRAY_PTR(load_path)[i];
RB_GC_GUARD(str) = rb_get_path_check(str, safe_level);
if (RSTRING_LEN(str) > 0) {
- file_expand_path(path, str, 0, tmp);
+ tmp = rb_funcall(rb_cFile, rb_intern("expand_path"), 2, path, str);
f = RSTRING_PTR(tmp);
if (file_load_ok(f)) goto found;
}
diff --git a/load.c b/load.c
index 9ed386d..3bd0037 100644
--- a/load.c
+++ b/load.c
@@ -43,7 +43,7 @@ rb_get_expanded_load_path(void)
ary = rb_ary_new2(RARRAY_LEN(load_path));
for (i = 0; i < RARRAY_LEN(load_path); ++i) {
- VALUE path = rb_file_expand_path(RARRAY_PTR(load_path)[i], Qnil);
+ VALUE path = rb_funcall(rb_cFile, rb_intern("expand_path"), 1, RARRAY_PTR(load_path)[i]);
rb_str_freeze(path);
rb_ary_push(ary, path);
}
@@ -233,7 +233,7 @@ rb_feature_provided(const char *feature, const char **loading)
if (*feature == '.' &&
(feature[1] == '/' || strncmp(feature+1, "./", 2) == 0)) {
- fullpath = rb_file_expand_path(rb_str_new2(feature), Qnil);
+ fullpath = rb_funcall(rb_cFile, rb_intern("expand_path"), 1, rb_str_new2(feature));
feature = RSTRING_PTR(fullpath);
}
if (ext && !strchr(ext, '/')) {
@@ -305,7 +305,7 @@ rb_load_internal(VALUE fname, int wrap)
th->mild_compile_error++;
node = (NODE *)rb_load_file(RSTRING_PTR(fname));
loaded = TRUE;
- iseq = rb_iseq_new_top(node, rb_str_new2("<top (required)>"), fname, rb_realpath_internal(Qnil, fname, 1), Qfalse);
+ iseq = rb_iseq_new_top(node, rb_str_new2("<top (required)>"), fname, rb_funcall(rb_cFile, rb_intern("realpath"), 1, fname), Qfalse);
th->mild_compile_error--;
rb_iseq_eval(iseq);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment