Giter Club home page Giter Club logo

Comments (6)

chumer avatar chumer commented on June 4, 2024 1

That is an interesting feature indeed. And we already discussed this internally. It requires to extend our interop protocol. So will take a bit to land.

from graaljs.

provegard avatar provegard commented on June 4, 2024

Sounds good!

Since it works in Nashorn, I'd also say that it's important from a migration perspective.

from graaljs.

chumer avatar chumer commented on June 4, 2024

Please note that this should already work for Java host classes. The limitation only applies to ProxyInstantiable.

from graaljs.

c9r avatar c9r commented on June 4, 2024

It appears that foo instanceof SomeProxyInstantiable doesn't work even when SomeProxyInstantiable also implements ProxyObject with a prototype member that is in the __proto__ chain of foo.

I realize that __proto__ is non-standard. Unfortunately, Object.setPrototypeOf(someProxyObject, someProxyObjectPrototype) doesn't work either. No exception is thrown, but Object.getPrototypeOf(someProxyObject) always returns null when called on a ProxyObject.

I've also tried to use the com.oracle.truffle.js APIs directly—which I would prefer anyway over the lowest common denominator polyglot APIs—but I keep running into private access roadblocks.

Is there any way at all to get instanceof to work for bridged objects? I'm open to any approach that I can do procedurally from Java. We're trying to procedurally map a large Java API to JavaScript, but with a number of transformations and context-sensitive security restrictions that make host access a no-go. Is there a proper way to get my hands on a JSContext (JavaScriptLanguage.getContext(Context) seems to throw under all circumstances) so that I can create Truffle DynamicObjects that are compatible with JSObject? I'd appreciate any pointers for where to look to do this. Thanks!

from graaljs.

woess avatar woess commented on June 4, 2024

I don't know if this would be as satisfactory solution for your use case, but you can test instanceof against a JS object that has a Symbol.hasInstance method that you can use to implement your own check, like so:

function MyType() {
    return new TypeLike(); // constructs the ProxyObject
}
Object.defineProperty(MyType, Symbol.hasInstance, {
  value: function myinstanceof(obj) {
    return isInstanceLike(obj);
  }
});
...
var instance = new MyType();
print(instance instanceof MyType);

Now the problem is how to actually check if the object is an instance of your ProxyObject. You can't do that from JS, but you could use another ProxyExecutable for that:

public static class IsInstanceLike implements ProxyExecutable {
    @Override
    public Object execute(Value... arguments) {
        Value obj = arguments[0];
        return obj.isProxyObject() && obj.asProxyObject() instanceof InstanceLike;
    }
}
context.getBindings("js").putMember("isInstanceLike", new IsInstanceLike());

from graaljs.

provegard avatar provegard commented on June 4, 2024

@woess thank you, that worked well! An addition to ProxyInstantiable would be cleaner, though.

from graaljs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.