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
704 stars 70 forks source link

Does Javet support assignment operations on nested complex objects after performing a bind? #407

Closed eastack closed 3 hours ago

eastack commented 3 hours ago

Javet looks fantastic! I tried assigning properties to a bound object that contains complex objects, and the result left me a bit confused. I went through almost all the documentation, but I’m still not sure what’s happening or how I should proceed. Could you help clarify this for me?

    public static void main(String[] args) throws JavetException {
        try (V8Runtime v8Runtime = V8Host.getV8Instance().createV8Runtime()) {
            JavetStandardConsoleInterceptor javetStandardConsoleInterceptor = new JavetStandardConsoleInterceptor(v8Runtime);
            javetStandardConsoleInterceptor.register(v8Runtime.getGlobalObject());

            User me = new User();
            try (V8ValueObject v8ValueObject = v8Runtime.createV8ValueObject()) {
                v8Runtime.getGlobalObject().set("me", v8ValueObject);
                v8ValueObject.bind(me);
            }

            Password password = new Password();
            try (V8ValueObject v8ValueObject = v8Runtime.createV8ValueObject()) {
                v8Runtime.getGlobalObject().set("password", v8ValueObject);
                v8ValueObject.bind(password);
            }
            password.setHashedPassword("${plain}12345");
            password.setHashMethod("plain");

            //me.setPassword(password);
            // Commenting out the line above will print -> "me.password is initially null."
            // Uncomment out the line above will set password value in java and print -> "me.password is initially undefined."

            v8Runtime.getExecutor("console.log(`me.password is initially ${me.password}.`);").executeVoid();
            v8Runtime.getExecutor("console.log(`password is initially ${JSON.stringify(password)}.`);").executeVoid();
            // set password in js will throw exception
            // Caused by: com.caoccao.javet.exceptions.JavetException:
            //      Callback signature mismatches:
            //          expected parameter type is class me.wangheng.javet.Password,
            //          actual parameter type is class java.util.HashMap
            v8Runtime.getExecutor("me.password = password;").executeVoid();
            v8Runtime.getExecutor("console.log(`me.password is initially ${me.password}.`);").executeVoid();
        }
    }
eastack commented 3 hours ago

Perhaps it would be more appropriate to post my question in the Discussions section. I'll go ahead and close this issue.

Does Javet support assignment operations on nested complex objects after performing a bind?