aboutsummaryrefslogtreecommitdiff
path: root/src/MoonP/moonplus.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/MoonP/moonplus.h')
-rw-r--r--src/MoonP/moonplus.h250
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 @@
1R"moonscript_codes(
2--[[
3Copyright (C) 2020 by Leaf Corcoran, modified by Li Jin
4
5Permission is hereby granted, free of charge, to any person obtaining a copy
6of this software and associated documentation files (the "Software"), to deal
7in the Software without restriction, including without limitation the rights
8to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9copies of the Software, and to permit persons to whom the Software is
10furnished to do so, subject to the following conditions:
11
12The above copyright notice and this permission notice shall be included in
13all copies or substantial portions of the Software.
14
15THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21THE SOFTWARE.]]
22
23local moonp = require("moonp")
24local concat, insert, remove = table.concat, table.insert, table.remove
25local unpack = unpack or table.unpack
26local lua = {
27 loadstring = loadstring,
28 load = load
29}
30local dirsep, split, get_options, create_moonpath, moon_loader, load_text, moon_call, loadstring, loadfile, dofile, insert_loader, remove_loader, moon_require, find_modulepath
31dirsep = "/"
32moonp.moon_compiled = { }
33moonp.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
41end
42moonp.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
50end
51split = 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
63end
64get_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
74end
75create_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, ";")
101end
102find_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
120end
121load_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
127end
128moon_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"
138end
139moon_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)
148end
149loadstring = 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 }))
163end
164loadfile = function(fname, ...)
165 local text = moonp.read_file(fname)
166 return loadstring(text, tostring(fname), ...)
167end
168dofile = function(...)
169 local f = assert(loadfile(...))
170 return f()
171end
172insert_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
188end
189remove_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
198end
199moon_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
213end
214setmetatable(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})
236for 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
249end
250)moonscript_codes";