Skip to content

Improved EM_JS formatting #25123

@SingleAccretion

Description

@SingleAccretion

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:

; 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):

EM_JS_STR(int, JSAdd, (int x, int y), R"({
    console.log(`In JSAdd(${x}, ${y})`);
    return x + y;
})");

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions