xref: /sqlite-3.40.0/ext/wasm/api/pre-js.js (revision cd0df83c)
195639269Sstephan/**
295639269Sstephan   BEGIN FILE: api/pre-js.js
395639269Sstephan
495639269Sstephan   This file is intended to be prepended to the sqlite3.js build using
595639269Sstephan   Emscripten's --pre-js=THIS_FILE flag (or equivalent).
695639269Sstephan*/
7*cd0df83cSstephan
8*cd0df83cSstephan// See notes in extern-post-js.js
9*cd0df83cSstephanconst sqlite3InitModuleState = self.sqlite3InitModuleState || Object.create(null);
10*cd0df83cSstephandelete self.sqlite3InitModuleState;
11*cd0df83cSstephan
12*cd0df83cSstephan/**
13*cd0df83cSstephan   This custom locateFile() tries to figure out where to load `path`
14*cd0df83cSstephan   from. The intent is to provide a way for foo/bar/X.js loaded from a
15*cd0df83cSstephan   Worker constructor or importScripts() to be able to resolve
16*cd0df83cSstephan   foo/bar/X.wasm (in the latter case, with some help):
17*cd0df83cSstephan
18*cd0df83cSstephan   1) If URL param named the same as `path` is set, it is returned.
19*cd0df83cSstephan
20*cd0df83cSstephan   2) If sqlite3InitModuleState.sqlite3Dir is set, then (thatName + path)
21*cd0df83cSstephan      is returned (note that it's assumed to end with '/').
22*cd0df83cSstephan
23*cd0df83cSstephan   3) If this code is running in the main UI thread AND it was loaded
24*cd0df83cSstephan      from a SCRIPT tag, the directory part of that URL is used
25*cd0df83cSstephan      as the prefix. (This form of resolution unfortunately does not
26*cd0df83cSstephan      function for scripts loaded via importScripts().)
27*cd0df83cSstephan
28*cd0df83cSstephan   4) If none of the above apply, (prefix+path) is returned.
29*cd0df83cSstephan*/
30f71c954cSstephanModule['locateFile'] = function(path, prefix) {
31*cd0df83cSstephan  let theFile;
32*cd0df83cSstephan  const up = this.urlParams;
33*cd0df83cSstephan  if(0){
34*cd0df83cSstephan    console.warn("locateFile(",arguments[0], ',', arguments[1],")",
35*cd0df83cSstephan                 'self.location =',self.location,
36*cd0df83cSstephan                 'sqlite3InitModuleState.scriptDir =',this.scriptDir,
37*cd0df83cSstephan                 'up.entries() =',Array.from(up.entries()));
38*cd0df83cSstephan  }
39*cd0df83cSstephan  if(up.has(path)){
40*cd0df83cSstephan    theFile = up.get(path);
41*cd0df83cSstephan  }else if(this.sqlite3Dir){
42*cd0df83cSstephan    theFile = this.sqlite3Dir + path;
43*cd0df83cSstephan  }else if(this.scriptDir){
44*cd0df83cSstephan    theFile = this.scriptDir + path;
45*cd0df83cSstephan  }else{
46*cd0df83cSstephan    theFile = prefix + path;
47*cd0df83cSstephan  }
48*cd0df83cSstephan  return theFile;
49*cd0df83cSstephan}.bind(sqlite3InitModuleState);
50f71c954cSstephan
51f71c954cSstephan/**
52f71c954cSstephan   Bug warning: this xInstantiateWasm bit must remain disabled
53f71c954cSstephan   until this bug is resolved or wasmfs won't work:
54f71c954cSstephan
55f71c954cSstephan   https://github.com/emscripten-core/emscripten/issues/17951
56f71c954cSstephan*/
57f71c954cSstephanconst xInstantiateWasm = 1
58f71c954cSstephan      ? 'emscripten-bug-17951'
59f71c954cSstephan      : 'instantiateWasm';
60f71c954cSstephanModule[xInstantiateWasm] = function callee(imports,onSuccess){
61f6c686c9Sstephan  imports.env.foo = function(){};
62f71c954cSstephan  console.warn("instantiateWasm() uri =",callee.uri, self.location.href);
63f71c954cSstephan  const wfetch = ()=>fetch(callee.uri, {credentials: 'same-origin'});
64f71c954cSstephan  const loadWasm = WebAssembly.instantiateStreaming
65f6c686c9Sstephan        ? async ()=>{
66f71c954cSstephan          return WebAssembly.instantiateStreaming(wfetch(), imports)
67f71c954cSstephan            .then((arg)=>onSuccess(arg.instance, arg.module));
68f71c954cSstephan        }
69f6c686c9Sstephan        : async ()=>{ // Safari < v15
70f71c954cSstephan          return wfetch()
71f71c954cSstephan            .then(response => response.arrayBuffer())
72f71c954cSstephan            .then(bytes => WebAssembly.instantiate(bytes, imports))
73f71c954cSstephan            .then((arg)=>onSuccess(arg.instance, arg.module));
74f71c954cSstephan        };
75f71c954cSstephan  loadWasm();
76f71c954cSstephan  return {};
77f71c954cSstephan};
78f71c954cSstephan/*
79f6c686c9Sstephan  It is literally impossible to reliably get the name of _this_ script
80f6c686c9Sstephan  at runtime, so impossible to derive X.wasm from script name
81f6c686c9Sstephan  X.js. Thus we need, at build-time, to redefine
82f6c686c9Sstephan  Module[xInstantiateWasm].uri by appending it to a build-specific
83f6c686c9Sstephan  copy of this file with the name of the wasm file. This is apparently
84f6c686c9Sstephan  why Emscripten hard-codes the name of the wasm file into their glue
85f6c686c9Sstephan  scripts.
86f71c954cSstephan*/
87f71c954cSstephanModule[xInstantiateWasm].uri = 'sqlite3.wasm';
8895639269Sstephan/* END FILE: api/pre-js.js */
89