Skip to content
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

GraalVM Polyglot JS #1353

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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");
}
}
Original file line number Diff line number Diff line change
@@ -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");
}
}
10 changes: 10 additions & 0 deletions buildSrc/src/main/resources/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.graalvm.polyglot</groupId>
<artifactId>polyglot</artifactId>
<version>23.1.0</version>
</dependency>
<dependency>
<groupId>org.graalvm.polyglot</groupId>
<artifactId>js</artifactId>
<version>23.1.0</version>
sdelamo marked this conversation as resolved.
Show resolved Hide resolved
</dependency>
<dependency>
<groupId>org.mock-server</groupId>
<artifactId>mockserver-client-java</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String> 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();
}
}
Original file line number Diff line number Diff line change
@@ -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));
}
}
15 changes: 15 additions & 0 deletions guides/micronaut-graalvm-polyglot-js/metadata.json
Original file line number Diff line number Diff line change
@@ -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"]
}
]
}
Empty file.
Loading