emscripten-core / emscripten-core/emscripten
Can't use --pre-js with -s MODULARIZE=1 and --preload-file
- Dominant language
- C++
- Stars
- 27.6k
- Forks
- 3.6k
- Avg merge
- 1d 1h
- Merged PRs (30d)
- 105
Description
There is a problem when you try to use --pre-js to define custom path for preloaded file. --pre-js code can be appended after function that download preload file called. So you can't change path for the file (even there is call of locateFile in download function, that function defined after download function)
My example
```js
(function() {
if (Module["ENVIRONMENT_IS_PTHREAD"]) return;
var loadPackage = function(metadata) {
var PACKAGE_PATH = "";
if (typeof window === "object") {
PACKAGE_PATH = window["encodeURIComponent"](window.location.pathname.toString().substring(0, window.location.pathname.toString().lastIndexOf("/")) + "/");
} else if (typeof process === "undefined" && typeof location !== "undefined") {
PACKAGE_PATH = encodeURIComponent(location.pathname.toString().substring(0, location.pathname.toString().lastIndexOf("/")) + "/");
}
var PACKAGE_NAME = "spotter.data";
var REMOTE_PACKAGE_BASE = "spotter.data";
if (typeof Module["locateFilePackage"] === "function" && !Module["locateFile"]) {
Module["locateFile"] = Module["locateFilePackage"];
err("warning: you defined Module.locateFilePackage, that has been renamed to Module.locateFile (using your locateFilePackage for now)");
}
var REMOTE_PACKAGE_NAME = Module["locateFile"] ? Module["locateFile"](REMOTE_PACKAGE_BASE, "") : REMOTE_PACKAGE_BASE;
var REMOTE_PACKAGE_SIZE = metadata["remote_package_size"];
var PACKAGE_UUID = metadata["package_uuid"];
function fetchRemotePackage(packageName, packageSize, callback, errback) {
if (typeof process === "object" && typeof process.versions === "object" && typeof process.versions.node === "string") {
require("fs").readFile(packageName, function(err, contents) {
if (err) {
errback(err);
} else {
callback(contents.buffer);
}
});
return;
}
var xhr = new XMLHttpRequest();
...
// in some code below
...
Module["locateFile"] = function(prefix, path) {
...
}
```
I fixed it with patch of .js file, but I think that download function should be called after --pre-js content.
**Version of emscripten/emsdk:**
emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.3 (c06156773675b2cb437e41459c12d44bd9a8f235)
clang version 14.0.0 (https://github.com/llvm/llvm-project d8f929a567083a6b90264193f1e4476f6b77c5fe)
Target: wasm32-unknown-emscripten
Thread model: posix
**Full link command and output with `-v` appended:**
em++ --preload-file ../../models/stargate@/ -s MODULARIZE=1 -s 'EXPORT_NAME="createMyModule"' --pre-js pre.js -s ALLOW_TABLE_GROWTH -s PTHREAD_POOL_SIZE=5 -s TOTAL_MEMORY=1000MB -fsanitize=address -g -s USE_PTHREADS=1 -flto
Contributor guide
Assessment
This issue has not been assessed yet.