Skip to content

Commit

Permalink
🐞 fix: Switch to unique locker for inspector send
Browse files Browse the repository at this point in the history
  • Loading branch information
caoccao committed Feb 27, 2024
1 parent 0b42d1e commit 7db626a
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 11 deletions.
7 changes: 1 addition & 6 deletions cpp/jni/javet_jni_core_v8.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -420,12 +420,7 @@ JNIEXPORT void JNICALL Java_com_caoccao_javet_interop_V8Native_unregisterGCProlo

JNIEXPORT void JNICALL Java_com_caoccao_javet_interop_V8Native_v8InspectorSend
(JNIEnv* jniEnv, jobject caller, jlong v8RuntimeHandle, jstring mMessage) {
auto v8Runtime = Javet::V8Runtime::FromHandle(v8RuntimeHandle);
auto v8LockerForInspector = v8Runtime->GetSharedV8LockerForInspector();
auto v8IsolateScope = v8Runtime->GetV8IsolateScope();
V8HandleScope v8HandleScope(v8Runtime->v8Isolate);
auto v8Context = v8Runtime->GetV8LocalContext();
auto v8ContextScope = v8Runtime->GetV8ContextScope(v8Context);
RUNTIME_HANDLES_TO_OBJECTS_WITH_SCOPE_WITH_UNIQUE_LOCKER(v8RuntimeHandle);
char const* umMessage = jniEnv->GetStringUTFChars(mMessage, nullptr);
std::string message(umMessage, jniEnv->GetStringUTFLength(mMessage));
v8Runtime->v8Inspector->send(message);
Expand Down
8 changes: 8 additions & 0 deletions cpp/jni/javet_native.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@
auto v8Context = v8Runtime->GetV8LocalContext(); \
auto v8ContextScope = v8Runtime->GetV8ContextScope(v8Context);

#define RUNTIME_HANDLES_TO_OBJECTS_WITH_SCOPE_WITH_UNIQUE_LOCKER(v8RuntimeHandle) \
auto v8Runtime = Javet::V8Runtime::FromHandle(v8RuntimeHandle); \
auto v8Locker = v8Runtime->GetUniqueV8Locker(); \
auto v8IsolateScope = v8Runtime->GetV8IsolateScope(); \
V8HandleScope v8HandleScope(v8Runtime->v8Isolate); \
auto v8Context = v8Runtime->GetV8LocalContext(); \
auto v8ContextScope = v8Runtime->GetV8ContextScope(v8Context);

#define RUNTIME_AND_DATA_HANDLES_TO_OBJECTS_WITH_SCOPE(v8RuntimeHandle, v8DataHandle) \
auto v8Runtime = Javet::V8Runtime::FromHandle(v8RuntimeHandle); \
auto v8PersistentDataPointer = TO_V8_PERSISTENT_DATA_POINTER(v8DataHandle); \
Expand Down
5 changes: 0 additions & 5 deletions cpp/jni/javet_v8_runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,6 @@ namespace Javet {
return v8Locker ? v8Locker : std::make_shared<v8::Locker>(v8Isolate);
}

inline auto GetSharedV8LockerForInspector() const noexcept {
return v8LockerForInspector ? v8LockerForInspector : std::make_shared<v8::Locker>(v8Isolate);
}

/*
* Unique V8 locker is for explicit mode.
* Application manages the lock.
Expand Down Expand Up @@ -185,7 +181,6 @@ namespace Javet {
std::unique_ptr<v8::SnapshotCreator> v8SnapshotCreator;
std::unique_ptr<v8::StartupData, std::function<void(v8::StartupData*)>> v8StartupData;
std::shared_ptr<v8::Locker> v8Locker;
std::shared_ptr<v8::Locker> v8LockerForInspector;
V8PersistentContext v8PersistentContext;
};
}
Expand Down

0 comments on commit 7db626a

Please sign in to comment.