diff --git a/src/index.ts b/src/index.ts index d39b722..f6b08dc 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,3 +3,4 @@ export { parseRecordBatch } from "./record-batch"; export { parseSchema } from "./schema"; export { parseVector, parseData } from "./vector"; export { parseTable } from "./table"; +export { releaseSchema } from "./release"; diff --git a/src/release.ts b/src/release.ts new file mode 100644 index 0000000..1357694 --- /dev/null +++ b/src/release.ts @@ -0,0 +1,10 @@ +export function releaseSchema( + buffer: ArrayBuffer, + ptr: number, + funcTable: WebAssembly.Table, +): void { + const dataView = new DataView(buffer); + const releasePtr = dataView.getUint32(ptr + 40, true); + const releaseCallback = funcTable.get(releasePtr); + releaseCallback(); +} diff --git a/tests/release.test.ts b/tests/release.test.ts new file mode 100644 index 0000000..2e34877 --- /dev/null +++ b/tests/release.test.ts @@ -0,0 +1,19 @@ +import { describe, it, expect } from "vitest"; +import * as wasm from "rust-arrow-ffi"; +import { arrowTableToFFI, loadIPCTableFromDisk } from "./utils"; +import { releaseSchema } from "../src"; + +wasm.setPanicHook(); + +const WASM_MEMORY = wasm.wasmMemory(); + +describe("test release", (t) => { + it("should release schema", () => { + const TEST_TABLE = loadIPCTableFromDisk("tests/table.arrow"); + const FFI_TABLE = arrowTableToFFI(TEST_TABLE); + + const funcTable = wasm._functionTable(); + const schemaAddr = FFI_TABLE.schemaAddr(0); + releaseSchema(WASM_MEMORY.buffer, schemaAddr, funcTable); + }); +}); diff --git a/tests/rust-arrow-ffi/src/lib.rs b/tests/rust-arrow-ffi/src/lib.rs index b392539..1366b3b 100644 --- a/tests/rust-arrow-ffi/src/lib.rs +++ b/tests/rust-arrow-ffi/src/lib.rs @@ -57,3 +57,16 @@ pub fn set_panic_hook() { // https://github.com/rustwasm/console_error_panic_hook#readme console_error_panic_hook::set_once(); } + +/// Returns a handle to this wasm instance's `WebAssembly.Memory` +#[wasm_bindgen(js_name = wasmMemory)] +pub fn memory() -> JsValue { + wasm_bindgen::memory() +} + +/// Returns a handle to this wasm instance's `WebAssembly.Table` which is the indirect function +/// table used by Rust +#[wasm_bindgen(js_name = _functionTable)] +pub fn function_table() -> JsValue { + wasm_bindgen::function_table() +}