Improvements to native JavaScript interaction
Exporting declarations from Java to JavaScript
This long expected feature is finally delivered by this release. Now TeaVM can not only take standard Java main classes with main methods, but also module classes. Instead of main
method, they can declare static
methods annotated with @JSExport
.
These methods will be exported from generated TeaVM module (or rather as top-level declarations in non-module output).
Additionally, @JSExport
annotation can be used to mark methods of classes that should be available in JS. See full documentation in Creating JavaScript modules section.
Generating modules
From this release, TeaVM can produce ES2015 modules. Moreover, there's a new build parameter that allows to specify module type (CommonJS, ES2015, UMD, immediately-invoked function).
Importing constructors
Previously, to declare a constructor for a JS class, you had to define static factory method, annotated with @JSBody
and some JS within. Now, it's possible to declare a non-abstract overlay class (should be additionally annotated with @JSClass
) with constructors. To instantiate such classes, you can use new
syntax, as you used for normal Java classes.
Importing static and top-level methods
Since this release, you can define static native
methods of overlay classes without extra annotations. These methods will be automatically mapped to corresponding static JS methods.
You can also put @JSTopLevel
annotation on such methods so that they aren't mapped to static methods of containing classes, but to top-level methods.
Support instanceof
Overlay classes now properly support instanceof
and casts (in strict mode). Note that instanceof
is not available for interfaces (i.e. as previously, always produces true
). Support of instanceof
can be disabled for classes by applying @JSClass(transparent = true)
.
Importing declarations from external modules
You can also use @JSModule
annotation to import declarations from JS modules. Following cases possible:
@JSModule
on class – imports class@JSModule
and@JSTopLevel
– imports function@JSModule
,@JSTopLevel
and@JSProperty
– imports property
Support varargs
Varargs methods of overlay classes now automatically mapped to varargs methods in JS.
Improved string representation
This release has changed the way java.lang.String
represented internally. Instead of array of char
, java.lang.String
now contains field of native JS string. This can improve performance in some cases, most likely in calls to native JS methods. Previously, to convert java.lang.String
to and from JS method, a full copy should have been made. Now, TeaVM only wraps/unwraps JS string with java.lang.String
implementation, which is much faster.
This also improves debugging experience. Previously, if you open a java.lang.String
value in browser dev tools, you had a object, which has a field, which is an object in turn, which wraps UInt16Array
. Now, you only have to deal with one level of indirection (object that has a field), and an actual representation is not array of char codes, but JS string, easily readable in dev tools.
ES2015 modules
Since this release, TeaVM generates ES2015 output, not ES5. This allows not only to support ES2015 modules, but reduces output side thanks to arrow function syntax.
Class library emulation
String.toLowerCase
/toUpperCase
methods that takeLocale
parameterchars
andcodePoints
methods inCharSequence
String.format
/Formatter.format
now support more format specifiersMath
methods for arithmetic operations that throw exception on overflowOutputStream.nullOutputStream
SecureRandom
BigInteger.sqrt
WeakHashMap
CopyOnWriteArrayList
- Fair support of
ThreadLocal
in green threads - Support
@Inherited
annotations PrintStream
implementsAppendable
BufferedReader.lines
method
Gradle plugin improvements
- Improve source file resolution when generating source maps in Gradle
- Add tasks to start/update and stop development server
Other stuff
- Overlay class for JS
Promise
(JSPromise
class) - Support
@Inherited
annotations - New convention for event registration in standard overlay classes (onEventName returning
Registration
to unbind the listener, instead of listenEventName/neglectEventName).
Sponsorship
Worth mentioning that TeaVM evolved with the support from its sponsors. If you like this project, you can start donating.
Special thanks to @reportmill and @shannah, our permanent sponsors!