MCPcopy Create free account
hub / github.com/WebAssembly/wasm-c-api / run

Function run

example/memory.cc:66–167  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

64
65
66void run() {
67 // Initialize.
68 std::cout << "Initializing..." << std::endl;
69 auto engine = wasm::Engine::make();
70 auto store_ = wasm::Store::make(engine.get());
71 auto store = store_.get();
72
73 // Load binary.
74 std::cout << "Loading binary..." << std::endl;
75 std::ifstream file("memory.wasm");
76 file.seekg(0, std::ios_base::end);
77 auto file_size = file.tellg();
78 file.seekg(0);
79 auto binary = wasm::vec<byte_t>::make_uninitialized(file_size);
80 file.read(binary.get(), file_size);
81 file.close();
82 if (file.fail()) {
83 std::cout << "> Error loading module!" << std::endl;
84 exit(1);
85 }
86
87 // Compile.
88 std::cout << "Compiling module..." << std::endl;
89 auto module = wasm::Module::make(store, binary);
90 if (!module) {
91 std::cout << "> Error compiling module!" << std::endl;
92 exit(1);
93 }
94
95 // Instantiate.
96 std::cout << "Instantiating module..." << std::endl;
97 auto imports = wasm::vec<wasm::Extern*>::make();
98 auto instance = wasm::Instance::make(store, module.get(), imports);
99 if (!instance) {
100 std::cout << "> Error instantiating module!" << std::endl;
101 exit(1);
102 }
103
104 // Extract export.
105 std::cout << "Extracting exports..." << std::endl;
106 auto exports = instance->exports();
107 size_t i = 0;
108 auto memory = get_export_memory(exports, i++);
109 auto size_func = get_export_func(exports, i++);
110 auto load_func = get_export_func(exports, i++);
111 auto store_func = get_export_func(exports, i++);
112
113 // Try cloning.
114 assert(memory->copy()->same(memory));
115
116 // Check initial memory.
117 std::cout << "Checking memory..." << std::endl;
118 check(memory->size(), 2u);
119 check(memory->data_size(), 0x20000u);
120 check(memory->data()[0], 0);
121 check(memory->data()[0x1000], 1);
122 check(memory->data()[0x1003], 4);
123

Callers 1

mainFunction · 0.70

Calls 15

LimitsClass · 0.85
exportsMethod · 0.80
sameMethod · 0.80
data_sizeMethod · 0.80
dataMethod · 0.80
growMethod · 0.80
get_export_memoryFunction · 0.70
get_export_funcFunction · 0.70
checkFunction · 0.70
callFunction · 0.70
check_trapFunction · 0.70
check_okFunction · 0.70

Tested by

no test coverage detected