caoccao / Javet

Javet is Java + V8 (JAVa + V + EighT). It is an awesome way of embedding Node.js and V8 in Java.
https://www.caoccao.com/Javet/
Apache License 2.0
712 stars 70 forks source link

inject java obj to v8 #372

Closed xiaoye-2018 closed 2 months ago

xiaoye-2018 commented 2 months ago

beacuase my project use large js config, and alway use JDK Noshorn engine, recently I found this project, its use v8 as js engine, its significant improvement in performance。but I found some problem, please help.

eg: I want to inject a java pojo to javascript ,but it not work. But able to work in noshorn code:

  private static void test_noshorn() throws ScriptException {
        ScriptEngineManager sem = new ScriptEngineManager();
        ScriptEngine engine = sem.getEngineByName("javascript");
        User user = new User(1, "user", 22);
        HashMap<String, Object> map = new HashMap<>();
        map.put("user", user);
        SimpleBindings simpleBindings = new SimpleBindings(map);
        Object eval = engine.eval("user.name;", simpleBindings);
        System.out.println(eval);
    }

    private static void test_v8() {
        try (V8Runtime v8Runtime = V8Host.getV8Instance().createV8Runtime()) {
            // V8Locker v8Locker = v8Runtime.getV8Locker();
            V8ValueGlobalObject globalObject = v8Runtime.getGlobalObject();
            User user = new User(1, "user", 22);
            globalObject.set("user", user);
            IV8Executor executor = v8Runtime.getExecutor("user;");
            String s1 = executor.executeString();
            System.out.println(s1);

        } catch (JavetException e) {
            throw new RuntimeException(e);
        }
    }

output: user // noshorn null // v8

Is it possible to provide similar support? I also want to know how V8Locker in this project improves performance. I looked through the code but didn't find any results.

xiaoye-2018 commented 2 months ago

And I found that another project j2v8 has a higher execution speed than your project. Why is this? Is it because too many things are introduced in the design?

anonyein commented 2 months ago

And I found that another project j2v8 has a higher execution speed than your project. Why is this? Is it because too many things are introduced in the design?

What is "User"? From your code IV8Executor executor = v8Runtime.getExecutor("user;"); end with ";", output "null" is right!! J2V8 hasnot been maintained for years!

caoccao commented 2 months ago

Please go over the tutorials. There are a couple of things you've missed.

xiaoye-2018 commented 2 months ago

Please go over the tutorials. There are a couple of things you've missed.

Although I solved my problem using JavetProxyConverter, I think the official documentation examples are not clear and not practical. Only part of the content can be seen in the source code

xiaoye-2018 commented 2 months ago

Why does the following code prompt that the object is not recycled? image

try (V8Runtime v8Runtime = V8Host.getV8Instance().createV8Runtime()) {
            // JavetBridgeConverter javetBridgeConverter = new JavetBridgeConverter();
            JavetProxyConverter converter = new JavetProxyConverter();
            v8Runtime.setConverter(converter);
            V8Locker v8Locker = v8Runtime.getV8Locker();
            V8ValueGlobalObject globalObject = v8Runtime.getGlobalObject();

            User user = new User(1, "user", 22);
            HashMap<String, Object> map = new HashMap<>();
            map.put("p1", user);
            // map.put("p2", "p2");

            // globalObject.setProperty("user2", user);
            globalObject.set("map", map);
            String s = v8Runtime.getExecutor("map.p1;").executeString();
            System.out.println(s);
            v8Locker.close();
            globalObject.close();
        } catch (JavetException e) {
            throw new RuntimeException(e);
        }
caoccao commented 2 months ago

Obviously, you haven't fully understood the tutorial. Run the code in the tutorial and find out why it doesn't have any leaks.

xiaoye-2018 commented 2 months ago

I just performed a small case, which is a bit difficult for novices