/* * 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 "../../src/Exception.h" #include "../../src/NativeConverter.hpp" #include "../../src/Reference.h" #include "../../src/Scope.h" #include "../../src/utils/GlobalWeakBookkeeping.hpp" #include "JscHelper.h" namespace script { namespace jsc_backend { inline void retain(JSContextRef context, JSValueRef val) { if (val) JSValueProtect(context, val); } inline void release(JSContextRef context, JSValueRef val) { if (val) JSValueUnprotect(context, val); } struct JscBookKeepFetcher { template static ::script::internal::GlobalWeakBookkeeping* get(const T* ref) { if (!ref) return nullptr; auto& val = JscEngine::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 = JscEngine::refVal(const_cast(ref)); return val.handle_; } }; using BookKeep = ::script::internal::GlobalWeakBookkeeping::Helper; } // namespace jsc_backend template Global::Global() noexcept : val_() {} template Global::Global(const script::Local& localReference) : val_{localReference.val_, jsc_backend::currentEngine()} { if (val_.ref_) jsc_backend::retain(val_.engine_->context_, val_.ref_); jsc_backend::BookKeep::keep(this); } template Global::Global(const script::Weak& weak) : val_{const_cast::jscType>(weak.val_.ref_.get()), weak.val_.engine_} { if (val_.ref_) jsc_backend::retain(val_.engine_->context_, val_.ref_); jsc_backend::BookKeep::keep(this); } template Global::Global(const script::Global& copy) : val_() { *this = copy; } template Global::Global(script::Global&& move) noexcept : val_() { *this = std::move(move); } template Global::~Global() { if (!isEmpty()) { EngineScope scope(val_.engine_); reset(); } } template Global& Global::operator=(const script::Global& assign) { bool wasEmtpy = isEmpty(); if (!wasEmtpy) { jsc_backend::release(val_.engine_->context_, val_.ref_); } val_.ref_ = assign.val_.ref_; val_.engine_ = assign.val_.engine_; if (val_.ref_) jsc_backend::retain(val_.engine_->context_, val_.ref_); jsc_backend::BookKeep::afterCopy(wasEmtpy, this, &assign); return *this; } template Global& Global::operator=(script::Global&& move) noexcept { bool wasEmtpy = isEmpty(); if (!wasEmtpy) { wasEmtpy = false; jsc_backend::release(val_.engine_->context_, val_.ref_); } val_.ref_ = move.val_.ref_; val_.engine_ = move.val_.engine_; move.val_.ref_ = nullptr; move.val_.engine_ = nullptr; jsc_backend::BookKeep::afterMove(wasEmtpy, this, &move); return *this; } template void Global::swap(Global& rhs) noexcept { std::swap(val_.ref_, rhs.val_.ref_); std::swap(val_.engine_, rhs.val_.engine_); jsc_backend::BookKeep::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_); } template Local Global::getValue() const { return Local(val_.ref_); } template bool Global::isEmpty() const { return val_.ref_ == nullptr; } template void Global::reset() { if (!isEmpty()) { jsc_backend::release(val_.engine_->context_, val_.ref_); jsc_backend::BookKeep::remove(this); val_.ref_ = nullptr; val_.engine_ = nullptr; } } // == Weak == template Weak::Weak() noexcept : val_() {} template Weak::~Weak() { if (!isEmpty()) { EngineScope scope(val_.engine_); reset(); } } template Weak::Weak(const script::Local& localReference) : val_(localReference.val_, jsc_backend::currentEngine()) { jsc_backend::BookKeep::keep(this); } template Weak::Weak(const script::Global& globalReference) : val_(globalReference.val_.ref_, jsc_backend::currentEngine()) { jsc_backend::BookKeep::keep(this); } template Weak::Weak(const script::Weak& copy) : val_() { *this = copy; } template Weak::Weak(script::Weak&& move) noexcept : val_() { *this = std::move(move); } template Weak& Weak::operator=(const script::Weak& assign) { bool wasEmtpy = isEmpty(); val_.ref_ = assign.val_.ref_; val_.engine_ = assign.val_.engine_; jsc_backend::BookKeep::afterCopy(wasEmtpy, this, &assign); return *this; } template Weak& Weak::operator=(script::Weak&& move) noexcept { bool wasEmpty = isEmpty(); val_.ref_ = std::move(move.val_.ref_); val_.engine_ = std::move(move.val_.engine_); jsc_backend::BookKeep::afterMove(wasEmpty, this, &move); return *this; } template void Weak::swap(Weak& rhs) noexcept { std::swap(val_.ref_, rhs.val_.ref_); std::swap(val_.engine_, rhs.val_.engine_); jsc_backend::BookKeep::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 empty Weak"); return converter::Converter>::toCpp(value); } template Local Weak::getValue() const { using type = typename jsc_backend::RefTypeMap::jscType; return Local(const_cast(val_.ref_.get())); } template bool Weak::isEmpty() const { return val_.ref_.isEmpty(); } template void Weak::reset() noexcept { if (!isEmpty()) { jsc_backend::BookKeep::remove(this); val_.ref_.reset(val_.engine_); val_.engine_ = nullptr; } } } // namespace script