Skip to content

Commit ab42ab0

Browse files
feat(js): add typed array toArray (#70)
1 parent d134a6a commit ab42ab0

4 files changed

Lines changed: 32 additions & 1 deletion

File tree

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ c.count; // 1 (getter, not a method call)
9090
| `Object(T)` | `object` | `get()`, `set(value)` — `T` fields must be DSL types |
9191
| `Function` | `Function` | `call(args)` |
9292
| `Value` | `any` | `isNumber()`, `asNumber()`, type checking/narrowing |
93-
| `Uint8Array` etc. | `TypedArray` | `toSlice()`, `from(slice)` |
93+
| `Uint8Array` etc. | `TypedArray` | `toSlice()`, `toArray(len)`, `from(slice)` |
9494
| `Promise(T)` | `Promise` | `resolve(value)`, `reject(err)` |
9595

9696
---

‎examples/js_dsl/mod.test.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,15 @@ describe("typed arrays", () => {
201201
expect(mod.uint8Sum(data)).toEqual(15);
202202
});
203203

204+
it("toArray copies a TypedArray of the expected length", () => {
205+
expect(mod.uint8Array4Sum(new Uint8Array([1, 2, 3, 4]))).toEqual(10);
206+
});
207+
208+
it("toArray rejects TypedArrays of a different length", () => {
209+
expect(() => mod.uint8Array4Sum(new Uint8Array([1, 2, 3]))).toThrow();
210+
expect(() => mod.uint8Array4Sum(new Uint8Array([1, 2, 3, 4, 5]))).toThrow();
211+
});
212+
204213
it("float64Scale scales values", () => {
205214
const data = new Float64Array([1.0, 2.0, 3.0]);
206215
const result = mod.float64Scale(data, 2.5);

‎examples/js_dsl/mod.zig‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,16 @@ pub fn uint8Sum(data: Uint8Array) !Number {
190190
return Number.from(sum);
191191
}
192192

193+
/// Sum exactly four bytes copied from a Uint8Array into a Zig array.
194+
pub fn uint8Array4Sum(data: Uint8Array) !Number {
195+
const array = try data.toArray(4);
196+
var sum: i32 = 0;
197+
for (array) |byte| {
198+
sum += @intCast(byte);
199+
}
200+
return Number.from(sum);
201+
}
202+
193203
/// Scale all values in a Float64Array by a factor. Returns a new array.
194204
pub fn float64Scale(data: Float64Array, factor: Number) !Float64Array {
195205
const slice = try data.toSlice();

‎src/js/typed_arrays.zig‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,18 @@ pub fn TypedArray(comptime Element: type, comptime array_type: TypedarrayType) t
5050
return typed_ptr[0..info.length];
5151
}
5252

53+
/// Copies the TypedArray into a zig array of exactly `len` elements.
54+
///
55+
/// Returns:
56+
/// 1) `error.LengthMismatch` when the TypedArray length differs from
57+
/// `len`, or
58+
/// 2) `error.TypeMismatch` when its element type is wrong.
59+
pub fn toArray(self: Self, comptime len: usize) ![len]Element {
60+
const slice = try self.toSlice();
61+
if (slice.len != len) return error.LengthMismatch;
62+
return slice[0..len].*;
63+
}
64+
5365
/// Creates a new JavaScript TypedArray backed by an *external* (native-heap)
5466
/// ArrayBuffer.
5567
///

0 commit comments

Comments
 (0)