Skip to content

Commit

Permalink
Only mark array-like if Count or Length present, don't make it error (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
lahma committed Apr 24, 2020
1 parent e87a949 commit 7d9c61c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
12 changes: 11 additions & 1 deletion Jint.Tests/Runtime/InteropTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Dynamic;
using System.Globalization;
using System.Linq;
using System.Reflection;
Expand Down Expand Up @@ -728,7 +729,16 @@ public void CanAddArrayPrototypeForArrayLikeClrObjects()

var name = e.Execute("o.values.filter(x => x.age == 12)[0].name").GetCompletionValue().ToString();
Assert.Equal("John", name);

}

[Fact]
public void CanAccessExpandoObject()
{
var engine = new Engine();
dynamic expando = new ExpandoObject();
expando.Name = "test";
engine.SetValue("expando", expando);
Assert.Equal("test", engine.Execute("expando.Name").GetCompletionValue().ToString());
}

[Fact]
Expand Down
7 changes: 3 additions & 4 deletions Jint/Runtime/Interop/ObjectWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,20 @@ public ObjectWrapper(Engine engine, object obj)
var type = obj.GetType();
if (ObjectIsArrayLikeClrCollection(type))
{
IsArrayLike = true;
// create a forwarder to produce length from Count or Length, whichever is present
// create a forwarder to produce length from Count or Length if one of them is present
var lengthProperty = type.GetProperty("Count") ?? type.GetProperty("Length");
if (lengthProperty is null)
{
ExceptionHelper.ThrowArgumentException("collection is supposed to either have Count or Length property");
return;
}
IsArrayLike = true;
var functionInstance = new ClrFunctionInstance(engine, "length", (thisObj, arguments) => JsNumber.Create((int) lengthProperty.GetValue(obj)));
var descriptor = new GetSetPropertyDescriptor(functionInstance, Undefined, PropertyFlag.Configurable);
AddProperty(CommonProperties.Length, descriptor);
}
}

internal static bool ObjectIsArrayLikeClrCollection(Type type)
private static bool ObjectIsArrayLikeClrCollection(Type type)
{
if (typeof(ICollection).IsAssignableFrom(type))
{
Expand Down

0 comments on commit 7d9c61c

Please sign in to comment.