Skip to content
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

Added a workaround for Safari and BigUint64Array #411

Merged
merged 3 commits into from
Feb 8, 2022
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 bindings/web/rune/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@hotg-ai/rune",
"version": "0.11.5",
"version": "0.11.6",
"description": "Execute Runes inside a JavaScript environment.",
"repository": "https://github.com/hotg-ai/rune",
"homepage": "https://hotg.dev/",
Expand Down
77 changes: 73 additions & 4 deletions bindings/web/rune/src/Tensor.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
import Shape from "./Shape";

// Some versions of Safari doesn't support BigUint64Array and friends, and
// it's not possible to polyfill these types because bigint is a builtin type.
//
// This workaround lets us use them when possible and throws an exception at
// runtime when they aren't.
const BigUint64ArrayShim = global.BigUint64Array ?? class { constructor() { throw new Error("BigUint64Array is not supported on this device"); } };
const BigInt64ArrayShim = global.BigInt64Array ?? class { constructor() { throw new Error("BigInt64Array is not supported on this device"); } };

const typedArrays = {
"f64": Float64Array,
"i64": BigInt64Array,
"u64": BigUint64Array,
"i64": BigInt64ArrayShim,
"u64": BigUint64ArrayShim,
"f32": Float32Array,
"i32": Int32Array,
"u32": Uint32Array,
Expand All @@ -13,25 +21,86 @@ const typedArrays = {
"i8": Int8Array,
} as const;

/**
* An opaque tensor.
*/
export default class Tensor {
/**
* The raw bytes containing the tensor data.
*/
public readonly elements: Uint8Array;
/**
* The tensor's shape (element type and dimensions).
*/
public readonly shape: Shape;

constructor(shape: Shape, elements: Uint8Array) {
this.shape = shape;
this.elements = elements;
}

/**
* View this tensor's data as an array of 64-bit floats.
*
* This will fail if this isn't a f64 tensor.
*/
public asTypedArray(elementType: "f64"): Float64Array;
/**
* View this tensor's data as an array of 64-bit signed integers.
*
* This will fail if this isn't a i64 tensor. It may also fail on
* versions of Safari because they don't support BigInt64Array.
*/
public asTypedArray(elementType: "i64"): BigInt64Array;
/**
* View this tensor's data as an array of 64-bit unsigned integers.
*
* This will fail if this isn't a u64 tensor. It may also fail on
* versions of Safari because they don't support BigUint64Array.
*/
public asTypedArray(elementType: "u64"): BigUint64Array;
/**
* View this tensor's data as an array of 32-bit floats.
*
* This will fail if this isn't a f32 tensor.
*/
public asTypedArray(elementType: "f32"): Float32Array;
/**
* View this tensor's data as an array of 32-bit signed integers.
*
* This will fail if this isn't a i32 tensor.
*/
public asTypedArray(elementType: "i32"): Int32Array;
/**
* View this tensor's data as an array of 32-bit unsigned integers.
*
* This will fail if this isn't a u32 tensor.
*/
public asTypedArray(elementType: "u32"): Uint32Array;
public asTypedArray(elementType: "u16"): Uint16Array;
/**
* View this tensor's data as an array of 16-bit signed integers.
*
* This will fail if this isn't a i16 tensor.
*/
public asTypedArray(elementType: "i16"): Int16Array;
public asTypedArray(elementType: "u8"): Uint8ClampedArray;
/**
* View this tensor's data as an array of 16-bit unsigned integers.
*
* This will fail if this isn't a u16 tensor.
*/
public asTypedArray(elementType: "u16"): Uint16Array;
/**
* View this tensor's data as an array of 8-bit signed integers.
*
* This will fail if this isn't a i8 tensor.
*/
public asTypedArray(elementType: "i8"): Int8Array;
/**
* View this tensor's data as an array of 8-bit unsigned integers.
*
* This will fail if this isn't a u8 tensor.
*/
public asTypedArray(elementType: "u8"): Uint8ClampedArray;

public asTypedArray(elementType: keyof typeof typedArrays): ArrayBuffer {
if (this.shape.type != elementType) {
Expand Down