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
6 changes: 3 additions & 3 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -2207,8 +2207,7 @@ lazy val `engine-runner` = project
"com.sun.imageio",
"com.sun.jna.internal.Cleaner",
"com.sun.jna.Structure$FFIType",
"akka.http",
"org.enso.interpreter.arrow.util.MemoryUtil"
"akka.http"
)
)
.dependsOn(assembly)
Expand Down Expand Up @@ -3021,7 +3020,8 @@ buildEngineDistribution := {
val _ = (`engine-runner` / assembly).value
updateLibraryManifests.value
val modulesToCopy = componentModulesPaths.value.map(_.data)
val engineModules = Seq(file("runtime.jar"))
val arrow = Seq((`runtime-language-arrow` / Compile / packageBin).value)
val engineModules = Seq(file("runtime.jar")) ++ arrow
val root = engineDistributionRoot.value
val log = streams.value.log
val cacheFactory = streams.value.cacheStoreFactory
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
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 Polyglot_Array_Builder
## PRIVATE

Appends a value at a next available index.
append : Any -> Nothing ! Index_Out_Of_Bounds
append self value =
Polyglot_Array_Builder.append_builtin self value

## 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 =
Polyglot_Array_Builder.build_builtin self

## PRIVATE

Returns a mutable builder backed by the provided polyglot array.
from_polyglot_array : Any -> Polyglot_Array_Builder
from_polyglot_array arr =
Polyglot_Array_Builder.from_polyglot_array_builtin arr
10 changes: 10 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,16 @@ type Polyglot
read_array_element : Any -> Integer -> Any
read_array_element array index = @Builtin_Method "Polyglot.read_array_element"

## PRIVATE
ADVANCED
Writes the element to a polyglot array object at a given index.

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

## PRIVATE
ADVANCED
Executes a polyglot function object (e.g. a lambda).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ public void checkAllConstantGenValuesArePresent() throws Exception {
}
}
expecting.remove(ConstantsGen.ARRAY_LIKE_HELPERS);
expecting.remove(ConstantsGen.ARROW_ARRAY_BUILDER);
var w = new StringBuilder();
var f = new StringWriter();
var err = new PrintWriter(f);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public record Result(PhysicalLayout physicalLayout, LogicalLayout logicalLayout,

public static Result parse(Source source) {
String src = source.getCharacters().toString();
Matcher m = ARRAY_PATTERN.matcher(src);
Matcher m = NEW_ARRAY_CONSTR.matcher(src);
if (m.find()) {
try {
var layout = LogicalLayout.valueOf(m.group(1));
Expand All @@ -36,8 +36,8 @@ public static Result parse(Source source) {
return null;
}

private static final Pattern ARRAY_PATTERN = Pattern.compile("new\\[(.+)\\]");
private static final Pattern CAST_PATTERN = Pattern.compile("cast\\[(.+)\\]");
private static final Pattern NEW_ARRAY_CONSTR = Pattern.compile("^new\\[(.+)\\]$");
private static final Pattern CAST_PATTERN = Pattern.compile("^cast\\[(.+)\\]$");

public enum Mode {
Allocate,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@
import com.oracle.truffle.api.library.CachedLibrary;
import com.oracle.truffle.api.library.ExportLibrary;
import com.oracle.truffle.api.library.ExportMessage;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import org.enso.interpreter.arrow.LogicalLayout;
import org.enso.interpreter.arrow.util.MemoryUtil;

@ExportLibrary(InteropLibrary.class)
public class ArrowCastToFixedSizeArrayFactory implements TruffleObject {
Expand Down Expand Up @@ -114,20 +111,16 @@ private static ByteBufferDirect pointer(Object[] args, InteropLibrary interop, S
new Object[] {args[0]}, "Size of allocated memory is invalid");
}

var size = interop.asInt(args[1]);
var targetSize = size * unit.sizeInBytes();
ByteBuffer buffer = MemoryUtil.directBuffer(interop.asLong(args[0]), targetSize);
buffer.order(ByteOrder.LITTLE_ENDIAN);
var capacity = interop.asInt(args[1]);
if (args.length == 3) {
if (!interop.isNumber(args[2]) || !interop.fitsInLong(args[2])) {
throw UnsupportedTypeException.create(
new Object[] {args[2]}, "Address of non-null bitmap is invalid");
}
ByteBuffer validityMap =
MemoryUtil.directBuffer(interop.asLong(args[2]), (int) Math.ceil(size / 8) + 1);
return new ByteBufferDirect(buffer, validityMap);
return ByteBufferDirect.fromAddress(
interop.asLong(args[0]), interop.asLong(args[2]), capacity, unit);
} else {
return new ByteBufferDirect(buffer, size);
return ByteBufferDirect.fromAddress(interop.asLong(args[0]), capacity, unit);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public final class ArrowFixedArrayDate implements TruffleObject {
public ArrowFixedArrayDate(int size, DateUnit unit) {
this.size = size;
this.unit = unit;
this.buffer = allocateBuffer(size * unit.sizeInBytes(), size);
this.buffer = ByteBufferDirect.forSize(size, unit);
}

public ArrowFixedArrayDate(ByteBufferDirect buffer, DateUnit unit)
Expand Down Expand Up @@ -207,11 +207,6 @@ public ZoneId asTimeZone() {
}
}

@CompilerDirectives.TruffleBoundary
private static ByteBufferDirect allocateBuffer(int sizeInBytes, int size) {
return new ByteBufferDirect(sizeInBytes, size);
}

@CompilerDirectives.TruffleBoundary
private static LocalDate localDateFromDays(int daysSinceEpoch) {
return LocalDate.ofEpochDay(daysSinceEpoch);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package org.enso.interpreter.arrow.runtime;

import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.dsl.Cached;
import com.oracle.truffle.api.dsl.ImportStatic;
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.TruffleObject;
import com.oracle.truffle.api.interop.UnsupportedMessageException;
import com.oracle.truffle.api.library.CachedLibrary;
Expand All @@ -21,7 +21,7 @@ public final class ArrowFixedArrayInt implements TruffleObject {
public ArrowFixedArrayInt(int size, IntUnit unit) {
this.size = size;
this.unit = unit;
this.buffer = allocateBuffer(size * this.unit.sizeInBytes(), size);
this.buffer = ByteBufferDirect.forSize(size, unit);
}

public ArrowFixedArrayInt(ByteBufferDirect buffer, IntUnit unit)
Expand All @@ -45,7 +45,8 @@ public boolean hasArrayElements() {
static class ReadArrayElement {
@Specialization(guards = "receiver.getUnit() == Byte1")
public static Object doByte(ArrowFixedArrayInt receiver, long index)
throws UnsupportedMessageException {
throws UnsupportedMessageException, InvalidArrayIndexException {
validAccess(receiver, index);
if (receiver.buffer.isNull((int) index)) {
return NullValue.get();
}
Expand All @@ -55,7 +56,8 @@ public static Object doByte(ArrowFixedArrayInt receiver, long index)

@Specialization(guards = "receiver.getUnit() == Byte2")
public static Object doShort(ArrowFixedArrayInt receiver, long index)
throws UnsupportedMessageException {
throws UnsupportedMessageException, InvalidArrayIndexException {
validAccess(receiver, index);
if (receiver.buffer.isNull((int) index)) {
return NullValue.get();
}
Expand All @@ -65,8 +67,8 @@ public static Object doShort(ArrowFixedArrayInt receiver, long index)

@Specialization(guards = "receiver.getUnit() == Byte4")
public static Object doInt(ArrowFixedArrayInt receiver, long index)
throws UnsupportedMessageException {

throws UnsupportedMessageException, InvalidArrayIndexException {
validAccess(receiver, index);
if (receiver.buffer.isNull((int) index)) {
return NullValue.get();
}
Expand All @@ -76,13 +78,21 @@ public static Object doInt(ArrowFixedArrayInt receiver, long index)

@Specialization(guards = "receiver.getUnit() == Byte8")
public static Object doLong(ArrowFixedArrayInt receiver, long index)
throws UnsupportedMessageException {
throws UnsupportedMessageException, InvalidArrayIndexException {
validAccess(receiver, index);
if (receiver.buffer.isNull((int) index)) {
return NullValue.get();
}
var at = typeAdjustedIndex(index, receiver.unit);
return receiver.buffer.getLong(at);
}

private static void validAccess(ArrowFixedArrayInt receiver, long index)
throws InvalidArrayIndexException {
if (index >= receiver.size || index < 0) {
throw InvalidArrayIndexException.create(index);
}
}
}

@ExportMessage
Expand All @@ -94,7 +104,8 @@ public static void doByte(
long index,
Object value,
@Cached.Shared("interop") @CachedLibrary(limit = "1") InteropLibrary iop)
throws UnsupportedMessageException {
throws UnsupportedMessageException, InvalidArrayIndexException {
validAccess(receiver, index);
if (!iop.fitsInByte(value)) {
throw UnsupportedMessageException.create();
}
Expand All @@ -107,7 +118,8 @@ public static void doShort(
long index,
Object value,
@Cached.Shared("interop") @CachedLibrary(limit = "1") InteropLibrary iop)
throws UnsupportedMessageException {
throws UnsupportedMessageException, InvalidArrayIndexException {
validAccess(receiver, index);
if (!iop.fitsInShort(value)) {
throw UnsupportedMessageException.create();
}
Expand All @@ -120,7 +132,8 @@ public static void doInt(
long index,
Object value,
@Cached.Shared("interop") @CachedLibrary(limit = "1") InteropLibrary iop)
throws UnsupportedMessageException {
throws UnsupportedMessageException, InvalidArrayIndexException {
validAccess(receiver, index);
if (!iop.fitsInInt(value)) {
throw UnsupportedMessageException.create();
}
Expand All @@ -133,12 +146,20 @@ public static void doLong(
long index,
Object value,
@Cached.Shared("interop") @CachedLibrary(limit = "1") InteropLibrary iop)
throws UnsupportedMessageException {
throws UnsupportedMessageException, InvalidArrayIndexException {
validAccess(receiver, index);
if (!iop.fitsInLong(value)) {
throw UnsupportedMessageException.create();
}
receiver.buffer.putLong(typeAdjustedIndex(index, receiver.unit), (iop.asLong(value)));
}

private static void validAccess(ArrowFixedArrayInt receiver, long index)
throws InvalidArrayIndexException {
if (index >= receiver.size || index < 0) {
throw InvalidArrayIndexException.create(index);
}
}
}

public enum IntUnit implements SizeInBytes {
Expand Down Expand Up @@ -178,11 +199,6 @@ boolean isArrayElementInsertable(long index) {
return index >= 0 && index < size;
}

@CompilerDirectives.TruffleBoundary
private ByteBufferDirect allocateBuffer(int sizeInBytes, int size) {
return new ByteBufferDirect(sizeInBytes, size);
}

private static int typeAdjustedIndex(long index, SizeInBytes unit) {
return Math.toIntExact(index * unit.sizeInBytes());
}
Expand Down
Loading
Loading