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