Skip to content

feat: add BinaryenExpressionAllocateAndWriteText#8717

Open
chharvey wants to merge 5 commits into
WebAssembly:mainfrom
chharvey:feat/8716-stringify-expression
Open

feat: add BinaryenExpressionAllocateAndWriteText#8717
chharvey wants to merge 5 commits into
WebAssembly:mainfrom
chharvey:feat/8716-stringify-expression

Conversation

@chharvey
Copy link
Copy Markdown

adds a function for returning a string representation of an expression in s-expression format.

closes #8716

@chharvey chharvey requested a review from a team as a code owner May 16, 2026 23:39
@chharvey chharvey requested review from stevenfontanella and removed request for a team May 16, 2026 23:39
Comment thread src/binaryen-c.cpp
auto str = os.str();
const size_t len = str.length() + 1;
char* output = (char*)malloc(len);
std::copy_n(str.c_str(), len, output);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

copy_n is a little strange for copying bytes, can we use strncpy or memcpy instead to convey the intent better?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm honestly not sure, i just copied the exact code from BinaryenModuleAllocateAndWriteText.

I'll defer to the C developers for what's best. I'm happy as long as it's consistent.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @stevenfontanella , but yeah, let's keep it consistent here. We can simplify all these copy_n uses separately.

Comment thread src/binaryen-c.h Outdated
// Serialize an expression in s-expression form. Implicitly allocates the returned
// char* with malloc(), and expects the user to free() them manually
// once not needed anymore.
BINARYEN_API char* BinaryenExpressionAllocateAndWriteText(BinaryenExpressionRef expr);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add a CHANGELOG.md entry for this and unit tests in test/example/c-api-kitchen-sink.c / test/example/c-api-kitchen-sink.txt.

Copy link
Copy Markdown
Member

@kripken kripken left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm with @stevenfontanella 's comment about the changelog and testing

Comment thread src/binaryen-c.cpp
auto str = os.str();
const size_t len = str.length() + 1;
char* output = (char*)malloc(len);
std::copy_n(str.c_str(), len, output);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @stevenfontanella , but yeah, let's keep it consistent here. We can simplify all these copy_n uses separately.

Comment thread src/js/binaryen.js-post.js Outdated
Comment on lines +3311 to +3313
const textPtr = BinaryenObj["_BinaryenExpressionAllocateAndWriteText"](expr);
const text = UTF8ToString(textPtr);
if (textPtr) _free(textPtr);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use try ... finally here to ensure that the pointer is freed even if UTF8ToString throws an exception? I can't find the definition of UTF8ToString so I'm not sure if that's possible, but it conveys the intent better especially in case this code changes.

Also, we typically use braces around if bodies, let's do that here.

Copy link
Copy Markdown
Author

@chharvey chharvey May 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, i again copied from the emitText for Modules but i can make the same updates to both.

Copy link
Copy Markdown
Author

@chharvey chharvey May 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also… digging into this project helped me learn a lot about Emscripten and where all this stuff like UTF8ToString comes from. see my .d.ts file here for a quasi-explanation. Looks like Emscripten declares it here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Please provide a stringify-expression function in the C libarary

3 participants