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

[GR-54959] ProxyInstantiable carries no class information #9167

Open
sgammon opened this issue Jun 23, 2024 · 4 comments
Open

[GR-54959] ProxyInstantiable carries no class information #9167

sgammon opened this issue Jun 23, 2024 · 4 comments
Assignees
Labels

Comments

@sgammon
Copy link

sgammon commented Jun 23, 2024

Describe the issue
I want to create polyglot-capable objects with a ProxyInstantiable, but I also want such objects to pass type checks in guest languages.

Code snippet or code repository that reproduces the issue

val ctx: Context = /* ... */
val bindings = ctx.getBindings("js").putMember("SomeType", ProxyInstantiable {
  SomeImplementingHostType()
})

Later, in a guest language...

const x = new SomeType()
x instanceof SomeType  // always returns `false`, because `SomeType` is a `ProxyInstantiable`

Additional context
I've considered the alternative of a facade guest type, like:

// obtain raw host type somehow
const RawSomeType = /* ... */

class SomeType extends RawSomeType {
  // ...
}

But this requires up-front execution of preparatory code in the guest context.

I've also tried installing the host type directly, but then I must register my constructors for reflective access, and I must share host constructors with guest constructors, which especially complicates instantiation with varargs Value. The flexibility of ProxyInstantiable, which allows vararg Value, is especially useful for interop with JavaScript, but is hard to adapt to type checks.

@selhagani selhagani self-assigned this Jun 24, 2024
@selhagani
Copy link
Member

Hi @sgammon , thanks for reaching out to us. We'll be taking a look into this shortly

@selhagani selhagani changed the title ProxyInstantiable carries no class information [GR-54959] ProxyInstantiable carries no class information Jun 26, 2024
@selhagani selhagani assigned chumer and unassigned selhagani Jun 26, 2024
@iamstolis
Copy link
Member

iamstolis commented Jun 27, 2024

I've considered the alternative of a facade guest type, like:
...
but, constructors can't return other types

In fact, constructors (in JavaScript) can return arbitrary objects and you can abuse that to implement what you need using something like

class SomeType {
    constructor(...args) {
        return new RawSomeType(...args);
    };
    static [Symbol.hasInstance](o) {
        // some code that is able to recognize instances of RawSomeType
        // and return true for them (and false otherwise)
    }
}

Would that work for you?

@sgammon
Copy link
Author

sgammon commented Jun 27, 2024

@iamstolis Lol. JavaScript never ceases to amaze. I updated my description.

This will work for us for now, yes, but this approach generally requires us to execute preparatory code in the guest context before executing actual guest code which relies on instanceof working.

Ideally, we'd be able to inject a type from the host context, without resorting to guest code. It would be really cool to make static [Symbol.hasInstance] possible from the host, and I don't know of a way to do that today.

@iamstolis
Copy link
Member

This will work for us for now, yes, but this approach generally requires us to execute preparatory code in the guest context before executing actual guest code ... Ideally, we'd be able to inject a type from the host context, without resorting to guest code.

I am sorry, I don't understand why exactly are you more concerned by the execution of preparatory code compared to the usage of methods of Value class (like putMember()).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants