diff options
author | Alon Zakai <alonzakai@gmail.com> | 2016-06-25 12:03:02 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-06-25 12:03:02 -0700 |
commit | c410d93d3af9813f889b4011f964d4becf43bc27 (patch) | |
tree | e6f3f330d5dd44755ab065a552403c957f96583c | |
parent | a88f8f275e068d6cdfb8879dc3bab1fe3201efbc (diff) | |
download | binaryen-c410d93d3af9813f889b4011f964d4becf43bc27.tar.gz binaryen-c410d93d3af9813f889b4011f964d4becf43bc27.tar.bz2 binaryen-c410d93d3af9813f889b4011f964d4becf43bc27.zip |
add c api method to interpret a module, calling its start method (#601)
-rw-r--r-- | src/binaryen-c.cpp | 8 | ||||
-rw-r--r-- | src/binaryen-c.h | 5 | ||||
-rw-r--r-- | test/example/c-api-kitchen-sink.c | 21 | ||||
-rw-r--r-- | test/example/c-api-kitchen-sink.txt | 13 |
4 files changed, 47 insertions, 0 deletions
diff --git a/src/binaryen-c.cpp b/src/binaryen-c.cpp index 4dff289fb..a9a22ab34 100644 --- a/src/binaryen-c.cpp +++ b/src/binaryen-c.cpp @@ -23,9 +23,11 @@ #include "wasm.h" #include "wasm-binary.h" #include "wasm-builder.h" +#include "wasm-interpreter.h" #include "wasm-printing.h" #include "wasm-validator.h" #include "cfg/Relooper.h" +#include "shell-interface.h" using namespace wasm; @@ -484,6 +486,12 @@ BinaryenModuleRef BinaryenModuleRead(char* input, size_t inputSize) { return wasm; } +void BinaryenModuleInterpret(BinaryenModuleRef module) { + Module* wasm = (Module*)module; + ShellExternalInterface interface; + ModuleInstance instance(*wasm, &interface); +} + // // ========== CFG / Relooper ========== // diff --git a/src/binaryen-c.h b/src/binaryen-c.h index a587e03ac..7403d0e81 100644 --- a/src/binaryen-c.h +++ b/src/binaryen-c.h @@ -344,6 +344,11 @@ size_t BinaryenModuleWrite(BinaryenModuleRef module, char* output, size_t output // Deserialize a module from binary form. BinaryenModuleRef BinaryenModuleRead(char* input, size_t inputSize); +// Execute a module in the Binaryen interpreter. This will create an instance of +// the module, run it in the interpreter - which means running the start method - +// and then destroying the instance. +void BinaryenModuleInterpret(BinaryenModuleRef module); + // // ========== CFG / Relooper ========== // diff --git a/test/example/c-api-kitchen-sink.c b/test/example/c-api-kitchen-sink.c index 84f04b45a..feffc9d4c 100644 --- a/test/example/c-api-kitchen-sink.c +++ b/test/example/c-api-kitchen-sink.c @@ -433,8 +433,29 @@ void test_binaries() { BinaryenModuleDispose(module); } +void test_interpret() { + // create a simple module with a start method that prints a number, and interpret it, printing that number. + BinaryenModuleRef module = BinaryenModuleCreate(); + + BinaryenType iparams[2] = { BinaryenInt32() }; + BinaryenFunctionTypeRef vi = BinaryenAddFunctionType(module, "vi", BinaryenNone(), iparams, 1); + BinaryenAddImport(module, "print-i32", "spectest", "print", vi); + + BinaryenFunctionTypeRef v = BinaryenAddFunctionType(module, "v", BinaryenNone(), NULL, 0); + BinaryenExpressionRef callOperands[] = { makeInt32(module, 1234) }; + BinaryenExpressionRef call = BinaryenCallImport(module, "print-i32", callOperands, 1, BinaryenNone()); + BinaryenFunctionRef starter = BinaryenAddFunction(module, "starter", v, NULL, 0, call); + BinaryenSetStart(module, starter); + + BinaryenModulePrint(module); + assert(BinaryenModuleValidate(module)); + BinaryenModuleInterpret(module); + BinaryenModuleDispose(module); +} + int main() { test_core(); test_relooper(); test_binaries(); + test_interpret(); } diff --git a/test/example/c-api-kitchen-sink.txt b/test/example/c-api-kitchen-sink.txt index e4973ba16..6776fe2a5 100644 --- a/test/example/c-api-kitchen-sink.txt +++ b/test/example/c-api-kitchen-sink.txt @@ -664,3 +664,16 @@ module loaded from binary form: ) ) ) +(module + (memory 0) + (start $starter) + (type $vi (func (param i32))) + (type $v (func)) + (import $print-i32 "spectest" "print" (param i32)) + (func $starter (type $v) + (call_import $print-i32 + (i32.const 1234) + ) + ) +) +(i32.const 1234) |