Skip to content

Commit 6074cc9

Browse files
committed
Add bit array FFI functions
1 parent 69b1a05 commit 6074cc9

File tree

4 files changed

+69
-29
lines changed

4 files changed

+69
-29
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020

2121
([Adi Salimgereyev](https://github.com/abs0luty))
2222

23+
- The JavaScript prelude JavaScript API now contains the `BitArray$isBitArray`
24+
and `BitArray$BitArray$data` functions.
25+
([Louis Pilfold](https://github.com/lpil))
26+
2327
### Build tool
2428

2529
- When adding a package that does not exist on Hex, the message is a bit

compiler-core/templates/prelude.d.mts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ export const List: {
3131
}
3232
export function List$Empty<T>(): List<T>;
3333
export function List$NonEmpty<T>(head: T, tail: List<T>): List<T>;
34-
export function List$isEmpty<T>(list: List<T>): boolean;
35-
export function List$isNonEmpty<T>(list: List<T>): boolean;
34+
export function List$isEmpty(list: any): boolean;
35+
export function List$isNonEmpty(list: any): boolean;
3636
export function List$NonEmpty$first<T>(list: List<T>): T | undefined;
3737
export function List$NonEmpty$rest<T>(list: List<T>): List<T> | undefined;
3838
/** @deprecated */
@@ -72,6 +72,8 @@ export function BitArray$BitArray(
7272
bitSize: number,
7373
bitOffset: number,
7474
): BitArray;
75+
export function BitArray$isBitArray(value: any): boolean;
76+
export function BitArray$BitArray$data(value: BitArray): DataView;
7577

7678
export interface UtfCodepoint {
7779
readonly __gleam: unique symbol;
@@ -95,8 +97,8 @@ export const Result: {
9597
}
9698
export function Result$Ok<T, E>(value: T): Result<T, E>;
9799
export function Result$Error<T, E>(error: E): Result<T, E>;
98-
export function Result$isError<T, E>(result: Result<T, E>): boolean;
99-
export function Result$isOk<T, E>(result: Result<T, E>): boolean;
100+
export function Result$isError(data: any): boolean;
101+
export function Result$isOk(data: any): boolean;
100102
export function Result$Ok$0<T, E>(result: Result<T, E>): T | undefined;
101103
export function Result$Error$0<T, E>(result: Result<T, E>): E | undefined;
102104
/** @deprecated */

compiler-core/templates/prelude.mjs

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -262,16 +262,11 @@ export class BitArray {
262262
/**
263263
* Returns this bit array's internal buffer.
264264
*
265-
* @deprecated Use `BitArray.byteAt()` or `BitArray.rawBuffer` instead.
265+
* @deprecated
266266
*
267267
* @returns {Uint8Array}
268268
*/
269269
get buffer() {
270-
bitArrayPrintDeprecationWarning(
271-
"buffer",
272-
"Use BitArray.byteAt() or BitArray.rawBuffer instead",
273-
);
274-
275270
if (this.bitOffset !== 0 || this.bitSize % 8 !== 0) {
276271
throw new globalThis.Error(
277272
"BitArray.buffer does not support unaligned bit arrays",
@@ -284,16 +279,11 @@ export class BitArray {
284279
/**
285280
* Returns the length in bytes of this bit array's internal buffer.
286281
*
287-
* @deprecated Use `BitArray.bitSize` or `BitArray.byteSize` instead.
282+
* @deprecated
288283
*
289284
* @returns {number}
290285
*/
291286
get length() {
292-
bitArrayPrintDeprecationWarning(
293-
"length",
294-
"Use BitArray.bitSize or BitArray.byteSize instead",
295-
);
296-
297287
if (this.bitOffset !== 0 || this.bitSize % 8 !== 0) {
298288
throw new globalThis.Error(
299289
"BitArray.length does not support unaligned bit arrays",
@@ -306,6 +296,11 @@ export class BitArray {
306296

307297
export const BitArray$BitArray = (buffer, bitSize, bitOffset) =>
308298
new BitArray(buffer, bitSize, bitOffset);
299+
export const BitArray$isBitArray = (value) => value instanceof BitArray;
300+
export const BitArray$BitArray$data = (bitArray) => {
301+
const array = bitArray.rawBuffer;
302+
return new DataView(array.buffer, array.byteOffest, bitArray.byteLength);
303+
};
309304

310305
/**
311306
* Returns the nth byte in the given buffer, after applying the specified bit
@@ -333,19 +328,6 @@ export class UtfCodepoint {
333328
}
334329
}
335330

336-
const isBitArrayDeprecationMessagePrinted = {};
337-
function bitArrayPrintDeprecationWarning(name, message) {
338-
if (isBitArrayDeprecationMessagePrinted[name]) {
339-
return;
340-
}
341-
342-
console.warn(
343-
`Deprecated BitArray.${name} property used in JavaScript FFI code. ${message}.`,
344-
);
345-
346-
isBitArrayDeprecationMessagePrinted[name] = true;
347-
}
348-
349331
/**
350332
* Slices a bit array to produce a new bit array. If `end` is not supplied then
351333
* all bits from `start` onward are returned.

test/javascript_prelude/main.mjs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import {
22
BitArray,
3+
BitArray$BitArray,
4+
BitArray$BitArray$data,
5+
BitArray$isBitArray,
36
CustomType,
47
Error,
58
List,
@@ -66,6 +69,14 @@ function assertThrows(msg, callable) {
6669
}
6770
}
6871

72+
function assert(msg, value) {
73+
if (value) {
74+
pass();
75+
} else {
76+
fail(msg);
77+
}
78+
}
79+
6980
class ExampleRecordImpl extends CustomType {
7081
constructor(first, detail, boop) {
7182
super();
@@ -1172,6 +1183,47 @@ assertEqual(new BitArray(new Uint8Array([1, 2])).byteSize, 2);
11721183
assertEqual(new BitArray(new Uint8Array([1, 2, 3, 4])).byteSize, 4);
11731184
assertEqual(new BitArray(new Uint8Array([1, 2, 3, 4], 28)).byteSize, 4);
11741185

1186+
// BitArray
1187+
1188+
assert("numbers are not bit arrays", !BitArray$isBitArray(123));
1189+
assert("strings are not bit arrays", !BitArray$isBitArray("!"));
1190+
assert("results are not bit arrays", new Ok("!"));
1191+
assert(
1192+
"Bit arrays are bit arrays",
1193+
BitArray$isBitArray(BitArray$BitArray(new Uint8Array([]))),
1194+
);
1195+
1196+
// Byte aligned bit array
1197+
{
1198+
const bitArray = BitArray$BitArray(new Uint8Array([1, 2, 3]));
1199+
assertEqual(bitArray.bitSize, 3 * 8);
1200+
assertEqual(bitArray.byteSize, 3);
1201+
assertEqual(bitArray.bitOffset, 0);
1202+
assertEqual(bitArray.rawBuffer, new Uint8Array([1, 2, 3]));
1203+
assertEqual(
1204+
BitArray$BitArray$data(bitArray),
1205+
new DataView(new Uint8Array([1, 2, 3]).buffer),
1206+
);
1207+
}
1208+
1209+
// Non-byte aligned bit array
1210+
{
1211+
const bitArray = BitArray$BitArray(new Uint8Array([1, 2, 3]), 23, 1);
1212+
assertEqual(bitArray.bitSize, 23);
1213+
assertEqual(bitArray.byteSize, 3);
1214+
assertEqual(bitArray.bitOffset, 1);
1215+
assertEqual(bitArray.rawBuffer, new Uint8Array([1, 2, 3]));
1216+
assertEqual(
1217+
BitArray$BitArray$data(bitArray),
1218+
new DataView(new Uint8Array([1, 2, 3]).buffer),
1219+
);
1220+
}
1221+
1222+
assertEqual(
1223+
BitArray$BitArray(new Uint8Array([])),
1224+
new BitArray(new Uint8Array([])),
1225+
);
1226+
11751227
//
11761228
// Division
11771229
//

0 commit comments

Comments
 (0)