Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
GregoryTravis committed Mar 6, 2025
1 parent 46d26cf commit 74d152b
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import project.Panic.Panic

polyglot java import java.lang.IllegalArgumentException

@Builtin_Type
type Illegal_Argument
## PRIVATE
UNSTABLE
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
## An API for manual resource management.

import project.Any.Any
import project.Data.Text.Text
import project.Error.Error
import project.Errors.Common.Uninitialized_State
import project.Errors.Illegal_Argument.Illegal_Argument
import project.Meta
import project.Nothing.Nothing
import project.Panic.Panic
from project.Data.Boolean import Boolean, False, True
from project.Data.Text.Extensions import all

## Resource provides an API for manual management of computation resources.

Expand Down Expand Up @@ -47,7 +52,7 @@ type Managed_Resource
! Multiple `Managed_Resource`s for the same resource

Using the same underlying resource with multiple managed resource
instances is an error and will result in a `Forbidden_Operation` panic.
instances is an error and will result in an `Illegal_Argument` panic.

! Values Eligible to be `Managed_Resource`s

Expand All @@ -64,7 +69,8 @@ type Managed_Resource
A `Managed_Resource` object that can be used to access the resource.
register : Any -> (Any -> Nothing) -> Boolean -> Managed_Resource
register resource function system_finalization_allowed=False =
@Tail_Call register_builtin resource function system_finalization_allowed
_handle_double_registration <|
@Tail_Call register_builtin resource function system_finalization_allowed

## PRIVATE
ADVANCED
Expand Down Expand Up @@ -115,3 +121,13 @@ register_builtin r fn sys:Boolean = @Builtin_Method "Managed_Resource.register_b

## PRIVATE
with_builtin r fn = @Builtin_Method "Managed_Resource.with_builtin"

## PRIVATE
Catch a double-registration panic and re-raise it as an `Illegal_Argument`.
_handle_double_registration ~action =
handler caught =
payload = caught.payload
is_double_registration = payload.is_a Text && payload.starts_with "Object is already registered as a ManagedResource"
if is_double_registration.not then Panic.throw caught else
Panic.throw (Illegal_Argument.Error payload)
Panic.catch Any action handler
2 changes: 1 addition & 1 deletion docs/semantics/managed-resources.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ be finalized as soon as the first managed resource is garbage collected.
Moreover, the finalizer will be called for each garbage collected managed
resource, leading to multiple-finalization of the underlying object. Therefore,
using the same underlying resource with multiple managed resource instances
is an error and will result in a `Forbidden_Operation` panic.
is an error and will result in an `Illegal_Argument` panic.

### Objects Eligible to be a Managed Resource

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
* Allows the context to attach garbage collection hooks on the removal of certain objects.
*
* <p><Using the same underlying resource with multiple managed resource instances is an error and
* will result in a `Forbidden_Operation` panic.
* will result in an `Illegal_Argument` panic.
*
* <p>Truly atomic values such as integer `2` cannot be managed resources.
*/
Expand Down Expand Up @@ -156,8 +156,7 @@ public synchronized ManagedResource register(
if (alreadyRegistered(object)) {
var error = context.getBuiltins().error();
var msg = "Object is already registered as a ManagedResource: " + object;
var payload = error.makeIllegalArgument(msg);
throw new PanicException(payload, null);
throw new PanicException(msg, null);
}

if (CLOSED == processor) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import org.enso.interpreter.node.expression.builtin.error.CaughtPanic;
import org.enso.interpreter.node.expression.builtin.error.CompileError;
import org.enso.interpreter.node.expression.builtin.error.ForbiddenOperation;
import org.enso.interpreter.node.expression.builtin.error.IllegalArgument;
import org.enso.interpreter.node.expression.builtin.error.IncomparableValues;
import org.enso.interpreter.node.expression.builtin.error.IndexOutOfBounds;
import org.enso.interpreter.node.expression.builtin.error.InexhaustivePatternMatch;
Expand Down Expand Up @@ -59,7 +58,6 @@ public final class Error {
private final ArithmeticError arithmeticError;
private final InvalidArrayIndex invalidArrayIndex;
private final ArityError arityError;
private final IllegalArgument illegalArgument;
private final IncomparableValues incomparableValues;
private final UnsupportedArgumentTypes unsupportedArgumentsError;
private final ModuleDoesNotExist moduleDoesNotExistError;
Expand Down Expand Up @@ -100,7 +98,6 @@ public Error(Builtins builtins, EnsoContext context) {
arithmeticError = builtins.getBuiltinType(ArithmeticError.class);
invalidArrayIndex = builtins.getBuiltinType(InvalidArrayIndex.class);
arityError = builtins.getBuiltinType(ArityError.class);
illegalArgument = builtins.getBuiltinType(IllegalArgument.class);
incomparableValues = builtins.getBuiltinType(IncomparableValues.class);
unsupportedArgumentsError = builtins.getBuiltinType(UnsupportedArgumentTypes.class);
moduleDoesNotExistError = builtins.getBuiltinType(ModuleDoesNotExist.class);
Expand Down Expand Up @@ -133,10 +130,6 @@ public Atom makeIndexOutOfBounds(long index, long length) {
return indexOutOfBounds.newInstance(index, length);
}

public Atom makeIllegalArgument(Object message) {
return illegalArgument.newInstance(message);
}

public Atom makeIncomparableValues(Object leftOperand, Object rightOperand) {
return incomparableValues.newInstance(leftOperand, rightOperand);
}
Expand Down Expand Up @@ -364,8 +357,8 @@ public Atom makePrivateAccessError(
thisProjName, targetProjName, Text.create(targetMethodName));
}

public Atom makeForbiddenOperation(String message) {
return forbiddenOperation.newInstance(Text.create(message));
public ForbiddenOperation getForbiddenOperation() {
return forbiddenOperation;
}

public Atom makeUnimplemented(String operation) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
* ProcessItems} processor.
*
* <p><Using the same underlying resource with multiple managed resource instances is an error and
* will result in a `Forbidden_Operation` panic.
* will result in an `Illegal_Argument` panic.
*
* <p>Truly atomic values such as integer `2` cannot be managed resources.
*/
Expand Down

0 comments on commit 74d152b

Please sign in to comment.