summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSiddharth <siddharth.bath@tweag.io>2019-05-22 02:42:19 +0200
committerAlon Zakai <azakai@google.com>2019-05-21 17:42:19 -0700
commit257a4c5ac940fe14bb85518a78fc9dba1c78b959 (patch)
tree3f9a20ceea7b12fa77daf3df0b7c97185e5c3728
parent9b5e470bcef1c6934ae74457d8094143bad8d74a (diff)
downloadbinaryen-257a4c5ac940fe14bb85518a78fc9dba1c78b959.tar.gz
binaryen-257a4c5ac940fe14bb85518a78fc9dba1c78b959.tar.bz2
binaryen-257a4c5ac940fe14bb85518a78fc9dba1c78b959.zip
Add BinaryenModuleWriteSExpr to write a module to a string in s-expr format (#2106)
Fixes #2103.
-rw-r--r--src/binaryen-c.cpp37
-rw-r--r--src/binaryen-c.h12
-rw-r--r--test/example/c-api-kitchen-sink.c13
-rw-r--r--test/example/c-api-kitchen-sink.txt22
4 files changed, 84 insertions, 0 deletions
diff --git a/src/binaryen-c.cpp b/src/binaryen-c.cpp
index 990612829..fb8b730c7 100644
--- a/src/binaryen-c.cpp
+++ b/src/binaryen-c.cpp
@@ -35,6 +35,8 @@
#include "wasm-validator.h"
#include "wasm.h"
#include "wasm2js.h"
+#include <iostream>
+#include <sstream>
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
@@ -3288,6 +3290,26 @@ BinaryenModuleWrite(BinaryenModuleRef module, char* output, size_t outputSize) {
.outputBytes;
}
+size_t BinaryenModuleWriteText(BinaryenModuleRef module,
+ char* output,
+ size_t outputSize) {
+
+ if (tracing) {
+ std::cout << " // BinaryenModuleWriteTextr\n";
+ }
+
+ // use a stringstream as an std::ostream. Extract the std::string
+ // representation, and then store in the output.
+ std::stringstream ss;
+ WasmPrinter::printModule((Module*)module, ss);
+
+ const auto temp = ss.str();
+ const auto ctemp = temp.c_str();
+
+ strncpy(output, ctemp, outputSize);
+ return std::min(outputSize, temp.size());
+}
+
BinaryenBufferSizes BinaryenModuleWriteWithSourceMap(BinaryenModuleRef module,
const char* url,
char* output,
@@ -3333,6 +3355,21 @@ BinaryenModuleAllocateAndWrite(BinaryenModuleRef module,
return {binary, buffer.size(), sourceMap};
}
+char* BinaryenModuleAllocateAndWriteText(BinaryenModuleRef* module) {
+ if (tracing) {
+ std::cout << " // BinaryenModuleAllocateAndWriteText(the_module);";
+ }
+
+ std::stringstream ss;
+ WasmPrinter::printModule((Module*)module, ss);
+
+ const std::string out = ss.str();
+ const int len = out.length() + 1;
+ char* cout = (char*)malloc(len);
+ strncpy(cout, out.c_str(), len);
+ return cout;
+}
+
BinaryenModuleRef BinaryenModuleRead(char* input, size_t inputSize) {
if (tracing) {
std::cout << " // BinaryenModuleRead\n";
diff --git a/src/binaryen-c.h b/src/binaryen-c.h
index e287e2541..ae6747cea 100644
--- a/src/binaryen-c.h
+++ b/src/binaryen-c.h
@@ -1012,6 +1012,13 @@ void BinaryenModuleAutoDrop(BinaryenModuleRef module);
size_t
BinaryenModuleWrite(BinaryenModuleRef module, char* output, size_t outputSize);
+// Serialize a module in s-expression text format.
+// @return how many bytes were written. This will be less than or equal to
+// outputSize
+size_t BinaryenModuleWriteText(BinaryenModuleRef module,
+ char* output,
+ size_t outputSize);
+
typedef struct BinaryenBufferSizes {
size_t outputBytes;
size_t sourceMapBytes;
@@ -1046,6 +1053,11 @@ BinaryenModuleAllocateAndWriteResult
BinaryenModuleAllocateAndWrite(BinaryenModuleRef module,
const char* sourceMapUrl);
+// Serialize a module in s-expression form. Implicity allocates the returned
+// char* with malloc(), and expects the user to free() them manually
+// once not needed anymore.
+char* BinaryenModuleAllocateAndWriteText(BinaryenModuleRef* module);
+
// Deserialize a module from binary form.
BinaryenModuleRef BinaryenModuleRead(char* input, size_t inputSize);
diff --git a/test/example/c-api-kitchen-sink.c b/test/example/c-api-kitchen-sink.c
index 0efafde52..43069c865 100644
--- a/test/example/c-api-kitchen-sink.c
+++ b/test/example/c-api-kitchen-sink.c
@@ -785,6 +785,19 @@ void test_binaries() {
assert(BinaryenModuleValidate(module));
printf("module loaded from binary form:\n");
BinaryenModulePrint(module);
+
+ // write the s-expr representation of the module.
+ BinaryenModuleWriteText(module, buffer, 1024);
+ printf("module s-expr printed (in memory):\n%s\n", buffer);
+
+
+ // writ the s-expr representation to a pointer which is managed by the
+ // caller
+ char *text = BinaryenModuleAllocateAndWriteText(module);
+ printf("module s-expr printed (in memory, caller-owned):\n%s\n", text);
+ free(text);
+
+
BinaryenModuleDispose(module);
}
diff --git a/test/example/c-api-kitchen-sink.txt b/test/example/c-api-kitchen-sink.txt
index 5a4055ddb..f1eba84b7 100644
--- a/test/example/c-api-kitchen-sink.txt
+++ b/test/example/c-api-kitchen-sink.txt
@@ -1875,6 +1875,28 @@ module loaded from binary form:
)
)
)
+module s-expr printed (in memory):
+(module
+ (type $0 (func (param i32 i32) (result i32)))
+ (func $adder (; 0 ;) (type $0) (param $0 i32) (param $1 i32) (result i32)
+ (i32.add
+ (local.get $0)
+ (local.get $1)
+ )
+ )
+)
+
+module s-expr printed (in memory, caller-owned):
+(module
+ (type $0 (func (param i32 i32) (result i32)))
+ (func $adder (; 0 ;) (type $0) (param $0 i32) (param $1 i32) (result i32)
+ (i32.add
+ (local.get $0)
+ (local.get $1)
+ )
+ )
+)
+
(module
(type $vi (func (param i32)))
(type $v (func))