aboutsummaryrefslogtreecommitdiff
path: root/ext/wasm/api
diff options
context:
space:
mode:
authorstephan <stephan@noemail.net>2022-09-29 22:08:22 +0000
committerstephan <stephan@noemail.net>2022-09-29 22:08:22 +0000
commitf71c954cbc07fa0fc8a59e2a8a5a2a32b8e5d95c (patch)
tree44ceac4d12ee7e382ed6d3c8124fa66996617720 /ext/wasm/api
parentb0ccf50cbd0430eefaf50f5b6c2485ee8d68d6d4 (diff)
downloadsqlite-f71c954cbc07fa0fc8a59e2a8a5a2a32b8e5d95c.tar.gz
sqlite-f71c954cbc07fa0fc8a59e2a8a5a2a32b8e5d95c.zip
Add JS infrastructure to ostensibly allow us to customize the wasm imports, which will hypothetically allow us to eliminate the dependency on EM_JS(), but the corresponding Emscripten glue-level feature currently breaks fatally with WASMFS builds so it's disabled.
FossilOrigin-Name: 88d9253b0db5494bf1c9b6d24f22524eeec856b89e64a66ffb30d945f0df21d3
Diffstat (limited to 'ext/wasm/api')
-rw-r--r--ext/wasm/api/extern-post-js.js2
-rw-r--r--ext/wasm/api/extern-pre-js.js4
-rw-r--r--ext/wasm/api/pre-js.js40
3 files changed, 45 insertions, 1 deletions
diff --git a/ext/wasm/api/extern-post-js.js b/ext/wasm/api/extern-post-js.js
index acb54c3c9..25d2d1b32 100644
--- a/ext/wasm/api/extern-post-js.js
+++ b/ext/wasm/api/extern-post-js.js
@@ -1,4 +1,4 @@
-/* emscripten-js-addenda.js must be appended to the resulting sqlite3.js
+/* extern-post-js.js must be appended to the resulting sqlite3.js
file. */
(function(){
/**
diff --git a/ext/wasm/api/extern-pre-js.js b/ext/wasm/api/extern-pre-js.js
new file mode 100644
index 000000000..26d066d2e
--- /dev/null
+++ b/ext/wasm/api/extern-pre-js.js
@@ -0,0 +1,4 @@
+/* extern-pre-js.js must be prepended to the resulting sqlite3.js
+ file. This file is currently only used for holding snippets during
+ test and development.
+*/
diff --git a/ext/wasm/api/pre-js.js b/ext/wasm/api/pre-js.js
new file mode 100644
index 000000000..f373fecec
--- /dev/null
+++ b/ext/wasm/api/pre-js.js
@@ -0,0 +1,40 @@
+Module['locateFile'] = function(path, prefix) {
+ return prefix + path;
+};
+
+/**
+ Bug warning: this xInstantiateWasm bit must remain disabled
+ until this bug is resolved or wasmfs won't work:
+
+ https://github.com/emscripten-core/emscripten/issues/17951
+*/
+const xInstantiateWasm = 1
+ ? 'emscripten-bug-17951'
+ : 'instantiateWasm';
+Module[xInstantiateWasm] = function callee(imports,onSuccess){
+ imports.foo = function(){};
+ console.warn("instantiateWasm() uri =",callee.uri, self.location.href);
+ const wfetch = ()=>fetch(callee.uri, {credentials: 'same-origin'});
+ const loadWasm = WebAssembly.instantiateStreaming
+ ? function loadWasmStreaming(){
+ return WebAssembly.instantiateStreaming(wfetch(), imports)
+ .then((arg)=>onSuccess(arg.instance, arg.module));
+ }
+ : function loadWasmOldSchool(){ // Safari < v15
+ return wfetch()
+ .then(response => response.arrayBuffer())
+ .then(bytes => WebAssembly.instantiate(bytes, imports))
+ .then((arg)=>onSuccess(arg.instance, arg.module));
+ };
+ loadWasm();
+ return {};
+};
+/*
+ It is literally impossible to get the name of a Worker's own script,
+ so impossible to derive X.wasm from script name X.js. Thus we need,
+ at build-time, to redifine Module['instantiateWasm'].uri by
+ appending it to a build-specific copy of this file with the name of
+ the wasm file. This is apparently why Emscripten hard-codes the name of
+ the wasm file into their glue scripts.
+*/
+Module[xInstantiateWasm].uri = 'sqlite3.wasm';