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

JavaScript can't see Java properties/functions #13

Closed
adikm opened this issue Jun 8, 2018 · 3 comments
Closed

JavaScript can't see Java properties/functions #13

adikm opened this issue Jun 8, 2018 · 3 comments
Assignees

Comments

@adikm
Copy link

adikm commented Jun 8, 2018

Hey,
I'm playing around with GraalVM/Graaljs and I stumbled upon an issue. Snippet first:

class Application {
    public static void main(String[] args) {
        String script = "console.log(phone.number)";
        String script2 = "phone.call('Somebody')";
        Phone phone = new Phone();

        Context context = Context.create("js");
        context.getBindings("js").putMember("phone", phone);

        runScript(script, context);
        runScript(script2, context);
    }
    private static Value runScript(String script, Context context) {
        return context.eval("js", script);
    }
}
class Phone {
    public int number = 123;
    public void call(String name) {
        System.out.println("Calling...: " + name);
    }
}

When I execute the main method, that's what I'm getting:
image

In my pom.xml, there's graal-sdk included.

@iamstolis
Copy link
Member

Note that your snippet will work when you make your Phone class public. I will let others comment on whether this behaviour is a bug or an intention.

@adikm
Copy link
Author

adikm commented Jun 8, 2018

Thank you @iamstolis. I missed that.
Going to leave it open though because as you said - it may be not intended.

@woess
Copy link
Member

woess commented Jun 8, 2018

It's intentional (and afaik in line with Nashorn).
While the Application class has access to Phone's members, the engine has not.

@woess woess closed this as completed Jun 8, 2018
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