Skip to content

Commit

Permalink
Move from duckdb_web_tokenize to duckdb_web_tokenize_buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
carlopi committed Oct 30, 2024
1 parent 1954bc8 commit e89d2d7
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@ if(EMSCRIPTEN)
_duckdb_web_query_run_buffer, \
_duckdb_web_reset, \
_duckdb_web_tokenize, \
_duckdb_web_tokenize_buffer, \
_duckdb_web_udf_scalar_create \
]' \
-s EXPORTED_RUNTIME_METHODS='[\"ccall\", \"stackSave\", \"stackAlloc\", \"stackRestore\"]' \
Expand Down
7 changes: 7 additions & 0 deletions lib/src/webdb_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,13 @@ void duckdb_web_tokenize(WASMResponse* packed, const char* query) {
auto tokens = webdb.Tokenize(query);
WASMResponseBuffer::Get().Store(*packed, arrow::Result(std::move(tokens)));
}
/// Tokenize a query
void duckdb_web_tokenize_buffer(WASMResponse* packed, const uint8_t* buffer, size_t buffer_length) {
GET_WEBDB(*packed);
std::string_view query(reinterpret_cast<const char*>(buffer), buffer_length);
auto tokens = webdb.Tokenize(query);
WASMResponseBuffer::Get().Store(*packed, arrow::Result(std::move(tokens)));
}
/// Create scalar UDF queries
void duckdb_web_udf_scalar_create(WASMResponse* packed, ConnectionHdl connHdl, const char* args) {
auto c = reinterpret_cast<WebDB::Connection*>(connHdl);
Expand Down
8 changes: 7 additions & 1 deletion packages/duckdb-wasm/src/bindings/bindings_base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,17 @@ export abstract class DuckDBBindingsBase implements DuckDBBindings {

/** Tokenize a script */
public tokenize(text: string): ScriptTokens {
const [s, d, n] = callSRet(this.mod, 'duckdb_web_tokenize', ['string'], [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_tokenize_buffer', ['number', 'number'], [bufferPtr, BUF.length]);
if (s !== StatusCode.SUCCESS) {
this.mod._free(bufferPtr);
throw new Error(readString(this.mod, d, n));
}
const res = readString(this.mod, d, n);
this.mod._free(bufferPtr);
dropResponseBuffers(this.mod);
return JSON.parse(res) as ScriptTokens;
}
Expand Down

0 comments on commit e89d2d7

Please sign in to comment.