| 24 | } |
| 25 | |
| 26 | int main() { |
| 27 | std::cout << "Initializing...\n"; |
| 28 | Config config; |
| 29 | config.wasm_reference_types(true); |
| 30 | config.wasm_function_references(true); |
| 31 | config.wasm_gc(true); |
| 32 | Engine engine(std::move(config)); |
| 33 | Store store(engine); |
| 34 | |
| 35 | std::cout << "Compiling module...\n"; |
| 36 | auto wat = readFile("examples/anyref.wat"); |
| 37 | Module module = Module::compile(engine, wat).unwrap(); |
| 38 | |
| 39 | std::cout << "Instantiating module...\n"; |
| 40 | Instance instance = Instance::create(store, module, {}).unwrap(); |
| 41 | |
| 42 | std::cout << "Creating new `anyref` from i31...\n"; |
| 43 | // Create an i31ref wrapping 1234 |
| 44 | auto cx = store.context(); |
| 45 | AnyRef i31 = AnyRef::i31(cx, 1234); |
| 46 | Val anyref_val(i31); |
| 47 | auto opt_any = anyref_val.anyref(); |
| 48 | if (!opt_any || !opt_any->u31(cx) || *opt_any->u31(cx) != 1234) { |
| 49 | std::cerr << "> Error creating i31 anyref\n"; |
| 50 | return 1; |
| 51 | } |
| 52 | |
| 53 | std::cout << "Touching `anyref` table...\n"; |
| 54 | Table table = std::get<Table>(*instance.get(store, "table")); |
| 55 | table.set(store, 3, anyref_val).unwrap(); |
| 56 | auto elem_opt = table.get(store, 3); |
| 57 | if (!elem_opt) { |
| 58 | std::cerr << "> Error getting table element\n"; |
| 59 | return 1; |
| 60 | } |
| 61 | auto elem_any = elem_opt->anyref(); |
| 62 | if (!elem_any || !elem_any->u31(cx) || *elem_any->u31(cx) != 1234) { |
| 63 | std::cerr << "> Error verifying table element\n"; |
| 64 | return 1; |
| 65 | } |
| 66 | |
| 67 | std::cout << "Touching `anyref` global...\n"; |
| 68 | Global global = std::get<Global>(*instance.get(store, "global")); |
| 69 | global.set(store, anyref_val).unwrap(); |
| 70 | Val global_val = global.get(store); |
| 71 | auto global_any = global_val.anyref(); |
| 72 | if (!global_any || !global_any->u31(cx) || *global_any->u31(cx) != 1234) { |
| 73 | std::cerr << "> Error verifying global value\n"; |
| 74 | return 1; |
| 75 | } |
| 76 | |
| 77 | std::cout << "Passing `anyref` into func...\n"; |
| 78 | Func take_anyref = std::get<Func>(*instance.get(store, "take_anyref")); |
| 79 | take_anyref.call(store, {anyref_val}).unwrap(); |
| 80 | |
| 81 | std::cout << "Getting `anyref` from func...\n"; |
| 82 | Func return_anyref = std::get<Func>(*instance.get(store, "return_anyref")); |
| 83 | auto results = return_anyref.call(store, {}).unwrap(); |
nothing calls this directly
no test coverage detected