Skip to content

Commit

Permalink
Encapsulating State access by moving nodes to runtime.state package (#…
Browse files Browse the repository at this point in the history
…12314)

Refactoring to fix #12294.
  • Loading branch information
JaroslavTulach authored Feb 22, 2025
1 parent 96440df commit 6709d47
Show file tree
Hide file tree
Showing 25 changed files with 406 additions and 133 deletions.
4 changes: 0 additions & 4 deletions distribution/lib/Standard/Base/0.0.0-dev/docs/api/Runtime.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
- Output
- if_enabled self ~action:Standard.Base.Any.Any environment:Standard.Base.Data.Text.Text= disabled_message:Standard.Base.Data.Text.Text= panic:Standard.Base.Data.Boolean.Boolean= -> Standard.Base.Any.Any
- is_enabled self environment:Standard.Base.Any.Any= -> Standard.Base.Any.Any
- is_enabled_builtin self environment:Standard.Base.Any.Any -> Standard.Base.Any.Any
- name self -> Standard.Base.Any.Any
- with_disabled self ~action:Standard.Base.Any.Any -> Standard.Base.Any.Any
- with_enabled self ~action:Standard.Base.Any.Any -> Standard.Base.Any.Any
Expand All @@ -20,10 +19,7 @@
- get_stack_trace -> Standard.Base.Any.Any
- no_inline ~action:Standard.Base.Any.Any -> Standard.Base.Any.Any
- no_inline_with_arg function:Standard.Base.Any.Any arg:Standard.Base.Any.Any -> Standard.Base.Any.Any
- primitive_get_stack_trace -> Standard.Base.Any.Any
- value_for_uuid id:Standard.Base.Data.Text.Text -> Standard.Base.Any.Any
- with_disabled_context context:Standard.Base.Any.Any environment:Standard.Base.Any.Any= ~action:Standard.Base.Any.Any -> Standard.Base.Any.Any
- with_disabled_context_builtin context:Standard.Base.Any.Any environment:Standard.Base.Any.Any ~action:Standard.Base.Any.Any -> Standard.Base.Any.Any
- with_enabled_context context:Standard.Base.Any.Any environment:Standard.Base.Any.Any= ~action:Standard.Base.Any.Any -> Standard.Base.Any.Any
- with_enabled_context_builtin context:Standard.Base.Any.Any environment:Standard.Base.Any.Any ~action:Standard.Base.Any.Any -> Standard.Base.Any.Any
- wrap_primitive_stack_trace_element el:Standard.Base.Any.Any -> Standard.Base.Any.Any
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
private

import project.Error.Error
import project.Runtime.Context
import project.Data.Numbers.Integer
import project.Data.Text.Text
from project.Data.Boolean import Boolean, False, True


Integer.from (that:Context) = case that of
Context.Input -> 1
Context.Output -> 2
Context.Dataflow_Stack_Trace -> 4
_ -> Error.throw "Unknown context "+that.to_text


type Context_Helpers
is_enabled c env = @Builtin_Method "Context.is_enabled_builtin"
with_enabled_context_builtin context environment ~action = @Builtin_Method "Runtime.with_enabled_context_builtin"
with_disabled_context_builtin context environment ~action = @Builtin_Method "Runtime.with_disabled_context_builtin"
current_execution_environment = @Builtin_Method "Runtime.current_execution_environment"
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
private

## PRIVATE

Returns a raw representation of the current execution stack trace.
You probably want `Runtime.get_stack_trace` instead.
primitive_get_stack_trace = @Builtin_Method "Runtime.primitive_get_stack_trace"

gc = @Builtin_Method "Runtime.gc"
assertions_enabled = @Builtin_Method "Runtime.assertions_enabled"
no_inline ~action = @Builtin_Method "Runtime.no_inline"
no_inline_with_arg function arg = @Builtin_Method "Runtime.no_inline_with_arg"


60 changes: 11 additions & 49 deletions distribution/lib/Standard/Base/0.0.0-dev/src/Runtime.enso
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,20 @@ import project.Panic.Panic
import project.Polyglot.Polyglot
import project.Runtime.Source_Location.Source_Location
import project.System
import project.Internal.Runtime_Helpers
import project.Internal.Context_Helpers.Context_Helpers
from project.Data.Boolean import Boolean, False, True
from project.Data.Text.Extensions import all
from project.Runtime.Context import Dataflow_Stack_Trace, Input, Output

## Utilities for interacting with the runtime.

## PRIVATE

Returns a raw representation of the current execution stack trace.
You probably want `Runtime.get_stack_trace` instead.
primitive_get_stack_trace : Array
primitive_get_stack_trace = @Builtin_Method "Runtime.primitive_get_stack_trace"

## PRIVATE
ADVANCED

