Skip to content

Commit 8733225

Browse files
authored
fix: disable shared global context and fix reanimated hanging issue (#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
1 parent dbf2d61 commit 8733225

File tree

8 files changed

+78
-23
lines changed

8 files changed

+78
-23
lines changed

android/build.gradle

+23-8
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ File findNodePackageDir(String packageName, boolean absolute = true) {
2323
return absolute ? dir.getAbsoluteFile() : dir
2424
}
2525

26-
String toPlatformFileString(File path) {
26+
static String toPlatformFileString(File path) {
2727
def result = path.toString()
2828
if (Os.isFamily(Os.FAMILY_WINDOWS)) {
2929
result = result.replace(File.separatorChar, '/' as char)
@@ -89,6 +89,7 @@ if (v8AndroidVersionMajor < 11) {
8989
ext.CODECACHE_MODE_NONE = 0
9090
ext.CODECACHE_MODE_NORMAL = 1
9191
ext.CODECACHE_MODE_STUB_BUNDLE = 2
92+
9293
def parseCacheMode(cacheMode) {
9394
switch (cacheMode) {
9495
case null:
@@ -118,6 +119,7 @@ def reactNativeArchitectures() {
118119
apply plugin: "com.android.library"
119120

120121
android {
122+
namespace "io.csie.kudo.reactnative.v8"
121123
compileSdkVersion safeExtGet("compileSdkVersion", 33)
122124

123125
if (rootProject.hasProperty("ndkPath")) {
@@ -206,20 +208,28 @@ android {
206208
}
207209
}
208210

209-
task cleanCmakeCache() {
210-
tasks.getByName("clean").dependsOn(cleanCmakeCache)
211+
def cleanCmakeCache = tasks.register('cleanCmakeCache') {
211212
doFirst {
212213
delete "${projectDir}/.cxx"
213214
}
214215
}
216+
tasks.named("clean").configure {
217+
dependsOn(cleanCmakeCache)
218+
}
215219

216-
task prepareHeadersForPrefab(type: Copy) {
220+
tasks.register('prepareHeadersForPrefabV8Runtime', Copy) {
217221
from("${projectDir}/../src/v8runtime")
218222
include("**/*.h")
219223
into(file("${prefabHeadersDir}/v8runtime"))
220224
}
221225

222-
task extractSOFiles {
226+
tasks.register('prepareHeadersForPrefabV8', Copy) {
227+
from("${v8AndroidDir}/dist/include")
228+
include("**/*.h")
229+
into(file("${prefabHeadersDir}"))
230+
}
231+
232+
tasks.register('extractSOFiles') {
223233
doLast {
224234
configurations.extractSO.files.each {
225235
def file = it.absoluteFile
@@ -259,10 +269,15 @@ def nativeBuildDependsOn(dependsOnTask, buildTypesIncludes) {
259269
}
260270

261271
afterEvaluate {
262-
preBuild.dependsOn(prepareHeadersForPrefab)
272+
preBuild.dependsOn(prepareHeadersForPrefabV8Runtime)
273+
preBuild.dependsOn(prepareHeadersForPrefabV8)
263274

264275
nativeBuildDependsOn(extractSOFiles, null)
265276

266-
tasks.findByName("mergeReleaseJniLibFolders")?.dependsOn(extractSOFiles)
267-
tasks.findByName("mergeDebugJniLibFolders")?.dependsOn(extractSOFiles)
277+
tasks.named("mergeReleaseJniLibFolders")?.configure {
278+
dependsOn(extractSOFiles)
279+
}
280+
tasks.named("mergeDebugJniLibFolders")?.configure {
281+
dependsOn(extractSOFiles)
282+
}
268283
}

android/src/main/AndroidManifest.xml

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,2 @@
1-
2-
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3-
package="io.csie.kudo.reactnative.v8">
4-
1+
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
52
</manifest>

android/src/main/java/io/csie/kudo/reactnative/v8/executor/V8Executor.java

-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import com.facebook.react.bridge.RuntimeExecutor;
1616
import com.facebook.soloader.SoLoader;
1717
import io.csie.kudo.reactnative.v8.BuildConfig;
18-
import java.io.File;
1918

2019
public class V8Executor extends JavaScriptExecutor {
2120
static {

android/src/main/java/io/csie/kudo/reactnative/v8/executor/V8Module.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ public V8Module(ReactApplicationContext reactContext) {
2020
}
2121

2222
@Override
23-
public void onCatalystInstanceDestroy() {
23+
public void invalidate() {
2424
unregisterMainIdleHandler();
25-
super.onCatalystInstanceDestroy();
25+
super.invalidate();
2626
}
2727

2828
@Override

expo/build.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ buildscript {
1515
}
1616

1717
android {
18+
namespace "io.csie.kudo.reactnative.v8.expo"
1819
compileSdkVersion safeExtGet("compileSdkVersion", 31)
1920

2021
compileOptions {

expo/src/main/AndroidManifest.xml

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
<manifest package="io.csie.kudo.reactnative.v8.expo" xmlns:android="http://schemas.android.com/apk/res/android">
2-
1+
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
32
</manifest>

src/v8runtime/V8Runtime.cpp

+46-4
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,44 @@ V8Runtime::V8Runtime(
8888
const V8Runtime *v8Runtime,
8989
std::unique_ptr<V8RuntimeConfig> config)
9090
: config_(std::move(config)) {
91+
arrayBufferAllocator_.reset(
92+
v8::ArrayBuffer::Allocator::NewDefaultAllocator());
93+
v8::Isolate::CreateParams createParams;
94+
createParams.array_buffer_allocator = arrayBufferAllocator_.get();
95+
if (v8Runtime->config_->snapshotBlob) {
96+
snapshotBlob_ = std::make_unique<v8::StartupData>();
97+
snapshotBlob_->data = v8Runtime->config_->snapshotBlob->c_str();
98+
snapshotBlob_->raw_size =
99+
static_cast<int>(v8Runtime->config_->snapshotBlob->size());
100+
createParams.snapshot_blob = snapshotBlob_.get();
101+
}
102+
config_->codecacheMode = V8RuntimeConfig::CodecacheMode::kNone;
103+
104+
isolate_ = v8::Isolate::New(createParams);
105+
#if defined(__ANDROID__)
106+
if (!v8Runtime->config_->timezoneId.empty()) {
107+
isolate_->DateTimeConfigurationChangeNotification(
108+
v8::Isolate::TimeZoneDetection::kCustom,
109+
v8Runtime->config_->timezoneId.c_str());
110+
}
111+
#endif
112+
v8::Locker locker(isolate_);
113+
v8::Isolate::Scope scopedIsolate(isolate_);
114+
v8::HandleScope scopedHandle(isolate_);
115+
context_.Reset(isolate_, CreateGlobalContext(isolate_));
116+
v8::Context::Scope scopedContext(context_.Get(isolate_));
117+
jsQueue_ = v8Runtime->jsQueue_;
118+
119+
if (config_->enableInspector) {
120+
inspectorClient_ = std::make_shared<InspectorClient>(
121+
jsQueue_,
122+
context_.Get(isolate_),
123+
config_->appName,
124+
config_->deviceName);
125+
inspectorClient_->ConnectToReactFrontend();
126+
}
127+
128+
#if 0 // Experimental shared global context
91129
isSharedRuntime_ = true;
92130
isolate_ = v8Runtime->isolate_;
93131
jsQueue_ = v8Runtime->jsQueue_;
@@ -121,6 +159,7 @@ V8Runtime::V8Runtime(
121159
config_->deviceName);
122160
inspectorClient_->ConnectToReactFrontend();
123161
}
162+
#endif
124163
}
125164

126165
V8Runtime::~V8Runtime() {
@@ -414,11 +453,13 @@ jsi::Value V8Runtime::evaluatePreparedJavaScript(
414453
return evaluateJavaScript(sourceJs, sourceJs->sourceURL());
415454
}
416455

417-
#if REACT_NATIVE_MINOR_VERSION >= 75 || (REACT_NATIVE_MINOR_VERSION >= 74 && REACT_NATIVE_PATCH_VERSION >= 3)
456+
#if REACT_NATIVE_MINOR_VERSION >= 75 || \
457+
(REACT_NATIVE_MINOR_VERSION >= 74 && REACT_NATIVE_PATCH_VERSION >= 3)
418458
void V8Runtime::queueMicrotask(const jsi::Function &callback) {
419459
// TODO: add this when we revisit new architecture support
420460
}
421-
#endif // REACT_NATIVE_MINOR_VERSION >= 75 || (REACT_NATIVE_MINOR_VERSION >= 74 && REACT_NATIVE_PATCH_VERSION >= 3
461+
#endif // REACT_NATIVE_MINOR_VERSION >= 75 || (REACT_NATIVE_MINOR_VERSION >= 74
462+
// && REACT_NATIVE_PATCH_VERSION >= 3
422463

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

15611602
#if REACT_NATIVE_MINOR_VERSION >= 74
1562-
void V8Runtime::setExternalMemoryPressure(const jsi::Object &obj, size_t amount) {
1563-
}
1603+
void V8Runtime::setExternalMemoryPressure(
1604+
const jsi::Object &obj,
1605+
size_t amount) {}
15641606
#endif
15651607

15661608
//

src/v8runtime/V8Runtime.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,11 @@ class V8Runtime : public facebook::jsi::Runtime {
7575
const std::shared_ptr<const facebook::jsi::PreparedJavaScript> &js)
7676
override;
7777

78-
#if REACT_NATIVE_MINOR_VERSION >= 75 || (REACT_NATIVE_MINOR_VERSION >= 74 && REACT_NATIVE_PATCH_VERSION >= 3)
78+
#if REACT_NATIVE_MINOR_VERSION >= 75 || \
79+
(REACT_NATIVE_MINOR_VERSION >= 74 && REACT_NATIVE_PATCH_VERSION >= 3)
7980
void queueMicrotask(const facebook::jsi::Function &callback) override;
80-
#endif // REACT_NATIVE_MINOR_VERSION >= 75 || (REACT_NATIVE_MINOR_VERSION >= 74 && REACT_NATIVE_PATCH_VERSION >= 3
81+
#endif // REACT_NATIVE_MINOR_VERSION >= 75 || (REACT_NATIVE_MINOR_VERSION >= 74
82+
// && REACT_NATIVE_PATCH_VERSION >= 3
8183
bool drainMicrotasks(int maxMicrotasksHint = -1) override;
8284

8385
facebook::jsi::Object global() override;

0 commit comments

Comments
 (0)