2019-04-17 17:27:15 +00:00
|
|
|
#include <iostream>
|
|
|
|
#include <fstream>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <string>
|
|
|
|
#include <cinttypes>
|
|
|
|
|
|
|
|
#include "wasm.hh"
|
|
|
|
|
|
|
|
|
|
|
|
// A function to be called from Wasm code.
|
|
|
|
auto hello_callback(
|
|
|
|
const wasm::Val args[], wasm::Val results[]
|
2019-08-19 14:35:47 +00:00
|
|
|
) -> wasm::own<wasm::Trap> {
|
2019-04-17 17:27:15 +00:00
|
|
|
std::cout << "Calling back..." << std::endl;
|
|
|
|
std::cout << "> Hello world!" << std::endl;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void run() {
|
|
|
|
// Initialize.
|
|
|
|
std::cout << "Initializing..." << std::endl;
|
|
|
|
auto engine = wasm::Engine::make();
|
|
|
|
auto store_ = wasm::Store::make(engine.get());
|
|
|
|
auto store = store_.get();
|
|
|
|
|
|
|
|
// Load binary.
|
|
|
|
std::cout << "Loading binary..." << std::endl;
|
|
|
|
std::ifstream file("hello.wasm");
|
|
|
|
file.seekg(0, std::ios_base::end);
|
|
|
|
auto file_size = file.tellg();
|
|
|
|
file.seekg(0);
|
|
|
|
auto binary = wasm::vec<byte_t>::make_uninitialized(file_size);
|
|
|
|
file.read(binary.get(), file_size);
|
|
|
|
file.close();
|
|
|
|
if (file.fail()) {
|
|
|
|
std::cout << "> Error loading module!" << std::endl;
|
2019-08-09 13:43:42 +00:00
|
|
|
exit(1);
|
2019-04-17 17:27:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Compile.
|
|
|
|
std::cout << "Compiling module..." << std::endl;
|
|
|
|
auto module = wasm::Module::make(store, binary);
|
|
|
|
if (!module) {
|
|
|
|
std::cout << "> Error compiling module!" << std::endl;
|
2019-08-09 13:43:42 +00:00
|
|
|
exit(1);
|
2019-04-17 17:27:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create external print functions.
|
|
|
|
std::cout << "Creating callback..." << std::endl;
|
|
|
|
auto hello_type = wasm::FuncType::make(
|
2019-08-19 14:35:47 +00:00
|
|
|
wasm::ownvec<wasm::ValType>::make(), wasm::ownvec<wasm::ValType>::make()
|
2019-04-17 17:27:15 +00:00
|
|
|
);
|
|
|
|
auto hello_func = wasm::Func::make(store, hello_type.get(), hello_callback);
|
|
|
|
|
|
|
|
// Instantiate.
|
|
|
|
std::cout << "Instantiating module..." << std::endl;
|
|
|
|
wasm::Extern* imports[] = {hello_func.get()};
|
|
|
|
auto instance = wasm::Instance::make(store, module.get(), imports);
|
|
|
|
if (!instance) {
|
|
|
|
std::cout << "> Error instantiating module!" << std::endl;
|
2019-08-09 13:43:42 +00:00
|
|
|
exit(1);
|
2019-04-17 17:27:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Extract export.
|
|
|
|
std::cout << "Extracting export..." << std::endl;
|
|
|
|
auto exports = instance->exports();
|
|
|
|
if (exports.size() == 0 || exports[0]->kind() != wasm::EXTERN_FUNC || !exports[0]->func()) {
|
|
|
|
std::cout << "> Error accessing export!" << std::endl;
|
2019-08-09 13:43:42 +00:00
|
|
|
exit(1);
|
2019-04-17 17:27:15 +00:00
|
|
|
}
|
|
|
|
auto run_func = exports[0]->func();
|
|
|
|
|
|
|
|
// Call.
|
|
|
|
std::cout << "Calling export..." << std::endl;
|
|
|
|
if (run_func->call()) {
|
|
|
|
std::cout << "> Error calling function!" << std::endl;
|
2019-08-09 13:43:42 +00:00
|
|
|
exit(1);
|
2019-04-17 17:27:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Shut down.
|
|
|
|
std::cout << "Shutting down..." << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, const char* argv[]) {
|
|
|
|
run();
|
|
|
|
std::cout << "Done." << std::endl;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|