-
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.
Slicing node to handle Array as well as polyglot objects
- Loading branch information
1 parent
415623d
commit 9b1229f
Showing
3 changed files
with
79 additions
and
17 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
...ain/java/org/enso/interpreter/node/expression/builtin/immutable/SliceArrayVectorNode.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,44 @@ | ||
package org.enso.interpreter.node.expression.builtin.immutable; | ||
|
||
import org.enso.interpreter.dsl.BuiltinMethod; | ||
import org.enso.interpreter.runtime.EnsoContext; | ||
import org.enso.interpreter.runtime.data.Array; | ||
import org.enso.interpreter.runtime.error.PanicException; | ||
|
||
import com.oracle.truffle.api.CompilerDirectives; | ||
import com.oracle.truffle.api.dsl.Specialization; | ||
import com.oracle.truffle.api.interop.InteropLibrary; | ||
import com.oracle.truffle.api.interop.UnsupportedMessageException; | ||
import com.oracle.truffle.api.library.CachedLibrary; | ||
import com.oracle.truffle.api.nodes.Node; | ||
|
||
@BuiltinMethod(type = "Array", name = "slice", description = "Returns a slice of this Array.") | ||
public abstract class SliceArrayVectorNode extends Node { | ||
SliceArrayVectorNode() {} | ||
|
||
public static SliceArrayVectorNode build() { | ||
return SliceArrayVectorNodeGen.create(); | ||
} | ||
|
||
abstract Object execute(Object self, long start, long end); | ||
|
||
@Specialization | ||
Object executeArray(Array self, long start, long end) { | ||
return Array.slice(self, start, end, self.length()); | ||
} | ||
|
||
@Specialization(replaces = "executeArray") | ||
Object executeArrayLike( | ||
Object self, long start, long end, @CachedLibrary(limit = "3") InteropLibrary iop) { | ||
try { | ||
long len = iop.getArraySize(self); | ||
return Array.slice(self, start, end, len); | ||
} catch (UnsupportedMessageException ex) { | ||
CompilerDirectives.transferToInterpreter(); | ||
var ctx = EnsoContext.get(this); | ||
var arrayType = ctx.getBuiltins().array(); | ||
throw new PanicException( | ||
ctx.getBuiltins().error().makeTypeError(arrayType, self, "self"), this); | ||
} | ||
} | ||
} |
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