File: externref.cc

package info (click to toggle)
rust-wasmtime 36.0.5%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 60,580 kB
  • sloc: cpp: 5,670; ansic: 4,079; sh: 636; javascript: 608; asm: 110; ml: 96; makefile: 61; python: 12
file content (54 lines) | stat: -rw-r--r-- 1,810 bytes parent folder | download | duplicates (2)
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
49
50
51
52
53
54
#include <fstream>
#include <iostream>
#include <sstream>
#include <wasmtime.hh>

using namespace wasmtime;

std::string readFile(const char *name) {
  std::ifstream watFile;
  watFile.open(name);
  std::stringstream strStream;
  strStream << watFile.rdbuf();
  return strStream.str();
}

int main() {
  std::cout << "Initializing...\n";
  Engine engine;
  Store store(engine);

  std::cout << "Compiling module...\n";
  auto wat = readFile("examples/externref.wat");
  Module module = Module::compile(engine, wat).unwrap();
  std::cout << "Instantiating module...\n";
  Instance instance = Instance::create(store, module, {}).unwrap();

  ExternRef externref(store, std::string("Hello, world!"));
  std::any &data = externref.data(store);
  std::cout << "externref data: " << std::any_cast<std::string>(data) << "\n";

  std::cout << "Touching `externref` table..\n";
  Table table = std::get<Table>(*instance.get(store, "table"));
  table.set(store, 3, externref).unwrap();
  ExternRef val = *table.get(store, 3)->externref(store);
  std::cout << "externref data: " << std::any_cast<std::string>(val.data(store))
            << "\n";

  std::cout << "Touching `externref` global..\n";
  Global global = std::get<Global>(*instance.get(store, "global"));
  global.set(store, externref).unwrap();
  val = *global.get(store).externref(store);
  std::cout << "externref data: " << std::any_cast<std::string>(val.data(store))
            << "\n";

  std::cout << "Calling `externref` func..\n";
  Func func = std::get<Func>(*instance.get(store, "func"));
  auto results = func.call(store, {externref}).unwrap();
  val = *results[0].externref(store);
  std::cout << "externref data: " << std::any_cast<std::string>(val.data(store))
            << "\n";

  std::cout << "Running a gc..\n";
  store.context().gc();
}