Skip to content

Commit e67e58a

Browse files
authored
Improve interop finding of properties in derived types (#1969)
1 parent 6b2d622 commit e67e58a

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

Jint.Tests/Runtime/InteropTests.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3624,4 +3624,45 @@ public void StringifyShouldIncludeInheritedFieldsAndProperties()
36243624
engine.SetValue("c", new Circle(12.34));
36253625
engine.Evaluate("JSON.stringify(c)").ToString().Should().Be("{\"Radius\":12.34,\"Color\":0,\"Id\":123}");
36263626
}
3627+
3628+
public class Animal
3629+
{
3630+
public virtual string name { get; set; } = "animal";
3631+
}
3632+
3633+
public class Elephant : Animal
3634+
{
3635+
public override string name { get; set; } = "elephant";
3636+
public int earSize = 5;
3637+
}
3638+
3639+
public class Lion : Animal
3640+
{
3641+
public override string name { get; set; } = "lion";
3642+
public int maneLength = 10;
3643+
}
3644+
3645+
public class Zoo
3646+
{
3647+
public Animal king { get => (new Animal[] { new Lion() })[0]; }
3648+
public Animal[] animals { get => new Animal[] { new Lion(), new Elephant() }; }
3649+
}
3650+
3651+
[Fact]
3652+
public void CanFindDerivedPropertiesFail() // Fails in 4.01 but success in 2.11
3653+
{
3654+
var engine = new Engine();
3655+
engine.SetValue("zoo", new Zoo());
3656+
var kingManeLength = engine.Evaluate("zoo.King.maneLength");
3657+
Assert.Equal(10, kingManeLength.AsNumber());
3658+
}
3659+
3660+
[Fact]
3661+
public void CanFindDerivedPropertiesSucceed() // Similar case that continues to succeed
3662+
{
3663+
var engine = new Engine();
3664+
engine.SetValue("zoo", new Zoo());
3665+
var lionManeLength = engine.Evaluate("zoo.animals[0].maneLength");
3666+
Assert.Equal(10, lionManeLength.AsNumber());
3667+
}
36273668
}

Jint/Runtime/Interop/ObjectWrapper.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,10 @@ private PropertyDescriptor GetOwnProperty(JsValue property, bool mustBeReadable,
364364
}
365365

366366
var accessor = _engine.Options.Interop.TypeResolver.GetAccessor(_engine, ClrType, member, mustBeReadable, mustBeWritable);
367+
if (accessor == ConstantValueAccessor.NullAccessor && ClrType != Target.GetType())
368+
{
369+
accessor = _engine.Options.Interop.TypeResolver.GetAccessor(_engine, Target.GetType(), member, mustBeReadable, mustBeWritable);
370+
}
367371
var descriptor = accessor.CreatePropertyDescriptor(_engine, Target, member, enumerable: !isDictionary);
368372
if (!isDictionary
369373
&& !ReferenceEquals(descriptor, PropertyDescriptor.Undefined)

0 commit comments

Comments
 (0)