-
Notifications
You must be signed in to change notification settings - Fork 327
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
Use enso.dev.insight
property to turn Insight on
#11385
Merged
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1a02b7c
Specify -Denso.dev.insight=insightScript.js property to enable GraalV…
JaroslavTulach 1cd842b
Sent JavaScript print to stdout when enso.dev.insight property is spe…
JaroslavTulach b3259ff
Make the error messages more verbose
JaroslavTulach f50329a
Don't throw away the first argument. That means insight variable is d…
JaroslavTulach 954e628
No need for this prelude. insight variable is always defined.
JaroslavTulach a3938bf
Merge remote-tracking branch 'origin/develop' into wip/jtulach/EnsoIn…
JaroslavTulach 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
106 changes: 106 additions & 0 deletions
106
engine/common/src/main/java/org/enso/common/ContextInsightSetup.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,106 @@ | ||
package org.enso.common; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.function.Function; | ||
import org.graalvm.polyglot.Context; | ||
import org.graalvm.polyglot.Source; | ||
|
||
/** | ||
* Development support for running <a | ||
* href="https://www.graalvm.org/latest/tools/graalvm-insight/">GraalVM Insight</a> scripts in the | ||
* Enso execution enviroments. Works both - in the CLI as well as in IDE. To use specify JVM | ||
* property {@link #INSIGHT_PROP} when executing the CLI: | ||
* | ||
* <pre> | ||
* enso --vm.D=enso.dev.insight=insightScript.js | ||
* </pre> | ||
* | ||
* or when launching the {@code project-manager}: | ||
* | ||
* <pre> | ||
* ENSO_JVM_OPTS=-Denso.dev.insight=`pwd`/insightScript.js project-manager | ||
* </pre> | ||
* | ||
* The sample {@code insightScript.js} can look for example like: | ||
* | ||
* <pre> | ||
* if (typeof insight == 'undefined') { | ||
* insight = this; | ||
* } | ||
* | ||
* print("Initializing Insight: " + insight); | ||
* insight.on("enter", function(ctx) { | ||
* print("Calling " + ctx.name); | ||
* }, { | ||
* roots: true | ||
* }); | ||
* </pre> | ||
* | ||
* More information about Insight scripts can be found in the <a | ||
* href="https://www.graalvm.org/latest/tools/graalvm-insight/manual/">Insight manual</a> and <a | ||
* href="https://www.graalvm.org/tools/javadoc/org/graalvm/tools/insight/Insight.html">programatic | ||
* documentation</a>. | ||
*/ | ||
final class ContextInsightSetup { | ||
private static final String INSIGHT_PROP = "enso.dev.insight"; | ||
private static ContextInsightSetup ACTIVE; | ||
|
||
private final Context ctx; | ||
private final Path insightFile; | ||
private AutoCloseable insightHandle; | ||
|
||
private ContextInsightSetup(Context ctx, Path file) { | ||
this.ctx = ctx; | ||
this.insightFile = file; | ||
} | ||
|
||
/** | ||
* Configures the context if {@link #INSIGHT_PROP} property is specified. This support is | ||
* <em>development only</em>. It can be (and will be) removed in the future. | ||
* | ||
* @param ctx context to configure | ||
* @throws AssertionError throws assertion error if the property is specified, but something goes | ||
* wrong | ||
*/ | ||
@SuppressWarnings("CallToPrintStackTrace") | ||
static void configureContext(Context ctx) throws AssertionError { | ||
var insightProp = System.getProperty(INSIGHT_PROP); | ||
if (insightProp != null) { | ||
var insightFile = new File(insightProp); | ||
try { | ||
insightFile = insightFile.getCanonicalFile(); | ||
assert insightFile.isFile() | ||
: "Cannot find " + insightFile + " specified via " + INSIGHT_PROP + " property"; | ||
ACTIVE = new ContextInsightSetup(ctx, insightFile.toPath()); | ||
ACTIVE.initialize(); | ||
} catch (Error | Exception ex) { | ||
var ae = | ||
new AssertionError( | ||
"Cannot initialize " + insightFile + " specified via " + INSIGHT_PROP + " property", | ||
ex); | ||
ae.printStackTrace(); | ||
throw ae; | ||
} | ||
} | ||
} | ||
|
||
private void initialize() throws IOException { | ||
var insightCode = Files.readString(insightFile); | ||
var language = "js"; | ||
if (insightFile.getFileName().toString().endsWith(".py")) { | ||
language = "python"; | ||
} | ||
var insightSrc = | ||
Source.newBuilder("epb", insightFile.toFile()) | ||
.content(language + ":0#" + insightCode) | ||
.build(); | ||
|
||
var instrument = ctx.getEngine().getInstruments().get("insight"); | ||
@SuppressWarnings("unchecked") | ||
var insight = (Function<Source, AutoCloseable>) instrument.lookup(Function.class); | ||
insightHandle = insight.apply(insightSrc); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could detect changes to the
that way the speed of development would be closer to JavaScript development experience. |
||
} | ||
} |
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
60 changes: 60 additions & 0 deletions
60
...runtime-integration-tests/src/test/java/org/enso/common/test/ContextInsightSetupTest.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,60 @@ | ||
package org.enso.common.test; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.containsString; | ||
import static org.junit.Assert.assertEquals; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.File; | ||
import java.io.FileWriter; | ||
import org.enso.common.ContextFactory; | ||
import org.enso.test.utils.ContextUtils; | ||
import org.hamcrest.core.AllOf; | ||
import org.junit.AfterClass; | ||
import org.junit.Test; | ||
|
||
/** | ||
* Demonstrates usage of {@code -Denso.dev.insight=insightScript.js} property. This is a | ||
* developement only support for playing with GraalVM Insight scripts inside of the IDE as well in | ||
* CLI. | ||
*/ | ||
public class ContextInsightSetupTest { | ||
|
||
public ContextInsightSetupTest() {} | ||
|
||
@AfterClass | ||
public static void cleanupInsightProperty() { | ||
System.getProperties().remove("enso.dev.insight"); | ||
} | ||
|
||
@Test | ||
public void initializeInsightViaProperty() throws Exception { | ||
var insight = File.createTempFile("insight", ".js"); | ||
try (java.io.FileWriter w = new FileWriter(insight)) { | ||
w.write( | ||
""" | ||
if (typeof insight === 'undefined') { | ||
insight = this; | ||
} | ||
print("Insight started. Properties: " + Object.getOwnPropertyNames(insight).sort()); | ||
"""); | ||
} | ||
|
||
System.setProperty("enso.dev.insight", insight.getPath()); | ||
|
||
var out = new ByteArrayOutputStream(); | ||
try (var ctx = ContextFactory.create().out(out).build()) { | ||
|
||
var fourtyTwo = ContextUtils.evalModule(ctx, """ | ||
main = 42 | ||
"""); | ||
|
||
assertEquals("42", fourtyTwo.toString()); | ||
|
||
assertThat( | ||
out.toString(), | ||
AllOf.allOf( | ||
containsString("Insight started."), containsString("Properties: id,version"))); | ||
} | ||
} | ||
} |
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.
Where is this cryptic
:0#
string constant comming from? What does it mean?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.
epb
is just an internal language. As such the documentation is provided on the level of tests https://github.com/enso-org/enso/pull/11385/files#diff-208ccc44a38434f59f03b9e2f77d891939bcd11de260a8a9c00c2e652fa3df5dR25 and sibling source code only.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.
The support is primarily for
foreign
functions. Language is language. The number is the line number where the script is located in the original file. The text behind#
is the code of theforeign
function.We are re-using
epb
as it has special support for invoking JavaScript in its own inner context to workaround the single threaded limitation.