diff --git a/buildSrc/src/main/java/io/micronaut/guides/feature/GraalvmPolyglot.java b/buildSrc/src/main/java/io/micronaut/guides/feature/GraalvmPolyglot.java new file mode 100644 index 0000000000..827f4c5478 --- /dev/null +++ b/buildSrc/src/main/java/io/micronaut/guides/feature/GraalvmPolyglot.java @@ -0,0 +1,10 @@ +package io.micronaut.guides.feature; + +import jakarta.inject.Singleton; + +@Singleton +public class GraalvmPolyglot extends AbstractFeature { + protected GraalvmPolyglot() { + super("graalvm-polyglot", "polyglot"); + } +} diff --git a/buildSrc/src/main/java/io/micronaut/guides/feature/GraalvmPolyglotJs.java b/buildSrc/src/main/java/io/micronaut/guides/feature/GraalvmPolyglotJs.java new file mode 100644 index 0000000000..a0c876ed65 --- /dev/null +++ b/buildSrc/src/main/java/io/micronaut/guides/feature/GraalvmPolyglotJs.java @@ -0,0 +1,10 @@ +package io.micronaut.guides.feature; + +import jakarta.inject.Singleton; + +@Singleton +public class GraalvmPolyglotJs extends AbstractFeature { + protected GraalvmPolyglotJs() { + super("graalvm-polyglot-js", "js"); + } +} diff --git a/buildSrc/src/main/resources/pom.xml b/buildSrc/src/main/resources/pom.xml index 07b1c67129..d30f98779d 100644 --- a/buildSrc/src/main/resources/pom.xml +++ b/buildSrc/src/main/resources/pom.xml @@ -12,6 +12,16 @@ + + org.graalvm.polyglot + polyglot + 23.1.0 + + + org.graalvm.polyglot + js + 23.1.0 + org.mock-server mockserver-client-java diff --git a/guides/micronaut-graalvm-polyglot-js/java/src/main/java/example/micronaut/HomeController.java b/guides/micronaut-graalvm-polyglot-js/java/src/main/java/example/micronaut/HomeController.java new file mode 100644 index 0000000000..1e47fd0ba5 --- /dev/null +++ b/guides/micronaut-graalvm-polyglot-js/java/src/main/java/example/micronaut/HomeController.java @@ -0,0 +1,25 @@ +package com.example; + +import io.micronaut.http.HttpResponse; +import io.micronaut.http.MediaType; +import io.micronaut.http.annotation.Controller; +import io.micronaut.http.annotation.Get; +import io.micronaut.http.annotation.Produces; +import org.graalvm.polyglot.Context; +import org.graalvm.polyglot.Value; + +@Controller +public class HomeController { + + @Produces(MediaType.TEXT_PLAIN) + @Get + HttpResponse index() { + try (Context context = Context.create()) { + Value f = context.eval("js", "x => x + 1"); + if (f.canExecute()) { + return HttpResponse.ok("" + f.execute(41).asInt()); + } + } + return HttpResponse.serverError(); + } +} diff --git a/guides/micronaut-graalvm-polyglot-js/java/src/test/java/example/micronaut/HomeControllerTest.java b/guides/micronaut-graalvm-polyglot-js/java/src/test/java/example/micronaut/HomeControllerTest.java new file mode 100644 index 0000000000..f20944bf56 --- /dev/null +++ b/guides/micronaut-graalvm-polyglot-js/java/src/test/java/example/micronaut/HomeControllerTest.java @@ -0,0 +1,22 @@ +package com.example; + +import io.micronaut.http.HttpRequest; +import io.micronaut.http.MediaType; +import io.micronaut.http.client.BlockingHttpClient; +import io.micronaut.http.client.HttpClient; +import io.micronaut.http.client.annotation.Client; +import io.micronaut.test.extensions.junit5.annotation.MicronautTest; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +@MicronautTest +class HomeControllerTest { + + @Test + void jsembedding(@Client("/") HttpClient httpClient) { + BlockingHttpClient client = httpClient.toBlocking(); + HttpRequest request = HttpRequest.GET("/").accept(MediaType.TEXT_PLAIN_TYPE); + assertEquals("42", client.retrieve(request)); + } +} diff --git a/guides/micronaut-graalvm-polyglot-js/metadata.json b/guides/micronaut-graalvm-polyglot-js/metadata.json new file mode 100644 index 0000000000..5f18abe47e --- /dev/null +++ b/guides/micronaut-graalvm-polyglot-js/metadata.json @@ -0,0 +1,15 @@ +{ + "title": "GraalVM Polyglot JS", + "intro": "GraalVM Polyglot JS.", + "authors": ["Sergio del Amo"], + "categories": ["GraalVM"], + "publicationDate": "2023-09-22", + "languages": ["java"], + "buildTools": ["gradle"], + "apps": [ + { + "name": "default", + "features": ["graalvm-polyglot", "graalvm-polyglot-js"] + } + ] +} \ No newline at end of file diff --git a/guides/micronaut-graalvm-polyglot-js/micronaut-graalvm-polyglot-js.adoc b/guides/micronaut-graalvm-polyglot-js/micronaut-graalvm-polyglot-js.adoc new file mode 100644 index 0000000000..e69de29bb2