Returns the execution stack trace of its call site. The ordering of the
resulting vector is such that the top stack frame is the first element.
get_stack_trace : Vector Stack_Trace_Element
get_stack_trace =
prim_stack = primitive_get_stack_trace
prim_stack = Runtime_Helpers.primitive_get_stack_trace
stack_with_own_frame = Vector.from_polyglot_array prim_stack
stack = stack_with_own_frame.drop (..First 1)
stack.map wrap_primitive_stack_trace_element
Expand All @@ -53,7 +46,7 @@ get_stack_trace =

Runtime.gc
gc : Nothing
gc = @Builtin_Method "Runtime.gc"
gc = Runtime_Helpers.gc

## PRIVATE
ADVANCED
Expand All @@ -72,7 +65,7 @@ assert (~action : Boolean) (message : Text = "") =

## PRIVATE
Returns True if assertions are enabled.
assertions_enabled = @Builtin_Method "Runtime.assertions_enabled"
assertions_enabled = Runtime_Helpers.assertions_enabled

## PRIVATE
ADVANCED
Expand All @@ -91,7 +84,7 @@ assertions_enabled = @Builtin_Method "Runtime.assertions_enabled"

Runtime.no_inline <| IO.println "Hi!"
no_inline : Any -> Any
no_inline ~action = @Builtin_Method "Runtime.no_inline"
no_inline ~action = Runtime_Helpers.no_inline action

## PRIVATE
ADVANCED
Expand All @@ -110,7 +103,7 @@ no_inline ~action = @Builtin_Method "Runtime.no_inline"

Runtime.no_inline_with_arg IO.println "Hi!"
no_inline_with_arg : (Any -> Any) -> Any -> Any
no_inline_with_arg function arg = @Builtin_Method "Runtime.no_inline_with_arg"
no_inline_with_arg function arg = Runtime_Helpers.no_inline_with_arg function arg


## PRIVATE
Expand Down Expand Up @@ -208,11 +201,7 @@ type Context
- context: The context to enable.
is_enabled : Text -> Boolean
is_enabled self environment=Runtime.current_execution_environment =
self.is_enabled_builtin (environment.to_case Case.Lower)

## PRIVATE
is_enabled_builtin : Text -> Boolean
is_enabled_builtin self environment = @Builtin_Method "Context.is_enabled_builtin"
Context_Helpers.is_enabled self environment

## PRIVATE
Run an action with the Context enabled.
Expand All @@ -231,7 +220,7 @@ type Context

Returns the name of the current execution environment.
current_execution_environment : Text
current_execution_environment = @Builtin_Method "Runtime.current_execution_environment"
current_execution_environment = Context_Helpers.current_execution_environment

## PRIVATE
ADVANCED
Expand All @@ -244,21 +233,8 @@ current_execution_environment = @Builtin_Method "Runtime.current_execution_envir
- action: Action to be performed with the context enabled.
with_enabled_context : Context -> Text -> Function -> Any
with_enabled_context context environment=Runtime.current_execution_environment ~action =
with_enabled_context_builtin context (environment.to_case Case.Lower) action

## PRIVATE
ADVANCED

Enables a specific context in the provided runtime environment for the duration of the execution of the action.
Context_Helpers.with_enabled_context_builtin context (environment.to_case Case.Lower) action

This method is internal, using `with_enabled_context` is preferred as it provides correct defaults.

Arguments:
- environment: Name of the execution environment.
- context: The context to enable.
- action: Action to be performed with the context enabled.
with_enabled_context_builtin : Context -> Text -> Function -> Any
with_enabled_context_builtin context environment ~action = @Builtin_Method "Runtime.with_enabled_context_builtin"

## PRIVATE
ADVANCED
Expand All @@ -271,18 +247,4 @@ with_enabled_context_builtin context environment ~action = @Builtin_Method "Runt
- action: Action to be performed with the context disabled.
with_disabled_context : Context -> Text -> Function -> Any
with_disabled_context context environment=Runtime.current_execution_environment ~action =
with_disabled_context_builtin context (environment.to_case Case.Lower) action

## PRIVATE
ADVANCED

Disables a specific context in the provided runtime environment for the duration of the execution of the action.

This method is internal, using `with_disabled_context` is preferred as it provides correct defaults.

Arguments:
- environment: Name of the execution environment.
- context: The context to disable.
- action: Action to be performed with the context disabled.
with_disabled_context_builtin : Context -> Text -> Function -> Any
with_disabled_context_builtin context environment ~action = @Builtin_Method "Runtime.with_disabled_context_builtin"
Context_Helpers.with_disabled_context_builtin context (environment.to_case Case.Lower) action
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,17 @@ public Object getAnyValue(UUID key) {

@Override
public Object apply(String uuid) {
var key = UUID.fromString(uuid);
var ref = expressions.get(key);
var res = ref != null ? ref.get() : null;
var callback = observer;
if (callback != null) {
callback.accept(key);
Object res;
try {
var key = UUID.fromString(uuid);
var ref = expressions.get(key);
res = ref != null ? ref.get() : null;
var callback = observer;
if (callback != null) {
callback.accept(key);
}
} catch (IllegalArgumentException ex) {
res = null;
}
return res;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import com.oracle.truffle.api.interop.UnsupportedTypeException;
import com.oracle.truffle.api.nodes.Node;
import com.oracle.truffle.api.nodes.RootNode;
import com.oracle.truffle.api.object.DynamicObjectLibrary;
import com.oracle.truffle.api.source.SourceSection;
import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -50,6 +49,7 @@
import org.enso.interpreter.runtime.instrument.NotificationHandler;
import org.enso.interpreter.runtime.instrument.Timer;
import org.enso.interpreter.runtime.scope.ModuleScope;
import org.enso.interpreter.runtime.state.RunStateNode;
import org.enso.interpreter.runtime.state.State;
import org.enso.interpreter.service.error.FailedToApplyEditsException;
import org.enso.interpreter.service.error.MethodNotFoundException;
Expand Down Expand Up @@ -203,12 +203,10 @@ public void execute(
service ->
service.bind(module, call.getFunction().getCallTarget(), callbacks, this.timer));

DynamicObjectLibrary.getUncached()
.put(call.getState().getContainer(), IdExecutionService.class, cache);

Object p = context.getThreadManager().enter();
try {
execute.getCallTarget().call(substituteMissingArguments(call));
var callFn = Function.fullyApplied(execute.getCallTarget(), substituteMissingArguments(call));
RunStateNode.getUncached().execute(null, cacheKey(), cache, callFn);
} finally {
context.getThreadManager().leave(p);
eventNodeFactory.ifPresent(EventBinding::dispose);
Expand Down Expand Up @@ -335,7 +333,9 @@ public String toDisplayString(Object receiver) {
public Object callFunction(Object fn, Object argument) {
Object p = context.getThreadManager().enter();
try {
return call.getCallTarget().call(fn, new Object[] {argument});
var callArgs =
Function.ArgumentsHelper.buildArguments(null, new Object[] {fn, new Object[] {argument}});
return call.getCallTarget().call(callArgs);
} finally {
context.getThreadManager().leave(p);
}
Expand Down Expand Up @@ -398,19 +398,20 @@ public Object callFunctionWithInstrument(
state = State.create(context);
function = new FunctionCallInstrumentationNode.FunctionCall(fn, state, new Object[0]);
}
if (executionCache != null) {
DynamicObjectLibrary.getUncached()
.put(state.getContainer(), IdExecutionService.class, executionCache);
}

ret[0] = call.getCallTarget().call(function, arguments);
var callArgs = new Object[] {function, arguments};
var callFn = Function.fullyApplied(call.getCallTarget(), callArgs);
ret[0] = RunStateNode.getUncached().execute(null, cacheKey(), executionCache, callFn);
} finally {
context.getThreadManager().leave(p);
eventNodeFactory.ifPresent(EventBinding::dispose);
}
return ret[0];
}

private Type cacheKey() {
return context.getBuiltins().instrumentor();
}

/**
* Sets a module at a given path to use a literal source.
*
Expand Down Expand Up @@ -576,10 +577,11 @@ private static final class ExecuteRootNode extends RootNode {
@Override
public Object execute(VirtualFrame frame) {
try {
if (frame.getArguments()[0] instanceof FunctionCallInstrumentationNode.FunctionCall call) {
var args = Function.ArgumentsHelper.getPositionalArguments(frame.getArguments());
if (args[0] instanceof FunctionCallInstrumentationNode.FunctionCall call) {
return iop.execute(call);
}
throw ArityException.create(1, 1, frame.getArguments().length);
throw ArityException.create(1, 1, args.length);
} catch (UnsupportedTypeException | ArityException | UnsupportedMessageException ex) {
throw raise(RuntimeException.class, ex);
}
Expand All @@ -596,9 +598,10 @@ private static final class CallRootNode extends RootNode {
@Override
public Object execute(VirtualFrame frame) {
try {
var self = frame.getArguments()[0];
var args = (Object[]) frame.getArguments()[1];
return iop.execute(self, args);
var callArgs = Function.ArgumentsHelper.getPositionalArguments(frame.getArguments());
var fn = callArgs[0];
var args = (Object[]) callArgs[1];
return iop.execute(fn, args);
} catch (UnsupportedTypeException | ArityException | UnsupportedMessageException ex) {
throw raise(RuntimeException.class, ex);
}
Expand Down
Loading

0 comments on commit 6709d47

Please sign in to comment.