aboutsummaryrefslogtreecommitdiff
path: root/src/Moonscript.h
blob: 6e84552bbe9449aa2c309861e3583e65909b7e06 (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
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.]]

import "moonp"
import concat, insert, remove from table
unpack = unpack or table.unpack
lua = :loadstring, :load

local *

dirsep = "/"

moonp.moon_compiled = {}

split = (str, delim) ->
	return {} if str == ""
	str ..= delim
	[m for m in str\gmatch("(.-)"..delim)]

get_options = (...) ->
	count = select "#", ...
	opts = select count, ...
	if type(opts) == "table"
		opts, unpack {...}, nil, count - 1
	else
		{}, ...

-- create moon path package from lua package path
create_moonpath = (package_path) ->
	moonpaths = for path in *split package_path, ";"
		prefix = path\match "^(.-)%.lua$"
		continue unless prefix
		prefix .. ".moon"
	concat moonpaths, ";"

moon_loader = (name) ->
	name_path = name\gsub "%.", dirsep

	local file, file_path
	for path in package.moonpath\gmatch "[^;]+"
		file_path = path\gsub "?", name_path
		file = io.open file_path
		break if file

	if file
		text = file\read "*a"
		file\close!
		res, err = loadstring text, "@#{file_path}"
		if not res
				error file_path .. ": " .. err

		return res

	return nil, "Could not find moon file"


loadstring = (...) ->
	options, str, chunk_name, mode, env = get_options ...
	chunk_name or= "=(moonscript.loadstring)"

	code, err = moonp.to_lua str, options
	unless code
		return nil, err

	moonp.moon_compiled[chunk_name] = code if chunk_name
	-- the unpack prevents us from passing nil
	(lua.loadstring or lua.load) code, chunk_name, unpack { mode, env }

loadfile = (fname, ...) ->
	file, err = io.open fname
	return nil, err unless file
	text = assert file\read "*a"
	file\close!
	loadstring text, "@#{fname}", ...

-- throws errros
dofile = (...) ->
	f = assert loadfile ...
	f!

insert_loader = (pos=2) ->
	if not package.moonpath
		package.moonpath = create_moonpath package.path

	loaders = package.loaders or package.searchers
	for loader in *loaders
		return false if loader == moon_loader

	insert loaders, pos, moon_loader
	true

remove_loader = ->
	loaders = package.loaders or package.searchers

	for i, loader in ipairs loaders
		if loader == moon_loader
			remove loaders, i
			return true

	false

moon_require = (name)->
	insert_loader!
	xpcall (-> require name), (err)->
		msg = moonp.stp.stacktrace err, 2
		print msg

setmetatable moonp, {
	__index: (key)=>
		return nil unless key == "stp"
		stp = rawget moonp,"stp"
		unless stp
			stp = with moonp.load_stacktraceplus!
				.dump_locals = false
				.simplified = true
			rawset moonp,"stp",stp
			rawset moonp,"load_stacktraceplus",nil
		stp
	__call: (name)=> @.require name
}

moonp[k] = v for k,v in pairs {
	:insert_loader, :remove_loader, :moon_loader, :dirsep,
	:dofile, :loadfile, :loadstring, :create_moonpath,
	require:moon_require
}
)moonscript_codes";