Skip to content

Commit 3e32a18

Browse files
committed
test/link: add test case for function table
Adds 3 linker tests to verify the indirect function table functionality for importing, exporting and its regular definitions.
1 parent 3ca3fe9 commit 3e32a18

3 files changed

Lines changed: 75 additions & 0 deletions

File tree

test/link.zig

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ fn addWasmCases(cases: *tests.StandaloneContext) void {
5858
.requires_stage2 = true,
5959
});
6060

61+
cases.addBuildFile("test/link/wasm/function-table/build.zig", .{
62+
.build_modes = true,
63+
.requires_stage2 = true,
64+
});
65+
6166
cases.addBuildFile("test/link/wasm/infer-features/build.zig", .{
6267
.requires_stage2 = true,
6368
});
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
const std = @import("std");
2+
const Builder = std.build.Builder;
3+
4+
pub fn build(b: *Builder) void {
5+
const mode = b.standardReleaseOptions();
6+
7+
const test_step = b.step("test", "Test");
8+
test_step.dependOn(b.getInstallStep());
9+
10+
const import_table = b.addSharedLibrary("lib", "lib.zig", .unversioned);
11+
import_table.setBuildMode(mode);
12+
import_table.setTarget(.{ .cpu_arch = .wasm32, .os_tag = .freestanding });
13+
import_table.use_llvm = false;
14+
import_table.use_lld = false;
15+
import_table.import_table = true;
16+
17+
const export_table = b.addSharedLibrary("lib", "lib.zig", .unversioned);
18+
export_table.setBuildMode(mode);
19+
export_table.setTarget(.{ .cpu_arch = .wasm32, .os_tag = .freestanding });
20+
export_table.use_llvm = false;
21+
export_table.use_lld = false;
22+
export_table.export_table = true;
23+
24+
const regular_table = b.addSharedLibrary("lib", "lib.zig", .unversioned);
25+
regular_table.setBuildMode(mode);
26+
regular_table.setTarget(.{ .cpu_arch = .wasm32, .os_tag = .freestanding });
27+
regular_table.use_llvm = false;
28+
regular_table.use_lld = false;
29+
30+
const check_import = import_table.checkObject(.wasm);
31+
const check_export = export_table.checkObject(.wasm);
32+
const check_regular = regular_table.checkObject(.wasm);
33+
34+
check_import.checkStart("Section import");
35+
check_import.checkNext("entries 1");
36+
check_import.checkNext("module env");
37+
check_import.checkNext("name __indirect_function_table");
38+
check_import.checkNext("kind table");
39+
check_import.checkNext("type funcref");
40+
check_import.checkNext("min 1"); // 1 function pointer
41+
check_import.checkNotPresent("max"); // when importing, we do not provide a max
42+
check_import.checkNotPresent("Section table"); // we're importing it
43+
44+
check_export.checkStart("Section export");
45+
check_export.checkNext("entries 2");
46+
check_export.checkNext("name __indirect_function_table"); // as per linker specification
47+
check_export.checkNext("kind table");
48+
49+
check_regular.checkStart("Section table");
50+
check_regular.checkNext("entries 1");
51+
check_regular.checkNext("type funcref");
52+
check_regular.checkNext("min 2"); // index starts at 1 & 1 function pointer = 2.
53+
check_regular.checkNext("max 2");
54+
check_regular.checkStart("Section element");
55+
check_regular.checkNext("entries 1");
56+
check_regular.checkNext("table index 0");
57+
check_regular.checkNext("i32.const 1"); // we want to start function indexes at 1
58+
check_regular.checkNext("indexes 1"); // 1 function pointer
59+
60+
test_step.dependOn(&check_import.step);
61+
test_step.dependOn(&check_export.step);
62+
test_step.dependOn(&check_regular.step);
63+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
var func: *const fn () void = &bar;
2+
3+
export fn foo() void {
4+
func();
5+
}
6+
7+
fn bar() void {}

0 commit comments

Comments
 (0)