Fixes for setting to tables not working properly
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2019-09-22 15:20:22 +02:00
parent 62be1c78f3
commit 256244eabb
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
5 changed files with 53 additions and 11 deletions

View File

@ -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 {

View File

@ -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]]

View File

@ -95,7 +95,7 @@ namespace Porygon{
return _isContentAware;
}
shared_ptr<const ScriptType> GetIndexedType(shared_ptr<const ScriptType> indexer) const override {
[[nodiscard]] shared_ptr<const ScriptType> GetIndexedType(shared_ptr<const ScriptType> indexer) const override {
if (_tableType == TableType::Unknown)
return ScriptType::AnyType;
else if (_tableType == TableType::StringKeyed){
@ -116,7 +116,12 @@ namespace Porygon{
[[nodiscard]] shared_ptr<const ScriptType> 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<TableScriptType*>(this);
if (indexer->GetClass() == TypeClass::Number){
t->_tableType = TableType ::Numerical;
t->_valueType = val;
}
}
}
void SetIndexValue(Utilities::HashedString indexer, shared_ptr<const ScriptType> val) const override {

View File

@ -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

View File

@ -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