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

graal.js compatibility problem #397

Closed
lordk911 opened this issue Dec 23, 2020 · 6 comments
Closed

graal.js compatibility problem #397

lordk911 opened this issue Dec 23, 2020 · 6 comments

Comments

@lordk911
Copy link

I have some code originally run on nashorn , when change to graal.js , the code not work.

My code will pass a java object as parameter to a js function.

The java object struct is :

public class AggregateResult {
    private List<ColumnIndex> columnList;
    private String[][] data;
    private String resultQuery;
    private long datasetId;
    private String aggConfig;

and the js function will use the object ,read out the data :

for (var i = 0; i < aggData.data.length; i++) {
        var newKey = getRowElements(aggData.data[i], keysIdx);
        var jk = newKey.join('-');
        if (_.isUndefined(joinedKeys[jk])) {
            castedKeys.push(newKey);
            joinedKeys[jk] = true;
        }

when use graal.js as ScriptEngine , an ERROR will throw out and tell me the aggData's data property is undefine when code run to :
aggData.data.length

@lordk911
Copy link
Author

I've tried to enable js.nashorn-compat=true , and the issues I mentioned above is gone. But I have another parameter which type is com.alibaba.fastjson.JSONObject. graal.js can't read it's property

@wirthi
Copy link
Member

wirthi commented Dec 23, 2020

Hi @lordk911

thanks for your question.

com.alibaba.fastjson.JSONObject seems to be a java.util.Map implementation. We currently do not special-case Maps in any way. This means, you can access a Java map from JavaScript just as you can in Java, by calling e.g. get and set. https://github.com/oracle/graaljs/blob/master/docs/user/JavaInteroperability.md#map-access I assume what your code tries to do is to access it with JavaScript's property access, e.g. print(javaMap.key). This does not work currently, you need to rewrite that to print(javaMap.get('key');. For the methods to be available, you need to have HostAccess enabled when creating the ScriptEngine/Context.

Another option is to wrap your Map into a ProxyObject. Then you can limit the changes to the JavaSide and need not change the JavaScript code. For a flat array, use ProxyObject.fromMap(yourJavaMap) on the Java side before passing the result to JavaScript (e.g. via a binding). For complex (nested) data structures, you will need your own implementation of the ProxyObject interface.

Best,
Christian

@lordk911
Copy link
Author

lordk911 commented Dec 24, 2020

Thank you. I understand.

@Phillipus
Copy link

Phillipus commented Jan 5, 2021

@wirthi

Thanks for you answer. I just ran into this same situation. I tried your suggestion of using ProxyObject.fromMap(yourJavaMap) and it works. But I was wondering...

This does not work currently

Are there plans to implement JS property type access on java.util.Map without using a ProxyObject?

@wirthi
Copy link
Member

wirthi commented Jan 7, 2021

Hi @Phillipus

yes this is planned, see e.g. #143

This is quite complicated as we have to implement it in the Truffle framework, so make it compatible with all programming languages we support (not just JavaScript). The Truffle team is working on it, but I don't think there is an ETA for that currently.

Best,
Christian

@Phillipus
Copy link

@wirthi Thanks for the info, I appreciate it. No hurry, as ProxyObject is working for me.

Phillipus added a commit to archimatetool/archi-scripting-plugin that referenced this issue Sep 6, 2022
Phillipus added a commit to archimatetool/archi-scripting-plugin that referenced this issue Oct 31, 2022
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

No branches or pull requests

3 participants