Skip to content

Commit 2d1097a

Browse files
jmagaramzth
authored andcommitted
Specific instance of typed array
1 parent 4818f79 commit 2d1097a

File tree

5 files changed

+255
-9
lines changed

5 files changed

+255
-9
lines changed
+47-6
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,55 @@
1+
/** The `BigInt64Array` typed array represents an array of 64-bit signed integers in platform byte order. See [BigInt64Array on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt64Array)
2+
*/
13
type t = Core__TypedArray.t<Core__BigInt.t>
24

35
module Constants = {
4-
@val external bytesPerElement: int = "BigInt64Array.BYTES_PER_ELEMENT"
6+
/**`bytesPerElement` returns the element size. See [BYTES_PER_ELEMENT on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/BYTES_PER_ELEMENT)
7+
*/
8+
@val
9+
external bytesPerElement: int = "BigInt64Array.BYTES_PER_ELEMENT"
510
}
611

7-
@new external make: array<int> => t = "BigInt64Array"
8-
@new external fromBuffer: Core__ArrayBuffer.t => t = "BigInt64Array"
9-
@new external fromBufferToEnd: (Core__ArrayBuffer.t, ~byteOffset: int) => t = "BigInt64Array"
12+
/** `fromArray` creates a `BitInt64Array` from an array of values. See [TypedArray constructor on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt64Array/BigInt64Array)
13+
*/
14+
@new
15+
external fromArray: array<Core__BigInt.t> => t = "BigInt64Array"
16+
17+
/** `fromBuffer` creates a `BitInt64Array` from an `ArrayBuffer.t`. See [TypedArray constructor on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt64Array/BigInt64Array)
18+
19+
**Note:** This is a potentially unsafe operation. Ensure the buffer is large enough and only accessed within its bounds.
20+
*/
21+
@new
22+
external fromBuffer: Core__ArrayBuffer.t => t = "BigInt64Array"
23+
24+
/** `fromBufferToEnd` creates a `BitInt64Array` from an `ArrayBuffer.t`, starting at a particular offset and continuing through to the end. See [TypedArray constructor on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt64Array/BigInt64Array)
25+
26+
**Note:** This is a potentially unsafe operation. Ensure the buffer is large enough and only accessed within its bounds.
27+
*/
28+
@new
29+
external fromBufferToEnd: (Core__ArrayBuffer.t, ~byteOffset: int) => t = "BigInt64Array"
30+
31+
/** `fromBufferWithRange` creates a `BitInt64Array` from an `ArrayBuffer.t`, starting at a particular offset and consuming `length` **bytes**. See [TypedArray constructor on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt64Array/BigInt64Array)
32+
33+
**Note:** This is a potentially unsafe operation. Ensure the buffer is large enough and only accessed within its bounds.
34+
*/
1035
@new
1136
external fromBufferWithRange: (Core__ArrayBuffer.t, ~byteOffset: int, ~length: int) => t =
1237
"BigInt64Array"
13-
@new external fromLength: int => t = "BigInt64Array"
14-
@val external from: 'a => t = "BigInt64Array.from"
38+
39+
/** `fromLength` creates a zero-initialized `BitInt64Array` to hold the specified count of numbers; this is **not** a byte length. See [TypedArray constructor on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt64Array/BigInt64Array)
40+
41+
**Note:** This is a potentially unsafe operation. Ensure the buffer is large enough and only accessed within its bounds.
42+
*/
43+
@new
44+
external fromLength: int => t = "BigInt64Array"
45+
46+
/** `fromArrayLikeOrIterable` creates a `BitInt64Array` from an array-like or iterable object. See [TypedArray.from on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/from)
47+
*/
48+
@val
49+
external fromArrayLikeOrIterable: 'a => t = "BigInt64Array.from"
50+
51+
/** `fromArrayLikeOrIterableWithMap` creates a `BitInt64Array` from an array-like or iterable object and applies the mapping function to each item. The mapping function expects (value, index). See [TypedArray.from on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/from)
52+
*/
53+
@val
54+
external fromArrayLikeOrIterableWithMap: ('a, ('b, int) => Core__BigInt.t) => t =
55+
"BigInt64Array.from"

test/TestSuite.mjs

