stephan | 9563926 | 2022-10-16 15:38:03 +0000 | [diff] [blame] | 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 | */ |
stephan | cd0df83 | 2022-10-19 04:44:58 +0000 | [diff] [blame] | 7 | |
| 8 | // See notes in extern-post-js.js |
| 9 | const sqlite3InitModuleState = self.sqlite3InitModuleState || Object.create(null); |
| 10 | delete self.sqlite3InitModuleState; |
stephan | 1fc6ffc | 2022-10-30 09:47:33 +0000 | [diff] [blame] | 11 | sqlite3InitModuleState.debugModule('self.location =',self.location); |
stephan | cd0df83 | 2022-10-19 04:44:58 +0000 | [diff] [blame] | 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 | */ |
stephan | f71c954 | 2022-09-29 22:08:22 +0000 | [diff] [blame] | 31 | Module['locateFile'] = function(path, prefix) { |
stephan | ee026c5 | 2022-11-18 02:29:59 +0000 | [diff] [blame^] | 32 | //#if SQLITE_JS_ESM |
stephan | b0ab21d | 2022-11-03 22:14:47 +0000 | [diff] [blame] | 33 | return new URL(path, import.meta.url).href; |
stephan | ee026c5 | 2022-11-18 02:29:59 +0000 | [diff] [blame^] | 34 | //#else |
stephan | b0ab21d | 2022-11-03 22:14:47 +0000 | [diff] [blame] | 35 | 'use strict'; |
stephan | cd0df83 | 2022-10-19 04:44:58 +0000 | [diff] [blame] | 36 | let theFile; |
| 37 | const up = this.urlParams; |
stephan | cd0df83 | 2022-10-19 04:44:58 +0000 | [diff] [blame] | 38 | 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 | } |
stephan | 1fc6ffc | 2022-10-30 09:47:33 +0000 | [diff] [blame] | 47 | sqlite3InitModuleState.debugModule( |
| 48 | "locateFile(",arguments[0], ',', arguments[1],")", |
| 49 | 'sqlite3InitModuleState.scriptDir =',this.scriptDir, |
| 50 | 'up.entries() =',Array.from(up.entries()), |
| 51 | "result =", theFile |
| 52 | ); |
stephan | cd0df83 | 2022-10-19 04:44:58 +0000 | [diff] [blame] | 53 | return theFile; |
stephan | ee026c5 | 2022-11-18 02:29:59 +0000 | [diff] [blame^] | 54 | //#endif /* SQLITE_JS_EMS */ |
stephan | cd0df83 | 2022-10-19 04:44:58 +0000 | [diff] [blame] | 55 | }.bind(sqlite3InitModuleState); |
stephan | f71c954 | 2022-09-29 22:08:22 +0000 | [diff] [blame] | 56 | |
| 57 | /** |
stephan | 1fc6ffc | 2022-10-30 09:47:33 +0000 | [diff] [blame] | 58 | Bug warning: a custom Module.instantiateWasm() does not work |
| 59 | in WASMFS builds: |
stephan | f71c954 | 2022-09-29 22:08:22 +0000 | [diff] [blame] | 60 | |
| 61 | https://github.com/emscripten-core/emscripten/issues/17951 |
stephan | 1fc6ffc | 2022-10-30 09:47:33 +0000 | [diff] [blame] | 62 | |
| 63 | In such builds we must disable this. |
stephan | f71c954 | 2022-09-29 22:08:22 +0000 | [diff] [blame] | 64 | */ |
stephan | 1fc6ffc | 2022-10-30 09:47:33 +0000 | [diff] [blame] | 65 | const xNameOfInstantiateWasm = true |
| 66 | ? 'instantiateWasm' |
| 67 | : 'emscripten-bug-17951'; |
| 68 | Module[xNameOfInstantiateWasm] = function callee(imports,onSuccess){ |
stephan | f6c686c | 2022-09-30 11:01:44 +0000 | [diff] [blame] | 69 | imports.env.foo = function(){}; |
stephan | 1fc6ffc | 2022-10-30 09:47:33 +0000 | [diff] [blame] | 70 | const uri = Module.locateFile( |
| 71 | callee.uri, ( |
| 72 | ('undefined'===typeof scriptDirectory/*var defined by Emscripten glue*/) |
stephan | b0ab21d | 2022-11-03 22:14:47 +0000 | [diff] [blame] | 73 | ? "" : scriptDirectory) |
stephan | 1fc6ffc | 2022-10-30 09:47:33 +0000 | [diff] [blame] | 74 | ); |
| 75 | sqlite3InitModuleState.debugModule( |
| 76 | "instantiateWasm() uri =", uri |
| 77 | ); |
| 78 | const wfetch = ()=>fetch(uri, {credentials: 'same-origin'}); |
stephan | f71c954 | 2022-09-29 22:08:22 +0000 | [diff] [blame] | 79 | const loadWasm = WebAssembly.instantiateStreaming |
stephan | f6c686c | 2022-09-30 11:01:44 +0000 | [diff] [blame] | 80 | ? async ()=>{ |
stephan | f71c954 | 2022-09-29 22:08:22 +0000 | [diff] [blame] | 81 | return WebAssembly.instantiateStreaming(wfetch(), imports) |
| 82 | .then((arg)=>onSuccess(arg.instance, arg.module)); |
| 83 | } |
stephan | f6c686c | 2022-09-30 11:01:44 +0000 | [diff] [blame] | 84 | : async ()=>{ // Safari < v15 |
stephan | f71c954 | 2022-09-29 22:08:22 +0000 | [diff] [blame] | 85 | 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 | /* |
stephan | f6c686c | 2022-09-30 11:01:44 +0000 | [diff] [blame] | 94 | 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 |
stephan | 1fc6ffc | 2022-10-30 09:47:33 +0000 | [diff] [blame] | 97 | Module[xNameOfInstantiateWasm].uri by appending it to a build-specific |
stephan | f6c686c | 2022-09-30 11:01:44 +0000 | [diff] [blame] | 98 | 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. |
stephan | f71c954 | 2022-09-29 22:08:22 +0000 | [diff] [blame] | 101 | */ |
stephan | 1fc6ffc | 2022-10-30 09:47:33 +0000 | [diff] [blame] | 102 | Module[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. */ |