diff options
Diffstat (limited to 'test/example/c-api-kitchen-sink.c')
-rw-r--r-- | test/example/c-api-kitchen-sink.c | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/test/example/c-api-kitchen-sink.c b/test/example/c-api-kitchen-sink.c index ee32725c1..4a63e931d 100644 --- a/test/example/c-api-kitchen-sink.c +++ b/test/example/c-api-kitchen-sink.c @@ -377,8 +377,38 @@ void test_relooper() { BinaryenModuleDispose(module); } +void test_binaries() { + char buffer[1024]; + size_t size; + + { // create a module and write it to binary + BinaryenModuleRef module = BinaryenModuleCreate(); + BinaryenType params[2] = { BinaryenInt32(), BinaryenInt32() }; + BinaryenFunctionTypeRef iii = BinaryenAddFunctionType(module, "iii", BinaryenInt32(), params, 2); + BinaryenExpressionRef x = BinaryenGetLocal(module, 0, BinaryenInt32()), + y = BinaryenGetLocal(module, 1, BinaryenInt32()); + BinaryenExpressionRef add = BinaryenBinary(module, BinaryenAdd(), x, y); + BinaryenFunctionRef adder = BinaryenAddFunction(module, "adder", iii, NULL, 0, add); + size = BinaryenModuleWrite(module, buffer, 1024); // write out the module + BinaryenModuleDispose(module); + } + + assert(size > 0); + assert(size < 512); // this is a tiny module + + // read the module from the binary + BinaryenModuleRef module = BinaryenModuleRead(buffer, size); + + // validate, print, and free + assert(BinaryenModuleValidate(module)); + printf("module loaded from binary form:\n"); + BinaryenModulePrint(module); + BinaryenModuleDispose(module); +} + int main() { test_core(); test_relooper(); + test_binaries(); } |