+25-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import * as ArrayTests from "./ArrayTests.mjs";
66
import * as ErrorTests from "./ErrorTests.mjs";
77
import * as PromiseTest from "./PromiseTest.mjs";
88
import * as ResultTests from "./ResultTests.mjs";
9+
import * as TypedArrayTests from "./TypedArrayTests.mjs";
910

1011
var bign = TestTests.bign;
1112

@@ -29,12 +30,26 @@ var panicTest = ErrorTests.panicTest;
2930

3031
var $$catch = IntTests.$$catch;
3132

32-
var eq = ResultTests.eq;
33-
3433
var forEachIfOkCallFunction = ResultTests.forEachIfOkCallFunction;
3534

3635
var forEachIfErrorDoNotCallFunction = ResultTests.forEachIfErrorDoNotCallFunction;
3736

37+
var eq = TypedArrayTests.eq;
38+
39+
var num1 = TypedArrayTests.num1;
40+
41+
var num2 = TypedArrayTests.num2;
42+
43+
var num3 = TypedArrayTests.num3;
44+
45+
var assertTrue = TypedArrayTests.assertTrue;
46+
47+
var assertWillThrow = TypedArrayTests.assertWillThrow;
48+
49+
var areSame = TypedArrayTests.areSame;
50+
51+
var o = TypedArrayTests.o;
52+
3853
export {
3954
bign ,
4055
TestError ,
@@ -47,8 +62,15 @@ export {
4762
Concurrently ,
4863
panicTest ,
4964
$$catch ,
50-
eq ,
5165
forEachIfOkCallFunction ,
5266
forEachIfErrorDoNotCallFunction ,
67+
eq ,
68+
num1 ,
69+
num2 ,
70+
num3 ,
71+
assertTrue ,
72+
assertWillThrow ,
73+
areSame ,
74+
o ,
5375
}
5476
/* IntTests Not a pure module */

test/TestSuite.res

+1
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ include ErrorTests
44
include ArrayTests
55
include IntTests
66
include ResultTests
7+
include TypedArrayTests

test/TypedArrayTests.mjs

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
// Generated by ReScript, PLEASE EDIT WITH CARE
2+
3+
import * as Test from "./Test.mjs";
4+
import * as Curry from "rescript/lib/es6/curry.js";
5+
import * as Caml_obj from "rescript/lib/es6/caml_obj.js";
6+
import * as Core__Option from "../src/Core__Option.mjs";
7+
8+
var eq = Caml_obj.equal;
9+
10+
Test.run([
11+
[
12+
"TypedArrayTests.res",
13+
7,
14+
20,
15+
44
16+
],
17+
"bytes per element is 8"
18+
], BigInt64Array.BYTES_PER_ELEMENT, eq, 8);
19+
20+
var num1 = BigInt("123456789");
21+
22+
var num2 = BigInt("987654321");
23+
24+
var num3 = BigInt("555555555");
25+
26+
function assertTrue(message, predicate) {
27+
try {
28+
if (Curry._1(predicate, undefined)) {
29+
return ;
30+
}
31+
throw new Error(message);
32+
}
33+
catch (exn){
34+
throw new Error(message);
35+
}
36+
}
37+
38+
function assertWillThrow(message, f) {
39+
var didThrow = false;
40+
try {
41+
Curry._1(f, undefined);
42+
}
43+
catch (exn){
44+
didThrow = true;
45+
}
46+
if (didThrow !== false) {
47+
return ;
48+
}
49+
throw new Error(message);
50+
}
51+
52+
function areSame(x, y) {
53+
return x.toString() === y.toString();
54+
}
55+
56+
assertTrue("fromArray", (function (param) {
57+
return areSame(Core__Option.getExn(new BigInt64Array([
58+
num1,
59+
num2
60+
])[1]), num2);
61+
}));
62+
63+
assertTrue("fromBuffer", (function (param) {
64+
var x = new BigInt64Array(new ArrayBuffer(16));
65+
x[1] = num2;
66+
return areSame(Core__Option.getExn(x[1]), num2);
67+
}));
68+
69+
assertWillThrow("fromBuffer when too short can throw when used", (function (param) {
70+
var x = new BigInt64Array(new ArrayBuffer(1));
71+
x[0] = num1;
72+
areSame(Core__Option.getExn(x[0]), num1);
73+
}));
74+
75+
assertTrue("fromBufferWithRange", (function (param) {
76+
var x = new BigInt64Array(new ArrayBuffer(16), 0, 1);
77+
x[0] = num1;
78+
return areSame(Core__Option.getExn(x[0]), num1);
79+
}));
80+
81+
assertWillThrow("fromBufferWithRange is unsafe, out of range", (function (param) {
82+
var x = new BigInt64Array(new ArrayBuffer(16), 13, 1);
83+
x[0] = num1;
84+
areSame(Core__Option.getExn(x[0]), num1);
85+
}));
86+
87+
assertTrue("fromLength is NOT in bytes", (function (param) {
88+
var x = new BigInt64Array(1);
89+
return x.byteLength === 8;
90+
}));
91+
92+
function o(prim0, prim1) {
93+
return BigInt64Array.from(prim0, prim1);
94+
}
95+
96+
export {
97+
eq ,
98+
num1 ,
99+
num2 ,
100+
num3 ,
101+
assertTrue ,
102+
assertWillThrow ,
103+
areSame ,
104+
o ,
105+
}
106+
/* Not a pure module */

test/TypedArrayTests.res

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
open RescriptCore
2+
3+
let eq = (a, b) => a == b
4+
5+
// ===== BigInt64Array Tests =====
6+
7+
Test.run(__POS_OF__("bytes per element is 8"), BigInt64Array.Constants.bytesPerElement, eq, 8)
8+
9+
let num1 = BigInt.fromString("123456789")
10+
let num2 = BigInt.fromString("987654321")
11+
let num3 = BigInt.fromString("555555555")
12+
13+
let assertTrue = (message, predicate) => {
14+
try {
15+
if !predicate() {
16+
message->Error.make->Error.raise
17+
}
18+
} catch {
19+
| _ => message->Error.make->Error.raise
20+
}
21+
}
22+
23+
let assertWillThrow = (message, f) => {
24+
let didThrow = ref(false)
25+
try {
26+
f()
27+
} catch {
28+
| _ => didThrow := true
29+
}
30+
if didThrow.contents == false {
31+
message->Error.make->Error.raise
32+
}
33+
}
34+
35+
let areSame = (x: BigInt.t, y: BigInt.t) => BigInt.toString(x) == BigInt.toString(y)
36+
37+
// What's going on here?
38+
// assertTrue("big ints if different then not equal", () => num1 != num2)
39+
// assertTrue("big ints if same then equal", () => num1 == num1)
40+
41+
assertTrue("fromArray", () =>
42+
[num1, num2]->BigInt64Array.fromArray->TypedArray.get(1)->Option.getExn->areSame(num2)
43+
)
44+
45+
assertTrue("fromBuffer", () => {
46+
let x = ArrayBuffer.make(16)->BigInt64Array.fromBuffer
47+
x->TypedArray.set(1, num2)
48+
x->TypedArray.get(1)->Option.getExn->areSame(num2)
49+
})
50+
51+
assertWillThrow("fromBuffer when too short can throw when used", () => {
52+
let x = ArrayBuffer.make(1)->BigInt64Array.fromBuffer
53+
x->TypedArray.set(0, num1)
54+
x->TypedArray.get(0)->Option.getExn->areSame(num1)->ignore
55+
})
56+
57+
assertTrue("fromBufferWithRange", () => {
58+
let x = ArrayBuffer.make(16)->BigInt64Array.fromBufferWithRange(~byteOffset=0, ~length=1)
59+
x->TypedArray.set(0, num1)
60+
x->TypedArray.get(0)->Option.getExn->areSame(num1)
61+
})
62+
63+
// assertWillThrow("testing", () => {"a"->Error.make->Error.raise})
64+
// assertWillThrow("testing", () => {Console.log("f")})
65+
66+
assertWillThrow("fromBufferWithRange is unsafe, out of range", () => {
67+
let x = ArrayBuffer.make(16)->BigInt64Array.fromBufferWithRange(~byteOffset=13, ~length=1)
68+
x->TypedArray.set(0, num1)
69+
x->TypedArray.get(0)->Option.getExn->areSame(num1)->ignore
70+
})
71+
72+
assertTrue("fromLength is NOT in bytes", () => {
73+
let x = BigInt64Array.fromLength(1)
74+
TypedArray.byteLength(x) == 8
75+
})
76+
let o = BigInt64Array.fromArrayLikeOrIterableWithMap

0 commit comments

Comments
 (0)