Attila-
Perhaps a more fundamental question is, is what I am trying to do
(implement commonJS require) really possible in Vanilla Nashorn?
I did notice this post by you in December 2012:
http://mail.openjdk.java.net/pipermail/nashorn-dev/2012-December/000014.html
I understand there is no intention to implement require() directly in
Nashorn, but my previous understanding was that it should be possible to
implement it myself on top of Nashorn given that I can create different
scopes and pass JS objects between them. After my recent experiments I'm
not sure that understanding is really correct!
What's your view on this?
On 10/12/13 19:49, Tim Fox wrote:
Sorry for the deluge of posts, but I think I've found another issue
with scopes/ScriptObjectMirror.
Consider this case: https://gist.github.com/purplefox/7896892
I have a simple JS object which contains a function 'setCallback'
which simply delegates to a Java object which also has a setCallback
method.
Calling this method works fine from within the same scope that it was
created in.
However, if I export the object to another scope - wrapping in
ScriptObjectMirror as advised (and this is exactly the kind of thing I
would need to do to implement CommonJS require in Nashorn), and then
try to call the setCallback method from there, it yields the following
exception:
java.lang.ClassCastException: Cannot cast
jdk.nashorn.api.scripting.ScriptObjectMirror to java.lang.Runnable
at
sun.invoke.util.ValueConversions.newClassCastException(ValueConversions.java:461)
at
sun.invoke.util.ValueConversions.castReference(ValueConversions.java:456)
at jdk.nashorn.internal.scripts.Script$\^eval\_._L2(<eval>:3)
at
jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:500)
at
jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:207)
at
jdk.nashorn.internal.runtime.ScriptRuntime.apply(ScriptRuntime.java:378)
at
jdk.nashorn.api.scripting.ScriptObjectMirror.call(ScriptObjectMirror.java:107)
at jdk.nashorn.internal.scripts.Script$\^eval\_.runScript(<eval>:1)
at
jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:498)
at
jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:207)
at
jdk.nashorn.internal.runtime.ScriptRuntime.apply(ScriptRuntime.java:378)
at
jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:544)
at
jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:526)
at
jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:522)
at
jdk.nashorn.api.scripting.NashornScriptEngine.eval(NashornScriptEngine.java:193)
at
org.vertx.java.platform.impl.HandlerScopeTest.run(HandlerScopeTest.java:48)
at
org.vertx.java.platform.impl.HandlerScopeTest.main(HandlerScopeTest.java:9)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at
com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)