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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
|
/*
* load methods from eval.c
*/
#include "eval_intern.h"
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
static const char *const loadable_ext[] = {
".rb", DLEXT,
#ifdef DLEXT2
DLEXT2,
#endif
0
};
static VALUE
get_loaded_features(void)
{
return GET_VM()->loaded_features;
}
static st_table *
get_loading_table(void)
{
return GET_VM()->loading_table;
}
static int
rb_feature_p(const char *feature, const char *ext, int rb)
{
VALUE v, features;
const char *f, *e;
long i, len, elen;
st_table *loading_tbl;
if (ext) {
len = ext - feature;
elen = strlen(ext);
}
else {
len = strlen(feature);
elen = 0;
}
features = get_loaded_features();
for (i = 0; i < RARRAY_LEN(features); ++i) {
v = RARRAY_PTR(features)[i];
f = StringValuePtr(v);
if (RSTRING_LEN(v) < len || strncmp(f, feature, len) != 0)
continue;
if (!*(e = f + len)) {
if (ext) continue;
return 'u';
}
if (*e != '.') continue;
if ((!rb || !ext) && (IS_SOEXT(e) || IS_DLEXT(e))) {
return 's';
}
if ((rb || !ext) && (strcmp(e, ".rb") == 0)) {
return 'r';
}
}
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';
}
}
}
}
return 0;
}
int
rb_provided(const char *feature)
{
const char *ext = strrchr(feature, '.');
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;
}
}
if (rb_feature_p(feature, feature + strlen(feature), Qtrue))
return Qtrue;
return Qfalse;
}
static void
rb_provide_feature(VALUE feature)
{
rb_ary_push(get_loaded_features(), feature);
}
void
rb_provide(const char *feature)
{
rb_provide_feature(rb_str_new2(feature));
}
VALUE rb_load_path;
NORETURN(static void load_failed _((VALUE)));
void
rb_load(VALUE fname, int wrap)
{
VALUE tmp;
int state;
rb_thread_t *th = GET_THREAD();
VALUE wrapper = th->top_wrapper;
VALUE self = th->top_self;
volatile int parse_in_eval;
volatile int loaded = Qfalse;
FilePathValue(fname);
fname = rb_str_new4(fname);
tmp = rb_find_file(fname);
if (!tmp) {
load_failed(fname);
}
RB_GC_GUARD(fname) = rb_str_new4(tmp);
th->errinfo = Qnil; /* ensure */
if (!wrap) {
rb_secure(4); /* should alter global state */
th->top_wrapper = 0;
}
else {
/* load in anonymous module as toplevel */
th->top_self = rb_obj_clone(rb_vm_top_self());
th->top_wrapper = rb_module_new();
rb_extend_object(th->top_self, th->top_wrapper);
}
parse_in_eval = th->parse_in_eval;
PUSH_TAG();
state = EXEC_TAG();
if (state == 0) {
NODE *node;
VALUE iseq;
th->parse_in_eval++;
node = (NODE *)rb_load_file(RSTRING_PTR(fname));
th->parse_in_eval--;
loaded = Qtrue;
iseq = rb_iseq_new(node, rb_str_new2("<top (required)>"),
fname, Qfalse, ISEQ_TYPE_TOP);
rb_iseq_eval(iseq);
}
POP_TAG();
th->parse_in_eval = parse_in_eval;
th->top_self = self;
th->top_wrapper = wrapper;
if (!loaded) {
rb_exc_raise(GET_THREAD()->errinfo);
}
if (state) {
vm_jump_tag_but_local_jump(state, Qundef);
}
if (!NIL_P(GET_THREAD()->errinfo)) {
/* exception during load */
rb_exc_raise(th->errinfo);
}
}
void
rb_load_protect(VALUE fname, int wrap, int *state)
{
int status;
PUSH_TAG();
if ((status = EXEC_TAG()) == 0) {
rb_load(fname, wrap);
}
POP_TAG();
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;
}
static char *
load_lock(const char *ftptr)
{
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;
}
rb_barrier_wait((VALUE)data);
return 0;
}
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);
}
}
}
/*
* 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, volatile 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)) != 0) {
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)) != 0) {
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)) != 0) {
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) {
case 0:
ftptr = RSTRING_PTR(tmp);
if (ft)
break;
return rb_feature_p(ftptr, 0, Qfalse);
default:
if (ft)
break;
case 1:
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));
}
static VALUE
load_ext(VALUE path)
{
SCOPE_SET(NOEX_PUBLIC);
return (VALUE)dln_load(RSTRING_PTR(path));
}
VALUE
rb_require_safe(VALUE fname, int safe)
{
VALUE result = Qnil;
rb_thread_t *th = GET_THREAD();
volatile VALUE errinfo = th->errinfo;
int state;
struct {
int safe;
} volatile saved;
char *volatile ftptr = 0;
PUSH_TAG();
saved.safe = rb_safe_level();
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) {
if (!path || !(ftptr = load_lock(RSTRING_PTR(path)))) {
result = Qfalse;
}
else {
rb_set_safe_level_force(0);
switch (found) {
case 'r':
rb_load(path, 0);
break;
case 's':
handle = (long)rb_vm_call_cfunc(rb_vm_top_self(), load_ext,
path, 0, path);
rb_ary_push(ruby_dln_librefs, LONG2NUM(handle));
break;
}
rb_provide_feature(path);
result = Qtrue;
}
}
}
POP_TAG();
load_unlock(ftptr);
rb_set_safe_level_force(saved.safe);
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());
}
static VALUE
init_ext_call(VALUE arg)
{
SCOPE_SET(NOEX_PUBLIC);
(*(void (*)(void))arg)();
return Qnil;
}
void
ruby_init_ext(const char *name, void (*init)(void))
{
if (load_lock(name)) {
rb_vm_call_cfunc(rb_vm_top_self(), init_ext_call, (VALUE)init,
0, rb_str_new2(name));
rb_provide(name);
load_unlock(name);
}
}
/*
* 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);
rb_load_path = rb_ary_new();
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();
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);
}
|