Skip to content

Commit

Permalink
PR comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
jdunkerley committed Jul 3, 2023
1 parent 2cd4bf3 commit 75e85c3
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 39 deletions.
6 changes: 0 additions & 6 deletions distribution/lib/Standard/Base/0.0.0-dev/src/Data/Array.enso
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,6 @@ type Array
insert : Integer -> Any -> Vector ! Index_Out_Of_Bounds
insert self at=self.length item=Nothing = Vector.insert self at item

## PRIVATE
insert_builtin self at values = @Builtin_Method "Vector.insert_builtin"

## Removes the item at the given index from the array.

Arguments:
Expand All @@ -195,9 +192,6 @@ type Array
remove : Integer -> Vector
remove self at=-1 = Vector.remove self at

## PRIVATE
remove_builtin self at = @Builtin_Method "Vector.remove_builtin"

## Get the first element from the array, or an `Index_Out_Of_Bounds` if the array
is empty.

Expand Down
12 changes: 6 additions & 6 deletions distribution/lib/Standard/Base/0.0.0-dev/src/Data/Vector.enso
Original file line number Diff line number Diff line change
Expand Up @@ -686,9 +686,6 @@ type Vector a
_ : Array -> self + Vector.from_polyglot_array that
_ -> Error.throw (Type_Error.Error Vector that "that")

## PRIVATE
insert_builtin self at values = @Builtin_Method("Vector.insert_builtin")

## Inserts the given item into the vector at the given index.

Arguments:
Expand All @@ -711,7 +708,10 @@ type Vector a
if used_index < 0 || used_index > self_len then Error.throw (Index_Out_Of_Bounds.Error at self_len+1) else
if used_index == self_len then self + [item] else
if used_index == 0 then [item] + self else
self.insert_builtin used_index [item]
Vector.insert_builtin self used_index [item]

## PRIVATE
insert_builtin vec at values = @Builtin_Method("Vector.insert_builtin")

## Removes the item at the given index from the vector.

Expand All @@ -724,10 +724,10 @@ type Vector a
self_len = self.length
used_index = if at < 0 then self_len + at else at
if used_index >= self_len || used_index < 0 then Error.throw (Index_Out_Of_Bounds.Error at self_len) else
self.remove_builtin used_index
Vector.remove_builtin self used_index

## PRIVATE
remove_builtin self at = @Builtin_Method "Vector.remove_builtin"
remove_builtin vec at = @Builtin_Method "Vector.remove_builtin"

