fix(scripting): #1092 a returned concrete collection (List<T>, HashSet<T>, ...) is materialised into the same typed array in both runners
ScriptResultMaterializer returned a non-array ICollection as-is (capped by Count only), so in process the host got the List<double> itself while the out-of-process worker, whose wire carries arrays but no collection type, answered "unsupported type" for the same script. Every non-array, non-string sequence now goes through the one materialisation: a collection is held to Scripting:MaxResultElements by its Count before it is copied, then becomes T[] of its IEnumerable<T> element type; an open element type (List<object>, List<List<int>>) or none (ArrayList) is refused like the lazy equivalent. Both runners give double[] for a List<double>. ParityTests.Result_materialisation_and_the_element_cap_agree gains list / at-cap / over-cap / hash set / queue / List<object> / nested-list rows plus a positive control (6 rows + the control fail against the old materializer). ContainmentTests' "unsupported result type" example moves from List<int> (now int[]) to KeyValuePair<int,int>. Behaviour change: a script returning List<object> (or another open element type) now fails the run (Bad + script log) in process too, as the lazy form already did.
This commit is contained in:
@@ -26,13 +26,19 @@ public sealed class ScriptResultException : InvalidOperationException
|
||||
/// <c>maxElements</c>.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// What changes: a lazy sequence (an <see cref="IEnumerable"/> that is neither an array, a string nor an
|
||||
/// <see cref="ICollection"/>) becomes a typed <c>T[]</c> of its <c>IEnumerable<T></c> element type. A
|
||||
/// sequence whose element type could itself be a sequence (<c>object</c>, an interface, another collection) is
|
||||
/// refused — the host copies and compares returned arrays recursively, so an open-slot array can nest or contain
|
||||
/// itself (see <c>ForbiddenTypeAnalyzer</c>). Arrays and already-concrete collections are returned as-is (they
|
||||
/// were fully built by the run) but are still held to the element cap. Everything else — scalars, strings,
|
||||
/// null — is untouched.
|
||||
/// What changes: every sequence that is not an array or a string — a lazy one, or a concrete collection such as
|
||||
/// a <c>List<double></c> or a <c>HashSet<string></c> — becomes a typed <c>T[]</c> of its
|
||||
/// <c>IEnumerable<T></c> element type. A sequence whose element type could itself be a sequence
|
||||
/// (<c>object</c>, an interface, another collection) is refused — the host copies and compares returned arrays
|
||||
/// recursively, so an open-slot array can nest or contain itself (see <c>ForbiddenTypeAnalyzer</c>). Arrays are
|
||||
/// returned as-is (fully built by the run) but are still held to the element cap. Everything else — scalars,
|
||||
/// strings, null — is untouched.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// Concrete collections used to be returned as-is too (lmxopcua#1092): in process the host got the
|
||||
/// <c>List<T></c> itself, while the out-of-process worker, whose wire carries arrays but no collection
|
||||
/// type, answered "unsupported type" for the same script. Copying a collection into an array is the one value
|
||||
/// both runners can give; a collection that reports its size is held to the cap BEFORE it is copied.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// This does NOT make the in-process runner a hard sandbox: memory exhaustion inside the run and a
|
||||
@@ -64,23 +70,23 @@ public static class ScriptResultMaterializer
|
||||
return array;
|
||||
case ICollection collection:
|
||||
EnforceCap(collection.Count, maxElements);
|
||||
return value;
|
||||
return MaterializeSequence(collection, maxElements, "collection");
|
||||
case IEnumerable sequence:
|
||||
return MaterializeLazy(sequence, maxElements);
|
||||
return MaterializeSequence(sequence, maxElements, IsGenericCollection(sequence.GetType()) ? "collection" : "lazy sequence");
|
||||
default:
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
private static object MaterializeLazy(IEnumerable sequence, int maxElements)
|
||||
private static object MaterializeSequence(IEnumerable sequence, int maxElements, string what)
|
||||
{
|
||||
var elementType = ElementTypeOf(sequence.GetType())
|
||||
?? throw new ScriptResultException(
|
||||
$"The script returned a lazy sequence ({sequence.GetType().Name}) with no IEnumerable<T> element type; " +
|
||||
$"The script returned a {what} ({sequence.GetType().Name}) with no IEnumerable<T> element type; " +
|
||||
"return an array of a concrete type (double[], string[], …).");
|
||||
if (IsOpenSlot(elementType))
|
||||
throw new ScriptResultException(
|
||||
$"The script returned a lazy sequence of '{elementType.Name}', which can contain another sequence or itself; " +
|
||||
$"The script returned a {what} of '{elementType.Name}', which can contain another sequence or itself; " +
|
||||
"return a typed array of a concrete type (double[], string[], …).");
|
||||
|
||||
var items = new List<object?>();
|
||||
@@ -117,6 +123,11 @@ public static class ScriptResultMaterializer
|
||||
return found;
|
||||
}
|
||||
|
||||
// ICollection<T> / IReadOnlyCollection<T> without the non-generic ICollection (HashSet<T>, …): only the wording.
|
||||
private static bool IsGenericCollection(Type type) =>
|
||||
type.GetInterfaces().Any(i => i.IsGenericType
|
||||
&& (i.GetGenericTypeDefinition() == typeof(ICollection<>) || i.GetGenericTypeDefinition() == typeof(IReadOnlyCollection<>)));
|
||||
|
||||
private static bool IsOpenSlot(Type t) =>
|
||||
t == typeof(object) || t.IsInterface || t.IsAbstract
|
||||
|| (t != typeof(string) && typeof(IEnumerable).IsAssignableFrom(t));
|
||||
|
||||
Reference in New Issue
Block a user