From 9c8b5b537676f38e2e79793650aabf2f1395224d Mon Sep 17 00:00:00 2001 From: Carlo Piovesan Date: Wed, 30 Oct 2024 17:17:17 +0100 Subject: [PATCH] getTableNames to pass strings via malloc --- lib/CMakeLists.txt | 1 + lib/src/webdb_api.cc | 7 +++++++ packages/duckdb-wasm/src/bindings/bindings_base.ts | 7 ++++++- 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index 72d76dfb8..8cfc7a59e 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -316,6 +316,7 @@ if(EMSCRIPTEN) _duckdb_web_get_feature_flags, \ _duckdb_web_get_global_file_info, \ _duckdb_web_get_tablenames, \ + _duckdb_web_get_tablenames_buffer, \ _duckdb_web_get_version, \ _duckdb_web_insert_arrow_from_ipc_stream, \ _duckdb_web_insert_csv_from_path, \ diff --git a/lib/src/webdb_api.cc b/lib/src/webdb_api.cc index 674137430..1ccb4c431 100644 --- a/lib/src/webdb_api.cc +++ b/lib/src/webdb_api.cc @@ -248,6 +248,13 @@ void duckdb_web_get_tablenames(WASMResponse* packed, ConnectionHdl connHdl, cons auto r = c->GetTableNames(query); WASMResponseBuffer::Get().Store(*packed, std::move(r)); } +/// Get table names +void duckdb_web_get_tablenames_buffer(WASMResponse* packed, ConnectionHdl connHdl, const uint8_t* buffer, size_t buffer_length) { + auto c = reinterpret_cast(connHdl); + std::string_view query(reinterpret_cast(buffer), buffer_length); + auto r = c->GetTableNames(query); + WASMResponseBuffer::Get().Store(*packed, std::move(r)); +} /// Insert arrow from an ipc stream void duckdb_web_insert_arrow_from_ipc_stream(WASMResponse* packed, ConnectionHdl connHdl, const uint8_t* buffer, size_t buffer_length, const char* options) { diff --git a/packages/duckdb-wasm/src/bindings/bindings_base.ts b/packages/duckdb-wasm/src/bindings/bindings_base.ts index 04d85c4d5..70d5475cf 100644 --- a/packages/duckdb-wasm/src/bindings/bindings_base.ts +++ b/packages/duckdb-wasm/src/bindings/bindings_base.ts @@ -233,7 +233,12 @@ export abstract class DuckDBBindingsBase implements DuckDBBindings { } /** Get table names */ public getTableNames(conn: number, text: string): string[] { - const [s, d, n] = callSRet(this.mod, 'duckdb_web_get_tablenames', ['number', 'string'], [conn, text]); + const BUF = TEXT_ENCODER.encode(text); + const bufferPtr = this.mod._malloc(BUF.length); + const bufferOfs = this.mod.HEAPU8.subarray(bufferPtr, bufferPtr + BUF.length); + bufferOfs.set(BUF); + const [s, d, n] = callSRet(this.mod, 'duckdb_web_get_tablenames_buffer', ['number', 'number', 'number'], [conn, bufferPtr, BUF.length]); + this.mod._free(bufferPtr); if (s !== StatusCode.SUCCESS) { throw new Error(readString(this.mod, d, n)); }