Skip to content

Commit feeab47

Browse files
lahmaMartin Burtscher
andauthored
Fix MethodAccessor property flags (#2153)
--------- Co-authored-by: Martin Burtscher <[email protected]>
1 parent c83b629 commit feeab47

File tree

3 files changed

+87
-2
lines changed

3 files changed

+87
-2
lines changed

Jint.Tests/Runtime/PropertyDescriptorTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ JsValue ExtractClrAccessDescriptor(JsValue jsArugments)
189189
public void PropertyDescriptorMethod()
190190
{
191191
var pdMethod = _engine.Evaluate("Object.getOwnPropertyDescriptor(testClass, 'Method')");
192-
CheckPropertyDescriptor(pdMethod, false, false, false, true, false, false);
192+
CheckPropertyDescriptor(jsPropertyDescriptor: pdMethod, configurable: true, enumerable: false, writable: false, hasValue: true, hasGet: false, hasSet: false);
193193

194194
var pd = _engine.Evaluate("testClass").AsObject().GetOwnProperty("Method");
195195
// use PropertyDescriptor to wrap method directly

Jint.Tests/Runtime/ProxyTests.cs

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,4 +522,89 @@ public void ProxyIterateClrList()
522522

523523
Assert.Equal([1, 2, 3, 3], res.AsArray());
524524
}
525+
526+
[Fact]
527+
public void ProxyClrObjectMethod()
528+
{
529+
var res = _engine
530+
.SetValue("T", new TestClass())
531+
.Evaluate("""
532+
const handler = {
533+
get(target, property, receiver) {
534+
535+
if (property == "Add") {
536+
return function(...args) { return 42};
537+
}
538+
539+
return Reflect.get(...arguments);
540+
}
541+
};
542+
543+
const p = new Proxy(T, handler);
544+
p.Add(5,3); // throws 'get' on proxy: property 'Add' is a read-only and non-configurable data property
545+
// on the proxy target but the proxy did not return its actual value
546+
// (expected 'function Jint.Tests.Runtime.ProxyTests+TestClass.Add() { [native code] }' but got 'function () { [native code] }')
547+
""");
548+
549+
Assert.Equal(42, res.AsInteger());
550+
}
551+
552+
[Fact]
553+
public void ProxyClrObjectMethodWithDelegate()
554+
{
555+
var res = _engine
556+
.SetValue("T", new TestClass())
557+
.Evaluate("""
558+
const handler = {
559+
get(target, property, receiver) {
560+
561+
if (property == "Add") {
562+
return (...args) => 42;
563+
}
564+
565+
return Reflect.get(...arguments);
566+
}
567+
};
568+
569+
const p = new Proxy(T, handler);
570+
p.Add(5,3); // throws 'get' on proxy: property 'Add' is a read-only and non-configurable data property
571+
// on the proxy target but the proxy did not return its actual value
572+
// (expected 'function Jint.Tests.Runtime.ProxyTests+TestClass.Add() { [native code] }' but got 'function () { [native code] }')
573+
""");
574+
575+
Assert.Equal(42, res.AsInteger());
576+
}
577+
578+
[Fact]
579+
public void ProxyClrObjectWithTmpObjectMethod()
580+
{
581+
var res = _engine
582+
.SetValue("T", new TestClass())
583+
.Evaluate("""
584+
const handler = {
585+
get(target, property, receiver) {
586+
587+
if (property == "Add") {
588+
return (...args) => target.target[property](...args) + 34;
589+
}
590+
591+
if (typeof target.target[property] === "function")
592+
return (...args) => target.target[property](...args);
593+
594+
return Reflect.get(target.target, property, receiver)
595+
}
596+
};
597+
598+
const tmpObj = { target: T };
599+
const p = new Proxy(tmpObj, handler);
600+
601+
const name = p.Name;
602+
p.SayHello();
603+
const res = p.Add(5,3); // works now
604+
605+
name + " " + res
606+
""");
607+
608+
Assert.Equal("My Name is Test 42", res.AsString());
609+
}
525610
}

Jint/Runtime/Interop/Reflection/MethodAccessor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@ protected override void DoSetValue(object target, string memberName, object? val
2323

2424
public override PropertyDescriptor CreatePropertyDescriptor(Engine engine, object target, string memberName, bool enumerable = true)
2525
{
26-
return new(new MethodInfoFunction(engine, _targetType, target, memberName, _methods), PropertyFlag.AllForbidden);
26+
return new(new MethodInfoFunction(engine, _targetType, target, memberName, _methods), PropertyFlag.Configurable | PropertyFlag.NonData);
2727
}
2828
}

0 commit comments

Comments
 (0)