Skip to content

Instantly share code, notes, and snippets.

@nahi
Created June 6, 2011 04:31
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 nahi/1009750 to your computer and use it in GitHub Desktop.
Save nahi/1009750 to your computer and use it in GitHub Desktop.
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'
+//
+// 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
+ long 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;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment