authorgravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2025-01-22 21:15:25+01:00
committergravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2025-01-22 21:18:15+01:00
log68c6a88770420b897467073ec328ddc2bb3317d6
treed47cba01cbebf77181d4da5b932695f02fd824ad
parent35e804bfb8c57e29eda8ecb18f671b720f469af4
signaturebadge-check Signed by SSH key SHA256:7B/LJ7bpR1eX8aCXSr4mtd5M45VMPKcx9zY8e95b5QM

link.Wasm.Feature: Make fromCpuFeature() and toCpuFeature() less cute.

This is more verbose, but at least we now get a compile error instead of UB when a new feature is added to std.Target.wasm.Feature but not to link.Wasm.Feature.

1 files changed, 35 insertions(+), 5 deletions(-)

src/link/Wasm.zig+35-5
......@@ -2836,14 +2836,44 @@ pub const Feature = packed struct(u8) {
28362836 @"shared-mem",
28372837
28382838 pub fn fromCpuFeature(feature: std.Target.wasm.Feature) Tag {
2839 return @enumFromInt(@intFromEnum(feature));
2839 return switch (feature) {
2840 .atomics => .atomics,
2841 .bulk_memory => .@"bulk-memory",
2842 .exception_handling => .@"exception-handling",
2843 .extended_const => .@"extended-const",
2844 .half_precision => .@"half-precision",
2845 .multimemory => .multimemory,
2846 .multivalue => .multivalue,
2847 .mutable_globals => .@"mutable-globals",
2848 .nontrapping_bulk_memory_len0 => .@"nontrapping-bulk-memory-len0", // Zig extension.
2849 .nontrapping_fptoint => .@"nontrapping-fptoint",
2850 .reference_types => .@"reference-types",
2851 .relaxed_simd => .@"relaxed-simd",
2852 .sign_ext => .@"sign-ext",
2853 .simd128 => .simd128,
2854 .tail_call => .@"tail-call",
2855 };
28402856 }
28412857
28422858 pub fn toCpuFeature(tag: Tag) ?std.Target.wasm.Feature {
2843 return if (@intFromEnum(tag) < @typeInfo(std.Target.wasm.Feature).@"enum".fields.len)
2844 @enumFromInt(@intFromEnum(tag))
2845 else
2846 null;
2859 return switch (tag) {
2860 .atomics => .atomics,
2861 .@"bulk-memory" => .bulk_memory,
2862 .@"exception-handling" => .exception_handling,
2863 .@"extended-const" => .extended_const,
2864 .@"half-precision" => .half_precision,
2865 .multimemory => .multimemory,
2866 .multivalue => .multivalue,
2867 .@"mutable-globals" => .mutable_globals,
2868 .@"nontrapping-bulk-memory-len0" => .nontrapping_bulk_memory_len0, // Zig extension.
2869 .@"nontrapping-fptoint" => .nontrapping_fptoint,
2870 .@"reference-types" => .reference_types,
2871 .@"relaxed-simd" => .relaxed_simd,
2872 .@"sign-ext" => .sign_ext,
2873 .simd128 => .simd128,
2874 .@"tail-call" => .tail_call,
2875 .@"shared-mem" => null, // Linker-only feature.
2876 };
28472877 }
28482878
28492879 pub const format = @compileError("use @tagName instead");