Skip navigation links

Package org.teavm.jso

JSO is a specification that describes how Java platform can interact with JavaScript code.

See: Description

Package org.teavm.jso Description

JSO is a specification that describes how Java platform can interact with JavaScript code. There are two parts of this specification: JSO core, that defines mapping between Java and JavaScript objects and JSO APIs, that define Java wrappers around various JavaScript and HTML5 APIs. The latter part is simply application of the former one. JSO implementor must implement only the first part, and it may ignore the second part, since it should work properly this way. However, it may implement some of the APIs itself in some cases, for example to improve performance.

The first part of JSO is directly in this package. All subpackages declare the second part of JSO.

JSO does not do anything itself. It is only a set of interfaces that define interaction. To use JSO in your application, you should include one of its implementations, that may exist for different platforms, such as JVM, RoboVM, Android, TeaVM or bck2brwsr.

JSBody annotation

The easiest way to invoke JavaScript code from Java is to define native method marked with the JSBody annotation that contains the JavaScript code.


     @JSBody(params = { "message" }, script = "window.alert(message);")
    public static native void alert(String message);

Overlay types

Often you need to pass complex values between Java and JavaScript. Primitives are usually insufficient for this purposed. JSO comes with concept of overlay types, that are usable from both Java and JavaScript. For detailed description, see JSObject interface.

When wrapping JavaScript APIs in Java classes, you usually write boilerplate JSBody like this:

  @JSBody(params = "newChild", script = "return this.appendChild(newChild);")
 Node appendChild(Node newChild);

JSO offers shortcut annotations that help to avoid such boilerplate. They are: JSMethod, JSProperty, JSIndexer.

Skip navigation links

Copyright © 2017. All rights reserved.