xref: /sqlite-3.40.0/ext/wasm/api/pre-js.js (revision 1fc6ffcc)
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*/
7
8// See notes in extern-post-js.js
9const sqlite3InitModuleState = self.sqlite3InitModuleState || Object.create(null);
10delete self.sqlite3InitModuleState;
11sqlite3InitModuleState.debugModule('self.location =',self.location);
12
13/**
14   This custom locateFile() tries to figure out where to load `path`
15   from. The intent is to provide a way for foo/bar/X.js loaded from a
16   Worker constructor or importScripts() to be able to resolve
17   foo/bar/X.wasm (in the latter case, with some help):
18
19   1) If URL param named the same as `path` is set, it is returned.
20
21   2) If sqlite3InitModuleState.sqlite3Dir is set, then (thatName + path)
22      is returned (note that it's assumed to end with '/').
23
24   3) If this code is running in the main UI thread AND it was loaded
25      from a SCRIPT tag, the directory part of that URL is used
26      as the prefix. (This form of resolution unfortunately does not
27      function for scripts loaded via importScripts().)
28
29   4) If none of the above apply, (prefix+path) is returned.
30*/
31Module['locateFile'] = function(path, prefix) {
32  let theFile;
33  const up = this.urlParams;
34  if(up.has(path)){
35    theFile = up.get(path);
36  }else if(this.sqlite3Dir){
37    theFile = this.sqlite3Dir + path;
38  }else if(this.scriptDir){
39    theFile = this.scriptDir + path;
40  }else{
41    theFile = prefix + path;
42  }
43  sqlite3InitModuleState.debugModule(
44    "locateFile(",arguments[0], ',', arguments[1],")",
45    'sqlite3InitModuleState.scriptDir =',this.scriptDir,
46    'up.entries() =',Array.from(up.entries()),
47    "result =", theFile
48  );
49  return theFile;
50}.bind(sqlite3InitModuleState);
51
52/**
53   Bug warning: a custom Module.instantiateWasm() does not work
54   in WASMFS builds:
55
56   https://github.com/emscripten-core/emscripten/issues/17951
57
58   In such builds we must disable this.
59*/
60const xNameOfInstantiateWasm = true
61      ? 'instantiateWasm'
62      : 'emscripten-bug-17951';
63Module[xNameOfInstantiateWasm] = function callee(imports,onSuccess){
64  imports.env.foo = function(){};
65  const uri = Module.locateFile(
66    callee.uri, (
67      ('undefined'===typeof scriptDirectory/*var defined by Emscripten glue*/)
68        ? '' : scriptDirectory)
69  );
70  sqlite3InitModuleState.debugModule(
71    "instantiateWasm() uri =", uri
72  );
73  const wfetch = ()=>fetch(uri, {credentials: 'same-origin'});
74  const loadWasm = WebAssembly.instantiateStreaming
75        ? async ()=>{
76          return WebAssembly.instantiateStreaming(wfetch(), imports)
77            .then((arg)=>onSuccess(arg.instance, arg.module));
78        }
79        : async ()=>{ // Safari < v15
80          return wfetch()
81            .then(response => response.arrayBuffer())
82            .then(bytes => WebAssembly.instantiate(bytes, imports))
83            .then((arg)=>onSuccess(arg.instance, arg.module));
84        };
85  loadWasm();
86  return {};
87};
88/*
89  It is literally impossible to reliably get the name of _this_ script
90  at runtime, so impossible to derive X.wasm from script name
91  X.js. Thus we need, at build-time, to redefine
92  Module[xNameOfInstantiateWasm].uri by appending it to a build-specific
93  copy of this file with the name of the wasm file. This is apparently
94  why Emscripten hard-codes the name of the wasm file into their glue
95  scripts.
96*/
97Module[xNameOfInstantiateWasm].uri = 'sqlite3.wasm';
98/* END FILE: api/pre-js.js, noting that the build process may add a
99   line after this one to change the above .uri to a build-specific
100   one. */
101