equals method
Implementation
bool equals(Object other) {
final otherRaw = rawLuaSlot(other);
// Lua: NaN == anything is always false
if ((raw is num && raw.isNaN) || (otherRaw is num && otherRaw.isNaN)) {
Logger.debugLazy(
() => 'COMPARE ==: NaN detected, returning false',
category: 'Value',
);
return false;
}
// Lua: int == float only if float is finite and exactly represents int
if ((raw is int || raw is BigInt) && otherRaw is double) {
if (!otherRaw.isFinite) {
Logger.debugLazy(
() => 'COMPARE ==: int == non-finite double, returning false',
category: 'Value',
);
return false;
}
final intVal = raw is BigInt ? raw as BigInt : BigInt.from(raw);
final doubleVal = otherRaw;
BigInt intFromDouble;
try {
// Handle very large numbers that toStringAsFixed might return in scientific notation
final stringVal = doubleVal.toStringAsFixed(0);
if (stringVal.contains('e') || stringVal.contains('E')) {
// Use LuaNumberParser for scientific notation
final parsed = LuaNumberParser.parse(stringVal);
if (parsed is double) {
return false; // If it's not an exact integer, they're not equal
}
intFromDouble = parsed is BigInt ? parsed : BigInt.from(parsed);
} else {
intFromDouble = BigInt.parse(stringVal);
}
} catch (e) {
return false; // If we can't parse it as an integer, they're not equal
}
final doubleFromInt = intVal.toDouble();
final isExact = (doubleVal == doubleFromInt) && (intVal == intFromDouble);
Logger.debugLazy(
() =>
'COMPARE ==: int=$intVal, double=$doubleVal, doubleFromInt=$doubleFromInt, intFromDouble=$intFromDouble, isExact=$isExact',
category: 'Value',
);
return isExact;
}
if (raw is double && (otherRaw is int || otherRaw is BigInt)) {
if (!raw.isFinite) {
Logger.debugLazy(
() =>
'COMPARE ==: double == int, but double is not finite, '
'returning false',
category: 'Value',
);
return false;
}
final intVal = otherRaw is BigInt
? otherRaw
: BigInt.from(otherRaw as num);
final doubleVal = raw;
BigInt intFromDouble;
try {
// Handle very large numbers that toStringAsFixed might return in scientific notation
final stringVal = doubleVal.toStringAsFixed(0);
if (stringVal.contains('e') || stringVal.contains('E')) {
// Use LuaNumberParser for scientific notation
final parsed = LuaNumberParser.parse(stringVal);
if (parsed is double) {
return false; // If it's not an exact integer, they're not equal
}
intFromDouble = parsed is BigInt ? parsed : BigInt.from(parsed);
} else {
intFromDouble = BigInt.parse(stringVal);
}
} catch (e) {
return false; // If we can't parse it as an integer, they're not equal
}
final doubleFromInt = intVal.toDouble();
final isExact = (doubleVal == doubleFromInt) && (intVal == intFromDouble);
Logger.debugLazy(
() =>
'COMPARE ==: double=$doubleVal, int=$intVal, doubleFromInt=$doubleFromInt, intFromDouble=$intFromDouble, isExact=$isExact',
category: 'Value',
);
return isExact;
}
if ((raw is BigInt && otherRaw is double) ||
(raw is double && otherRaw is BigInt)) {
final d1 = raw is BigInt ? raw.toDouble() : raw;
final d2 = otherRaw is BigInt ? otherRaw.toDouble() : otherRaw;
Logger.debugLazy(
() => 'COMPARE ==: promoting BigInt to double: $d1 == $d2',
category: 'Value',
);
return d1 == d2;
}
if (identical(this, other)) return true;
if (raw is BigInt && otherRaw is BigInt) return raw == otherRaw;
if (raw is BigInt && otherRaw is num) {
if (otherRaw is int) return raw == BigInt.from(otherRaw);
return otherRaw.isFinite &&
raw.toDouble() == otherRaw &&
raw == BigInt.from(otherRaw);
}
if (raw is num && otherRaw is BigInt) {
if (raw is int) return BigInt.from(raw) == otherRaw;
return raw.isFinite &&
raw == otherRaw.toDouble() &&
BigInt.from(raw) == otherRaw;
}
if (raw is LuaString && otherRaw is LuaString) {
return raw == otherRaw;
}
if (raw is LuaString && otherRaw is String) {
return raw == LuaString.fromDartString(otherRaw);
}
if (raw is String && otherRaw is LuaString) {
return LuaString.fromDartString(raw) == otherRaw;
}
if (raw is Map && otherRaw is Map) {
// Tables compare by reference when no '__eq' metamethod is present.
// Using the default Dart equality here preserves this behavior.
return raw == otherRaw;
}
return raw == otherRaw;
}