generated from micronaut-projects/micronaut-project-template
-
Notifications
You must be signed in to change notification settings - Fork 1
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
feat: module for Polyglot Context injection #15
Open
sdelamo
wants to merge
1
commit into
1.0.x
Choose a base branch
from
context-factory
base: 1.0.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
plugins { | ||
id("io.micronaut.build.internal.graal-languages-module") | ||
} | ||
dependencies { | ||
api(libs.managed.graal.polyglot) | ||
implementation(mn.micronaut.context) | ||
testRuntimeOnly(libs.managed.graal.js) | ||
testImplementation(mn.micronaut.http.server.netty) | ||
testImplementation(mn.micronaut.jackson.databind) | ||
testImplementation(mn.micronaut.http.client) | ||
testAnnotationProcessor(mn.micronaut.inject.java) | ||
testImplementation(mnTest.micronaut.test.junit5) | ||
testRuntimeOnly(libs.junit.jupiter.engine) | ||
} | ||
tasks.withType<Test> { | ||
useJUnitPlatform() | ||
} | ||
micronautBuild { | ||
binaryCompatibility { | ||
enabled.set(false) | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
graal-languages-core/src/main/java/io/micronaut/graallanguages/ContextFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Copyright 2017-2024 original authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.micronaut.graallanguages; | ||
|
||
import io.micronaut.context.annotation.Factory; | ||
import io.micronaut.core.annotation.Internal; | ||
import io.micronaut.runtime.context.scope.ThreadLocal; | ||
import jakarta.inject.Singleton; | ||
import org.graalvm.polyglot.Context; | ||
|
||
@Internal | ||
@Factory | ||
class ContextFactory { | ||
@Singleton | ||
Context.Builder createContextBuilder() { | ||
return Context.newBuilder(); | ||
} | ||
|
||
@ThreadLocal | ||
ContextProvider createContext(Context.Builder builder) { | ||
ContextProvider contextProvider = new ContextProvider(); | ||
contextProvider.setContext(builder.build()); | ||
return contextProvider; | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
graal-languages-core/src/main/java/io/micronaut/graallanguages/ContextProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Copyright 2017-2024 original authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.micronaut.graallanguages; | ||
|
||
import io.micronaut.core.annotation.Experimental; | ||
import org.graalvm.polyglot.Context; | ||
|
||
/** | ||
* Wrapper around {@link Context} since it is a final class and cannot be proxied after its instantiation in a factory. | ||
* @author Sergio del Amo | ||
* @since 1.1.0 | ||
*/ | ||
@Experimental | ||
public class ContextProvider { | ||
private Context context; | ||
|
||
/** | ||
* Empty Constructor. | ||
*/ | ||
public ContextProvider() { | ||
} | ||
|
||
/** | ||
* | ||
* @param context Graal Languages {@link Context} | ||
*/ | ||
public void setContext(Context context) { | ||
this.context = context; | ||
} | ||
|
||
/** | ||
* | ||
* @return Graal Languages {@link Context} | ||
*/ | ||
public Context getContext() { | ||
return context; | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
graal-languages-core/src/test/java/io/micronaut/graallanguages/core/HomeController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package io.micronaut.graallanguages.core; | ||
|
||
import io.micronaut.context.annotation.Property; | ||
import io.micronaut.context.annotation.Requires; | ||
import io.micronaut.graallanguages.ContextProvider; | ||
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.Value; | ||
|
||
@Requires(property = "spec.name", value = "HomeControllerTest") | ||
//tag::controller[] | ||
@Controller | ||
public class HomeController { | ||
private final ContextProvider contextProvider; | ||
|
||
HomeController(ContextProvider contextProvider) { | ||
this.contextProvider = contextProvider; | ||
} | ||
|
||
@Produces(MediaType.TEXT_PLAIN) | ||
@Get | ||
HttpResponse<String> index() { | ||
Value f = contextProvider.getContext().eval("js", "x => x + 1"); | ||
if (f.canExecute()) { | ||
return HttpResponse.ok("" + f.execute(41).asInt()); | ||
} | ||
return HttpResponse.serverError(); | ||
} | ||
} | ||
//end::controller[] |
24 changes: 24 additions & 0 deletions
24
graal-languages-core/src/test/java/io/micronaut/graallanguages/core/HomeControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package io.micronaut.graallanguages.core; | ||
|
||
import io.micronaut.context.annotation.Property; | ||
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; | ||
|
||
@Property(name = "spec.name", value = "HomeControllerTest") | ||
@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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
You can embed https://www.graalvm.org/latest/reference-manual/languages/[Graal Languages] in your application. For example, to embed Javascript you could add the following dependencies: | ||
|
||
dependency:io.micronaut.micronaut-graal-languages:micronaut-graal-languages-core[scope=compile] | ||
|
||
dependency:org.graalvm.js:js[scope=runtime] | ||
|
||
[source,java] | ||
.src/main/java/example/micronaut/HomeController.java | ||
---- | ||
include::{sourceDir}/graal-languages-core/src/test/java/io/micronaut/graallanguages/core/HomeController.java[tag=controller, indent=0] | ||
---- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to the comments on the PR, contexts should be pooled.
The best would be to have something like:
With the implementation:
WDYT @steve-s @mikehearn ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks nice. There is still one issue: you can get Java objects that wrap JavaScript/Python/... objects that are associated with given context. Example:
thinking about this, I am not sure there can ever be some "compile-time safety net" for this, users will have to understand the constraints and make sure they do not hold onto
Value
instances from theContext
outside of the lambda.@mikehearn used try-with-resources for his
Context
pool (but that doesn't solve this issue either).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Techically we can check what type is being returned from the method, but that would have to be an implementation specific check
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Side note: it is even trickier with the proxy feature: