-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathlib.rs
286 lines (232 loc) · 8.51 KB
/
lib.rs
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2025 Fundament Software SPC <https://fundament.software>
use mlua::prelude::*;
const ALICORN: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/alicorn.lua"));
const PRELUDE: &[u8] = include_bytes!("prelude.alc");
const GLSL_PRELUDE: &[u8] = include_bytes!("glsl-prelude.alc");
const SANDBOX: &[u8] = include_bytes!("libs/sandbox.lua");
extern "C-unwind" {
fn luaopen_lpeg(L: *mut mlua::lua_State) -> std::ffi::c_int;
fn luaopen_lfs(L: *mut mlua::lua_State) -> std::ffi::c_int;
}
pub struct Alicorn {
lua: Lua,
module: mlua::Value,
}
struct NamedChunk<'a>(&'a [u8], &'a str);
impl mlua::AsChunk<'static> for NamedChunk<'static> {
fn name(&self) -> Option<String> {
Some(self.1.into())
}
fn source(self) -> std::io::Result<std::borrow::Cow<'static, [u8]>> {
Ok(std::borrow::Cow::Borrowed(self.0))
}
}
impl Alicorn {
pub fn new(lua: Lua, additional_interface: &mlua::Table) -> Result<Self, mlua::Error> {
// Load C libraries we already linked into our rust binary using our build script. This works because we can
// declare the C functions directly and have the linker resolve them during the link step.
let _lpeg: mlua::Value =
lua.load_from_function("lpeg", unsafe { lua.create_c_function(luaopen_lpeg)? })?;
let _: mlua::Value =
lua.load_from_function("lfs", unsafe { lua.create_c_function(luaopen_lfs)? })?;
lua.load(NamedChunk(SANDBOX, "sandbox")).exec()?;
let mut buf: Vec<u8> = r#"
local injected_dep = ...
package = {preload = {}, loaded = {lpeg = lpeg}}
require = function(name) -- require stub for inside sandbox
if not package.loaded[name] then
if not package.preload[name] then
error("Couldn't find package.preload for " .. name)
end
package.loaded[name] = package.preload[name]()
end
return package.loaded[name]
end
"#
.into();
buf.extend_from_slice(ALICORN);
buf.extend_from_slice(r#"
metalanguage = require "metalanguage"
evaluator = require "evaluator"
format = require "format-adapter"
formatter = require "format"
base_env = require "base-env"
terms = require "terms"
exprs = require "alicorn-expressions"
profile = require "profile"
util = require "alicorn-utils"
glsl_print = require "glsl-print"
env = base_env.create()
original_env, env = env:enter_block(terms.block_purity.effectful)
local M = {}
function M.alc_process(code, cur_env)
local ok, expr, inner_env = code:match({
exprs.top_level_block(
metalanguage.accept_handler,
{ exprargs = exprs.ExpressionArgs.new(terms.expression_goal.infer, cur_env), name = name }
),
}, metalanguage.failure_handler, nil)
if not ok then
print(tostring(expr))
error("processing failed (error printed to stdout)")
end
return expr, inner_env
end
function M.alc_include_string(src, name)
local bound_expr, inner_env = M.alc_process(format.read(src, name), env)
env = inner_env
return bound_expr
end
function M.alc_include_file(filename)
local f = io.open(filename)
if not f then
error("Couldn't find " .. filename)
end
local s = format.read(f:read("a"), filename)
f:close()
local bound_expr, inner_env = M.alc_process(s, env)
env = inner_env
return bound_expr
end
function M.alc_evaluate(bound_expr, cur_env)
local ok, type, usages, term = evaluator.infer(bound_expr, cur_env.typechecking_context)
if not ok then
print(tostring(type))
error("inference failed (error printed to stdout)")
end
local gen = require "terms-generators"
local set = gen.declare_set
local unique_id = gen.builtin_table
local ok, err = evaluator.typechecker_state:flow(
type,
cur_env.typechecking_context,
terms.flex_value.program_type(
terms.flex_value.effect_row(terms.unique_id_set(terms.TCState, terms.lua_prog)),
evaluator.typechecker_state:metavariable(cur_env.typechecking_context):as_flex()
),
cur_env.typechecking_context,
terms.constraintcause.primitive("final flow check", formatter.anchor_here())
)
if not ok then
print(err)
error("flow check failed (error printed to stdout)")
end
return pcall(function()
return evaluator.evaluate(term, cur_env.typechecking_context.runtime_context, cur_env.typechecking_context)
end)
end
function M.alc_execute(src, name)
local shadowed, cur_env = env:enter_block(terms.block_purity.effectful)
local bound_expr, cur_env = M.alc_process(format.read(src, name), cur_env)
local cur_env, block_expr, _ = cur_env:exit_block(bound_expr, shadowed)
local ok, result = M.alc_evaluate(block_expr, cur_env)
if not ok then
print(result)
error("evaluation failed (error printed to stdout)")
end
local result_exec = evaluator.execute_program(result)
return result_exec:unwrap_host_tuple_value():unpack()
end
function M.alc_execute_file(filename)
local f = io.open(filename)
if not f then
error("Couldn't find " .. filename)
end
local s = format.read(f:read("a"), filename)
f:close()
return M.alc_execute(s, filename)
end
return M
"#.as_bytes());
lua.load(
r#"
jit.opt.start("maxtrace=10000")
jit.opt.start("maxmcode=4096")
jit.opt.start("recunroll=5")
jit.opt.start("loopunroll=60")
local create_module = sandbox_impl(true)
function load_in_sandbox(bytes, additional_interface)
local r, err = create_module(bytes, "alicorn_lib", additional_interface)
if r == nil then
error(err)
end
return r()
end
"#,
)
.exec()?;
let load_in_sandbox: LuaFunction = lua.load("load_in_sandbox").eval()?;
let module: mlua::Value =
load_in_sandbox.call((lua.create_string(buf)?, additional_interface))?;
let alicorn = Self { lua, module };
let _ = alicorn.include(std::str::from_utf8(PRELUDE)?, "prelude.alc")?;
Ok(alicorn)
}
pub fn load_glsl_prelude(&self) -> Result<(), mlua::Error> {
let _ = self.include(std::str::from_utf8(GLSL_PRELUDE)?, "glsl-prelude.alc")?;
Ok(())
}
pub fn include(
&self,
source: impl AsRef<str>,
name: impl AsRef<str>,
) -> Result<mlua::Value, mlua::Error> {
let alc_include_string: LuaFunction = self
.lua
.load("function(m, ...) return m.alc_include_string(...) end")
.eval()?;
alc_include_string.call((&self.module, source.as_ref(), name.as_ref()))
}
pub fn include_file(
&self,
path: impl AsRef<std::path::Path>,
) -> Result<mlua::Value, mlua::Error> {
let alc_include_file: LuaFunction = self
.lua
.load("function(m, ...) return m.alc_include_file(...) end")
.eval()?;
alc_include_file.call((&self.module, path.as_ref().as_os_str().to_string_lossy()))
}
pub fn execute<R: FromLuaMulti>(
&self,
source: impl AsRef<str>,
name: impl AsRef<str>,
) -> Result<R, mlua::Error> {
let execute_program: LuaFunction = self
.lua
.load("function(m, ...) return m.alc_execute(...) end")
.eval()?;
execute_program.call((&self.module, source.as_ref(), name.as_ref()))
}
pub fn execute_file<R: FromLuaMulti>(
&self,
path: impl AsRef<std::path::Path>,
) -> Result<R, mlua::Error> {
let execute_program: LuaFunction = self
.lua
.load("function(m, ...) return m.alc_execute_path(...) end")
.eval()?;
execute_program.call((&self.module, path.as_ref().as_os_str().to_string_lossy()))
}
}
#[test]
fn test_runtest_file() {
// set the working directory to some random temporary folder to sabotage loading any of the files from this crate, as a crate including this one wouldn't have access to those files
let old = std::env::current_dir().unwrap();
let temp_dir = std::env::temp_dir();
let root = std::path::Path::new(&temp_dir);
std::env::set_current_dir(&root).unwrap();
let lua = Lua::new();
let interface = lua.create_table().unwrap();
let alicorn = Alicorn::new(lua, &interface).unwrap();
// Restore working dir so we can find prelude.alc
std::env::set_current_dir(&old).unwrap();
let result: LuaValue = alicorn
.execute(
"host-tuple-of(tuple-desc-singleton(host-type, host-number))(4)",
format!("{}:{}", file!(), line!() - 1),
)
.unwrap();
assert_eq!(result, LuaValue::Integer(4));
}