I use e.g.
context.getBindings("js").putMember("thing", new Thing(this));
to define a variable for my javascript.
How can I expose Java's Math
""""object""""?
I can't do
context.getBindings("js").putMember("math", Math);
nor
context.getBindings("js").putMember("math", new Math());
(bc the constructor is private)
Even using reflection like this to pass it
Constructor[] cs = Math.class.getDeclaredConstructors(); cs[0].setAccessible(true); Math m = (Math) cs[0].newInstance(); context.getBindings("js").putMember("math", m);
Doesn't work in the end, because graaljs doesn't make static properties of Java host objects visible to the JavaScript.
So the solution is to use the .static
property of Math.class
context.getBindings("js").putMember("math", Math.class); System.out.println(context.eval("js", "math.static.toRadians(180)")); // prints 3.141592653589793
As I learnt here