Skip to content

Commit 9c32140

Browse files
ryantremfacebook-github-bot
authored andcommitted
Enable array buffers in JSCRuntime.cpp (#28961)
Summary: The JavaScriptCore implementation of JSI [does not currently support array buffers](https://github.com/facebook/react-native/blob/master/ReactCommon/jsi/JSCRuntime.cpp#L925-L943). The comments in the code suggest the JSC version used by React Native does not work with array buffers, but this seems to be out of date since the current version of JSC used by React Native does indeed support array buffers. This change just enables array buffers in JSCRuntime.cpp. NOTE: See react-native-community/discussions-and-proposals#91 (comment) for more background on this change. ## Changelog [General] [Added] - Support for array buffers in the JavaScriptCore implementation of JSI Pull Request resolved: #28961 Test Plan: To test these changes, I just made some temporary changes to RNTester to use JSI to inject a test function into the JS runtime that reads from and writes to an array buffer, and call that function from the JS of the RNTester app (see ryantrem@28152ce). For the JS side of this, specifically look at https://github.com/ryantrem/react-native/blob/28152ce3f4ae0fa906557415d106399b3f072118/RNTester/js/RNTesterApp.android.js#L13-L18 For the native side of this, specifically look at https://github.com/ryantrem/react-native/blob/28152ce3f4ae0fa906557415d106399b3f072118/RNTester/android/app/src/main/cpp/JSITest.cpp#L22-L38 Reviewed By: shergin Differential Revision: D21717995 Pulled By: tmikov fbshipit-source-id: 5788479bb33c24d01aa80fa7f509e0ff9dcefea6
1 parent a5386ff commit 9c32140

File tree

1 file changed

+23
-11
lines changed

1 file changed

+23
-11
lines changed

ReactCommon/jsi/JSCRuntime.cpp

+23-11
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,9 @@ class JSCRuntime : public jsi::Runtime {
276276
#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_9_0
277277
#define _JSC_FAST_IS_ARRAY
278278
#endif
279+
#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_10_0
280+
#define _JSC_NO_ARRAY_BUFFERS
281+
#endif
279282
#endif
280283
#if defined(__MAC_OS_X_VERSION_MIN_REQUIRED)
281284
#if __MAC_OS_X_VERSION_MIN_REQUIRED >= __MAC_10_11
@@ -284,6 +287,9 @@ class JSCRuntime : public jsi::Runtime {
284287
// we understand why.
285288
#define _JSC_FAST_IS_ARRAY
286289
#endif
290+
#if __MAC_OS_X_VERSION_MIN_REQUIRED < __MAC_10_12
291+
#define _JSC_NO_ARRAY_BUFFERS
292+
#endif
287293
#endif
288294

289295
// JSStringRef utilities
@@ -922,24 +928,30 @@ bool JSCRuntime::isArray(const jsi::Object &obj) const {
922928
#endif
923929
}
924930

925-
bool JSCRuntime::isArrayBuffer(const jsi::Object & /*obj*/) const {
926-
// TODO: T23270523 - This would fail on builds that use our custom JSC
927-
// auto typedArrayType = JSValueGetTypedArrayType(ctx_, objectRef(obj),
928-
// nullptr); return typedArrayType == kJSTypedArrayTypeArrayBuffer;
931+
bool JSCRuntime::isArrayBuffer(const jsi::Object &obj) const {
932+
#if defined(_JSC_NO_ARRAY_BUFFERS)
929933
throw std::runtime_error("Unsupported");
934+
#else
935+
auto typedArrayType = JSValueGetTypedArrayType(ctx_, objectRef(obj), nullptr);
936+
return typedArrayType == kJSTypedArrayTypeArrayBuffer;
937+
#endif
930938
}
931939

932-
uint8_t *JSCRuntime::data(const jsi::ArrayBuffer & /*obj*/) {
933-
// TODO: T23270523 - This would fail on builds that use our custom JSC
934-
// return static_cast<uint8_t*>(
935-
// JSObjectGetArrayBufferBytesPtr(ctx_, objectRef(obj), nullptr));
940+
uint8_t *JSCRuntime::data(const jsi::ArrayBuffer &obj) {
941+
#if defined(_JSC_NO_ARRAY_BUFFERS)
936942
throw std::runtime_error("Unsupported");
943+
#else
944+
return static_cast<uint8_t *>(
945+
JSObjectGetArrayBufferBytesPtr(ctx_, objectRef(obj), nullptr));
946+
#endif
937947
}
938948

939-
size_t JSCRuntime::size(const jsi::ArrayBuffer & /*obj*/) {
940-
// TODO: T23270523 - This would fail on builds that use our custom JSC
941-
// return JSObjectGetArrayBufferByteLength(ctx_, objectRef(obj), nullptr);
949+
size_t JSCRuntime::size(const jsi::ArrayBuffer &obj) {
950+
#if defined(_JSC_NO_ARRAY_BUFFERS)
942951
throw std::runtime_error("Unsupported");
952+
#else
953+
return JSObjectGetArrayBufferByteLength(ctx_, objectRef(obj), nullptr);
954+
#endif
943955
}
944956

945957
bool JSCRuntime::isFunction(const jsi::Object &obj) const {

0 commit comments

Comments
 (0)