-
Notifications
You must be signed in to change notification settings - Fork 1
/
WasmFunction.cpp
46 lines (37 loc) · 1.04 KB
/
WasmFunction.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <fstream>
#include <iostream>
#include <sstream>
#include "WasmFunction.h"
WasmFunction::WasmFunction()
{
engine = new wasmtime::Engine;
store = new wasmtime::Store(*engine);
}
WasmFunction::~WasmFunction()
{
delete (store);
delete (engine);
}
std::string readFile(const std::string name)
{
std::ifstream watFile;
watFile.open(name);
std::stringstream strStream;
strStream << watFile.rdbuf();
return strStream.str();
}
void WasmFunction::runWatFile(const std::string &fileName) const
{
// Read our input file, which in this case is a wat text file.
auto wat = readFile(fileName);
runWat(wat);
}
void WasmFunction::runWat(const std::string &watString) const
{
auto module = wasmtime::Module::compile(*engine, watString).unwrap();
auto instance = wasmtime::Instance::create(store, module, {}).unwrap();
// Invoke `gcd` export
auto func = std::get<wasmtime::Func>(*instance.get(store, "main"));
auto results = func.call(store, {6, 27}).unwrap();
std::cout << "gcd(6, 27) = " << results[0].i32() << "\n";
}