public
Created

  • Download Gist
r31875-comment.diff
Diff
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
Index: load.c
===================================================================
--- load.c (revision 31933)
+++ load.c (working copy)
@@ -68,41 +68,66 @@
return GET_VM()->loading_table;
}
+// If the given loaded_name looks match with the given feature, it returns
+// the loaded path in $LOAD_PATH where the loaded_name is considered to be
+// loaded from. Otherwise, it returns 0.
+//
+// ex. /usr/lib/ruby/1.9.1/logger.rb, logger
+// => /usr/lib/ruby/1.9.1
+// ex. /usr/lib/ruby/1.9.1/rubygems/version.rb, version
+// => generally 0 since $LOAD_PATH does not include '/usr/lib/ruby/1.9.1/rubygems'
+//
+// Returned value is only used as a flag for indicating 'found' or 'not found.'
+// So it's the function for 'is this feature matches with this loaded_feature?'
+//
static VALUE
-loaded_feature_path(const char *name, long vlen, const char *feature, long len,
+loaded_feature_path(const char *loaded_name, long nlen, const char *feature, long flen,
int type, VALUE load_path)
{
long i;
long plen;
const char *e;
- if(vlen < len) return 0;
- if (!strncmp(name+(vlen-len),feature,len)){
- plen = vlen - len - 1;
+ if(nlen < flen) return 0;
+
+ // set path length of load_name if the given loaded_name looks match with the feature.
+
+ // set plen temporarily
+ plen = nlen - flen;
+ if (!strncmp(loaded_name + plen, feature, flen)){
+ // tail match
+ plen = plen - 1;
} else {
- for (e = name + vlen; name != e && *e != '.' && *e != '/'; --e);
- if (*e!='.' ||
- e-name < len ||
- strncmp(e-len,feature,len) )
+ // scan ext part of loaded_name
+ for (e = loaded_name + nlen; loaded_name != e && *e != '.' && *e != '/'; --e);
+ if (*e != '.' || // no extension
+ e - loaded_name < flen || // feature cannot match this loaded_name
+ strncmp(e - flen, feature, flen) ) // basename does not match
return 0;
- plen = e - name - len - 1;
+ // basename match
+ plen = e - loaded_name - flen - 1;
}
+
+ // search path in $LOAD_PATH
for (i = 0; i < RARRAY_LEN(load_path); ++i) {
- VALUE p = RARRAY_PTR(load_path)[i];
- const char *s = StringValuePtr(p);
- long n = RSTRING_LEN(p);
+ VALUE path = RARRAY_PTR(load_path)[i];
+ const char *cpath = StringValuePtr(path);
+ long len = RSTRING_LEN(path);
- if (n != plen ) continue;
- if (n && (strncmp(name, s, n) || name[n] != '/')) continue;
+ if (len != plen ) continue; // path length does not match
+ if (len && (strncmp(loaded_name, cpath, len) || loaded_name[len] != '/')) continue; // path part does not match
switch (type) {
case 's':
- if (IS_DLEXT(&name[n+len+1])) return p;
+ // check if it's EXT (.so) if type is specified
+ if (IS_DLEXT(&loaded_name[len + flen + 1])) return path;
break;
case 'r':
- if (IS_RBEXT(&name[n+len+1])) return p;
+ // check if it's Ruby (.rb) if type is specified
+ if (IS_RBEXT(&loaded_name[len + flen + 1])) return path;
break;
default:
- return p;
+ // type is not specified
+ return path;
}
}
return 0;

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.