Skip to content

Commit 243847c

Browse files
committed
Permissions in ExecutionEnvironment is a final record
1 parent 1e58228 commit 243847c

File tree

3 files changed

+49
-57
lines changed

3 files changed

+49
-57
lines changed

engine/runtime/src/main/java/org/enso/interpreter/node/expression/builtin/runtime/ContextIsEnabledNode.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,6 @@ Object execute(Atom self, Object environmentName) {
2626
.makeUnimplemented("execution environment mismatch");
2727
throw new PanicException(error, this);
2828
}
29-
return currentEnv.hasContextEnabled(self.getConstructor().getName());
29+
return currentEnv.hasContextEnabled(self.getConstructor(), EnsoContext.get(this));
3030
}
3131
}

engine/runtime/src/main/java/org/enso/interpreter/runtime/error/DataflowError.java

+7-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import static org.enso.interpreter.runtime.error.PanicException.handleExceptionMessage;
44

5+
import com.oracle.truffle.api.CompilerAsserts;
56
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
67
import com.oracle.truffle.api.TruffleStackTrace;
78
import com.oracle.truffle.api.TruffleStackTraceElement;
@@ -17,7 +18,6 @@
1718
import com.oracle.truffle.api.nodes.Node;
1819
import java.util.Objects;
1920
import org.enso.interpreter.node.callable.IndirectInvokeMethodNode;
20-
import org.enso.interpreter.node.expression.builtin.runtime.Context;
2121
import org.enso.interpreter.node.expression.builtin.text.util.TypeToDisplayTextNode;
2222
import org.enso.interpreter.runtime.EnsoContext;
2323
import org.enso.interpreter.runtime.callable.UnresolvedSymbol;
@@ -54,11 +54,14 @@ public final class DataflowError extends AbstractTruffleException implements Ens
5454
*/
5555
public static DataflowError withDefaultTrace(State state, Object payload, Node location) {
5656
assert payload != null;
57+
var ensoCtx = EnsoContext.get(location);
58+
var dataflowStacktraceCtx = ensoCtx.getBuiltins().context().getDataflowStackTrace();
59+
// dataflowStacktraceCtx is a compiler constant, because it is a constructor of a builtin
60+
// type.
61+
CompilerAsserts.partialEvaluationConstant(dataflowStacktraceCtx);
5762
boolean attachFullStackTrace =
5863
state == null
59-
|| EnsoContext.get(location)
60-
.getExecutionEnvironment()
61-
.hasContextEnabled(Context.DATAFLOW_STACK_TRACE_NAME);
64+
|| ensoCtx.getExecutionEnvironment().hasContextEnabled(dataflowStacktraceCtx, ensoCtx);
6265
if (attachFullStackTrace) {
6366
var result = new DataflowError(payload, UNLIMITED_STACK_TRACE, location);
6467
TruffleStackTrace.fillIn(result);
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
package org.enso.interpreter.runtime.state;
22

3-
import org.enso.interpreter.node.expression.builtin.runtime.Context;
3+
import com.oracle.truffle.api.CompilerDirectives;
44
import org.enso.interpreter.runtime.EnsoContext;
55
import org.enso.interpreter.runtime.data.atom.Atom;
6+
import org.enso.interpreter.runtime.data.atom.AtomConstructor;
67

78
public class ExecutionEnvironment {
89

910
private final String name;
1011

11-
// Ideally we would "just" use a map here. But that leads
12-
// to native image build problems. This in turn leads to
13-
// TruffleBoundary annotations which in turn leads to slow path.
14-
private final String[] keys;
15-
private final Boolean[] permissions;
12+
private final Permissions permissions;
1613

1714
public static final String LIVE_ENVIRONMENT_NAME = "live";
1815

@@ -22,21 +19,18 @@ public class ExecutionEnvironment {
2219
public static final ExecutionEnvironment DESIGN =
2320
new ExecutionEnvironment(DESIGN_ENVIRONMENT_NAME);
2421

25-
private static final ExecutionEnvironment initLive(String name) {
26-
String[] keys = new String[] {Context.INPUT_NAME, Context.OUTPUT_NAME};
27-
Boolean[] permissions = new Boolean[] {true, true};
28-
return new ExecutionEnvironment(name, keys, permissions);
22+
private static ExecutionEnvironment initLive(String name) {
23+
var permissions = new Permissions(true, true, false);
24+
return new ExecutionEnvironment(name, permissions);
2925
}
3026

3127
public ExecutionEnvironment(String name) {
3228
this.name = name;
33-
this.keys = new String[0];
34-
this.permissions = new Boolean[0];
29+
this.permissions = new Permissions(false, false, false);
3530
}
3631

37-
private ExecutionEnvironment(String name, String[] keys, Boolean[] permissions) {
32+
private ExecutionEnvironment(String name, Permissions permissions) {
3833
this.name = name;
39-
this.keys = keys;
4034
this.permissions = permissions;
4135
}
4236

@@ -53,49 +47,39 @@ public ExecutionEnvironment withContextDisabled(Atom context) {
5347
}
5448

5549
private ExecutionEnvironment update(Atom context, boolean value) {
56-
assert context.getConstructor().getType()
57-
== EnsoContext.get(null).getBuiltins().context().getType();
58-
int keyFound = -1;
59-
for (int i = 0; i < keys.length; i++) {
60-
if (keys[i].equals(context.getConstructor().getName())) {
61-
keyFound = i;
62-
}
63-
}
64-
String[] keys1;
65-
Boolean[] permissions1;
66-
if (keyFound != -1) {
67-
keys1 = cloneArray(keys, new String[keys.length]);
68-
permissions1 = cloneArray(permissions, new Boolean[keys.length]);
69-
permissions1[keyFound] = value;
50+
var contextBuiltin = EnsoContext.get(null).getBuiltins().context();
51+
assert context.getConstructor().getType() == contextBuiltin.getType();
52+
var ctor = context.getConstructor();
53+
Permissions newPermissions;
54+
if (ctor == contextBuiltin.getInput()) {
55+
newPermissions = new Permissions(value, permissions.output, permissions.dataflowStacktrace);
56+
} else if (ctor == contextBuiltin.getOutput()) {
57+
newPermissions = new Permissions(permissions.input, value, permissions.dataflowStacktrace);
58+
} else if (ctor == contextBuiltin.getDataflowStackTrace()) {
59+
newPermissions = new Permissions(permissions.input, permissions.output, value);
7060
} else {
71-
keys1 = cloneArray(keys, new String[keys.length + 1]);
72-
permissions1 = cloneArray(permissions, new Boolean[keys.length + 1]);
73-
keyFound = keys.length;
74-
keys1[keyFound] = context.getConstructor().getName();
75-
permissions1[keyFound] = value;
61+
throw CompilerDirectives.shouldNotReachHere("Unknown context `" + ctor.getName() + "`");
7662
}
77-
return new ExecutionEnvironment(name, keys1, permissions1);
63+
return new ExecutionEnvironment(name, newPermissions);
7864
}
7965

80-
private <T> T[] cloneArray(T[] fromArray, T[] toArray) {
81-
for (int i = 0; i < fromArray.length; i++) {
82-
toArray[i] = fromArray[i];
83-
}
84-
return toArray;
85-
}
86-
87-
public Boolean hasContextEnabled(String context) {
88-
int keyFound = -1;
89-
for (int i = 0; i < keys.length; i++) {
90-
if (keys[i].equals(context)) {
91-
keyFound = i;
92-
}
93-
}
94-
if (keyFound != -1) {
95-
return permissions[keyFound];
96-
} else {
97-
return false;
66+
/**
67+
* Checks if the context is enabled in this execution environment.
68+
*
69+
* @param runtimeCtx Constructor of {@code Standard.Base.Runtime.Context} builtin type.
70+
* @return {@code true} if the context is enabled in this execution environment.
71+
*/
72+
public boolean hasContextEnabled(AtomConstructor runtimeCtx, EnsoContext ensoCtx) {
73+
var contextBuiltin = ensoCtx.getBuiltins().context();
74+
if (runtimeCtx == contextBuiltin.getInput()) {
75+
return permissions.input;
76+
} else if (runtimeCtx == contextBuiltin.getOutput()) {
77+
return permissions.output;
78+
} else if (runtimeCtx == contextBuiltin.getDataflowStackTrace()) {
79+
return permissions.dataflowStacktrace;
9880
}
81+
throw CompilerDirectives.shouldNotReachHere(
82+
"Unknown runtimeCtx `" + runtimeCtx.getName() + "`");
9983
}
10084

10185
public static ExecutionEnvironment forName(String name) {
@@ -108,4 +92,9 @@ public static ExecutionEnvironment forName(String name) {
10892
throw new IllegalArgumentException("Unsupported Execution Environment `" + name + "`");
10993
}
11094
}
95+
96+
/**
97+
* Fields correspond to the constructors of {@code Standard.Base.Runtime.Context} builtin type.
98+
*/
99+
private record Permissions(boolean input, boolean output, boolean dataflowStacktrace) {}
111100
}

0 commit comments

Comments
 (0)