Files
crosslang/src/types/associativearray.cpp
T

108 lines
3.5 KiB
C++

/*
CrossLang is a dynamically-typed scripting language built on
TessesFramework, named in honor of Jesus's sacrifice.
Copyright (C) 2026 Mike Nolan
SPDX-License-Identifier: GPL-3.0-or-later WITH TessesFramework-Exception-1.0
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "CrossLang.hpp"
namespace Tesses::CrossLang {
TAssociativeArray *TAssociativeArray::Create(GCList &ls) {
return ls.Create<TAssociativeArray>();
}
TAssociativeArray *TAssociativeArray::Create(GCList *ls) {
return ls->Create<TAssociativeArray>();
}
void TAssociativeArray::Set(std::shared_ptr<GC> gc, TObject key,
TObject value) {
if (std::holds_alternative<Undefined>(key))
return;
gc->BarrierBegin();
for (auto index = this->items.begin(); index < this->items.end(); index++) {
auto first = index->first;
gc->BarrierEnd();
auto eq = Equals(gc, key, first);
gc->BarrierBegin();
if (eq) {
if (std::holds_alternative<Undefined>(value)) {
this->items.erase(index);
} else {
index->second = value;
}
gc->BarrierEnd();
return;
}
}
this->items.push_back(std::pair<TObject, TObject>(key, value));
gc->BarrierEnd();
}
TObject TAssociativeArray::Get(std::shared_ptr<GC> gc, TObject key) {
if (std::holds_alternative<Undefined>(key))
return Undefined();
gc->BarrierBegin();
for (auto &item : this->items) {
auto first = item.first;
gc->BarrierEnd();
auto eq = Equals(gc, key, first);
gc->BarrierBegin();
if (eq) {
gc->BarrierEnd();
return item.second;
}
}
gc->BarrierEnd();
return Undefined();
}
TObject TAssociativeArray::GetKey(int64_t index) {
if (index >= 0 && index < (int64_t)this->items.size()) {
return this->items[index].first;
}
return Undefined();
}
TObject TAssociativeArray::GetValue(int64_t index) {
if (index >= 0 && index < (int64_t)this->items.size()) {
return this->items[index].second;
}
return Undefined();
}
void TAssociativeArray::SetKey(int64_t index, TObject key) {
if (std::holds_alternative<Undefined>(key))
return;
if (index >= 0 && index < (int64_t)this->items.size()) {
this->items[index].first = key;
}
}
void TAssociativeArray::SetValue(int64_t index, TObject value) {
if (std::holds_alternative<Undefined>(value))
return;
if (index >= 0 && index < (int64_t)this->items.size()) {
this->items[index].first = value;
}
}
int64_t TAssociativeArray::Count() { return (int64_t)this->items.size(); }
void TAssociativeArray::Mark() {
if (this->marked)
return;
this->marked = true;
for (auto &item : this->items) {
GC::Mark(item.first);
GC::Mark(item.second);
}
}
} // namespace Tesses::CrossLang