google / j2cl

Java to Closure JavaScript transpiler
Apache License 2.0
1.25k stars 146 forks source link

goog.reflect.sinkValue and deadcode elimination #202

Closed treblereel closed 1 year ago

treblereel commented 1 year ago

One of the tips for preventing code removal by the compiler is the use of reflect.sinkValue from closure-library. Is it possible to use this feature without resorting to creating native.js?

lets say I have something like this:

public class Test {
    Random random = new Random();
    private void test() {
        DomGlobal.console.log("test "  + random.nextInt(10) + this.getClass().getCanonicalName());
    }
}

And I want to call it like this:

        Test test = new Test();
        ((Function)Js.asPropertyMap(test).get(objectProperty("m_test___$p_org_treblereel_Test", test))).bind(test).call();
         // it works for non inlined/removed functions       
    @JsMethod(namespace = "goog.reflect")
    public native static String objectProperty(String property, Object object);

What should I pass to sinkValue wrapper?

    @JsMethod(namespace = "goog.reflect")
    public native static Object sinkValue(??? value);

As I can see from the docs I should use something like sinkValue(test.m_test___$p_org_treblereel_Test) but it's not possible from java.