From 3e0de014a0f986f6d5953ecdd53dd0d2d0ca7032 Mon Sep 17 00:00:00 2001 From: yanzhiqi1 Date: Sat, 5 Jul 2025 18:48:26 +0800 Subject: [PATCH] adapt multi-context Issue: #ICJDH0 Signed-off-by: yanzhiqi1 Change-Id: I832e221b5377fe5924b12518f9be4a20defa5c3d --- tooling/agent/debugger_impl.cpp | 12 +++++++++++- tooling/agent/runtime_impl.cpp | 2 +- tooling/backend/debugger_executor.cpp | 17 +++++++++++++---- tooling/backend/debugger_executor.h | 1 + 4 files changed, 26 insertions(+), 6 deletions(-) diff --git a/tooling/agent/debugger_impl.cpp b/tooling/agent/debugger_impl.cpp index 353b7424..63e33289 100755 --- a/tooling/agent/debugger_impl.cpp +++ b/tooling/agent/debugger_impl.cpp @@ -1265,10 +1265,20 @@ DispatchResponse DebuggerImpl::EvaluateOnCallFrame(const EvaluateOnCallFramePara return DispatchResponse::Create(ret); } + Local currentContext = DebuggerApi::GetCurrentGlobalEnv(vm_); + Local globalObj = JSNApi::GetGlobalObject(vm_, currentContext); + DebuggerExecutor::SetEvaluateToGlobal(vm_, globalObj); + + Local originContext = JSNApi::GetCurrentContext(vm_); + JSNApi::SwitchContext(vm_, currentContext); + auto funcRef = DebuggerApi::GenerateFuncFromBuffer(vm_, dest.data(), dest.size(), JSPandaFile::ENTRY_FUNCTION_NAME); auto res = DebuggerApi::EvaluateViaFuncCall(const_cast(vm_), funcRef, callFrameHandlers_[callFrameId]); + + JSNApi::SwitchContext(vm_, originContext); + if (vm_->GetJSThread()->HasPendingException()) { LOG_DEBUGGER(ERROR) << "EvaluateValue: has pending exception"; std::string msg; @@ -2384,7 +2394,7 @@ std::unique_ptr DebuggerImpl::GetGlobalScopeChain(const FrameHandler *fra .SetClassName(ObjectClassName::Global) .SetDescription(RemoteObject::GlobalDescription); globalScope->SetType(Scope::Type::Global()).SetObject(std::move(global)); - globalObj = JSNApi::GetGlobalObject(vm_); + globalObj = JSNApi::GetGlobalObject(vm_, DebuggerApi::GetCurrentGlobalEnv(vm_, frameHandler)); DebuggerApi::AddInternalProperties(vm_, globalObj, ArkInternalValueType::Scope, runtime_->internalObjects_); auto *sp = DebuggerApi::GetSp(frameHandler); scopeObjects_[sp][Scope::Type::Global()].push_back(runtime_->curObjectId_); diff --git a/tooling/agent/runtime_impl.cpp b/tooling/agent/runtime_impl.cpp index 31229018..663623b7 100644 --- a/tooling/agent/runtime_impl.cpp +++ b/tooling/agent/runtime_impl.cpp @@ -592,7 +592,7 @@ void RuntimeImpl::GetGeneratorObjectValue(Local value, SetKeyValue(jsValueRef, outPropertyDesc, "[[GeneratorState]]"); jsValueRef = genObjRef->GetGeneratorFunction(vm_); SetKeyValue(jsValueRef, outPropertyDesc, "[[GeneratorFunction]]"); - jsValueRef = JSNApi::GetGlobalObject(vm_); + jsValueRef = JSNApi::GetGlobalObject(vm_, DebuggerApi::GetCurrentGlobalEnv(vm_)); SetKeyValue(jsValueRef, outPropertyDesc, "[[GeneratorReceiver]]"); } } diff --git a/tooling/backend/debugger_executor.cpp b/tooling/backend/debugger_executor.cpp index 976ef29d..806618e7 100644 --- a/tooling/backend/debugger_executor.cpp +++ b/tooling/backend/debugger_executor.cpp @@ -23,10 +23,19 @@ void DebuggerExecutor::Initialize(const EcmaVM *vm) { [[maybe_unused]] EcmaHandleScope handleScope(vm->GetJSThread()); Local globalObj = JSNApi::GetGlobalObject(vm); - globalObj->Set(vm, StringRef::NewFromUtf8(vm, "debuggerSetValue"), FunctionRef::New( - const_cast(vm), DebuggerExecutor::DebuggerSetValue)); - globalObj->Set(vm, StringRef::NewFromUtf8(vm, "debuggerGetValue"), FunctionRef::New( - const_cast(vm), DebuggerExecutor::DebuggerGetValue)); + SetEvaluateToGlobal(vm, globalObj); +} + +void DebuggerExecutor::SetEvaluateToGlobal(const EcmaVM *vm, Local &globalObj) +{ + auto setStr = StringRef::NewFromUtf8(vm, "debuggerSetValue"); + auto getStr = StringRef::NewFromUtf8(vm, "debuggerGetValue"); + if (!globalObj->Has(vm, setStr) || !globalObj->Has(vm, getStr)) { + globalObj->Set(vm, setStr, FunctionRef::New( + const_cast(vm), DebuggerExecutor::DebuggerSetValue)); + globalObj->Set(vm, getStr, FunctionRef::New( + const_cast(vm), DebuggerExecutor::DebuggerGetValue)); + } } Local DebuggerExecutor::DebuggerGetValue(JsiRuntimeCallInfo *runtimeCallInfo) diff --git a/tooling/backend/debugger_executor.h b/tooling/backend/debugger_executor.h index 5f1f64fb..2527f048 100644 --- a/tooling/backend/debugger_executor.h +++ b/tooling/backend/debugger_executor.h @@ -29,6 +29,7 @@ public: ~DebuggerExecutor() = default; static void Initialize(const EcmaVM *vm); + static void SetEvaluateToGlobal(const EcmaVM *vm, Local &globalObj); static Local GetValue(const EcmaVM *vm, const FrameHandler *frameHandler, Local name); static bool SetValue(const EcmaVM *vm, FrameHandler *frameHandler, -- Gitee