Kudo / react-native-v8

Opt-in V8 runtime for React Native Android
MIT License
900 stars 69 forks source link

Add experimental iOS support #166

Closed Kudo closed 1 year ago

Kudo commented 1 year ago

Why

Since Xcode 14 has deprecated BitCode, it is a good time to revisit iOS support.

How

Test Plan

$ yarn create expo-app sdk48
$ cd sdk48
$ npx expo install react-native-v8 v8-ios
$ npx expo run:ios 
aweffr commented 1 year ago

for app that don't use expo, is there any guide to integrate v8?

Kudo commented 1 year ago

@aweffr sorry i didn't get a chance to update the readme. the necessary change should only be inside AppDelegate.mm

  1. adding these imports
#ifndef FOLLY_NO_CONFIG
#define FOLLY_NO_CONFIG 1
#endif
#ifndef FOLLY_MOBILE
#define FOLLY_MOBILE 1
#endif
#ifndef FOLLY_USE_LIBCPP
#define FOLLY_USE_LIBCPP 1
#endif
#ifndef FOLLY_HAVE_PTHREAD
#define FOLLY_HAVE_PTHREAD 1
#endif
#import <memory>
#import <React/RCTCxxBridgeDelegate.h>
#import <React/RCTJSIExecutorRuntimeInstaller.h>
#import <RNV8/V8ExecutorFactory.h>
#if RCT_NEW_ARCH_ENABLED
#import <ReactCommon/RCTTurboModuleManager.h>
#endif
  1. adding the category
#if RCT_NEW_ARCH_ENABLED
@interface AppDelegate() <RCTCxxBridgeDelegate, RCTTurboModuleManagerDelegate>
@end
#else
@interface AppDelegate() <RCTCxxBridgeDelegate>
@end
#endif // RCT_NEW_ARCH_ENABLED
  1. adding the jsExecutorFactoryForBridge:
- (std::unique_ptr<facebook::react::JSExecutorFactory>)jsExecutorFactoryForBridge:(RCTBridge *)bridge
{
#if RCT_NEW_ARCH_ENABLED
  self.turboModuleManager = [[RCTTurboModuleManager alloc] initWithBridge:bridge
                                                                 delegate:self
                                                                jsInvoker:bridge.jsCallInvoker];
  // Necessary to allow NativeModules to lookup TurboModules
  [bridge setRCTTurboModuleRegistry:self.turboModuleManager];
#if RCT_DEV
  if (!RCTTurboModuleEagerInitEnabled()) {
    /**
     * Instantiating DevMenu has the side-effect of registering
     * shortcuts for CMD + d, CMD + i,  and CMD + n via RCTDevMenu.
     * Therefore, when TurboModules are enabled, we must manually create this
     * NativeModule.
     */
    [self.turboModuleManager moduleForName:"RCTDevMenu"];
  }
#endif // RCT_DEV
  return std::make_unique<rnv8::V8ExecutorFactory>(
      facebook::react::RCTJSIExecutorRuntimeInstaller([bridge, turboModuleManager = self.turboModuleManager](facebook::jsi::Runtime &runtime) {
        if (!bridge || !turboModuleManager) {
          return;
        }
        facebook::react::RuntimeExecutor syncRuntimeExecutor =
            [&](std::function<void(facebook::jsi::Runtime & runtime_)> &&callback) { callback(runtime); };
        [turboModuleManager installJSBindingWithRuntimeExecutor:syncRuntimeExecutor];
      }));
#else
  return std::make_unique<rnv8::V8ExecutorFactory>(facebook::react::RCTJSIExecutorRuntimeInstaller(nullptr));
#endif // RCT_NEW_ARCH_ENABLED
}