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

Use enso.dev.insight property to turn Insight on #11385

Merged
merged 6 commits into from
Oct 24, 2024
Merged
24 changes: 15 additions & 9 deletions engine/common/src/main/java/org/enso/common/ContextFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public final class ContextFactory {
private OutputStream out = System.out;
private OutputStream err = System.err;
private MessageTransport messageTransport;
private Level logLevel;
private Level logLevel = Level.INFO;
private boolean logMasking;
private boolean enableIrCaches;
private boolean disablePrivateCheck;
Expand Down Expand Up @@ -166,7 +166,6 @@ public Context build() {
.allowExperimentalOptions(true)
.allowAllAccess(true)
.allowHostAccess(allWithTypeMapping())
.option(RuntimeOptions.PROJECT_ROOT, projectRoot)
.option(RuntimeOptions.STRICT_ERRORS, Boolean.toString(strictErrors))
.option(RuntimeOptions.DISABLE_LINTING, Boolean.toString(disableLinting))
.option(RuntimeOptions.WAIT_FOR_PENDING_SERIALIZATION_JOBS, "true")
Expand All @@ -183,6 +182,7 @@ public Context build() {
.out(out)
.err(err)
.in(in);

if (checkForWarnings != null) {
builder.option(DebugServerInfo.METHOD_BREAKPOINT_OPTION, checkForWarnings);
}
Expand All @@ -206,12 +206,15 @@ public Context build() {
}
builder.logHandler(logHandler);

var graalpy =
new File(
new File(new File(new File(new File(projectRoot), "polyglot"), "python"), "bin"),
"graalpy");
if (graalpy.exists()) {
builder.option("python.Executable", graalpy.getAbsolutePath());
if (projectRoot != null) {
builder.option(RuntimeOptions.PROJECT_ROOT, projectRoot);
var graalpy =
new File(
new File(new File(new File(new File(projectRoot), "polyglot"), "python"), "bin"),
"graalpy");
if (graalpy.exists()) {
builder.option("python.Executable", graalpy.getAbsolutePath());
}
}
if (ENGINE_HAS_JAVA) {
var javaHome = System.getProperty("java.home");
Expand All @@ -224,7 +227,10 @@ public Context build() {
.option("java.UseBindingsLoader", "true")
.allowCreateThread(true);
}
return builder.build();

var ctx = builder.build();
ContextInsightSetup.configureContext(ctx);
return ctx;
}

/**
Expand Down
106 changes: 106 additions & 0 deletions engine/common/src/main/java/org/enso/common/ContextInsightSetup.java
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)
Copy link
Member

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?

Copy link
Member Author

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.

Copy link
Member Author

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 the foreign 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.

.build();

var instrument = ctx.getEngine().getInstruments().get("insight");
@SuppressWarnings("unchecked")
var insight = (Function<Source, AutoCloseable>) instrument.lookup(Function.class);
insightHandle = insight.apply(insightSrc);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could detect changes to the insightFile via filesystem watcher and re-initialize the Insight script automatically. Just:

  • insightHandle.close() to disable the currently active insights
  • load the new content
  • insight.apply(theNewSrc)

that way the speed of development would be closer to JavaScript development experience.

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,9 @@ class MainModule(serverConfig: LanguageServerConfig, logLevel: Level) {
connection
} else null
})
if (System.getProperty("enso.dev.insight") != null) {
stdOut.attach(arr => System.out.write(arr))
}

system.eventStream.setLogLevel(AkkaConverter.toAkka(logLevel))
log.trace("Set akka log level to [{}]", logLevel)
Expand Down
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")));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ private int splitAt(CharSequence seq, char ch) {
}
at++;
}
throw new ForeignParsingException("No " + ch + " found", this);
throw new ForeignParsingException("No `" + ch + "` found", this);
}

@Override
Expand Down Expand Up @@ -106,12 +106,17 @@ yield switch (id) {
private ForeignFunctionCallNode parseJs() {
var context = EpbContext.get(this);
var inner = context.getInnerContext();
var code = foreignSource(langAndCode);
var args = Arrays.stream(argNames).skip(1).collect(Collectors.joining(","));
var wrappedSrc = "var poly_enso_eval=function(" + args + "){" + code + "\n};poly_enso_eval";
Source source = newSource("js", wrappedSrc);
var fn = inner.evalPublic(this, source);
return JsForeignNode.build(fn);
if (inner != null) {
var code = foreignSource(langAndCode);
var args = Arrays.stream(argNames).skip(1).collect(Collectors.joining(","));
var wrappedSrc = "var poly_enso_eval=function(" + args + "){" + code + "\n};poly_enso_eval";
Source source = newSource("js", wrappedSrc);
var fn = inner.evalPublic(this, source);
return JsForeignNode.build(fn);
} else {
return new GenericForeignNode(
RootNode.createConstantNode("Cannot evaluate script in inner context!").getCallTarget());
}
}

private ForeignFunctionCallNode parseGeneric(
Expand Down
Loading