-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switch from ArrayBuffer to ByteArray (#187)
According to [The Big O of Code Reviews](https://www.egorand.dev/the-big-o-of-code-reviews/), this is a O(_n_) change. ubrn lowers most types to an `ArrayBuffer` and then maps it to a `jsi::ArrayBuffer`, and then on to `RustBuffer`. `wasm-bindgen` maps a `Uint8Array` on to a `Vec<u8>`. In order to lower types to a `Uint8Array` for WASM and `ArrayBuffer` for JSI, some amount of bundler violence was needed. Given we're using multiple bundlers to package up the frontend Typescript, _and_ we're not really in control of what bundler the developer uses, this doesn't seem like a viable approach. Instead, this PR changes ubrn to lower types down to a `Uint8Array`, so that both WASM and React Native use the same underlying representation of byte arrays. Unfortunately, jsi doesn't expose API for typed arrays (it may in the future), so we look up the [`buffer` property](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray#instance_properties) to get an underlying `ArrayBuffer`. On the way back, an object of type `{ buffer: ArrayBuffer; }` is returned, which is enough to make the uniffi work and the typescript compiler think it that a typed array has come back from Rust/C++. Most of this PR was mechanical refactoring (e.g. `ArrayBuffer` renamed to `UniffiByteArray`) but there are enough manual changes which merit it being O(_n_).
- Loading branch information
Showing
22 changed files
with
177 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Upcoming releases | ||
|
||
## 🦊 What's Changed | ||
* Switched from passing `ArrayBuffer`s to using `Uint8Array`, to accommodate WASM better. ([#187](https://github.com/jhugman/uniffi-bindgen-react-native/pull/187)) | ||
|
||
**Full Changelog**: https://github.com/jhugman/uniffi-bindgen-react-native/compare/0.28.3-1...main | ||
|
||
# 0.28.3-1 | ||
|
||
This is the first supported release of the `uniffi-bindgen-react-native`. Please hack responsibly. Share and enjoy. | ||
|
||
## 🦊 What's Changed | ||
* Handle type parameter change in crnl 0.45.1 ([#182](https://github.com/jhugman/uniffi-bindgen-react-native/pull/182)) | ||
* Make first run more informative while compiling ([#185](https://github.com/jhugman/uniffi-bindgen-react-native/pull/185)) | ||
* Initial refactor in preparing for WASM ([#174](https://github.com/jhugman/uniffi-bindgen-react-native/pull/174)) | ||
* Add callbacks-example fixture from uniffi-rs ([#172](https://github.com/jhugman/uniffi-bindgen-react-native/pull/172)) | ||
* Fix CLI working without an extension ([#183](https://github.com/jhugman/uniffi-bindgen-react-native/pull/183)) | ||
* Use version released to Cocoapods and npm ([#184](https://github.com/jhugman/uniffi-bindgen-react-native/pull/184)) | ||
|
||
**Full Changelog**: https://github.com/jhugman/uniffi-bindgen-react-native/compare/0.28.3-0...0.28.3-1 | ||
|
||
/* | ||
## ✨ What's New | ||
|
||
## 🦊 What's Changed | ||
|
||
## ⚠️ Breaking Changes | ||
|
||
**Full Changelog**: https://github.com/jhugman/uniffi-bindgen-react-native/compare/{{previous}}...{{current}} | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/ | ||
*/ | ||
#pragma once | ||
|
||
#include "Bridging.h" | ||
#include <jsi/jsi.h> | ||
|
||
namespace uniffi_jsi { | ||
using namespace facebook; | ||
|
||
template <> struct Bridging<jsi::ArrayBuffer> { | ||
static jsi::ArrayBuffer value_to_arraybuffer(jsi::Runtime &rt, | ||
const jsi::Value &value) { | ||
try { | ||
return value.asObject(rt) | ||
.getPropertyAsObject(rt, "buffer") | ||
.getArrayBuffer(rt); | ||
} catch (const std::logic_error &e) { | ||
throw jsi::JSError(rt, e.what()); | ||
} | ||
} | ||
|
||
static jsi::Value arraybuffer_to_value(jsi::Runtime &rt, | ||
const jsi::ArrayBuffer &arrayBuffer) { | ||
try { | ||
jsi::Object obj(rt); | ||
obj.setProperty(rt, "buffer", arrayBuffer); | ||
return jsi::Value(rt, obj); | ||
} catch (const std::logic_error &e) { | ||
throw jsi::JSError(rt, e.what()); | ||
} | ||
} | ||
}; | ||
|
||
} // namespace uniffi_jsi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.