Skip to content

Commit

Permalink
fix boundary
Browse files Browse the repository at this point in the history
  • Loading branch information
arvyy committed May 12, 2024
1 parent 8005808 commit aa5e18e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,7 @@ int bigintValue(LispBigInteger arg) {
@Specialization
Object executeArray(LispArray arr, int[] lookup) {
if (arr.dimensions() != lookup.length) {
return errorSignalerNode.signalDomainError(
"Expected dimension size " + arr.dimensions() + "; received" + lookup.length,
arr,
ISLISPContext.get(this).lookupClass("<basic-array*>"));
return signalWrongDimension(arr, arr.dimensions(), lookup.length);
}
Object obj = arr.data();
for (var i: lookup) {
Expand All @@ -72,10 +69,7 @@ Object executeArray(LispArray arr, int[] lookup) {
@Specialization
Object executeVector(LispVector vec, int[] lookup) {
if (lookup.length != 1) {
return errorSignalerNode.signalDomainError(
"Expected dimension size 1; received" + lookup.length,
vec,
ISLISPContext.get(this).lookupClass("<basic-vector>"));
return signalWrongDimension(vec, 1, lookup.length);
}
var index = lookup[0];
if (index < 0 || index >= vec.values().length) {
Expand All @@ -87,10 +81,7 @@ Object executeVector(LispVector vec, int[] lookup) {
@Specialization
Object executeString(String s, int[] lookup) {
if (lookup.length != 1) {
return errorSignalerNode.signalDomainError(
"Expected dimension size 1; received" + lookup.length,
s,
ISLISPContext.get(this).lookupClass("<string>"));
return signalWrongDimension(s, 1, lookup.length);
}
var index = lookup[0];
if (index < 0 || index >= s.length()) {
Expand All @@ -103,10 +94,7 @@ Object executeString(String s, int[] lookup) {
@Specialization
Object executeMutableString(LispMutableString s, int[] lookup) {
if (lookup.length != 1) {
return errorSignalerNode.signalDomainError(
"Expected dimension size 1; received" + lookup.length,
s,
ISLISPContext.get(this).lookupClass("<string>"));
return signalWrongDimension(s, 1, lookup.length);
}
var index = lookup[0];
if (index < 0 || index >= s.chars().length) {
Expand All @@ -115,6 +103,14 @@ Object executeMutableString(LispMutableString s, int[] lookup) {
return s.chars()[index];
}

@CompilerDirectives.TruffleBoundary
Object signalWrongDimension(Object obj, int expected, int actual) {
return errorSignalerNode.signalDomainError(
"Expected dimension size " + expected + "; received " + actual,
obj,
ISLISPContext.get(this).lookupClass("<basic-array>"));
}

@Fallback
Object fallback(Object arr, int[] lookup) {
var ctx = ISLISPContext.get(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,7 @@ int bigIntValue(LispBigInteger arg) {
@Specialization
Object doArray(Object value, LispArray arr, int[] lookup) {
if (arr.dimensions() != lookup.length) {
return errorSignalerNode.signalDomainError(
"Expected dimension size " + arr.dimensions() + "; received" + lookup.length,
arr,
ISLISPContext.get(this).lookupClass("<basic-array*>"));
return signalWrongDimension(arr, arr.dimensions(), lookup.length);
}
Object[] obj = arr.data();
for (var i = 0; i < lookup.length - 1; i++) {
Expand All @@ -82,10 +79,7 @@ Object doArray(Object value, LispArray arr, int[] lookup) {
@Specialization
Object doVector(Object value, LispVector vec, int[] lookup) {
if (lookup.length != 1) {
return errorSignalerNode.signalDomainError(
"Expected dimension size 1; received" + lookup.length,
vec,
ISLISPContext.get(this).lookupClass("<basic-vector>"));
return signalWrongDimension(vec, 1, lookup.length);
}
var index = lookup[0];
if (index < 0 || index >= vec.values().length) {
Expand All @@ -106,10 +100,7 @@ Object doString(Object value, String str, int[] lookup) {
@Specialization
Object doMutableString(LispChar c, LispMutableString str, int[] lookup) {
if (lookup.length != 1) {
return errorSignalerNode.signalDomainError(
"Expected dimension size 1; received" + lookup.length,
str,
ISLISPContext.get(this).lookupClass("<string>"));
return signalWrongDimension(str, 1, lookup.length);
}
var index = lookup[0];
str.chars()[index] = c;
Expand All @@ -124,10 +115,7 @@ Object doTruffleVector(
@CachedLibrary("o") InteropLibrary interop
) {
if (lookup.length != 1) {
return errorSignalerNode.signalDomainError(
"Expected dimension size 1; received" + lookup.length,
o,
ISLISPContext.get(this).lookupClass("<string>"));
return signalWrongDimension(o, 1, lookup.length);
}
try {
interop.writeArrayElement(o, lookup[0], value);
Expand All @@ -143,6 +131,14 @@ Object fallback(Object value, Object arr, int[] lookup) {
return errorSignalerNode.signalWrongType(arr, ctx.lookupClass("<basic-array>"));
}

@CompilerDirectives.TruffleBoundary
Object signalWrongDimension(Object obj, int expected, int actual) {
return errorSignalerNode.signalDomainError(
"Expected dimension size " + expected + "; received " + actual,
obj,
ISLISPContext.get(this).lookupClass("<basic-array>"));
}

/**
* Construct LispFunction using this root node.
*
Expand Down

0 comments on commit aa5e18e

Please sign in to comment.