aboutsummaryrefslogtreecommitdiff
path: root/test/link/wasm/function-table/build.zig
blob: 156ea788053c1f04b252d1ce9adf94e0db11f9da (plain)
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
55
56
57
58
59
60
61
62
63
64
65
66
67
const std = @import("std");

pub fn build(b: *std.Build) void {
    const test_step = b.step("test", "Test it");
    b.default_step = test_step;

    add(b, test_step, .Debug);
}

fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void {
    const export_table = b.addExecutable(.{
        .name = "export_table",
        .root_module = b.createModule(.{
            .root_source_file = b.path("lib.zig"),
            .target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }),
            .optimize = optimize,
        }),
    });
    export_table.entry = .disabled;
    export_table.use_llvm = false;
    export_table.use_lld = false;
    export_table.export_table = true;
    export_table.link_gc_sections = false;
    // Don't pull in ubsan, since we're just expecting a very minimal executable.
    export_table.bundle_ubsan_rt = false;

    const regular_table = b.addExecutable(.{
        .name = "regular_table",
        .root_module = b.createModule(.{
            .root_source_file = b.path("lib.zig"),
            .target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }),
            .optimize = optimize,
        }),
    });
    regular_table.entry = .disabled;
    regular_table.use_llvm = false;
    regular_table.use_lld = false;
    regular_table.link_gc_sections = false; // Ensure function table is not empty
    // Don't pull in ubsan, since we're just expecting a very minimal executable.
    regular_table.bundle_ubsan_rt = false;

    const check_export = export_table.checkObject();
    const check_regular = regular_table.checkObject();

    check_export.checkInHeaders();
    check_export.checkExact("Section export");
    check_export.checkExact("entries 3");
    check_export.checkExact("name __indirect_function_table"); // as per linker specification
    check_export.checkExact("kind table");

    check_regular.checkInHeaders();
    check_regular.checkExact("Section table");
    check_regular.checkExact("entries 1");
    check_regular.checkExact("type funcref");
    check_regular.checkExact("min 2"); // index starts at 1 & 1 function pointer = 2.
    check_regular.checkExact("max 2");

    check_regular.checkInHeaders();
    check_regular.checkExact("Section element");
    check_regular.checkExact("entries 1");
    check_regular.checkExact("table index 0");
    check_regular.checkExact("i32.const 1"); // we want to start function indexes at 1
    check_regular.checkExact("indexes 1"); // 1 function pointer

    test_step.dependOn(&check_export.step);
    test_step.dependOn(&check_regular.step);
}