diff --git a/crates/lune-std-net/src/lib.rs b/crates/lune-std-net/src/lib.rs index 3f42889..47db8f7 100644 --- a/crates/lune-std-net/src/lib.rs +++ b/crates/lune-std-net/src/lib.rs @@ -65,9 +65,9 @@ async fn net_request(lua: &Lua, config: RequestConfig) -> LuaResult { res.await?.into_lua_table(lua) } -async fn net_socket(lua: &Lua, url: String) -> LuaResult { +async fn net_socket(lua: &Lua, url: String) -> LuaResult { let (ws, _) = tokio_tungstenite::connect_async(url).await.into_lua_err()?; - NetWebSocket::new(ws).into_lua_table(lua) + NetWebSocket::new(ws).into_lua(lua) } async fn net_serve<'lua>( diff --git a/crates/lune-std-net/src/server/service.rs b/crates/lune-std-net/src/server/service.rs index 7bc7e53..2787ca6 100644 --- a/crates/lune-std-net/src/server/service.rs +++ b/crates/lune-std-net/src/server/service.rs @@ -40,13 +40,13 @@ impl Service> for Svc { lua.spawn_local(async move { let sock = sock.await.unwrap(); let lua_sock = NetWebSocket::new(sock); - let lua_tab = lua_sock.into_lua_table(&lua_inner).unwrap(); + let lua_val = lua_sock.into_lua(&lua_inner).unwrap(); let handler_websocket: LuaFunction = keys.websocket_handler(&lua_inner).unwrap().unwrap(); lua_inner - .push_thread_back(handler_websocket, lua_tab) + .push_thread_back(handler_websocket, lua_val) .unwrap(); }); diff --git a/crates/lune-std-net/src/websocket.rs b/crates/lune-std-net/src/websocket.rs index ae2208a..0225451 100644 --- a/crates/lune-std-net/src/websocket.rs +++ b/crates/lune-std-net/src/websocket.rs @@ -23,29 +23,6 @@ use hyper_tungstenite::{ WebSocketStream, }; -use lune_utils::TableBuilder; - -// Wrapper implementation for compatibility and changing colon syntax to dot syntax -const WEB_SOCKET_IMPL_LUA: &str = r#" -return freeze(setmetatable({ - close = function(...) - return websocket:close(...) - end, - send = function(...) - return websocket:send(...) - end, - next = function(...) - return websocket:next(...) - end, -}, { - __index = function(self, key) - if key == "closeCode" then - return websocket.closeCode - end - end, -})) -"#; - #[derive(Debug)] pub struct NetWebSocket { close_code_exists: Arc, @@ -125,25 +102,6 @@ where let mut ws = self.write_stream.lock().await; ws.close().await.into_lua_err() } - - pub fn into_lua_table(self, lua: &Lua) -> LuaResult { - let setmetatable = lua.globals().get::<_, LuaFunction>("setmetatable")?; - let table_freeze = lua - .globals() - .get::<_, LuaTable>("table")? - .get::<_, LuaFunction>("freeze")?; - - let env = TableBuilder::new(lua)? - .with_value("websocket", self.clone())? - .with_value("setmetatable", setmetatable)? - .with_value("freeze", table_freeze)? - .build_readonly()?; - - lua.load(WEB_SOCKET_IMPL_LUA) - .set_name("websocket") - .set_environment(env) - .eval() - } } impl LuaUserData for NetWebSocket