2023-02-10 11:14:28 +00:00
|
|
|
use mlua::prelude::*;
|
|
|
|
|
2023-01-21 03:01:02 +00:00
|
|
|
mod fs;
|
|
|
|
mod net;
|
|
|
|
mod process;
|
2023-01-24 07:05:54 +00:00
|
|
|
mod require;
|
2023-03-21 18:13:41 +00:00
|
|
|
#[cfg(feature = "roblox")]
|
|
|
|
mod roblox;
|
2023-03-21 15:48:28 +00:00
|
|
|
mod serde;
|
2023-02-06 05:13:12 +00:00
|
|
|
mod stdio;
|
2023-01-21 18:33:33 +00:00
|
|
|
mod task;
|
2023-02-10 11:14:28 +00:00
|
|
|
mod top_level;
|
2023-01-21 03:01:02 +00:00
|
|
|
|
2023-03-21 15:48:28 +00:00
|
|
|
const BUILTINS_AS_GLOBALS: &[&str] = &["fs", "net", "process", "stdio", "task"];
|
|
|
|
|
2023-03-21 14:06:28 +00:00
|
|
|
pub fn create(lua: &'static Lua, args: Vec<String>) -> LuaResult<()> {
|
|
|
|
// Create all builtins
|
|
|
|
let builtins = vec![
|
|
|
|
("fs", fs::create(lua)?),
|
|
|
|
("net", net::create(lua)?),
|
|
|
|
("process", process::create(lua, args)?),
|
2023-03-21 18:13:41 +00:00
|
|
|
#[cfg(feature = "roblox")]
|
|
|
|
("roblox", roblox::create(lua)?),
|
2023-03-21 15:48:28 +00:00
|
|
|
("serde", self::serde::create(lua)?),
|
2023-03-21 14:06:28 +00:00
|
|
|
("stdio", stdio::create(lua)?),
|
|
|
|
("task", task::create(lua)?),
|
|
|
|
];
|
|
|
|
|
2023-03-21 15:48:28 +00:00
|
|
|
// TODO: Remove this when we have proper LSP support for custom
|
|
|
|
// require types and no longer need to have builtins as globals
|
2023-03-21 14:06:28 +00:00
|
|
|
let lua_globals = lua.globals();
|
2023-03-21 15:48:28 +00:00
|
|
|
for name in BUILTINS_AS_GLOBALS {
|
|
|
|
let builtin = builtins.iter().find(|(gname, _)| gname == name).unwrap();
|
|
|
|
lua_globals.set(*name, builtin.1.clone())?;
|
2023-02-10 11:14:28 +00:00
|
|
|
}
|
2023-02-06 03:25:36 +00:00
|
|
|
|
2023-03-21 14:06:28 +00:00
|
|
|
// Create our importer (require) with builtins
|
|
|
|
let require_fn = require::create(lua, builtins)?;
|
|
|
|
|
|
|
|
// Create all top-level globals
|
|
|
|
let globals = vec![
|
|
|
|
("require", require_fn),
|
|
|
|
("print", lua.create_function(top_level::top_level_print)?),
|
|
|
|
("warn", lua.create_function(top_level::top_level_warn)?),
|
|
|
|
("error", lua.create_function(top_level::top_level_error)?),
|
|
|
|
(
|
|
|
|
"printinfo",
|
|
|
|
lua.create_function(top_level::top_level_printinfo)?,
|
|
|
|
),
|
|
|
|
];
|
|
|
|
|
|
|
|
// Set top-level globals and seal them
|
|
|
|
for (name, global) in globals {
|
|
|
|
lua_globals.set(name, global)?;
|
2023-02-10 11:14:28 +00:00
|
|
|
}
|
2023-03-21 14:06:28 +00:00
|
|
|
lua_globals.set_readonly(true);
|
2023-02-06 03:25:36 +00:00
|
|
|
|
2023-03-21 14:06:28 +00:00
|
|
|
Ok(())
|
2023-02-06 03:25:36 +00:00
|
|
|
}
|