~ubuntu-branches/ubuntu/quantal/ruby1.9.1/quantal

« back to all changes in this revision

Viewing changes to load.c

  • Committer: Bazaar Package Importer
  • Author(s): Lucas Nussbaum
  • Date: 2011-09-24 19:16:17 UTC
  • mfrom: (1.1.8 upstream) (13.1.7 experimental)
  • Revision ID: james.westby@ubuntu.com-20110924191617-o1qz4rcmqjot8zuy
Tags: 1.9.3~rc1-1
* New upstream release: 1.9.3 RC1.
  + Includes load.c fixes. Closes: #639959.
* Upload to unstable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
 
5
5
#include "ruby/ruby.h"
6
6
#include "ruby/util.h"
 
7
#include "internal.h"
7
8
#include "dln.h"
8
9
#include "eval_intern.h"
9
10
 
10
11
VALUE ruby_dln_librefs;
11
12
 
12
 
#define IS_RBEXT(e) (strcmp(e, ".rb") == 0)
13
 
#define IS_SOEXT(e) (strcmp(e, ".so") == 0 || strcmp(e, ".o") == 0)
 
13
#define IS_RBEXT(e) (strcmp((e), ".rb") == 0)
 
14
#define IS_SOEXT(e) (strcmp((e), ".so") == 0 || strcmp((e), ".o") == 0)
14
15
#ifdef DLEXT2
15
 
#define IS_DLEXT(e) (strcmp(e, DLEXT) == 0 || strcmp(e, DLEXT2) == 0)
 
16
#define IS_DLEXT(e) (strcmp((e), DLEXT) == 0 || strcmp((e), DLEXT2) == 0)
16
17
#else
17
 
#define IS_DLEXT(e) (strcmp(e, DLEXT) == 0)
 
18
#define IS_DLEXT(e) (strcmp((e), DLEXT) == 0)
18
19
#endif
19
20
 
20
21
 
73
74
                    int type, VALUE load_path)
