Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ c.count; // 1 (getter, not a method call)
| `Object(T)` | `object` | `get()`, `set(value)` — `T` fields must be DSL types |
| `Function` | `Function` | `call(args)` |
| `Value` | `any` | `isNumber()`, `asNumber()`, type checking/narrowing |
| `Uint8Array` etc. | `TypedArray` | `toSlice()`, `from(slice)` |
| `Uint8Array` etc. | `TypedArray` | `toSlice()`, `toArray(len)`, `from(slice)` |
| `Promise(T)` | `Promise` | `resolve(value)`, `reject(err)` |

---
Expand Down
9 changes: 9 additions & 0 deletions examples/js_dsl/mod.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,15 @@ describe("typed arrays", () => {
expect(mod.uint8Sum(data)).toEqual(15);
});

it("toArray copies a TypedArray of the expected length", () => {
expect(mod.uint8Array4Sum(new Uint8Array([1, 2, 3, 4]))).toEqual(10);
});

it("toArray rejects TypedArrays of a different length", () => {
expect(() => mod.uint8Array4Sum(new Uint8Array([1, 2, 3]))).toThrow();
expect(() => mod.uint8Array4Sum(new Uint8Array([1, 2, 3, 4, 5]))).toThrow();
});

it("float64Scale scales values", () => {
const data = new Float64Array([1.0, 2.0, 3.0]);
const result = mod.float64Scale(data, 2.5);
Expand Down
10 changes: 10 additions & 0 deletions examples/js_dsl/mod.zig
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,16 @@ pub fn uint8Sum(data: Uint8Array) !Number {
return Number.from(sum);
}

/// Sum exactly four bytes copied from a Uint8Array into a Zig array.
pub fn uint8Array4Sum(data: Uint8Array) !Number {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is this fn for? Did this get committed accidentally?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nope, it's just an example usage of toArray (like the rest of the fn in this file)

but honestly im not a fan of the entire examples/ dir to begin with but not sure of a better alternative at this time

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, sorry, my sloppy review. I didn't see that this was an example.

const array = try data.toArray(4);
var sum: i32 = 0;
for (array) |byte| {
sum += @intCast(byte);
}
return Number.from(sum);
}

/// Scale all values in a Float64Array by a factor. Returns a new array.
pub fn float64Scale(data: Float64Array, factor: Number) !Float64Array {
const slice = try data.toSlice();
Expand Down
12 changes: 12 additions & 0 deletions src/js/typed_arrays.zig
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,18 @@ pub fn TypedArray(comptime Element: type, comptime array_type: TypedarrayType) t
return typed_ptr[0..info.length];
}

/// Copies the TypedArray into a zig array of exactly `len` elements.
///
/// Returns:
/// 1) `error.LengthMismatch` when the TypedArray length differs from
/// `len`, or
/// 2) `error.TypeMismatch` when its element type is wrong.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Side note, best for a separate PR: it would be nice to define the error sets for consumer use and use explicit error sets in return types.

pub fn toArray(self: Self, comptime len: usize) ![len]Element {
const slice = try self.toSlice();
if (slice.len != len) return error.LengthMismatch;
return slice[0..len].*;
}

/// Creates a new JavaScript TypedArray backed by an *external* (native-heap)
/// ArrayBuffer.
///
Expand Down
Loading