[331] | 1 | class LoadError < ScriptError; end
|
---|
| 2 |
|
---|
| 3 | begin
|
---|
| 4 | eval "1", nil
|
---|
| 5 | def _require_eval_load(*args)
|
---|
| 6 | self.eval(*args)
|
---|
| 7 | end
|
---|
| 8 | rescue ArgumentError
|
---|
| 9 | def _require_eval_load(*args)
|
---|
| 10 | self.eval(args[0])
|
---|
| 11 | end
|
---|
| 12 | end
|
---|
| 13 |
|
---|
| 14 | module Kernel
|
---|
| 15 | def load(path)
|
---|
| 16 | raise TypeError unless path.class == String
|
---|
| 17 |
|
---|
| 18 | if File.exist?(path) && File.extname(path) == ".mrb"
|
---|
| 19 | _load_mrb_file path
|
---|
| 20 | elsif File.exist?(path)
|
---|
| 21 | # _load_rb_str File.open(path).read.to_s, path
|
---|
| 22 | _require_eval_load File.open(path).read.to_s, nil, path
|
---|
| 23 | else
|
---|
| 24 | raise LoadError.new "File not found -- #{path}"
|
---|
| 25 | end
|
---|
| 26 |
|
---|
| 27 | true
|
---|
| 28 | end
|
---|
| 29 |
|
---|
| 30 | def require(path)
|
---|
| 31 | raise TypeError unless path.class == String
|
---|
| 32 |
|
---|
| 33 | # require method can load .rb, .mrb or without-ext filename only.
|
---|
| 34 | unless ["", ".rb", ".mrb"].include? File.extname(path)
|
---|
| 35 | raise LoadError.new "cannot load such file -- #{path}"
|
---|
| 36 | end
|
---|
| 37 |
|
---|
| 38 | filenames = []
|
---|
| 39 | if File.extname(path).size == 0
|
---|
| 40 | filenames << "#{path}.rb"
|
---|
| 41 | filenames << "#{path}.mrb"
|
---|
| 42 | else
|
---|
| 43 | filenames << path
|
---|
| 44 | end
|
---|
| 45 |
|
---|
| 46 | dir = nil
|
---|
| 47 | filename = nil
|
---|
| 48 | if ['/', '.'].include? path[0]
|
---|
| 49 | path0 = filenames.find do |fname|
|
---|
| 50 | File.file?(fname) && File.exist?(fname)
|
---|
| 51 | end
|
---|
| 52 | else
|
---|
| 53 | dir = ($LOAD_PATH || []).find do |dir0|
|
---|
| 54 | filename = filenames.find do |fname|
|
---|
| 55 | path0 = File.join dir0, fname
|
---|
| 56 | File.file?(path0) && File.exist?(path0)
|
---|
| 57 | end
|
---|
| 58 | end
|
---|
| 59 | path0 = dir && filename ? File.join(dir, filename) : nil
|
---|
| 60 | end
|
---|
| 61 |
|
---|
| 62 | if path0 && File.exist?(path0) && File.file?(path0)
|
---|
| 63 | __require__ path0
|
---|
| 64 | else
|
---|
| 65 | raise LoadError.new "cannot load such file -- #{path}"
|
---|
| 66 | end
|
---|
| 67 | end
|
---|
| 68 |
|
---|
| 69 | def __require__(realpath)
|
---|
| 70 | raise LoadError.new "File not found -- #{realpath}" unless File.exist? realpath
|
---|
| 71 | $" ||= []
|
---|
| 72 | $__mruby_loading_files__ ||= []
|
---|
| 73 |
|
---|
| 74 | # already required
|
---|
| 75 | return false if ($" + $__mruby_loading_files__).include?(realpath)
|
---|
| 76 |
|
---|
| 77 | $__mruby_loading_files__ << realpath
|
---|
| 78 | load realpath
|
---|
| 79 | $" << realpath
|
---|
| 80 | $__mruby_loading_files__.delete realpath
|
---|
| 81 |
|
---|
| 82 | true
|
---|
| 83 | end
|
---|
| 84 | end
|
---|
| 85 |
|
---|
| 86 |
|
---|
| 87 | $LOAD_PATH ||= []
|
---|
| 88 | $LOAD_PATH << '.'
|
---|
| 89 |
|
---|
| 90 | if Object.const_defined?(:ENV)
|
---|
| 91 | $LOAD_PATH.unshift(*ENV['MRBLIB'].split(':')) unless ENV['MRBLIB'].nil?
|
---|
| 92 | end
|
---|
| 93 |
|
---|
| 94 | $LOAD_PATH.uniq!
|
---|
| 95 |
|
---|
| 96 | $" ||= []
|
---|
| 97 | $__mruby_loading_files__ ||= []
|
---|