@thi.ng/wasm-api 0.14.0 → 0.15.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +36 -1
- package/README.md +173 -108
- package/api.d.ts +78 -21
- package/api.js +10 -4
- package/cli.js +6 -2
- package/codegen/align.d.ts +14 -0
- package/codegen/align.js +35 -0
- package/codegen/c11.js +71 -55
- package/codegen/typescript.js +14 -11
- package/codegen/utils.d.ts +12 -4
- package/codegen/utils.js +8 -0
- package/codegen/zig.js +93 -69
- package/codegen.d.ts +2 -1
- package/codegen.js +77 -54
- package/package.json +13 -9
- package/string.d.ts +4 -0
- package/string.js +4 -0
- package/zig/managed-index.zig +223 -0
- package/{include → zig}/wasmapi.zig +2 -0
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
const std = @import("std");
|
|
2
|
+
const builtin = @import("builtin");
|
|
3
|
+
const Allocator = std.mem.Allocator;
|
|
4
|
+
|
|
5
|
+
/// `std.Arraylist` based data structure for associating and auto-indexing
|
|
6
|
+
/// newly added items with numeric IDs and managing a pool of free IDs.
|
|
7
|
+
/// Wraps `std.Arraylist` with a simple add()/remove()/get()/has() API and
|
|
8
|
+
/// adds free slot tracking & re-use (depending on value type, potentially
|
|
9
|
+
/// without any memory overhead). Free slots are stored as implicit linked
|
|
10
|
+
/// list within the same array as the actual values/items.
|
|
11
|
+
/// Important: The provided `.remove()` function MUST be used to remove
|
|
12
|
+
/// unused items and mark their slot IDs for re-use.
|
|
13
|
+
pub fn ManagedIndex(comptime T: type, comptime I: type) type {
|
|
14
|
+
const info = @typeInfo(I);
|
|
15
|
+
if (!(info == .Int and info.Int.bits > 0 and info.Int.signedness == std.builtin.Signedness.unsigned)) {
|
|
16
|
+
@compileError("require unsigned int as index type");
|
|
17
|
+
}
|
|
18
|
+
const Item = union(enum) {
|
|
19
|
+
/// Actual user defined value
|
|
20
|
+
value: T,
|
|
21
|
+
/// Free list slot ID. Index of the next avail free slot
|
|
22
|
+
id: ?I,
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
return struct {
|
|
26
|
+
/// backing array list
|
|
27
|
+
list: std.ArrayList(Item),
|
|
28
|
+
/// Index of the head of the implicitly stored list of free slots
|
|
29
|
+
/// Free slots in the array list use `Item.id` fields, each storing
|
|
30
|
+
/// the index of the next available free slot (or null)
|
|
31
|
+
/// The backing array list only grows if `freeID` is null
|
|
32
|
+
freeID: ?I = null,
|
|
33
|
+
/// Number of currently used items
|
|
34
|
+
used: usize = 0,
|
|
35
|
+
|
|
36
|
+
const Self = @This();
|
|
37
|
+
|
|
38
|
+
/// Initializes the underlying array list
|
|
39
|
+
pub fn init(allocator: Allocator) Self {
|
|
40
|
+
return .{
|
|
41
|
+
.list = std.ArrayList(Item).init(allocator),
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/// De-initializes the underlying array list
|
|
46
|
+
pub fn deinit(self: *Self) void {
|
|
47
|
+
self.list.deinit();
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/// Clears, frees & resets this list and its backing array list.
|
|
51
|
+
pub fn clear(self: *Self) void {
|
|
52
|
+
self.list.clearAndFree();
|
|
53
|
+
self.freeID = null;
|
|
54
|
+
self.used = 0;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/// Adds given `item` to the list and returns slot ID. If possible
|
|
58
|
+
/// re-uses existing free slots, else attempts to allocate a new slot.
|
|
59
|
+
pub fn add(self: *Self, item: T) Allocator.Error!I {
|
|
60
|
+
if (self.freeID) |id| {
|
|
61
|
+
self.freeID = self.list.items[id].id;
|
|
62
|
+
self.list.items[id] = .{ .value = item };
|
|
63
|
+
self.used += 1;
|
|
64
|
+
return @intCast(I, id);
|
|
65
|
+
} else {
|
|
66
|
+
const id = self.list.items.len;
|
|
67
|
+
try self.list.append(.{ .value = item });
|
|
68
|
+
self.used += 1;
|
|
69
|
+
return @intCast(I, id);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/// Adds given `id` to the internal free slot list and removes related
|
|
74
|
+
/// value/item
|
|
75
|
+
pub fn remove(self: *Self, id: I) void {
|
|
76
|
+
if (!self.has(id)) return;
|
|
77
|
+
self.list.items[id] = .{ .id = self.freeID };
|
|
78
|
+
self.freeID = id;
|
|
79
|
+
self.used -= 1;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/// Checks if given `id` is valid (skipped in unsafe release modes) and
|
|
83
|
+
/// returns item value or null if ID is invalid.
|
|
84
|
+
pub fn get(self: *const Self, id: I) ?T {
|
|
85
|
+
if (builtin.mode == .ReleaseFast or builtin.mode == .ReleaseSmall) {
|
|
86
|
+
return self.list.items[id].value;
|
|
87
|
+
}
|
|
88
|
+
return if (self.has(id)) self.list.items[id].value else null;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/// Returns true if given `id` is valid, i.e. is referring to a
|
|
92
|
+
/// currently used item (and isn't part of the list of free slots).
|
|
93
|
+
pub fn has(self: *const Self, id: I) bool {
|
|
94
|
+
if (id >= self.list.items.len) return false;
|
|
95
|
+
var freeID = self.freeID;
|
|
96
|
+
while (true) {
|
|
97
|
+
if (freeID) |fid| {
|
|
98
|
+
if (fid == id) return false;
|
|
99
|
+
freeID = self.list.items[fid].id;
|
|
100
|
+
} else {
|
|
101
|
+
return true;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/// Iterator for currently used values (in ascending index order)
|
|
107
|
+
pub const ValueIterator = struct {
|
|
108
|
+
parent: *const Self,
|
|
109
|
+
i: usize = 0,
|
|
110
|
+
|
|
111
|
+
pub fn init(parent: *const Self) @This() {
|
|
112
|
+
return .{ .parent = parent };
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
pub fn next(self: *@This()) ?T {
|
|
116
|
+
const items = self.parent.list.items;
|
|
117
|
+
if (self.i >= items.len) return null;
|
|
118
|
+
while (self.i < items.len) {
|
|
119
|
+
const val = switch (items[self.i]) {
|
|
120
|
+
.value => |value| value,
|
|
121
|
+
.id => null,
|
|
122
|
+
};
|
|
123
|
+
self.i += 1;
|
|
124
|
+
if (val != null) return val;
|
|
125
|
+
}
|
|
126
|
+
return null;
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
/// Returns iterator of currently used values
|
|
131
|
+
pub fn values(self: *const Self) ValueIterator {
|
|
132
|
+
return ValueIterator.init(self);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/// Allocates a new slice containing only currently used values and
|
|
136
|
+
/// returns it. Caller owns returned memory.
|
|
137
|
+
pub fn valuesAsSlice(self: *Self, allocator: Allocator) Allocator.Error![]T {
|
|
138
|
+
var slice = try allocator.alloc(T, self.used);
|
|
139
|
+
var iter = self.values();
|
|
140
|
+
var i: usize = 0;
|
|
141
|
+
while (iter.next()) |value| {
|
|
142
|
+
slice[i] = value;
|
|
143
|
+
i += 1;
|
|
144
|
+
}
|
|
145
|
+
return slice;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/// Allocates a new slice of all currently free slot IDs and returns it.
|
|
149
|
+
/// Caller owns returned memory.
|
|
150
|
+
/// Internal/debug only.
|
|
151
|
+
fn freeIndicesAsSlice(self: *const Self, allocator: Allocator) Allocator.Error![]I {
|
|
152
|
+
var slice = try allocator.alloc(I, self.list.items.len - self.used);
|
|
153
|
+
var i: I = 0;
|
|
154
|
+
var freeID = self.freeID;
|
|
155
|
+
while (i < slice.len) {
|
|
156
|
+
if (freeID) |fid| {
|
|
157
|
+
slice[i] = fid;
|
|
158
|
+
i += 1;
|
|
159
|
+
freeID = self.list.items[fid].id;
|
|
160
|
+
} else {
|
|
161
|
+
break;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
return slice;
|
|
165
|
+
}
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
test "ManagedIndex" {
|
|
170
|
+
const allocator = std.testing.allocator;
|
|
171
|
+
const expect = std.testing.expect;
|
|
172
|
+
const expectEqualSlices = std.testing.expectEqualSlices;
|
|
173
|
+
|
|
174
|
+
var list = ManagedIndex(f64, u8).init(allocator);
|
|
175
|
+
defer list.deinit();
|
|
176
|
+
try expect(try list.add(123) == 0);
|
|
177
|
+
try expect(try list.add(123) == 1);
|
|
178
|
+
|
|
179
|
+
try expect(list.has(0));
|
|
180
|
+
try expect(list.has(1));
|
|
181
|
+
try expect(list.used == 2);
|
|
182
|
+
|
|
183
|
+
list.remove(0);
|
|
184
|
+
try expect(!list.has(0));
|
|
185
|
+
try expect(!list.has(2));
|
|
186
|
+
try expect(list.used == 1);
|
|
187
|
+
|
|
188
|
+
try expect(try list.add(123) == 0);
|
|
189
|
+
try expect(try list.add(123) == 2);
|
|
190
|
+
try expect(list.has(2));
|
|
191
|
+
try expect(list.used == 3);
|
|
192
|
+
|
|
193
|
+
list.remove(1);
|
|
194
|
+
list.remove(2);
|
|
195
|
+
|
|
196
|
+
var ids = try list.freeIndicesAsSlice(allocator);
|
|
197
|
+
defer allocator.free(ids);
|
|
198
|
+
try expectEqualSlices(u8, &[_]u8{ 2, 1 }, ids);
|
|
199
|
+
|
|
200
|
+
try expect(try list.add(202) == 2);
|
|
201
|
+
list.remove(0);
|
|
202
|
+
try expect(try list.add(200) == 0);
|
|
203
|
+
try expect(try list.add(201) == 1);
|
|
204
|
+
try expect(list.freeID == null);
|
|
205
|
+
|
|
206
|
+
var iter = list.values();
|
|
207
|
+
try expect(if (iter.next()) |v| v == 200 else false);
|
|
208
|
+
try expect(if (iter.next()) |v| v == 201 else false);
|
|
209
|
+
try expect(if (iter.next()) |v| v == 202 else false);
|
|
210
|
+
try expect(iter.next() == null);
|
|
211
|
+
|
|
212
|
+
const slice = try list.valuesAsSlice(allocator);
|
|
213
|
+
defer allocator.free(slice);
|
|
214
|
+
try expectEqualSlices(f64, &[_]f64{ 200, 201, 202 }, slice);
|
|
215
|
+
|
|
216
|
+
list.remove(0);
|
|
217
|
+
list.remove(2);
|
|
218
|
+
list.remove(1);
|
|
219
|
+
|
|
220
|
+
var ids2 = try list.freeIndicesAsSlice(allocator);
|
|
221
|
+
defer allocator.free(ids2);
|
|
222
|
+
try expectEqualSlices(u8, &[_]u8{ 1, 2, 0 }, ids2);
|
|
223
|
+
}
|