diff --git a/src/sfall_arrays.cc b/src/sfall_arrays.cc index 002250d..0252352 100644 --- a/src/sfall_arrays.cc +++ b/src/sfall_arrays.cc @@ -126,15 +126,39 @@ public: void SetArray(const SFallScriptValue& key, const SFallScriptValue& val, bool allowUnset) { + auto iter = map.find(key); - // TODO: assoc + bool lookupMap = (mFlags & SFALL_ARRAYFLAG_CONSTVAL) != 0; - // if (key.isInt()) { - // auto index = key.asInt(); - // if (index >= 0 && index < size()) { - // values[index] = key; - // } - // } + if (iter != map.end() && lookupMap) { + // don't update value of key + return; + } + + if (allowUnset && !lookupMap && val.isInt() && val.asInt() == 0) { + // after assigning zero to a key, no need to store it, because "get_array" returns 0 for non-existent keys: try unset + if (iter != map.end()) { + map.erase(iter); + std::vector newKeys; + newKeys.reserve(keys.size() - 1); + for (auto keyCandidate : keys) { + if (keyCandidate == key) { + // skip this key + } else { + newKeys.push_back(keyCandidate); + } + }; + keys = newKeys; + } + } else { + if (iter == map.end()) { + // size check + if (size() >= ARRAY_MAX_SIZE) return; + + keys.push_back(key); + map[key] = val; + } + } } }; diff --git a/src/sfall_script_value.cc b/src/sfall_script_value.cc index d6a059a..8da8ca4 100644 --- a/src/sfall_script_value.cc +++ b/src/sfall_script_value.cc @@ -86,4 +86,24 @@ bool SFallScriptValue::operator<(SFallScriptValue const& other) const } } +bool SFallScriptValue::operator==(SFallScriptValue const& other) const +{ + if (opcode != other.opcode) { + return false; + } + + switch (opcode) { + case VALUE_TYPE_DYNAMIC_STRING: + case VALUE_TYPE_STRING: + case VALUE_TYPE_PTR: + return pointerValue == other.pointerValue; + case VALUE_TYPE_INT: + return integerValue == other.integerValue; + case VALUE_TYPE_FLOAT: + return floatValue == other.floatValue; + default: + throw(std::exception()); + } +} + } \ No newline at end of file diff --git a/src/sfall_script_value.h b/src/sfall_script_value.h index 0d6405d..b5a0ffd 100644 --- a/src/sfall_script_value.h +++ b/src/sfall_script_value.h @@ -19,6 +19,7 @@ public: int asInt() const; bool operator<(SFallScriptValue const& other) const; + bool operator==(SFallScriptValue const& other) const; }; }