Skip to content

Commit 257a4c5

Browse files
bollukripken
authored andcommitted
Add BinaryenModuleWriteSExpr to write a module to a string in s-expr format (#2106)
Fixes #2103.
1 parent 9b5e470 commit 257a4c5

4 files changed

Lines changed: 84 additions & 0 deletions

File tree

src/binaryen-c.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
#include "wasm-validator.h"
3636
#include "wasm.h"
3737
#include "wasm2js.h"
38+
#include <iostream>
39+
#include <sstream>
3840

3941
#ifdef __EMSCRIPTEN__
4042
#include <emscripten.h>
@@ -3288,6 +3290,26 @@ BinaryenModuleWrite(BinaryenModuleRef module, char* output, size_t outputSize) {
32883290
.outputBytes;
32893291
}
32903292

3293+
size_t BinaryenModuleWriteText(BinaryenModuleRef module,
3294+
char* output,
3295+
size_t outputSize) {
3296+
3297+
if (tracing) {
3298+
std::cout << " // BinaryenModuleWriteTextr\n";
3299+
}
3300+
3301+
// use a stringstream as an std::ostream. Extract the std::string
3302+
// representation, and then store in the output.
3303+
std::stringstream ss;
3304+
WasmPrinter::printModule((Module*)module, ss);
3305+
3306+
const auto temp = ss.str();
3307+
const auto ctemp = temp.c_str();
3308+
3309+
strncpy(output, ctemp, outputSize);
3310+
return std::min(outputSize, temp.size());
3311+
}
3312+
32913313
BinaryenBufferSizes BinaryenModuleWriteWithSourceMap(BinaryenModuleRef module,
32923314
const char* url,
32933315
char* output,
@@ -3333,6 +3355,21 @@ BinaryenModuleAllocateAndWrite(BinaryenModuleRef module,
33333355
return {binary, buffer.size(), sourceMap};
33343356
}
33353357

3358+
char* BinaryenModuleAllocateAndWriteText(BinaryenModuleRef* module) {
3359+
if (tracing) {
3360+
std::cout << " // BinaryenModuleAllocateAndWriteText(the_module);";
3361+
}
3362+
3363+
std::stringstream ss;
3364+
WasmPrinter::printModule((Module*)module, ss);
3365+
3366+
const std::string out = ss.str();
3367+
const int len = out.length() + 1;
3368+
char* cout = (char*)malloc(len);
3369+
strncpy(cout, out.c_str(), len);
3370+
return cout;
3371+
}
3372+
33363373
BinaryenModuleRef BinaryenModuleRead(char* input, size_t inputSize) {
33373374
if (tracing) {
33383375
std::cout << " // BinaryenModuleRead\n";

src/binaryen-c.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,6 +1012,13 @@ void BinaryenModuleAutoDrop(BinaryenModuleRef module);
10121012
size_t
10131013
BinaryenModuleWrite(BinaryenModuleRef module, char* output, size_t outputSize);
10141014

1015+
// Serialize a module in s-expression text format.
1016+
// @return how many bytes were written. This will be less than or equal to
1017+
// outputSize
1018+
size_t BinaryenModuleWriteText(BinaryenModuleRef module,
1019+
char* output,
1020+
size_t outputSize);
1021+
10151022
typedef struct BinaryenBufferSizes {
10161023
size_t outputBytes;
10171024
size_t sourceMapBytes;
@@ -1046,6 +1053,11 @@ BinaryenModuleAllocateAndWriteResult
10461053
BinaryenModuleAllocateAndWrite(BinaryenModuleRef module,
10471054
const char* sourceMapUrl);
10481055

1056+
// Serialize a module in s-expression form. Implicity allocates the returned
1057+
// char* with malloc(), and expects the user to free() them manually
1058+
// once not needed anymore.
1059+
char* BinaryenModuleAllocateAndWriteText(BinaryenModuleRef* module);
1060+
10491061
// Deserialize a module from binary form.
10501062
BinaryenModuleRef BinaryenModuleRead(char* input, size_t inputSize);
10511063

test/example/c-api-kitchen-sink.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -785,6 +785,19 @@ void test_binaries() {
785785
assert(BinaryenModuleValidate(module));
786786
printf("module loaded from binary form:\n");
787787
BinaryenModulePrint(module);
788+
789+
// write the s-expr representation of the module.
790+
BinaryenModuleWriteText(module, buffer, 1024);
791+
printf("module s-expr printed (in memory):\n%s\n", buffer);
792+
793+
794+
// writ the s-expr representation to a pointer which is managed by the
795+
// caller
796+
char *text = BinaryenModuleAllocateAndWriteText(module);
797+
printf("module s-expr printed (in memory, caller-owned):\n%s\n", text);
798+
free(text);
799+
800+
788801
BinaryenModuleDispose(module);
789802
}
790803

test/example/c-api-kitchen-sink.txt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1875,6 +1875,28 @@ module loaded from binary form:
18751875
)
18761876
)
18771877
)
1878+
module s-expr printed (in memory):
1879+
(module
1880+
(type $0 (func (param i32 i32) (result i32)))
1881+
(func $adder (; 0 ;) (type $0) (param $0 i32) (param $1 i32) (result i32)
1882+
(i32.add
1883+
(local.get $0)
1884+
(local.get $1)
1885+
)
1886+
)
1887+
)
1888+
1889+
module s-expr printed (in memory, caller-owned):
1890+
(module
1891+
(type $0 (func (param i32 i32) (result i32)))
1892+
(func $adder (; 0 ;) (type $0) (param $0 i32) (param $1 i32) (result i32)
1893+
(i32.add
1894+
(local.get $0)
1895+
(local.get $1)
1896+
)
1897+
)
1898+
)
1899+
18781900
(module
18791901
(type $vi (func (param i32)))
18801902
(type $v (func))

0 commit comments

Comments
 (0)