use of com.oracle.truffle.api.strings.TruffleString in project graal by oracle.
the class TStringLastByteIndexOfStringTest method testWithMask.
@Test
public void testWithMask() throws Exception {
String javaStrA = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int[] codepointsA = toIntArray(javaStrA);
TruffleString strA = TruffleString.fromJavaStringUncached(javaStrA, TruffleString.Encoding.UTF_16);
TruffleString strB = TruffleString.fromJavaStringUncached("abc", TruffleString.Encoding.UTF_16);
TruffleString.WithMask[] withMask = { TruffleString.WithMask.createUncached(strB.switchEncodingUncached(TruffleString.Encoding.UTF_8), new byte[] { 0x20, 0x20, 0x20 }, TruffleString.Encoding.UTF_8), TruffleString.WithMask.createUTF16Uncached(strB.switchEncodingUncached(TruffleString.Encoding.UTF_16), new char[] { 0x20, 0x20, 0x20 }), TruffleString.WithMask.createUTF32Uncached(strB.switchEncodingUncached(TruffleString.Encoding.UTF_32), new int[] { 0x20, 0x20, 0x20 }) };
TruffleString.Encoding[] encodings = { TruffleString.Encoding.UTF_8, TruffleString.Encoding.UTF_16, TruffleString.Encoding.UTF_32 };
for (int i = 0; i < encodings.length; i++) {
TruffleString.Encoding encoding = encodings[i];
byte[] arr = new byte[strA.byteLength(encoding)];
strA.switchEncodingUncached(encoding).copyToByteArrayNodeUncached(0, arr, 0, arr.length, encoding);
int iFinal = i;
checkStringVariants(arr, TruffleString.CodeRange.ASCII, true, encoding, codepointsA, null, (a, array, codeRange, isValid, enc, codepoints, byteIndices) -> {
Assert.assertEquals(0, node.execute(a, withMask[iFinal], array.length, 0, encoding));
});
}
}
use of com.oracle.truffle.api.strings.TruffleString in project graal by oracle.
the class TStringForceEncodingTest method testAll.
@Test
public void testAll() throws Exception {
forAllStrings(true, (a, array, codeRange, isValid, encoding, codepoints, byteIndices) -> {
for (TruffleString.Encoding targetEncoding : TruffleString.Encoding.values()) {
if (targetEncoding == TruffleString.Encoding.UTF_32 && (array.length & 3) != 0 || targetEncoding == TruffleString.Encoding.UTF_16 && (array.length & 1) != 0) {
expectIllegalArgumentException(() -> node.execute(a, encoding, targetEncoding));
expectIllegalArgumentException(() -> nodeMutable.execute(a, encoding, targetEncoding));
} else {
TruffleString b = node.execute(a, encoding, targetEncoding);
MutableTruffleString bMutable = nodeMutable.execute(a, encoding, targetEncoding);
if (a instanceof MutableTruffleString && encoding == targetEncoding) {
Assert.assertSame(a, bMutable);
}
assertBytesEqual(b, targetEncoding, array);
assertBytesEqual(bMutable, targetEncoding, array);
}
}
});
}
use of com.oracle.truffle.api.strings.TruffleString in project graal by oracle.
the class TStringRepeatTest method testAll.
@Test
public void testAll() throws Exception {
forAllStrings(true, (a, array, codeRange, isValid, encoding, codepoints, byteIndices) -> {
Assert.assertTrue(node.execute(a, 0, encoding).isEmpty());
if (a instanceof TruffleString) {
Assert.assertSame(a, node.execute(a, 1, encoding));
}
int n = 3;
TruffleString b = node.execute(a, n, encoding);
if (isValid) {
Assert.assertEquals(array.length * n, b.byteLength(encoding));
Assert.assertEquals(codepoints.length * n, b.codePointLengthUncached(encoding));
Assert.assertEquals(codeRange, b.getCodeRangeUncached(encoding));
}
byte[] cmp = new byte[array.length * 3];
b.copyToByteArrayNodeUncached(0, cmp, 0, cmp.length, encoding);
for (int i = 0; i < n; i++) {
int offset = array.length * i;
for (int j = 0; j < array.length; j++) {
Assert.assertEquals(array[j], cmp[offset + j]);
}
}
});
}
use of com.oracle.truffle.api.strings.TruffleString in project graal by oracle.
the class TStringToJavaStringTest method testAll.
@Test
public void testAll() throws Exception {
forAllStrings(true, (a, array, codeRange, isValid, encoding, codepoints, byteIndices) -> {
String s = node.execute(a);
if (codeRange == TruffleString.CodeRange.ASCII || isUTF(encoding)) {
TruffleStringIterator it = a.createCodePointIteratorUncached(encoding);
int i = 0;
while (it.hasNext()) {
int expected = s.codePointAt(i);
Assert.assertEquals(expected, it.nextUncached());
i += expected > 0xffff ? 2 : 1;
}
}
if (a instanceof TruffleString) {
Assert.assertTrue(InteropLibrary.getUncached(a).isString(a));
Assert.assertEquals(s, InteropLibrary.getUncached(a).asString(a));
}
if (encoding == TruffleString.Encoding.BYTES && codeRange != TruffleString.CodeRange.ASCII) {
StringBuilder sb = new StringBuilder(array.length * 4);
for (byte b : array) {
if (b < 0) {
sb.append(String.format("\\x%02x", b));
} else {
sb.append((char) b);
}
}
Assert.assertEquals(sb.toString(), a.toString());
} else {
Assert.assertEquals(s, a.toString());
}
});
}
use of com.oracle.truffle.api.strings.TruffleString in project graal by oracle.
the class SLLanguage method lookupBuiltin.
public RootCallTarget lookupBuiltin(NodeFactory<? extends SLBuiltinNode> factory) {
RootCallTarget target = builtinTargets.get(factory);
if (target != null) {
return target;
}
/*
* The builtin node factory is a class that is automatically generated by the Truffle DSL.
* The signature returned by the factory reflects the signature of the @Specialization
*
* methods in the builtin classes.
*/
int argumentCount = factory.getExecutionSignature().size();
SLExpressionNode[] argumentNodes = new SLExpressionNode[argumentCount];
/*
* Builtin functions are like normal functions, i.e., the arguments are passed in as an
* Object[] array encapsulated in SLArguments. A SLReadArgumentNode extracts a parameter
* from this array.
*/
for (int i = 0; i < argumentCount; i++) {
argumentNodes[i] = new SLReadArgumentNode(i);
}
/* Instantiate the builtin node. This node performs the actual functionality. */
SLBuiltinNode builtinBodyNode = factory.createNode((Object) argumentNodes);
builtinBodyNode.addRootTag();
/* The name of the builtin function is specified via an annotation on the node class. */
TruffleString name = SLStrings.fromJavaString(lookupNodeInfo(builtinBodyNode.getClass()).shortName());
builtinBodyNode.setUnavailableSourceSection();
/* Wrap the builtin in a RootNode. Truffle requires all AST to start with a RootNode. */
SLRootNode rootNode = new SLRootNode(this, new FrameDescriptor(), builtinBodyNode, BUILTIN_SOURCE.createUnavailableSection(), name);
/*
* Register the builtin function in the builtin registry. Call targets for builtins may be
* reused across multiple contexts.
*/
RootCallTarget newTarget = rootNode.getCallTarget();
RootCallTarget oldTarget = builtinTargets.putIfAbsent(factory, newTarget);
if (oldTarget != null) {
return oldTarget;
}
return newTarget;
}
Aggregations