Skip to content

Commit

Permalink
JsProxy should return proxied target's ToObject via ToObject (#1675)
Browse files Browse the repository at this point in the history
  • Loading branch information
lahma authored Nov 10, 2023
1 parent bf72899 commit cbe3b6f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
39 changes: 39 additions & 0 deletions Jint.Tests/Runtime/ProxyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,17 @@ class TestClass

private int x = 1;
public int PropertySideEffect => x++;

public string Name => "My Name is Test";

public void SayHello()
{
}

public int Add(int a, int b)
{
return a + b;
}
}

[Fact]
Expand Down Expand Up @@ -443,4 +454,32 @@ public void ClrPropertySideEffect()
Assert.Equal(2, _engine.Evaluate("p.PropertySideEffect").AsInteger()); // no call to PropertySideEffect
Assert.Equal(2, TestClass.Instance.PropertySideEffect); // second call to PropertySideEffect
}

[Fact]
public void ToObjectReturnsProxiedToObject()
{
_engine
.SetValue("T", new TestClass())
.Execute("""
const handler = {
get(target, property, receiver) {

if (!target[property]) {
return (...args) => "Not available";
}

// return Reflect.get(target, property, receiver);
return Reflect.get(...arguments);
}
};

const p = new Proxy(T, handler);
const name = p.Name; // works
const s = p.GetX(); // works because method does NOT exist on clr object

p.SayHello(); // throws System.Reflection.TargetException: 'Object does not match target type.'
const t = p.Add(5,3); // throws System.Reflection.TargetException: 'Object does not match target type.'
""");

}
}
2 changes: 2 additions & 0 deletions Jint/Native/Proxy/JsProxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ public override bool IsArray()
return _target.IsArray();
}

public override object ToObject() => _target.ToObject();

internal override bool IsConstructor
{
get
Expand Down

0 comments on commit cbe3b6f

Please sign in to comment.