aboutsummaryrefslogtreecommitdiff
path: root/src/MoonP/moonplus.h
diff options
context:
space:
mode:
authorLi Jin <dragon-fly@qq.com>2021-02-17 11:22:07 +0800
committerLi Jin <dragon-fly@qq.com>2021-02-17 11:22:07 +0800
commit7066392d1c974065181d95d93274136dcd625d43 (patch)
treecf51eafc2c52cbc12246a306bca172d799193d30 /src/MoonP/moonplus.h
parent90cd12ad9ef465f3e435e1bd034dcfbe4e19d016 (diff)
downloadyuescript-7066392d1c974065181d95d93274136dcd625d43.tar.gz
yuescript-7066392d1c974065181d95d93274136dcd625d43.tar.bz2
yuescript-7066392d1c974065181d95d93274136dcd625d43.zip
stop reusing variables, rename project.
Diffstat (limited to 'src/MoonP/moonplus.h')
-rw-r--r--src/MoonP/moonplus.h251
1 files changed, 0 insertions, 251 deletions
diff --git a/src/MoonP/moonplus.h b/src/MoonP/moonplus.h
deleted file mode 100644
index ff82e9f..0000000
--- a/src/MoonP/moonplus.h
+++ /dev/null
@@ -1,251 +0,0 @@
1R"moonplus_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 split, get_options, create_moonpath, moon_loader, load_text, moon_call, loadstring, loadfile, dofile, insert_loader, remove_loader, moon_require, find_modulepath
31moonp.dirsep = "/"
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 extension = moonp.options.extension
77 local moonpaths
78 do
79 local _accum_0 = { }
80 local _len_0 = 1
81 local _list_0 = split(package_path, ";")
82 for _index_0 = 1, #_list_0 do
83 local path = _list_0[_index_0]
84 local _continue_0 = false
85 repeat
86 local prefix = path:match("^(.-)%.lua$")
87 if not prefix then
88 _continue_0 = true
89 break
90 end
91 _accum_0[_len_0] = prefix .. "." .. extension
92 _len_0 = _len_0 + 1
93 _continue_0 = true
94 until true
95 if not _continue_0 then
96 break
97 end
98 end
99 moonpaths = _accum_0
100 end
101 return concat(moonpaths, ";")
102end
103find_modulepath = function(name)
104 if not package.moonpath then
105 package.moonpath = create_moonpath(package.path)
106 end
107 local name_path = name:gsub("%.", moonp.dirsep)
108 local file_exist, file_path
109 for path in package.moonpath:gmatch("[^;]+") do
110 file_path = path:gsub("?", name_path)
111 file_exist = moonp.file_exist(file_path)
112 if file_exist then
113 break
114 end
115 end
116 if file_exist then
117 return file_path
118 else
119 return nil
120 end
121end
122load_text = function(name)
123 local file_path = find_modulepath(name)
124 if file_path then
125 return moonp.read_file(file_path), file_path
126 end
127 return nil, nil
128end
129moon_loader = function(name)
130 local text, file_path = load_text(name)
131 if text then
132 local res, err = loadstring(text, file_path)
133 if not res then
134 error(file_path .. ": " .. err)
135 end
136 return res
137 end
138 return nil, "Could not find moonp file"
139end
140moon_call = function(f, ...)
141 local args = {
142 ...
143 }
144 return xpcall((function()
145 return f(unpack(args))
146 end), function(err)
147 return moonp.stp.stacktrace(err, 1)
148 end)
149end
150loadstring = function(...)
151 local options, str, chunk_name, mode, env = get_options(...)
152 chunk_name = chunk_name or "=(moonplus.loadstring)"
153 local code, err = moonp.to_lua(str, options)
154 if not code then
155 return nil, err
156 end
157 if chunk_name then
158 moonp.moon_compiled["@" .. chunk_name] = code
159 end
160 return (lua.loadstring or lua.load)(code, chunk_name, unpack({
161 mode,
162 env
163 }))
164end
165loadfile = function(fname, ...)
166 local text = moonp.read_file(fname)
167 return loadstring(text, tostring(fname), ...)
168end
169dofile = function(...)
170 local f = assert(loadfile(...))
171 return f()
172end
173insert_loader = function(pos)
174 if pos == nil then
175 pos = 2
176 end
177 if not package.moonpath then
178 package.moonpath = create_moonpath(package.path)
179 end
180 local loaders = package.loaders or package.searchers
181 for _index_0 = 1, #loaders do
182 local loader = loaders[_index_0]
183 if loader == moon_loader then
184 return false
185 end
186 end
187 insert(loaders, pos, moon_loader)
188 return true
189end
190remove_loader = function()
191 local loaders = package.loaders or package.searchers
192 for i, loader in ipairs(loaders) do
193 if loader == moon_loader then
194 remove(loaders, i)
195 return true
196 end
197 end
198 return false
199end
200moon_require = function(name)
201 insert_loader()
202 local success, res = xpcall((function()
203 return require(name)
204 end), function(err)
205 local msg = moonp.stp.stacktrace(err, 1)
206 print(msg)
207 return msg
208 end)
209 if success then
210 return res
211 else
212 return nil
213 end
214end
215setmetatable(moonp, {
216 __index = function(self, key)
217 if not (key == "stp") then
218 return nil
219 end
220 local stp = rawget(moonp, "stp")
221 if not stp then
222 do
223 local _with_0 = moonp.load_stacktraceplus()
224 _with_0.dump_locals = false
225 _with_0.simplified = true
226 stp = _with_0
227 end
228 rawset(moonp, "stp", stp)
229 rawset(moonp, "load_stacktraceplus", nil)
230 end
231 return stp
232 end,
233 __call = function(self, name)
234 return self.require(name)
235 end
236})
237for k, v in pairs({
238 insert_loader = insert_loader,
239 remove_loader = remove_loader,
240 loader = moon_loader,
241 dofile = dofile,
242 loadfile = loadfile,
243 loadstring = loadstring,
244 create_moonpath = create_moonpath,
245 find_modulepath = find_modulepath,
246 pcall = moon_call,
247 require = moon_require
248}) do
249 moonp[k] = v
250end
251)moonplus_codes";