Fix issue with deepMerge() caused by handling maps incorrectly, causing an unhandleable crash.

This commit is contained in:
EliteMasterEric 2024-04-03 15:05:54 -04:00
parent ad39ce3c21
commit 4f2f28cb31

View file

@ -1,5 +1,6 @@
package funkin.util; package funkin.util;
import funkin.util.tools.MapTools;
import haxe.DynamicAccess; import haxe.DynamicAccess;
/** /**
@ -26,6 +27,57 @@ class StructureUtil
return result; return result;
} }
public static function toMap(a:Dynamic):haxe.ds.Map<String, Dynamic>
{
var result:haxe.ds.Map<String, Dynamic> = [];
for (field in Reflect.fields(a))
{
result.set(field, Reflect.field(a, field));
}
return result;
}
public static function isMap(a:Dynamic):Bool
{
return Std.isOfType(a, haxe.Constraints.IMap);
}
public static function isObject(a:Dynamic):Bool
{
switch (Type.typeof(a))
{
case TObject:
return true;
default:
return false;
}
}
public static function isPrimitive(a:Dynamic):Bool
{
switch (Type.typeof(a))
{
case TInt | TFloat | TBool:
return true;
case TClass(c):
return false;
case TEnum(e):
return false;
case TObject:
return false;
case TFunction:
return false;
case TNull:
return true;
case TUnknown:
return false;
default:
return false;
}
}
/** /**
* Merge two structures, with the second overwriting the first. * Merge two structures, with the second overwriting the first.
* Performs a DEEP clone, where child structures are also merged recursively. * Performs a DEEP clone, where child structures are also merged recursively.
@ -37,6 +89,18 @@ class StructureUtil
{ {
if (a == null) return b; if (a == null) return b;
if (b == null) return null; if (b == null) return null;
if (isPrimitive(a) && isPrimitive(b)) return b;
if (isMap(b))
{
if (isMap(a))
{
return MapTools.merge(a, b);
}
else
{
return StructureUtil.toMap(a).merge(b);
}
}
if (!Reflect.isObject(a) || !Reflect.isObject(b)) return b; if (!Reflect.isObject(a) || !Reflect.isObject(b)) return b;
var result:DynamicAccess<Dynamic> = Reflect.copy(a); var result:DynamicAccess<Dynamic> = Reflect.copy(a);