aboutsummaryrefslogtreecommitdiff
path: root/ext/wasm/api
diff options
context:
space:
mode:
authorstephan <stephan@noemail.net>2022-12-25 14:04:06 +0000
committerstephan <stephan@noemail.net>2022-12-25 14:04:06 +0000
commit7015aa9f4957c3131c8bc814c81c4c5d2dea8f89 (patch)
tree45f0703be21222001bd129f5ba98100c1e860782 /ext/wasm/api
parent485229e14774ef12d31a815b05aee047739086dd (diff)
downloadsqlite-7015aa9f4957c3131c8bc814c81c4c5d2dea8f89.tar.gz
sqlite-7015aa9f4957c3131c8bc814c81c4c5d2dea8f89.zip
Add missing sqlite3_context_db_handle() JS binding. Reimplement sqlite3_set_authorizer() and sqlite3_set_auxdata() JS bindings to take advantage of [7f9ace1b11a67]. Teach FuncPtrAdapter to emit a console.warn() message if it is invoked after the library is bootstrapped, the goal being to inform users that it's an internal API and should not be invoked from client-side code.
FossilOrigin-Name: 8e3d4f6294037396e388ec21abb18bf0201a6bec6ff004730cc5d11b705a6d2b
Diffstat (limited to 'ext/wasm/api')
-rw-r--r--ext/wasm/api/EXPORTED_FUNCTIONS.sqlite3-api1
-rw-r--r--ext/wasm/api/sqlite3-api-glue.js114
-rw-r--r--ext/wasm/api/sqlite3-api-prologue.js1
-rw-r--r--ext/wasm/api/sqlite3-wasm.c12
4 files changed, 63 insertions, 65 deletions
diff --git a/ext/wasm/api/EXPORTED_FUNCTIONS.sqlite3-api b/ext/wasm/api/EXPORTED_FUNCTIONS.sqlite3-api
index 75e5ea3da..47a7e4a06 100644
--- a/ext/wasm/api/EXPORTED_FUNCTIONS.sqlite3-api
+++ b/ext/wasm/api/EXPORTED_FUNCTIONS.sqlite3-api
@@ -29,6 +29,7 @@ _sqlite3_column_value
_sqlite3_compileoption_get
_sqlite3_compileoption_used
_sqlite3_complete
+_sqlite3_context_db_handle
_sqlite3_create_collation
_sqlite3_create_collation_v2
_sqlite3_create_function
diff --git a/ext/wasm/api/sqlite3-api-glue.js b/ext/wasm/api/sqlite3-api-glue.js
index 9066b3431..85c549ae3 100644
--- a/ext/wasm/api/sqlite3-api-glue.js
+++ b/ext/wasm/api/sqlite3-api-glue.js
@@ -82,7 +82,7 @@ self.sqlite3ApiBootstrap.initializers.push(function(sqlite3){
"sqlite3*",
new wasm.xWrap.FuncPtrAdapter({
signature: 'i(pi)',
- contextKey: (argv,argIndex)=>'sqlite3@'+argv[0]
+ contextKey: (argv,argIndex)=>argv[0/* sqlite3* */]
}),
"*"
]],
@@ -103,6 +103,8 @@ self.sqlite3ApiBootstrap.initializers.push(function(sqlite3){
["sqlite3_compileoption_get", "string", "int"],
["sqlite3_compileoption_used", "int", "string"],
["sqlite3_complete", "int", "string:flexible"],
+ ["sqlite3_context_db_handle", "sqlite3*", "sqlite3_context*"],
+
/* sqlite3_create_function(), sqlite3_create_function_v2(), and
sqlite3_create_window_function() use hand-written bindings to
simplify handling of their function-type arguments. */
@@ -154,16 +156,14 @@ self.sqlite3ApiBootstrap.initializers.push(function(sqlite3){
for those, depending on how their SQL argument is provided. */
/* sqlite3_randomness() uses a hand-written wrapper to extend
the range of supported argument types. */
- [
- "sqlite3_progress_handler", undefined, [
- "sqlite3*", "int", new wasm.xWrap.FuncPtrAdapter({
- name: 'xProgressHandler',
- signature: 'i(p)',
- bindScope: 'context',
- contextKey: (argv,argIndex)=>'sqlite3@'+argv[0]
- }), "*"
- ]
- ],
+ ["sqlite3_progress_handler", undefined, [
+ "sqlite3*", "int", new wasm.xWrap.FuncPtrAdapter({
+ name: 'xProgressHandler',
+ signature: 'i(p)',
+ bindScope: 'context',
+ contextKey: (argv,argIndex)=>argv[0/* sqlite3* */]
+ }), "*"
+ ]],
["sqlite3_realloc", "*","*","int"],
["sqlite3_reset", "int", "sqlite3_stmt*"],
["sqlite3_result_blob", undefined, "sqlite3_context*", "*", "int", "*"],
@@ -179,7 +179,34 @@ self.sqlite3ApiBootstrap.initializers.push(function(sqlite3){
["sqlite3_result_subtype", undefined, "sqlite3_value*", "int"],
["sqlite3_result_text", undefined, "sqlite3_context*", "string", "int", "*"],
["sqlite3_result_zeroblob", undefined, "sqlite3_context*", "int"],
- ["sqlite3_set_auxdata", undefined, "sqlite3_context*", "int", "*", "*"/* => v(*) */],
+ ["sqlite3_set_authorizer", "int", [
+ "sqlite3*",
+ new wasm.xWrap.FuncPtrAdapter({
+ name: "sqlite3_set_authorizer::xAuth",
+ signature: "i(pi"+"ssss)",
+ contextKey: (argv, argIndex)=>argv[0/*(sqlite3*)*/],
+ callProxy: (callback)=>{
+ return (pV, iCode, s0, s1, s2, s3)=>{
+ try{
+ s0 = s0 && wasm.cstrToJs(s0); s1 = s1 && wasm.cstrToJs(s1);
+ s2 = s2 && wasm.cstrToJs(s2); s3 = s3 && wasm.cstrToJs(s3);
+ return callback(pV, iCode, s0, s1, s2, s3) || 0;
+ }catch(e){
+ return e.resultCode || capi.SQLITE_ERROR;
+ }
+ }
+ }
+ }),
+ "*"/*pUserData*/
+ ]],
+ ["sqlite3_set_auxdata", undefined, [
+ "sqlite3_context*", "int", "*",
+ new wasm.xWrap.FuncPtrAdapter({
+ name: 'xDestroyAuxData',
+ signature: 'v(*)',
+ contextKey: (argv, argIndex)=>argv[0/* sqlite3_context* */]
+ })
+ ]],
["sqlite3_shutdown", undefined],
["sqlite3_sourceid", "string"],
["sqlite3_sql", "string", "sqlite3_stmt*"],
@@ -196,12 +223,15 @@ self.sqlite3ApiBootstrap.initializers.push(function(sqlite3){
"sqlite3*", "string", "string", "string",
"**", "**", "*", "*", "*"],
["sqlite3_total_changes", "int", "sqlite3*"],
- ["sqlite3_trace_v2", "int", "sqlite3*", "int",
- new wasm.xWrap.FuncPtrAdapter({
- name: 'sqlite3_trace_v2::callback',
- signature: 'i(ippp)',
- contextKey: (argv,argIndex)=>'sqlite3@'+argv[0]
- }), "*"],
+ ["sqlite3_trace_v2", "int", [
+ "sqlite3*", "int",
+ new wasm.xWrap.FuncPtrAdapter({
+ name: 'sqlite3_trace_v2::callback',
+ signature: 'i(ippp)',
+ contextKey: (argv,argIndex)=>argv[0/* sqlite3* */]
+ }),
+ "*"
+ ]],
["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
@@ -267,8 +297,6 @@ self.sqlite3ApiBootstrap.initializers.push(function(sqlite3){
["sqlite3_result_int64", undefined, "*", "i64"],
["sqlite3_result_zeroblob64", "int", "*", "i64"],
["sqlite3_serialize","*", "sqlite3*", "string", "*", "int"],
- /* sqlite3_set_authorizer() requires a hand-written binding for
- string conversions, so is defined elsewhere. */
["sqlite3_set_last_insert_rowid", undefined, ["sqlite3*", "i64"]],
["sqlite3_status64", "int", "int", "*", "*", "int"],
["sqlite3_total_changes64", "i64", ["sqlite3*"]],
@@ -500,8 +528,8 @@ self.sqlite3ApiBootstrap.initializers.push(function(sqlite3){
{/* Bindings for sqlite3_create_collation[_v2]() */
// contextKey() impl for wasm.xWrap.FuncPtrAdapter
const contextKey = (argv,argIndex)=>{
- return 'argv['+argIndex+']:sqlite3@'+argv[0]+
- ':'+wasm.cstrToJs(argv[1]).toLowerCase()
+ return 'argv['+argIndex+']:'+argv[0/* sqlite3* */]+
+ ':'+wasm.cstrToJs(argv[1/* collation name */]).toLowerCase()
};
const __sqlite3CreateCollationV2 = wasm.xWrap(
'sqlite3_create_collation_v2', 'int', [
@@ -581,11 +609,10 @@ self.sqlite3ApiBootstrap.initializers.push(function(sqlite3){
`(char**)` arguments to arrays of strings... */
let aNames;
const cbwrap = function(pVoid, nCols, pColVals, pColNames){
- let rc = capi.SQLITE_ERROR;
try {
const aVals = wasm.cArgvToJs(nCols, pColVals);
if(!aNames) aNames = wasm.cArgvToJs(nCols, pColNames);
- rc = callback(aVals, aNames) | 0;
+ return callback(aVals, aNames) | 0;
}catch(e){
/* If we set the db error state here, the higher-level
exec() call replaces it with its own, so we have no way
@@ -593,11 +620,8 @@ self.sqlite3ApiBootstrap.initializers.push(function(sqlite3){
must not propagate exceptions through the C API. Though
we make an effort to report OOM here, sqlite3_exec()
translates that into SQLITE_ABORT as well. */
- rc = (e instanceof sqlite3.WasmAllocError)
- ? capi.SQLITE_NOMEM
- : (e.resultCode || capi.SQLITE_ERROR);
+ return e.resultCode || capi.SQLITE_ERROR;
}
- return rc;
};
let rc;
try{
@@ -617,7 +641,7 @@ self.sqlite3ApiBootstrap.initializers.push(function(sqlite3){
*/
const contextKey = function(argv,argIndex){
return (
- 'sqlite3@'+argv[0]
+ argv[0/* sqlite3* */]
+':'+argIndex
+':'+wasm.cstrToJs(argv[1]).toLowerCase()
)
@@ -941,37 +965,6 @@ self.sqlite3ApiBootstrap.initializers.push(function(sqlite3){
}/*sqlite3_bind_text/blob()*/
- {/* sqlite3_set_authorizer() */
- const __ssa = wasm.xWrap("sqlite3_set_authorizer", 'int', [
- "sqlite3*",
- new wasm.xWrap.FuncPtrAdapter({
- name: "sqlite3_set_authorizer::xAuth",
- signature: "i(pi"+"ssss)",
- contextKey: (argIndex, argv)=>argv[0/*(sqlite3*)*/]
- }),
- "*"
- ]);
- capi.sqlite3_set_authorizer = function(pDb, xAuth, pUserData){
- if(3!==arguments.length) return __dbArgcMismatch(pDb, 'sqlite3_set_authorizer', 3);
- if(xAuth instanceof Function){
- const xProxy = xAuth;
- /* Create a proxy which will receive the C-strings from WASM
- and convert them to JS strings for the client-supplied
- function. */
- xAuth = function(pV, iCode, s0, s1, s2, s3){
- try{
- s0 = s0 && wasm.cstrToJs(s0); s1 = s1 && wasm.cstrToJs(s1);
- s2 = s2 && wasm.cstrToJs(s2); s3 = s3 && wasm.cstrToJs(s3);
- return xProxy(pV, iCode, s0, s1, s2, s3) || 0;
- }catch(e){
- return util.sqlite3_wasm_db_error(pDb, e);
- }
- };
- }
- return __ssa(pDb, xAuth, pUserData);
- };
- }/* sqlite3_set_authorizer() */
-
{/* sqlite3_config() */
/**
Wraps a small subset of the C API's sqlite3_config() options.
@@ -1190,4 +1183,5 @@ self.sqlite3ApiBootstrap.initializers.push(function(sqlite3){
}
}/*pKvvfs*/
+ wasm.xWrap.FuncPtrAdapter.warnOnUse = true;
});
diff --git a/ext/wasm/api/sqlite3-api-prologue.js b/ext/wasm/api/sqlite3-api-prologue.js
index e207fc194..7804c0458 100644
--- a/ext/wasm/api/sqlite3-api-prologue.js
+++ b/ext/wasm/api/sqlite3-api-prologue.js
@@ -405,6 +405,7 @@ self.sqlite3ApiBootstrap = function sqlite3ApiBootstrap(
}else{
super("Allocation failed.");
}
+ this.resultCode = capi.SQLITE_NOMEM;
this.name = 'WasmAllocError';
}
};
diff --git a/ext/wasm/api/sqlite3-wasm.c b/ext/wasm/api/sqlite3-wasm.c
index f2bf5fef1..5c9b53318 100644
--- a/ext/wasm/api/sqlite3-wasm.c
+++ b/ext/wasm/api/sqlite3-wasm.c
@@ -323,11 +323,13 @@ SQLITE_WASM_KEEP int sqlite3_wasm_pstack_quota(void){
*/
SQLITE_WASM_KEEP
int sqlite3_wasm_db_error(sqlite3*db, int err_code, const char *zMsg){
- if( 0!=zMsg ){
- const int nMsg = sqlite3Strlen30(zMsg);
- sqlite3ErrorWithMsg(db, err_code, "%.*s", nMsg, zMsg);
- }else{
- sqlite3ErrorWithMsg(db, err_code, NULL);
+ if( db!=0 ){
+ if( 0!=zMsg ){
+ const int nMsg = sqlite3Strlen30(zMsg);
+ sqlite3ErrorWithMsg(db, err_code, "%.*s", nMsg, zMsg);
+ }else{
+ sqlite3ErrorWithMsg(db, err_code, NULL);
+ }
}
return err_code;
}