emscripten-core / emscripten-core/emscripten

No list of possible EXPORTED_RUNTIME_METHODS available in documentation.

Open
#22,162 4 comments 1 reaction 0 assignees View on GitHub
Dominant language
C++
Stars
27.6k
Forks
3.6k
Avg merge
1d 1h
Merged PRs (30d)
105

Description

I have gone over the emscripten documentation several times by now and the documentation always seems incomplete.
Let's take an example code I used recently.

`main.c`
```c
#include
#include

EMSCRIPTEN_KEEPALIVE
void sayName(char* string) {
printf("%s\n", string);
}
```

Now, I want to pass a string from Javascript side to the function `sayName`

I used the following command to compile main.c because I'm using it in a svelte app.
`emcc main.cpp -o test.js -sMODULARIZE -s EXPORT_ES6=1 -s ALLOW_MEMORY_GROWTH=1 -s EXPORT_ALL=1 -lembind --emit-tsd test.d.ts`

I used the following code in the `App.svelte` file
```svelte

import Module from "./test.js";
import type { MainModule } from "./test";
let m: MainModule;
Module().then((module) => {
m = module;
console.log(m);
const name = "svelte app";
const namePtr = m.stringToNewUTF8(name);
m._sayName(namePtr);
});

```

According to what's described in the [documentation](https://emscripten.org/docs/porting/connecting_cpp_and_javascript/Interacting-with-code.html?highlight=stringtonewutf8#call-compiled-c-c-code-directly-from-javascript), this should work.

But instead, I get this error
```javascript
TypeError: m.stringToNewUTF8 is not a function
```

I look up the error and find out the correct solution in [this issue](https://github.com/emscripten-core/emscripten/issues/20096) because the entire emscripten documentation doesn't mention that `stringToNewUTF8` is supposed to be passed as a command line parameter `-sEXPORTED_RUNTIME_METHODS="stringToNewUTF8"`

![image](https://github.com/emscripten-core/emscripten/assets/98010443/96562fc8-2720-4789-91ab-28c0a8822d25)

Since I was printing the Module object (`m` in my case) in the js code, I found that all exported functions appear in that object. So to test it out, I try exporting another function, `readPointer` with the command line parameter: `sEXPORTED_RUNTIME_METHODS="['stringToNewUTF8','readPointer']"`
And I get
`warning: invalid item in EXPORTED_RUNTIME_METHODS: readPointer`

How do I know which item is valid?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.