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