From ef6881f22a8436bb93d2dafeac8b53877f6881dc Mon Sep 17 00:00:00 2001 From: wengchangcheng Date: Thu, 7 Oct 2021 16:26:45 +0800 Subject: [PATCH] optimize get exception and get current frame Signed-off-by: wengchangcheng --- ecmascript/builtins/builtins_promise.cpp | 2 +- ecmascript/builtins/builtins_promise_handler.cpp | 2 +- ecmascript/ecma_language_context.cpp | 2 +- ecmascript/ecma_vm.cpp | 6 +++--- ecmascript/ecma_vm.h | 10 ---------- ecmascript/interpreter/interpreter-inl.h | 14 +++++++------- ecmascript/interpreter/slow_runtime_stub.cpp | 2 +- ecmascript/js_iterator.cpp | 6 +++--- ecmascript/js_promise.cpp | 2 +- ecmascript/js_thread.cpp | 5 +++++ ecmascript/js_thread.h | 6 ++---- ecmascript/tests/tagged_value_test.cpp | 2 +- ecmascript/tooling/interface/debugger_api.cpp | 3 +-- 13 files changed, 27 insertions(+), 35 deletions(-) diff --git a/ecmascript/builtins/builtins_promise.cpp b/ecmascript/builtins/builtins_promise.cpp index 0e57dd6ad3..6de1fbfe71 100644 --- a/ecmascript/builtins/builtins_promise.cpp +++ b/ecmascript/builtins/builtins_promise.cpp @@ -491,7 +491,7 @@ JSHandle BuiltinsPromise::PerformPromiseAll(JSThread *thread, // f. If nextValue is an abrupt completion, set iteratorRecord.[[done]] to true. if (thread->HasPendingException()) { itRecord->SetDone(thread, JSTaggedValue::True()); - nextVal = JSHandle(thread, thread->GetException()); + nextVal = thread->GetException(); } // g. ReturnIfAbrupt(nextValue). diff --git a/ecmascript/builtins/builtins_promise_handler.cpp b/ecmascript/builtins/builtins_promise_handler.cpp index 0d9a8cf690..a348734f61 100644 --- a/ecmascript/builtins/builtins_promise_handler.cpp +++ b/ecmascript/builtins/builtins_promise_handler.cpp @@ -75,7 +75,7 @@ JSTaggedValue BuiltinsPromiseHandler::Resolve(EcmaRuntimeCallInfo *argv) JSHandle thenValue = JSObject::GetProperty(thread, resolution, thenKey).GetValue(); if (thread->HasPendingException()) { if (!thenValue->IsJSError()) { - thenValue = JSHandle(thread, thread->GetException()); + thenValue = thread->GetException(); } thread->ClearException(); return JSPromise::RejectPromise(thread, resolvePromise, thenValue); diff --git a/ecmascript/ecma_language_context.cpp b/ecmascript/ecma_language_context.cpp index 5bd15494b5..52c0d01b3c 100644 --- a/ecmascript/ecma_language_context.cpp +++ b/ecmascript/ecma_language_context.cpp @@ -43,7 +43,7 @@ std::pair EcmaLanguageContext::GetCatchMethodAndOffset(Metho if (catchMethod->IsNative()) { continue; } - catchOffset = catchMethod->FindCatchBlock(jsThread->GetException().GetTaggedObject()->ClassAddr(), + catchOffset = catchMethod->FindCatchBlock(jsThread->GetException()->GetTaggedObject()->ClassAddr(), frameHandler.GetBytecodeOffset()); if (catchOffset != panda_file::INVALID_OFFSET) { break; diff --git a/ecmascript/ecma_vm.cpp b/ecmascript/ecma_vm.cpp index fd4b50b2c7..cb873d1d8f 100644 --- a/ecmascript/ecma_vm.cpp +++ b/ecmascript/ecma_vm.cpp @@ -441,7 +441,7 @@ Expected EcmaVM::InvokeEcmaEntrypoint(const panda_file::Fil // print exception information if (thread_->HasPendingException()) { auto exception = thread_->GetException(); - HandleUncaughtException(exception.GetTaggedObject()); + HandleUncaughtException(exception->GetTaggedObject()); } return 0; } @@ -471,10 +471,10 @@ bool EcmaVM::IsFrameworkPandaFile(std::string_view filename) const JSHandle EcmaVM::GetEcmaUncaughtException() const { - if (thread_->GetException().IsHole()) { + if (!thread_->HasPendingException()) { return JSHandle(); } - JSHandle exceptionHandle(thread_, thread_->GetException()); + JSHandle exceptionHandle = thread_->GetException(); thread_->ClearException(); // clear for ohos app return exceptionHandle; diff --git a/ecmascript/ecma_vm.h b/ecmascript/ecma_vm.h index 94c5079c85..2d25700b80 100644 --- a/ecmascript/ecma_vm.h +++ b/ecmascript/ecma_vm.h @@ -330,16 +330,6 @@ public: return moduleManager_; } - static constexpr uint32_t GetGlobalEnvOffset() - { - return MEMBER_OFFSET(EcmaVM, globalEnv_); - } - - static constexpr uint32_t GetMicroJobQueueOffset() - { - return MEMBER_OFFSET(EcmaVM, microJobQueue_); - } - void SetupRegExpResultCache(); JSHandle GetRegExpCache() { diff --git a/ecmascript/interpreter/interpreter-inl.h b/ecmascript/interpreter/interpreter-inl.h index a9232e5d88..c89ecde9fb 100644 --- a/ecmascript/interpreter/interpreter-inl.h +++ b/ecmascript/interpreter/interpreter-inl.h @@ -206,7 +206,7 @@ namespace panda::ecmascript { JSTaggedValue EcmaInterpreter::ExecuteNative(JSThread *thread, const CallParams& params) { INTERPRETER_TRACE(thread, ExecuteNative); - JSTaggedType *sp = const_cast(thread->GetCurrentSPFrame()); + JSTaggedType *sp = thread->currentFrame_; JSMethod *methodToCall = params.callTarget->GetCallTarget(); ASSERT(methodToCall->GetNumVregs() == 0); uint32_t numActualArgs = params.argc + RESERVED_CALL_ARGCOUNT; @@ -250,7 +250,7 @@ JSTaggedValue EcmaInterpreter::Execute(JSThread *thread, const CallParams& param return EcmaInterpreter::ExecuteNative(thread, params); } - JSTaggedType *originalPrevSp = const_cast(thread->GetCurrentSPFrame()); + JSTaggedType *originalPrevSp = thread->currentFrame_; // push break state // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic) @@ -327,7 +327,7 @@ JSTaggedValue EcmaInterpreter::GeneratorReEnterInterpreter(JSThread *thread, JSH JSHandle func = JSHandle::Cast(JSHandle(thread, context->GetMethod())); JSMethod *method = func->GetCallTarget(); - JSTaggedType *currentSp = const_cast(thread->GetCurrentSPFrame()); + JSTaggedType *currentSp = thread->currentFrame_; // push break frame // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic) @@ -388,7 +388,7 @@ void EcmaInterpreter::ChangeGenContext(JSThread *thread, JSHandle func = JSHandle::Cast(JSHandle(thread, context->GetMethod())); JSMethod *method = func->GetCallTarget(); - JSTaggedType *currentSp = const_cast(thread->GetCurrentSPFrame()); + JSTaggedType *currentSp = thread->currentFrame_; // push break frame // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic) @@ -436,7 +436,7 @@ void EcmaInterpreter::ChangeGenContext(JSThread *thread, JSHandle(thread->GetCurrentSPFrame()); + JSTaggedType *sp = thread->currentFrame_; FrameState *state = GET_FRAME(sp); thread->SetCurrentSPFrame(state->prev); } @@ -3172,7 +3172,7 @@ NO_UB_SANITIZE void EcmaInterpreter::RunInternal(JSThread *thread, ConstantPool DISPATCH(BytecodeInstruction::Format::PREF_NONE); } HANDLE_OPCODE(EXCEPTION_HANDLER) { - auto exception = thread->GetException(); + auto exception = thread->GetException().GetTaggedValue(); EcmaFrameHandler frameHandler(sp); uint32_t pcOffset = panda_file::INVALID_OFFSET; @@ -3207,7 +3207,7 @@ NO_UB_SANITIZE void EcmaInterpreter::RunInternal(JSThread *thread, ConstantPool void EcmaInterpreter::InitStackFrame(JSThread *thread) { - uint64_t *prevSp = const_cast(thread->GetCurrentSPFrame()); + uint64_t *prevSp = thread->currentFrame_; FrameState *state = GET_FRAME(prevSp); state->pc = nullptr; state->sp = nullptr; diff --git a/ecmascript/interpreter/slow_runtime_stub.cpp b/ecmascript/interpreter/slow_runtime_stub.cpp index 43f35cdbf2..89795d081f 100644 --- a/ecmascript/interpreter/slow_runtime_stub.cpp +++ b/ecmascript/interpreter/slow_runtime_stub.cpp @@ -941,7 +941,7 @@ JSTaggedValue SlowRuntimeStub::CloseIterator(JSThread *thread, JSTaggedValue ite JSHandle record; if (thread->HasPendingException()) { record = JSHandle(factory->NewCompletionRecord(CompletionRecord::THROW, - JSHandle(thread, thread->GetException()))); + thread->GetException())); } else { JSHandle undefinedVal = globalConst->GetHandledUndefined(); record = JSHandle( diff --git a/ecmascript/js_iterator.cpp b/ecmascript/js_iterator.cpp index 8ba9a3a871..9759e5436e 100644 --- a/ecmascript/js_iterator.cpp +++ b/ecmascript/js_iterator.cpp @@ -29,9 +29,9 @@ JSTaggedValue JSIterator::IteratorCloseAndReturn(JSThread *thread, const JSHandl { ASSERT(thread->HasPendingException()); ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); - JSTaggedValue exception = thread->GetException(); + JSHandle exception = thread->GetException(); JSHandle record = JSHandle(factory->NewCompletionRecord(CompletionRecord::THROW, - JSHandle(thread, exception))); + exception)); JSHandle result = JSIterator::IteratorClose(thread, iter, record); if (result->IsCompletionRecord()) { return CompletionRecord::Cast(result->GetTaggedObject())->GetValue(); @@ -153,7 +153,7 @@ JSHandle JSIterator::IteratorClose(JSThread *thread, const JSHand const GlobalEnvConstants *globalConst = thread->GlobalConstants(); JSHandle exceptionOnThread; if (thread->HasPendingException()) { - exceptionOnThread = JSHandle(thread, thread->GetException()); + exceptionOnThread = thread->GetException(); thread->ClearException(); } JSHandle returnStr(globalConst->GetHandledReturnString()); diff --git a/ecmascript/js_promise.cpp b/ecmascript/js_promise.cpp index a8a3a1f41e..06ecc49610 100644 --- a/ecmascript/js_promise.cpp +++ b/ecmascript/js_promise.cpp @@ -186,6 +186,6 @@ JSTaggedValue JSPromise::TriggerPromiseReactions(JSThread *thread, const JSHandl JSHandle JSPromise::IfThrowGetThrowValue(JSThread *thread) { - return JSHandle(thread, thread->GetException()); + return thread->GetException(); } } // namespace panda::ecmascript diff --git a/ecmascript/js_thread.cpp b/ecmascript/js_thread.cpp index 5ed583444c..e963b93e3c 100644 --- a/ecmascript/js_thread.cpp +++ b/ecmascript/js_thread.cpp @@ -71,6 +71,11 @@ void JSThread::SetException(JSTaggedValue exception) exception_ = exception; } +JSHandle JSThread::GetException() const +{ + return JSHandle(reinterpret_cast(&exception_)); +} + void JSThread::ClearException() { exception_ = JSTaggedValue::Hole(); diff --git a/ecmascript/js_thread.h b/ecmascript/js_thread.h index 7f597de23a..164a0956ae 100644 --- a/ecmascript/js_thread.h +++ b/ecmascript/js_thread.h @@ -124,10 +124,7 @@ public: void SetException(JSTaggedValue exception); - JSTaggedValue GetException() const - { - return exception_; - } + JSHandle GetException() const; bool HasPendingException() const { @@ -234,6 +231,7 @@ private: InternalCallParams *internalCallParams_ {nullptr}; friend class EcmaHandleScope; + friend class EcmaInterpreter; friend class GlobalHandleCollection; }; } // namespace panda::ecmascript diff --git a/ecmascript/tests/tagged_value_test.cpp b/ecmascript/tests/tagged_value_test.cpp index 0cefd004dd..80acbedd80 100644 --- a/ecmascript/tests/tagged_value_test.cpp +++ b/ecmascript/tests/tagged_value_test.cpp @@ -355,7 +355,7 @@ HWTEST_F_L0(JSTaggedValueTest, ToNumber) JSHandle symbolV1(thread->GetEcmaVM()->GetFactory()->NewJSSymbol()); JSTaggedValue::ToNumber(thread, symbolV1); EXPECT_TRUE(thread->HasPendingException()); - EXPECT_TRUE(thread->GetException().IsJSError()); + EXPECT_TRUE(thread->GetException()->IsJSError()); } HWTEST_F_L0(JSTaggedValueTest, ToInteger) diff --git a/ecmascript/tooling/interface/debugger_api.cpp b/ecmascript/tooling/interface/debugger_api.cpp index 93ae8e71d0..5b669b3168 100644 --- a/ecmascript/tooling/interface/debugger_api.cpp +++ b/ecmascript/tooling/interface/debugger_api.cpp @@ -124,8 +124,7 @@ Local DebuggerApi::GetVRegValue(const EcmaVM *ecmaVm, const EcmaFram // JSThread Local DebuggerApi::GetException(const EcmaVM *ecmaVm) { - auto exception = ecmaVm->GetJSThread()->GetException(); - JSHandle handledException(ecmaVm->GetJSThread(), exception); + auto handledException = ecmaVm->GetJSThread()->GetException(); return JSNApiHelper::ToLocal(handledException); } -- Gitee