Comments (6)
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.
Sounds good!
Since it works in Nashorn, I'd also say that it's important from a migration perspective.
from graaljs.
Please note that this should already work for Java host classes. The limitation only applies to ProxyInstantiable.
from graaljs.
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 DynamicObject
s that are compatible with JSObject
? I'd appreciate any pointers for where to look to do this. Thanks!
from graaljs.
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.
@woess thank you, that worked well! An addition to ProxyInstantiable
would be cleaner, though.
from graaljs.
Related Issues (20)
- [23.1.2] WarnInterpreterOnly=false is ignored HOT 2
- Cannot call R code - how to install more supported language? HOT 1
- Using Promises setTimeout HOT 1
- polyglot Engine fails to initialize with "NoClassDefFoundError: com/ibm/icu/text/DateFormat" HOT 2
- Better stack trace for module not found error HOT 2
- Adding a classpath while no direct access to Node HOT 2
- Temporal toLocaleString returns ISO string instead of localized text HOT 1
- Parallel ScriptEngine script compilation
- Get (heap) memory usage information
- "ab".split(/(?:ab)*/) crashes graaljs HOT 1
- Losing double type when returning 1.0 from JS HOT 1
- Polyglot version compatibility check failed on GraalVM for JDK 22 Community 22.0.0
- Expose wrapPromise and Thenable in the GraalJS public API
- Polyglot options not working consistently with JS HOT 2
- No org.graalvm.js:js version 24.0.0 on Maven Central? HOT 4
- No language for id regex found, Supported languages are: [js] HOT 2
- Support for Node.js `http` and `fs` modules in GraalJS HOT 1
- Can not resolve NodeJS Promise Concurrently from Java Side HOT 6
- GraalJS should support Ahead-of-Time(AOT) compilation to Native binary like GraalPy.
- nodejs c-addon seems to leak HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from graaljs.