aboutsummaryrefslogtreecommitdiff
path: root/ext/wasm/api/sqlite3-api-glue.js
diff options
context:
space:
mode:
Diffstat (limited to 'ext/wasm/api/sqlite3-api-glue.js')
-rw-r--r--ext/wasm/api/sqlite3-api-glue.js1104
1 files changed, 773 insertions, 331 deletions
diff --git a/ext/wasm/api/sqlite3-api-glue.js b/ext/wasm/api/sqlite3-api-glue.js
index 7b5fa1181..7db23bacc 100644
--- a/ext/wasm/api/sqlite3-api-glue.js
+++ b/ext/wasm/api/sqlite3-api-glue.js
@@ -24,7 +24,24 @@ self.sqlite3ApiBootstrap.initializers.push(function(sqlite3){
self.WhWasmUtilInstaller(wasm);
delete self.WhWasmUtilInstaller;
- {
+ if(0){
+ /**
+ Please keep this block around as a maintenance reminder
+ that we cannot rely on this type of check.
+
+ This block fails on Safari, per a report at
+ https://sqlite.org/forum/forumpost/e5b20e1feb.
+
+ It turns out that what Safari serves from the indirect function
+ table (e.g. wasm.functionEntry(X)) is anonymous functions which
+ wrap the WASM functions, rather than returning the WASM
+ functions themselves. That means comparison of such functions
+ is useless for determining whether or not we have a specific
+ function from wasm.exports. i.e. if function X is indirection
+ function table entry N then wasm.exports.X is not equal to
+ wasm.functionEntry(N) in Safari, despite being so in the other
+ browsers.
+ */
/**
Find a mapping for SQLITE_WASM_DEALLOC, which the API
guarantees is a WASM pointer to the same underlying function as
@@ -69,6 +86,7 @@ self.sqlite3ApiBootstrap.initializers.push(function(sqlite3){
wasm.bindingSignatures = [
// Please keep these sorted by function name!
["sqlite3_aggregate_context","void*", "sqlite3_context*", "int"],
+ /* sqlite3_auto_extension() has a hand-written binding. */
/* sqlite3_bind_blob() and sqlite3_bind_text() have hand-written
bindings to permit more flexible inputs. */
["sqlite3_bind_double","int", "sqlite3_stmt*", "int", "f64"],
@@ -82,12 +100,14 @@ self.sqlite3ApiBootstrap.initializers.push(function(sqlite3){
"sqlite3*",
new wasm.xWrap.FuncPtrAdapter({
signature: 'i(pi)',
- contextKey: (argIndex,argv)=>'sqlite3@'+argv[0]
+ contextKey: (argv,argIndex)=>argv[0/* sqlite3* */]
}),
"*"
]],
["sqlite3_busy_timeout","int", "sqlite3*", "int"],
- ["sqlite3_close_v2", "int", "sqlite3*"],
+ /* sqlite3_cancel_auto_extension() has a hand-written binding. */
+ /* sqlite3_close_v2() is implemented by hand to perform some
+ extra work. */
["sqlite3_changes", "int", "sqlite3*"],
["sqlite3_clear_bindings","int", "sqlite3_stmt*"],
["sqlite3_collation_needed", "int", "sqlite3*", "*", "*"/*=>v(ppis)*/],
@@ -100,9 +120,20 @@ self.sqlite3ApiBootstrap.initializers.push(function(sqlite3){
["sqlite3_column_text","string", "sqlite3_stmt*", "int"],
["sqlite3_column_type","int", "sqlite3_stmt*", "int"],
["sqlite3_column_value","sqlite3_value*", "sqlite3_stmt*", "int"],
+ ["sqlite3_commit_hook", "void*", [
+ "sqlite3*",
+ new wasm.xWrap.FuncPtrAdapter({
+ name: 'sqlite3_commit_hook',
+ signature: 'i(p)',
+ contextKey: (argv)=>argv[0/* 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. */
@@ -125,9 +156,32 @@ self.sqlite3ApiBootstrap.initializers.push(function(sqlite3){
["sqlite3_errmsg", "string", "sqlite3*"],
["sqlite3_error_offset", "int", "sqlite3*"],
["sqlite3_errstr", "string", "int"],
- /*["sqlite3_exec", "int", "sqlite3*", "string", "*", "*", "**"
- Handled seperately to perform translation of the callback
- into a WASM-usable one. ],*/
+ ["sqlite3_exec", "int", [
+ "sqlite3*", "string:flexible",
+ new wasm.xWrap.FuncPtrAdapter({
+ signature: 'i(pipp)',
+ bindScope: 'transient',
+ callProxy: (callback)=>{
+ let aNames;
+ return (pVoid, nCols, pColVals, pColNames)=>{
+ try {
+ const aVals = wasm.cArgvToJs(nCols, pColVals);
+ if(!aNames) aNames = wasm.cArgvToJs(nCols, pColNames);
+ 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
+ of reporting the exception message except the console. We
+ 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. */
+ return e.resultCode || capi.SQLITE_ERROR;
+ }
+ }
+ }
+ }),
+ "*", "**"
+ ]],
["sqlite3_expanded_sql", "string", "sqlite3_stmt*"],
["sqlite3_extended_errcode", "int", "sqlite3*"],
["sqlite3_extended_result_codes", "int", "sqlite3*", "int"],
@@ -154,18 +208,17 @@ 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: (argIndex,argv)=>'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_reset_auto_extension() has a hand-written binding. */
["sqlite3_result_blob", undefined, "sqlite3_context*", "*", "int", "*"],
["sqlite3_result_double", undefined, "sqlite3_context*", "f64"],
["sqlite3_result_error", undefined, "sqlite3_context*", "string", "int"],
@@ -179,7 +232,43 @@ 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_rollback_hook", "void*", [
+ "sqlite3*",
+ new wasm.xWrap.FuncPtrAdapter({
+ name: 'sqlite3_rollback_hook',
+ signature: 'v(p)',
+ contextKey: (argv)=>argv[0/* sqlite3* */]
+ }),
+ '*'
+ ]],
+ ["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 +285,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: (argIndex, argv)=>'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
@@ -234,7 +326,7 @@ self.sqlite3ApiBootstrap.initializers.push(function(sqlite3){
optional features into account. */
wasm.bindingSignatures.push(["sqlite3_normalized_sql", "string", "sqlite3_stmt*"]);
}
-
+
/**
Functions which require BigInt (int64) support are separated from
the others because we need to conditionally bind them or apply
@@ -263,15 +355,47 @@ self.sqlite3ApiBootstrap.initializers.push(function(sqlite3){
["sqlite3_malloc64", "*","i64"],
["sqlite3_msize", "i64", "*"],
["sqlite3_overload_function", "int", ["sqlite3*","string","int"]],
+ ["sqlite3_preupdate_blobwrite", "int", "sqlite3*"],
+ ["sqlite3_preupdate_count", "int", "sqlite3*"],
+ ["sqlite3_preupdate_depth", "int", "sqlite3*"],
+ ["sqlite3_preupdate_hook", "*", [
+ "sqlite3*",
+ new wasm.xWrap.FuncPtrAdapter({
+ name: 'sqlite3_preupdate_hook',
+ signature: "v(ppippjj)",
+ contextKey: (argv)=>argv[0/* sqlite3* */],
+ callProxy: (callback)=>{
+ return (p,db,op,zDb,zTbl,iKey1,iKey2)=>{
+ callback(p, db, op, wasm.cstrToJs(zDb), wasm.cstrToJs(zTbl),
+ iKey1, iKey2);
+ };
+ }
+ }),
+ "*"
+ ]],
+ ["sqlite3_preupdate_new", "int", ["sqlite3*", "int", "**"]],
+ ["sqlite3_preupdate_old", "int", ["sqlite3*", "int", "**"]],
["sqlite3_realloc64", "*","*", "i64"],
["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*"]],
+ ["sqlite3_update_hook", "*", [
+ "sqlite3*",
+ new wasm.xWrap.FuncPtrAdapter({
+ name: 'sqlite3_update_hook',
+ signature: "v(iippj)",
+ contextKey: (argv)=>argv[0/* sqlite3* */],
+ callProxy: (callback)=>{
+ return (p,op,z0,z1,rowid)=>{
+ callback(p, op, wasm.cstrToJs(z0), wasm.cstrToJs(z1), rowid);
+ };
+ }
+ }),
+ "*"
+ ]],
["sqlite3_uri_int64", "i64", ["sqlite3_filename", "string", "i64"]],
["sqlite3_value_int64","i64", "sqlite3_value*"],
["sqlite3_vtab_collation","string","sqlite3_index_info*","int"],
@@ -286,6 +410,184 @@ self.sqlite3ApiBootstrap.initializers.push(function(sqlite3){
["sqlite3_vtab_rhs_value","int", "sqlite3_index_info*", "int", "**"]
];
+ // Add session/changeset APIs...
+ if(wasm.bigIntEnabled && !!wasm.exports.sqlite3changegroup_add){
+ /* ACHTUNG: 2022-12-23: the session/changeset API bindings are
+ COMPLETELY UNTESTED. */
+ /**
+ FuncPtrAdapter options for session-related callbacks with the
+ native signature "i(ps)". This proxy converts the 2nd argument
+ from a C string to a JS string before passing the arguments on
+ to the client-provided JS callback.
+ */
+ const __ipsProxy = {
+ signature: 'i(ps)',
+ callProxy:(callback)=>{
+ return (p,s)=>{
+ try{return callback(p, wasm.cstrToJs(s)) | 0}
+ catch(e){return e.resultCode || capi.SQLITE_ERROR}
+ }
+ }
+ };
+
+ wasm.bindingSignatures.int64.push(...[
+ ['sqlite3changegroup_add', 'int', ['sqlite3_changegroup*', 'int', 'void*']],
+ ['sqlite3changegroup_add_strm', 'int', [
+ 'sqlite3_changegroup*',
+ new wasm.xWrap.FuncPtrAdapter({
+ name: 'xInput', signature: 'i(ppp)', bindScope: 'transient'
+ }),
+ 'void*'
+ ]],
+ ['sqlite3changegroup_delete', undefined, ['sqlite3_changegroup*']],
+ ['sqlite3changegroup_new', 'int', ['**']],
+ ['sqlite3changegroup_output', 'int', ['sqlite3_changegroup*', 'int*', '**']],
+ ['sqlite3changegroup_output_strm', 'int', [
+ 'sqlite3_changegroup*',
+ new wasm.xWrap.FuncPtrAdapter({
+ name: 'xOutput', signature: 'i(ppi)', bindScope: 'transient'
+ }),
+ 'void*'
+ ]],
+ ['sqlite3changeset_apply', 'int', [
+ 'sqlite3*', 'int', 'void*',
+ new wasm.xWrap.FuncPtrAdapter({
+ name: 'xFilter', bindScope: 'transient', ...__ipsProxy
+ }),
+ new wasm.xWrap.FuncPtrAdapter({
+ name: 'xConflict', signature: 'i(pip)', bindScope: 'transient'
+ }),
+ 'void*'
+ ]],
+ ['sqlite3changeset_apply_strm', 'int', [
+ 'sqlite3*',
+ new wasm.xWrap.FuncPtrAdapter({
+ name: 'xInput', signature: 'i(ppp)', bindScope: 'transient'
+ }),
+ 'void*',
+ new wasm.xWrap.FuncPtrAdapter({
+ name: 'xFilter', bindScope: 'transient', ...__ipsProxy
+ }),
+ new wasm.xWrap.FuncPtrAdapter({
+ name: 'xConflict', signature: 'i(pip)', bindScope: 'transient'
+ }),
+ 'void*'
+ ]],
+ ['sqlite3changeset_apply_v2', 'int', [
+ 'sqlite3*', 'int', 'void*',
+ new wasm.xWrap.FuncPtrAdapter({
+ name: 'xFilter', bindScope: 'transient', ...__ipsProxy
+ }),
+ new wasm.xWrap.FuncPtrAdapter({
+ name: 'xConflict', signature: 'i(pip)', bindScope: 'transient'
+ }),
+ 'void*', '**', 'int*', 'int'
+
+ ]],
+ ['sqlite3changeset_apply_v2_strm', 'int', [
+ 'sqlite3*',
+ new wasm.xWrap.FuncPtrAdapter({
+ name: 'xInput', signature: 'i(ppp)', bindScope: 'transient'
+ }),
+ 'void*',
+ new wasm.xWrap.FuncPtrAdapter({
+ name: 'xFilter', bindScope: 'transient', ...__ipsProxy
+ }),
+ new wasm.xWrap.FuncPtrAdapter({
+ name: 'xConflict', signature: 'i(pip)', bindScope: 'transient'
+ }),
+ 'void*', '**', 'int*', 'int'
+ ]],
+ ['sqlite3changeset_concat', 'int', ['int','void*', 'int', 'void*', 'int*', '**']],
+ ['sqlite3changeset_concat_strm', 'int', [
+ new wasm.xWrap.FuncPtrAdapter({
+ name: 'xInputA', signature: 'i(ppp)', bindScope: 'transient'
+ }),
+ 'void*',
+ new wasm.xWrap.FuncPtrAdapter({
+ name: 'xInputB', signature: 'i(ppp)', bindScope: 'transient'
+ }),
+ 'void*',
+ new wasm.xWrap.FuncPtrAdapter({
+ name: 'xOutput', signature: 'i(ppi)', bindScope: 'transient'
+ }),
+ 'void*'
+ ]],
+ ['sqlite3changeset_conflict', 'int', ['sqlite3_changeset_iter*', 'int', '**']],
+ ['sqlite3changeset_finalize', 'int', ['sqlite3_changeset_iter*']],
+ ['sqlite3changeset_fk_conflicts', 'int', ['sqlite3_changeset_iter*', 'int*']],
+ ['sqlite3changeset_invert', 'int', ['int', 'void*', 'int*', '**']],
+ ['sqlite3changeset_invert_strm', 'int', [
+ new wasm.xWrap.FuncPtrAdapter({
+ name: 'xInput', signature: 'i(ppp)', bindScope: 'transient'
+ }),
+ 'void*',
+ new wasm.xWrap.FuncPtrAdapter({
+ name: 'xOutput', signature: 'i(ppi)', bindScope: 'transient'
+ }),
+ 'void*'
+ ]],
+ ['sqlite3changeset_new', 'int', ['sqlite3_changeset_iter*', 'int', '**']],
+ ['sqlite3changeset_next', 'int', ['sqlite3_changeset_iter*']],
+ ['sqlite3changeset_old', 'int', ['sqlite3_changeset_iter*', 'int', '**']],
+ ['sqlite3changeset_op', 'int', [
+ 'sqlite3_changeset_iter*', '**', 'int*', 'int*','int*'
+ ]],
+ ['sqlite3changeset_pk', 'int', ['sqlite3_changeset_iter*', '**', 'int*']],
+ ['sqlite3changeset_start', 'int', ['**', 'int', '*']],
+ ['sqlite3changeset_start_strm', 'int', [
+ '**',
+ new wasm.xWrap.FuncPtrAdapter({
+ name: 'xInput', signature: 'i(ppp)', bindScope: 'transient'
+ }),
+ 'void*'
+ ]],
+ ['sqlite3changeset_start_v2', 'int', ['**', 'int', '*', 'int']],
+ ['sqlite3changeset_start_v2_strm', 'int', [
+ '**',
+ new wasm.xWrap.FuncPtrAdapter({
+ name: 'xInput', signature: 'i(ppp)', bindScope: 'transient'
+ }),
+ 'void*', 'int'
+ ]],
+ ['sqlite3session_attach', 'int', ['sqlite3_session*', 'string']],
+ ['sqlite3session_changeset', 'int', ['sqlite3_session*', 'int*', '**']],
+ ['sqlite3session_changeset_size', 'i64', ['sqlite3_session*']],
+ ['sqlite3session_changeset_strm', 'int', [
+ 'sqlite3_session*',
+ new wasm.xWrap.FuncPtrAdapter({
+ name: 'xOutput', signature: 'i(ppp)', bindScope: 'transient'
+ }),
+ 'void*'
+ ]],
+ ['sqlite3session_config', 'int', ['int', 'void*']],
+ ['sqlite3session_create', 'int', ['sqlite3*', 'string', '**']],
+ //sqlite3session_delete() is bound manually
+ ['sqlite3session_diff', 'int', ['sqlite3_session*', 'string', 'string', '**']],
+ ['sqlite3session_enable', 'int', ['sqlite3_session*', 'int']],
+ ['sqlite3session_indirect', 'int', ['sqlite3_session*', 'int']],
+ ['sqlite3session_isempty', 'int', ['sqlite3_session*']],
+ ['sqlite3session_memory_used', 'i64', ['sqlite3_session*']],
+ ['sqlite3session_object_config', 'int', ['sqlite3_session*', 'int', 'void*']],
+ ['sqlite3session_patchset', 'int', ['sqlite3_session*', '*', '**']],
+ ['sqlite3session_patchset_strm', 'int', [
+ 'sqlite3_session*',
+ new wasm.xWrap.FuncPtrAdapter({
+ name: 'xOutput', signature: 'i(ppp)', bindScope: 'transient'
+ }),
+ 'void*'
+ ]],
+ ['sqlite3session_table_filter', undefined, [
+ 'sqlite3_session*',
+ new wasm.xWrap.FuncPtrAdapter({
+ name: 'xFilter', ...__ipsProxy,
+ contextKey: (argv,argIndex)=>argv[0/* (sqlite3_session*) */]
+ }),
+ '*'
+ ]]
+ ]);
+ }/*session/changeset APIs*/
+
/**
Functions which are intended solely for API-internal use by the
WASM components, not client code. These get installed into
@@ -313,8 +615,10 @@ self.sqlite3ApiBootstrap.initializers.push(function(sqlite3){
});
delete self.Jaccwabyt;
- {/* Convert Arrays and certain TypedArrays to strings for
- 'string:flexible'-type arguments */
+ {// wasm.xWrap() bindings...
+
+ /* Convert Arrays and certain TypedArrays to strings for
+ 'string:flexible'-type arguments */
const __xString = wasm.xWrap.argAdapter('string');
wasm.xWrap.argAdapter(
'string:flexible', (v)=>__xString(util.flexibleString(v))
@@ -350,33 +654,35 @@ self.sqlite3ApiBootstrap.initializers.push(function(sqlite3){
return rc || (this[v] = wasm.allocCString(v));
}.bind(Object.create(null))
);
- }/* special-case string-type argument conversions */
- if(1){// wasm.xWrap() bindings...
/**
Add some descriptive xWrap() aliases for '*' intended to (A)
- initially improve readability/correctness of capi.signatures
- and (B) provide automatic conversion from higher-level
- representations, e.g. capi.sqlite3_vfs to `sqlite3_vfs*` via
- capi.sqlite3_vfs.pointer.
+ initially improve readability/correctness of
+ wasm.bindingSignatures and (B) provide automatic conversion
+ from higher-level representations, e.g. capi.sqlite3_vfs to
+ `sqlite3_vfs*` via capi.sqlite3_vfs.pointer.
*/
- const aPtr = wasm.xWrap.argAdapter('*');
- const nilType = function(){};
- wasm.xWrap.argAdapter('sqlite3_filename', aPtr)
- ('sqlite3_context*', aPtr)
- ('sqlite3_value*', aPtr)
- ('void*', aPtr)
+ const __xArgPtr = wasm.xWrap.argAdapter('*');
+ const nilType = function(){}/*a class no value can ever be an instance of*/;
+ wasm.xWrap.argAdapter('sqlite3_filename', __xArgPtr)
+ ('sqlite3_context*', __xArgPtr)
+ ('sqlite3_value*', __xArgPtr)
+ ('void*', __xArgPtr)
+ ('sqlite3_changegroup*', __xArgPtr)
+ ('sqlite3_changeset_iter*', __xArgPtr)
+ //('sqlite3_rebaser*', __xArgPtr)
+ ('sqlite3_session*', __xArgPtr)
('sqlite3_stmt*', (v)=>
- aPtr((v instanceof (sqlite3?.oo1?.Stmt || nilType))
+ __xArgPtr((v instanceof (sqlite3?.oo1?.Stmt || nilType))
? v.pointer : v))
('sqlite3*', (v)=>
- aPtr((v instanceof (sqlite3?.oo1?.DB || nilType))
+ __xArgPtr((v instanceof (sqlite3?.oo1?.DB || nilType))
? v.pointer : v))
('sqlite3_index_info*', (v)=>
- aPtr((v instanceof (capi.sqlite3_index_info || nilType))
+ __xArgPtr((v instanceof (capi.sqlite3_index_info || nilType))
? v.pointer : v))
('sqlite3_module*', (v)=>
- aPtr((v instanceof (capi.sqlite3_module || nilType))
+ __xArgPtr((v instanceof (capi.sqlite3_module || nilType))
? v.pointer : v))
/**
`sqlite3_vfs*`:
@@ -397,8 +703,8 @@ self.sqlite3ApiBootstrap.initializers.push(function(sqlite3){
"Unknown sqlite3_vfs name:", v
);
}
- return aPtr((v instanceof (capi.sqlite3_vfs || nilType))
- ? v.pointer : v);
+ return __xArgPtr((v instanceof (capi.sqlite3_vfs || nilType))
+ ? v.pointer : v);
});
const __xRcPtr = wasm.xWrap.resultAdapter('*');
@@ -467,7 +773,7 @@ self.sqlite3ApiBootstrap.initializers.push(function(sqlite3){
message = message || ''+resultCode;
resultCode = (resultCode.resultCode || capi.SQLITE_ERROR);
}
- return __db_err(pDb, resultCode, message);
+ return pDb ? __db_err(pDb, resultCode, message) : resultCode;
};
}else{
util.sqlite3_wasm_db_error = function(pDb,errCode,msg){
@@ -475,7 +781,87 @@ self.sqlite3ApiBootstrap.initializers.push(function(sqlite3){
return errCode;
};
}
- }/*xWrap() bindings*/;
+ }/*xWrap() bindings*/
+
+ {/* Import C-level constants and structs... */
+ const cJson = wasm.xCall('sqlite3_wasm_enum_json');
+ if(!cJson){
+ toss("Maintenance required: increase sqlite3_wasm_enum_json()'s",
+ "static buffer size!");
+ }
+ //console.debug('wasm.ctype length =',wasm.cstrlen(cJson));
+ wasm.ctype = JSON.parse(wasm.cstrToJs(cJson));
+ // Groups of SQLITE_xyz macros...
+ const defineGroups = ['access', 'authorizer',
+ 'blobFinalizers', 'changeset',
+ 'config', 'dataTypes',
+ 'dbConfig', 'dbStatus',
+ 'encodings', 'fcntl', 'flock', 'ioCap',
+ 'limits', 'openFlags',
+ 'prepareFlags', 'resultCodes',
+ 'sqlite3Status',
+ 'stmtStatus', 'syncFlags',
+ 'trace', 'txnState', 'udfFlags',
+ 'version' ];
+ if(wasm.bigIntEnabled){
+ defineGroups.push('serialize', 'session', 'vtab');
+ }
+ for(const t of defineGroups){
+ for(const e of Object.entries(wasm.ctype[t])){
+ // ^^^ [k,v] there triggers a buggy code transformation via
+ // one of the Emscripten-driven optimizers.
+ capi[e[0]] = e[1];
+ }
+ }
+ if(!wasm.functionEntry(capi.SQLITE_WASM_DEALLOC)){
+ toss("Internal error: cannot resolve exported function",
+ "entry SQLITE_WASM_DEALLOC (=="+capi.SQLITE_WASM_DEALLOC+").");
+ }
+ const __rcMap = Object.create(null);
+ for(const t of ['resultCodes']){
+ for(const e of Object.entries(wasm.ctype[t])){
+ __rcMap[e[1]] = e[0];
+ }
+ }
+ /**
+ For the given integer, returns the SQLITE_xxx result code as a
+ string, or undefined if no such mapping is found.
+ */
+ capi.sqlite3_js_rc_str = (rc)=>__rcMap[rc];
+ /* Bind all registered C-side structs... */
+ const notThese = Object.assign(Object.create(null),{
+ // For each struct to NOT register, map its name to true:
+ WasmTestStruct: true,
+ /* We unregister the kvvfs VFS from Worker threads below. */
+ sqlite3_kvvfs_methods: !util.isUIThread(),
+ /* sqlite3_index_info and friends require int64: */
+ sqlite3_index_info: !wasm.bigIntEnabled,
+ sqlite3_index_constraint: !wasm.bigIntEnabled,
+ sqlite3_index_orderby: !wasm.bigIntEnabled,
+ sqlite3_index_constraint_usage: !wasm.bigIntEnabled
+ });
+ for(const s of wasm.ctype.structs){
+ if(!notThese[s.name]){
+ capi[s.name] = sqlite3.StructBinder(s);
+ }
+ }
+ if(capi.sqlite3_index_info){
+ /* Move these inner structs into sqlite3_index_info. Binding
+ ** them to WASM requires that we create global-scope structs to
+ ** model them with, but those are no longer needed after we've
+ ** passed them to StructBinder. */
+ for(const k of ['sqlite3_index_constraint',
+ 'sqlite3_index_orderby',
+ 'sqlite3_index_constraint_usage']){
+ capi.sqlite3_index_info[k] = capi[k];
+ delete capi[k];
+ }
+ capi.sqlite3_vtab_config = wasm.xWrap(
+ 'sqlite3_wasm_vtab_config','int',[
+ 'sqlite3*', 'int', 'int']
+ );
+ }/* end vtab-related setup */
+ }/*end C constant and struct imports*/
/**
Internal helper to assist in validating call argument counts in
@@ -497,28 +883,191 @@ self.sqlite3ApiBootstrap.initializers.push(function(sqlite3){
);
};
- if(1){/* Bindings for sqlite3_create_collation[_v2]() */
- const __collationContextKey = (argIndex,argv)=>{
- return 'argv['+argIndex+']:sqlite3@'+argv[0]+
- ':'+wasm.cstrToJs(argv[1]).toLowerCase()
+ /**
+ __dbCleanupMap is infrastructure for recording registration of
+ UDFs and collations so that sqlite3_close_v2() can clean up any
+ automated JS-to-WASM function conversions installed by those.
+ */
+ const __argPDb = (pDb)=>wasm.xWrap.argAdapter('sqlite3*')(pDb);
+ const __argStr = (str)=>wasm.isPtr(str) ? wasm.cstrToJs(str) : str;
+ const __dbCleanupMap = function(
+ pDb, mode/*0=remove, >0=create if needed, <0=do not create if missing*/
+ ){
+ pDb = __argPDb(pDb);
+ let m = this.dbMap.get(pDb);
+ if(!mode){
+ this.dbMap.delete(pDb);
+ return m;
+ }else if(!m && mode>0){
+ this.dbMap.set(pDb, (m = Object.create(null)));
+ }
+ return m;
+ }.bind(Object.assign(Object.create(null),{
+ dbMap: new Map
+ }));
+
+ __dbCleanupMap.addCollation = function(pDb, name){
+ const m = __dbCleanupMap(pDb, 1);
+ if(!m.collation) m.collation = new Set;
+ m.collation.add(__argStr(name).toLowerCase());
+ };
+
+ __dbCleanupMap._addUDF = function(pDb, name, arity, map){
+ /* Map UDF name to a Set of arity values */
+ name = __argStr(name).toLowerCase();
+ let u = map.get(name);
+ if(!u) map.set(name, (u = new Set));
+ u.add((arity<0) ? -1 : arity);
+ };
+
+ __dbCleanupMap.addFunction = function(pDb, name, arity){
+ const m = __dbCleanupMap(pDb, 1);
+ if(!m.udf) m.udf = new Map;
+ this._addUDF(pDb, name, arity, m.udf);
+ };
+
+ __dbCleanupMap.addWindowFunc = function(pDb, name, arity){
+ const m = __dbCleanupMap(pDb, 1);
+ if(!m.wudf) m.wudf = new Map;
+ this._addUDF(pDb, name, arity, m.wudf);
+ };
+
+ /**
+ Intended to be called _only_ from sqlite3_close_v2(),
+ passed its non-0 db argument.
+
+ This function frees up certain automatically-installed WASM
+ function bindings which were installed on behalf of the given db,
+ as those may otherwise leak.
+
+ Notable caveat: this is only ever run via
+ sqlite3.capi.sqlite3_close_v2(). If a client, for whatever
+ reason, uses sqlite3.wasm.exports.sqlite3_close_v2() (the
+ function directly exported from WASM), this cleanup will not
+ happen.
+
+ This is not a silver bullet for avoiding automation-related
+ leaks but represents "an honest effort."
+
+ The issue being addressed here is covered at:
+
+ https://sqlite.org/wasm/doc/trunk/api-c-style.md#convert-func-ptr
+ */
+ __dbCleanupMap.cleanup = function(pDb){
+ pDb = __argPDb(pDb);
+ //wasm.xWrap.FuncPtrAdapter.debugFuncInstall = false;
+ /**
+ Installing NULL functions in the C API will remove those
+ bindings. The FuncPtrAdapter which sits between us and the C
+ API will also treat that as an opportunity to
+ wasm.uninstallFunction() any WASM function bindings it has
+ installed for pDb.
+ */
+ const closeArgs = [pDb];
+ for(const name of [
+ 'sqlite3_busy_handler',
+ 'sqlite3_commit_hook',
+ 'sqlite3_preupdate_hook',
+ 'sqlite3_progress_handler',
+ 'sqlite3_rollback_hook',
+ 'sqlite3_set_authorizer',
+ 'sqlite3_trace_v2',
+ 'sqlite3_update_hook'
+ ]) {
+ const x = wasm.exports[name];
+ closeArgs.length = x.length/*==argument count*/
+ /* recall that undefined entries translate to 0 when passed to
+ WASM. */;
+ try{ capi[name](...closeArgs) }
+ catch(e){
+ console.warn("close-time call of",name+"(",closeArgs,") threw:",e);
+ }
+ }
+ const m = __dbCleanupMap(pDb, 0);
+ if(!m) return;
+ if(m.collation){
+ for(const name of m.collation){
+ try{
+ capi.sqlite3_create_collation_v2(
+ pDb, name, capi.SQLITE_UTF8, 0, 0, 0
+ );
+ }catch(e){
+ /*ignored*/
+ }
+ }
+ delete m.collation;
+ }
+ let i;
+ for(i = 0; i < 2; ++i){ /* Clean up UDFs... */
+ const fmap = i ? m.wudf : m.udf;
+ if(!fmap) continue;
+ const func = i
+ ? capi.sqlite3_create_window_function
+ : capi.sqlite3_create_function_v2;
+ for(const e of fmap){
+ const name = e[0], arities = e[1];
+ const fargs = [pDb, name, 0/*arity*/, capi.SQLITE_UTF8, 0, 0, 0, 0, 0];
+ if(i) fargs.push(0);
+ for(const arity of arities){
+ try{ fargs[2] = arity; func.apply(null, fargs); }
+ catch(e){/*ignored*/}
+ }
+ arities.clear();
+ }
+ fmap.clear();
+ }
+ delete m.udf;
+ delete m.wudf;
+ }/*__dbCleanupMap.cleanup()*/;
+
+ {/* Binding of sqlite3_close_v2() */
+ const __sqlite3CloseV2 = wasm.xWrap("sqlite3_close_v2", "int", "sqlite3*");
+ capi.sqlite3_close_v2 = function(pDb){
+ if(1!==arguments.length) return __dbArgcMismatch(pDb, 'sqlite3_close_v2', 1);
+ if(pDb){
+ try{__dbCleanupMap.cleanup(pDb)} catch(e){/*ignored*/}
+ }
+ return __sqlite3CloseV2(pDb);
};
- const __ccv2 = wasm.xWrap(
- 'sqlite3_create_collation_v2', 'int',
- 'sqlite3*','string','int','*',
- new wasm.xWrap.FuncPtrAdapter({
- /* int(*xCompare)(void*,int,const void*,int,const void*) */
- name: 'sqlite3_create_collation_v2::xCompare',
- signature: 'i(pipip)',
- bindScope: 'context',
- contextKey: __collationContextKey
- }),
- new wasm.xWrap.FuncPtrAdapter({
- /* void(*xDestroy(void*) */
- name: 'sqlite3_create_collation_v2::xDestroy',
- signature: 'v(p)',
- bindScope: 'context',
- contextKey: __collationContextKey
- })
+ }/*sqlite3_close_v2()*/
+
+ if(capi.sqlite3session_table_filter){
+ const __sqlite3SessionDelete = wasm.xWrap(
+ 'sqlite3session_delete', undefined, ['sqlite3_session*']
+ );
+ capi.sqlite3session_delete = function(pSession){
+ if(1!==arguments.length){
+ return __dbArgcMismatch(pDb, 'sqlite3session_delete', 1);
+ /* Yes, we're returning a value from a void function. That seems
+ like the lesser evil compared to not maintaining arg-count
+ consistency as we do with other similar bindings. */
+ }
+ else if(pSession){
+ //wasm.xWrap.FuncPtrAdapter.debugFuncInstall = true;
+ capi.sqlite3session_table_filter(pSession, 0, 0);
+ }
+ __sqlite3SessionDelete(pSession);
+ };
+ }
+
+ {/* Bindings for sqlite3_create_collation[_v2]() */
+ // contextKey() impl for wasm.xWrap.FuncPtrAdapter
+ const contextKey = (argv,argIndex)=>{
+ return 'argv['+argIndex+']:'+argv[0/* sqlite3* */]+
+ ':'+wasm.cstrToJs(argv[1/* collation name */]).toLowerCase()
+ };
+ const __sqlite3CreateCollationV2 = wasm.xWrap(
+ 'sqlite3_create_collation_v2', 'int', [
+ 'sqlite3*', 'string', 'int', '*',
+ new wasm.xWrap.FuncPtrAdapter({
+ /* int(*xCompare)(void*,int,const void*,int,const void*) */
+ name: 'xCompare', signature: 'i(pipip)', contextKey
+ }),
+ new wasm.xWrap.FuncPtrAdapter({
+ /* void(*xDestroy(void*) */
+ name: 'xDestroy', signature: 'v(p)', contextKey
+ })
+ ]
);
/**
@@ -552,13 +1101,15 @@ self.sqlite3ApiBootstrap.initializers.push(function(sqlite3){
}else if( capi.SQLITE_UTF8 !== (eTextRep & 0xf) ){
return __errEncoding(pDb);
}
- let rc, pfCompare, pfDestroy;
- try{
- rc = __ccv2(pDb, zName, eTextRep, pArg, xCompare, xDestroy);
+ try{
+ const rc = __sqlite3CreateCollationV2(pDb, zName, eTextRep, pArg, xCompare, xDestroy);
+ if(0===rc && xCompare instanceof Function){
+ __dbCleanupMap.addCollation(pDb, zName);
+ }
+ return rc;
}catch(e){
- rc = util.sqlite3_wasm_db_error(pDb, e);
+ return util.sqlite3_wasm_db_error(pDb, e);
}
- return rc;
};
capi.sqlite3_create_collation = (pDb,zName,eTextRep,pArg,xCompare)=>{
@@ -569,130 +1120,92 @@ self.sqlite3ApiBootstrap.initializers.push(function(sqlite3){
}/*sqlite3_create_collation() and friends*/
- if(1){/* Special-case handling of sqlite3_exec() */
- const __exec = wasm.xWrap("sqlite3_exec", "int",
- ["sqlite3*", "string:flexible",
- new wasm.xWrap.FuncPtrAdapter({
- signature: 'i(pipp)',
- bindScope: 'transient'
- }), "*", "**"]);
- /* Documented in the api object's initializer. */
- capi.sqlite3_exec = function f(pDb, sql, callback, pVoid, pErrMsg){
- if(f.length!==arguments.length){
- return __dbArgcMismatch(pDb,"sqlite3_exec",f.length);
- }else if(!(callback instanceof Function)){
- return __exec(pDb, sql, callback, pVoid, pErrMsg);
- }
- /* Wrap the callback in a WASM-bound function and convert the callback's
- `(char**)` arguments to arrays of strings... */
- const cbwrap = function(pVoid, nCols, pColVals, pColNames){
- let rc = capi.SQLITE_ERROR;
- try {
- let aVals = [], aNames = [], i = 0, offset = 0;
- for( ; i < nCols; offset += (wasm.ptrSizeof * ++i) ){
- aVals.push( wasm.cstrToJs(wasm.peekPtr(pColVals + offset)) );
- aNames.push( wasm.cstrToJs(wasm.peekPtr(pColNames + offset)) );
- }
- rc = callback(pVoid, nCols, aVals, aNames) | 0;
- /* The first 2 args of the callback are useless for JS but
- we want the JS mapping of the C API to be as close to the
- C API as possible. */
- }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 of reporting the
- exception message except the console. We must not propagate
- exceptions through the C API. */
- }
- return rc;
- };
- let rc;
- try{
- rc = __exec(pDb, sql, cbwrap, pVoid, pErrMsg);
- }catch(e){
- rc = util.sqlite3_wasm_db_error(pDb, capi.SQLITE_ERROR,
- "Error running exec(): "+e);
- }
- return rc;
+ {/* Special-case handling of sqlite3_create_function_v2()
+ and sqlite3_create_window_function(). */
+ /** FuncPtrAdapter for contextKey() for sqlite3_create_function()
+ and friends. */
+ const contextKey = function(argv,argIndex){
+ return (
+ argv[0/* sqlite3* */]
+ +':'+(argv[2/*number of UDF args*/] < 0 ? -1 : argv[2])
+ +':'+argIndex/*distinct for each xAbc callback type*/
+ +':'+wasm.cstrToJs(argv[1]).toLowerCase()
+ )
};
- }/*sqlite3_exec() proxy*/;
-
- if(1){/* Special-case handling of sqlite3_create_function_v2()
- and sqlite3_create_window_function() */
- /* Maintenance reminder: FuncPtrAdapter is not expressive enough
- to be able to perform these mappings. */
- const sqlite3CreateFunction = wasm.xWrap(
- "sqlite3_create_function_v2", "int",
- ["sqlite3*", "string"/*funcName*/, "int"/*nArg*/,
- "int"/*eTextRep*/, "*"/*pApp*/,
- "*"/*xStep*/,"*"/*xFinal*/, "*"/*xValue*/, "*"/*xDestroy*/]
- );
-
- const sqlite3CreateWindowFunction = wasm.xWrap(
- "sqlite3_create_window_function", "int",
- ["sqlite3*", "string"/*funcName*/, "int"/*nArg*/,
- "int"/*eTextRep*/, "*"/*pApp*/,
- "*"/*xStep*/,"*"/*xFinal*/, "*"/*xValue*/,
- "*"/*xInverse*/, "*"/*xDestroy*/]
- );
- const __xFunc = function(callback){
- return function(pCtx, argc, pArgv){
- try{
- capi.sqlite3_result_js(
- pCtx,
- callback(pCtx, ...capi.sqlite3_values_to_js(argc, pArgv))
- );
- }catch(e){
- //console.error('xFunc() caught:',e);
- capi.sqlite3_result_error_js(pCtx, e);
+ /**
+ JS proxies for the various sqlite3_create[_window]_function()
+ callbacks, structured in a form usable by wasm.xWrap.FuncPtrAdapter.
+ */
+ const __cfProxy = Object.assign(Object.create(null), {
+ xInverseAndStep: {
+ signature:'v(pip)', contextKey,
+ callProxy: (callback)=>{
+ return (pCtx, argc, pArgv)=>{
+ try{ callback(pCtx, ...capi.sqlite3_values_to_js(argc, pArgv)) }
+ catch(e){ capi.sqlite3_result_error_js(pCtx, e) }
+ };
}
- };
- };
-
- const __xInverseAndStep = function(callback){
- return function(pCtx, argc, pArgv){
- try{ callback(pCtx, ...capi.sqlite3_values_to_js(argc, pArgv)) }
- catch(e){ capi.sqlite3_result_error_js(pCtx, e) }
- };
- };
-
- const __xFinalAndValue = function(callback){
- return function(pCtx){
- try{ capi.sqlite3_result_js(pCtx, callback(pCtx)) }
- catch(e){ capi.sqlite3_result_error_js(pCtx, e) }
- };
- };
-
- const __xDestroy = function(callback){
- return function(pVoid){
- try{ callback(pVoid) }
- catch(e){ console.error("UDF xDestroy method threw:",e) }
- };
- };
-
- const __xMap = Object.assign(Object.create(null), {
- xFunc: {sig:'v(pip)', f:__xFunc},
- xStep: {sig:'v(pip)', f:__xInverseAndStep},
- xInverse: {sig:'v(pip)', f:__xInverseAndStep},
- xFinal: {sig:'v(p)', f:__xFinalAndValue},
- xValue: {sig:'v(p)', f:__xFinalAndValue},
- xDestroy: {sig:'v(p)', f:__xDestroy}
- });
-
- /* Internal helper for sqlite3_create_function() and friends. */
- const __xWrapFuncs = function(theKeys, theFuncs, tgtUninst){
- const rc = []
- for(const k of theKeys){
- let fArg = theFuncs[k];
- if('function'===typeof fArg){
- const w = __xMap[k] || toss3("Internal error in __xWrapFuncs: invalid key:",k);
- fArg = wasm.installFunction(w.sig, w.f(fArg));
- tgtUninst.push(fArg);
+ },
+ xFinalAndValue: {
+ signature:'v(p)', contextKey,
+ callProxy: (callback)=>{
+ return (pCtx)=>{
+ try{ capi.sqlite3_result_js(pCtx, callback(pCtx)) }
+ catch(e){ capi.sqlite3_result_error_js(pCtx, e) }
+ };
+ }
+ },
+ xFunc: {
+ signature:'v(pip)', contextKey,
+ callProxy: (callback)=>{
+ return (pCtx, argc, pArgv)=>{
+ try{
+ capi.sqlite3_result_js(
+ pCtx,
+ callback(pCtx, ...capi.sqlite3_values_to_js(argc, pArgv))
+ );
+ }catch(e){
+ //console.error('xFunc() caught:',e);
+ capi.sqlite3_result_error_js(pCtx, e);
+ }
+ };
+ }
+ },
+ xDestroy: {
+ signature:'v(p)', contextKey,
+ //Arguable: a well-behaved destructor doesn't require a proxy.
+ callProxy: (callback)=>{
+ return (pVoid)=>{
+ try{ callback(pVoid) }
+ catch(e){ console.error("UDF xDestroy method threw:",e) }
+ };
}
- rc.push(fArg);
}
- return rc;
- };
+ })/*__cfProxy*/;
+
+ const __sqlite3CreateFunction = wasm.xWrap(
+ "sqlite3_create_function_v2", "int", [
+ "sqlite3*", "string"/*funcName*/, "int"/*nArg*/,
+ "int"/*eTextRep*/, "*"/*pApp*/,
+ new wasm.xWrap.FuncPtrAdapter({name: 'xFunc', ...__cfProxy.xFunc}),
+ new wasm.xWrap.FuncPtrAdapter({name: 'xStep', ...__cfProxy.xInverseAndStep}),
+ new wasm.xWrap.FuncPtrAdapter({name: 'xFinal', ...__cfProxy.xFinalAndValue}),
+ new wasm.xWrap.FuncPtrAdapter({name: 'xDestroy', ...__cfProxy.xDestroy})
+ ]
+ );
+
+ const __sqlite3CreateWindowFunction = wasm.xWrap(
+ "sqlite3_create_window_function", "int", [
+ "sqlite3*", "string"/*funcName*/, "int"/*nArg*/,
+ "int"/*eTextRep*/, "*"/*pApp*/,
+ new wasm.xWrap.FuncPtrAdapter({name: 'xStep', ...__cfProxy.xInverseAndStep}),
+ new wasm.xWrap.FuncPtrAdapter({name: 'xFinal', ...__cfProxy.xFinalAndValue}),
+ new wasm.xWrap.FuncPtrAdapter({name: 'xValue', ...__cfProxy.xFinalAndValue}),
+ new wasm.xWrap.FuncPtrAdapter({name: 'xInverse', ...__cfProxy.xInverseAndStep}),
+ new wasm.xWrap.FuncPtrAdapter({name: 'xDestroy', ...__cfProxy.xDestroy})
+ ]
+ );
/* Documented in the api object's initializer. */
capi.sqlite3_create_function_v2 = function f(
@@ -709,26 +1222,23 @@ self.sqlite3ApiBootstrap.initializers.push(function(sqlite3){
}else if( capi.SQLITE_UTF8 !== (eTextRep & 0xf) ){
return __errEncoding(pDb);
}
- /* Wrap the callbacks in a WASM-bound functions... */
- const uninstall = [/*funcs to uninstall on error*/];
- let rc;
try{
- const funcArgs = __xWrapFuncs(['xFunc','xStep','xFinal','xDestroy'],
- {xFunc, xStep, xFinal, xDestroy},
- uninstall);
- rc = sqlite3CreateFunction(pDb, funcName, nArg, eTextRep,
- pApp, ...funcArgs);
+ const rc = __sqlite3CreateFunction(pDb, funcName, nArg, eTextRep,
+ pApp, xFunc, xStep, xFinal, xDestroy);
+ if(0===rc && (xFunc instanceof Function
+ || xStep instanceof Function
+ || xFinal instanceof Function
+ || xDestroy instanceof Function)){
+ __dbCleanupMap.addFunction(pDb, funcName, nArg);
+ }
+ return rc;
}catch(e){
console.error("sqlite3_create_function_v2() setup threw:",e);
- for(let v of uninstall){
- wasm.uninstallFunction(v);
- }
- rc = util.sqlite3_wasm_db_error(pDb, capi.SQLITE_ERROR,
- "Creation of UDF threw: "+e.message);
+ return util.sqlite3_wasm_db_error(pDb, e, "Creation of UDF threw: "+e);
}
- return rc;
};
+ /* Documented in the api object's initializer. */
capi.sqlite3_create_function = function f(
pDb, funcName, nArg, eTextRep, pApp,
xFunc, xStep, xFinal
@@ -744,8 +1254,8 @@ self.sqlite3ApiBootstrap.initializers.push(function(sqlite3){
pDb, funcName, nArg, eTextRep, pApp,
xStep, //void (*xStep)(sqlite3_context*,int,sqlite3_value**)
xFinal, //void (*xFinal)(sqlite3_context*)
- xValue, //void (*xFinal)(sqlite3_context*)
- xInverse,//void (*xStep)(sqlite3_context*,int,sqlite3_value**)
+ xValue, //void (*xValue)(sqlite3_context*)
+ xInverse,//void (*xInverse)(sqlite3_context*,int,sqlite3_value**)
xDestroy //void (*xDestroy)(void*)
){
if( f.length!==arguments.length ){
@@ -755,24 +1265,22 @@ self.sqlite3ApiBootstrap.initializers.push(function(sqlite3){
}else if( capi.SQLITE_UTF8 !== (eTextRep & 0xf) ){
return __errEncoding(pDb);
}
- /* Wrap the callbacks in a WASM-bound functions... */
- const uninstall = [/*funcs to uninstall on error*/];
- let rc;
try{
- const funcArgs = __xWrapFuncs(['xStep','xFinal','xValue','xInverse','xDestroy'],
- {xStep, xFinal, xValue, xInverse, xDestroy},
- uninstall);
- rc = sqlite3CreateWindowFunction(pDb, funcName, nArg, eTextRep,
- pApp, ...funcArgs);
+ const rc = __sqlite3CreateWindowFunction(pDb, funcName, nArg, eTextRep,
+ pApp, xStep, xFinal, xValue,
+ xInverse, xDestroy);
+ if(0===rc && (xStep instanceof Function
+ || xFinal instanceof Function
+ || xValue instanceof Function
+ || xInverse instanceof Function
+ || xDestroy instanceof Function)){
+ __dbCleanupMap.addWindowFunc(pDb, funcName, nArg);
+ }
+ return rc;
}catch(e){
console.error("sqlite3_create_window_function() setup threw:",e);
- for(let v of uninstall){
- wasm.uninstallFunction(v);
- }
- rc = util.sqlite3_wasm_db_error(pDb, capi.SQLITE_ERROR,
- "Creation of UDF threw: "+e.message);
+ return util.sqlite3_wasm_db_error(pDb, e, "Creation of UDF threw: "+e);
}
- return rc;
};
/**
A _deprecated_ alias for capi.sqlite3_result_js() which
@@ -800,22 +1308,24 @@ self.sqlite3ApiBootstrap.initializers.push(function(sqlite3){
}/*sqlite3_create_function_v2() and sqlite3_create_window_function() proxies*/;
- if(1){/* Special-case handling of sqlite3_prepare_v2() and
- sqlite3_prepare_v3() */
+ {/* Special-case handling of sqlite3_prepare_v2() and
+ sqlite3_prepare_v3() */
/**
Helper for string:flexible conversions which require a
byte-length counterpart argument. Passed a value and its
- ostensible length, this function returns [V,N], where V
- is either v or a transformed copy of v and N is either n,
- -1, or the byte length of v (if it's a byte array).
+ ostensible length, this function returns [V,N], where V is
+ either v or a transformed copy of v and N is either n, -1, or
+ the byte length of v (if it's a byte array or ArrayBuffer).
*/
const __flexiString = (v,n)=>{
if('string'===typeof v){
n = -1;
}else if(util.isSQLableTypedArray(v)){
n = v.byteLength;
- v = util.typedArrayToString(v);
+ v = util.typedArrayToString(
+ (v instanceof ArrayBuffer) ? new Uint8Array(v) : v
+ );
}else if(Array.isArray(v)){
v = v.join("");
n = -1;
@@ -958,37 +1468,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.
@@ -1035,79 +1514,41 @@ self.sqlite3ApiBootstrap.initializers.push(function(sqlite3){
};
}/* sqlite3_config() */
- {/* Import C-level constants and structs... */
- const cJson = wasm.xCall('sqlite3_wasm_enum_json');
- if(!cJson){
- toss("Maintenance required: increase sqlite3_wasm_enum_json()'s",
- "static buffer size!");
- }
- wasm.ctype = JSON.parse(wasm.cstrToJs(cJson));
- //console.debug('wasm.ctype length =',wasm.cstrlen(cJson));
- const defineGroups = ['access', 'authorizer',
- 'blobFinalizers', 'config', 'dataTypes',
- 'dbConfig', 'dbStatus',
- 'encodings', 'fcntl', 'flock', 'ioCap',
- 'limits', 'openFlags',
- 'prepareFlags', 'resultCodes',
- 'serialize', 'sqlite3Status',
- 'stmtStatus', 'syncFlags',
- 'trace', 'txnState', 'udfFlags',
- 'version' ];
- if(wasm.bigIntEnabled){
- defineGroups.push('vtab');
- }
- for(const t of defineGroups){
- for(const e of Object.entries(wasm.ctype[t])){
- // ^^^ [k,v] there triggers a buggy code transformation via
- // one of the Emscripten-driven optimizers.
- capi[e[0]] = e[1];
- }
- }
- const __rcMap = Object.create(null);
- for(const t of ['resultCodes']){
- for(const e of Object.entries(wasm.ctype[t])){
- __rcMap[e[1]] = e[0];
- }
- }
- /**
- For the given integer, returns the SQLITE_xxx result code as a
- string, or undefined if no such mapping is found.
- */
- capi.sqlite3_js_rc_str = (rc)=>__rcMap[rc];
- /* Bind all registered C-side structs... */
- const notThese = Object.assign(Object.create(null),{
- // For each struct to NOT register, map its name to true:
- WasmTestStruct: true,
- /* We unregister the kvvfs VFS from Worker threads below. */
- sqlite3_kvvfs_methods: !util.isUIThread(),
- /* sqlite3_index_info and friends require int64: */
- sqlite3_index_info: !wasm.bigIntEnabled,
- sqlite3_index_constraint: !wasm.bigIntEnabled,
- sqlite3_index_orderby: !wasm.bigIntEnabled,
- sqlite3_index_constraint_usage: !wasm.bigIntEnabled
- });
- for(const s of wasm.ctype.structs){
- if(!notThese[s.name]){
- capi[s.name] = sqlite3.StructBinder(s);
+ {/*auto-extension bindings.*/
+ const __autoExtFptr = new Set;
+
+ capi.sqlite3_auto_extension = function(fPtr){
+ if( fPtr instanceof Function ){
+ fPtr = wasm.installFunction('i(ppp)', fPtr);
+ }else if( 1!==arguments.length || !wasm.isPtr(fPtr) ){
+ return capi.SQLITE_MISUSE;
}
- }
- if(capi.sqlite3_index_info){
- /* Move these inner structs into sqlite3_index_info. Binding
- ** them to WASM requires that we create global-scope structs to
- ** model them with, but those are no longer needed after we've
- ** passed them to StructBinder. */
- for(const k of ['sqlite3_index_constraint',
- 'sqlite3_index_orderby',
- 'sqlite3_index_constraint_usage']){
- capi.sqlite3_index_info[k] = capi[k];
- delete capi[k];
+ const rc = wasm.exports.sqlite3_auto_extension(fPtr);
+ if( fPtr!==arguments[0] ){
+ if(0===rc) __autoExtFptr.add(fPtr);
+ else wasm.uninstallFunction(fPtr);
}
- capi.sqlite3_vtab_config = wasm.xWrap(
- 'sqlite3_wasm_vtab_config','int',[
- 'sqlite3*', 'int', 'int']
- );
- }/* end vtab-related setup */
- }/*end C constant and struct imports*/
+ return rc;
+ };
+
+ capi.sqlite3_cancel_auto_extension = function(fPtr){
+ /* We do not do an automatic JS-to-WASM function conversion here
+ because it would be senseless: the converted pointer would
+ never possibly match an already-installed one. */;
+ if(!fPtr || 1!==arguments.length || !wasm.isPtr(fPtr)) return 0;
+ return wasm.exports.sqlite3_cancel_auto_extension(fPtr);
+ /* Note that it "cannot happen" that a client passes a pointer which
+ is in __autoExtFptr because __autoExtFptr only contains automatic
+ conversions created inside sqlite3_auto_extension() and
+ never exposed to the client. */
+ };
+
+ capi.sqlite3_reset_auto_extension = function(){
+ wasm.exports.sqlite3_reset_auto_extension();
+ for(const fp of __autoExtFptr) wasm.uninstallFunction(fp);
+ __autoExtFptr.clear();
+ };
+ }/* auto-extension */
const pKvvfs = capi.sqlite3_vfs_find("kvvfs");
if( pKvvfs ){/* kvvfs-specific glue */
@@ -1206,4 +1647,5 @@ self.sqlite3ApiBootstrap.initializers.push(function(sqlite3){
}
}/*pKvvfs*/
+ wasm.xWrap.FuncPtrAdapter.warnOnUse = true;
});