diff --git a/src/Evaluator/EvalValues/TableEvalValue.cpp b/src/Evaluator/EvalValues/TableEvalValue.cpp index 509dd05..1ba0998 100644 --- a/src/Evaluator/EvalValues/TableEvalValue.cpp +++ b/src/Evaluator/EvalValues/TableEvalValue.cpp @@ -18,11 +18,8 @@ namespace Porygon::Evaluation { void Porygon::Evaluation::TableEvalValue::SetIndexValue(const Porygon::Utilities::HashedString *key, const Porygon::Evaluation::EvalValue *value) const { - auto insert = _table->insert({*key, value}); - if (!insert.second) { - _table->at(*key).ClearAssign(value); - } - + delete this->_table->operator[](*key).Take(); + this->_table->operator[](*key) = value; } Porygon::Evaluation::Iterator *Porygon::Evaluation::NumericTableEvalValue::GetKeyIterator() const { diff --git a/src/Evaluator/EvalValues/TableEvalValue.hpp b/src/Evaluator/EvalValues/TableEvalValue.hpp index c386bef..88cfd1d 100644 --- a/src/Evaluator/EvalValues/TableEvalValue.hpp +++ b/src/Evaluator/EvalValues/TableEvalValue.hpp @@ -78,10 +78,8 @@ namespace Porygon::Evaluation { inline void SetIndexValue(const EvalValue *key, const EvalValue* value) const final { auto hash = key->GetHashCode(); auto lookup = Utilities::HashedString::CreateLookup(hash); - auto insert = _table->insert({lookup, value}); - if (!insert.second) { - _table->at(lookup).ClearAssign(value); - } + delete this->_table->operator[](lookup).Take(); + this->_table->operator[](lookup) = value; } [[nodiscard]] diff --git a/src/ScriptTypes/TableScriptType.hpp b/src/ScriptTypes/TableScriptType.hpp index 367c15f..d46d0fe 100644 --- a/src/ScriptTypes/TableScriptType.hpp +++ b/src/ScriptTypes/TableScriptType.hpp @@ -95,7 +95,7 @@ namespace Porygon{ return _isContentAware; } - shared_ptr GetIndexedType(shared_ptr indexer) const override { + [[nodiscard]] shared_ptr GetIndexedType(shared_ptr indexer) const override { if (_tableType == TableType::Unknown) return ScriptType::AnyType; else if (_tableType == TableType::StringKeyed){ @@ -116,7 +116,12 @@ namespace Porygon{ [[nodiscard]] shared_ptr GetIndexedType(uint32_t hash) const override { auto lookup = Utilities::HashedString::CreateLookup(hash); - return GetContentTypes()->at(lookup); + if (GetContentTypes()->find(lookup) != GetContentTypes()->end()){ + return GetContentTypes()->at(lookup); + } + else{ + return ScriptType::AnyType; + } } [[nodiscard]] inline ContentTypes GetContentTypes() const{ @@ -160,6 +165,13 @@ namespace Porygon{ GetContentTypes()->insert({key, val}); } } + else if (_tableType == TableType::Unknown){ + auto t = const_cast(this); + if (indexer->GetClass() == TypeClass::Number){ + t->_tableType = TableType ::Numerical; + t->_valueType = val; + } + } } void SetIndexValue(Utilities::HashedString indexer, shared_ptr val) const override { diff --git a/tests/integration/LoopTests.cpp b/tests/integration/LoopTests.cpp index 22282fe..d29fc76 100644 --- a/tests/integration/LoopTests.cpp +++ b/tests/integration/LoopTests.cpp @@ -184,5 +184,22 @@ end delete runCount; } +TEST_CASE( "Generic for loop next", "[integration]" ) { + auto script = Script::Create(uR"( +local table = {1, 3, 5, 7, 9} +result = 0 +for i,v in table do + if i == 3 then next end + result = result + v +end +)"); + REQUIRE(!script->Diagnostics -> HasErrors()); + script->Evaluate(); + auto var = script->GetVariable(u"result"); + REQUIRE(var->EvaluateInteger() == 20); + delete script; + delete var; +} + #endif diff --git a/tests/integration/TablesTests.cpp b/tests/integration/TablesTests.cpp index b792679..747eeea 100644 --- a/tests/integration/TablesTests.cpp +++ b/tests/integration/TablesTests.cpp @@ -111,6 +111,24 @@ return table[1] delete script; } +TEST_CASE( "assign nil to a string table", "[integration]" ) { + Script* script = Script::Create( + R"( +table = {'bla', 'test', 'foo', 'bar'} +table[3] = nil +return table +)"); + REQUIRE(!script->Diagnostics -> HasErrors()); + auto variable = script->Evaluate(); + REQUIRE(variable.Get() != nullptr); + auto lookup = Utilities::HashedString::CreateLookup(3); + auto tableValue = variable->IndexValue(&lookup); + REQUIRE(tableValue->GetTypeClass() == TypeClass::Nil); + delete script; + delete tableValue; +} + + #endif