Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Demonstrate usage of foreign arrow function #9150

Merged
merged 17 commits into from
Mar 8, 2024
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import project.Any.Any
import project.Data.Vector.Vector
import project.Data.Numbers.Integer
import project.Internal.Array_Like_Helpers
import project.Errors.Common.Index_Out_Of_Bounds
import project.Nothing.Nothing

@Builtin_Type
type Arrow_Array_Builder
hubertp marked this conversation as resolved.
Show resolved Hide resolved
## PRIVATE

Sets a provided value at a given index.
set : Integer -> Any -> Nothing ! Index_Out_Of_Bounds
set self index value =
Arrow_Array_Builder.set_builtin self index value
hubertp marked this conversation as resolved.
Show resolved Hide resolved

## PRIVATE

Returns the size of the builder.
length : Integer
length self =
Array_Like_Helpers.length self

## PRIVATE

Returns an element at the provided index.
at : Integer -> Any ! Index_Out_Of_Bounds
at self index =
Array_Like_Helpers.at self index

## PRIVATE

Returns an immutable Vector from this builder.
The builder is marked as sealed and cannot be modified further.
build : Vector
build self =
Arrow_Array_Builder.build_builtin self

## PRIVATE

Returns a mutable builder backed by the provided Arrow array.
from_arrow_array : Any -> Arrow_Array_Builder
from_arrow_array arr =
Arrow_Array_Builder.from_arrow_array_builtin arr
9 changes: 9 additions & 0 deletions distribution/lib/Standard/Base/0.0.0-dev/src/Polyglot.enso
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ type Polyglot
read_array_element : Any -> Integer -> Any
read_array_element array index = @Builtin_Method "Polyglot.read_array_element"

## PRIVATE
ADVANCED
Reads the element in a given polyglot array object.

Arguments:
- index: The index to get the element from.
write_array_element : Any -> Integer -> Any
write_array_element array index = @Builtin_Method "Polyglot.write_array_element"

hubertp marked this conversation as resolved.
Show resolved Hide resolved
## PRIVATE
ADVANCED
Executes a polyglot function object (e.g. a lambda).
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package org.enso.interpreter.node.expression.builtin.interop.generic;

import com.oracle.truffle.api.interop.InteropLibrary;
import com.oracle.truffle.api.interop.InvalidArrayIndexException;
import com.oracle.truffle.api.interop.UnsupportedMessageException;
import com.oracle.truffle.api.interop.UnsupportedTypeException;
import com.oracle.truffle.api.nodes.Node;
import com.oracle.truffle.api.profiles.BranchProfile;
import org.enso.interpreter.Constants;
import org.enso.interpreter.dsl.BuiltinMethod;
import org.enso.interpreter.runtime.EnsoContext;
import org.enso.interpreter.runtime.builtin.Builtins;
import org.enso.interpreter.runtime.error.PanicException;

