diff --git a/engine/runtime-integration-tests/src/test/java/org/enso/interpreter/runtime/ModuleTest.java b/engine/runtime-integration-tests/src/test/java/org/enso/interpreter/runtime/ModuleTest.java index c37652ab4795..9cf19f466d3c 100644 --- a/engine/runtime-integration-tests/src/test/java/org/enso/interpreter/runtime/ModuleTest.java +++ b/engine/runtime-integration-tests/src/test/java/org/enso/interpreter/runtime/ModuleTest.java @@ -1,9 +1,12 @@ package org.enso.interpreter.runtime; import static org.enso.scala.wrapper.ScalaConversions.nil; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.containsString; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; import java.io.File; import java.io.IOException; @@ -17,6 +20,7 @@ import org.enso.pkg.QualifiedName; import org.graalvm.polyglot.Context; import org.graalvm.polyglot.Engine; +import org.graalvm.polyglot.PolyglotException; import org.graalvm.polyglot.Source; import org.graalvm.polyglot.io.IOAccess; import org.junit.After; @@ -52,6 +56,17 @@ public void cleanup() { this.ctx = null; } + @Test + public void noSuchModuleError() { + var b = ctx.getBindings(LanguageInfo.ID); + try { + var r = b.invokeMember(MethodNames.TopScope.GET_MODULE, "Does.Not.Exist.Module"); + fail("Expecting failure, but got: " + r); + } catch (PolyglotException ex) { + assertThat(ex.getMessage(), containsString("Module_Does_Not_Exist")); + } + } + @Test public void moduleKeepsFileRefAfterSourceUnset() { var name = QualifiedName.simpleName("local.Unnamed_1"); diff --git a/engine/runtime/src/main/java/org/enso/interpreter/runtime/EnsoContext.java b/engine/runtime/src/main/java/org/enso/interpreter/runtime/EnsoContext.java index 531a116cc317..c3f3bebd0f44 100644 --- a/engine/runtime/src/main/java/org/enso/interpreter/runtime/EnsoContext.java +++ b/engine/runtime/src/main/java/org/enso/interpreter/runtime/EnsoContext.java @@ -272,8 +272,8 @@ private static void reportSlowContextAccess(Node n) { with root nodes: {r} """ .replace("{n}", "" + n) - .replace("{s}", "" + n.getEncapsulatingSourceSection()) - .replace("{r}", "" + n.getRootNode())); + .replace("{s}", "" + (n != null ? n.getEncapsulatingSourceSection() : null)) + .replace("{r}", "" + (n != null ? n.getRootNode() : null))); ex.printStackTrace(); checkUntil = System.currentTimeMillis() + 10000; var assertsOn = false; diff --git a/engine/runtime/src/main/java/org/enso/interpreter/runtime/scope/TopLevelScope.java b/engine/runtime/src/main/java/org/enso/interpreter/runtime/scope/TopLevelScope.java index 79452f8043f8..d48cacddf02b 100644 --- a/engine/runtime/src/main/java/org/enso/interpreter/runtime/scope/TopLevelScope.java +++ b/engine/runtime/src/main/java/org/enso/interpreter/runtime/scope/TopLevelScope.java @@ -2,6 +2,7 @@ import com.oracle.truffle.api.CompilerDirectives; import com.oracle.truffle.api.TruffleFile; +import com.oracle.truffle.api.dsl.Bind; import com.oracle.truffle.api.dsl.Specialization; import com.oracle.truffle.api.interop.ArityException; import com.oracle.truffle.api.interop.InteropLibrary; @@ -10,6 +11,7 @@ import com.oracle.truffle.api.interop.UnsupportedTypeException; import com.oracle.truffle.api.library.ExportLibrary; import com.oracle.truffle.api.library.ExportMessage; +import com.oracle.truffle.api.nodes.Node; import java.io.File; import java.util.Collection; import java.util.Optional; @@ -22,6 +24,7 @@ import org.enso.interpreter.runtime.builtin.Builtins; import org.enso.interpreter.runtime.data.EnsoObject; import org.enso.interpreter.runtime.data.vector.ArrayLikeHelpers; +import org.enso.interpreter.runtime.error.PanicException; import org.enso.interpreter.runtime.type.Types; import org.enso.pkg.Package; import org.enso.pkg.QualifiedName; @@ -130,7 +133,8 @@ private static Module getModule(TopLevelScope scope, Object[] arguments) var module = scope.getModule(moduleName); if (module.isEmpty()) { - throw UnknownIdentifierException.create(moduleName); + throw new PanicException( + scope.builtins.error().makeModuleDoesNotExistError(moduleName), null); } return module.get(); @@ -194,21 +198,23 @@ private static Object compile(Object[] arguments, EnsoContext context) } @Specialization - static Object doInvoke(TopLevelScope scope, String member, Object[] arguments) + static Object doInvoke( + TopLevelScope scope, String member, Object[] arguments, @Bind("$node") Node node) throws UnknownIdentifierException, ArityException, UnsupportedTypeException { + var ctx = EnsoContext.get(node); switch (member) { case MethodNames.TopScope.GET_MODULE: return getModule(scope, arguments); case MethodNames.TopScope.CREATE_MODULE: - return createModule(scope, arguments, EnsoContext.get(null)); + return createModule(scope, arguments, ctx); case MethodNames.TopScope.REGISTER_MODULE: - return registerModule(scope, arguments, EnsoContext.get(null)); + return registerModule(scope, arguments, ctx); case MethodNames.TopScope.UNREGISTER_MODULE: - return unregisterModule(scope, arguments, EnsoContext.get(null)); + return unregisterModule(scope, arguments, ctx); case MethodNames.TopScope.LEAK_CONTEXT: - return leakContext(EnsoContext.get(null)); + return leakContext(ctx); case MethodNames.TopScope.COMPILE: - return compile(arguments, EnsoContext.get(null)); + return compile(arguments, ctx); default: throw UnknownIdentifierException.create(member); }