Skip to content

Commit

Permalink
revert
Browse files Browse the repository at this point in the history
  • Loading branch information
Kudo committed Aug 20, 2024
1 parent 8be8d99 commit 6459a2b
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 168 deletions.
14 changes: 11 additions & 3 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,8 +92,16 @@ class V8ExecutorHolder
std::move(config)));
}

static void onMainLoopIdle(jni::alias_ref<jclass>) {
MainLoopRegistry::GetInstance()->OnMainLoopIdle();
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 registerNatives() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
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;

Expand Down Expand Up @@ -56,5 +57,6 @@ private static native HybridData initHybrid(
int codecacheMode,
String codecacheDir);

/* package */ static native void onMainLoopIdle();
/* package */ static native void onMainLoopIdle(
RuntimeExecutor runtimeExecutor);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
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 Down Expand Up @@ -54,7 +55,10 @@ public boolean queueIdle() {
if (getReactApplicationContext().hasActiveReactInstance() &&
SystemClock.uptimeMillis() - mLastMainLoopIdleCallbackTime >
MAIN_LOOP_IDLE_THROTTLE) {
V8Executor.onMainLoopIdle();
final RuntimeExecutor runtimeExecutor = getReactApplicationContext()
.getCatalystInstance()
.getRuntimeExecutor();
V8Executor.onMainLoopIdle(runtimeExecutor);
mLastMainLoopIdleCallbackTime = SystemClock.uptimeMillis();
}
return true;
Expand Down
45 changes: 0 additions & 45 deletions src/v8runtime/MainLoopRegistry.cpp

This file was deleted.

41 changes: 0 additions & 41 deletions src/v8runtime/MainLoopRegistry.h

This file was deleted.

133 changes: 60 additions & 73 deletions src/v8runtime/V8Runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,70 +82,73 @@ V8Runtime::V8Runtime(
config_->deviceName);
inspectorClient_->ConnectToReactFrontend();
}
mainLoopIdleCallback_ = std::bind(&V8Runtime::OnMainLoopIdle, this);
MainLoopRegistry::GetInstance()->RegisterCallback(mainLoopIdleCallback_);
}

V8Runtime::V8Runtime(
const V8Runtime *v8Runtime,
std::unique_ptr<V8RuntimeConfig> config,
bool sharedGlobalContext)
std::unique_ptr<V8RuntimeConfig> config)
: config_(std::move(config)) {
if (!sharedGlobalContext) {
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;
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);
isolate_ = v8::Isolate::New(createParams);
#if defined(__ANDROID__)
if (!v8Runtime->config_->timezoneId.empty()) {
isolate_->DateTimeConfigurationChangeNotification(
v8::Isolate::TimeZoneDetection::kCustom,
v8Runtime->config_->timezoneId.c_str());
}
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_;
mainLoopIdleCallback_ = std::bind(&V8Runtime::OnMainLoopIdle, this);
MainLoopRegistry::GetInstance()->RegisterCallback(mainLoopIdleCallback_);
} else {
isSharedRuntime_ = true;
isolate_ = v8Runtime->isolate_;
jsQueue_ = v8Runtime->jsQueue_;
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_;

v8::Locker locker(isolate_);
v8::Isolate::Scope scopedIsolate(isolate_);
v8::HandleScope scopedHandle(isolate_);
context_.Reset(isolate_, CreateGlobalContext(isolate_));

auto localContext = context_.Get(isolate_);
localContext->SetSecurityToken(
v8Runtime->context_.Get(isolate_)->GetSecurityToken());

bool inheritProtoResult =
localContext->Global()
->GetPrototype()
.As<v8::Object>()
->SetPrototype(
localContext,
v8Runtime->context_.Get(isolate_)->Global()->GetPrototype())
.FromJust();
if (!inheritProtoResult) {
LOG(ERROR) << "Unable to inherit prototype from parent shared runtime.";
}
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_;

v8::Locker locker(isolate_);
v8::Isolate::Scope scopedIsolate(isolate_);
v8::HandleScope scopedHandle(isolate_);
context_.Reset(isolate_, CreateGlobalContext(isolate_));

auto localContext = context_.Get(isolate_);
localContext->SetSecurityToken(
v8Runtime->context_.Get(isolate_)->GetSecurityToken());

bool inheritProtoResult =
localContext->Global()
->GetPrototype()
.As<v8::Object>()
->SetPrototype(
localContext,
v8Runtime->context_.Get(isolate_)->Global()->GetPrototype())
.FromJust();
if (!inheritProtoResult) {
LOG(ERROR) << "Unable to inherit prototype from parent shared runtime.";
}

if (config_->enableInspector) {
Expand All @@ -156,11 +159,10 @@ V8Runtime::V8Runtime(
config_->deviceName);
inspectorClient_->ConnectToReactFrontend();
}
#endif
}

