Skip to content

Commit

Permalink
Enable JSC (#38007)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #38007

Enable JSC for Bridgeless and wire it up with RNTester Android

Changelog: [Internal]

Reviewed By: fkgozali

Differential Revision: D46527692

fbshipit-source-id: 68f36a6db5573a70156864904bb428a4f789d4f5
  • Loading branch information
Lulu Wu authored and facebook-github-bot committed Jun 26, 2023
1 parent 85666e4 commit 016e77a
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 2 deletions.
1 change: 1 addition & 0 deletions packages/react-native/ReactAndroid/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,7 @@ android {
"rninstance",
"hermesinstancejni",
"uimanagerjni",
"jscinstance",
// prefab targets
"reactnativejni",
"react_render_debug",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

package com.facebook.react.bridgeless;

import com.facebook.infer.annotation.Nullsafe;
import com.facebook.jni.HybridData;
import com.facebook.jni.annotations.DoNotStrip;
import com.facebook.soloader.SoLoader;

@Nullsafe(Nullsafe.Mode.LOCAL)
public class JSCInstance extends JSEngineInstance {
static {
SoLoader.loadLibrary("jscinstance");
}

@DoNotStrip
protected static native HybridData initHybrid();

public JSCInstance() {
super(initHybrid());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.facebook.infer.annotation.Nullsafe;
import com.facebook.infer.annotation.ThreadConfined;
import com.facebook.infer.annotation.ThreadSafe;
import com.facebook.react.JSEngineResolutionAlgorithm;
import com.facebook.react.MemoryPressureRouter;
import com.facebook.react.ReactInstanceEventListener;
import com.facebook.react.bridge.Callback;
Expand Down Expand Up @@ -122,6 +123,7 @@ public class ReactHost implements ReactHostInterface {

private static final AtomicInteger mCounter = new AtomicInteger(0);
private final int mId = mCounter.getAndIncrement();
private @Nullable JSEngineResolutionAlgorithm mJSEngineResolutionAlgorithm = null;

public ReactHost(
Context context,
Expand Down Expand Up @@ -1504,4 +1506,13 @@ private void oldDestroyReactInstanceAndContext(final String callingMethod, final
}
}
}

public void setJSEngineResolutionAlgorithm(
@Nullable JSEngineResolutionAlgorithm jsEngineResolutionAlgorithm) {
mJSEngineResolutionAlgorithm = jsEngineResolutionAlgorithm;
}

public @Nullable JSEngineResolutionAlgorithm getJSEngineResolutionAlgorithm() {
return mJSEngineResolutionAlgorithm;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ add_react_android_subdir(src/main/jni/react/hermes/reactexecutor)
add_react_android_subdir(src/main/jni/react/hermes/instrumentation/)
add_react_android_subdir(src/main/jni/react/bridgeless/jni)
add_react_android_subdir(src/main/jni/react/bridgeless/hermes/jni)
add_react_android_subdir(src/main/jni/react/bridgeless/jsc/jni)

# GTest dependencies
add_executable(reactnative_unittest
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

cmake_minimum_required(VERSION 3.13)
set(CMAKE_VERBOSE_MAKEFILE on)

add_compile_options(-fvisibility=hidden -fexceptions -frtti)

file(GLOB jscinstance_SRC CONFIGURE_DEPENDS "*.cpp")
add_library(jscinstance SHARED ${jscinstance_SRC})

target_include_directories(jscinstance PUBLIC .)

target_link_libraries(
jscinstance
bridgeless
jscruntime
fbjni
reactnativejni
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

#include <fbjni/fbjni.h>
#include <jsc/JSCRuntime.h>
#include <jsi/jsi.h>
#include <react/bridgeless/JSEngineInstance.h>
#include <react/bridgeless/jni/JJSEngineInstance.h>
#include <react/jni/ReadableNativeMap.h>

namespace facebook::react {

class JSCInstance : public jni::HybridClass<JSCInstance, JJSEngineInstance> {
public:
static constexpr auto kJavaDescriptor =
"Lcom/facebook/react/bridgeless/JSCInstance;";

static jni::local_ref<jhybriddata> initHybrid(jni::alias_ref<jhybridobject>) {
return makeCxxInstance();
}

static void registerNatives() {
registerHybrid({
makeNativeMethod("initHybrid", JSCInstance::initHybrid),
});
}

std::unique_ptr<jsi::Runtime> createJSRuntime() noexcept {
return jsc::makeJSCRuntime();
}

private:
friend HybridBase;
using HybridBase::HybridBase;
};

} // namespace facebook::react

JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) {
return facebook::jni::initialize(
vm, [] { facebook::react::JSCInstance::registerNatives(); });
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import android.app.Application;
import androidx.annotation.NonNull;
import com.facebook.fbreact.specs.SampleTurboModule;
import com.facebook.react.JSEngineResolutionAlgorithm;
import com.facebook.react.ReactApplication;
import com.facebook.react.ReactNativeHost;
import com.facebook.react.ReactPackage;
Expand Down Expand Up @@ -144,11 +145,18 @@ public void onCreate() {
if (BuildConfig.IS_NEW_ARCHITECTURE_ENABLED) {
DefaultNewArchitectureEntryPoint.load();
}
ReactNativeFlipper.initializeFlipper(this, getReactNativeHost().getReactInstanceManager());
if (ReactFeatureFlags.enableBridgelessArchitecture) {
// TODO: initialize Flipper for Bridgeless
} else {
ReactNativeFlipper.initializeFlipper(this, getReactNativeHost().getReactInstanceManager());
}
}

@Override
public ReactNativeHost getReactNativeHost() {
if (ReactFeatureFlags.enableBridgelessArchitecture) {
throw new RuntimeException("Should not use ReactNativeHost when Bridgeless enabled");
}
return mReactNativeHost;
}

Expand All @@ -173,6 +181,11 @@ public ReactHostInterface reactHostInterface() {
true,
reactJsExceptionHandler,
true);
if (BuildConfig.IS_HERMES_ENABLED_IN_FLAVOR) {
mReactHost.setJSEngineResolutionAlgorithm(JSEngineResolutionAlgorithm.HERMES);
} else {
mReactHost.setJSEngineResolutionAlgorithm(JSEngineResolutionAlgorithm.JSC);
}
reactHostDelegate.setReactHost(mReactHost);
}
return mReactHost;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.facebook.fbreact.specs.SampleTurboModule;
import com.facebook.react.JSEngineResolutionAlgorithm;
import com.facebook.react.ReactPackage;
import com.facebook.react.ReactPackageTurboModuleManagerDelegate;
import com.facebook.react.TurboReactPackage;
import com.facebook.react.bridge.JSBundleLoader;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridgeless.BindingsInstaller;
import com.facebook.react.bridgeless.JSCInstance;
import com.facebook.react.bridgeless.JSEngineInstance;
import com.facebook.react.bridgeless.ReactHost;
import com.facebook.react.bridgeless.ReactHostDelegate;
Expand Down Expand Up @@ -77,7 +79,11 @@ public ReactPackageTurboModuleManagerDelegate.Builder getTurboModuleManagerDeleg

@Override
public JSEngineInstance getJSEngineInstance() {
return new HermesInstance();
if (mReactHost.getJSEngineResolutionAlgorithm() == JSEngineResolutionAlgorithm.JSC) {
return new JSCInstance();
} else {
return new HermesInstance();
}
}

@Override
Expand Down

0 comments on commit 016e77a

Please sign in to comment.