source: EcnlProtoTool/trunk/mrbgems/mruby-require/mrblib/require.rb@ 331

Last change on this file since 331 was 331, checked in by coas-nagasima, 6 years ago

prototoolに関連するプロジェクトをnewlibからmuslを使うよう変更・更新
ntshellをnewlibの下位の実装から、muslのsyscallの実装に変更・更新
以下のOSSをアップデート
・mruby-1.3.0
・musl-1.1.18
・onigmo-6.1.3
・tcc-0.9.27
以下のOSSを追加
・openssl-1.1.0e
・curl-7.57.0
・zlib-1.2.11
以下のmrbgemsを追加
・iij/mruby-digest
・iij/mruby-env
・iij/mruby-errno
・iij/mruby-iijson
・iij/mruby-ipaddr
・iij/mruby-mock
・iij/mruby-require
・iij/mruby-tls-openssl

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-ruby
File size: 2.2 KB
Line 
1class LoadError < ScriptError; end
2
3begin
4 eval "1", nil
5 def _require_eval_load(*args)
6 self.eval(*args)
7 end
8rescue ArgumentError
9 def _require_eval_load(*args)
10 self.eval(args[0])
11 end
12end
13
14module 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
84end
85
86
87$LOAD_PATH ||= []
88$LOAD_PATH << '.'
89
90if Object.const_defined?(:ENV)
91 $LOAD_PATH.unshift(*ENV['MRBLIB'].split(':')) unless ENV['MRBLIB'].nil?
92end
93
94$LOAD_PATH.uniq!
95
96$" ||= []
97$__mruby_loading_files__ ||= []
Note: See TracBrowser for help on using the repository browser.