-
Notifications
You must be signed in to change notification settings - Fork 323
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use
enso.dev.insight
property to turn Insight on (#11385)
- Loading branch information
1 parent
3515976
commit fe45da9
Showing
8 changed files
with
208 additions
and
22 deletions.
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
102 changes: 102 additions & 0 deletions
102
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,102 @@ | ||
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> | ||
* 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); | ||
} | ||
} |
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
57 changes: 57 additions & 0 deletions
57
...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,57 @@ | ||
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( | ||
""" | ||
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
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