Skip to content

Commit 40aedfc

Browse files
on_missing behavior for managed resources that get access after being finalized
1 parent 205811c commit 40aedfc

File tree

3 files changed

+22
-18
lines changed

3 files changed

+22
-18
lines changed

distribution/lib/Standard/Base/0.0.0-dev/src/Runtime/Managed_Resource.enso

+5-2
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,13 @@ type Managed_Resource
7373
Arguments:
7474
- action: The action that will be applied to the resource managed by
7575
resource.
76+
- on_missing: Optional action to perform when the value is no longer
77+
reachable
7678
Returns:
7779
Value returned from the `action` or `Nothing` if the managed resource
7880
was already finalized
79-
with : (Any -> Any) -> Any
80-
with self ~action = @Builtin_Method "Managed_Resource.with"
81+
with : (Any -> Any) -> Any -> Any
82+
with self ~action ~on_missing=Nothing = with_builtin self action on_missing...
8183

8284
## PRIVATE
8385
ADVANCED
@@ -89,3 +91,4 @@ type Managed_Resource
8991
take self = @Builtin_Method "Managed_Resource.take"
9092

9193
register_builtin r fn sys:Boolean = @Builtin_Method "Managed_Resource.register_builtin"
94+
with_builtin r fn ~on_missing = @Builtin_Method "Managed_Resource.with_builtin"

distribution/lib/Standard/Base/0.0.0-dev/src/System/Input_Stream.enso

+6-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import project.Data.Numbers.Integer
44
import project.Data.Text.Encoding.Encoding
55
import project.Data.Vector.Vector
66
import project.Error.Error
7+
import project.Panic.Panic
78
import project.Errors.File_Error.File_Error
89
import project.Errors.Illegal_State.Illegal_State
910
import project.Errors.Problem_Behavior.Problem_Behavior
@@ -17,6 +18,7 @@ import project.System.File.Generic.Writable_File.Writable_File
1718
import project.System.Internal.Reporting_Stream_Decoder_Helper
1819
from project.Data.Boolean import Boolean, False, True
1920

21+
polyglot java import java.io.IOException
2022
polyglot java import java.io.BufferedInputStream
2123
polyglot java import java.io.ByteArrayInputStream
2224
polyglot java import java.io.InputStream as Java_Input_Stream
@@ -114,9 +116,10 @@ type Input_Stream
114116
Arguments:
115117
- f: Applies a function over the internal java stream.
116118
with_java_stream : (Java_Input_Stream -> Any) -> Any
117-
with_java_stream self f = self.stream_resource . with java_like_stream->
118-
java_stream = Stream_Utils.asInputStream java_like_stream
119-
self.error_handler <| f java_stream
119+
with_java_stream self f =
120+
self.stream_resource . with on_missing=(Panic.throw <| IOException.new "Stream closed") java_like_stream->
121+
java_stream = Stream_Utils.asInputStream java_like_stream
122+
self.error_handler <| f java_stream
120123

121124
## PRIVATE
122125
Runs an action with a `ReportingStreamDecoder` decoding data from the
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package org.enso.interpreter.node.expression.builtin.resource;
22

3-
import com.oracle.truffle.api.dsl.Specialization;
43
import com.oracle.truffle.api.frame.VirtualFrame;
54
import com.oracle.truffle.api.nodes.Node;
65
import org.enso.interpreter.dsl.BuiltinMethod;
@@ -12,11 +11,12 @@
1211

1312
@BuiltinMethod(
1413
type = "Managed_Resource",
15-
name = "with",
14+
name = "with_builtin",
1615
description =
1716
"Applies the passed action to the underlying resource managed by the passed"
1817
+ " Managed_Resource object.")
19-
public abstract class WithNode extends Node {
18+
public final class WithNode extends Node {
19+
private WithNode() {}
2020

2121
private @Child InvokeCallableNode invokeCallableNode =
2222
InvokeCallableNode.build(
@@ -25,24 +25,22 @@ public abstract class WithNode extends Node {
2525
InvokeCallableNode.ArgumentsExecutionMode.PRE_EXECUTED);
2626

2727
static WithNode build() {
28-
return WithNodeGen.create();
28+
return new WithNode();
2929
}
3030

31-
abstract Object execute(State state, VirtualFrame frame, Object self, Object action);
32-
33-
@Specialization
34-
Object doWith(State state, VirtualFrame frame, ManagedResource self, Object action) {
31+
Object execute(
32+
State state, VirtualFrame frame, ManagedResource mr, Object action, Object onMissing) {
3533
var ctx = EnsoContext.get(this);
3634
var resourceManager = ctx.getResourceManager();
37-
if (self.getPhantomReference().refersTo(self)) {
38-
resourceManager.park(self);
35+
if (mr.getPhantomReference().refersTo(mr)) {
36+
resourceManager.park(mr);
3937
try {
40-
return invokeCallableNode.execute(action, frame, state, new Object[] {self.getResource()});
38+
return invokeCallableNode.execute(action, frame, state, new Object[] {mr.getResource()});
4139
} finally {
42-
resourceManager.unpark(self);
40+
resourceManager.unpark(mr);
4341
}
4442
} else {
45-
return ctx.getBuiltins().nothing();
43+
return onMissing;
4644
}
4745
}
4846
}

0 commit comments

Comments
 (0)