Add free function (#243)

This commit is contained in:
qwreey 2024-11-08 15:20:09 +00:00
parent a655a4dad0
commit 3a6f3bd016
No known key found for this signature in database
GPG key ID: D28DB79297A214BD
4 changed files with 35 additions and 17 deletions

27
Cargo.lock generated
View file

@ -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",

View file

@ -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"

View file

@ -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<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)?)?;

View file

@ -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