-
Notifications
You must be signed in to change notification settings - Fork 323
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make ArrayOverBuffer behave like an Array/Array.sort no longer mutate…
…s the Array (#4022) Most of the problems with accessing `ArrayOverBuffer` have been resolved by using `CoerceArrayNode` (#3817). In `Array.sort` we still however specialized on Array which wasn't compatible with `ArrayOverBuffer`. Similarly sorting JS or Python arrays wouldn't work. Added a specialization to `Array.sort` to deal with that case. A generic specialization (with `hasArrayElements`) not only handles `ArrayOverBuffer` but also polyglot arrays coming from JS or Python. We could have an additional specialization for `ArrayOverBuffer` only (removed in the last commit) that returns `ArrayOverBuffer` rather than `Array` although that adds additional complexity which so far is unnecessary. Also fixed an example in `Array.enso` by providing a default argument.
- Loading branch information
Showing
11 changed files
with
190 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
engine/runtime/src/main/java/org/enso/interpreter/runtime/data/DisplayArrayUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package org.enso.interpreter.runtime.data; | ||
|
||
import com.oracle.truffle.api.CompilerDirectives; | ||
import com.oracle.truffle.api.interop.InteropLibrary; | ||
import com.oracle.truffle.api.interop.InvalidArrayIndexException; | ||
import com.oracle.truffle.api.interop.UnsupportedMessageException; | ||
|
||
import java.io.PrintWriter; | ||
import java.io.StringWriter; | ||
|
||
public class DisplayArrayUtils { | ||
|
||
@CompilerDirectives.TruffleBoundary | ||
public static String toDisplayString( | ||
Object arrayLike, boolean allowSideEffects, InteropLibrary iop) { | ||
StringBuilder sb = new StringBuilder(); | ||
try { | ||
sb.append('['); | ||
String sep = ""; | ||
long len = iop.getArraySize(arrayLike); | ||
for (long i = 0; i < len; i++) { | ||
sb.append(sep); | ||
|
||
Object at = iop.readArrayElement(arrayLike, i); | ||
Object str = showObject(iop, allowSideEffects, at); | ||
if (iop.isString(str)) { | ||
sb.append(iop.asString(str)); | ||
} else { | ||
sb.append("_"); | ||
} | ||
sep = ", "; | ||
} | ||
sb.append(']'); | ||
} catch (InvalidArrayIndexException | UnsupportedMessageException ex) { | ||
StringWriter w = new StringWriter(); | ||
ex.printStackTrace(new PrintWriter(w)); | ||
sb.append("...\n").append(w); | ||
} | ||
return sb.toString(); | ||
} | ||
|
||
private static Object showObject(InteropLibrary iop, boolean allowSideEffects, Object child) | ||
throws UnsupportedMessageException { | ||
if (child == null) { | ||
return "null"; | ||
} else if (child instanceof Boolean) { | ||
return (boolean) child ? "True" : "False"; | ||
} else { | ||
return iop.toDisplayString(child, allowSideEffects); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.