From 3a6f3bd016ac480f5a869623802936288190a904 Mon Sep 17 00:00:00 2001 From: qwreey Date: Fri, 8 Nov 2024 15:20:09 +0000 Subject: [PATCH] Add free function (#243) --- Cargo.lock | 27 +++++++++++++-------------- crates/lune-std-ffi/Cargo.toml | 5 +++-- crates/lune-std-ffi/src/lib.rs | 9 ++++++++- types/ffi.luau | 11 +++++++++++ 4 files changed, 35 insertions(+), 17 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 95fb7fc4..4ec79331 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -789,9 +789,9 @@ dependencies = [ [[package]] name = "dlopen2" -version = "0.6.1" +version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6bc2c7ed06fd72a8513ded8d0d2f6fd2655a85d6885c48cae8625d80faf28c03" +checksum = "9e1297103d2bbaea85724fcee6294c2d50b1081f9ad47d0f6f6f61eda65315a6" dependencies = [ "dlopen2_derive", "libc", @@ -1445,9 +1445,9 @@ checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" [[package]] name = "libc" -version = "0.2.160" +version = "0.2.162" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f0b21006cd1874ae9e650973c565615676dc4a274c965bb0a73796dac838ce4f" +checksum = "18d287de67fe55fd7e1581fe933d965a5a9477b38e949cfa9f8574ef01506398" [[package]] name = "libffi" @@ -1633,6 +1633,7 @@ name = "lune-std-ffi" version = "0.1.1" dependencies = [ "dlopen2", + "libc", "libffi", "lune-utils", "mlua", @@ -1937,9 +1938,9 @@ dependencies = [ [[package]] name = "num" -version = "0.3.1" +version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8b7a8e9be5e039e2ff869df49155f1c06bd01ade2117ec783e56ab0932b67a8f" +checksum = "35bd024e8b2ff75562e5f34e7f4905839deb4b22955ef5e73d2fea1b9813cb23" dependencies = [ "num-bigint", "num-complex", @@ -1951,20 +1952,19 @@ dependencies = [ [[package]] name = "num-bigint" -version = "0.3.3" +version = "0.4.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5f6f7833f2cbf2360a6cfd58cd41a53aa7a90bd4c202f5b1c7dd2ed73c57b2c3" +checksum = "a5e44f723f1133c9deac646763579fdb3ac745e418f2a7af9cd0c431da1f20b9" dependencies = [ - "autocfg", "num-integer", "num-traits", ] [[package]] name = "num-complex" -version = "0.3.1" +version = "0.4.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "747d632c0c558b87dbabbe6a82f3b4ae03720d0646ac5b7b4dae89394be5f2c5" +checksum = "73f88a1307638156682bada9d7604135552957b7818057dcef22705b4d509495" dependencies = [ "num-traits", ] @@ -1997,11 +1997,10 @@ dependencies = [ [[package]] name = "num-rational" -version = "0.3.2" +version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "12ac428b1cb17fce6f731001d307d351ec70a6d202fc2e60f7d4c5e42d8f4f07" +checksum = "f83d14da390562dca69fc84082e73e548e1ad308d24accdedd2720017cb37824" dependencies = [ - "autocfg", "num-bigint", "num-integer", "num-traits", diff --git a/crates/lune-std-ffi/Cargo.toml b/crates/lune-std-ffi/Cargo.toml index 0420f1dc..78eebda6 100644 --- a/crates/lune-std-ffi/Cargo.toml +++ b/crates/lune-std-ffi/Cargo.toml @@ -15,8 +15,9 @@ workspace = true [dependencies] mlua = { version = "0.9.9", features = ["luau"] } mlua-sys = { version = "0.6.2", features = ["luau"] } -num = "0.3.1" -dlopen2 = "0.6" +num = "0.4.3" +dlopen2 = "0.7.0" +libc = "0.2.162" libffi = "3.2.0" diff --git a/crates/lune-std-ffi/src/lib.rs b/crates/lune-std-ffi/src/lib.rs index 35a2450f..62b2572d 100644 --- a/crates/lune-std-ffi/src/lib.rs +++ b/crates/lune-std-ffi/src/lib.rs @@ -1,5 +1,8 @@ #![allow(clippy::cargo_common_metadata)] +use std::ffi::c_void; + +use libc::free; use lune_utils::TableBuilder; use mlua::prelude::*; @@ -9,7 +12,7 @@ mod ffi; use crate::{ c::{export_c, export_fixed_types}, - data::{create_nullref, BoxData, LibData}, + data::{create_nullref, BoxData, GetFfiData, LibData}, }; /** @@ -25,6 +28,10 @@ pub fn module(lua: &Lua) -> LuaResult { .with_function("box", |_lua, size: usize| Ok(BoxData::new(size)))? .with_function("open", |_lua, name: String| LibData::new(name))? .with_function("isInteger", |_lua, num: LuaValue| Ok(num.is_integer()))? + .with_function("free", |_lua, data: LuaAnyUserData| { + unsafe { free(data.get_ffi_data()?.get_inner_pointer().cast::()) }; + Ok(()) + })? .with_values(export_fixed_types(lua)?)? .with_value("c", export_c(lua)?)?; diff --git a/types/ffi.luau b/types/ffi.luau index b6ec423e..bfc465e0 100644 --- a/types/ffi.luau +++ b/types/ffi.luau @@ -1129,4 +1129,15 @@ function ffi.isInteger(val: T): boolean return nil :: any end +--[=[ + @within FFI + + Free referenced memory. + + @param data Target memory to free +]=] +function ffi.free(data: RefData | BoxData) + return nil :: any +end + return ffi