74
75
{
75
76
    long i;
 
77
    long plen;
 
78
    const char *e;
76
79
 
 
80
    if(vlen < len) return 0;
 
81
    if (!strncmp(name+(vlen-len),feature,len)){
 
82
        plen = vlen - len - 1;
 
83
    } else {
 
84
        for (e = name + vlen; name != e && *e != '.' && *e != '/'; --e);
 
85
        if (*e!='.' ||
 
86
            e-name < len ||
 
87
            strncmp(e-len,feature,len) )
 
88
            return 0;
 
89
        plen = e - name - len - 1;
 
90
    }
77
91
    for (i = 0; i < RARRAY_LEN(load_path); ++i) {
78
92
        VALUE p = RARRAY_PTR(load_path)[i];
79
93
        const char *s = StringValuePtr(p);
80
94
        long n = RSTRING_LEN(p);
81
95
 
82
 
        if (vlen < n + len + 1) continue;
 
96
        if (n != plen ) continue;
83
97
        if (n && (strncmp(name, s, n) || name[n] != '/')) continue;
84
 
        if (strncmp(name + n + 1, feature, len)) continue;
85
 
        if (name[n+len+1] && name[n+len+1] != '.') continue;
86
98
        switch (type) {
87
99
          case 's':
88
100
            if (IS_DLEXT(&name[n+len+1])) return p;
242
254
static void
243
255
rb_provide_feature(VALUE feature)
244
256
{
 
257
    if (OBJ_FROZEN(get_loaded_features())) {
 
258
        rb_raise(rb_eRuntimeError,
 
259
                 "$LOADED_FEATURES is frozen; cannot append feature");
 
260
    }
245
261
    rb_ary_push(get_loaded_features(), feature);
246
262
}
247
263
 
289
305
        th->mild_compile_error++;
290
306
        node = (NODE *)rb_load_file(RSTRING_PTR(fname));
291
307
        loaded = TRUE;
292
 
        iseq = rb_iseq_new_top(node, rb_str_new2("<top (required)>"), fname, fname, Qfalse);
 
308
        iseq = rb_iseq_new_top(node, rb_str_new2("<top (required)>"), fname, rb_realpath_internal(Qnil, fname, 1), Qfalse);
293
309
        th->mild_compile_error--;
294
310
        rb_iseq_eval(iseq);
295
311
    }
414
430
 
415
431
/*
416
432
 *  call-seq:
417
 
 *     require(string)    -> true or false
418
 
 *
419
 
 *  Ruby tries to load the library named _string_, returning
420
 
 *  +true+ if successful. If the filename does not resolve to
421
 
 *  an absolute path, it will be searched for in the directories listed
422
 
 *  in <code>$:</code>. If the file has the extension ``.rb'', it is
423
 
 *  loaded as a source file; if the extension is ``.so'', ``.o'', or
424
 
 *  ``.dll'', or whatever the default shared library extension is on
425
 
 *  the current platform, Ruby loads the shared library as a Ruby
426
 
 *  extension. Otherwise, Ruby tries adding ``.rb'', ``.so'', and so on
427
 
 *  to the name. The name of the loaded feature is added to the array in
428
 
 *  <code>$"</code>. A feature will not be loaded if its name already
429
 
 *  appears in <code>$"</code>. The file name is converted to an absolute
430
 
 *  path, so ``<code>require 'a'; require './a'</code>'' will not load
431
 
 *  <code>a.rb</code> twice.
432
 
 *
433
 
 *     require "my-library.rb"
434
 
 *     require "db-driver"
 
433
 *     require(name)    -> true or false
 
434
 *
 
435
 *  Loads the given +name+, returning +true+ if successful and +false+ if the
 
436
 *  feature is already loaded.
 
437
 *
 
438
 *  If the filename does not resolve to an absolute path, it will be searched
 
439
 *  for in the directories listed in <code>$LOAD_PATH</code> (<code>$:</code>).
 
440
 *
 
441
 *  If the filename has the extension ".rb", it is loaded as a source file; if
 
442
 *  the extension is ".so", ".o", or ".dll", or the default shared library
 
443
 *  extension on the current platform, Ruby loads the shared library as a
 
444
 *  Ruby extension.  Otherwise, Ruby tries adding ".rb", ".so", and so on
 
445
 *  to the name until found.  If the file named cannot be found, a LoadError
 
446
 *  will be raised.
 
447
 *
 
448
 *  For Ruby extensions the filename given may use any shared library
 
449
 *  extension.  For example, on Linux the socket extension is "socket.so" and
 
450
 *  <code>require 'socket.dll'</code> will load the socket extension.
 
451
 *
 
452
 *  The absolute path of the loaded file is added to
 
453
 *  <code>$LOADED_FEATURES</code> (<code>$"</code>).  A file will not be
 
454
 *  loaded again if its path already appears in <code>$"</code>.  For example,
 
455
 *  <code>require 'a'; require './a'</code> will not load <code>a.rb</code>
 
456
 *  again.
 
457
 *
 
458
 *    require "my-library.rb"
 
459
 *    require "db-driver"
435
460
 */
436
461
 
437
462
VALUE
440
465
    return rb_require_safe(fname, rb_safe_level());
441
466
}
442
467
 
 
468
/*
 
469
 * call-seq:
 
470
 *   require_relative(string) -> true or false
 
471
 *
 
472
 * Ruby tries to load the library named _string_ relative to the requiring
 
473
 * file's path.  If the file's path cannot be determined a LoadError is raised.
 
474
 * If a file is loaded +true+ is returned and false otherwise.
 
475
 */
443
476
VALUE
444
477
rb_f_require_relative(VALUE obj, VALUE fname)
445
478
{
446
 
    VALUE rb_current_realfilepath(void);
447
479
    VALUE base = rb_current_realfilepath();
448
480
    if (NIL_P(base)) {
449
481
        rb_raise(rb_eLoadError, "cannot infer basepath");
542
574
static void
543
575
load_failed(VALUE fname)
544
576
{
545
 
    VALUE mesg = rb_str_buf_new_cstr("no such file to load -- ");
 
577
    VALUE mesg = rb_str_buf_new_cstr("cannot load such file -- ");
546
578
    rb_str_append(mesg, fname); /* should be ASCII compatible */
547
579
    rb_exc_raise(rb_exc_new3(rb_eLoadError, mesg));
548
580
}
589
621
 
590
622
                  case 's':
591
623
                    handle = (long)rb_vm_call_cfunc(rb_vm_top_self(), load_ext,
592
 
                                                    path, 0, path, path);
 
624
                                                    path, 0, path);
593
625
                    rb_ary_push(ruby_dln_librefs, LONG2NUM(handle));
594
626
                    break;
595
627
                }
631
663
    return Qnil;
632
664
}
633
665
 
634
 
void
 
666
RUBY_FUNC_EXPORTED void
635
667
ruby_init_ext(const char *name, void (*init)(void))
636
668
{
637
669
    if (load_lock(name)) {
638
670
        rb_vm_call_cfunc(rb_vm_top_self(), init_ext_call, (VALUE)init,
639
 
                         0, rb_str_new2(name), Qnil);
 
671
                         0, rb_str_new2(name));
640
672
        rb_provide(name);
641
673
        load_unlock(name, 1);
642
674
    }
699
731
static VALUE
700
732
rb_f_autoload(VALUE obj, VALUE sym, VALUE file)
701
733
{
702
 
    VALUE klass = rb_vm_cbase();
 
734
    VALUE klass = rb_class_real(rb_vm_cbase());
703
735
    if (NIL_P(klass)) {
704
736
        rb_raise(rb_eTypeError, "Can not set autoload on singleton class");
705
737
    }
732
764
Init_load()
733
765
{
734
766
#undef rb_intern
735
 
#define rb_intern(str) rb_intern2(str, strlen(str))
 
767
#define rb_intern(str) rb_intern2((str), strlen(str))
736
768
    rb_vm_t *vm = GET_VM();
737
769
    static const char var_load_path[] = "$:";
738
770
    ID id_load_path = rb_intern2(var_load_path, sizeof(var_load_path)-1);