From aa5e18e66e575afbddeef199750781e80a051e74 Mon Sep 17 00:00:00 2001 From: Arvydas Silanskas Date: Sun, 12 May 2024 10:57:58 +0300 Subject: [PATCH] fix boundary --- .../arvyy/islisp/functions/ISLISPAref.java | 28 ++++++++----------- .../arvyy/islisp/functions/ISLISPSetAref.java | 28 ++++++++----------- 2 files changed, 24 insertions(+), 32 deletions(-) diff --git a/language/src/main/java/com/github/arvyy/islisp/functions/ISLISPAref.java b/language/src/main/java/com/github/arvyy/islisp/functions/ISLISPAref.java index 757d0b6..bc0f71b 100644 --- a/language/src/main/java/com/github/arvyy/islisp/functions/ISLISPAref.java +++ b/language/src/main/java/com/github/arvyy/islisp/functions/ISLISPAref.java @@ -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("")); + return signalWrongDimension(arr, arr.dimensions(), lookup.length); } Object obj = arr.data(); for (var i: lookup) { @@ -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("")); + return signalWrongDimension(vec, 1, lookup.length); } var index = lookup[0]; if (index < 0 || index >= vec.values().length) { @@ -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("")); + return signalWrongDimension(s, 1, lookup.length); } var index = lookup[0]; if (index < 0 || index >= s.length()) { @@ -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("")); + return signalWrongDimension(s, 1, lookup.length); } var index = lookup[0]; if (index < 0 || index >= s.chars().length) { @@ -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("")); + } + @Fallback Object fallback(Object arr, int[] lookup) { var ctx = ISLISPContext.get(this); diff --git a/language/src/main/java/com/github/arvyy/islisp/functions/ISLISPSetAref.java b/language/src/main/java/com/github/arvyy/islisp/functions/ISLISPSetAref.java index 9631871..1479242 100644 --- a/language/src/main/java/com/github/arvyy/islisp/functions/ISLISPSetAref.java +++ b/language/src/main/java/com/github/arvyy/islisp/functions/ISLISPSetAref.java @@ -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("")); + return signalWrongDimension(arr, arr.dimensions(), lookup.length); } Object[] obj = arr.data(); for (var i = 0; i < lookup.length - 1; i++) { @@ -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("")); + return signalWrongDimension(vec, 1, lookup.length); } var index = lookup[0]; if (index < 0 || index >= vec.values().length) { @@ -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("")); + return signalWrongDimension(str, 1, lookup.length); } var index = lookup[0]; str.chars()[index] = c; @@ -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("")); + return signalWrongDimension(o, 1, lookup.length); } try { interop.writeArrayElement(o, lookup[0], value); @@ -143,6 +131,14 @@ Object fallback(Object value, Object arr, int[] lookup) { return errorSignalerNode.signalWrongType(arr, ctx.lookupClass("")); } + @CompilerDirectives.TruffleBoundary + Object signalWrongDimension(Object obj, int expected, int actual) { + return errorSignalerNode.signalDomainError( + "Expected dimension size " + expected + "; received " + actual, + obj, + ISLISPContext.get(this).lookupClass("")); + } + /** * Construct LispFunction using this root node. *