diff options
author | stephan <stephan@noemail.net> | 2022-12-12 18:42:39 +0000 |
---|---|---|
committer | stephan <stephan@noemail.net> | 2022-12-12 18:42:39 +0000 |
commit | 33ce0b3ff34530a47e928fbce053c76db83ab3d2 (patch) | |
tree | 19dea951dedcbcf4be52fb58388870c595f69d10 /ext/wasm/api/sqlite3-api-glue.js | |
parent | d8e07c780c55ccd6bfb6f005cafbfdae560bf7e7 (diff) | |
download | sqlite-33ce0b3ff34530a47e928fbce053c76db83ab3d2.tar.gz sqlite-33ce0b3ff34530a47e928fbce053c76db83ab3d2.zip |
Revert part of [9386d6f63468] because the new automatic function pointer binding cannot properly track per-context function mappings when the context is more complex than a single context-type pointer. e.g. it is fine for sqlite3_trace_v2() but it breaks down with sqlite3_create_collation() because that one needs to use the collation name as part of the context key and we cannot sensibly do so with the current code.
FossilOrigin-Name: 6cd21b79075367227b57bccf829cc7d4ccc7d7fbcfaed226b4c8e942ddae4eb6
Diffstat (limited to 'ext/wasm/api/sqlite3-api-glue.js')
-rw-r--r-- | ext/wasm/api/sqlite3-api-glue.js | 52 |
1 files changed, 42 insertions, 10 deletions
diff --git a/ext/wasm/api/sqlite3-api-glue.js b/ext/wasm/api/sqlite3-api-glue.js index da17fcb98..91b42cd93 100644 --- a/ext/wasm/api/sqlite3-api-glue.js +++ b/ext/wasm/api/sqlite3-api-glue.js @@ -141,7 +141,8 @@ self.sqlite3ApiBootstrap.initializers.push(function(sqlite3){ "sqlite3*", "int", new wasm.xWrap.FuncPtrAdapter({ name: 'xProgressHandler', signature: 'i(p)', - bindMode: 'singleton' + bindScope: 'context', + contextKey: (argIndex,argv)=>'sqlite3@'+argv[0] }), "*" ] ], @@ -178,7 +179,12 @@ self.sqlite3ApiBootstrap.initializers.push(function(sqlite3){ "sqlite3*", "string", "string", "string", "**", "**", "*", "*", "*"], ["sqlite3_total_changes", "int", "sqlite3*"], - ["sqlite3_trace_v2", "int", "sqlite3*", "int", "*", "*"], + ["sqlite3_trace_v2", "int", "sqlite3*", "int", + new wasm.xWrap.FuncPtrAdapter({ + name: 'sqlite3_trace_v2::callback', + signature: 'i(ippp)', + contextKey: (argIndex, argv)=>'sqlite3@'+argv[0] + }), "*"], ["sqlite3_txn_state", "int", ["sqlite3*","string"]], /* Note that sqlite3_uri_...() have very specific requirements for their first C-string arguments, so we cannot perform any value @@ -454,27 +460,44 @@ self.sqlite3ApiBootstrap.initializers.push(function(sqlite3){ if(1){/* Bindings for sqlite3_create_collation() */ + const __collationContextKey = (argIndex,argv)=>{ + return 'argv['+argIndex+']:sqlite3@'+argv[0]+ + ':'+((/*THIS IS WRONG. We can't sensibly use a converted-to-C-string + address here and don't have access to the JS string (IF ANY) + which the user passed in.*/ + ''+argv[1] + ).toLowerCase()); + }; const __ccv2 = wasm.xWrap( 'sqlite3_create_collation_v2', 'int', - 'sqlite3*','string','int','*', + 'sqlite3*','string','int','*','*','*' + /* int(*xCompare)(void*,int,const void*,int,const void*) */ + /* void(*xDestroy(void*) */ + ); + if(0){ + // Problem: we cannot, due to xWrap() arg-passing limitations, + // currently easily/efficiently get a per-collation distinct + // key for purposes of creating distinct FuncPtrAdapter contexts. new wasm.xWrap.FuncPtrAdapter({ /* int(*xCompare)(void*,int,const void*,int,const void*) */ name: 'xCompare', signature: 'i(pipip)', - bindMode: 'static' + bindScope: 'context', + contextKey: __collationContextKey }), new wasm.xWrap.FuncPtrAdapter({ /* void(*xDestroy(void*) */ name: 'xDestroy', signature: 'v(p)', - bindMode: 'static' + bindScope: 'context', + contextKey: __collationContextKey }) - ); + } /** Works exactly like C's sqlite3_create_collation_v2() except that: - 1) It permits its two function arguments to be JS functions, + 1) It accepts JS functions for its function-pointer arguments, for which it will install WASM-bound proxies. The bindings are "permanent," in that they will stay in the WASM environment until it shuts down unless the client somehow finds and removes @@ -494,10 +517,19 @@ self.sqlite3ApiBootstrap.initializers.push(function(sqlite3){ pDb, capi.SQLITE_FORMAT, "SQLITE_UTF8 is the only supported encoding." ); } - let rc; + let rc, pfCompare, pfDestroy; try{ - rc = __ccv2(pDb, zName, eTextRep, pArg, xCompare, xDestroy); + if(xCompare instanceof Function){ + pfCompare = wasm.installFunction(xCompare, 'i(pipip)'); + } + if(xDestroy instanceof Function){ + pfDestroy = wasm.installFunction(xDestroy, 'v(p)'); + } + rc = __ccv2(pDb, zName, eTextRep, pArg, + pfCompare || xCompare, pfDestroy || xDestroy); }catch(e){ + if(pfCompare) wasm.uninstallFunction(pfCompare); + if(pfDestroy) wasm.uninstallFunction(pfDestroy); rc = util.sqlite3_wasm_db_error(pDb, e); } return rc; @@ -516,7 +548,7 @@ self.sqlite3ApiBootstrap.initializers.push(function(sqlite3){ ["sqlite3*", "string:flexible", new wasm.xWrap.FuncPtrAdapter({ signature: 'i(pipp)', - bindMode: 'transient' + bindScope: 'transient' }), "*", "**"]); /* Documented in the api object's initializer. */ capi.sqlite3_exec = function f(pDb, sql, callback, pVoid, pErrMsg){ |