Skip to content

Conversation

mediremi
Copy link
Member

@mediremi mediremi commented Aug 22, 2025

Closes #6757

While adding support for ArrayBuffer.t was simple
, supporting typed arrays (e.g. Int8Array) required changing Ast_untagged_variants.get_block_type_from_typ so it first looks at the unexpanded types (e.g. Int8Array) rather than just the fully expanded version (e.g. Stdlib_TypedArray.t).

Copy link

pkg-pr-new bot commented Aug 22, 2025

Open in StackBlitz

rescript

npm i https://pkg.pr.new/rescript-lang/rescript@7788

@rescript/darwin-arm64

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/darwin-arm64@7788

@rescript/darwin-x64

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/darwin-x64@7788

@rescript/linux-arm64

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/linux-arm64@7788

@rescript/linux-x64

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/linux-x64@7788

@rescript/win32-x64

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/win32-x64@7788

commit: 3a3f1d6

@mediremi mediremi changed the title Add support for ArrayBuffer to @unboxed Add support for ArrayBuffer and typed arrays to @unboxed Aug 22, 2025
| Some instance_type -> Some (InstanceType instance_type))
| {desc = Ttuple _} -> Some (InstanceType Array)
| _ -> None
(* First check the original (unexpanded) type for typed arrays and other instance types *)
Copy link
Member Author

Choose a reason for hiding this comment

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

The stdlib uses t = Stdlib_TypedArray.t<int> and t = Stdlib_TypedArray.t<float> for typed arrays, so we can't differentiate them by their expanded types.

@mediremi mediremi marked this pull request as ready for review August 22, 2025 12:24
@mediremi mediremi requested a review from zth August 22, 2025 12:25
@@ -1,5 +1,3 @@
module Array = Ocaml_Array
Copy link
Member Author

Choose a reason for hiding this comment

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

I removed this line and replaced arr[0] with arr->Array.getUnsafe(0) in the test to make compiling with ./cli/bsc.js tests/tests/src/UntaggedVariants.res easier (bsc would complain about unknown dependency Ocaml_Array otherwise)

@zth zth requested a review from cristianoc August 22, 2025 12:28
Copy link
Collaborator

@cristianoc cristianoc left a comment

Choose a reason for hiding this comment

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

Can we have a summary of how these new types compare to other types in terms of runtime representation?
Are they all objects and are all instance of cases mutually exclusive (with each other and with existing types).
Just double checking where this extension sits wrt what's already there.

@mediremi
Copy link
Member Author

mediremi commented Aug 23, 2025

typeof is "object" for ArrayBuffer and typed arrays:

const buffer = new ArrayBuffer(8)
const view = new Int32Array(buffer)

console.log(typeof buffer) // 'object'
console.log(typeof view) // 'object'

They can be differentiated with instanceof checks:

const buffer = new ArrayBuffer(8)
const view = new Int32Array(buffer)

console.log(buffer instanceof ArrayBuffer) // true
console.log(buffer instanceof Array) // false
console.log(Array.isArray(buffer)) // false
console.log(buffer instanceof DataView) // false

console.log(view instanceof Int32Array) // true
console.log(view instanceof Array) // false
console.log(Array.isArray(view)) // false
console.log(view instanceof ArrayBuffer) // false
console.log(view instanceof DataView) // false

@mediremi mediremi merged commit fe531a2 into rescript-lang:master Aug 23, 2025
27 checks passed
@cknitt
Copy link
Member

cknitt commented Aug 24, 2025

@mediremi Could you add a CHANGELOG entry for this PR?

@mediremi
Copy link
Member Author

@cknitt oops, sorry forgot to add one. Added an entry in #7800

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

@unboxed should support Js.TypedArray2.ArrayBuffer.t
3 participants