/* * Tencent is pleased to support the open source community by making ScriptX available. * Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include #include #include "../../src/NativeConverter.hpp" #include "../../src/Reference.h" #include "../../src/Value.h" #include "../../src/types.h" #include "../../src/utils/GlobalWeakBookkeeping.hpp" #include "V8Engine.h" #include "V8Helper.h" namespace script { namespace v8_backend { template GlobalRefState::GlobalRefState(V8Engine* scriptEngine, const GlobalRefState::V8Global& v8Global) : engine_(scriptEngine), ref_(v8Global) {} template GlobalRefState::GlobalRefState(V8Engine* scriptEngine, const Local& localReference) : engine_(scriptEngine), ref_{engine_->isolate_, v8_backend::V8Engine::toV8(engine_->isolate_, localReference)} {} struct V8BookKeepFetcher { template static ::script::internal::GlobalWeakBookkeeping* get(const T* ref) { if (!ref) return nullptr; auto& val = V8Engine::refVal(const_cast(ref)); if (!val.engine_) return nullptr; return &val.engine_->globalWeakBookkeeping_; } template static ::script::internal::GlobalWeakBookkeeping::HandleType& handle(const T* ref) { auto& val = V8Engine::refVal(const_cast(ref)); return val.handle_; } }; using V8BookKeep = internal::GlobalWeakBookkeeping::Helper; } // namespace v8_backend // == Global == template Global::Global() noexcept : val_() {} template Global::Global(const script::Local& localReference) : val_(v8_backend::currentEngine(), localReference) { v8_backend::V8BookKeep::keep(this); } template Global::Global(const script::Weak& weakReference) : val_(v8_backend::currentEngine(), weakReference.val_.ref_) { v8_backend::V8BookKeep::keep(this); } template Global::Global(const script::Global& copy) : val_(copy.val_) { v8_backend::V8BookKeep::afterCopy(true, this, ©); } template Global::Global(script::Global&& move) noexcept : val_(std::move(move.val_)) { v8_backend::V8BookKeep::afterMove(true, this, &move); } template Global::~Global() { if (!isEmpty()) { EngineScope scope(val_.engine_); reset(); } } template Global& Global::operator=(const script::Global& assign) { bool wasEmpty = isEmpty(); val_ = assign.val_; v8_backend::V8BookKeep::afterCopy(wasEmpty, this, &assign); return *this; } template Global& Global::operator=(Global&& move) noexcept { if (&move != this) { bool wasEmpty = isEmpty(); val_ = std::move(move.val_); v8_backend::V8BookKeep::afterMove(wasEmpty, this, &move); } return *this; } template void Global::swap(Global& rhs) noexcept { if (&rhs != this) { val_.swap(rhs.val_); v8_backend::V8BookKeep::afterSwap(this, &rhs); } } template Global& Global::operator=(const script::Local& assign) { *this = Global(assign); return *this; } template Local Global::get() const { if (isEmpty()) throw Exception("get on empty Global"); return Local(val_.ref_.Get(v8_backend::currentEngineIsolateChecked())); } template Local Global::getValue() const { return Local(val_.ref_.Get(v8_backend::currentEngineIsolateChecked())); } template bool Global::isEmpty() const { return val_.ref_.IsEmpty(); } template void Global::reset() { if (!isEmpty()) { val_.ref_.Reset(); v8_backend::V8BookKeep::remove(this); } } // == Weak == template Weak::Weak() noexcept : val_() {} template Weak::~Weak() { if (!isEmpty()) { EngineScope scope(val_.engine_); reset(); } } template Weak::Weak(const script::Local& localReference) : val_(v8_backend::currentEngine(), localReference) { val_.makeWeak(); v8_backend::V8BookKeep::keep(this); } template Weak::Weak(const script::Global& globalReference) : val_(globalReference.val_.engine_, globalReference.val_.ref_) { val_.makeWeak(); v8_backend::V8BookKeep::keep(this); } template Weak::Weak(const script::Weak& copy) : val_(copy.val_) { val_.makeWeak(); v8_backend::V8BookKeep::afterCopy(true, this, ©); } template Weak::Weak(script::Weak&& move) noexcept : val_(std::move(move.val_)) { val_.makeWeak(); v8_backend::V8BookKeep::afterMove(true, this, &move); } template Weak& Weak::operator=(const script::Weak& assign) { if (&assign != this) { bool wasEmpty = isEmpty(); val_ = assign.val_; val_.makeWeak(); v8_backend::V8BookKeep::afterCopy(wasEmpty, this, &assign); } return *this; } template Weak& Weak::operator=(script::Weak&& move) noexcept { if (&move != this) { bool wasEmpty = isEmpty(); val_ = std::move(move.val_); val_.makeWeak(); v8_backend::V8BookKeep::afterMove(wasEmpty, this, &move); } return *this; } template void Weak::swap(Weak& rhs) noexcept { if (&rhs != this) { val_.swap(rhs.val_); val_.makeWeak(); rhs.val_.makeWeak(); v8_backend::V8BookKeep::afterSwap(this, &rhs); } } template Weak& Weak::operator=(const script::Local& assign) { *this = Weak(assign); return *this; } template Local Weak::get() const { auto value = getValue(); if (value.isNull()) throw Exception("get on null Weak"); return converter::Converter>::toCpp(value); } template Local Weak::getValue() const { if (isEmpty()) return {}; return Local(val_.ref_.Get(val_.engine_->isolate_)); } template bool Weak::isEmpty() const { // V8 PersistentBase::IsEmpty will be true after gc // differs with our semantics, so we use then engine_ field to check this. return val_.engine_ == nullptr; } template void Weak::reset() noexcept { if (!isEmpty()) { val_.ref_.Reset(); v8_backend::V8BookKeep::remove(this); val_.engine_ = nullptr; } } } // namespace script