Created
December 21, 2011 14:30
-
-
Save jonforums/1506226 to your computer and use it in GitHub Desktop.
TCS merge patch resolution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/file.c b/file.c | |
index 81f28ff..f499aaf 100644 | |
--- a/file.c | |
+++ b/file.c | |
@@ -2819,6 +2819,20 @@ ntfs_tail(const char *path) | |
buflen = RSTRING_LEN(result),\ | |
pend = p + buflen) | |
+#define EXPAND_PATH()\ | |
+ if ( !(abs_mode & FEP_DIR_EXPANDED) ) { \ | |
+ file_expand_path(dname, Qnil, abs_mode, result); \ | |
+ } \ | |
+ else { \ | |
+ size_t dlen = RSTRING_LEN(dname); \ | |
+ BUFCHECK(dlen > buflen); \ | |
+ strncpy(buf, RSTRING_PTR(dname), dlen + 1); \ | |
+ rb_str_set_len(result, dlen); \ | |
+ rb_enc_associate(result, rb_enc_check(result, dname)); \ | |
+ ENC_CODERANGE_CLEAR(result); \ | |
+ } | |
+ | |
+ | |
VALUE | |
rb_home_dir(const char *user, VALUE result) | |
{ | |
@@ -2863,6 +2877,8 @@ rb_home_dir(const char *user, VALUE result) | |
return result; | |
} | |
+#define FEP_FILE_ABSOLUTE 1 | |
+#define FEP_DIR_EXPANDED 2 | |
static VALUE | |
file_expand_path(VALUE fname, VALUE dname, int abs_mode, VALUE result) | |
{ | |
@@ -2875,7 +2891,7 @@ file_expand_path(VALUE fname, VALUE dname, int abs_mode, VALUE result) | |
BUFINIT(); | |
tainted = OBJ_TAINTED(fname); | |
- if (s[0] == '~' && abs_mode == 0) { /* execute only if NOT absolute_path() */ | |
+ if (s[0] == '~' && !(abs_mode & FEP_FILE_ABSOLUTE)) { /* execute only if NOT absolute_path() */ | |
long userlen = 0; | |
tainted = 1; | |
if (isdirsep(s[1]) || s[1] == '\0') { | |
@@ -2923,7 +2939,7 @@ file_expand_path(VALUE fname, VALUE dname, int abs_mode, VALUE result) | |
/* specified drive, but not full path */ | |
int same = 0; | |
if (!NIL_P(dname) && !not_same_drive(dname, s[0])) { | |
- file_expand_path(dname, Qnil, abs_mode, result); | |
+ EXPAND_PATH(); | |
BUFINIT(); | |
if (has_drive_letter(p) && TOLOWER(p[0]) == TOLOWER(s[0])) { | |
/* ok, same drive */ | |
@@ -2949,7 +2965,7 @@ file_expand_path(VALUE fname, VALUE dname, int abs_mode, VALUE result) | |
#endif | |
else if (!rb_is_absolute_path(s)) { | |
if (!NIL_P(dname)) { | |
- file_expand_path(dname, Qnil, abs_mode, result); | |
+ EXPAND_PATH(); | |
BUFINIT(); | |
rb_enc_associate(result, rb_enc_check(result, fname)); | |
} | |
@@ -3232,7 +3248,7 @@ VALUE | |
rb_file_absolute_path(VALUE fname, VALUE dname) | |
{ | |
check_expand_path_args(fname, dname); | |
- return file_expand_path(fname, dname, 1, EXPAND_PATH_BUFFER()); | |
+ return file_expand_path(fname, dname, FEP_FILE_ABSOLUTE, EXPAND_PATH_BUFFER()); | |
} | |
/* | |
@@ -5060,7 +5076,38 @@ static int | |
file_load_ok(const char *path) | |
{ | |
int ret = 1; | |
- int fd = open(path, O_RDONLY); | |
+ int fd; | |
+#ifdef _WIN32 | |
+ static int use_attribute = -1; | |
+ if (use_attribute == -1) { | |
+ if (getenv("RUBY_USE_ATTRIBUTE") == NULL) { | |
+ use_attribute = 0; | |
+ } | |
+ else { | |
+ use_attribute = 1; | |
+ } | |
+ } | |
+ if (use_attribute) { | |
+ DWORD attr = GetFileAttributes(path); | |
+ if (attr == INVALID_FILE_ATTRIBUTES || | |
+ attr & FILE_ATTRIBUTE_DIRECTORY) { | |
+ ret = 0; | |
+ } | |
+ else { | |
+ HANDLE h = CreateFile(path, GENERIC_READ, | |
+ FILE_SHARE_READ | FILE_SHARE_WRITE, | |
+ NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); | |
+ if (h != INVALID_HANDLE_VALUE) { | |
+ CloseHandle(h); | |
+ } | |
+ else { | |
+ ret = 0; | |
+ } | |
+ } | |
+ return ret; | |
+ } | |
+#endif | |
+ fd = open(path, O_RDONLY); | |
if (fd == -1) return 0; | |
rb_update_max_fd(fd); | |
#if !defined DOSISH | |
@@ -5103,18 +5150,28 @@ rb_find_file_ext(VALUE *filep, const char *const *ext) | |
return rb_find_file_ext_safe(filep, ext, rb_safe_level()); | |
} | |
+#define GET_LOAD_PATH() \ | |
+ if (cached_expanded_load_path) { \ | |
+ RB_GC_GUARD(load_path) = rb_get_expanded_load_path(); \ | |
+ dirs_mode = FEP_DIR_EXPANDED; \ | |
+ } \ | |
+ else { \ | |
+ RB_GC_GUARD(load_path) = rb_get_load_path(); \ | |
+ dirs_mode = 0; \ | |
+ } | |
+ | |
int | |
rb_find_file_ext_safe(VALUE *filep, const char *const *ext, int safe_level) | |
{ | |
const char *f = StringValueCStr(*filep); | |
VALUE fname = *filep, load_path, tmp; | |
long i, j, fnlen; | |
- int expanded = 0; | |
+ int expanded = 0, dirs_mode; | |
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); | |
} | |
@@ -5127,7 +5184,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]); | |
@@ -5144,11 +5201,10 @@ rb_find_file_ext_safe(VALUE *filep, const char *const *ext, int safe_level) | |
rb_raise(rb_eSecurityError, "loading from non-absolute path %s", f); | |
} | |
- RB_GC_GUARD(load_path) = rb_get_load_path(); | |
+ GET_LOAD_PATH(); | |
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++) { | |
@@ -5158,14 +5214,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; | |
@@ -5182,10 +5238,10 @@ rb_find_file_safe(VALUE path, int safe_level) | |
{ | |
VALUE tmp, load_path; | |
const char *f = StringValueCStr(path); | |
- int expanded = 0; | |
+ int expanded = 0, dirs_mode; | |
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); | |
} | |
@@ -5200,7 +5256,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; | |
} | |
@@ -5208,7 +5264,7 @@ rb_find_file_safe(VALUE path, int safe_level) | |
rb_raise(rb_eSecurityError, "loading from non-absolute path %s", f); | |
} | |
- RB_GC_GUARD(load_path) = rb_get_load_path(); | |
+ GET_LOAD_PATH(); | |
if (load_path) { | |
long i; | |
@@ -5217,7 +5273,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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/load.c b/load.c | |
index 0ff4b60..eb72f38 100644 | |
--- a/load.c | |
+++ b/load.c | |
@@ -4,6 +4,7 @@ | |
#include "ruby/ruby.h" | |
#include "ruby/util.h" | |
+#include "ruby/encoding.h" | |
#include "internal.h" | |
#include "dln.h" | |
#include "eval_intern.h" | |
@@ -18,6 +19,13 @@ VALUE ruby_dln_librefs; | |
#define IS_DLEXT(e) (strcmp((e), DLEXT) == 0) | |
#endif | |
+static int sorted_loaded_features = 1; | |
+ | |
+#if defined __CYGWIN__ || defined DOSISH | |
+#define isdirsep(x) ((x) == '/' || (x) == '\\') | |
+#else | |
+#define isdirsep(x) ((x) == '/') | |
+#endif | |
static const char *const loadable_ext[] = { | |
".rb", DLEXT, | |
@@ -34,21 +42,34 @@ rb_get_load_path(void) | |
return load_path; | |
} | |
+static VALUE rb_checked_expanded_cache(int*); | |
+static void rb_set_expanded_cache(VALUE, int); | |
+static VALUE rb_expand_load_paths(int, VALUE*, int*); | |
+int cached_expanded_load_path = 1; | |
+ | |
VALUE | |
rb_get_expanded_load_path(void) | |
{ | |
- VALUE load_path = rb_get_load_path(); | |
- VALUE ary; | |
- long i; | |
+ VALUE expanded = rb_checked_expanded_cache(NULL); | |
- 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); | |
- rb_str_freeze(path); | |
- rb_ary_push(ary, path); | |
+ if ( !RTEST(expanded) ) { | |
+ VALUE load_path = rb_get_load_path(); | |
+ int has_relative = 0; | |
+ | |
+ if (!load_path) return 0; | |
+ | |
+ expanded = rb_expand_load_paths( | |
+ RARRAY_LEN(load_path), RARRAY_PTR(load_path), | |
+ &has_relative); | |
+ RB_GC_GUARD(load_path); | |
+ | |
+ if (cached_expanded_load_path) { | |
+ rb_set_expanded_cache(expanded, has_relative); | |
+ } | |
+ } else { | |
+ expanded = rb_ary_dup(expanded); | |
} | |
- rb_obj_freeze(ary); | |
- return ary; | |
+ return expanded; | |
} | |
static VALUE | |
@@ -129,6 +150,9 @@ loaded_feature_path_i(st_data_t v, st_data_t b, st_data_t f) | |
return ST_STOP; | |
} | |
+static int rb_feature_first_equal_or_greater(VALUE, const char *, long); | |
+static int rb_stop_search_feature(VALUE, const char *, long); | |
+ | |
static int | |
rb_feature_p(const char *feature, const char *ext, int rb, int expanded, const char **fn) | |
{ | |
@@ -151,8 +175,10 @@ rb_feature_p(const char *feature, const char *ext, int rb, int expanded, const c | |
type = 0; | |
} | |
features = get_loaded_features(); | |
- for (i = 0; i < RARRAY_LEN(features); ++i) { | |
+ i = rb_feature_first_equal_or_greater(features, feature, len); | |
+ for (; i < RARRAY_LEN(features); ++i) { | |
v = RARRAY_PTR(features)[i]; | |
+ if (rb_stop_search_feature(v, feature, len)) break; | |
f = StringValuePtr(v); | |
if ((n = RSTRING_LEN(v)) < len) continue; | |
if (strncmp(f, feature, len) != 0) { | |
@@ -176,14 +202,14 @@ rb_feature_p(const char *feature, const char *ext, int rb, int expanded, const c | |
} | |
} | |
loading_tbl = get_loading_table(); | |
- if (loading_tbl) { | |
+ if (loading_tbl && loading_tbl->num_entries > 0) { | |
f = 0; | |
if (!expanded) { | |
struct loaded_feature_searching fs; | |
fs.name = feature; | |
fs.len = len; | |
fs.type = type; | |
- fs.load_path = load_path ? load_path : rb_get_load_path(); | |
+ fs.load_path = load_path ? load_path : rb_get_expanded_load_path(); | |
fs.result = 0; | |
st_foreach(loading_tbl, loaded_feature_path_i, (st_data_t)&fs); | |
if ((f = fs.result) != 0) { | |
@@ -233,7 +259,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, '/')) { | |
@@ -251,6 +277,170 @@ rb_feature_provided(const char *feature, const char **loading) | |
return FALSE; | |
} | |
+static int | |
+feature_basename_length(const char *feature, long flen) | |
+{ | |
+ if (sorted_loaded_features) { | |
+ const char *ext = strrchr(feature, '.'); | |
+ return ext && !strchr(ext, '/') ? ext - feature : flen; | |
+ } else { | |
+ return 0; | |
+ } | |
+} | |
+ | |
+static int | |
+compare_feature_name(const char *left, long llen, const char *right, long rlen) | |
+{ | |
+ int diff = 0; | |
+ while (llen-- && rlen--) { | |
+ diff = left[llen] - right[rlen]; | |
+ if (diff) break; | |
+ if (left[llen] == '/') break; | |
+ } | |
+ return diff; | |
+} | |
+ | |
+static int | |
+rb_compare_feature_name(VALUE loaded, const char *feature, long flen) | |
+{ | |
+ const char *loaded_name = StringValuePtr(loaded); | |
+ long loaded_len = feature_basename_length(loaded_name, RSTRING_LEN(loaded)); | |
+ return compare_feature_name(loaded_name, loaded_len, feature, flen); | |
+} | |
+ | |
+/* used to find when equal features run out */ | |
+static int | |
+rb_stop_search_feature(VALUE loaded, const char *feature, long flen) | |
+{ | |
+ if (sorted_loaded_features) | |
+ return rb_compare_feature_name(loaded, feature, flen) > 0; | |
+ else | |
+ return FALSE; | |
+} | |
+ | |
+/* returns first position to search feature from */ | |
+static int | |
+rb_feature_first_equal_or_greater(VALUE features, const char *feature, long flen) | |
+{ | |
+ if (sorted_loaded_features) { | |
+ int before = 0, first = RARRAY_LEN(features); | |
+ VALUE *values = RARRAY_PTR(features); | |
+ if (first == 0) | |
+ return 0; | |
+ if (rb_compare_feature_name(values[0], feature, flen) >= 0) | |
+ return 0; | |
+ | |
+ while (first - before > 1) { | |
+ int mid = (first + before) / 2; | |
+ int cmp = rb_compare_feature_name(values[mid], feature, flen); | |
+ if (cmp >= 0) | |
+ first = mid; | |
+ else | |
+ before = mid; | |
+ } | |
+ return first; | |
+ } else { | |
+ return 0; | |
+ } | |
+} | |
+ | |
+/* returns position to insert new feature in */ | |
+static int | |
+rb_feature_first_greater(VALUE features, const char *feature, long flen) | |
+{ | |
+ if (sorted_loaded_features) { | |
+ int before = 0, first = RARRAY_LEN(features); | |
+ VALUE *values = RARRAY_PTR(features); | |
+ if (first == 0) | |
+ return 0; | |
+ if (rb_compare_feature_name(values[0], feature, flen) > 0) | |
+ return 0; | |
+ if (rb_compare_feature_name(values[first-1], feature, flen) <= 0) | |
+ return first; | |
+ | |
+ while (first - before > 1) { | |
+ int mid = (first + before) / 2; | |
+ int cmp = rb_compare_feature_name(values[mid], feature, flen); | |
+ if (cmp > 0) | |
+ first = mid; | |
+ else | |
+ before = mid; | |
+ } | |
+ return first; | |
+ } else { | |
+ return RARRAY_LEN(features); | |
+ } | |
+} | |
+ | |
+ | |
+static VALUE | |
+rb_push_feature_1(VALUE features, VALUE feature) | |
+{ | |
+ const char *fname = StringValuePtr(feature); | |
+ long flen = feature_basename_length(fname, RSTRING_LEN(feature)); | |
+ int i = rb_feature_first_greater(features, fname, flen); | |
+ rb_ary_push(features, feature); | |
+ if ( i < RARRAY_LEN(features) - 1 ) { | |
+ MEMMOVE(RARRAY_PTR(features) + i + 1, RARRAY_PTR(features) + i, | |
+ VALUE, RARRAY_LEN(features) - i - 1); | |
+ RARRAY_PTR(features)[i] = feature; | |
+ } | |
+ return features; | |
+} | |
+ | |
+static VALUE | |
+rb_push_feature_m(int argc, VALUE *argv, VALUE features) | |
+{ | |
+ while (argc--) { | |
+ rb_push_feature_1(features, *argv++); | |
+ } | |
+ return features; | |
+} | |
+ | |
+static VALUE | |
+rb_concat_features(VALUE features, VALUE add) | |
+{ | |
+ add = rb_convert_type(add, T_ARRAY, "Array", "to_ary"); | |
+ if (RARRAY_LEN(add)) { | |
+ rb_push_feature_m(RARRAY_LEN(add), RARRAY_PTR(add), features); | |
+ } | |
+ return features; | |
+} | |
+static const char *load_features_undefined_methods[] = { | |
+ "[]=", "reverse!", "rotate!", "sort!", "sort_by!", | |
+ "collect!", "map!", "shuffle!", "fill", "insert", | |
+ NULL | |
+}; | |
+ | |
+static VALUE | |
+rb_loaded_features_init(void) | |
+{ | |
+ char *sorted_flag; | |
+ const char **name; | |
+ VALUE loaded_features = rb_ary_new(); | |
+ VALUE loaded_features_c = rb_singleton_class(loaded_features); | |
+ | |
+ sorted_flag = getenv("RUBY_LOADED_FEATURES_SORTED"); | |
+ if (sorted_flag != NULL) { | |
+ int sorted_set = atoi(sorted_flag); | |
+ if (RTEST(ruby_verbose)) | |
+ fprintf(stderr, "sorted_loaded_features=%d (%d)\n", sorted_set, sorted_loaded_features); | |
+ sorted_loaded_features = sorted_set; | |
+ } | |
+ | |
+ for(name = load_features_undefined_methods; *name; name++) { | |
+ rb_undef_method(loaded_features_c, *name); | |
+ } | |
+ | |
+ if (sorted_loaded_features) { | |
+ rb_define_method(loaded_features_c, "<<", rb_push_feature_1, 1); | |
+ rb_define_method(loaded_features_c, "push", rb_push_feature_m, -1); | |
+ rb_define_method(loaded_features_c, "concat", rb_concat_features, 1); | |
+ rb_define_method(loaded_features_c, "unshift", rb_push_feature_m, -1); | |
+ } | |
+ return loaded_features; | |
+} | |
+ | |
static void | |
rb_provide_feature(VALUE feature) | |
{ | |
@@ -258,7 +448,10 @@ rb_provide_feature(VALUE feature) | |
rb_raise(rb_eRuntimeError, | |
"$LOADED_FEATURES is frozen; cannot append feature"); | |
} | |
- rb_ary_push(get_loaded_features(), feature); | |
+ if (sorted_loaded_features) | |
+ rb_push_feature_1(get_loaded_features(), feature); | |
+ else | |
+ rb_ary_push(get_loaded_features(), feature); | |
} | |
void | |
@@ -305,7 +498,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); | |
} | |
@@ -760,6 +953,223 @@ rb_f_autoload_p(VALUE obj, VALUE sym) | |
return rb_mod_autoload_p(klass, sym); | |
} | |
+// $LOAD_PATH methods which invalidates cache | |
+static const char *load_path_reset_cache_methods[] = { | |
+ "[]=", "collect!", "compact!", "delete", | |
+ "delete_if", "fill", "flatten!", "insert", "keep_if", | |
+ "map!", "reject!", "replace", "select!", "shuffle!", | |
+ "sort!", "sort_by!", "uniq!", NULL | |
+}; | |
+ | |
+// $LOAD_PATH methods which sends also to cache | |
+static const char *load_path_apply_to_cache_methods[] = { | |
+ "clear", "delete_at", "pop", "reverse!", "rotate!", | |
+ "shift", "slice!", NULL | |
+}; | |
+ | |
+// $LOAD_PATH methods which sends to cache whith expanded arguments | |
+static const char *load_path_apply_expanded_methods[] = { | |
+ "<<", "push", "unshift", NULL | |
+}; | |
+ | |
+void | |
+rb_reset_expanded_cache() | |
+{ | |
+ GET_VM()->load_path_expanded_cache = 0; | |
+} | |
+ | |
+static VALUE | |
+rb_load_path_expanded_cache() | |
+{ | |
+ VALUE cache = GET_VM()->load_path_expanded_cache; | |
+ VALUE expanded = Qnil; | |
+ if (RTEST(cache)) { | |
+ expanded = RARRAY_PTR(cache)[2]; | |
+ } | |
+ return expanded; | |
+} | |
+ | |
+// Return cache only if we still in the same working directory | |
+// and filesystem_encoding didn't change | |
+// Invalidate cache otherwise | |
+static VALUE | |
+rb_checked_expanded_cache(int *has_relative) | |
+{ | |
+ VALUE cache = GET_VM()->load_path_expanded_cache; | |
+ VALUE expanded = Qnil; | |
+ if (RTEST(cache)) { | |
+ VALUE curwd = RARRAY_PTR(cache)[0]; | |
+ VALUE encindex = RARRAY_PTR(cache)[1]; | |
+ int cache_valid = rb_filesystem_encindex() == FIX2INT(encindex); | |
+ | |
+ if ( cache_valid ) { | |
+ cache_valid = curwd == Qtrue; | |
+ if (has_relative) { | |
+ *has_relative = cache_valid; | |
+ } | |
+ if (!cache_valid ) { | |
+ char *cwd = my_getcwd(); | |
+ cache_valid = !strcmp(RSTRING_PTR(curwd), cwd); | |
+ xfree(cwd); | |
+ } | |
+ } | |
+ | |
+ if ( !cache_valid ) { | |
+ rb_reset_expanded_cache(); | |
+ } else { | |
+ expanded = RARRAY_PTR(cache)[2]; | |
+ } | |
+ } | |
+ RB_GC_GUARD(cache); | |
+ return expanded; | |
+} | |
+ | |
+static void | |
+rb_set_expanded_cache(VALUE expanded, int has_relative) | |
+{ | |
+ VALUE cache = rb_ary_new2(2); | |
+ | |
+ if (has_relative) { | |
+ char *cwd = my_getcwd(); | |
+ rb_ary_push(cache, rb_str_new_cstr(cwd)); | |
+ xfree(cwd); | |
+ } else { | |
+ rb_ary_push(cache, Qtrue); | |
+ } | |
+ | |
+ rb_ary_push(cache, INT2FIX(rb_filesystem_encindex())); | |
+ rb_ary_push(cache, rb_ary_dup(expanded)); | |
+ GET_VM()->load_path_expanded_cache = cache; | |
+} | |
+ | |
+static VALUE | |
+rb_expand_load_paths(int pathc, VALUE* paths, int *has_relative) | |
+{ | |
+ int i; | |
+ const char *p; | |
+ VALUE path, expanded = rb_ary_new2(pathc); | |
+ | |
+ for(i = 0; i < pathc; i++) { | |
+ path = rb_get_path(paths[i]); | |
+ p = RSTRING_PTR(path); | |
+ *has_relative = *has_relative || !rb_is_absolute_path(p); | |
+ path = rb_funcall(rb_cFile, rb_intern("expand_path"), 1, path); | |
+ rb_str_freeze(path); | |
+ rb_ary_push(expanded, path); | |
+ } | |
+ | |
+ return expanded; | |
+} | |
+ | |
+// Invalidating $LOAD_PATH methods implementation | |
+static VALUE | |
+rb_load_path_reset_cache_method(int argc, VALUE *argv, VALUE self) | |
+{ | |
+ rb_reset_expanded_cache(); | |
+ return rb_call_super(argc, argv); | |
+} | |
+ | |
+// Proxying $LOAD_PATH methods implementation | |
+static VALUE | |
+rb_load_path_apply_to_cache_method(int argc, VALUE *argv, VALUE self) | |
+{ | |
+ VALUE load_path_expanded = rb_load_path_expanded_cache(); | |
+ if (RTEST(load_path_expanded)) { | |
+ ID func = rb_frame_this_func(); | |
+ rb_funcall2(load_path_expanded, func, argc, argv); | |
+ } | |
+ return rb_call_super(argc, argv); | |
+} | |
+ | |
+// Proxying with expansion $LOAD_PATH methods implementation | |
+static VALUE | |
+rb_load_path_apply_expanded_method(int argc, VALUE *argv, VALUE self) | |
+{ | |
+ int old_has_relative = 0; | |
+ // We call methods on cache only if we still in the same working directory | |
+ VALUE load_path_expanded = rb_checked_expanded_cache(&old_has_relative); | |
+ if (RTEST(load_path_expanded)) { | |
+ int has_relative = 0; | |
+ ID func = rb_frame_this_func(); | |
+ VALUE expanded = rb_expand_load_paths(argc, argv, &has_relative); | |
+ | |
+ rb_funcall2(load_path_expanded, func, argc, RARRAY_PTR(expanded)); | |
+ | |
+ if (!old_has_relative && has_relative) { | |
+ rb_set_expanded_cache(load_path_expanded, has_relative); | |
+ } | |
+ RB_GC_GUARD(expanded); | |
+ } | |
+ return rb_call_super(argc, argv); | |
+} | |
+// $LOAD_PATH.concat(ary) - special, we call push(*ary) instead | |
+// cause I'm lazy a bit and wish not to rewrite method above second time :) | |
+static VALUE | |
+rb_load_path_concat(VALUE self, VALUE ary) | |
+{ | |
+ ID push; | |
+ CONST_ID(push, "push"); | |
+ RB_GC_GUARD(ary); | |
+ return rb_funcall2(self, push, RARRAY_LEN(ary), RARRAY_PTR(ary)); | |
+} | |
+ | |
+void | |
+rb_load_path_ary_push(VALUE path) | |
+{ | |
+ int old_has_relative = 0; | |
+ VALUE load_path_expanded = rb_checked_expanded_cache(&old_has_relative); | |
+ if (RTEST(load_path_expanded)) { | |
+ int has_relative = 0; | |
+ VALUE expanded = rb_expand_load_paths(1, &path, &has_relative); | |
+ | |
+ rb_ary_push(load_path_expanded, RARRAY_PTR(expanded)[0]); | |
+ | |
+ if (!old_has_relative && has_relative) { | |
+ rb_set_expanded_cache(load_path_expanded, has_relative); | |
+ } | |
+ RB_GC_GUARD(expanded); | |
+ } | |
+ | |
+ rb_ary_push(rb_get_load_path(), path); | |
+} | |
+ | |
+static VALUE | |
+rb_load_path_init(void) | |
+{ | |
+ const char **name; | |
+ VALUE load_path = rb_ary_new(); | |
+ char *cached_flag; | |
+ | |
+ cached_flag = getenv("RUBY_CACHED_LOAD_PATH"); | |
+ if (cached_flag != NULL) { | |
+ cached_expanded_load_path = atoi(cached_flag); | |
+ } | |
+ | |
+ // Do all the magick if user did not disable it | |
+ // with RUBY_CACHED_LOAD_PATH=0 environment variable | |
+ if (cached_expanded_load_path) { | |
+ VALUE load_path_c = rb_singleton_class(load_path); | |
+ | |
+ for(name = load_path_reset_cache_methods; *name; name++ ) { | |
+ rb_define_method(load_path_c, *name, rb_load_path_reset_cache_method, -1); | |
+ } | |
+ | |
+ for(name = load_path_apply_to_cache_methods; *name; name++ ) { | |
+ rb_define_method(load_path_c, *name, rb_load_path_apply_to_cache_method, -1); | |
+ } | |
+ | |
+ for(name = load_path_apply_expanded_methods; *name; name++ ) { | |
+ rb_define_method(load_path_c, *name, rb_load_path_apply_expanded_method, -1); | |
+ } | |
+ | |
+ rb_define_method(load_path_c, "concat", rb_load_path_concat, 1); | |
+ } | |
+ | |
+ rb_reset_expanded_cache(); | |
+ | |
+ return load_path; | |
+} | |
+ | |
void | |
Init_load() | |
{ | |
@@ -772,11 +1182,11 @@ Init_load() | |
rb_define_hooked_variable(var_load_path, (VALUE*)vm, load_path_getter, rb_gvar_readonly_setter); | |
rb_alias_variable(rb_intern("$-I"), id_load_path); | |
rb_alias_variable(rb_intern("$LOAD_PATH"), id_load_path); | |
- vm->load_path = rb_ary_new(); | |
+ vm->load_path = rb_load_path_init(); | |
rb_define_virtual_variable("$\"", get_loaded_features, 0); | |
rb_define_virtual_variable("$LOADED_FEATURES", get_loaded_features, 0); | |
- vm->loaded_features = rb_ary_new(); | |
+ vm->loaded_features = rb_loaded_features_init(); | |
rb_define_global_function("load", rb_f_load, -1); | |
rb_define_global_function("require", rb_f_require, 1); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment