Delphi's TValue is broken
Don't rely on TValue.AsOrdinal if you use negative values!
I've just filed a bug report over at Embarcadero, QC #94322.
The problem is that when creating a TValue.From<OrdinalType> which is less than Int64, the unused bits are set to all 0's. Bad, if the value happens to be negative, and all unused bits should either be all 1's, or not relied upon under any circumstance.
TValue.AsOrdinal does rely on all bits of Int64, so a fix is needed for either the From<T> or AsOrdinal to agree on how to treat the excess bits.
As a workaround, you can call AsType<Int64> (AsInt64) instead of AsOrdinal to get the same behaviour (although correct), because AsType calls TryCast, which takes care of the conversion. Unless the ordinal is an enumeration! In which case the TryCast will bail on you.