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
|
const std = @import("std");
const js = @import("js.zig");
const Object = @import("../Object.zig");
pub const Call = @import("call.zig").Call;
pub const AnyFuture = @import("future.zig").AnyFuture;
pub const Future = @import("future.zig").Future;
pub const Value = @import("value.zig").Value;
pub const Function = @import("Function.zig");
calls: std.ArrayList(Call) = .empty,
values: std.ArrayList(Value) = .empty,
pool: std.heap.MemoryPool(Value) = .empty,
results: std.ArrayList(*Value) = .empty,
gpa: std.mem.Allocator,
const Self = @This();
pub const default: Self = .{ .gpa = std.heap.wasm_allocator };
pub fn call(self: *Self, function: Function, args: anytype) AnyFuture {
const id = self.calls.items.len;
self.calls.append(self.gpa, .{
.kind = function.id,
.num_args = args.len,
}) catch |err| return .{ .err = err };
inline for (args) |arg| {
self.values.append(self.gpa, .from(arg)) catch |err| return .{ .err = err };
}
const result = self.pool.create(self.gpa) catch |err| return .{ .err = err };
errdefer self.pool.destroy(result);
self.results.append(self.gpa, result) catch |err| return .{ .err = err };
return .{
.unresolved = .{
.id = id,
.value = result,
},
};
}
pub fn flush(self: *Self) void {
js.process(
@ptrCast(self.calls.items),
@ptrCast(self.values.items),
@ptrCast(self.results.items),
self.results.items.len,
);
self.calls.clearRetainingCapacity();
self.values.clearRetainingCapacity();
self.results.clearRetainingCapacity();
}
pub fn release(self: *Self, value: *Value) void {
self.pool.destroy(value);
}
pub fn deinit(self: *Self) void {
self.flush();
self.calls.deinit(self.gpa);
self.values.deinit(self.gpa);
self.pool.deinit(self.gpa);
self.results.deinit(self.gpa);
self.* = undefined;
}
|