-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathshuffle.zig
More file actions
35 lines (30 loc) · 1.18 KB
/
shuffle.zig
File metadata and controls
35 lines (30 loc) · 1.18 KB
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
const std = @import("std");
const napi = @import("zapi:zapi");
const innerShuffleList = @import("state_transition").shuffle.innerShuffleList;
pub fn Shuffle_shuffleList(env: napi.Env, cb: napi.CallbackInfo(4)) !napi.Value {
const list_info = try cb.arg(0).getTypedarrayInfo();
if (list_info.array_type != .uint32) {
return error.InvalidShuffleListType;
}
const list: []u32 = @alignCast(std.mem.bytesAsSlice(u32, list_info.data));
const seed_info = try cb.arg(1).getTypedarrayInfo();
const seed = seed_info.data;
const rounds_u32 = try cb.arg(2).getValueUint32();
if (rounds_u32 > 255) {
return error.InvalidRoundsSize;
}
const rounds: u8 = @intCast(rounds_u32);
const forwards = try cb.arg(3).getValueBool();
try innerShuffleList(u32, list, seed, rounds, forwards);
return env.getUndefined();
}
pub fn register(env: napi.Env, exports: napi.Value) !void {
const shuffle_obj = try env.createObject();
try shuffle_obj.setNamedProperty("innerShuffleList", try env.createFunction(
"innerShuffleList",
4,
Shuffle_shuffleList,
null,
));
try exports.setNamedProperty("shuffle", shuffle_obj);
}