2021-05-06 06:11:55 -06:00
|
|
|
//
|
2023-07-21 09:52:30 -06:00
|
|
|
// JSINSObjectConversion.mm
|
2021-05-06 06:11:55 -06:00
|
|
|
// VisionCamera
|
|
|
|
//
|
2021-09-29 04:54:51 -06:00
|
|
|
// Forked and Adjusted by Marc Rousavy on 02.05.21.
|
|
|
|
// Copyright © 2021 mrousavy & Facebook. All rights reserved.
|
|
|
|
//
|
|
|
|
// Forked and adjusted from: https://github.com/facebook/react-native/blob/900210cacc4abca0079e3903781bc223c80c8ac7/ReactCommon/react/nativemodule/core/platform/ios/RCTTurboModule.mm
|
|
|
|
// Original Copyright Notice:
|
|
|
|
//
|
|
|
|
// Copyright (c) Facebook, Inc. and its affiliates.
|
|
|
|
//
|
|
|
|
// This source code is licensed under the MIT license found in the
|
|
|
|
// LICENSE file in the root directory of this source tree.
|
2021-05-06 06:11:55 -06:00
|
|
|
//
|
2021-09-29 04:52:45 -06:00
|
|
|
|
2023-07-21 09:52:30 -06:00
|
|
|
#import "JSINSObjectConversion.h"
|
2021-05-06 06:11:55 -06:00
|
|
|
#import <Foundation/Foundation.h>
|
|
|
|
#import <jsi/jsi.h>
|
|
|
|
#import <ReactCommon/CallInvoker.h>
|
|
|
|
#import <React/RCTBridge.h>
|
|
|
|
#import <ReactCommon/TurboModuleUtils.h>
|
2023-07-20 07:30:04 -06:00
|
|
|
#import <ReactCommon/RCTBlockGuard.h>
|
2021-06-09 02:57:05 -06:00
|
|
|
#import "../Frame Processor/Frame.h"
|
2021-06-08 06:20:07 -06:00
|
|
|
#import "../Frame Processor/FrameHostObject.h"
|
2021-05-06 06:11:55 -06:00
|
|
|
|
|
|
|
using namespace facebook;
|
|
|
|
using namespace facebook::react;
|
|
|
|
|
2023-07-31 03:34:21 -06:00
|
|
|
namespace JSINSObjectConversion {
|
|
|
|
|
2021-05-06 06:11:55 -06:00
|
|
|
jsi::Value convertNSNumberToJSIBoolean(jsi::Runtime &runtime, NSNumber *value)
|
|
|
|
{
|
|
|
|
return jsi::Value((bool)[value boolValue]);
|
|
|
|
}
|
|
|
|
|
|
|
|
jsi::Value convertNSNumberToJSINumber(jsi::Runtime &runtime, NSNumber *value)
|
|
|
|
{
|
|
|
|
return jsi::Value([value doubleValue]);
|
|
|
|
}
|
|
|
|
|
|
|
|
jsi::String convertNSStringToJSIString(jsi::Runtime &runtime, NSString *value)
|
|
|
|
{
|
|
|
|
return jsi::String::createFromUtf8(runtime, [value UTF8String] ?: "");
|
|
|
|
}
|
|
|
|
|
|
|
|
jsi::Object convertNSDictionaryToJSIObject(jsi::Runtime &runtime, NSDictionary *value)
|
|
|
|
{
|
|
|
|
jsi::Object result = jsi::Object(runtime);
|
|
|
|
for (NSString *k in value) {
|
|
|
|
result.setProperty(runtime, [k UTF8String], convertObjCObjectToJSIValue(runtime, value[k]));
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
jsi::Array convertNSArrayToJSIArray(jsi::Runtime &runtime, NSArray *value)
|
|
|
|
{
|
|
|
|
jsi::Array result = jsi::Array(runtime, value.count);
|
|
|
|
for (size_t i = 0; i < value.count; i++) {
|
|
|
|
result.setValueAtIndex(runtime, i, convertObjCObjectToJSIValue(runtime, value[i]));
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
jsi::Value convertObjCObjectToJSIValue(jsi::Runtime &runtime, id value)
|
|
|
|
{
|
|
|
|
if (value == nil) {
|
|
|
|
return jsi::Value::undefined();
|
|
|
|
} else if ([value isKindOfClass:[NSString class]]) {
|
|
|
|
return convertNSStringToJSIString(runtime, (NSString *)value);
|
|
|
|
} else if ([value isKindOfClass:[NSNumber class]]) {
|
|
|
|
if ([value isKindOfClass:[@YES class]]) {
|
|
|
|
return convertNSNumberToJSIBoolean(runtime, (NSNumber *)value);
|
|
|
|
}
|
|
|
|
return convertNSNumberToJSINumber(runtime, (NSNumber *)value);
|
|
|
|
} else if ([value isKindOfClass:[NSDictionary class]]) {
|
|
|
|
return convertNSDictionaryToJSIObject(runtime, (NSDictionary *)value);
|
|
|
|
} else if ([value isKindOfClass:[NSArray class]]) {
|
|
|
|
return convertNSArrayToJSIArray(runtime, (NSArray *)value);
|
|
|
|
} else if (value == (id)kCFNull) {
|
|
|
|
return jsi::Value::null();
|
2021-06-09 02:57:05 -06:00
|
|
|
} else if ([value isKindOfClass:[Frame class]]) {
|
|
|
|
auto frameHostObject = std::make_shared<FrameHostObject>((Frame*)value);
|
|
|
|
return jsi::Object::createFromHostObject(runtime, frameHostObject);
|
2021-05-06 06:11:55 -06:00
|
|
|
}
|
|
|
|
return jsi::Value::undefined();
|
|
|
|
}
|
|
|
|
|
|
|
|
NSString *convertJSIStringToNSString(jsi::Runtime &runtime, const jsi::String &value)
|
|
|
|
{
|
|
|
|
return [NSString stringWithUTF8String:value.utf8(runtime).c_str()];
|
|
|
|
}
|
|
|
|
|
|
|
|
NSArray* convertJSICStyleArrayToNSArray(jsi::Runtime &runtime, const jsi::Value* array, size_t length, std::shared_ptr<CallInvoker> jsInvoker) {
|
2021-06-09 03:14:49 -06:00
|
|
|
if (length < 1) return @[];
|
2021-05-06 06:11:55 -06:00
|
|
|
NSMutableArray *result = [NSMutableArray new];
|
|
|
|
for (size_t i = 0; i < length; i++) {
|
|
|
|
// Insert kCFNull when it's `undefined` value to preserve the indices.
|
|
|
|
[result
|
|
|
|
addObject:convertJSIValueToObjCObject(runtime, array[i], jsInvoker) ?: (id)kCFNull];
|
|
|
|
}
|
|
|
|
return [result copy];
|
|
|
|
}
|
|
|
|
|
|
|
|
jsi::Value* convertNSArrayToJSICStyleArray(jsi::Runtime &runtime, NSArray* array) {
|
|
|
|
auto result = new jsi::Value[array.count];
|
|
|
|
for (size_t i = 0; i < array.count; i++) {
|
|
|
|
result[i] = convertObjCObjectToJSIValue(runtime, array[i]);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
NSArray* convertJSIArrayToNSArray(jsi::Runtime &runtime, const jsi::Array &value, std::shared_ptr<CallInvoker> jsInvoker)
|
|
|
|
{
|
|
|
|
size_t size = value.size(runtime);
|
|
|
|
NSMutableArray *result = [NSMutableArray new];
|
|
|
|
for (size_t i = 0; i < size; i++) {
|
|
|
|
// Insert kCFNull when it's `undefined` value to preserve the indices.
|
|
|
|
[result
|
|
|
|
addObject:convertJSIValueToObjCObject(runtime, value.getValueAtIndex(runtime, i), jsInvoker) ?: (id)kCFNull];
|
|
|
|
}
|
|
|
|
return [result copy];
|
|
|
|
}
|
|
|
|
|
|
|
|
NSDictionary* convertJSIObjectToNSDictionary(jsi::Runtime &runtime, const jsi::Object &value, std::shared_ptr<CallInvoker> jsInvoker)
|
|
|
|
{
|
|
|
|
jsi::Array propertyNames = value.getPropertyNames(runtime);
|
|
|
|
size_t size = propertyNames.size(runtime);
|
|
|
|
NSMutableDictionary *result = [NSMutableDictionary new];
|
|
|
|
for (size_t i = 0; i < size; i++) {
|
|
|
|
jsi::String name = propertyNames.getValueAtIndex(runtime, i).getString(runtime);
|
|
|
|
NSString *k = convertJSIStringToNSString(runtime, name);
|
|
|
|
id v = convertJSIValueToObjCObject(runtime, value.getProperty(runtime, name), jsInvoker);
|
|
|
|
if (v) {
|
|
|
|
result[k] = v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return [result copy];
|
|
|
|
}
|
|
|
|
|
|
|
|
id convertJSIValueToObjCObject(jsi::Runtime &runtime, const jsi::Value &value, std::shared_ptr<CallInvoker> jsInvoker)
|
|
|
|
{
|
|
|
|
if (value.isUndefined() || value.isNull()) {
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
if (value.isBool()) {
|
|
|
|
return @(value.getBool());
|
|
|
|
}
|
|
|
|
if (value.isNumber()) {
|
|
|
|
return @(value.getNumber());
|
|
|
|
}
|
|
|
|
if (value.isString()) {
|
|
|
|
return convertJSIStringToNSString(runtime, value.getString(runtime));
|
|
|
|
}
|
|
|
|
if (value.isObject()) {
|
|
|
|
jsi::Object o = value.getObject(runtime);
|
|
|
|
if (o.isArray(runtime)) {
|
|
|
|
return convertJSIArrayToNSArray(runtime, o.getArray(runtime), jsInvoker);
|
|
|
|
}
|
|
|
|
if (o.isFunction(runtime)) {
|
|
|
|
return convertJSIFunctionToCallback(runtime, std::move(o.getFunction(runtime)), jsInvoker);
|
|
|
|
}
|
2021-06-08 06:20:07 -06:00
|
|
|
if (o.isHostObject(runtime)) {
|
|
|
|
auto hostObject = o.asHostObject(runtime);
|
|
|
|
auto frame = dynamic_cast<FrameHostObject*>(hostObject.get());
|
|
|
|
if (frame != nullptr) {
|
2021-06-09 02:57:05 -06:00
|
|
|
return frame->frame;
|
2021-06-08 06:20:07 -06:00
|
|
|
}
|
|
|
|
}
|
2021-05-06 06:11:55 -06:00
|
|
|
return convertJSIObjectToNSDictionary(runtime, o, jsInvoker);
|
|
|
|
}
|
2023-07-31 03:34:21 -06:00
|
|
|
|
2021-05-06 06:11:55 -06:00
|
|
|
throw std::runtime_error("Unsupported jsi::jsi::Value kind");
|
|
|
|
}
|
|
|
|
|
|
|
|
RCTResponseSenderBlock convertJSIFunctionToCallback(jsi::Runtime &runtime, const jsi::Function &value, std::shared_ptr<CallInvoker> jsInvoker)
|
|
|
|
{
|
|
|
|
auto weakWrapper = CallbackWrapper::createWeak(value.getFunction(runtime), runtime, jsInvoker);
|
2023-07-20 07:30:04 -06:00
|
|
|
RCTBlockGuard *blockGuard = [[RCTBlockGuard alloc] initWithCleanup:^() {
|
|
|
|
auto strongWrapper = weakWrapper.lock();
|
|
|
|
if (strongWrapper) {
|
|
|
|
strongWrapper->destroy();
|
|
|
|
}
|
|
|
|
}];
|
2023-07-31 03:34:21 -06:00
|
|
|
|
2021-05-06 06:11:55 -06:00
|
|
|
BOOL __block wrapperWasCalled = NO;
|
|
|
|
RCTResponseSenderBlock callback = ^(NSArray *responses) {
|
|
|
|
if (wrapperWasCalled) {
|
|
|
|
throw std::runtime_error("callback arg cannot be called more than once");
|
|
|
|
}
|
2023-07-31 03:34:21 -06:00
|
|
|
|
2021-05-06 06:11:55 -06:00
|
|
|
auto strongWrapper = weakWrapper.lock();
|
|
|
|
if (!strongWrapper) {
|
|
|
|
return;
|
|
|
|
}
|
2023-07-31 03:34:21 -06:00
|
|
|
|
2023-07-20 07:30:04 -06:00
|
|
|
strongWrapper->jsInvoker().invokeAsync([weakWrapper, responses, blockGuard]() {
|
2021-05-06 06:11:55 -06:00
|
|
|
auto strongWrapper2 = weakWrapper.lock();
|
|
|
|
if (!strongWrapper2) {
|
|
|
|
return;
|
|
|
|
}
|
2023-07-31 03:34:21 -06:00
|
|
|
|
2021-05-06 06:11:55 -06:00
|
|
|
const jsi::Value* args = convertNSArrayToJSICStyleArray(strongWrapper2->runtime(), responses);
|
|
|
|
strongWrapper2->callback().call(strongWrapper2->runtime(), args, static_cast<size_t>(responses.count));
|
|
|
|
strongWrapper2->destroy();
|
|
|
|
delete[] args;
|
2023-07-31 03:34:21 -06:00
|
|
|
|
2023-07-20 07:30:04 -06:00
|
|
|
// Delete the CallbackWrapper when the block gets dealloced without being invoked.
|
|
|
|
(void)blockGuard;
|
2021-05-06 06:11:55 -06:00
|
|
|
});
|
2023-07-31 03:34:21 -06:00
|
|
|
|
2021-05-06 06:11:55 -06:00
|
|
|
wrapperWasCalled = YES;
|
|
|
|
};
|
2023-07-31 03:34:21 -06:00
|
|
|
|
2021-07-26 02:12:26 -06:00
|
|
|
return [callback copy];
|
2021-05-06 06:11:55 -06:00
|
|
|
}
|
2023-07-31 03:34:21 -06:00
|
|
|
|
|
|
|
} // namespace JSIJNIObjectConversion
|