Skip to content

[core][Android] Improve JavaScriptArrayBuffer support#41404

Merged
barthap merged 6 commits intomainfrom
@barthap/array-buffers/js-android
Dec 4, 2025
Merged

[core][Android] Improve JavaScriptArrayBuffer support#41404
barthap merged 6 commits intomainfrom
@barthap/array-buffers/js-android

Conversation

@barthap
Copy link
Copy Markdown
Contributor

@barthap barthap commented Dec 4, 2025

Why

Follow up #39943, added support for returning array buffers from functions, and obtaining direct ByteBuffer for it

How

  • added JavaScriptArrayBuffer.toDirectBuffer()
  • added JNI to JSI converter

Test Plan

  • unit tests ✅

Checklist

@expo-bot expo-bot added the bot: suggestions ExpoBot has some suggestions label Dec 4, 2025
@expo-bot
Copy link
Copy Markdown
Collaborator

expo-bot commented Dec 4, 2025

The Pull Request introduced fingerprint changes against the base commit: 1ac84d3

Fingerprint diff
[
  {
    "op": "changed",
    "beforeSource": {
      "type": "dir",
      "filePath": "../../packages/expo-modules-core",
      "reasons": [
        "expoAutolinkingIos",
        "expoAutolinkingAndroid",
        "expoAutolinkingIos"
      ],
      "hash": "7a7c1d05c6b0afcac921753a366523e572ec704e"
    },
    "afterSource": {
      "type": "dir",
      "filePath": "../../packages/expo-modules-core",
      "reasons": [
        "expoAutolinkingIos",
        "expoAutolinkingAndroid",
        "expoAutolinkingIos"
      ],
      "hash": "e0c39816859f2e6e263c3ecdbba9b0f988110d80"
    }
  }
]

Generated by PR labeler 🤖

@expo-bot expo-bot added bot: passed checks ExpoBot has nothing to complain about and removed bot: suggestions ExpoBot has some suggestions labels Dec 4, 2025
@barthap barthap marked this pull request as ready for review December 4, 2025 11:00
@barthap barthap requested a review from lukmccall December 4, 2025 11:00
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Dec 4, 2025

Subscribed to pull request

