Skip to content

Commit

Permalink
Merge pull request #4 from Absolucy/isolation
Browse files Browse the repository at this point in the history
Add isolated lua states
  • Loading branch information
Y0SH1M4S73R authored Oct 27, 2024
2 parents 7a8f014 + 5aa8723 commit b98ebce
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 9 deletions.
5 changes: 3 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ pub(crate) mod wrappers;

pub use state::{
awaken, call_function, clear_execution_limit, clear_ref_userdata, collect_garbage, get_globals,
get_traceback, kill_sleeping_thread, kill_state, kill_yielded_thread, list_threads, load,
new_state, resume, set_execution_limit_millis, set_execution_limit_secs, set_usr,
get_traceback, is_isolated, kill_sleeping_thread, kill_state, kill_yielded_thread,
list_threads, load, new_state, resume, set_execution_limit_millis, set_execution_limit_secs,
set_usr,
};

pub use wrappers::{
Expand Down
15 changes: 10 additions & 5 deletions src/state/library/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ impl LuaModule for GlobalModule {
fn create_items<'lua>(&self, lua: &'lua Lua) -> LuaResult<Vec<(&str, LuaValue<'lua>)>> {
let id = self.0;
let id1 = self.0;
Ok(vec![
("dm", (&DmModule as &dyn LuaModule).into_lua(lua)?),
("list", (&ListModule as &dyn LuaModule).into_lua(lua)?),
let isolate = lua.named_registry_value::<bool>("isolated")?;
let mut items = vec![
(
"loadstring",
Function::wrap(|lua, code: String| {
Expand All @@ -29,7 +28,6 @@ impl LuaModule for GlobalModule {
})
.into_lua(lua)?,
),
("pointer", (&PointerModule as &dyn LuaModule).into_lua(lua)?),
(
"sleep",
unsafe { lua.create_c_function(sleep) }.map(LuaValue::Function)?,
Expand All @@ -40,8 +38,15 @@ impl LuaModule for GlobalModule {
),
("_exec", (&ExecModule as &dyn LuaModule).into_lua(lua)?),
("_state_id", LuaValue::Integer(id)),
])
];
if !isolate {
items.push(("dm", (&DmModule as &dyn LuaModule).into_lua(lua)?));
items.push(("list", (&ListModule as &dyn LuaModule).into_lua(lua)?));
items.push(("pointer", (&PointerModule as &dyn LuaModule).into_lua(lua)?));
}
Ok(items)
}

fn create_metamethods<'lua>(
&self,
_: &'lua Lua,
Expand Down
12 changes: 11 additions & 1 deletion src/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@ thread_local! {

#[map_statics(mut STATES)]
#[byond_fn]
pub fn new_state() -> ByondResult<usize> {
pub fn new_state(isolate: Option<bool>) -> ByondResult<usize> {
let lua: Lua = Lua::new();
lua.set_named_registry_value("isolated", isolate.unwrap_or(false))
.map_err(ByondError::boxed)?;
let new_state_index = states.iter().position(Option::is_none).unwrap_or_else(|| {
states.push(None);
states.len() - 1
Expand Down Expand Up @@ -70,6 +72,14 @@ fn get_state(index: usize) -> ByondResult<Rc<Lua>> {
)))
}

#[byond_fn]
pub fn is_isolated(index: usize) -> ByondResult<bool> {
get_state(index).and_then(|lua| {
lua.named_registry_value("isolated")
.map_err(ByondError::boxed)
})
}

#[byond_fn]
pub fn load(index: usize, code: String, name: Option<String>) -> ByondResult<ByondValue> {
get_state(index).and_then(|lua| run::load(lua.as_ref(), code, name))
Expand Down
8 changes: 7 additions & 1 deletion src/value/conversion/into.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ impl<'lua> IntoLua<'lua> for Value {
.into_printed_external()?
.into_lua(lua);
};
get_or_create_cached_userdata(self, lua)?.into_lua(lua)
// If isolated, never allow any sort of userdata to be exposed.
let isolate = lua.named_registry_value::<bool>("isolated")?;
if isolate {
Ok(LuaValue::Nil)
} else {
get_or_create_cached_userdata(self, lua)?.into_lua(lua)
}
}
}

0 comments on commit b98ebce

Please sign in to comment.