Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JS Proxy on Clr Object - Existing methods on underlying object cannot be called #1674

Closed
martinbu opened this issue Nov 8, 2023 · 0 comments · Fixed by #1675
Closed

JS Proxy on Clr Object - Existing methods on underlying object cannot be called #1674

martinbu opened this issue Nov 8, 2023 · 0 comments · Fixed by #1675

Comments

@martinbu
Copy link

martinbu commented Nov 8, 2023

Hi,

I am using Jint 3.0.0-beta-2054 now. And issue/discussion #1643 works as expected now. Thank you @viruscamp
But I think I have found another issue with clr objects. Calling an existing function on a proxy object leads to System.Reflection.TargetException: 'Object does not match target type.'

class TestClass
{
    public string Name => "My Name is Test";

    public void SayHello()
    {
        Console.WriteLine("Hello");
    }

    public int Add(int a, int b)
    {
        return a + b;
    }
}
new 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.'
    """);

The same code is working fine in pure javascript

class TestClass {
  get Name() {
    return 'My name is test';
  }

  SayHello() {
    console.log('Hello', this.Name);
  }

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

const handler = {
    get(target, property, receiver) {

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

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

const T = new TestClass();

const p = new Proxy(T, handler);
const name = p.Name;
const s = p.GetX();
console.log(s);

p.SayHello();
const t = p.Add(5,3);
console.log(t);

/* Output
> "Not available"
> "Hello" "My name is test"
> 8
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant