1
1
package org .enso .interpreter .runtime .state ;
2
2
3
- import org . enso . interpreter . node . expression . builtin . runtime . Context ;
3
+ import com . oracle . truffle . api . CompilerDirectives ;
4
4
import org .enso .interpreter .runtime .EnsoContext ;
5
5
import org .enso .interpreter .runtime .data .atom .Atom ;
6
+ import org .enso .interpreter .runtime .data .atom .AtomConstructor ;
6
7
7
8
public class ExecutionEnvironment {
8
9
9
10
private final String name ;
10
11
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 ;
16
13
17
14
public static final String LIVE_ENVIRONMENT_NAME = "live" ;
18
15
@@ -22,21 +19,18 @@ public class ExecutionEnvironment {
22
19
public static final ExecutionEnvironment DESIGN =
23
20
new ExecutionEnvironment (DESIGN_ENVIRONMENT_NAME );
24
21
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 );
29
25
}
30
26
31
27
public ExecutionEnvironment (String name ) {
32
28
this .name = name ;
33
- this .keys = new String [0 ];
34
- this .permissions = new Boolean [0 ];
29
+ this .permissions = new Permissions (false , false , false );
35
30
}
36
31
37
- private ExecutionEnvironment (String name , String [] keys , Boolean [] permissions ) {
32
+ private ExecutionEnvironment (String name , Permissions permissions ) {
38
33
this .name = name ;
39
- this .keys = keys ;
40
34
this .permissions = permissions ;
41
35
}
42
36
@@ -53,49 +47,39 @@ public ExecutionEnvironment withContextDisabled(Atom context) {
53
47
}
54
48
55
49
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 );
70
60
} 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 () + "`" );
76
62
}
77
- return new ExecutionEnvironment (name , keys1 , permissions1 );
63
+ return new ExecutionEnvironment (name , newPermissions );
78
64
}
79
65
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 ;
98
80
}
81
+ throw CompilerDirectives .shouldNotReachHere (
82
+ "Unknown runtimeCtx `" + runtimeCtx .getName () + "`" );
99
83
}
100
84
101
85
public static ExecutionEnvironment forName (String name ) {
@@ -108,4 +92,9 @@ public static ExecutionEnvironment forName(String name) {
108
92
throw new IllegalArgumentException ("Unsupported Execution Environment `" + name + "`" );
109
93
}
110
94
}
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 ) {}
111
100
}
0 commit comments