diff --git a/ReactAndroid/src/main/java/com/facebook/react/bridge/queue/NativeRunnable.java b/ReactAndroid/src/main/java/com/facebook/react/bridge/queue/NativeRunnable.java index 23eb266d4..d724405a9 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/bridge/queue/NativeRunnable.java +++ b/ReactAndroid/src/main/java/com/facebook/react/bridge/queue/NativeRunnable.java @@ -9,20 +9,19 @@ package com.facebook.react.bridge.queue; -import com.facebook.jni.Countable; +import com.facebook.jni.HybridData; import com.facebook.proguard.annotations.DoNotStrip; /** * A Runnable that has a native run implementation. */ @DoNotStrip -public class NativeRunnable extends Countable implements Runnable { +public class NativeRunnable implements Runnable { - /** - * Should only be instantiated via native (JNI) code. - */ - @DoNotStrip - private NativeRunnable() { + private final HybridData mHybridData; + + private NativeRunnable(HybridData hybridData) { + mHybridData = hybridData; } public native void run(); diff --git a/ReactAndroid/src/main/java/com/facebook/react/bridge/queue/NativeRunnableDeprecated.java b/ReactAndroid/src/main/java/com/facebook/react/bridge/queue/NativeRunnableDeprecated.java new file mode 100644 index 000000000..9e77c7eb8 --- /dev/null +++ b/ReactAndroid/src/main/java/com/facebook/react/bridge/queue/NativeRunnableDeprecated.java @@ -0,0 +1,27 @@ +/** + * Copyright (c) 2015-present, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + */ + +package com.facebook.react.bridge.queue; + +import com.facebook.jni.Countable; +import com.facebook.jni.HybridData; +import com.facebook.proguard.annotations.DoNotStrip; + +/** + * A Runnable that has a native run implementation. + */ +@DoNotStrip +public class NativeRunnableDeprecated extends Countable implements Runnable { + + @DoNotStrip + private NativeRunnableDeprecated() { + } + + public native void run(); +} diff --git a/ReactAndroid/src/main/jni/react/jni/JNativeRunnable.h b/ReactAndroid/src/main/jni/react/jni/JNativeRunnable.h new file mode 100644 index 000000000..5550af342 --- /dev/null +++ b/ReactAndroid/src/main/jni/react/jni/JNativeRunnable.h @@ -0,0 +1,44 @@ +// Copyright 2004-present Facebook. All Rights Reserved. + +#pragma once + +#include + +#include + +using namespace facebook::jni; + +namespace facebook { +namespace react { + +class Runnable : public JavaClass { +public: + static constexpr auto kJavaDescriptor = "Ljava/lang/Runnable;"; +}; + +/** + * The c++ interface for the Java NativeRunnable class + */ +class JNativeRunnable : public HybridClass { +public: + static auto constexpr kJavaDescriptor = "Lcom/facebook/react/bridge/queue/NativeRunnable;"; + + void run() { + m_runnable(); + } + + static void registerNatives() { + javaClassStatic()->registerNatives({ + makeNativeMethod("run", JNativeRunnable::run), + }); + } +private: + friend HybridBase; + + JNativeRunnable(std::function runnable) + : m_runnable(std::move(runnable)) {} + + std::function m_runnable; +}; + +} } diff --git a/ReactAndroid/src/main/jni/react/jni/OnLoad.cpp b/ReactAndroid/src/main/jni/react/jni/OnLoad.cpp index 5ef5c28aa..07e6549b9 100644 --- a/ReactAndroid/src/main/jni/react/jni/OnLoad.cpp +++ b/ReactAndroid/src/main/jni/react/jni/OnLoad.cpp @@ -13,6 +13,7 @@ #include #include #include +#include "JNativeRunnable.h" #include "JSLoader.h" #include "ReadableNativeArray.h" #include "ProxyExecutor.h" @@ -780,6 +781,7 @@ extern "C" JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved) { NativeArray::registerNatives(); ReadableNativeArray::registerNatives(); WritableNativeArray::registerNatives(); + JNativeRunnable::registerNatives(); registerNatives("com/facebook/react/bridge/NativeMap", { makeNativeMethod("initialize", map::initialize), @@ -861,7 +863,7 @@ extern "C" JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved) { }); - jclass nativeRunnableClass = env->FindClass("com/facebook/react/bridge/queue/NativeRunnable"); + jclass nativeRunnableClass = env->FindClass("com/facebook/react/bridge/queue/NativeRunnableDeprecated"); runnable::gNativeRunnableClass = (jclass)env->NewGlobalRef(nativeRunnableClass); runnable::gNativeRunnableCtor = env->GetMethodID(nativeRunnableClass, "", "()V"); wrap_alias(nativeRunnableClass)->registerNatives({