-
Notifications
You must be signed in to change notification settings - Fork 3
/
example.cpp
48 lines (38 loc) · 1.39 KB
/
example.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
46
47
48
#define EXTISM_NO_JSON
#include "extism.hpp"
#include <cstring>
#include <fstream>
#include <iostream>
using namespace extism;
std::vector<uint8_t> read(const char *filename) {
std::ifstream file(filename, std::ios::binary);
return std::vector<uint8_t>((std::istreambuf_iterator<char>(file)),
std::istreambuf_iterator<char>());
}
int main(int argc, char *argv[]) {
if (argc < 2) {
std::cout << "No input file provided" << std::endl;
return 1;
}
auto wasm = read(argv[1]);
std::string tmp = "Testing";
// A lambda can be used as a host function
auto hello_world = [&tmp](CurrentPlugin plugin, void *user_data) {
std::cout << "Hello from C++" << std::endl;
std::cout << (const char *)user_data << std::endl;
std::cout << tmp << std::endl;
plugin.outputVal(0).v = plugin.inputVal(0).v;
};
std::vector<Function> functions = {
Function("hello_world", {ValType::ExtismValType_I64},
{ValType::ExtismValType_I64}, hello_world,
(void *)"Hello again!",
[](void *x) { std::cout << "Free user data" << std::endl; }),
};
Plugin plugin(wasm, true, functions);
const char *input = argc > 1 ? argv[1] : "this is a test";
size_t length = strlen(input);
extism::Buffer output = plugin.call("count_vowels", (uint8_t *)input, length);
std::cout << (char *)output.data << std::endl;
return 0;
}