Skip to content

[GR-62854] [Native Image, WASM] The class annotated with @JS.Export isn’t available from JavaScript. #11122

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
2 tasks done
treblereel opened this issue May 2, 2025 · 2 comments
Assignees
Labels
bug native-image wasm-backend Issues related to the Wasm backend of GraalVM.

Comments

@treblereel
Copy link

Describe the Issue

The class annotated with @JS.Export isn’t available from JavaScript.

As I understand it, according to the documentation in JSObject and @JS.Export, in order for a Java class to be accessible from JavaScript you simply need to annotate the class with @JS.Export and have it extend JSObject:

package org.treblereel.graalvm.wasm;

import org.graalvm.webimage.api.JS;
import org.graalvm.webimage.api.JSObject;

import java.util.Random;

@JS.Export
public class Randomizer extends JSObject {
  private Random rng = new Random(719513L);

  public byte[] randomBytes(int length) {
    byte[] bytes = new byte[length];
    rng.nextBytes(bytes);
    return bytes;
  }
}

However, when I load it in JavaScript:

GraalVM.run([]).then(vm => {
  console.log("? " + JSON.stringify(vm.exports));
  const Randomizer = vm.exports.org.treblereel.graalvm.wasm.Randomizer;
  const r = new Randomizer();
  const bytes = r.randomBytes(1024);

  console.log("Random bytes: ", bytes);
});

I get:

TypeError: Cannot read properties of undefined (reading 'treblereel')

and it looks like vm.exports doesn’t contain any exports.

One more question: the docs say:

Exported classes should be used with care. Since these classes and all their methods are unconditionally included in the image, this annotation should only be used in cases in which the class is supposed to instantiated from JavaScript. If the JSObject subclass is only instantiated in Java and passed to JavaScript, then there is no need to use the @JS.Export annotation. Furthermore, the authors of exported classes should reduce the amount of code that is reachable from the methods of an exported class.

Let’s imagine this use case: you have a fairly large Java application with lots of dependencies and business logic, so you need to be careful about what you export. Ideally, in my case I’d like to export only a single method that takes a String (JSON/YAML/etc.) and returns a Promise. How can I achieve that?

it would probably be ideal if the svm-wasm-api were published on Maven. Thank you very much!

Using the latest version of GraalVM can resolve many issues.

GraalVM Version

java version "25" 2025-09-16 LTS
Java(TM) SE Runtime Environment Oracle GraalVM 25-dev+20.1 (build 25+20-LTS-jvmci-b01)
Java HotSpot(TM) 64-Bit Server VM Oracle GraalVM 25-dev+20.1 (build 25+20-LTS-jvmci-b01, mixed mode, sharing)

Operating System and Version

macOs 15.3.1 (24D70)

Troubleshooting Confirmation

Run Command

mvn clean package -Pwasm

Expected Behavior

export works

Actual Behavior

there is an exception in the console

Steps to Reproduce

  1. build the repro
  2. run http-server in /web
  3. open browser and check the console

export_wasm.zip

Additional Context

No response

Run-Time Log Output and Error Messages

No response

@treblereel
Copy link
Author

treblereel commented May 2, 2025

@fniephaus done, thank you

@fniephaus fniephaus added the wasm-backend Issues related to the Wasm backend of GraalVM. label May 2, 2025
@patrick96
Copy link
Member

Anything related to subclassing JSObject, including @JS.Export doesn't work at all, because we haven't implented that part yet.

I'll let you know once I make progress on this

@axel22 axel22 changed the title [Native Image, WASM] The class annotated with @JS.Export isn’t available from JavaScript. [GR-62854] [Native Image, WASM] The class annotated with @JS.Export isn’t available from JavaScript. May 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug native-image wasm-backend Issues related to the Wasm backend of GraalVM.
Projects
None yet
Development

No branches or pull requests

3 participants