2006-12-31 10:02:22 -05:00
|
|
|
/*
|
|
|
|
* load methods from eval.c
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "eval_intern.h"
|
|
|
|
|
|
|
|
extern VALUE ruby_top_self;
|
|
|
|
|
|
|
|
VALUE ruby_dln_librefs;
|
|
|
|
|
|
|
|
#define IS_SOEXT(e) (strcmp(e, ".so") == 0 || strcmp(e, ".o") == 0)
|
|
|
|
#ifdef DLEXT2
|
|
|
|
#define IS_DLEXT(e) (strcmp(e, DLEXT) == 0 || strcmp(e, DLEXT2) == 0)
|
|
|
|
#else
|
|
|
|
#define IS_DLEXT(e) (strcmp(e, DLEXT) == 0)
|
|
|
|
#endif
|
|
|
|
|
2007-05-03 09:19:11 -04:00
|
|
|
|
|
|
|
static const char *const loadable_ext[] = {
|
|
|
|
".rb", DLEXT,
|
|
|
|
#ifdef DLEXT2
|
|
|
|
DLEXT2,
|
|
|
|
#endif
|
|
|
|
0
|
|
|
|
};
|
|
|
|
|
2007-02-13 21:19:02 -05:00
|
|
|
static VALUE
|
|
|
|
get_loaded_features(void)
|
|
|
|
{
|
|
|
|
return GET_VM()->loaded_features;
|
|
|
|
}
|
|
|
|
|
2007-05-03 09:19:11 -04:00
|
|
|
static st_table *
|
|
|
|
get_loading_table(void)
|
|
|
|
{
|
|
|
|
return GET_VM()->loading_table;
|
|
|
|
}
|
|
|
|
|
2006-12-31 10:02:22 -05:00
|
|
|
static int
|
|
|
|
rb_feature_p(const char *feature, const char *ext, int rb)
|
|
|
|
{
|
2007-05-03 09:19:11 -04:00
|
|
|
VALUE v, features;
|
|
|
|
const char *f, *e;
|
2006-12-31 10:02:22 -05:00
|
|
|
long i, len, elen;
|
2007-05-03 09:19:11 -04:00
|
|
|
st_table *loading_tbl;
|
2006-12-31 10:02:22 -05:00
|
|
|
|
|
|
|
if (ext) {
|
|
|
|
len = ext - feature;
|
|
|
|
elen = strlen(ext);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
len = strlen(feature);
|
|
|
|
elen = 0;
|
|
|
|
}
|
2007-05-03 09:19:11 -04:00
|
|
|
features = get_loaded_features();
|
|
|
|
for (i = 0; i < RARRAY_LEN(features); ++i) {
|
|
|
|
v = RARRAY_PTR(features)[i];
|
2006-12-31 10:02:22 -05:00
|
|
|
f = StringValuePtr(v);
|
2007-05-03 09:19:11 -04:00
|
|
|
if (RSTRING_LEN(v) < len || strncmp(f, feature, len) != 0)
|
2006-12-31 10:02:22 -05:00
|
|
|
continue;
|
|
|
|
if (!*(e = f + len)) {
|
2007-05-03 09:19:11 -04:00
|
|
|
if (ext) continue;
|
2006-12-31 10:02:22 -05:00
|
|
|
return 'u';
|
|
|
|
}
|
2007-05-03 09:19:11 -04:00
|
|
|
if (*e != '.') continue;
|
2006-12-31 10:02:22 -05:00
|
|
|
if ((!rb || !ext) && (IS_SOEXT(e) || IS_DLEXT(e))) {
|
|
|
|
return 's';
|
|
|
|
}
|
|
|
|
if ((rb || !ext) && (strcmp(e, ".rb") == 0)) {
|
|
|
|
return 'r';
|
|
|
|
}
|
|
|
|
}
|
2007-05-03 09:19:11 -04:00
|
|
|
loading_tbl = get_loading_table();
|
|
|
|
if (loading_tbl) {
|
|
|
|
if (st_lookup(loading_tbl, (st_data_t)feature, 0)) {
|
|
|
|
if (!ext) return 'u';
|
|
|
|
return strcmp(ext, ".rb") ? 's' : 'r';
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
char *buf;
|
|
|
|
|
|
|
|
if (ext && *ext) return 0;
|
|
|
|
buf = ALLOCA_N(char, len + DLEXT_MAXLEN + 1);
|
|
|
|
MEMCPY(buf, feature, char, len);
|
|
|
|
for (i = 0; (e = loadable_ext[i]) != 0; i++) {
|
|
|
|
strncpy(buf + len, e, DLEXT_MAXLEN + 1);
|
|
|
|
if (st_lookup(loading_tbl, (st_data_t)buf, 0)) {
|
|
|
|
return i ? 's' : 'r';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-12-31 10:02:22 -05:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
rb_provided(const char *feature)
|
|
|
|
{
|
2007-05-03 09:19:11 -04:00
|
|
|
const char *ext = strrchr(feature, '.');
|
2006-12-31 10:02:22 -05:00
|
|
|
|
2007-05-03 09:19:11 -04:00
|
|
|
if (ext && !strchr(ext, '/')) {
|
|
|
|
if (strcmp(".rb", ext) == 0) {
|
|
|
|
if (rb_feature_p(feature, ext, Qtrue)) return Qtrue;
|
|
|
|
return Qfalse;
|
|
|
|
}
|
|
|
|
else if (IS_SOEXT(ext) || IS_DLEXT(ext)) {
|
|
|
|
if (rb_feature_p(feature, ext, Qfalse)) return Qtrue;
|
|
|
|
return Qfalse;
|
2006-12-31 10:02:22 -05:00
|
|
|
}
|
|
|
|
}
|
2007-05-03 09:19:11 -04:00
|
|
|
if (rb_feature_p(feature, feature + strlen(feature), Qtrue))
|
|
|
|
return Qtrue;
|
|
|
|
|
2006-12-31 10:02:22 -05:00
|
|
|
return Qfalse;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
rb_provide_feature(VALUE feature)
|
|
|
|
{
|
2007-02-13 21:19:02 -05:00
|
|
|
rb_ary_push(get_loaded_features(), feature);
|
2006-12-31 10:02:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rb_provide(const char *feature)
|
|
|
|
{
|
|
|
|
rb_provide_feature(rb_str_new2(feature));
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE rb_load_path;
|
|
|
|
|
|
|
|
NORETURN(static void load_failed _((VALUE)));
|
* this commit is a result of refactoring. only renaming functions,
moving definitions place, add/remove prototypes, deleting
unused variables and removing yarv.h.
This commit doesn't change any behavior of ruby/vm.
* yarv.h, common.mk: remove yarv.h (contents are moved to yarvcore.h).
* error.c, eval_intern.h: include yarvcore.h instead yarv.h
* rename some functions:
* debug.[ch]: debug_*() -> ruby_debug_*()
* iseq.c: iseq_*() -> rb_iseq_*(), ruby_iseq_disasm()
* iseq.c: node_name() -> ruby_node_name()
* vm.c: yarv_check_redefinition_opt_method() ->
rb_vm_check_redefinition_opt_method()
* some refactoring with checking -Wall.
* array.c: remove rb_ary_ptr() (unused) and remove unused
local variables.
* object.c: add a prototype of rb_mod_module_exec().
* eval_intern.h (ruby_cref): set it inline.
* eval_load.c (rb_load), yarvcore.c: yarv_load() -> rb_load_internal().
* parse.y: add a prototype of rb_parse_in_eval() (in eval.c).
* process.c: add a prototype of rb_thread_stop_timer_thread() (in thread.c).
* thread.c: remove raw_gets() function (unused) and fix some format
mismatch (format mismatchs have remained yet. this is todo).
* thread.c (rb_thread_wait_fd_rw): fix typo on label name.
* thread_pthread.ci: comment out codes with USE_THREAD_CACHE.
* vm.c (rb_svar, rb_backref_get, rb_backref_get,
rb_lastline_get, rb_lastline_set) : moved from yarvcore.c.
* vm.c (yarv_init_redefined_flag): add a prototype and rename
yarv_opt_method_table to vm_opt_method_table.
* vm.c (rb_thread_eval): moved from yarvcore.c.
* yarvcore.c: remove unused global variables and fix to use nsdr().
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@11652 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-02-06 20:25:05 -05:00
|
|
|
|
|
|
|
RUBY_EXTERN NODE *ruby_eval_tree;
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_load_internal(char *file)
|
|
|
|
{
|
|
|
|
NODE *node;
|
|
|
|
VALUE iseq;
|
* blockinlining.c, error.c, eval.c, eval_error.h, eval_intern.h,
eval_jump.h, eval_load.c, eval_safe.h, gc.c, proc.c, signal.c,
thread.c, thread_pthread.ci, thread_win32.ci, vm.c, vm.h,
vm_dump.c, vm_evalbody.ci, yarvcore.c, yarvcore.h:
fix typo (rb_thead_t -> rb_thread_t).
* eval_intern.h: remove unused definitions.
* common.mk: fix around vm_opts.h path
and remove harmful argument passed to insns2vm.rb.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@11658 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-02-08 01:37:46 -05:00
|
|
|
rb_thread_t *th = GET_THREAD();
|
* this commit is a result of refactoring. only renaming functions,
moving definitions place, add/remove prototypes, deleting
unused variables and removing yarv.h.
This commit doesn't change any behavior of ruby/vm.
* yarv.h, common.mk: remove yarv.h (contents are moved to yarvcore.h).
* error.c, eval_intern.h: include yarvcore.h instead yarv.h
* rename some functions:
* debug.[ch]: debug_*() -> ruby_debug_*()
* iseq.c: iseq_*() -> rb_iseq_*(), ruby_iseq_disasm()
* iseq.c: node_name() -> ruby_node_name()
* vm.c: yarv_check_redefinition_opt_method() ->
rb_vm_check_redefinition_opt_method()
* some refactoring with checking -Wall.
* array.c: remove rb_ary_ptr() (unused) and remove unused
local variables.
* object.c: add a prototype of rb_mod_module_exec().
* eval_intern.h (ruby_cref): set it inline.
* eval_load.c (rb_load), yarvcore.c: yarv_load() -> rb_load_internal().
* parse.y: add a prototype of rb_parse_in_eval() (in eval.c).
* process.c: add a prototype of rb_thread_stop_timer_thread() (in thread.c).
* thread.c: remove raw_gets() function (unused) and fix some format
mismatch (format mismatchs have remained yet. this is todo).
* thread.c (rb_thread_wait_fd_rw): fix typo on label name.
* thread_pthread.ci: comment out codes with USE_THREAD_CACHE.
* vm.c (rb_svar, rb_backref_get, rb_backref_get,
rb_lastline_get, rb_lastline_set) : moved from yarvcore.c.
* vm.c (yarv_init_redefined_flag): add a prototype and rename
yarv_opt_method_table to vm_opt_method_table.
* vm.c (rb_thread_eval): moved from yarvcore.c.
* yarvcore.c: remove unused global variables and fix to use nsdr().
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@11652 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-02-06 20:25:05 -05:00
|
|
|
|
|
|
|
{
|
|
|
|
th->parse_in_eval++;
|
|
|
|
node = (NODE *)rb_load_file(file);
|
|
|
|
th->parse_in_eval--;
|
|
|
|
node = ruby_eval_tree;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ruby_nerrs > 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
iseq = rb_iseq_new(node, rb_str_new2("<top (required)>"),
|
2007-02-25 11:29:26 -05:00
|
|
|
rb_str_new2(file), Qfalse, ISEQ_TYPE_TOP);
|
* this commit is a result of refactoring. only renaming functions,
moving definitions place, add/remove prototypes, deleting
unused variables and removing yarv.h.
This commit doesn't change any behavior of ruby/vm.
* yarv.h, common.mk: remove yarv.h (contents are moved to yarvcore.h).
* error.c, eval_intern.h: include yarvcore.h instead yarv.h
* rename some functions:
* debug.[ch]: debug_*() -> ruby_debug_*()
* iseq.c: iseq_*() -> rb_iseq_*(), ruby_iseq_disasm()
* iseq.c: node_name() -> ruby_node_name()
* vm.c: yarv_check_redefinition_opt_method() ->
rb_vm_check_redefinition_opt_method()
* some refactoring with checking -Wall.
* array.c: remove rb_ary_ptr() (unused) and remove unused
local variables.
* object.c: add a prototype of rb_mod_module_exec().
* eval_intern.h (ruby_cref): set it inline.
* eval_load.c (rb_load), yarvcore.c: yarv_load() -> rb_load_internal().
* parse.y: add a prototype of rb_parse_in_eval() (in eval.c).
* process.c: add a prototype of rb_thread_stop_timer_thread() (in thread.c).
* thread.c: remove raw_gets() function (unused) and fix some format
mismatch (format mismatchs have remained yet. this is todo).
* thread.c (rb_thread_wait_fd_rw): fix typo on label name.
* thread_pthread.ci: comment out codes with USE_THREAD_CACHE.
* vm.c (rb_svar, rb_backref_get, rb_backref_get,
rb_lastline_get, rb_lastline_set) : moved from yarvcore.c.
* vm.c (yarv_init_redefined_flag): add a prototype and rename
yarv_opt_method_table to vm_opt_method_table.
* vm.c (rb_thread_eval): moved from yarvcore.c.
* yarvcore.c: remove unused global variables and fix to use nsdr().
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@11652 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-02-06 20:25:05 -05:00
|
|
|
|
|
|
|
rb_thread_eval(GET_THREAD(), iseq);
|
|
|
|
return 0;
|
|
|
|
}
|
2006-12-31 10:02:22 -05:00
|
|
|
|
|
|
|
void
|
|
|
|
rb_load(VALUE fname, int wrap)
|
|
|
|
{
|
|
|
|
VALUE tmp;
|
|
|
|
int state;
|
2007-02-25 11:29:26 -05:00
|
|
|
rb_thread_t *th = GET_THREAD();
|
|
|
|
VALUE wrapper = th->top_wrapper;
|
|
|
|
VALUE self = th->top_self;
|
2006-12-31 10:02:22 -05:00
|
|
|
|
|
|
|
FilePathValue(fname);
|
|
|
|
fname = rb_str_new4(fname);
|
|
|
|
tmp = rb_find_file(fname);
|
|
|
|
if (!tmp) {
|
|
|
|
load_failed(fname);
|
|
|
|
}
|
|
|
|
fname = tmp;
|
|
|
|
|
2007-02-25 11:29:26 -05:00
|
|
|
th->errinfo = Qnil; /* ensure */
|
|
|
|
|
2006-12-31 10:02:22 -05:00
|
|
|
if (!wrap) {
|
|
|
|
rb_secure(4); /* should alter global state */
|
2007-02-25 11:29:26 -05:00
|
|
|
th->top_wrapper = 0;
|
2006-12-31 10:02:22 -05:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* load in anonymous module as toplevel */
|
2007-02-25 11:29:26 -05:00
|
|
|
th->top_self = rb_obj_clone(ruby_top_self);
|
|
|
|
th->top_wrapper = rb_module_new();
|
|
|
|
rb_extend_object(th->top_self, th->top_wrapper);
|
2006-12-31 10:02:22 -05:00
|
|
|
}
|
|
|
|
|
2007-04-24 21:35:13 -04:00
|
|
|
PUSH_TAG();
|
2006-12-31 10:02:22 -05:00
|
|
|
state = EXEC_TAG();
|
|
|
|
if (state == 0) {
|
* this commit is a result of refactoring. only renaming functions,
moving definitions place, add/remove prototypes, deleting
unused variables and removing yarv.h.
This commit doesn't change any behavior of ruby/vm.
* yarv.h, common.mk: remove yarv.h (contents are moved to yarvcore.h).
* error.c, eval_intern.h: include yarvcore.h instead yarv.h
* rename some functions:
* debug.[ch]: debug_*() -> ruby_debug_*()
* iseq.c: iseq_*() -> rb_iseq_*(), ruby_iseq_disasm()
* iseq.c: node_name() -> ruby_node_name()
* vm.c: yarv_check_redefinition_opt_method() ->
rb_vm_check_redefinition_opt_method()
* some refactoring with checking -Wall.
* array.c: remove rb_ary_ptr() (unused) and remove unused
local variables.
* object.c: add a prototype of rb_mod_module_exec().
* eval_intern.h (ruby_cref): set it inline.
* eval_load.c (rb_load), yarvcore.c: yarv_load() -> rb_load_internal().
* parse.y: add a prototype of rb_parse_in_eval() (in eval.c).
* process.c: add a prototype of rb_thread_stop_timer_thread() (in thread.c).
* thread.c: remove raw_gets() function (unused) and fix some format
mismatch (format mismatchs have remained yet. this is todo).
* thread.c (rb_thread_wait_fd_rw): fix typo on label name.
* thread_pthread.ci: comment out codes with USE_THREAD_CACHE.
* vm.c (rb_svar, rb_backref_get, rb_backref_get,
rb_lastline_get, rb_lastline_set) : moved from yarvcore.c.
* vm.c (yarv_init_redefined_flag): add a prototype and rename
yarv_opt_method_table to vm_opt_method_table.
* vm.c (rb_thread_eval): moved from yarvcore.c.
* yarvcore.c: remove unused global variables and fix to use nsdr().
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@11652 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-02-06 20:25:05 -05:00
|
|
|
rb_load_internal(RSTRING_PTR(fname));
|
2006-12-31 10:02:22 -05:00
|
|
|
}
|
|
|
|
POP_TAG();
|
|
|
|
|
2007-02-25 11:29:26 -05:00
|
|
|
th->top_self = self;
|
|
|
|
th->top_wrapper = wrapper;
|
|
|
|
|
2006-12-31 10:02:22 -05:00
|
|
|
if (ruby_nerrs > 0) {
|
|
|
|
ruby_nerrs = 0;
|
|
|
|
rb_exc_raise(GET_THREAD()->errinfo);
|
|
|
|
}
|
|
|
|
if (state) {
|
* vm.c: some refactoring.
* rename th_* to vm_*.
* remove unused variables functions.
* add prototypes.
* blockinlining.c, compile.c, cont.c, eval.c, eval_intern.h,
eval_jump.h, eval_load.c, inits.c, insns.def, iseq.c, parse.y,
proc.c, process.c, signal.c, thread.c, vm.c, vm_dump.c,
vm_evalbody.ci, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12603 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-06-24 11:42:41 -04:00
|
|
|
vm_jump_tag_but_local_jump(state, Qundef);
|
2006-12-31 10:02:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!NIL_P(GET_THREAD()->errinfo)) {
|
|
|
|
/* exception during load */
|
2007-02-25 11:29:26 -05:00
|
|
|
rb_exc_raise(th->errinfo);
|
2006-12-31 10:02:22 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rb_load_protect(VALUE fname, int wrap, int *state)
|
|
|
|
{
|
|
|
|
int status;
|
|
|
|
|
2007-04-24 21:35:13 -04:00
|
|
|
PUSH_TAG();
|
2006-12-31 10:02:22 -05:00
|
|
|
if ((status = EXEC_TAG()) == 0) {
|
|
|
|
rb_load(fname, wrap);
|
|
|
|
}
|
2007-04-24 21:35:13 -04:00
|
|
|
POP_TAG();
|
2006-12-31 10:02:22 -05:00
|
|
|
if (state)
|
|
|
|
*state = status;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* load(filename, wrap=false) => true
|
|
|
|
*
|
|
|
|
* Loads and executes the Ruby
|
|
|
|
* program in the file _filename_. If the filename does not
|
|
|
|
* resolve to an absolute path, the file is searched for in the library
|
|
|
|
* directories listed in <code>$:</code>. If the optional _wrap_
|
|
|
|
* parameter is +true+, the loaded script will be executed
|
|
|
|
* under an anonymous module, protecting the calling program's global
|
|
|
|
* namespace. In no circumstance will any local variables in the loaded
|
|
|
|
* file be propagated to the loading environment.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_f_load(argc, argv)
|
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
{
|
|
|
|
VALUE fname, wrap;
|
|
|
|
|
|
|
|
rb_scan_args(argc, argv, "11", &fname, &wrap);
|
|
|
|
rb_load(fname, RTEST(wrap));
|
|
|
|
return Qtrue;
|
|
|
|
}
|
|
|
|
|
2007-05-03 09:19:11 -04:00
|
|
|
static char *
|
|
|
|
load_lock(const char *ftptr)
|
2006-12-31 10:02:22 -05:00
|
|
|
{
|
2007-05-03 09:19:11 -04:00
|
|
|
st_data_t data;
|
|
|
|
st_table *loading_tbl = get_loading_table();
|
|
|
|
|
|
|
|
if (!loading_tbl || !st_lookup(loading_tbl, (st_data_t)ftptr, &data)) {
|
|
|
|
/* loading ruby library should be serialized. */
|
|
|
|
if (!loading_tbl) {
|
|
|
|
GET_VM()->loading_table = loading_tbl = st_init_strtable();
|
|
|
|
}
|
|
|
|
/* partial state */
|
|
|
|
ftptr = ruby_strdup(ftptr);
|
|
|
|
data = (st_data_t)rb_barrier_new();
|
|
|
|
st_insert(loading_tbl, (st_data_t)ftptr, data);
|
|
|
|
return (char *)ftptr;
|
2006-12-31 10:02:22 -05:00
|
|
|
}
|
2007-05-03 09:19:11 -04:00
|
|
|
rb_barrier_wait((VALUE)data);
|
|
|
|
return 0;
|
|
|
|
}
|
2006-12-31 10:02:22 -05:00
|
|
|
|
2007-05-03 09:19:11 -04:00
|
|
|
static void
|
|
|
|
load_unlock(const char *ftptr)
|
|
|
|
{
|
|
|
|
if (ftptr) {
|
|
|
|
st_data_t key = (st_data_t)ftptr;
|
|
|
|
st_data_t data;
|
|
|
|
st_table *loading_tbl = get_loading_table();
|
|
|
|
|
|
|
|
if (st_delete(loading_tbl, &key, &data)) {
|
|
|
|
free((char *)key);
|
|
|
|
rb_barrier_release((VALUE)data);
|
|
|
|
}
|
|
|
|
}
|
2006-12-31 10:02:22 -05:00
|
|
|
}
|
|
|
|
|
2007-05-03 09:19:11 -04:00
|
|
|
|
2006-12-31 10:02:22 -05:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* require(string) => true or false
|
|
|
|
*
|
|
|
|
* Ruby tries to load the library named _string_, returning
|
|
|
|
* +true+ if successful. If the filename does not resolve to
|
|
|
|
* an absolute path, it will be searched for in the directories listed
|
|
|
|
* in <code>$:</code>. If the file has the extension ``.rb'', it is
|
|
|
|
* loaded as a source file; if the extension is ``.so'', ``.o'', or
|
|
|
|
* ``.dll'', or whatever the default shared library extension is on
|
|
|
|
* the current platform, Ruby loads the shared library as a Ruby
|
|
|
|
* extension. Otherwise, Ruby tries adding ``.rb'', ``.so'', and so on
|
|
|
|
* to the name. The name of the loaded feature is added to the array in
|
|
|
|
* <code>$"</code>. A feature will not be loaded if it's name already
|
|
|
|
* appears in <code>$"</code>. However, the file name is not converted
|
|
|
|
* to an absolute path, so that ``<code>require 'a';require
|
|
|
|
* './a'</code>'' will load <code>a.rb</code> twice.
|
|
|
|
*
|
|
|
|
* require "my-library.rb"
|
|
|
|
* require "db-driver"
|
|
|
|
*/
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_f_require(VALUE obj, VALUE fname)
|
|
|
|
{
|
|
|
|
return rb_require_safe(fname, rb_safe_level());
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
search_required(VALUE fname, VALUE *path)
|
|
|
|
{
|
|
|
|
VALUE tmp;
|
|
|
|
char *ext, *ftptr;
|
|
|
|
int type, ft = 0;
|
|
|
|
|
|
|
|
*path = 0;
|
|
|
|
ext = strrchr(ftptr = RSTRING_PTR(fname), '.');
|
|
|
|
if (ext && !strchr(ext, '/')) {
|
|
|
|
if (strcmp(".rb", ext) == 0) {
|
|
|
|
if (rb_feature_p(ftptr, ext, Qtrue))
|
|
|
|
return 'r';
|
|
|
|
if (tmp = rb_find_file(fname)) {
|
|
|
|
tmp = rb_file_expand_path(tmp, Qnil);
|
|
|
|
ext = strrchr(ftptr = RSTRING_PTR(tmp), '.');
|
|
|
|
if (!rb_feature_p(ftptr, ext, Qtrue))
|
|
|
|
*path = tmp;
|
|
|
|
return 'r';
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else if (IS_SOEXT(ext)) {
|
|
|
|
if (rb_feature_p(ftptr, ext, Qfalse))
|
|
|
|
return 's';
|
|
|
|
tmp = rb_str_new(RSTRING_PTR(fname), ext - RSTRING_PTR(fname));
|
|
|
|
#ifdef DLEXT2
|
|
|
|
OBJ_FREEZE(tmp);
|
|
|
|
if (rb_find_file_ext(&tmp, loadable_ext + 1)) {
|
|
|
|
tmp = rb_file_expand_path(tmp, Qnil);
|
|
|
|
ext = strrchr(ftptr = RSTRING_PTR(tmp), '.');
|
|
|
|
if (!rb_feature_p(ftptr, ext, Qfalse))
|
|
|
|
*path = tmp;
|
|
|
|
return 's';
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
rb_str_cat2(tmp, DLEXT);
|
|
|
|
OBJ_FREEZE(tmp);
|
|
|
|
if (tmp = rb_find_file(tmp)) {
|
|
|
|
tmp = rb_file_expand_path(tmp, Qnil);
|
|
|
|
ext = strrchr(ftptr = RSTRING_PTR(tmp), '.');
|
|
|
|
if (!rb_feature_p(ftptr, ext, Qfalse))
|
|
|
|
*path = tmp;
|
|
|
|
return 's';
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else if (IS_DLEXT(ext)) {
|
|
|
|
if (rb_feature_p(ftptr, ext, Qfalse))
|
|
|
|
return 's';
|
|
|
|
if (tmp = rb_find_file(fname)) {
|
|
|
|
tmp = rb_file_expand_path(tmp, Qnil);
|
|
|
|
ext = strrchr(ftptr = RSTRING_PTR(tmp), '.');
|
|
|
|
if (!rb_feature_p(ftptr, ext, Qfalse))
|
|
|
|
*path = tmp;
|
|
|
|
return 's';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if ((ft = rb_feature_p(ftptr, 0, Qfalse)) == 'r') {
|
|
|
|
return 'r';
|
|
|
|
}
|
|
|
|
tmp = fname;
|
|
|
|
type = rb_find_file_ext(&tmp, loadable_ext);
|
|
|
|
tmp = rb_file_expand_path(tmp, Qnil);
|
|
|
|
switch (type) {
|
2007-05-03 09:19:11 -04:00
|
|
|
case 0:
|
2006-12-31 10:02:22 -05:00
|
|
|
ftptr = RSTRING_PTR(tmp);
|
|
|
|
if (ft)
|
|
|
|
break;
|
|
|
|
return rb_feature_p(ftptr, 0, Qfalse);
|
|
|
|
|
2007-05-03 09:19:11 -04:00
|
|
|
default:
|
2006-12-31 10:02:22 -05:00
|
|
|
if (ft)
|
|
|
|
break;
|
2007-05-03 09:19:11 -04:00
|
|
|
case 1:
|
2006-12-31 10:02:22 -05:00
|
|
|
ext = strrchr(ftptr = RSTRING_PTR(tmp), '.');
|
|
|
|
if (rb_feature_p(ftptr, ext, !--type))
|
|
|
|
break;
|
|
|
|
*path = tmp;
|
|
|
|
}
|
|
|
|
return type ? 's' : 'r';
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
load_failed(VALUE fname)
|
|
|
|
{
|
|
|
|
rb_raise(rb_eLoadError, "no such file to load -- %s",
|
|
|
|
RSTRING_PTR(fname));
|
|
|
|
}
|
|
|
|
|
2007-06-18 04:02:30 -04:00
|
|
|
static VALUE
|
2007-06-18 21:53:31 -04:00
|
|
|
load_ext(VALUE path)
|
2007-06-18 04:02:30 -04:00
|
|
|
{
|
|
|
|
SCOPE_SET(NOEX_PUBLIC);
|
2007-06-18 21:53:31 -04:00
|
|
|
return (VALUE)dln_load(RSTRING_PTR(path));
|
2007-06-18 04:02:30 -04:00
|
|
|
}
|
|
|
|
|
2006-12-31 10:02:22 -05:00
|
|
|
VALUE
|
|
|
|
rb_require_safe(VALUE fname, int safe)
|
|
|
|
{
|
|
|
|
VALUE result = Qnil;
|
* blockinlining.c, error.c, eval.c, eval_error.h, eval_intern.h,
eval_jump.h, eval_load.c, eval_safe.h, gc.c, proc.c, signal.c,
thread.c, thread_pthread.ci, thread_win32.ci, vm.c, vm.h,
vm_dump.c, vm_evalbody.ci, yarvcore.c, yarvcore.h:
fix typo (rb_thead_t -> rb_thread_t).
* eval_intern.h: remove unused definitions.
* common.mk: fix around vm_opts.h path
and remove harmful argument passed to insns2vm.rb.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@11658 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-02-08 01:37:46 -05:00
|
|
|
rb_thread_t *th = GET_THREAD();
|
2007-05-03 09:19:11 -04:00
|
|
|
volatile VALUE errinfo = th->errinfo;
|
2006-12-31 10:02:22 -05:00
|
|
|
int state;
|
2007-02-20 19:14:07 -05:00
|
|
|
struct {
|
|
|
|
int safe;
|
|
|
|
} volatile saved;
|
2006-12-31 10:02:22 -05:00
|
|
|
char *volatile ftptr = 0;
|
|
|
|
|
2007-04-24 21:35:13 -04:00
|
|
|
PUSH_TAG();
|
2007-02-20 19:14:07 -05:00
|
|
|
saved.safe = rb_safe_level();
|
2006-12-31 10:02:22 -05:00
|
|
|
if ((state = EXEC_TAG()) == 0) {
|
|
|
|
VALUE path;
|
|
|
|
long handle;
|
|
|
|
int found;
|
|
|
|
|
|
|
|
rb_set_safe_level_force(safe);
|
|
|
|
FilePathValue(fname);
|
|
|
|
*(volatile VALUE *)&fname = rb_str_new4(fname);
|
|
|
|
found = search_required(fname, &path);
|
|
|
|
if (found) {
|
2007-05-03 09:19:11 -04:00
|
|
|
if (!path || !(ftptr = load_lock(RSTRING_PTR(path)))) {
|
2006-12-31 10:02:22 -05:00
|
|
|
result = Qfalse;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rb_set_safe_level_force(0);
|
|
|
|
switch (found) {
|
2007-05-03 09:19:11 -04:00
|
|
|
case 'r':
|
2006-12-31 10:02:22 -05:00
|
|
|
rb_load(path, 0);
|
|
|
|
break;
|
|
|
|
|
2007-05-03 09:19:11 -04:00
|
|
|
case 's':
|
2006-12-31 10:02:22 -05:00
|
|
|
ruby_sourcefile = rb_source_filename(RSTRING_PTR(path));
|
|
|
|
ruby_sourceline = 0;
|
2007-06-18 04:02:30 -04:00
|
|
|
handle = (long)rb_vm_call_cfunc(ruby_top_self, load_ext,
|
2007-06-18 21:53:31 -04:00
|
|
|
path, 0, path);
|
2006-12-31 10:02:22 -05:00
|
|
|
rb_ary_push(ruby_dln_librefs, LONG2NUM(handle));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
rb_provide_feature(path);
|
|
|
|
result = Qtrue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
POP_TAG();
|
2007-05-03 09:19:11 -04:00
|
|
|
load_unlock(ftptr);
|
2006-12-31 10:02:22 -05:00
|
|
|
|
2007-02-20 19:14:07 -05:00
|
|
|
rb_set_safe_level_force(saved.safe);
|
2006-12-31 10:02:22 -05:00
|
|
|
if (state) {
|
|
|
|
JUMP_TAG(state);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NIL_P(result)) {
|
|
|
|
load_failed(fname);
|
|
|
|
}
|
|
|
|
|
|
|
|
th->errinfo = errinfo;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_require(const char *fname)
|
|
|
|
{
|
|
|
|
VALUE fn = rb_str_new2(fname);
|
|
|
|
OBJ_FREEZE(fn);
|
|
|
|
return rb_require_safe(fn, rb_safe_level());
|
|
|
|
}
|
|
|
|
|
2007-06-18 04:02:30 -04:00
|
|
|
static VALUE
|
|
|
|
init_ext_call(VALUE arg)
|
|
|
|
{
|
|
|
|
SCOPE_SET(NOEX_PUBLIC);
|
|
|
|
(*(void (*)(void))arg)();
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
2007-05-03 09:19:11 -04:00
|
|
|
void
|
|
|
|
ruby_init_ext(const char *name, void (*init)(void))
|
|
|
|
{
|
|
|
|
ruby_sourcefile = rb_source_filename(name);
|
|
|
|
ruby_sourceline = 0;
|
2007-06-24 16:33:04 -04:00
|
|
|
|
2007-05-03 09:19:11 -04:00
|
|
|
if (load_lock(name)) {
|
2007-06-18 04:02:30 -04:00
|
|
|
rb_vm_call_cfunc(ruby_top_self, init_ext_call, (VALUE)init, 0, rb_str_new2(name));
|
2007-05-03 09:19:11 -04:00
|
|
|
rb_provide(name);
|
|
|
|
load_unlock(name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-12-31 10:02:22 -05:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* mod.autoload(name, filename) => nil
|
|
|
|
*
|
|
|
|
* Registers _filename_ to be loaded (using <code>Kernel::require</code>)
|
|
|
|
* the first time that _module_ (which may be a <code>String</code> or
|
|
|
|
* a symbol) is accessed in the namespace of _mod_.
|
|
|
|
*
|
|
|
|
* module A
|
|
|
|
* end
|
|
|
|
* A.autoload(:B, "b")
|
|
|
|
* A::B.doit # autoloads "b"
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_mod_autoload(VALUE mod, VALUE sym, VALUE file)
|
|
|
|
{
|
|
|
|
ID id = rb_to_id(sym);
|
|
|
|
|
|
|
|
Check_SafeStr(file);
|
|
|
|
rb_autoload(mod, id, RSTRING_PTR(file));
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* MISSING: documentation
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_mod_autoload_p(VALUE mod, VALUE sym)
|
|
|
|
{
|
|
|
|
return rb_autoload_p(mod, rb_to_id(sym));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* autoload(module, filename) => nil
|
|
|
|
*
|
|
|
|
* Registers _filename_ to be loaded (using <code>Kernel::require</code>)
|
|
|
|
* the first time that _module_ (which may be a <code>String</code> or
|
|
|
|
* a symbol) is accessed.
|
|
|
|
*
|
|
|
|
* autoload(:MyModule, "/usr/local/lib/modules/my_module.rb")
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_f_autoload(VALUE obj, VALUE sym, VALUE file)
|
|
|
|
{
|
|
|
|
VALUE klass = ruby_cbase();
|
|
|
|
if (NIL_P(klass)) {
|
|
|
|
rb_raise(rb_eTypeError, "Can not set autoload on singleton class");
|
|
|
|
}
|
|
|
|
return rb_mod_autoload(klass, sym, file);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* MISSING: documentation
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_f_autoload_p(VALUE obj, VALUE sym)
|
|
|
|
{
|
|
|
|
/* use ruby_cbase() as same as rb_f_autoload. */
|
|
|
|
VALUE klass = ruby_cbase();
|
|
|
|
if (NIL_P(klass)) {
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
return rb_mod_autoload_p(klass, sym);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Init_load()
|
|
|
|
{
|
|
|
|
rb_define_readonly_variable("$:", &rb_load_path);
|
|
|
|
rb_define_readonly_variable("$-I", &rb_load_path);
|
|
|
|
rb_define_readonly_variable("$LOAD_PATH", &rb_load_path);
|
2007-06-14 04:35:20 -04:00
|
|
|
rb_load_path = rb_ary_new();
|
2006-12-31 10:02:22 -05:00
|
|
|
|
2007-02-13 21:19:02 -05:00
|
|
|
rb_define_virtual_variable("$\"", get_loaded_features, 0);
|
|
|
|
rb_define_virtual_variable("$LOADED_FEATURES", get_loaded_features, 0);
|
|
|
|
GET_VM()->loaded_features = rb_ary_new();
|
2006-12-31 10:02:22 -05:00
|
|
|
|
|
|
|
rb_define_global_function("load", rb_f_load, -1);
|
|
|
|
rb_define_global_function("require", rb_f_require, 1);
|
|
|
|
rb_define_method(rb_cModule, "autoload", rb_mod_autoload, 2);
|
|
|
|
rb_define_method(rb_cModule, "autoload?", rb_mod_autoload_p, 1);
|
|
|
|
rb_define_global_function("autoload", rb_f_autoload, 2);
|
|
|
|
rb_define_global_function("autoload?", rb_f_autoload_p, 1);
|
|
|
|
|
|
|
|
ruby_dln_librefs = rb_ary_new();
|
|
|
|
rb_register_mark_object(ruby_dln_librefs);
|
|
|
|
}
|