Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions packages/react-native/NativeScriptNativeApi.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Pod::Spec.new do |s|
s.homepage = "https://github.com/NativeScript/runtimes"
s.license = "Apache-2.0"
s.author = package["author"]
s.platforms = { :ios => "15.1" }
s.platforms = { :ios => "15.1", :osx => "14.0" }
s.source = { :git => "https://github.com/NativeScript/runtimes.git", :tag => "react-native-v#{s.version}" }
s.requires_arc = false

Expand All @@ -23,10 +23,17 @@ Pod::Spec.new do |s|
"native-api/jsi/**/*.h",
"native-api/ffi/objc/hermes/NativeApiJsi.mm"
]
s.exclude_files = "ios/Fabric/**/*" unless fabric_enabled
s.ios.exclude_files = fabric_enabled ? [] : ["ios/Fabric/**/*"]
# Fabric component views host UIKit views; AppKit has no counterpart yet.
s.osx.exclude_files = [
"ios/Fabric/**/*"
]
s.public_header_files = "ios/**/*.h"
s.resource_bundles = {
"NativeScriptNativeApi" => ["metadata/*.nsmd"]
s.ios.resource_bundles = {
"NativeScriptNativeApi" => ["metadata/metadata.ios*.nsmd"]
}
s.osx.resource_bundles = {
"NativeScriptNativeApi" => ["metadata/metadata.macos*.nsmd"]
}
s.vendored_frameworks = "ios/vendor/Libffi.xcframework"

Expand Down
51 changes: 44 additions & 7 deletions packages/react-native/ios/NativeScriptNativeApiModule.mm
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

#import <Foundation/Foundation.h>
#import <TargetConditionals.h>
#if TARGET_OS_OSX
#import <AppKit/AppKit.h>
#else
#import <UIKit/UIKit.h>
#endif

#include <memory>
#include <mutex>
Expand All @@ -10,8 +15,10 @@

#include "NativeApiJsiReactNative.h"
#include "NativeScriptFabricGateway.h"
#if !TARGET_OS_OSX
#include "Fabric/NativeScriptComponentRegistration.h"
#include "Fabric/NativeScriptComponentView.h"
#endif

#import <React/RCTBridge+Private.h>
#import <React/RCTConvert.h>
Expand All @@ -37,7 +44,13 @@
}

