blob: c6d0683ff4841ab818aab99b1c228a6b68f3a784 [file] [log] [blame]
stephan95639262022-10-16 15:38:03 +00001/**
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*/
stephancd0df832022-10-19 04:44:58 +00007
8// See notes in extern-post-js.js
9const sqlite3InitModuleState = self.sqlite3InitModuleState || Object.create(null);
10delete self.sqlite3InitModuleState;
stephan1fc6ffc2022-10-30 09:47:33 +000011sqlite3InitModuleState.debugModule('self.location =',self.location);
stephancd0df832022-10-19 04:44:58 +000012
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*/
stephanf71c9542022-09-29 22:08:22 +000031Module['locateFile'] = function(path, prefix) {
stephanee026c52022-11-18 02:29:59 +000032//#if SQLITE_JS_ESM
stephanb0ab21d2022-11-03 22:14:47 +000033 return new URL(path, import.meta.url).href;
stephanee026c52022-11-18 02:29:59 +000034//#else
stephanb0ab21d2022-11-03 22:14:47 +000035 'use strict';
stephancd0df832022-10-19 04:44:58 +000036 let theFile;
37 const up = this.urlParams;
stephancd0df832022-10-19 04:44:58 +000038 if(up.has(path)){
39 theFile = up.get(path);
40 }else if(this.sqlite3Dir){
41 theFile = this.sqlite3Dir + path;
42 }else if(this.scriptDir){
43 theFile = this.scriptDir + path;
44 }else{
45 theFile = prefix + path;
46 }
stephan1fc6ffc2022-10-30 09:47:33 +000047 sqlite3InitModuleState.debugModule(
48 "locateFile(",arguments[0], ',', arguments[1],")",
49 'sqlite3InitModuleState.scriptDir =',this.scriptDir,
50 'up.entries() =',Array.from(up.entries()),
51 "result =", theFile
52 );
stephancd0df832022-10-19 04:44:58 +000053 return theFile;
stephanee026c52022-11-18 02:29:59 +000054//#endif /* SQLITE_JS_EMS */
stephancd0df832022-10-19 04:44:58 +000055}.bind(sqlite3InitModuleState);
stephanf71c9542022-09-29 22:08:22 +000056
57/**
stephan1fc6ffc2022-10-30 09:47:33 +000058 Bug warning: a custom Module.instantiateWasm() does not work
59 in WASMFS builds:
stephanf71c9542022-09-29 22:08:22 +000060
61 https://github.com/emscripten-core/emscripten/issues/17951
stephan1fc6ffc2022-10-30 09:47:33 +000062
63 In such builds we must disable this.
stephanf71c9542022-09-29 22:08:22 +000064*/
stephan1fc6ffc2022-10-30 09:47:33 +000065const xNameOfInstantiateWasm = true
66 ? 'instantiateWasm'
67 : 'emscripten-bug-17951';
68Module[xNameOfInstantiateWasm] = function callee(imports,onSuccess){
stephanf6c686c2022-09-30 11:01:44 +000069 imports.env.foo = function(){};
stephan1fc6ffc2022-10-30 09:47:33 +000070 const uri = Module.locateFile(
71 callee.uri, (
72 ('undefined'===typeof scriptDirectory/*var defined by Emscripten glue*/)
stephanb0ab21d2022-11-03 22:14:47 +000073 ? "" : scriptDirectory)
stephan1fc6ffc2022-10-30 09:47:33 +000074 );
75 sqlite3InitModuleState.debugModule(
76 "instantiateWasm() uri =", uri
77 );
78 const wfetch = ()=>fetch(uri, {credentials: 'same-origin'});
stephanf71c9542022-09-29 22:08:22 +000079 const loadWasm = WebAssembly.instantiateStreaming
stephanf6c686c2022-09-30 11:01:44 +000080 ? async ()=>{
stephanf71c9542022-09-29 22:08:22 +000081 return WebAssembly.instantiateStreaming(wfetch(), imports)
82 .then((arg)=>onSuccess(arg.instance, arg.module));
83 }
stephanf6c686c2022-09-30 11:01:44 +000084 : async ()=>{ // Safari < v15
stephanf71c9542022-09-29 22:08:22 +000085 return wfetch()
86 .then(response => response.arrayBuffer())
87 .then(bytes => WebAssembly.instantiate(bytes, imports))
88 .then((arg)=>onSuccess(arg.instance, arg.module));
89 };
90 loadWasm();
91 return {};
92};
93/*
stephanf6c686c2022-09-30 11:01:44 +000094 It is literally impossible to reliably get the name of _this_ script
95 at runtime, so impossible to derive X.wasm from script name
96 X.js. Thus we need, at build-time, to redefine
stephan1fc6ffc2022-10-30 09:47:33 +000097 Module[xNameOfInstantiateWasm].uri by appending it to a build-specific
stephanf6c686c2022-09-30 11:01:44 +000098 copy of this file with the name of the wasm file. This is apparently
99 why Emscripten hard-codes the name of the wasm file into their glue
100 scripts.
stephanf71c9542022-09-29 22:08:22 +0000101*/
stephan1fc6ffc2022-10-30 09:47:33 +0000102Module[xNameOfInstantiateWasm].uri = 'sqlite3.wasm';
103/* END FILE: api/pre-js.js, noting that the build process may add a
104 line after this one to change the above .uri to a build-specific
105 one. */