-
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
-
68
-
69
-
70
-
71
-
72
-
73
-
74
-
75
-
76
-
77
-
78
-
79
-
80
-
81
-
82
-
83
-
84
-
85
-
86
-
87
-
88
-
89
-
90
-
91
-
92
-
93
-
94
-
95
-
96
-
97
-
98
-
99
-
100
-
101
-
102
-
103
-
104
-
105
-
106
-
107
-
108
-
109
-
110
-
111
-
112
-
113
-
114
-
115
-
116
-
117
-
118
-
119
-
120
-
121
-
122
-
123
-
124
-
125
-
126
-
127
-
128
-
129
-
130
-
131
-
132
-
133
-
134
-
135
-
136
-
137
-
138
-
139
-
140
-
141
-
142
-
143
-
144
-
145
-
146
// Copyright 2025 Shota FUJI
//
// Licensed under the Zero-Clause BSD License or the Apache License, Version 2.0, at your option.
// You may not use, copy, modify, or distribute this file except according to those terms. You can
// find a copy of the Zero-Clause BSD License at LICENSES/0BSD.txt, and a copy of the Apache License,
// Version 2.0 at LICENSES/Apache-2.0.txt. You may also obtain a copy of the Zero-Clause BSD License
// at <https://opensource.org/license/0bsd> and a copy of the Apache License, Version 2.0 at
// <https://www.apache.org/licenses/LICENSE-2.0>
//
// SPDX-License-Identifier: 0BSD OR Apache-2.0
//
// ===
//
// WebAssembly API.
//
// This module does not export constants, because Zig has no support for WebAssembly
// Globals yet. Exporting const results in a pointer for the value in LLVM and compile
// error in self-hosted backend.
// <https://github.com/ziglang/zig/issues/4866>.
//
// For binary size and maintenance cost, WebAssembly API only expose high-level response
// parsing API. Low-level APIs such as Message.parse and Message.iterator are not included.
const std = @import("std");
const sood = @import("lib.zig");
// Shared result code constants.
const WasmResultCode = enum(u32) {
OK = 0,
ITERATOR_DONE = 1,
ERR_PARSE_HEADER_SIZE_MISMATCH = 2,
ERR_PARSE_INVALID_SIGNATURE = 3,
ERR_PARSE_EMPTY_KEY = 4,
ERR_PARSE_KEY_SIZE_MISMATCH = 5,
ERR_PARSE_NON_UTF8_KEY = 6,
ERR_PARSE_VALUE_SIZE_CORRUPTED = 7,
ERR_PARSE_VALUE_SIZE_MISMATCH = 8,
ERR_PARSE_NON_UTF8_VALUE = 9,
ERR_VALIDATE_MISSING_REQUIRED_PROPERTY = 10,
ERR_VALIDATE_UNEXPECTED_VALUE_TYPE = 11,
ERR_VALIDATE_UNEXPECTED_MESSAGE_KIND = 12,
};
const LibError = sood.Message.HeaderParseError || sood.Message.PropertyParseError || sood.discovery.Response.SchemaError;
inline fn errorsToCode(err: LibError) WasmResultCode {
return switch (err) {
sood.Message.HeaderParseError.InvalidSignature => .ERR_PARSE_INVALID_SIGNATURE,
sood.Message.HeaderParseError.InvalidHeaderSize => .ERR_PARSE_HEADER_SIZE_MISMATCH,
sood.Message.PropertyParseError.EmptyKey => .ERR_PARSE_EMPTY_KEY,
sood.Message.PropertyParseError.IncompleteKey => .ERR_PARSE_KEY_SIZE_MISMATCH,
sood.Message.PropertyParseError.NonUTF8Key => .ERR_PARSE_NON_UTF8_KEY,
sood.Message.PropertyParseError.InvalidValueSizeField => .ERR_PARSE_VALUE_SIZE_CORRUPTED,
sood.Message.PropertyParseError.IncompleteValue => .ERR_PARSE_VALUE_SIZE_MISMATCH,
sood.Message.PropertyParseError.NonUTF8Value => .ERR_PARSE_NON_UTF8_VALUE,
sood.discovery.Response.SchemaError.MissingRequiredProperty => .ERR_VALIDATE_MISSING_REQUIRED_PROPERTY,
sood.discovery.Response.SchemaError.UnexpectedPropertyValue => .ERR_VALIDATE_UNEXPECTED_VALUE_TYPE,
sood.discovery.Response.SchemaError.NonResponseKindMessage => .ERR_VALIDATE_UNEXPECTED_MESSAGE_KIND,
};
}
export fn result_code_string_ptr(code: WasmResultCode) [*]const u8 {
return @tagName(code).ptr;
}
export fn result_code_string_len(code: WasmResultCode) usize {
return @tagName(code).len;
}
export fn allocate_bytes(len: usize) [*]const u8 {
return (std.heap.wasm_allocator.alloc(u8, len) catch @trap()).ptr;
}
export fn free_bytes(ptr: [*]const u8, len: usize) void {
std.heap.wasm_allocator.free(ptr[0..len]);
}
export fn get_discovery_multicast_ipv4_address() [*]const u8 {
return &sood.discovery.multicast_ipv4_address;
}
export fn get_discovery_server_udp_port() u16 {
return sood.discovery.udp_port;
}
export fn prebuilt_discovery_query_ptr() [*]const u8 {
return sood.discovery.Query.prebuilt.ptr;
}
export fn prebuilt_discovery_query_len() usize {
return sood.discovery.Query.prebuilt.len;
}
export fn allocate_discovery_response() *sood.discovery.Response {
return std.heap.wasm_allocator.create(sood.discovery.Response) catch @trap();
}
export fn free_discovery_response(ptr: *sood.discovery.Response) void {
std.heap.wasm_allocator.destroy(ptr);
}
export fn parse_discovery_response_into(
dst: *sood.discovery.Response,
bytes_ptr: [*]const u8,
bytes_len: usize,
) WasmResultCode {
const resp = sood.discovery.Response.parse(bytes_ptr[0..bytes_len]) catch |err| {
return errorsToCode(err);
};
dst.http_port = resp.http_port;
dst.name = resp.name;
dst.display_version = resp.display_version;
dst.unique_id = resp.unique_id;
return .OK;
}
export fn get_discovery_response_http_port(resp: *const sood.discovery.Response) u16 {
return resp.http_port;
}
export fn get_discovery_response_name_ptr(resp: *const sood.discovery.Response) [*]const u8 {
return resp.name.ptr;
}
export fn get_discovery_response_name_len(resp: *const sood.discovery.Response) usize {
return resp.name.len;
}
export fn get_discovery_response_display_version_ptr(resp: *const sood.discovery.Response) [*]const u8 {
return resp.display_version.ptr;
}
export fn get_discovery_response_display_version_len(resp: *const sood.discovery.Response) usize {
return resp.display_version.len;
}
export fn get_discovery_response_unique_id_ptr(resp: *const sood.discovery.Response) [*]const u8 {
return resp.unique_id.ptr;
}
export fn get_discovery_response_unique_id_len(resp: *const sood.discovery.Response) usize {
return resp.unique_id.len;
}