summaryrefslogtreecommitdiff
path: root/src/wasm-js.cpp
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2015-10-31 12:30:30 -0700
committerAlon Zakai <alonzakai@gmail.com>2015-10-31 12:30:30 -0700
commitdcdf2e222f0b42c71ca41f7e37bc77decfef37f7 (patch)
tree5bc17c309ef4b7a8b631603a07d96df610bbbc75 /src/wasm-js.cpp
parentb39e36e0b3cc74de46d62f7448058942240555c5 (diff)
downloadbinaryen-dcdf2e222f0b42c71ca41f7e37bc77decfef37f7.tar.gz
binaryen-dcdf2e222f0b42c71ca41f7e37bc77decfef37f7.tar.bz2
binaryen-dcdf2e222f0b42c71ca41f7e37bc77decfef37f7.zip
start to sketch out interpreter/js
Diffstat (limited to 'src/wasm-js.cpp')
-rw-r--r--src/wasm-js.cpp63
1 files changed, 63 insertions, 0 deletions
diff --git a/src/wasm-js.cpp b/src/wasm-js.cpp
index ccc589485..e2ef47bbb 100644
--- a/src/wasm-js.cpp
+++ b/src/wasm-js.cpp
@@ -1,4 +1,67 @@
+//
+// wasm intepreter for asm2wasm output, in a js environment. receives asm.js,
+// generates a runnable module suitable as a polyfill.
+//
+
+#include <emscripten.h>
+
+#include "asm2wasm.h"
#include "wasm-interpreter.h"
+using namespace cashew;
+using namespace wasm;
+
+// receives asm.js code, parses into wasm and returns an instance handle.
+// this creates a module, an external interface, and a module instance,
+// all of which are then the responsibility of the caller to free.
+// note: this modifies the input.
+extern "C" ModuleInstance* EMSCRIPTEN_KEEPALIVE load_asm(char *input) {
+ // emcc --separate-asm modules look like
+ //
+ // Module["asm"] = (function(global, env, buffer) {
+ // ..
+ // });
+ //
+ // we need to clean that up.
+ size_t num = strlen(input);
+ assert(*input == 'M');
+ while (*input != 'f') {
+ input++;
+ num--;
+ }
+ char *end = input + num - 1;
+ while (*end != '}') {
+ *end = 0;
+ end--;
+ }
+
+ if (debug) std::cerr << "parsing...\n";
+ cashew::Parser<Ref, DotZeroValueBuilder> builder;
+ Ref asmjs = builder.parseToplevel(input);
+
+ Module* wasm = new Module();
+
+ if (debug) std::cerr << "wasming...\n";
+ Asm2WasmBuilder asm2wasm(*wasm);
+ asm2wasm.processAsm(asmjs);
+
+ if (debug) std::cerr << "optimizing...\n";
+ asm2wasm.optimize();
+
+ if (debug) std::cerr << "returning instance.\n";
+
+ struct JSExternalInterface : ModuleInstance::ExternalInterface {
+ Literal callImport(Import *import, ModuleInstance::LiteralList& arguments) override {
+ }
+
+ Literal load(Load* load, Literal ptr) override {
+ }
+
+ Literal store(Store* store, Literal ptr, Literal value) override {
+ }
+ };
+
+ return new ModuleInstance(*wasm, new JSExternalInterface());
+}