Custom functionality can be added with Javascript to APIs with this plugin. Small scripts can be very powerful, e.g. you can:
As an alternative to the Javascript Plugin you can use the Groovy plugin.
See the examples/javascript folder for some samples.
The plugin comes with the Rhino Javascript engine. To use the newest Javascript features the engine can be replaced with the GraalVM Javascript engine. Membrane is tested with GraalVM 24.1.2. Other versions may work but are untested.
The GraalVM Javascript engine consists of several JAR files. The easiest way to get all of them is to let Maven download them:
pom.xml in an empty folder:<project xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<groupId>local</groupId>
<artifactId>graaljs-for-membrane</artifactId>
<version>1</version>
<packaging>pom</packaging>
<properties>
<graalvm.version>24.1.2</graalvm.version>
</properties>
<dependencies>
<dependency>
<groupId>org.graalvm.polyglot</groupId>
<artifactId>polyglot</artifactId>
<version>${graalvm.version}</version>
</dependency>
<dependency>
<groupId>org.graalvm.polyglot</groupId>
<artifactId>js-community</artifactId>
<version>${graalvm.version}</version>
<type>pom</type>
</dependency>
</dependencies>
</project>
<project xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<groupId>local</groupId>
<artifactId>graaljs-for-membrane</artifactId>
<version>1</version>
<packaging>pom</packaging>
<properties>
<graalvm.version>24.1.2</graalvm.version>
</properties>
<dependencies>
<dependency>
<groupId>org.graalvm.polyglot</groupId>
<artifactId>polyglot</artifactId>
<version>${graalvm.version}</version>
</dependency>
<dependency>
<groupId>org.graalvm.polyglot</groupId>
<artifactId>js-community</artifactId>
<version>${graalvm.version}</version>
<type>pom</type>
</dependency>
</dependencies>
</project>
graaljs subfolder:mvn dependency:copy-dependencies -DoutputDirectory=graaljs -DexcludeTypes=pommvn dependency:copy-dependencies -DoutputDirectory=graaljs -DexcludeTypes=pom
graaljs into the MEMBRANE_HOME/lib folder.Use the js-community artifact as shown above. The org.graalvm.js:js artifact also pulls in truffle-enterprise, which is licensed under Oracle's GraalVM Free Terms and Conditions (GFTC).
At startup Membrane logs Found GraalVM Javascript engine. if the GraalVM engine is used. If the log says Found Rhino Javascript engine. instead, the GraalVM JARs are not on the classpath.
On a JDK without JVMCI enabled, GraalVM runs scripts in interpreter mode and logs a warning about the fallback runtime. Scripts still work, only slower. To turn the warning off, start Membrane with the Java option -Dpolyglot.engine.WarnInterpreterOnly=false, e.g. by setting the JAVA_OPTS environment variable.
{a: 1} or the result of JSON.parse(), can't be stored on the exchange and used later. Store them as strings with JSON.stringify() instead.json variable is a Java map. With Rhino it is a Javascript object.