-
Notifications
You must be signed in to change notification settings - Fork 195
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
Custom Java -> JS Conversion #144
Comments
Bump... Any thoughts @chumer ? Is |
No there is currently no equivalent mapping mechanism from Host Java to JS. |
Thanks for the response. Yes we've been using ProxyObject... a lot. But we might be able to eliminate a bunch of proxies with equivalent Java -> JS type mapping. One example involves an object with a date, where we want it to be both a Java Date and a JS Date. It needs conversion even when the parent object implements ProxyObject. I suppose one might be able to make some complex 'Date' proxy that fulfills both Java and JS without conversion... but that doesn't sound too palatable... |
We have plans to add first-class date support in interop. So we will do that conversion for you. (actually we won't convert, we will just interpret Java dates as JS dates and vice versa). |
@chumer do you have any ETA when Date support will be released? Currently I have an issue with passing js |
@mdsina For JS-Date to Java-Date, |
@chumer Another simpler case we've encountered is:
If we had the equivalent java->js type mapping, we wouldn't need every POJO containing an enum to be/use a proxy. |
And while the enum and Date are simple example cases, it's the same story anywhere a proxy/conversion is needed... Either the class is replaced with a proxy (everywhere used), or every parent container must become a proxy to handle the field (at least for Java enum <-> JS String, see above). If we had Java->Js type mapping, such a proxy could be defined once there, and automatically handled by the the system, including nested situations (field of a parent POJO) etc. Your thoughts, @chumer ? |
Hi @chumer , I'm pinging this again since:
|
I wanted to chime in with another use-case for this that first-class dates don't cover. I wanted to pass Jackson's JsonNode values into JS, and tried to define a ProxyObject and ProxyArray around the ObjectNode and ArrayNode respectively. This caused a problem, though. In Jackson, accessing an element of a JsonNode returns another JsonNode, so my ProxyObject object needs to wrap the result in another proxy before returning it from ProxyObject::get. However, if I wrap a new Proxy instance around it, I have the following issue: Suppose I have an object with a nested object, represented as a JsonNode. x = { foo: {bar: 'old' } } I create a proxy around that value and pass it into the following JS function: function(x) {
foo1 = x.foo;
foo2 = x.foo;
foo1.bar = 'new';
return [ Object.is(foo1, foo2), foo2.bar];
} This returns If Graal provided a way to define a custom mapping for Java objects when passing them into Graal, I could define that function to wrap a JsonNode in a ProxyObject. Graal would then, I presume, track the JsonNode's identity correctly in the already-existing code-path, making this use-case much simpler. |
Since there seems to be more interest in this issue. I've targeted this issue for 21.3. Can't promise it will make it though. |
(Originally posted in Slack:) The biggest use-case for us is we have a TON of Java methods that return Single (the majority of our Java code is RxJava) that need to be converted to Promises. To accomplish this I have to create a wrapper class for each Java class where each method is basically the same thing,
Another use-case for us is we have a few Java methods that return |
Hi Christian, We are migrating away from Rhino to GraalJS. Users of our system can create JS formulas that call into Java code. JS formulas can be tagged as "Wrap Java Primitives", in which case the "double" return value from a Java call is the real Java Object. We use Rhino's WrapFactory to decide how a primitive is presented to the JS code. a JS number or a JavaObject.
|
Hi, great to see some movement here! Our main reason why we still require the whole proxy infrastructure: JDBC 4.2+ has defined automatic type conversion for OffsetDateTime to the respective DB type, TIMESTAMP WITH TIMEZONE. There is no support for ZonedDateTime. As persistence is an important aspect of our framework, it uses internally OffsetDateTime in arbitrarily complex structures of maps/lists and all execution logic is based upon these. As Polyglot has chosen to only support ZonedDateTime, not OffsetDateTime, we've written a whole infrastructure of ProxyMaps, ProxyArrays, and so forth, to pass OffsetDateTime instances as a JS Date by ourselves, and this extends to argument lists of functions, promises, virtually everything. |
Hi, @zikifer, I think what you need is to set the |
Hi @gabrielmattar, thanks for the suggestion! This feature flag does make list -> array conversion better, and I can do without the additional |
@zikifer That might be something worth filing in another issue. I think at least this should be fixed. |
I had a similar case with vertx, async methods return |
The new
HostAccess.Builder#targetTypeMapping
API (see #94) is fantastic for JS -> Java conversion. Thank you!Can we similarly handle Java -> JS conversion? I tried using
.targetTypeMapping(MyPojo.class, Value.class, ..., ...)
, but it was not triggered when:context.getBindings("js").putMember("myPojo", myPojoInstance)
, but this probably makes sense, because it's added as a host objectvalue.execute(myPojoInstance)
wherevalue
is a JS function accepting an arg.Does
targetTypeMapping
only apply to JS -> Java conversions? If it does apply to Java -> JS conversions, how can I correctly use it?The text was updated successfully, but these errors were encountered: