aboutsummaryrefslogtreecommitdiff
path: root/src/MoonP/moonplus.h
blob: 7059286def641d608df6bb6e4179e1c288d54a6e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
R"moonscript_codes(
--[[
Copyright (C) 2020 by Leaf Corcoran, modified by Li Jin

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.]]

local moonp = require("moonp")
local concat, insert, remove = table.concat, table.insert, table.remove
local unpack = unpack or table.unpack
local lua = {
	loadstring = loadstring,
	load = load
}
local dirsep, split, get_options, create_moonpath, moon_loader, load_text, moon_call, loadstring, loadfile, dofile, insert_loader, remove_loader, moon_require, find_modulepath
dirsep = "/"
moonp.moon_compiled = { }
moonp.file_exist = function(fname)
	local file = io.open(fname)
	if file then
		file:close()
		return true
	else
		return false
	end
end
moonp.read_file = function(fname)
	local file, err = io.open(fname)
	if not file then
		return nil, err
	end
	local text = assert(file:read("*a"))
	file:close()
	return text
end
split = function(str, delim)
	if str == "" then
		return { }
	end
	str = str .. delim
	local _accum_0 = { }
	local _len_0 = 1
	for m in str:gmatch("(.-)" .. delim) do
		_accum_0[_len_0] = m
		_len_0 = _len_0 + 1
	end
	return _accum_0
end
get_options = function(...)
	local count = select("#", ...)
	local opts = select(count, ...)
	if type(opts) == "table" then
		return opts, unpack({
			...
		}, nil, count - 1)
	else
		return { }, ...
	end
end
create_moonpath = function(package_path)
	local moonpaths
	do
		local _accum_0 = { }
		local _len_0 = 1
		local _list_0 = split(package_path, ";")
		for _index_0 = 1, #_list_0 do
			local path = _list_0[_index_0]
			local _continue_0 = false
			repeat
				local prefix = path:match("^(.-)%.lua$")
				if not prefix then
					_continue_0 = true
					break
				end
				_accum_0[_len_0] = prefix .. ".moon"
				_len_0 = _len_0 + 1
				_continue_0 = true
			until true
			if not _continue_0 then
				break
			end
		end
		moonpaths = _accum_0
	end
	return concat(moonpaths, ";")
end
find_modulepath = function(name)
	if not package.moonpath then
		package.moonpath = create_moonpath(package.path)
	end
	local name_path = name:gsub("%.", dirsep)
	local file_exist, file_path
	for path in package.moonpath:gmatch("[^;]+") do
		file_path = path:gsub("?", name_path)
		file_exist = moonp.file_exist(file_path)
		if file_exist then
			break
		end
	end
	if file_exist then
		return file_path
	else
		return nil
	end
end
load_text = function(name)
	local file_path = find_modulepath(name)
	if file_path then
		return moonp.read_file(file_path), file_path
	end
	return nil, nil
end
moon_loader = function(name)
	local text, file_path = load_text(name)
	if text then
		local res, err = loadstring(text, file_path)
		if not res then
			error(file_path .. ": " .. err)
		end
		return res
	end
	return nil, "Could not find moon file"
end
moon_call = function(f, ...)
	local args = {
		...
	}
	return xpcall((function()
		return f(unpack(args))
	end), function(err)
		return moonp.stp.stacktrace(err, 1)
	end)
end
loadstring = function(...)
	local options, str, chunk_name, mode, env = get_options(...)
	chunk_name = chunk_name or "=(moonscript.loadstring)"
	local code, err = moonp.to_lua(str, options)
	if not code then
		return nil, err
	end
	if chunk_name then
		moonp.moon_compiled["@" .. chunk_name] = code
	end
	return (lua.loadstring or lua.load)(code, chunk_name, unpack({
		mode,
		env
	}))
end
loadfile = function(fname, ...)
	local text = moonp.read_file(fname)
	return loadstring(text, tostring(fname), ...)
end
dofile = function(...)
	local f = assert(loadfile(...))
	return f()
end
insert_loader = function(pos)
	if pos == nil then
		pos = 2
	end
	if not package.moonpath then
		package.moonpath = create_moonpath(package.path)
	end
	local loaders = package.loaders or package.searchers
	for _index_0 = 1, #loaders do
		local loader = loaders[_index_0]
		if loader == moon_loader then
			return false
		end
	end
	insert(loaders, pos, moon_loader)
	return true
end
remove_loader = function()
	local loaders = package.loaders or package.searchers
	for i, loader in ipairs(loaders) do
		if loader == moon_loader then
			remove(loaders, i)
			return true
		end
	end
	return false
end
moon_require = function(name)
	insert_loader()
	local success, res = xpcall((function()
		return require(name)
	end), function(err)
		local msg = moonp.stp.stacktrace(err, 1)
		print(msg)
		return msg
	end)
	if success then
		return res
	else
		return nil
	end
end
setmetatable(moonp, {
	__index = function(self, key)
		if not (key == "stp") then
			return nil
		end
		local stp = rawget(moonp, "stp")
		if not stp then
			do
				local _with_0 = moonp.load_stacktraceplus()
				_with_0.dump_locals = false
				_with_0.simplified = true
				stp = _with_0
			end
			rawset(moonp, "stp", stp)
			rawset(moonp, "load_stacktraceplus", nil)
		end
		return stp
	end,
	__call = function(self, name)
		return self.require(name)
	end
})
for k, v in pairs({
	insert_loader = insert_loader,
	remove_loader = remove_loader,
	loader = moon_loader,
	dofile = dofile,
	loadfile = loadfile,
	loadstring = loadstring,
	create_moonpath = create_moonpath,
	find_modulepath = find_modulepath,
	pcall = moon_call,
	require = moon_require
}) do
	moonp[k] = v
end
)moonscript_codes";