@BuiltinMethod(
type = "Polyglot",
hubertp marked this conversation as resolved.
Show resolved Hide resolved
name = "write_array_element",
description = "Write a value from the array specified by the index.",
hubertp marked this conversation as resolved.
Show resolved Hide resolved
autoRegister = false)
public class WriteArrayElementNode extends Node {
private @Child InteropLibrary library =
InteropLibrary.getFactory().createDispatched(Constants.CacheSizes.BUILTIN_INTEROP_DISPATCH);

private final BranchProfile err = BranchProfile.create();

Object execute(Object array, long index, Object value) {
try {
library.writeArrayElement(array, index, value);
return EnsoContext.get(this).getBuiltins().nothing();
} catch (UnsupportedMessageException e) {
err.enter();
Builtins builtins = EnsoContext.get(this).getBuiltins();
throw new PanicException(
builtins.error().makeTypeError(builtins.array(), array, "array"), this);
} catch (InvalidArrayIndexException e) {
err.enter();
Builtins builtins = EnsoContext.get(this).getBuiltins();
throw new PanicException(builtins.error().makeInvalidArrayIndex(array, index), this);
} catch (UnsupportedTypeException e) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

UnsupportedMessageException leads to makeTypeError while UnsupportedTypeException leads to panic. Isn't that strange? Shouldn't UnsupportedTypeException lead to some type error?

err.enter();
throw EnsoContext.get(library).raiseAssertionPanic(library, null, e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.enso.interpreter.node.expression.builtin.mutable;

import com.oracle.truffle.api.dsl.Fallback;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.nodes.Node;
import org.enso.interpreter.dsl.BuiltinMethod;
import org.enso.interpreter.runtime.EnsoContext;
import org.enso.interpreter.runtime.data.EnsoObject;
import org.enso.interpreter.runtime.error.PanicException;

@BuiltinMethod(
type = "Arrow_Array_Builder",
name = "build_builtin",
description = "Creates a Vector from this Arrow_Array_Builder.",
autoRegister = true)
public abstract class BuildBuiltinArrowArrayBuilderNode extends Node {

static BuildBuiltinArrowArrayBuilderNode build() {
return BuildBuiltinArrowArrayBuilderNodeGen.create();
}

abstract EnsoObject execute(Object arr);

@Specialization
EnsoObject doArrowArrayBuilder(
org.enso.interpreter.runtime.data.vector.ArrowArrayBuilder builder) {
builder.seal();
return org.enso.interpreter.runtime.data.vector.ArrayLikeHelpers.asVectorFromArray(builder);
}

@Fallback
EnsoObject doOther(Object arr) {
var ctx = EnsoContext.get(this);
throw new PanicException(
ctx.getBuiltins().error().makeTypeError("polyglot array", arr, "array"), this);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.enso.interpreter.node.expression.builtin.mutable;

import com.oracle.truffle.api.dsl.Fallback;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.interop.InteropLibrary;
import com.oracle.truffle.api.library.CachedLibrary;
import com.oracle.truffle.api.nodes.Node;
import org.enso.interpreter.dsl.BuiltinMethod;
import org.enso.interpreter.runtime.EnsoContext;
import org.enso.interpreter.runtime.data.EnsoObject;
import org.enso.interpreter.runtime.error.PanicException;

@BuiltinMethod(
type = "Arrow_Array_Builder",
name = "from_arrow_array_builtin",
description = "Creates an Arrow_Array_Builder from a polyglot array.",
autoRegister = true)
public abstract class FromArrowArrayBuiltinArrowArrayBuilderNode extends Node {

static FromArrowArrayBuiltinArrowArrayBuilderNode build() {
return FromArrowArrayBuiltinArrowArrayBuilderNodeGen.create();
}

abstract EnsoObject execute(Object arr);

@Specialization(guards = "interop.hasArrayElements(arr)")
EnsoObject doObject(Object arr, @CachedLibrary(limit = "1") InteropLibrary interop) {
return org.enso.interpreter.runtime.data.vector.ArrowArrayBuilder.fromArrowArray(arr);
}

@Fallback
EnsoObject doOther(Object arr) {
var ctx = EnsoContext.get(this);
throw new PanicException(
ctx.getBuiltins().error().makeTypeError("polyglot array", arr, "array"), this);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package org.enso.interpreter.node.expression.builtin.mutable;

import com.oracle.truffle.api.dsl.Cached;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.interop.InteropLibrary;
import com.oracle.truffle.api.interop.InvalidArrayIndexException;
import com.oracle.truffle.api.interop.UnsupportedMessageException;
import com.oracle.truffle.api.interop.UnsupportedTypeException;
import com.oracle.truffle.api.library.CachedLibrary;
import com.oracle.truffle.api.nodes.Node;
import org.enso.interpreter.dsl.BuiltinMethod;
import org.enso.interpreter.runtime.EnsoContext;
import org.enso.interpreter.runtime.data.vector.ArrayLikeLengthNode;
import org.enso.interpreter.runtime.error.DataflowError;

@BuiltinMethod(
type = "Arrow_Array_Builder",
name = "set_builtin",
description = "Sets a value into the Vector at the specified index.")
public abstract class SetBuiltinArrowArrayBuilderNode extends Node {
static SetBuiltinArrowArrayBuilderNode build() {
return SetBuiltinArrowArrayBuilderNodeGen.create();
}

abstract Object execute(Object arr, long index, Object value);

@Specialization
Object fromObject(
Object arr,
long index,
Object value,
@CachedLibrary(limit = "1") InteropLibrary interop,
@Cached ArrayLikeLengthNode lengthNode) {
try {
interop.writeArrayElement(arr, index, value);
} catch (UnsupportedMessageException e) {
var err =
EnsoContext.get(interop)
.getBuiltins()
.error()
.makeUnsupportedArgumentsError(new Object[] {value}, "invalid argument");
return DataflowError.withoutTrace(err, this);
} catch (UnsupportedTypeException e) {
throw EnsoContext.get(interop).raiseAssertionPanic(interop, null, e);
} catch (InvalidArrayIndexException e) {
var err =
EnsoContext.get(this)
.getBuiltins()
.error()
.makeIndexOutOfBounds(index, lengthNode.executeLength(arr));
return DataflowError.withoutTrace(err, this);
}
return EnsoContext.get(this).getBuiltins().nothing();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.enso.interpreter.node.expression.builtin.io.File;
import org.enso.interpreter.node.expression.builtin.meta.ProjectDescription;
import org.enso.interpreter.node.expression.builtin.mutable.Array;
import org.enso.interpreter.node.expression.builtin.mutable.ArrowArrayBuilder;
import org.enso.interpreter.node.expression.builtin.mutable.Ref;
import org.enso.interpreter.node.expression.builtin.ordering.Comparable;
import org.enso.interpreter.node.expression.builtin.ordering.DefaultComparator;
Expand Down Expand Up @@ -106,6 +107,8 @@ public static class Debug {
private final Builtin text;
private final Builtin array;
private final Builtin vector;

private final Builtin arrowArrayBuilder;
private final Builtin map;
private final Builtin dataflowError;
private final Builtin ref;
Expand Down Expand Up @@ -156,6 +159,7 @@ public Builtins(EnsoContext context) {
text = builtins.get(Text.class);
array = builtins.get(Array.class);
vector = builtins.get(Vector.class);
arrowArrayBuilder = builtins.get(ArrowArrayBuilder.class);
map = builtins.get(org.enso.interpreter.node.expression.builtin.Map.class);
dataflowError = builtins.get(org.enso.interpreter.node.expression.builtin.Error.class);
ref = builtins.get(Ref.class);
Expand Down Expand Up @@ -678,6 +682,10 @@ public Type vector() {
return vector.getType();
}

public Type arrowArrayBuilder() {
return arrowArrayBuilder.getType();
}

public Type map() {
return map.getType();
}
Expand Down
Loading
Loading