emscripten-core / emscripten-core/emscripten
Emscripten doesn't truly produce standalone/WASI output when requested
- Dominant language
- C++
- Stars
- 27.6k
- Forks
- 3.6k
- Avg merge
- 1d 1h
- Merged PRs (30d)
- 105
Description
# Overview
Emscripten compiler chain doesn't seem to honor `-sSTANDALONE_WASM=1 -sPURE_WASI=1`
* EMSDK version 3.1.47
* Related ticket in wasmer project: [link](https://github.com/wasmerio/wasmer/issues/4261)
# Description
See related `wasmer` ticket above for more info.
When compiling a toy "hello world" project using the above flags, I am able to compile an example program `say_example`, and run via wasmer, using the command `wasmer run say_example.wasm `, and I get the expected output.
However, when compiling an executable and linking against `gtest` (which I'm building from source in this project & implementing my own `main` function), I instead get the following error when running `wasmer run test_say.wasm`:
```
error: Instantiation failed
╰─▶ 1: Error while importing "env"."invoke_iii": unknown import. Expected Function(FunctionType { params: [I32, I32, I32], results: [I32] })
```
If I inspect `test_say.js` I see the `wasmImports` variable maps `invoke_iii` to a JS function implemented further down in the file. This seems to suggest that Emscripten isn't producing truly standalone webassembly.
# Source Code
#### `say.hpp`
```c++
#pragma once
#include
namespace wpe {
size_t say(const std::string& message);
} // namespace wpe
```
#### `say.cpp`
```c++
#include
#include
namespace wpe {
size_t say(const std::string& message) {
std::cout << message << std::endl;
return message.length();
}
} // namespace wpe
```
#### `say_example.cpp`
```c++
#include
int main(int argc, char** argv) {
std::string what;
for (int i = 1; i < argc; ++i) {
what += argv[i];
if (i < argc - 1) {
what += " ";
}
}
wpe::say(what);
return 0;
}
```
#### `test_say.cpp`
```c++
#include
#include
TEST(test_wpe, say) {
std::string what = "Hello, world!";
auto len = wpe::say(what);
TEST_EQ(what.length(), len);
}
```
#### `test_main.cpp`
```c++
#include
int main(int argc, char** argv) {
::testing::InitGoogleTest();
return RUN_ALL_TESTS();
}
```
Contributor guide
Assessment
This issue has not been assessed yet.