Throw exception if trying to add to a fixed size IList

This commit is contained in:
Deukhoofd 2019-02-21 14:21:15 +01:00
parent f1ae0af244
commit 668d60ce46
No known key found for this signature in database
GPG Key ID: B4C087AC81641654
1 changed files with 14 additions and 6 deletions

View File

@ -84,22 +84,30 @@ namespace Upsilon.BaseTypes.UserData
} }
else else
{ {
throw new ScriptRuntimeException(null, throw new EvaluationException(null,
$"Tried indexing a CSharp list with a value that's not an integer. Value: {scriptIndex.ToCSharpObject()}", $"Tried indexing a CSharp list with a value that's not an integer. Value: {scriptIndex.ToCSharpObject()}.",
span, diagnostics.ScriptString.GetSpan(span)); span);
} }
index--; index--;
if (index > List.Count) if (index > List.Count)
{ {
throw new Exception( throw new EvaluationException(null,
$"Tried adding an item to a list at position {index}, but list is only of length {List.Count}"); $"Tried adding an item to a list at position {index}, but list is only of length {List.Count}.", span);
} }
if (index == List.Count) if (index == List.Count)
{
if (!List.IsFixedSize)
{ {
List.Add(value.ToCSharpObject()); List.Add(value.ToCSharpObject());
} }
else else
{
throw new EvaluationException(null,
$"Tried adding an item to a list at position {index}, but list is of fixed size.", span);
}
}
else
{ {
List[index] = value.ToCSharpObject(); List[index] = value.ToCSharpObject();
} }