File Patterns Mentions
packages/expo-modules-core/** @Kudo, @lukmccall

Generated by CodeMention

- Add types for `process.env.EXPO_DOM_HOST_OS`. ([#40382](https://github.com/expo/expo/pull/40382) by [@EvanBacon](https://github.com/EvanBacon))
- [Android] Add `ComposableScope` for compose Content functions. ([#39155](https://github.com/expo/expo/pull/39155) by [@aleqsio](https://github.com/aleqsio))
- [Android] Adds support for `ArrayBuffer`. ([#39943](https://github.com/expo/expo/pull/39943) by [@lukmccall](https://github.com/lukmccall))
- [Android] Adds support for `ArrayBuffer`. ([#39943](https://github.com/expo/expo/pull/39943) by [@lukmccall](https://github.com/lukmccall), [#41404](https://github.com/expo/expo/pull/41404) by [@barthap](https://github.com/barthap))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I would put it as a separate entry

@barthap barthap merged commit 5d86878 into main Dec 4, 2025
2 of 4 checks passed
@barthap barthap deleted the @barthap/array-buffers/js-android branch December 4, 2025 13:50
aleqsio pushed a commit that referenced this pull request Dec 5, 2025
# Why

Follow up #39943, added support for returning array buffers from
functions, and obtaining direct `ByteBuffer` for it

# How

- added `JavaScriptArrayBuffer.toDirectBuffer()`
- added JNI to JSI converter

# Test Plan

- unit tests ✅

# Checklist

- [x] I added a `changelog.md` entry and rebuilt the package sources
according to [this short
guide](https://github.com/expo/expo/blob/main/CONTRIBUTING.md#-before-submitting)
- [ ] This diff will work correctly for `npx expo prebuild` & EAS Build
(eg: updated a module plugin).
- [ ] Conforms with the [Documentation Writing Style
Guide](https://github.com/expo/expo/blob/main/guides/Expo%20Documentation%20Writing%20Style%20Guide.md)
barthap added a commit that referenced this pull request Dec 11, 2025
# Why

Follow up #41404

In addition to accepting ArrayBuffers from JS as arguments, add
possibility to create them in native code. Such native buffers are safe
to use on non-JS threads. Also add possibility to copy JS buffer into
native buffer.

# How

- Introduced `NativeArrayBuffer` type, that is backed by Java direct
`ByteBuffer`:
- Can be created in native code, either by allocating memory of given
size, wrapping existing `ByteBuffer` or copying an existing
`JavaScriptArrayBuffer`.
- Can be passed as function argument - it creates a copy of the original
buffer (in contrast to `JavaScriptArrayBuffer` which references original
memory)
- Can be returned to JS, thanks to the `jsi::ArrayBuffer(rt,
jsi::MutableBuffer&)` constructor. This allows zero-copy passing
allocated memory to JS (in contrast to the existing `ByteArray`
converter)
- Introduced `ArrayBuffer` interface for operations common for both JS
and native buffers - useful for APIs that can work with either buffer
type.

# Test Plan

- Unit tests ✅
- Manual testing in NCL

# Checklist

- [x] I added a `changelog.md` entry and rebuilt the package sources
according to [this short
guide](https://github.com/expo/expo/blob/main/CONTRIBUTING.md#-before-submitting)
- [ ] This diff will work correctly for `npx expo prebuild` & EAS Build
(eg: updated a module plugin).
- [ ] Conforms with the [Documentation Writing Style
Guide](https://github.com/expo/expo/blob/main/guides/Expo%20Documentation%20Writing%20Style%20Guide.md)

---------

Co-authored-by: Łukasz Kosmaty <kosmatylukasz@gmail.com>
aleqsio pushed a commit that referenced this pull request Dec 22, 2025
# Why

Follow up #41404

In addition to accepting ArrayBuffers from JS as arguments, add
possibility to create them in native code. Such native buffers are safe
to use on non-JS threads. Also add possibility to copy JS buffer into
native buffer.

# How

- Introduced `NativeArrayBuffer` type, that is backed by Java direct
`ByteBuffer`:
- Can be created in native code, either by allocating memory of given
size, wrapping existing `ByteBuffer` or copying an existing
`JavaScriptArrayBuffer`.
- Can be passed as function argument - it creates a copy of the original
buffer (in contrast to `JavaScriptArrayBuffer` which references original
memory)
- Can be returned to JS, thanks to the `jsi::ArrayBuffer(rt,
jsi::MutableBuffer&)` constructor. This allows zero-copy passing
allocated memory to JS (in contrast to the existing `ByteArray`
converter)
- Introduced `ArrayBuffer` interface for operations common for both JS
and native buffers - useful for APIs that can work with either buffer
type.

# Test Plan

- Unit tests ✅
- Manual testing in NCL

# Checklist

- [x] I added a `changelog.md` entry and rebuilt the package sources
according to [this short
guide](https://github.com/expo/expo/blob/main/CONTRIBUTING.md#-before-submitting)
- [ ] This diff will work correctly for `npx expo prebuild` & EAS Build
(eg: updated a module plugin).
- [ ] Conforms with the [Documentation Writing Style
Guide](https://github.com/expo/expo/blob/main/guides/Expo%20Documentation%20Writing%20Style%20Guide.md)

---------

Co-authored-by: Łukasz Kosmaty <kosmatylukasz@gmail.com>
barthap added a commit that referenced this pull request Jan 12, 2026
# Why

Follow up #41404 and #41415 for Android.
Adds support for `JavaScriptArrayBuffer` and `NativeArrayBuffer` in Expo
modules.

# How

- Similarly to Android version, created two types of ArrayBuffers:
  - `JavaScriptArrayBuffer` that wraps JSI ArrayBuffer object
- `NativeArrayBuffer` that manages direct memory, is thread safe, can be
created on native side. When used as argument type, a JS buffer is
copied. Then, zero-copy JSI ArrayBuffer is created upon returning.
- Both inherit the `ArrayBuffer` class which contains some utilities for
working with `Unsafe[...]Pointer`s and `Data`.

I tried to keep code organized similarly to the typed arrays
implementation, to keep it somehow consistent.

# Test Plan

- Created native unit test specs for the APIs.
- Manual testing in NCL

# Checklist

- [x] I added a `changelog.md` entry and rebuilt the package sources
according to [this short
guide](https://github.com/expo/expo/blob/main/CONTRIBUTING.md#-before-submitting)
- [ ] This diff will work correctly for `npx expo prebuild` & EAS Build
(eg: updated a module plugin).
- [ ] Conforms with the [Documentation Writing Style
Guide](https://github.com/expo/expo/blob/main/guides/Expo%20Documentation%20Writing%20Style%20Guide.md)

---------

Co-authored-by: Łukasz Kosmaty <kosmatylukasz@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot: fingerprint changed bot: passed checks ExpoBot has nothing to complain about

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants