Skip to content

Commit

Permalink
Add free function (lune-org#243)
Browse files Browse the repository at this point in the history
  • Loading branch information
qwreey committed Nov 8, 2024
1 parent a655a4d commit 3a6f3bd
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 17 deletions.
27 changes: 13 additions & 14 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions crates/lune-std-ffi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
9 changes: 8 additions & 1 deletion crates/lune-std-ffi/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#![allow(clippy::cargo_common_metadata)]

use std::ffi::c_void;

use libc::free;
use lune_utils::TableBuilder;
use mlua::prelude::*;

Expand All @@ -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},
};

/**
Expand All @@ -25,6 +28,10 @@ pub fn module(lua: &Lua) -> LuaResult<LuaTable> {
.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::<c_void>()) };
Ok(())
})?
.with_values(export_fixed_types(lua)?)?
.with_value("c", export_c(lua)?)?;

Expand Down
11 changes: 11 additions & 0 deletions types/ffi.luau
Original file line number Diff line number Diff line change
Expand Up @@ -1129,4 +1129,15 @@ function ffi.isInteger<T>(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

0 comments on commit 3a6f3bd

Please sign in to comment.