Skip to content

Commit

Permalink
fix: disable shared global context and fix reanimated hanging issue (#…
Browse files Browse the repository at this point in the history
…210)

# Why

fixes #208

# How

- the reanimated problem was originally mentioned at software-mansion/react-native-reanimated#4953. since reanimated doesn't use the shared global context. this pr just comment out the code and unblock the reanimated integration.
- also did some refactoring to remove outdated code
  • Loading branch information
Kudo authored Aug 20, 2024
1 parent dbf2d61 commit 8733225
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 23 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>
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
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
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ public V8Module(ReactApplicationContext reactContext) {
}

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

@Override
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>
50 changes: 46 additions & 4 deletions src/v8runtime/V8Runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,44 @@ V8Runtime::V8Runtime(
const V8Runtime *v8Runtime,
std::unique_ptr<V8RuntimeConfig> config)
: config_(std::move(config)) {
arrayBufferAllocator_.reset(
v8::ArrayBuffer::Allocator::NewDefaultAllocator());
v8::Isolate::CreateParams createParams;
createParams.array_buffer_allocator = arrayBufferAllocator_.get();
if (v8Runtime->config_->snapshotBlob) {
snapshotBlob_ = std::make_unique<v8::StartupData>();
snapshotBlob_->data = v8Runtime->config_->snapshotBlob->c_str();
snapshotBlob_->raw_size =
static_cast<int>(v8Runtime->config_->snapshotBlob->size());
createParams.snapshot_blob = snapshotBlob_.get();
}
config_->codecacheMode = V8RuntimeConfig::CodecacheMode::kNone;

isolate_ = v8::Isolate::New(createParams);
#if defined(__ANDROID__)
if (!v8Runtime->config_->timezoneId.empty()) {
isolate_->DateTimeConfigurationChangeNotification(
v8::Isolate::TimeZoneDetection::kCustom,
v8Runtime->config_->timezoneId.c_str());
}
#endif
v8::Locker locker(isolate_);
v8::Isolate::Scope scopedIsolate(isolate_);
v8::HandleScope scopedHandle(isolate_);
context_.Reset(isolate_, CreateGlobalContext(isolate_));
v8::Context::Scope scopedContext(context_.Get(isolate_));
jsQueue_ = v8Runtime->jsQueue_;

if (config_->enableInspector) {
inspectorClient_ = std::make_shared<InspectorClient>(
jsQueue_,
context_.Get(isolate_),
config_->appName,
config_->deviceName);
inspectorClient_->ConnectToReactFrontend();
}

#if 0 // Experimental shared global context
isSharedRuntime_ = true;
isolate_ = v8Runtime->isolate_;
jsQueue_ = v8Runtime->jsQueue_;
Expand Down Expand Up @@ -121,6 +159,7 @@ V8Runtime::V8Runtime(
config_->deviceName);
inspectorClient_->ConnectToReactFrontend();
}
#endif
}

V8Runtime::~V8Runtime() {
Expand Down Expand Up @@ -414,11 +453,13 @@ jsi::Value V8Runtime::evaluatePreparedJavaScript(
return evaluateJavaScript(sourceJs, sourceJs->sourceURL());
}

#if REACT_NATIVE_MINOR_VERSION >= 75 || (REACT_NATIVE_MINOR_VERSION >= 74 && REACT_NATIVE_PATCH_VERSION >= 3)
#if REACT_NATIVE_MINOR_VERSION >= 75 || \
(REACT_NATIVE_MINOR_VERSION >= 74 && REACT_NATIVE_PATCH_VERSION >= 3)
void V8Runtime::queueMicrotask(const jsi::Function &callback) {
// TODO: add this when we revisit new architecture support
}
#endif // REACT_NATIVE_MINOR_VERSION >= 75 || (REACT_NATIVE_MINOR_VERSION >= 74 && REACT_NATIVE_PATCH_VERSION >= 3
#endif // REACT_NATIVE_MINOR_VERSION >= 75 || (REACT_NATIVE_MINOR_VERSION >= 74
// && REACT_NATIVE_PATCH_VERSION >= 3

bool V8Runtime::drainMicrotasks(int maxMicrotasksHint) {
v8::Locker locker(isolate_);
Expand Down Expand Up @@ -1559,8 +1600,9 @@ bool V8Runtime::instanceOf(const jsi::Object &o, const jsi::Function &f) {
}

#if REACT_NATIVE_MINOR_VERSION >= 74
void V8Runtime::setExternalMemoryPressure(const jsi::Object &obj, size_t amount) {
}
void V8Runtime::setExternalMemoryPressure(
const jsi::Object &obj,
size_t amount) {}
#endif

//
Expand Down
6 changes: 4 additions & 2 deletions src/v8runtime/V8Runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,11 @@ class V8Runtime : public facebook::jsi::Runtime {
const std::shared_ptr<const facebook::jsi::PreparedJavaScript> &js)
override;

#if REACT_NATIVE_MINOR_VERSION >= 75 || (REACT_NATIVE_MINOR_VERSION >= 74 && REACT_NATIVE_PATCH_VERSION >= 3)
#if REACT_NATIVE_MINOR_VERSION >= 75 || \
(REACT_NATIVE_MINOR_VERSION >= 74 && REACT_NATIVE_PATCH_VERSION >= 3)
void queueMicrotask(const facebook::jsi::Function &callback) override;
#endif // REACT_NATIVE_MINOR_VERSION >= 75 || (REACT_NATIVE_MINOR_VERSION >= 74 && REACT_NATIVE_PATCH_VERSION >= 3
#endif // REACT_NATIVE_MINOR_VERSION >= 75 || (REACT_NATIVE_MINOR_VERSION >= 74
// && REACT_NATIVE_PATCH_VERSION >= 3
bool drainMicrotasks(int maxMicrotasksHint = -1) override;

facebook::jsi::Object global() override;
Expand Down

0 comments on commit 8733225

Please sign in to comment.