std::string bundledMetadataPath() {
#if TARGET_OS_SIMULATOR
#if TARGET_OS_OSX
#if defined(__x86_64__)
NSString* metadataName = @"metadata.macos.x86_64";
#else
NSString* metadataName = @"metadata.macos.arm64";
#endif
#elif TARGET_OS_SIMULATOR
#if defined(__x86_64__)
NSString* metadataName = @"metadata.ios-sim.x86_64";
#else
Expand Down Expand Up @@ -199,13 +212,26 @@ bool nativeApiInstalled(facebook::jsi::Runtime& runtime) {
return imageLoader;
}

UIImage* imageWithRenderingMode(UIImage* image, bool isTemplate) {
#if TARGET_OS_OSX
using NativeScriptPlatformImage = NSImage;
#else
using NativeScriptPlatformImage = UIImage;
#endif

NativeScriptPlatformImage* imageWithRenderingMode(NativeScriptPlatformImage* image,
bool isTemplate) {
if (image == nil) {
return nil;
}

#if TARGET_OS_OSX
NSImage* copy = [[image copy] autorelease];
[copy setTemplate:isTemplate];
return copy;
#else
return [image imageWithRenderingMode:isTemplate ? UIImageRenderingModeAlwaysTemplate
: UIImageRenderingModeAlwaysOriginal];
#endif
}

id imageSourceFromJSIValue(facebook::jsi::Runtime& runtime,
Expand All @@ -225,9 +251,9 @@ id imageSourceFromJSIValue(facebook::jsi::Runtime& runtime,
void callImageLoadCallback(
std::weak_ptr<worklets::WorkletRuntime> workletRuntimeWeak,
std::shared_ptr<facebook::jsi::Function> callback,
UIImage* image,
NativeScriptPlatformImage* image,
NSString* errorMessage) {
UIImage* retainedImage = image != nil ? [image retain] : nil;
NativeScriptPlatformImage* retainedImage = image != nil ? [image retain] : nil;
std::string imageHandle =
retainedImage != nil ? nativeScriptHandleFromNSObject(retainedImage).UTF8String : "";
std::string errorText = errorMessage.UTF8String != nullptr ? errorMessage.UTF8String : "";
Expand Down Expand Up @@ -380,10 +406,12 @@ void callImageLoadCallback(
nativescript::InstallNativeApiJSI(workletRuntime, config);
}

#if !TARGET_OS_OSX
// ctx.emit / ctx.setContentSize / ctx.scheduleOnMainQueue targets
// (src/ui/dispatcher.ts); idempotent, safe to call on every
// install (including reload re-installs onto a fresh UI VM).
NativeScriptInstallComponentHostFunctions(workletRuntime);
#endif

std::weak_ptr<worklets::WorkletRuntime> imageWorkletRuntimeWeak(workletRuntimeRef);
auto loadImage = jsi::Function::createFromHostFunction(
Expand Down Expand Up @@ -420,11 +448,11 @@ void callImageLoadCallback(
resizeMode:RCTResizeModeCenter
progressBlock:^(int64_t, int64_t) {
}
partialLoadBlock:^(UIImage*) {
partialLoadBlock:^(NativeScriptPlatformImage*) {
}
completionBlock:^(NSError* error, UIImage* image) {
completionBlock:^(NSError* error, NativeScriptPlatformImage* image) {
dispatch_async(dispatch_get_main_queue(), ^{
UIImage* renderedImage =
NativeScriptPlatformImage* renderedImage =
imageWithRenderingMode(image, isTemplate);
callImageLoadCallback(
imageWorkletRuntimeWeak, callback, renderedImage,
Expand Down Expand Up @@ -479,6 +507,14 @@ void callImageLoadCallback(

bool NativeScriptNativeApiModule::registerComponent(jsi::Runtime& runtime, std::string name, jsi::Object spec,
double hookMask, double shouldBeRecycled) {
#if TARGET_OS_OSX
(void)runtime;
(void)name;
(void)spec;
(void)hookMask;
(void)shouldBeRecycled;
return false;
#else
if (name.empty()) {
return false;
}
Expand Down Expand Up @@ -515,6 +551,7 @@ void callImageLoadCallback(
BOOL shouldBeRecycledValue = shouldBeRecycled > 0;
NativeScriptRegisterFlavoredComponent(nsName, hookMaskValue, hasShouldBeRecycled, shouldBeRecycledValue);
return true;
#endif
}

} // namespace facebook::react
25 changes: 24 additions & 1 deletion scripts/build_react_native_turbomodule.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ VERSION_OVERRIDE=${NPM_PACKAGE_VERSION:-}
GENERATED_SIGNATURE_DISPATCH_OVERRIDE=${NS_SIGNATURE_BINDINGS_CPP_PATH:-${TNS_SIGNATURE_BINDINGS_CPP_PATH:-}}
GENERATED_SIGNATURE_DISPATCH=${GENERATED_SIGNATURE_DISPATCH_OVERRIDE:-"$REPO_ROOT/dist/intermediates/react-native/GeneratedSignatureDispatch.ios-sim.tmp.inc"}
DEVICE_SIGNATURE_DISPATCH="$REPO_ROOT/dist/intermediates/react-native/GeneratedSignatureDispatch.ios-device.tmp.inc"
MACOS_SIGNATURE_DISPATCH="$REPO_ROOT/dist/intermediates/react-native/GeneratedSignatureDispatch.macos.tmp.inc"
SKIP_PACK=false

function metadata_generator_source_hash {
Expand Down Expand Up @@ -55,6 +56,12 @@ mkdir -p "$(dirname "$DEVICE_SIGNATURE_DISPATCH")"
NS_SIGNATURE_BINDINGS_CPP_PATH="$DEVICE_SIGNATURE_DISPATCH" npm run metagen ios
rm -f "$DEVICE_SIGNATURE_DISPATCH" "$DEVICE_SIGNATURE_DISPATCH.stamp"

# Before ios-sim: each metagen run rewrites GeneratedGsdSignatureDispatch.inc,
# and the staged copy has to stay the ios-sim one.
checkpoint "Generating macOS metadata and signature dispatch bindings for the TurboModule..."
mkdir -p "$(dirname "$MACOS_SIGNATURE_DISPATCH")"
NS_SIGNATURE_BINDINGS_CPP_PATH="$MACOS_SIGNATURE_DISPATCH" npm run metagen macos

checkpoint "Generating Hermes signature dispatch bindings for the TurboModule..."
mkdir -p "$(dirname "$GENERATED_SIGNATURE_DISPATCH")"
NS_SIGNATURE_BINDINGS_CPP_PATH="$GENERATED_SIGNATURE_DISPATCH" npm run metagen ios-sim
Expand Down Expand Up @@ -106,13 +113,25 @@ cp -R "$REPO_ROOT/NativeScript/ffi/objc/hermes/." "$PACKAGE_DIR/native-api/ffi/o
cp -R "$REPO_ROOT/NativeScript/ffi/objc/shared/." "$PACKAGE_DIR/native-api/ffi/objc/shared/"
# Build leftovers, not sources.
find "$PACKAGE_DIR/native-api/ffi/objc" -name '*.stamp' -delete
cp "$GENERATED_SIGNATURE_DISPATCH" "$PACKAGE_DIR/native-api/ffi/objc/shared/GeneratedSignatureDispatch.inc"
# One pod serves iOS and macOS, so the dispatch table is chosen per platform at
# compile time. A signature missing from the table falls back to libffi.
cp "$GENERATED_SIGNATURE_DISPATCH" "$PACKAGE_DIR/native-api/ffi/objc/shared/GeneratedSignatureDispatch.ios.inc"
cp "$MACOS_SIGNATURE_DISPATCH" "$PACKAGE_DIR/native-api/ffi/objc/shared/GeneratedSignatureDispatch.macos.inc"
cat > "$PACKAGE_DIR/native-api/ffi/objc/shared/GeneratedSignatureDispatch.inc" <<'INC'
#include <TargetConditionals.h>
#if TARGET_OS_OSX
#include "GeneratedSignatureDispatch.macos.inc"
#else
#include "GeneratedSignatureDispatch.ios.inc"
#endif
INC
GENERATED_GSD_SIGNATURE_DISPATCH="$(dirname "$GENERATED_SIGNATURE_DISPATCH")/GeneratedGsdSignatureDispatch.inc"
if [ -f "$GENERATED_GSD_SIGNATURE_DISPATCH" ]; then
cp "$GENERATED_GSD_SIGNATURE_DISPATCH" "$PACKAGE_DIR/native-api/ffi/objc/shared/GeneratedGsdSignatureDispatch.inc"
fi
if [ -z "$GENERATED_SIGNATURE_DISPATCH_OVERRIDE" ]; then
rm -f "$GENERATED_SIGNATURE_DISPATCH" "$GENERATED_SIGNATURE_DISPATCH.stamp" \
"$MACOS_SIGNATURE_DISPATCH" "$MACOS_SIGNATURE_DISPATCH.stamp" \
"$GENERATED_GSD_SIGNATURE_DISPATCH"
fi
cp metadata-generator/include/Metadata.h "$PACKAGE_DIR/native-api/metadata/include/"
Expand All @@ -123,6 +142,8 @@ cp NativeScript/libffi/iphonesimulator-universal/include/ffitarget.h "$PACKAGE_D
cp metadata-generator/metadata/metadata.ios-sim.arm64.nsmd "$PACKAGE_DIR/metadata/"
cp metadata-generator/metadata/metadata.ios-sim.x86_64.nsmd "$PACKAGE_DIR/metadata/"
cp metadata-generator/metadata/metadata.ios.arm64.nsmd "$PACKAGE_DIR/metadata/"
cp metadata-generator/metadata/metadata.macos.arm64.nsmd "$PACKAGE_DIR/metadata/"
cp metadata-generator/metadata/metadata.macos.x86_64.nsmd "$PACKAGE_DIR/metadata/"

checkpoint "Staging iOS SDK TypeScript declarations..."
cp packages/objc-node-api/index.d.ts "$PACKAGE_DIR/types/objc-node-api/"
Expand Down Expand Up @@ -232,6 +253,8 @@ xcodebuild -create-xcframework \
-headers NativeScript/libffi/iphoneos-arm64/include \
-library NativeScript/libffi/iphonesimulator-universal/libffi.a \
-headers NativeScript/libffi/iphonesimulator-universal/include \
-library NativeScript/libffi/macosx-universal/libffi.a \
-headers NativeScript/libffi/macosx-universal/include \
-output "$PACKAGE_DIR/ios/vendor/Libffi.xcframework"

if [[ -n "$VERSION_OVERRIDE" ]]; then
Expand Down
6 changes: 4 additions & 2 deletions scripts/metagen.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,9 +276,11 @@ const sdks = {
macos: {
path: getSDKPath("macosx"),
frameworks: [...COMMON_FRAMEWORKS, ...MACOS_FRAMEWORKS],
// The generator leaves out declarations introduced after the target OS, so
// the target is the SDK itself; classes missing at runtime resolve to undefined.
targets: {
x86_64: "x86_64-apple-macos11.0",
arm64: "arm64-apple-macos11.0",
x86_64: `x86_64-apple-macos${getSDKVersion("macosx")}`,
arm64: `arm64-apple-macos${getSDKVersion("macosx")}`,
},
},
ios: {
Expand Down
Loading