This repository has been archived by the owner on Dec 5, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
js_common.zig
558 lines (514 loc) · 21.4 KB
/
js_common.zig
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
pub const std = @import("std");
pub const dida = @import("../lib/dida.zig");
/// root.abi defines functions for interacting with javascript values
const abi = @import("root").abi;
// --- global allocator ---
var gpa = std.heap.GeneralPurposeAllocator(.{
.safety = true,
.never_unmap = true,
}){};
pub const allocator = gpa.allocator();
// --- constructors for External types need to use global allocator ---
pub fn GraphBuilder_init() !dida.core.GraphBuilder {
return dida.core.GraphBuilder.init(allocator);
}
pub fn Graph_init(node_specs: []const dida.core.NodeSpec, node_immediate_subgraphs: []const dida.core.Subgraph, subgraph_parents: []const dida.core.Subgraph) !dida.core.Graph {
return dida.core.Graph.init(allocator, node_specs, node_immediate_subgraphs, subgraph_parents);
}
pub fn Shard_init(graph: *const dida.core.Graph) !dida.core.Shard {
return dida.core.Shard.init(allocator, graph);
}
// --- serde ---
pub const exported_functions = .{
.{ "GraphBuilder_init", GraphBuilder_init },
.{ "GraphBuilder_addSubgraph", dida.core.GraphBuilder.addSubgraph },
.{ "GraphBuilder_addNode", dida.core.GraphBuilder.addNode },
.{ "GraphBuilder_connectLoop", dida.core.GraphBuilder.connectLoop },
.{ "GraphBuilder_finishAndReset", dida.core.GraphBuilder.finishAndReset },
.{ "Graph_init", Graph_init },
.{ "Shard_init", Shard_init },
.{ "Shard_pushInput", dida.core.Shard.pushInput },
.{ "Shard_flushInput", dida.core.Shard.flushInput },
.{ "Shard_advanceInput", dida.core.Shard.advanceInput },
.{ "Shard_hasWork", dida.core.Shard.hasWork },
.{ "Shard_doWork", dida.core.Shard.doWork },
.{ "Shard_popOutput", dida.core.Shard.popOutput },
};
pub const types_with_js_constructors = .{
dida.core.GraphBuilder,
dida.core.Graph,
dida.core.Shard,
dida.core.Change,
dida.core.ChangeBatch,
dida.core.Subgraph,
dida.core.Node,
dida.core.NodeSpec,
dida.core.NodeSpec.MapSpec,
dida.core.NodeSpec.JoinSpec,
dida.core.NodeSpec.TimestampPushSpec,
dida.core.NodeSpec.TimestampPopSpec,
dida.core.NodeSpec.TimestampIncrementSpec,
dida.core.NodeSpec.IndexSpec,
dida.core.NodeSpec.UnionSpec,
dida.core.NodeSpec.DistinctSpec,
dida.core.NodeSpec.ReduceSpec,
dida.core.NodeSpec.OutputSpec,
};
pub fn hasJsConstructor(comptime T: type) bool {
inline for (types_with_js_constructors) |T2| {
if (T == T2) return true;
}
return false;
}
/// How a given type will be (de)serialized across the abi boundary
pub const SerdeStrategy = enum {
/// Heap-allocate on zig side and give a pointer to js side
External,
/// Convert between zig and js values at the callsite
Value,
};
pub fn serdeStrategy(comptime T: type) SerdeStrategy {
return switch (T) {
dida.core.GraphBuilder,
*dida.core.GraphBuilder,
dida.core.Graph,
*dida.core.Graph,
*const dida.core.Graph,
dida.core.Shard,
*dida.core.Shard,
*const dida.core.Shard,
=> .External,
void,
bool,
usize,
isize,
[]const usize,
dida.core.Timestamp,
dida.core.Frontier,
f64,
[]const u8,
dida.core.Value,
std.meta.Tag(dida.core.Value),
[]const dida.core.Value,
dida.core.Row,
dida.core.Change,
[]dida.core.Change,
dida.core.ChangeBatch,
?dida.core.ChangeBatch,
dida.core.Subgraph,
[]const dida.core.Subgraph,
dida.core.Node,
?dida.core.Node,
[2]dida.core.Node,
dida.core.NodeSpec,
[]const dida.core.NodeSpec,
std.meta.Tag(dida.core.NodeSpec),
dida.core.NodeSpec.MapSpec,
*dida.core.NodeSpec.MapSpec.Mapper,
dida.core.NodeSpec.JoinSpec,
dida.core.NodeSpec.TimestampPushSpec,
dida.core.NodeSpec.TimestampPopSpec,
dida.core.NodeSpec.TimestampIncrementSpec,
dida.core.NodeSpec.IndexSpec,
dida.core.NodeSpec.UnionSpec,
dida.core.NodeSpec.DistinctSpec,
dida.core.NodeSpec.ReduceSpec,
*dida.core.NodeSpec.ReduceSpec.Reducer,
dida.core.NodeSpec.OutputSpec,
=> .Value,
else => dida.util.compileError("No SerdeStrategy for {}", .{T}),
};
}
/// Take a zig function and return a wrapped version that handles serde
pub fn handleSerdeForFunction(comptime zig_fn: anytype) fn (abi.Env, []const abi.Value) abi.Value {
return struct {
fn wrappedMethod(env: abi.Env, js_args: []const abi.Value) abi.Value {
var zig_args: std.meta.ArgsTuple(@TypeOf(zig_fn)) = undefined;
comptime var i: usize = 0;
inline while (i < zig_args.len) : (i += 1) {
const js_arg = js_args[i];
const zig_arg_type = @TypeOf(zig_args[i]);
zig_args[i] = switch (comptime serdeStrategy(zig_arg_type)) {
.External => deserializeExternal(env, js_arg, zig_arg_type),
.Value => deserializeValue(env, js_arg, zig_arg_type),
};
}
const result_or_err = @call(.{}, zig_fn, zig_args);
const result = if (@typeInfo(@TypeOf(result_or_err)) == .ErrorUnion)
result_or_err catch |err| dida.util.panic("{}", .{err})
else
result_or_err;
switch (comptime serdeStrategy(@TypeOf(result))) {
.External => {
const result_ptr = allocator.create(@TypeOf(result)) catch |err| dida.util.panic("{}", .{err});
result_ptr.* = result;
return serializeExternal(env, result_ptr);
},
.Value => {
return serializeValue(env, result);
},
}
}
}.wrappedMethod;
}
// --- private serde stuff ---
const usize_bits = @bitSizeOf(usize);
comptime {
dida.util.comptimeAssert(
usize_bits == 32 or usize_bits == 64,
"Can only handle 32 bit or 64 bit architectures, not {}",
.{usize_bits},
);
}
fn serializeExternal(env: abi.Env, value: anytype) abi.Value {
const info = @typeInfo(@TypeOf(value));
dida.util.comptimeAssert(
comptime (serdeStrategy(@TypeOf(value)) == .External),
"Used serializeExternal on a type that is expected to require serializeValue: {}",
.{@TypeOf(value)},
);
dida.util.comptimeAssert(
info == .Pointer and info.Pointer.size == .One,
"serializeExternal should be called with *T, got {}",
.{@TypeOf(value)},
);
dida.util.comptimeAssert(
comptime hasJsConstructor(info.Pointer.child),
"Tried to create an external for a type that doesn't have a matching js constructor: {}",
.{@TypeOf(value)},
);
const external = abi.createExternal(env, @ptrCast(*anyopaque, value));
const result = abi.createObject(env);
abi.setProperty(env, result, abi.createString(env, "external"), external);
return result;
}
fn deserializeExternal(env: abi.Env, value: abi.Value, comptime ReturnType: type) ReturnType {
dida.util.comptimeAssert(
comptime (serdeStrategy(ReturnType) == .External),
"Used deserializeExternal on a type that is expected to require deserializeValue: {}",
.{ReturnType},
);
const info = @typeInfo(ReturnType);
dida.util.comptimeAssert(
info == .Pointer and info.Pointer.size == .One,
"deserializeExternal should be called with *T, got {}",
.{ReturnType},
);
const external = abi.getProperty(env, value, abi.createString(env, "external"));
const ptr = abi.getExternal(env, external);
return @ptrCast(ReturnType, @alignCast(@alignOf(info.Pointer.child), ptr));
}
fn serializeValue(env: abi.Env, value: anytype) abi.Value {
dida.util.comptimeAssert(
comptime (serdeStrategy(@TypeOf(value)) == .Value),
"Used serializeValue on a type that is expected to require serializeExternal: {}",
.{@TypeOf(value)},
);
if (@TypeOf(value) == []const u8) {
return abi.createString(env, value);
}
if (@TypeOf(value) == dida.core.Value) {
return switch (value) {
.String => |string| serializeValue(env, string),
.Number => |number| serializeValue(env, number),
};
}
if (@TypeOf(value) == dida.core.Row) {
return serializeValue(env, value.values);
}
if (@TypeOf(value) == dida.core.Timestamp) {
return serializeValue(env, value.coords);
}
if (@TypeOf(value) == dida.core.Frontier) {
const len = value.timestamps.count();
const js_array = abi.createArray(env, len);
var iter = value.timestamps.iterator();
var i: usize = 0;
while (iter.next()) |entry| {
const js_timestamp = serializeValue(env, entry.key_ptr.*);
abi.setElement(env, js_array, @intCast(u32, i), js_timestamp);
i += 1;
}
return js_array;
}
const info = @typeInfo(@TypeOf(value));
switch (info) {
.Bool => {
return abi.createBoolean(env, value);
},
.Int => {
return switch (@TypeOf(value)) {
usize => if (usize_bits == 64) abi.createI64(env, @intCast(i64, value)) else abi.createU32(env, value),
isize => if (usize_bits == 64) abi.createI64(env, value) else abi.createI32(env, value),
else => dida.util.compileError("Don't know how to create js value for {}", .{@TypeOf(value)}),
};
},
.Float => {
const abi_fn = switch (@TypeOf(value)) {
f64 => abi.createF64,
else => dida.util.compileError("Don't know how to create js value for {}", .{@TypeOf(value)}),
};
return @call(.{}, abi_fn, .{ env, value });
},
.Struct => |struct_info| {
dida.util.comptimeAssert(
comptime hasJsConstructor(@TypeOf(value)),
"Tried to create a value for a struct type that doesn't have a matching js constructor: {}",
.{@TypeOf(value)},
);
const result = abi.createObject(env);
inline for (struct_info.fields) |field_info| {
const field_value = serializeValue(env, @field(value, field_info.name));
abi.setProperty(env, result, abi.createString(env, field_info.name), field_value);
}
return result;
},
.Union => |union_info| {
if (union_info.tag_type) |tag_type| {
dida.util.comptimeAssert(
comptime hasJsConstructor(@TypeOf(value)),
"Tried to create a value for a union type that doesn't have a matching js constructor: {}",
.{@TypeOf(value)},
);
const result = abi.createObject(env);
const tag = std.meta.activeTag(value);
abi.setProperty(env, result, abi.createString(env, "tag"), abi.createString(env, @tagName(tag)));
inline for (@typeInfo(tag_type).Enum.fields) |enum_field_info| {
if (@enumToInt(tag) == enum_field_info.value) {
const payload = @field(value, enum_field_info.name);
const js_payload = serializeValue(env, payload);
abi.setProperty(env, result, abi.createString(env, "payload"), js_payload);
return result;
}
}
unreachable;
} else {
dida.util.compileError("Can't create value for untagged union type {}", .{@TypeOf(value)});
}
},
.Enum => |enum_info| {
comptime var max_len: usize = 0;
inline for (enum_info.fields) |field_info| {
comptime {
max_len = dida.util.max(max_len, field_info.name.len);
}
}
// max_len+1 to make space for null byte
var buffer: [max_len + 1]u8 = undefined;
const tag_name = abi.getStringInto(env, value, &buffer);
inline for (enum_info.fields) |field_info| {
if (std.mem.eql(u8, tag_name, field_info.name)) {
return @intToEnum(@TypeOf(value), field_info.value);
}
}
dida.util.panic("Type {s} does not contain a tag named \"{s}\"", .{ @TypeOf(value), tag_name });
},
.Pointer => |pointer_info| {
switch (pointer_info.size) {
.Slice => {
const js_array = abi.createArray(env, value.len);
for (value) |elem, i| {
const js_elem = serializeValue(env, elem);
abi.setElement(env, js_array, @intCast(u32, i), js_elem);
}
return js_array;
},
else => dida.util.compileError("Don't know how to create value of type {}", .{@TypeOf(value)}),
}
},
.Optional => {
return if (value) |payload|
serializeValue(env, payload)
else
abi.createUndefined(env);
},
.Void => {
return abi.createUndefined(env);
},
else => dida.util.compileError("Don't know how to create value of type {}", .{@TypeOf(value)}),
}
}
const JsMapper = struct {
// TODO is it safe to hold on to this?
env: abi.Env,
js_fn_ref: abi.RefCounted,
mapper: dida.core.NodeSpec.MapSpec.Mapper,
fn map(self: *dida.core.NodeSpec.MapSpec.Mapper, row: dida.core.Row) error{OutOfMemory}!dida.core.Row {
const parent = @fieldParentPtr(JsMapper, "mapper", self);
const js_fn = abi.getRefCounted(parent.env, parent.js_fn_ref);
const js_args = [_]abi.Value{
serializeValue(parent.env, row),
};
const js_output = abi.callFunction(parent.env, js_fn, &js_args);
const output = deserializeValue(parent.env, js_output, dida.core.Row);
return output;
}
};
const JsReducer = struct {
// TODO is it safe to hold on to this?
env: abi.Env,
js_fn_ref: abi.RefCounted,
reducer: dida.core.NodeSpec.ReduceSpec.Reducer,
fn reduce(self: *dida.core.NodeSpec.ReduceSpec.Reducer, reduced_value: dida.core.Value, row: dida.core.Row, count: usize) error{OutOfMemory}!dida.core.Value {
const parent = @fieldParentPtr(JsReducer, "reducer", self);
const js_fn = abi.getRefCounted(parent.env, parent.js_fn_ref);
const js_args = [_]abi.Value{
serializeValue(parent.env, reduced_value),
serializeValue(parent.env, row),
serializeValue(parent.env, count),
};
const js_output = abi.callFunction(parent.env, js_fn, &js_args);
const output = deserializeValue(parent.env, js_output, dida.core.Value);
return output;
}
};
fn deserializeValue(env: abi.Env, value: abi.Value, comptime ReturnType: type) ReturnType {
dida.util.comptimeAssert(
comptime (serdeStrategy(ReturnType) == .Value),
"Used deserializeValue on a type that is expected to require deserializeExternal: {}",
.{ReturnType},
);
if (ReturnType == []const u8) {
return abi.getString(env, value) catch |err| dida.util.panic("{}", .{err});
}
if (ReturnType == dida.core.Value) {
const js_type = abi.jsTypeOf(env, value);
return switch (js_type) {
.String => dida.core.Value{ .String = deserializeValue(env, value, []const u8) },
.Number => dida.core.Value{ .Number = deserializeValue(env, value, f64) },
else => dida.util.panic("Don't know how to get a dida.core.Value from {}", .{js_type}),
};
}
if (ReturnType == dida.core.Row) {
return dida.core.Row{ .values = deserializeValue(env, value, []const dida.core.Value) };
}
if (ReturnType == dida.core.Timestamp) {
return dida.core.Timestamp{ .coords = deserializeValue(env, value, []const usize) };
}
if (ReturnType == *dida.core.NodeSpec.MapSpec.Mapper) {
// TODO we're just leaking this for now
var js_mapper = allocator.create(JsMapper) catch |err| dida.util.panic("{}", .{err});
js_mapper.* = JsMapper{
.env = env,
.js_fn_ref = abi.createRefCounted(env, value, 1),
.mapper = .{
.map_fn = JsMapper.map,
},
};
return &js_mapper.mapper;
}
if (ReturnType == *dida.core.NodeSpec.ReduceSpec.Reducer) {
// TODO we're just leaking this for now
var js_reducer = allocator.create(JsReducer) catch |err| dida.util.panic("{}", .{err});
js_reducer.* = JsReducer{
.env = env,
.js_fn_ref = abi.createRefCounted(env, value, 1),
.reducer = .{
.reduce_fn = JsReducer.reduce,
},
};
return &js_reducer.reducer;
}
const info = @typeInfo(ReturnType);
switch (info) {
.Int => {
return switch (ReturnType) {
usize => if (usize_bits == 64) @intCast(usize, abi.getI64(env, value)) else abi.getU32(env, value),
isize => if (usize_bits == 64) abi.getI64(env, value) else abi.getI32(env, value),
else => dida.util.compileError("Don't know how to create js value for {}", .{ReturnType}),
};
},
.Float => {
const abi_fn = switch (ReturnType) {
f64 => abi.getF64,
else => dida.util.compileError("Don't know how to create js value for {}", .{ReturnType}),
};
return @call(.{}, abi_fn, .{ env, value });
},
.Struct => |struct_info| {
var result: ReturnType = undefined;
inline for (struct_info.fields) |field_info| {
const field_value = abi.getProperty(env, value, abi.createString(env, field_info.name));
@field(result, field_info.name) = deserializeValue(env, field_value, field_info.field_type);
}
return result;
},
.Union => |union_info| {
if (union_info.tag_type) |tag_type| {
const tag = deserializeValue(env, abi.getProperty(env, value, abi.createString(env, "tag")), tag_type);
inline for (@typeInfo(tag_type).Enum.fields) |enum_field_info, i| {
if (@enumToInt(tag) == enum_field_info.value) {
const union_field_info = union_info.fields[i];
const payload = deserializeValue(env, abi.getProperty(env, value, abi.createString(env, "payload")), union_field_info.field_type);
return @unionInit(ReturnType, union_field_info.name, payload);
}
}
unreachable;
} else {
dida.util.compileError("Can't get value for untagged union type {}", .{ReturnType});
}
},
.Enum => |enum_info| {
comptime var max_len: usize = 0;
inline for (enum_info.fields) |field_info| {
comptime {
max_len = dida.util.max(max_len, field_info.name.len);
}
}
// max_len+1 to make space for null byte :(
var buffer: [max_len + 1]u8 = undefined;
const tag_name = abi.getStringInto(env, value, &buffer);
inline for (enum_info.fields) |field_info| {
if (std.mem.eql(u8, tag_name, field_info.name)) {
return @intToEnum(ReturnType, field_info.value);
}
}
dida.util.panic("Type {s} does not contain a tag named \"{s}\"", .{ ReturnType, tag_name });
},
.Array => |array_info| {
const js_len = abi.getArrayLength(env, value);
dida.util.assert(js_len == array_info.len, "Expected array of length {}, got length {}", .{ array_info.len, js_len });
var result: ReturnType = undefined;
for (result) |*elem, i| {
const js_elem = abi.getElement(env, value, @intCast(u32, i));
elem.* = deserializeValue(env, js_elem, array_info.child);
}
return result;
},
.Pointer => |pointer_info| {
switch (pointer_info.size) {
.Slice => {
const len = abi.getArrayLength(env, value);
const result = allocator.alloc(pointer_info.child, len) catch |err| dida.util.panic("{}", .{err});
for (result) |*elem, i| {
const js_elem = abi.getElement(env, value, @intCast(u32, i));
elem.* = deserializeValue(env, js_elem, pointer_info.child);
}
return result;
},
else => dida.util.compileError("Don't know how to get value for type {}", .{ReturnType}),
}
},
.Optional => |optional_info| {
const js_type = abi.jsTypeOf(env, value);
return switch (js_type) {
.Undefined, .Null => null,
else => deserializeValue(env, value, optional_info.child),
};
},
.Void => {
return {};
},
else => dida.util.compileError("Don't know how to get value for type {}", .{ReturnType}),
}
}
// --- abi types ---
pub const JsType = enum(u8) {
Undefined = 0,
Null = 1,
Boolean = 2,
Number = 3,
String = 4,
Object = 5,
Function = 6,
};