## When `self` is a vector of text values, concatenates all the values by
interspersing them with `separator`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,34 @@ static FlattenVectorNode build() {
@Specialization
Vector fromVector(
Vector self, @Cached CopyNode copyNode, @CachedLibrary(limit = "3") InteropLibrary interop) {
return flatten(self.toArray(), copyNode, interop);
try {
return flatten(self.toArray(), copyNode, interop);
} catch (UnsupportedMessageException e) {
CompilerDirectives.transferToInterpreter();
Builtins builtins = EnsoContext.get(this).getBuiltins();
throw new PanicException(
builtins.error().makeTypeError(builtins.vector(), self, "self"), this);
}
}

@Specialization
Vector fromArray(
Array self, @Cached CopyNode copyNode, @CachedLibrary(limit = "3") InteropLibrary interop) {
return flatten(self, copyNode, interop);
try {
return flatten(self, copyNode, interop);
} catch (UnsupportedMessageException e) {
throw unsupportedException(self);
}
}

@Specialization(guards = "interop.hasArrayElements(self)")
Vector fromArrayLike(
Object self, @Cached CopyNode copyNode, @CachedLibrary(limit = "3") InteropLibrary interop) {
return flatten(self, copyNode, interop);
try {
return flatten(self, copyNode, interop);
} catch (UnsupportedMessageException e) {
throw unsupportedException(self);
}
}

@Fallback
Expand All @@ -53,22 +68,24 @@ Vector fromUnknown(Object self) {
}

private PanicException unsupportedException(Object self) {
CompilerDirectives.transferToInterpreter();
var ctx = EnsoContext.get(this);
var err = ctx.getBuiltins().error().makeTypeError("polyglot array", self, "self");
throw new PanicException(err, this);
}

private Vector flatten(Object storage, CopyNode copyNode, InteropLibrary interop) {
private Vector flatten(Object storage, CopyNode copyNode, InteropLibrary interop) throws UnsupportedMessageException{
try {
long length = interop.getArraySize(storage);

long flattened_length = 0;
for (long i = 0; i < length; i++) {
var item = interop.readArrayElement(storage, i);
if (!interop.hasArrayElements(item)) {
CompilerDirectives.transferToInterpreter();
Builtins builtins = EnsoContext.get(this).getBuiltins();
throw new PanicException(
builtins.error().makeTypeError(builtins.array(), item, "[" + i + "]"), this);
builtins.error().makeTypeError(builtins.vector(), item, "[" + i + "]"), this);
}

flattened_length += interop.getArraySize(item);
Expand All @@ -84,9 +101,11 @@ private Vector flatten(Object storage, CopyNode copyNode, InteropLibrary interop
}

return Vector.fromArray(result);
} catch (UnsupportedMessageException | InvalidArrayIndexException e) {
} catch (InvalidArrayIndexException e) {
CompilerDirectives.transferToInterpreter();
throw new IllegalStateException(e);
Builtins builtins = EnsoContext.get(this).getBuiltins();
throw new PanicException(
builtins.error().makeInvalidArrayIndex(storage, e.getInvalidIndex()), this);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,12 @@ private PanicException unsupportedException(Object values) {
private Vector insertBuiltin(
Object current, long index, Object values, CopyNode copyNode, InteropLibrary interop) {
try {
long current_length = interop.getArraySize(current);
long values_length = interop.getArraySize(values);
Array result = Array.allocate(current_length + values_length);
long currentLength = interop.getArraySize(current);
long valuesLength = interop.getArraySize(values);
Array result = Array.allocate(currentLength + valuesLength);
copyNode.execute(current, 0, result, 0, index);
copyNode.execute(values, 0, result, index, values_length);
copyNode.execute(current, index, result, index + values_length, current_length - index);
copyNode.execute(values, 0, result, index, valuesLength);
copyNode.execute(current, index, result, index + valuesLength, currentLength - index);
return Vector.fromArray(result);
} catch (UnsupportedMessageException e) {
throw unsupportedException(values);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.enso.interpreter.dsl.BuiltinMethod;
import org.enso.interpreter.node.expression.builtin.mutable.CopyNode;
import org.enso.interpreter.runtime.EnsoContext;
import org.enso.interpreter.runtime.builtin.Builtins;
import org.enso.interpreter.runtime.data.Array;
import org.enso.interpreter.runtime.data.Vector;
import org.enso.interpreter.runtime.error.PanicException;
Expand All @@ -33,7 +34,14 @@ Vector fromVector(
long index,
@Cached CopyNode copyNode,
@CachedLibrary(limit = "3") InteropLibrary interop) {
return removeAtIndex(self.toArray(), index, copyNode, interop);
try {
return removeAtIndex(self.toArray(), index, copyNode, interop);
} catch (UnsupportedMessageException e) {
CompilerDirectives.transferToInterpreter();
Builtins builtins = EnsoContext.get(this).getBuiltins();
throw new PanicException(
builtins.error().makeTypeError(builtins.vector(), self, "self"), this);
}
}

@Specialization
Expand All @@ -42,7 +50,11 @@ Vector fromArray(
long index,
@Cached CopyNode copyNode,
@CachedLibrary(limit = "3") InteropLibrary interop) {
return removeAtIndex(self, index, copyNode, interop);
try {
return removeAtIndex(self, index, copyNode, interop);
} catch (UnsupportedMessageException e) {
throw unsupportedException(self);
}
}

@Specialization(guards = "interop.hasArrayElements(self)")
Expand All @@ -51,7 +63,11 @@ Vector fromArrayLike(
long index,
@Cached CopyNode copyNode,
@CachedLibrary(limit = "3") InteropLibrary interop) {
return removeAtIndex(self, index, copyNode, interop);
try {
return removeAtIndex(self, index, copyNode, interop);
} catch (UnsupportedMessageException e) {
throw unsupportedException(self);
}
}

@Fallback
Expand All @@ -60,23 +76,19 @@ Vector fromUnknown(Object self, long index) {
}

private PanicException unsupportedException(Object self) {
CompilerDirectives.transferToInterpreter();
var ctx = EnsoContext.get(this);
var err = ctx.getBuiltins().error().makeTypeError("polyglot array", self, "self");
throw new PanicException(err, this);
}

private Vector removeAtIndex(
Object storage, long index, CopyNode copyArrayNode, InteropLibrary interop) {
try {
long length = interop.getArraySize(storage);
long actualIndex = index < 0 ? index + length : index;
Array array = Array.allocate(length - 1);
copyArrayNode.execute(storage, 0, array, 0, actualIndex);
copyArrayNode.execute(storage, actualIndex + 1, array, actualIndex, length - actualIndex - 1);
return Vector.fromArray(array);
} catch (UnsupportedMessageException e) {
CompilerDirectives.transferToInterpreter();
throw new IllegalStateException(e);
}
Object storage, long index, CopyNode copyArrayNode, InteropLibrary interop) throws UnsupportedMessageException {
long length = interop.getArraySize(storage);
long actualIndex = index < 0 ? index + length : index;
Array array = Array.allocate(length - 1);
copyArrayNode.execute(storage, 0, array, 0, actualIndex);
copyArrayNode.execute(storage, actualIndex + 1, array, actualIndex, length - actualIndex - 1);
return Vector.fromArray(array);
}
}

0 comments on commit 75e85c3

Please sign in to comment.