xref: /sqlite-3.40.0/ext/wasm/api/pre-js.js (revision ac79fa1c)
1/**
2   BEGIN FILE: api/pre-js.js
3
4   This file is intended to be prepended to the sqlite3.js build using
5   Emscripten's --pre-js=THIS_FILE flag (or equivalent).
6*/
7Module['locateFile'] = function(path, prefix) {
8  return prefix + path;
9};
10
11/**
12   Bug warning: this xInstantiateWasm bit must remain disabled
13   until this bug is resolved or wasmfs won't work:
14
15   https://github.com/emscripten-core/emscripten/issues/17951
16*/
17const xInstantiateWasm = 1
18      ? 'emscripten-bug-17951'
19      : 'instantiateWasm';
20Module[xInstantiateWasm] = function callee(imports,onSuccess){
21  imports.env.foo = function(){};
22  console.warn("instantiateWasm() uri =",callee.uri, self.location.href);
23  const wfetch = ()=>fetch(callee.uri, {credentials: 'same-origin'});
24  const loadWasm = WebAssembly.instantiateStreaming
25        ? async ()=>{
26          return WebAssembly.instantiateStreaming(wfetch(), imports)
27            .then((arg)=>onSuccess(arg.instance, arg.module));
28        }
29        : async ()=>{ // Safari < v15
30          return wfetch()
31            .then(response => response.arrayBuffer())
32            .then(bytes => WebAssembly.instantiate(bytes, imports))
33            .then((arg)=>onSuccess(arg.instance, arg.module));
34        };
35  loadWasm();
36  return {};
37};
38/*
39  It is literally impossible to reliably get the name of _this_ script
40  at runtime, so impossible to derive X.wasm from script name
41  X.js. Thus we need, at build-time, to redefine
42  Module[xInstantiateWasm].uri by appending it to a build-specific
43  copy of this file with the name of the wasm file. This is apparently
44  why Emscripten hard-codes the name of the wasm file into their glue
45  scripts.
46*/
47Module[xInstantiateWasm].uri = 'sqlite3.wasm';
48/* END FILE: api/pre-js.js */
49