diff options
Diffstat (limited to 'src/MoonP/moonplus.h')
-rw-r--r-- | src/MoonP/moonplus.h | 250 |
1 files changed, 250 insertions, 0 deletions
diff --git a/src/MoonP/moonplus.h b/src/MoonP/moonplus.h new file mode 100644 index 0000000..7059286 --- /dev/null +++ b/src/MoonP/moonplus.h | |||
@@ -0,0 +1,250 @@ | |||
1 | R"moonscript_codes( | ||
2 | --[[ | ||
3 | Copyright (C) 2020 by Leaf Corcoran, modified by Li Jin | ||
4 | |||
5 | Permission is hereby granted, free of charge, to any person obtaining a copy | ||
6 | of this software and associated documentation files (the "Software"), to deal | ||
7 | in the Software without restriction, including without limitation the rights | ||
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
9 | copies of the Software, and to permit persons to whom the Software is | ||
10 | furnished to do so, subject to the following conditions: | ||
11 | |||
12 | The above copyright notice and this permission notice shall be included in | ||
13 | all copies or substantial portions of the Software. | ||
14 | |||
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
21 | THE SOFTWARE.]] | ||
22 | |||
23 | local moonp = require("moonp") | ||
24 | local concat, insert, remove = table.concat, table.insert, table.remove | ||
25 | local unpack = unpack or table.unpack | ||
26 | local lua = { | ||
27 | loadstring = loadstring, | ||
28 | load = load | ||
29 | } | ||
30 | local dirsep, split, get_options, create_moonpath, moon_loader, load_text, moon_call, loadstring, loadfile, dofile, insert_loader, remove_loader, moon_require, find_modulepath | ||
31 | dirsep = "/" | ||
32 | moonp.moon_compiled = { } | ||
33 | moonp.file_exist = function(fname) | ||
34 | local file = io.open(fname) | ||
35 | if file then | ||
36 | file:close() | ||
37 | return true | ||
38 | else | ||
39 | return false | ||
40 | end | ||
41 | end | ||
42 | moonp.read_file = function(fname) | ||
43 | local file, err = io.open(fname) | ||
44 | if not file then | ||
45 | return nil, err | ||
46 | end | ||
47 | local text = assert(file:read("*a")) | ||
48 | file:close() | ||
49 | return text | ||
50 | end | ||
51 | split = function(str, delim) | ||
52 | if str == "" then | ||
53 | return { } | ||
54 | end | ||
55 | str = str .. delim | ||
56 | local _accum_0 = { } | ||
57 | local _len_0 = 1 | ||
58 | for m in str:gmatch("(.-)" .. delim) do | ||
59 | _accum_0[_len_0] = m | ||
60 | _len_0 = _len_0 + 1 | ||
61 | end | ||
62 | return _accum_0 | ||
63 | end | ||
64 | get_options = function(...) | ||
65 | local count = select("#", ...) | ||
66 | local opts = select(count, ...) | ||
67 | if type(opts) == "table" then | ||
68 | return opts, unpack({ | ||
69 | ... | ||
70 | }, nil, count - 1) | ||
71 | else | ||
72 | return { }, ... | ||
73 | end | ||
74 | end | ||
75 | create_moonpath = function(package_path) | ||
76 | local moonpaths | ||
77 | do | ||
78 | local _accum_0 = { } | ||
79 | local _len_0 = 1 | ||
80 | local _list_0 = split(package_path, ";") | ||
81 | for _index_0 = 1, #_list_0 do | ||
82 | local path = _list_0[_index_0] | ||
83 | local _continue_0 = false | ||
84 | repeat | ||
85 | local prefix = path:match("^(.-)%.lua$") | ||
86 | if not prefix then | ||
87 | _continue_0 = true | ||
88 | break | ||
89 | end | ||
90 | _accum_0[_len_0] = prefix .. ".moon" | ||
91 | _len_0 = _len_0 + 1 | ||
92 | _continue_0 = true | ||
93 | until true | ||
94 | if not _continue_0 then | ||
95 | break | ||
96 | end | ||
97 | end | ||
98 | moonpaths = _accum_0 | ||
99 | end | ||
100 | return concat(moonpaths, ";") | ||
101 | end | ||
102 | find_modulepath = function(name) | ||
103 | if not package.moonpath then | ||
104 | package.moonpath = create_moonpath(package.path) | ||
105 | end | ||
106 | local name_path = name:gsub("%.", dirsep) | ||
107 | local file_exist, file_path | ||
108 | for path in package.moonpath:gmatch("[^;]+") do | ||
109 | file_path = path:gsub("?", name_path) | ||
110 | file_exist = moonp.file_exist(file_path) | ||
111 | if file_exist then | ||
112 | break | ||
113 | end | ||
114 | end | ||
115 | if file_exist then | ||
116 | return file_path | ||
117 | else | ||
118 | return nil | ||
119 | end | ||
120 | end | ||
121 | load_text = function(name) | ||
122 | local file_path = find_modulepath(name) | ||
123 | if file_path then | ||
124 | return moonp.read_file(file_path), file_path | ||
125 | end | ||
126 | return nil, nil | ||
127 | end | ||
128 | moon_loader = function(name) | ||
129 | local text, file_path = load_text(name) | ||
130 | if text then | ||
131 | local res, err = loadstring(text, file_path) | ||
132 | if not res then | ||
133 | error(file_path .. ": " .. err) | ||
134 | end | ||
135 | return res | ||
136 | end | ||
137 | return nil, "Could not find moon file" | ||
138 | end | ||
139 | moon_call = function(f, ...) | ||
140 | local args = { | ||
141 | ... | ||
142 | } | ||
143 | return xpcall((function() | ||
144 | return f(unpack(args)) | ||
145 | end), function(err) | ||
146 | return moonp.stp.stacktrace(err, 1) | ||
147 | end) | ||
148 | end | ||
149 | loadstring = function(...) | ||
150 | local options, str, chunk_name, mode, env = get_options(...) | ||
151 | chunk_name = chunk_name or "=(moonscript.loadstring)" | ||
152 | local code, err = moonp.to_lua(str, options) | ||
153 | if not code then | ||
154 | return nil, err | ||
155 | end | ||
156 | if chunk_name then | ||
157 | moonp.moon_compiled["@" .. chunk_name] = code | ||
158 | end | ||
159 | return (lua.loadstring or lua.load)(code, chunk_name, unpack({ | ||
160 | mode, | ||
161 | env | ||
162 | })) | ||
163 | end | ||
164 | loadfile = function(fname, ...) | ||
165 | local text = moonp.read_file(fname) | ||
166 | return loadstring(text, tostring(fname), ...) | ||
167 | end | ||
168 | dofile = function(...) | ||
169 | local f = assert(loadfile(...)) | ||
170 | return f() | ||
171 | end | ||
172 | insert_loader = function(pos) | ||
173 | if pos == nil then | ||
174 | pos = 2 | ||
175 | end | ||
176 | if not package.moonpath then | ||
177 | package.moonpath = create_moonpath(package.path) | ||
178 | end | ||
179 | local loaders = package.loaders or package.searchers | ||
180 | for _index_0 = 1, #loaders do | ||
181 | local loader = loaders[_index_0] | ||
182 | if loader == moon_loader then | ||
183 | return false | ||
184 | end | ||
185 | end | ||
186 | insert(loaders, pos, moon_loader) | ||
187 | return true | ||
188 | end | ||
189 | remove_loader = function() | ||
190 | local loaders = package.loaders or package.searchers | ||
191 | for i, loader in ipairs(loaders) do | ||
192 | if loader == moon_loader then | ||
193 | remove(loaders, i) | ||
194 | return true | ||
195 | end | ||
196 | end | ||
197 | return false | ||
198 | end | ||
199 | moon_require = function(name) | ||
200 | insert_loader() | ||
201 | local success, res = xpcall((function() | ||
202 | return require(name) | ||
203 | end), function(err) | ||
204 | local msg = moonp.stp.stacktrace(err, 1) | ||
205 | print(msg) | ||
206 | return msg | ||
207 | end) | ||
208 | if success then | ||
209 | return res | ||
210 | else | ||
211 | return nil | ||
212 | end | ||
213 | end | ||
214 | setmetatable(moonp, { | ||
215 | __index = function(self, key) | ||
216 | if not (key == "stp") then | ||
217 | return nil | ||
218 | end | ||
219 | local stp = rawget(moonp, "stp") | ||
220 | if not stp then | ||
221 | do | ||
222 | local _with_0 = moonp.load_stacktraceplus() | ||
223 | _with_0.dump_locals = false | ||
224 | _with_0.simplified = true | ||
225 | stp = _with_0 | ||
226 | end | ||
227 | rawset(moonp, "stp", stp) | ||
228 | rawset(moonp, "load_stacktraceplus", nil) | ||
229 | end | ||
230 | return stp | ||
231 | end, | ||
232 | __call = function(self, name) | ||
233 | return self.require(name) | ||
234 | end | ||
235 | }) | ||
236 | for k, v in pairs({ | ||
237 | insert_loader = insert_loader, | ||
238 | remove_loader = remove_loader, | ||
239 | loader = moon_loader, | ||
240 | dofile = dofile, | ||
241 | loadfile = loadfile, | ||
242 | loadstring = loadstring, | ||
243 | create_moonpath = create_moonpath, | ||
244 | find_modulepath = find_modulepath, | ||
245 | pcall = moon_call, | ||
246 | require = moon_require | ||
247 | }) do | ||
248 | moonp[k] = v | ||
249 | end | ||
250 | )moonscript_codes"; | ||