master
 1const std = @import("std");
 2
 3pub fn build(b: *std.Build) void {
 4    const test_step = b.step("test", "Test it");
 5    b.default_step = test_step;
 6
 7    add(b, test_step, .Debug);
 8}
 9
10fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void {
11    const export_table = b.addExecutable(.{
12        .name = "export_table",
13        .root_module = b.createModule(.{
14            .root_source_file = b.path("lib.zig"),
15            .target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }),
16            .optimize = optimize,
17        }),
18    });
19    export_table.entry = .disabled;
20    export_table.use_llvm = false;
21    export_table.use_lld = false;
22    export_table.export_table = true;
23    export_table.link_gc_sections = false;
24    // Don't pull in ubsan, since we're just expecting a very minimal executable.
25    export_table.bundle_ubsan_rt = false;
26
27    const regular_table = b.addExecutable(.{
28        .name = "regular_table",
29        .root_module = b.createModule(.{
30            .root_source_file = b.path("lib.zig"),
31            .target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }),
32            .optimize = optimize,
33        }),
34    });
35    regular_table.entry = .disabled;
36    regular_table.use_llvm = false;
37    regular_table.use_lld = false;
38    regular_table.link_gc_sections = false; // Ensure function table is not empty
39    // Don't pull in ubsan, since we're just expecting a very minimal executable.
40    regular_table.bundle_ubsan_rt = false;
41
42    const check_export = export_table.checkObject();
43    const check_regular = regular_table.checkObject();
44
45    check_export.checkInHeaders();
46    check_export.checkExact("Section export");
47    check_export.checkExact("entries 3");
48    check_export.checkExact("name __indirect_function_table"); // as per linker specification
49    check_export.checkExact("kind table");
50
51    check_regular.checkInHeaders();
52    check_regular.checkExact("Section table");
53    check_regular.checkExact("entries 1");
54    check_regular.checkExact("type funcref");
55    check_regular.checkExact("min 2"); // index starts at 1 & 1 function pointer = 2.
56    check_regular.checkExact("max 2");
57
58    check_regular.checkInHeaders();
59    check_regular.checkExact("Section element");
60    check_regular.checkExact("entries 1");
61    check_regular.checkExact("table index 0");
62    check_regular.checkExact("i32.const 1"); // we want to start function indexes at 1
63    check_regular.checkExact("indexes 1"); // 1 function pointer
64
65    test_step.dependOn(&check_export.step);
66    test_step.dependOn(&check_regular.step);
67}