emscripten-core / emscripten-core/emscripten

Improved `EM_JS` formatting

Open
#25,123 8 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
27.6k
Forks
3.6k
Avg merge
1d 1h
Merged PRs (30d)
105

Description

`EM_JS` is a very useful feature for including JS-library-like code in object files (which can later be put into archives and linked fully transparently). However, it suffers from a usability issue: it is by default formatted as one large line:
```c
; input
EM_JS(int, JSAdd2, (int x, int y), {
console.log(`In JSAdd2(${x}, ${y})`);
return x + y;
});

; output
function JSAdd2(x,y) { console.log(`In JSAdd2(${x}, ${y})`); return x + y; }
```
Which makes larger functions very hard to debug / read in generated code.

I was thinking the situation could be improved; some possibilities:
1) Run an auto-formatter on these functions after the fact. This could be complex.
2) Add a new macro variation that accepts string literals instead of a token list.

I am not sure `1` is viable. `2` though seems like a simple enough change; it can be hacked together today by using the implementation macro `_EM_JS` (and indeed works as one would expect):
```c++
EM_JS_STR(int, JSAdd, (int x, int y), R"({
console.log(`In JSAdd(${x}, ${y})`);
return x + y;
})");
```

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.