Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Kudo committed Aug 20, 2024
1 parent dbf2d61 commit 8be8d99
Show file tree
Hide file tree
Showing 11 changed files with 209 additions and 66 deletions.
31 changes: 23 additions & 8 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ File findNodePackageDir(String packageName, boolean absolute = true) {
return absolute ? dir.getAbsoluteFile() : dir
}

String toPlatformFileString(File path) {
static String toPlatformFileString(File path) {
def result = path.toString()
if (Os.isFamily(Os.FAMILY_WINDOWS)) {
result = result.replace(File.separatorChar, '/' as char)
Expand Down Expand Up @@ -89,6 +89,7 @@ if (v8AndroidVersionMajor < 11) {
ext.CODECACHE_MODE_NONE = 0
ext.CODECACHE_MODE_NORMAL = 1
ext.CODECACHE_MODE_STUB_BUNDLE = 2

def parseCacheMode(cacheMode) {
switch (cacheMode) {
case null:
Expand Down Expand Up @@ -118,6 +119,7 @@ def reactNativeArchitectures() {
apply plugin: "com.android.library"

android {
namespace "io.csie.kudo.reactnative.v8"
compileSdkVersion safeExtGet("compileSdkVersion", 33)

if (rootProject.hasProperty("ndkPath")) {
Expand Down Expand Up @@ -206,20 +208,28 @@ android {
}
}

task cleanCmakeCache() {
tasks.getByName("clean").dependsOn(cleanCmakeCache)
def cleanCmakeCache = tasks.register('cleanCmakeCache') {
doFirst {
delete "${projectDir}/.cxx"
}
}
tasks.named("clean").configure {
dependsOn(cleanCmakeCache)
}

task prepareHeadersForPrefab(type: Copy) {
tasks.register('prepareHeadersForPrefabV8Runtime', Copy) {
from("${projectDir}/../src/v8runtime")
include("**/*.h")
into(file("${prefabHeadersDir}/v8runtime"))
}

task extractSOFiles {
tasks.register('prepareHeadersForPrefabV8', Copy) {
from("${v8AndroidDir}/dist/include")
include("**/*.h")
into(file("${prefabHeadersDir}"))
}

tasks.register('extractSOFiles') {
doLast {
configurations.extractSO.files.each {
def file = it.absoluteFile
Expand Down Expand Up @@ -259,10 +269,15 @@ def nativeBuildDependsOn(dependsOnTask, buildTypesIncludes) {
}

afterEvaluate {
preBuild.dependsOn(prepareHeadersForPrefab)
preBuild.dependsOn(prepareHeadersForPrefabV8Runtime)
preBuild.dependsOn(prepareHeadersForPrefabV8)

nativeBuildDependsOn(extractSOFiles, null)

tasks.findByName("mergeReleaseJniLibFolders")?.dependsOn(extractSOFiles)
tasks.findByName("mergeDebugJniLibFolders")?.dependsOn(extractSOFiles)
tasks.named("mergeReleaseJniLibFolders")?.configure {
dependsOn(extractSOFiles)
}
tasks.named("mergeDebugJniLibFolders")?.configure {
dependsOn(extractSOFiles)
}
}
5 changes: 1 addition & 4 deletions android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="io.csie.kudo.reactnative.v8">

<manifest xmlns:android="http://schemas.android.com/apk/res/android">
</manifest>
14 changes: 3 additions & 11 deletions android/src/main/cpp/OnLoad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
#include <glog/logging.h>
#include <jni.h>
#include <react/jni/JReactMarker.h>
#include <react/jni/JRuntimeExecutor.h>
#include <react/jni/JSLoader.h>
#include <react/jni/JSLogging.h>
#include <react/jni/JavaScriptExecutorHolder.h>

#include "MainLoopRegistry.h"
#include "V8ExecutorFactory.h"
#include "V8Runtime.h"
#include "V8RuntimeConfig.h"
Expand Down Expand Up @@ -92,16 +92,8 @@ class V8ExecutorHolder
std::move(config)));
}

static void onMainLoopIdle(
jni::alias_ref<jclass>,
jni::alias_ref<facebook::react::JRuntimeExecutor::javaobject>
runtimeExecutor) {
runtimeExecutor->cthis()->get()([](jsi::Runtime &runtime) {
auto v8Runtime = dynamic_cast<V8Runtime *>(&runtime);
if (v8Runtime) {
v8Runtime->OnMainLoopIdle();
}
});
static void onMainLoopIdle(jni::alias_ref<jclass>) {
MainLoopRegistry::GetInstance()->OnMainLoopIdle();
}

static void registerNatives() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@
import android.os.Build;
import com.facebook.jni.HybridData;
import com.facebook.react.bridge.JavaScriptExecutor;
import com.facebook.react.bridge.RuntimeExecutor;
import com.facebook.soloader.SoLoader;
import io.csie.kudo.reactnative.v8.BuildConfig;
import java.io.File;

public class V8Executor extends JavaScriptExecutor {
static {
Expand Down Expand Up @@ -58,6 +56,5 @@ private static native HybridData initHybrid(
int codecacheMode,
String codecacheDir);

/* package */ static native void onMainLoopIdle(
RuntimeExecutor runtimeExecutor);
/* package */ static native void onMainLoopIdle();
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import android.os.SystemClock;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.RuntimeExecutor;
import com.facebook.react.bridge.UiThreadUtil;

public class V8Module
Expand All @@ -20,9 +19,9 @@ public V8Module(ReactApplicationContext reactContext) {
}

@Override
public void onCatalystInstanceDestroy() {
public void invalidate() {
unregisterMainIdleHandler();
super.onCatalystInstanceDestroy();
super.invalidate();
}

@Override
Expand Down Expand Up @@ -55,10 +54,7 @@ public boolean queueIdle() {
if (getReactApplicationContext().hasActiveReactInstance() &&
SystemClock.uptimeMillis() - mLastMainLoopIdleCallbackTime >
MAIN_LOOP_IDLE_THROTTLE) {
final RuntimeExecutor runtimeExecutor = getReactApplicationContext()
.getCatalystInstance()
.getRuntimeExecutor();
V8Executor.onMainLoopIdle(runtimeExecutor);
V8Executor.onMainLoopIdle();
mLastMainLoopIdleCallbackTime = SystemClock.uptimeMillis();
}
return true;
Expand Down
1 change: 1 addition & 0 deletions expo/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ buildscript {
}

android {
namespace "io.csie.kudo.reactnative.v8.expo"
compileSdkVersion safeExtGet("compileSdkVersion", 31)

compileOptions {
Expand Down
3 changes: 1 addition & 2 deletions expo/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
<manifest package="io.csie.kudo.reactnative.v8.expo" xmlns:android="http://schemas.android.com/apk/res/android">

<manifest xmlns:android="http://schemas.android.com/apk/res/android">
</manifest>
45 changes: 45 additions & 0 deletions src/v8runtime/MainLoopRegistry.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright (c) Kudo Chien.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

#include "MainLoopRegistry.h"

namespace rnv8 {

// static
MainLoopRegistry *MainLoopRegistry::GetInstance() {
static MainLoopRegistry instance;
return &instance;
}

void MainLoopRegistry::RegisterCallback(Callback callback) {
std::lock_guard<std::mutex> lock(mutex_);
callbacks_.push_back(std::move(callback));
}

void MainLoopRegistry::UnregisterCallback(const Callback &callback) {
std::lock_guard<std::mutex> lock(mutex_);
callbacks_.erase(
std::remove_if(
callbacks_.begin(),
callbacks_.end(),
[&callback](const Callback &registered_callback) {
return callback.target_type() ==
registered_callback.target_type() &&
callback.target<void()>() ==
registered_callback.target<void()>();
}),
callbacks_.end());
}

void MainLoopRegistry::OnMainLoopIdle() {
std::lock_guard<std::mutex> lock(mutex_);
for (const auto &callback : callbacks_) {
callback();
}
}

} // namespace rnv8
41 changes: 41 additions & 0 deletions src/v8runtime/MainLoopRegistry.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright (c) Kudo Chien.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

#pragma once

#include <algorithm>
#include <functional>
#include <mutex>
#include <vector>

namespace rnv8 {

class MainLoopRegistry {
public:
using Callback = std::function<void()>;

static MainLoopRegistry *GetInstance();

public:
void RegisterCallback(Callback callback);

void UnregisterCallback(const Callback &callback);

void OnMainLoopIdle();

private:
MainLoopRegistry() = default;
~MainLoopRegistry() = default;

MainLoopRegistry(const MainLoopRegistry &) = delete;
MainLoopRegistry &operator=(const MainLoopRegistry &) = delete;

std::vector<Callback> callbacks_;
std::mutex mutex_;
};

} // namespace rnv8
Loading

0 comments on commit 8be8d99

Please sign in to comment.