Skip to content

Instantly share code, notes, and snippets.

@carlopi
Created March 21, 2021 19:47
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carlopi/74d7dcb986897139d447564d85ced63f to your computer and use it in GitHub Desktop.
Save carlopi/74d7dcb986897139d447564d85ced63f to your computer and use it in GitHub Desktop.
Skeleton of Jit implementation with Cheerp
namespace [[cheerp::genericjs]] client {
//forward declare a bunch of JavaScript APIs existing on the browser
namespace WebAssembly {
public:
Promise* instantiate(ArrayBuffer* code, ImportObject* imports);
};
}
[[cheerp::genericjs]] void onSuccess(ModuleAndInstance* res) {
auto start_function = res->get_instance()->get_start_function();
start_function();
}
[[cheerp::genericjs]] void onFailure(Error* error){
//do something
}
[[cheerp::genericjs]] void compile_and_execute(const ByteCode& bytecode) {
ArrayBuffer*array = new ArrayBuffer(bytecode.size());
for (int i=0; i<bytecode.size(); i++) array->set(i, bytecode[i]);
ImportObject * imports = new Object();
//initialize imports with whatever method needed
Promise* result = WebAssembly::instantiate(array, imports);
result->then( onSuccess, onFailure );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment