Get Runtime working...

This commit is contained in:
Isaac Clayton 2022-06-01 13:42:00 +02:00
parent 265be4a2fb
commit 13e0ad7253
4 changed files with 49 additions and 14 deletions

View file

@ -1,9 +1,10 @@
use std::collections::{HashMap, HashSet};
use mlua::{Error, FromLua, Function, Lua, ToLua, UserData, Value};
use mlua::{Error, FromLua, Function, Lua, LuaSerdeExt, ToLua, UserData, Value};
pub mod runtime;
pub use runtime::*;
use serde::{de::DeserializeOwned, Deserialize, Serialize};
impl Runtime for Lua {
type Module = String;
@ -14,7 +15,7 @@ impl Runtime for Lua {
return Some(lua);
}
fn interface(&self) -> Interface {
fn handles(&self) -> Handles {
let mut globals = HashSet::new();
for pair in self.globals().pairs::<String, Value>() {
if let Ok((k, _)) = pair {
@ -24,4 +25,16 @@ impl Runtime for Lua {
globals
}
fn val<T: DeserializeOwned>(&self, name: String) -> Option<T> {
let val: Value = self.globals().get(name).ok()?;
Some(self.from_value(val).ok()?)
}
fn call<T: Serialize + DeserializeOwned>(&self, name: String, arg: T) -> Option<T> {
let fun: Function = self.globals().get(name).ok()?;
let arg: Value = self.to_value(&arg).ok()?;
let result = fun.call(arg).ok()?;
Some(self.from_value(result).ok()?)
}
}