From 80117fed0fb93eb6049571f8b8abaf4f121e8e0b Mon Sep 17 00:00:00 2001 From: Kyle Barron Date: Tue, 30 Jan 2024 20:50:10 -0500 Subject: [PATCH 1/2] Add release callback --- src/index.ts | 1 + src/release.ts | 10 ++++++++++ tests/release.test.ts | 19 +++++++++++++++++++ tests/rust-arrow-ffi/src/lib.rs | 13 +++++++++++++ 4 files changed, 43 insertions(+) create mode 100644 src/release.ts create mode 100644 tests/release.test.ts 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..25bf682 --- /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() +} From 4e1625af48f0c6c496fa11a294135914bba13284 Mon Sep 17 00:00:00 2001 From: Kyle Barron Date: Tue, 30 Jan 2024 20:58:01 -0500 Subject: [PATCH 2/2] fmt --- src/release.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/release.ts b/src/release.ts index 25bf682..1357694 100644 --- a/src/release.ts +++ b/src/release.ts @@ -1,7 +1,7 @@ export function releaseSchema( buffer: ArrayBuffer, ptr: number, - funcTable: WebAssembly.Table + funcTable: WebAssembly.Table, ): void { const dataView = new DataView(buffer); const releasePtr = dataView.getUint32(ptr + 40, true);