/* * 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/Reference.h" namespace script { template Global::Global() noexcept : val_(-1) {} template Global::Global(const script::Local& localReference) { val_ = wasm_backend::GlobalHelper::newGlobal(localReference.val_, false); } template Global::Global(const script::Weak& weak) : val_(-1) { val_ = wasm_backend::GlobalHelper::dupGlobal(weak.val_, true, false); } template Global::Global(const script::Global& copy) : val_(-1) { val_ = wasm_backend::GlobalHelper::dupGlobal(copy.val_, false, false); } template Global::Global(script::Global&& move) noexcept : val_(move.val_) { move.val_ = -1; } template Global::~Global() { reset(); } template Global& Global::operator=(const script::Global& assign) { Global(assign).swap(*this); return *this; } template Global& Global::operator=(script::Global&& move) noexcept { Global(std::move(move)).swap(*this); return *this; } template void Global::swap(Global& rhs) noexcept { std::swap(val_, rhs.val_); } template Global& Global::operator=(const script::Local& assign) { *this = Global(assign); return *this; } template Local Global::get() const { if (isEmpty()) { throw Exception("get on null Global"); } return Local(wasm_backend::GlobalHelper::getGlobal(val_, false)); } template Local Global::getValue() const { return Local(wasm_backend::GlobalHelper::getGlobal(val_, false)); } template bool Global::isEmpty() const { return val_ == -1; } template void Global::reset() { if (!isEmpty()) { wasm_backend::GlobalHelper::deleteGlobal(val_, false); val_ = -1; } } // == Weak == template Weak::Weak() noexcept : val_(-1) {} template Weak::~Weak() { reset(); } template Weak::Weak(const script::Local& localReference) { val_ = wasm_backend::GlobalHelper::newGlobal(localReference.val_, true); } template Weak::Weak(const script::Global& globalReference) { val_ = wasm_backend::GlobalHelper::dupGlobal(globalReference.val_, false, true); } template Weak::Weak(const script::Weak& copy) : val_(copy.val_) { val_ = wasm_backend::GlobalHelper::dupGlobal(copy.val_, true, true); } template Weak::Weak(script::Weak&& move) noexcept : val_(std::move(move.val_)) { move.val_ = -1; } template Weak& Weak::operator=(const script::Weak& assign) { val_ = assign.val_; return *this; } template Weak& Weak::operator=(script::Weak&& move) noexcept { Weak(std::move(move)).swap(*this); return *this; } template void Weak::swap(Weak& rhs) noexcept { std::swap(val_, rhs.val_); } template Weak& Weak::operator=(const script::Local& assign) { *this = Weak(assign); return *this; } template Local Weak::get() const { if (isEmpty()) { throw Exception("get on null Weak"); } return Local(wasm_backend::GlobalHelper::getGlobal(val_, true)); } template Local Weak::getValue() const { return Local(wasm_backend::GlobalHelper::getGlobal(val_, true)); } template bool Weak::isEmpty() const { return val_ == -1; } template void Weak::reset() noexcept { if (!isEmpty()) { wasm_backend::GlobalHelper::deleteGlobal(val_, true); val_ = -1; } } } // namespace script