Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
hello_driver.cpp
Go to the documentation of this file.
1#include <sysio/vm/backend.hpp>
2#include <sysio/vm/error_codes.hpp>
3#include <sysio/vm/host_function.hpp>
4#include <sysio/vm/watchdog.hpp>
5
6#include <iostream>
7#include <string>
8
9using namespace sysio;
10using namespace sysio::vm;
11
12#include "hello.wasm.hpp"
13
15 void print_num(uint64_t n) { std::cout << "Number : " << n << "\n"; }
16 // example of a host "method"
17 void print_name(const char* nm) { std::cout << "Name : " << nm << " " << field << "\n"; }
18 // example of another type of host function
19 void* memset(char* ptr, int x, size_t n) { return ::memset(ptr, x, n); }
20 std::string field = "";
21
22 void sysio_assert(bool test, const char* msg) {
23 if (!test) {
24 std::cout << msg << std::endl;
25 throw 0;
26 }
27 }
28
30 std::cout << "Span : " << std::string{s.data(), s.size()} << "\n";
31 }
32};
33
34struct cnv : type_converter<example_host_methods> {
35 using type_converter::type_converter;
37 SYS_VM_FROM_WASM(bool, (uint32_t value)) { return value ? 1 : 0; }
38 SYS_VM_FROM_WASM(char*, (void* ptr)) { return static_cast<char*>(ptr); }
39 SYS_VM_FROM_WASM(const char*, (void* ptr)) { return static_cast<char*>(ptr); }
40};
41
43 SYS_VM_INVOKE_ON(const char*, [&](auto&& nm, auto&&... rest) {
44 std::string s = nm;
45 if (s == "sys-vm2")
46 throw "failure";
47 }))
48
52int main(int argc, char** argv) {
53 if (argc < 4) {
54 std::cerr << "Please enter three numbers\n";
55 return -1;
56 }
57 // Thread specific `allocator` used for wasm linear memory.
59 // Specific the backend with example_host_methods for host functions.
62
63 // register print_num
64 rhf_t::add<&example_host_methods::print_num>("env", "print_num");
65 // register sysio_assert
66 rhf_t::add<&example_host_methods::sysio_assert>("env", "sysio_assert");
67 // register print_name
68 rhf_t::add<&example_host_methods::print_name, test_name>("env", "print_name");
69 // finally register memset
70 rhf_t::add<&example_host_methods::memset>("env", "memset");
71 rhf_t::add<&example_host_methods::print_span>("env", "print_span");
72
73 watchdog wd{std::chrono::seconds(3)};
74 try {
76 // Instaniate a new backend using the wasm provided.
78
79 // Instaniate a "host"
80 ehm.field = "testing";
81 // Execute apply.
82 bkend(ehm, "env", "apply", (uint64_t)std::atoi(argv[1]), (uint64_t)std::atoi(argv[2]),
83 (uint64_t)std::atoi(argv[3]));
84
85 } catch (...) { std::cerr << "sys-vm interpreter error\n"; }
86 return 0;
87}
wasm_allocator wa
Definition main.cpp:10
Triggers a callback after a given time elapses.
Definition watchdog.hpp:13
std::vector< uint8_t > hello_wasm
backend_t bkend(hello_wasm, ehm, &wa)
watchdog wd
char ** argv
#define value
Definition pkcs11.h:157
unsigned int uint32_t
Definition stdint.h:126
unsigned __int64 uint64_t
Definition stdint.h:136
SYS_VM_FROM_WASM(char *,(void *ptr))
SYS_VM_FROM_WASM(const char *,(void *ptr))
SYS_VM_FROM_WASM(bool,(uint32_t value))
void print_name(const char *nm)
void print_span(span< const char > s)
void sysio_assert(bool test, const char *msg)
void print_num(uint64_t n)
void * memset(char *ptr, int x, size_t n)
auto from_wasm(vm::wasm_ptr_t ptr) const -> std::enable_if_t< std::is_pointer_v< T >, vm::argument_proxy< T > >
Definition common.hpp:88
#define SYS_VM_PRECONDITION(NAME,...)
#define SYS_VM_INVOKE_ON(TYPE, CONDITION)
char * s