CodeBaseX / jav8

Automatically exported from code.google.com/p/jav8
0 stars 0 forks source link

Cannot pass an exposed java function to a new variable. #11

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
Simplified Test case:

    ScriptEngine eng = new ScriptEngineManager().getEngineByName("jav8"); 
    MyObject myObj = new MyObject(); // Some object with a function, f(), to expose
    eng.put("myObj", myObj); // Expose myObj to javascript.
    eng.eval("myObj.f()"); // This line works as expected.
    eng.eval("var f = myObj.f;"); // This line runs silently.
    eng.eval("f()"); // This line errors.

What is the expected output? What do you see instead?

Expected the same result as if  eng.eval("myObj.f()");  was run.
Instead get the stack trace:

Exception in thread "main" javax.script.ScriptException: 
javax.script.ScriptException: java.lang.NullPointerException
    at lu.flier.script.V8ScriptEngine.eval(Unknown Source)
    at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:264)
    at page.freddie.melonheads.Test.main(Test.java:14)
Caused by: javax.script.ScriptException: java.lang.NullPointerException
    at lu.flier.script.V8CompiledScript.eval(Unknown Source)
    ... 3 more
Caused by: java.lang.NullPointerException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at lu.flier.script.V8CompiledScript.internalExecute(Native Method)
    ... 4 more

What version of the product are you using? On what operating system?

Using jav8-0.3 on Ubuntu 12.04 64bit compiled from source for amd64.

Please provide any additional information below.

Original issue reported on code.google.com by a.freddi...@gmail.com on 22 Aug 2012 at 8:48

GoogleCodeExporter commented 9 years ago
could you provide a complete demo to reproduce your issue?

The unit test has covered a similar usage

    @Test
    public void testJavaFunction() throws ScriptException, SecurityException, NoSuchMethodException
    {
        class Person {
            public String hello(String name) {
                return "hello " + name; 
            }
        }

        Person person = new Person();

        this.eng.put("person", person);
        this.eng.put("sprintf", this.getClass().getMethod("sprintf", String.class, V8Array.class));

        assertEquals("hello flier", this.eng.eval("person.hello('flier')"));
        assertEquals("hello world, 42", this.eng.eval("sprintf('hello %s, %d', ['world', 42]);"));
    }

Original comment by flier...@gmail.com on 14 Jun 2013 at 6:56

GoogleCodeExporter commented 9 years ago

Original comment by flier...@gmail.com on 14 Jun 2013 at 7:47