Javet icon indicating copy to clipboard operation
Javet copied to clipboard

proxyApply never called

Open huangjunAAAA opened this issue 9 months ago • 1 comments

IJavetDirectProxyHandler.proxyApply 什么情况下能被调用? 我创建了一个类实现了IJavetDirectProxyHandler 接口,函数调用一直回调的是proxyGet,从来不调用 proxyApply 。 下面是简单样例

public class TestProxyObject implements IJavetDirectProxyHandler<Exception> {

    // never get called.
    @Override
    public V8Value proxyApply(V8Value target, V8Value thisObject, V8ValueArray arguments) throws JavetException, Exception {

        System.out.println("proxyApply:"+target+" "+thisObject+" "+arguments);
        return IJavetDirectProxyHandler.super.proxyApply(target, thisObject, arguments);
    }


    @Override
    public V8Value proxyGet(V8Value target, V8Value property, V8Value receiver) throws JavetException, Exception {
        System.out.println("proxyGet:" + property.toString());
        return IJavetDirectProxyHandler.super.proxyGet(target, property, receiver);
    }
}

下面是测试代码

@Slf4j
@Service
public class V8Test {


    private static final IJavetEnginePool<V8Runtime> javetEnginePool = new JavetEnginePool<>();

    public static void test2() {
        Object o = exec(" tst.m1(1,2,'3')");
        System.out.println("o = " + o);

    }


    public static void main(String[] args) {
        test2();
    }


    public static Object exec(String script) {
        try {
            V8Runtime v8Runtime = getRuntime();
            V8Value tst = new JavetProxyConverter().toV8Value(v8Runtime, new TestProxyObject());
            v8Runtime.getGlobalObject().set("tst", tst);
            Object o = v8Runtime.getExecutor(script).executeObject();
            return o;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static V8Runtime getRuntime() {
        try (IJavetEngine<V8Runtime> javetEngine = javetEnginePool.getEngine()) {
            V8Runtime v8Runtime = javetEngine.getV8Runtime();
            JavetStandardConsoleInterceptor consoleInterceptor = new JavetStandardConsoleInterceptor(v8Runtime);
            consoleInterceptor.register(v8Runtime.getGlobalObject());
            return v8Runtime;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return null;
    }
}

huangjunAAAA avatar Mar 27 '25 08:03 huangjunAAAA

You need to pass the class new JavetProxyConverter().toV8Value(v8Runtime, TestProxyObject.class);.

caoccao avatar Mar 27 '25 13:03 caoccao