-
Notifications
You must be signed in to change notification settings - Fork 16
feat: support bitArrays in to_napi_value #368
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,7 +15,9 @@ pub fn sszValueToNapiValue(env: napi.Env, comptime ST: type, value: *const ST.Ty | |
| return try env.getBoolean(value.*); | ||
| }, | ||
| .vector => { | ||
| if (comptime ssz.isByteVectorType(ST)) { | ||
| if (comptime ssz.isBitVectorType(ST)) { | ||
| return try bitArrayToNapiValue(env, value.data[0..], ST.length); | ||
| } else if (comptime ssz.isByteVectorType(ST)) { | ||
| var bytes: [*]u8 = undefined; | ||
| const buf = try env.createArrayBuffer(ST.length, &bytes); | ||
| @memcpy(bytes[0..ST.length], value); | ||
|
|
@@ -30,7 +32,9 @@ pub fn sszValueToNapiValue(env: napi.Env, comptime ST: type, value: *const ST.Ty | |
| } | ||
| }, | ||
| .list => { | ||
| if (comptime ssz.isByteListType(ST)) { | ||
| if (comptime ssz.isBitListType(ST)) { | ||
| return try bitArrayToNapiValue(env, value.data.items, value.bit_len); | ||
| } else if (comptime ssz.isByteListType(ST)) { | ||
| var bytes: [*]u8 = undefined; | ||
| const buf = try env.createArrayBuffer(value.items.len, &bytes); | ||
| @memcpy(bytes[0..value.items.len], value.items); | ||
|
|
@@ -56,6 +60,22 @@ pub fn sszValueToNapiValue(env: napi.Env, comptime ST: type, value: *const ST.Ty | |
| } | ||
| } | ||
|
|
||
| /// Converts a bit array into a `napi.Value`. | ||
| /// | ||
| /// The napi value matches the shape of `ssz-ts` for interoperability. | ||
| fn bitArrayToNapiValue(env: napi.Env, data: []const u8, bit_len: usize) !napi.Value { | ||
| std.debug.assert(data.len == (bit_len + 7) / 8); | ||
| var bytes: [*]u8 = undefined; | ||
| const buf = try env.createArrayBuffer(data.len, &bytes); | ||
| @memcpy(bytes[0..data.len], data); | ||
| const uint8_array = try env.createTypedarray(.uint8, data.len, buf, 0); | ||
|
|
||
| const obj = try env.createObject(); | ||
| try obj.setNamedProperty("uint8Array", uint8_array); | ||
| try obj.setNamedProperty("bitLen", try env.createInt64(@intCast(bit_len))); | ||
| return obj; | ||
| } | ||
|
Comment on lines
+66
to
+77
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The According to the style guide:
References
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. accepted the assertion for |
||
|
|
||
| const NumberSliceOpts = struct { | ||
| typed_array: ?napi.value_types.TypedarrayType = null, | ||
| }; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggest to create known type under
bindings/napi/js_types.zigand then reuse here.This will reduce the code to following, will later be further optimized, as I will introduce an interface
Object.initthat will help to doBitArray.initsyntax.