From d25f2fc07ccddef435f69a2dee6c36a4487974b5 Mon Sep 17 00:00:00 2001 From: Alex Orlenko Date: Sun, 1 Sep 2024 22:49:12 +0100 Subject: [PATCH] Take `Table` instead of `impl IntoLua` in `Chunk::set_environment()` --- src/chunk.rs | 24 +++++++++++------------- src/state/raw.rs | 2 +- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/src/chunk.rs b/src/chunk.rs index 0dc781d..8722a06 100644 --- a/src/chunk.rs +++ b/src/chunk.rs @@ -5,11 +5,11 @@ use std::io::Result as IoResult; use std::path::{Path, PathBuf}; use std::string::String as StdString; -use crate::error::{Error, ErrorContext, Result}; +use crate::error::{Error, Result}; use crate::function::Function; use crate::state::{Lua, WeakLua}; use crate::table::Table; -use crate::value::{FromLuaMulti, IntoLua, IntoLuaMulti}; +use crate::value::{FromLuaMulti, IntoLuaMulti}; /// Trait for types [loadable by Lua] and convertible to a [`Chunk`] /// @@ -322,13 +322,8 @@ impl<'a> Chunk<'a> { /// All global variables (including the standard library!) are looked up in `_ENV`, so it may be /// necessary to populate the environment in order for scripts using custom environments to be /// useful. - pub fn set_environment(mut self, env: impl IntoLua) -> Self { - let guard = self.lua.lock(); - let lua = guard.lua(); - self.env = env - .into_lua(lua) - .and_then(|val| lua.unpack(val)) - .context("bad environment value"); + pub fn set_environment(mut self, env: Table) -> Self { + self.env = Ok(Some(env)); self } @@ -451,7 +446,7 @@ impl<'a> Chunk<'a> { let name = Self::convert_name(self.name)?; self.lua .lock() - .load_chunk(Some(&name), self.env?, self.mode, self.source?.as_ref()) + .load_chunk(Some(&name), self.env?.as_ref(), self.mode, self.source?.as_ref()) } /// Compiles the chunk and changes mode to binary. @@ -532,9 +527,12 @@ impl<'a> Chunk<'a> { .unwrap_or(source); let name = Self::convert_name(self.name.clone())?; - self.lua - .lock() - .load_chunk(Some(&name), self.env.clone()?, None, &source) + let env = match &self.env { + Ok(Some(env)) => Some(env), + Ok(None) => None, + Err(err) => return Err(err.clone()), + }; + self.lua.lock().load_chunk(Some(&name), env, None, &source) } fn detect_mode(&self) -> ChunkMode { diff --git a/src/state/raw.rs b/src/state/raw.rs index d82dcf8..bd0a449 100644 --- a/src/state/raw.rs +++ b/src/state/raw.rs @@ -301,7 +301,7 @@ impl RawLua { pub(crate) fn load_chunk( &self, name: Option<&CStr>, - env: Option, + env: Option<&Table>, mode: Option, source: &[u8], ) -> Result {