fix(worker): make array and FILETIME conversion total (WIP-003, WIP-004, #255)

Typed array projection no longer throws on an element that does not fit:
it falls back to the array's own element type, then to the raw projection
with a diagnostic naming the element index and CLR type. Time arrays parse
BSTR timestamps as MXAccess local time and reject 32-bit/double elements
instead of reading them as FILETIMEs near 1601; out-of-range FILETIMEs
(scalar and array) stay integers. Integer arrays widen like the scalar
path: uint/long/ulong use Int64Values, object[] retries as Int64 on
overflow, and a ulong above long.MaxValue takes the raw projection.
Conversion runs on the event path, where a throw faults the session.
This commit is contained in:
Joseph Doherty
2026-12-03 09:45:05 -05:00
parent d12fd4f5a0
commit 62649047cf
4 changed files with 448 additions and 132 deletions
+12 -19
View File
@@ -91,29 +91,22 @@ read-modify-write merge.
### Array projection
`ConvertArray` records the rank and per-dimension lengths so multi-dimensional `SAFEARRAY` shapes survive the round trip. The element type is resolved from the caller-supplied hint or the CLR element type via `ResolveArrayElementDataType`, then dispatched to the matching typed builder (`ConvertBoolArray`, `ConvertInt64Array`, `ConvertTimestampArray`, and so on).
`ConvertArray` records the rank and per-dimension lengths so multi-dimensional `SAFEARRAY` shapes survive the round trip. The element type is resolved from the caller-supplied hint or the CLR element type via `ResolveArrayElementDataType`, then `TryProjectTypedArray` fills the matching typed list. Nothing is written to the `MxArray` until every element has converted.
```csharp
for (int dimension = 0; dimension < array.Rank; dimension++)
{
mxArray.Dimensions.Add((uint)array.GetLength(dimension));
}
Conversion is total: it never throws for an element it cannot project. It runs on the MXAccess event path, where an exception is recorded as `MxaccessEventConversionFailed` and faults the whole session, so one bad sample in a buffered event would otherwise drop every subscription on the session. When an element does not fit the attempted type, `ConvertArray` falls back in this order:
System.Type? elementType = array.GetType().GetElementType();
MxDataType elementDataType = ResolveArrayElementDataType(elementType, expectedElementDataType);
mxArray.ElementDataType = elementDataType;
```
1. The projection the array's own CLR element type gives, when that differs from the hint. A `long[]` with a `Time` hint and an out-of-range FILETIME becomes an `Int64Values` array. `RawDiagnostic` names the element index and CLR type that failed, never its value.
2. The raw projection: `ElementDataType = Unknown`, `RawElementDataType` set to the original hint, each element serialized as a UTF-8 byte string by `ConvertRawArray`, and the same element diagnostic. An `object[]` (a `SAFEARRAY` of `VARIANT`) has no typed fallback and goes straight here.
When the element type cannot be classified, `ConvertArray` does not throw. It downgrades the result to `MxDataType.Unknown`, records the original expected type in `RawElementDataType`, and serializes each element via `ConvertRawArray` as a UTF-8 byte string. This satisfies the `gateway.md` requirement to keep both the best typed projection and the raw diagnostic metadata.
An element type that has no typed projection at all (`ElapsedTime`, `Enum`, an unrecognised CLR type) takes the raw projection with the generic "unsupported variant projection" diagnostic, as before.
```csharp
default:
mxArray.ElementDataType = MxDataType.Unknown;
mxArray.RawElementDataType = (int)expectedElementDataType;
mxArray.RawDiagnostic = CreateRawDiagnostic(array);
mxArray.RawValues = ConvertRawArray(array);
return mxArray;
```
Element rules per typed projection:
- **Integer.** Widened the same way as the scalar path. `uint[]`, `long[]` and `ulong[]` always use `Int64Values`, so a `uint` above `int.MaxValue` no longer overflows. Other arrays, `object[]` included, try `Int32Values` first and retry as `Int64Values` when an element does not fit. A `ulong` above `long.MaxValue` fits neither and takes the raw projection, mirroring the scalar `UInt64 value exceeds Int64 range` fallback.
- **Time.** A `DateTime` (`VT_DATE`) element goes through `ToTimestamp`. A `long`, or a `ulong` up to `long.MaxValue`, is a FILETIME; one outside the `DateTime` range fails the element. A string is parsed with `MxAccessEventMapper.TryParseSourceTimestamp`, the same culture-aware local-time parse used for the `OnDataChange` timestamp, because MXAccess formats that timestamp as a `VT_BSTR` and a buffered timestamp array may arrive the same way. Any other element (a 32-bit integer, which can only hold the low half of a FILETIME, or a `double`) fails the element instead of being read as a time near 1601.
- **Null elements** (`VT_EMPTY`/`VT_NULL`) still take the type's default (`false`, `0`, `""`, the Unix epoch). How MXAccess represents a missing element in a buffered array has not been captured live, and no null encoding is invented until it is (WIP-008, decision D8 in the 2026-12-02 review plan).
The scalar FILETIME projection (an `Int64` or `UInt64` value with a `Time` hint) has the same range guard: a value outside the `DateTime` range stays an `Int64Value` with a `RawDiagnostic` instead of throwing.
### Variant type names