xpenatan/gdx-teavm

how to use js library?

thawri1 opened this issue · 2 comments

I want to use js library in my game.
for example Yandex Game SDK:
https://yandex.com/dev/games/doc/en/sdk/sdk-about
is it possible?

I found this:
https://teavm.org/docs/runtime/jso.html
but stuck at the import module section

this code works:

    @JSBody(
        params = { "message" },
        script = "console.log(message)"
    )
    public static native int callModule(String message);

but if I add import, not working:

@JSBody(
        script = "return testModule.foo();",
        imports = @JSBodyImport(
            alias = "testModule",
            fromModule = "testModule.js"))
    public static native int callModule();

js console showing this error:
Uncaught TypeError: Cannot read properties of undefined (reading 'foo')
maybe the testModule.js file location is wrong.
I tested in the root folder, assets folder, and beside the TeaVmLauncher class. I have also tested the full path address.

private static final String addYandexLibScript = "var my_awesome_script = document.createElement('script');\n" +
        "\n" +
        "my_awesome_script.setAttribute('src','https://yandex.ru/games/sdk/v2');\n" +
        "\n" +
        "document.head.appendChild(my_awesome_script);";

    @JSBody(script = addYandexLibScript)
    public static native void addYandexLib();

    private static final String yandex =  "YaGames\n" +
        "    .init()\n" +
        "    .then(ysdk => {\n" +
        "        console.log('Yandex SDK initialized');\n" +
        "        window.ysdk = ysdk;\n" +
        "    });";


    @JSBody(script = yandex)
    public static native void initYandex();