V8Runtime::~V8Runtime() {
MainLoopRegistry::GetInstance()->UnregisterCallback(mainLoopIdleCallback_);
mainLoopIdleCallback_ = nullptr;
{
v8::Locker locker(isolate_);
v8::Isolate::Scope scopedIsolate(isolate_);
Expand Down Expand Up @@ -311,10 +313,6 @@ V8Runtime::LoadCodeCacheIfNeeded(const std::string &sourceURL) {
return nullptr;
}

if (sourceURL.empty()) {
return nullptr;
}

if (config_->codecacheMode == V8RuntimeConfig::CodecacheMode::kNone) {
return nullptr;
}
Expand Down Expand Up @@ -349,10 +347,6 @@ bool V8Runtime::SaveCodeCacheIfNeeded(
return false;
}

if (sourceURL.empty()) {
return false;
}

if (cachedData && !cachedData->rejected) {
return false;
}
Expand Down Expand Up @@ -462,14 +456,7 @@ jsi::Value V8Runtime::evaluatePreparedJavaScript(
#if REACT_NATIVE_MINOR_VERSION >= 75 || \
(REACT_NATIVE_MINOR_VERSION >= 74 && REACT_NATIVE_PATCH_VERSION >= 3)
void V8Runtime::queueMicrotask(const jsi::Function &callback) {
v8::Locker locker(isolate_);
v8::Isolate::Scope scopedIsolate(isolate_);
v8::HandleScope scopedHandle(isolate_);
v8::Context::Scope scopedContext(context_.Get(isolate_));

v8::Local<v8::Function> v8Function =
JSIV8ValueConverter::ToV8Function(*this, callback);
isolate_->EnqueueMicrotask(v8Function);
// 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
Expand Down
5 changes: 1 addition & 4 deletions src/v8runtime/V8Runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#pragma once

#include <cxxreact/MessageQueueThread.h>
#include "MainLoopRegistry.h"
#include "V8RuntimeConfig.h"
#include "jsi/jsi.h"
#include "libplatform/libplatform.h"
Expand All @@ -27,8 +26,7 @@ class V8Runtime : public facebook::jsi::Runtime {
std::shared_ptr<facebook::react::MessageQueueThread> jsQueue);
V8Runtime(
const V8Runtime *v8Runtime,
std::unique_ptr<V8RuntimeConfig> config,
bool sharedGlobalContext = false);
std::unique_ptr<V8RuntimeConfig> config);
~V8Runtime();

// Calling this function when the platform main runloop is idle
Expand Down Expand Up @@ -279,7 +277,6 @@ class V8Runtime : public facebook::jsi::Runtime {
std::shared_ptr<InspectorClient> inspectorClient_;
bool isSharedRuntime_ = false;
std::shared_ptr<facebook::react::MessageQueueThread> jsQueue_;
MainLoopRegistry::Callback mainLoopIdleCallback_;
};

} // namespace rnv8

0 comments on commit 6459a2b

Please sign in to comment.