Search in sources :

Example 16 with TruffleString

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));
        });
    }
}
Also used : TruffleString(com.oracle.truffle.api.strings.TruffleString) TruffleString(com.oracle.truffle.api.strings.TruffleString) Test(org.junit.Test)

Example 17 with TruffleString

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);
            }
        }
    });
}
Also used : TruffleString(com.oracle.truffle.api.strings.TruffleString) MutableTruffleString(com.oracle.truffle.api.strings.MutableTruffleString) MutableTruffleString(com.oracle.truffle.api.strings.MutableTruffleString) Test(org.junit.Test)

Example 18 with TruffleString

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]);
            }
        }
    });
}
Also used : TruffleString(com.oracle.truffle.api.strings.TruffleString) Test(org.junit.Test)

Example 19 with TruffleString

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());
        }
    });
}
Also used : TruffleString(com.oracle.truffle.api.strings.TruffleString) TruffleStringIterator(com.oracle.truffle.api.strings.TruffleStringIterator) TruffleString(com.oracle.truffle.api.strings.TruffleString) Test(org.junit.Test)

Example 20 with TruffleString

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;
}
Also used : FrameDescriptor(com.oracle.truffle.api.frame.FrameDescriptor) TruffleString(com.oracle.truffle.api.strings.TruffleString) SLExpressionNode(com.oracle.truffle.sl.nodes.SLExpressionNode) SLReadArgumentNode(com.oracle.truffle.sl.nodes.local.SLReadArgumentNode) SLRootNode(com.oracle.truffle.sl.nodes.SLRootNode) RootCallTarget(com.oracle.truffle.api.RootCallTarget) SLBuiltinNode(com.oracle.truffle.sl.builtins.SLBuiltinNode)

Aggregations

TruffleString (com.oracle.truffle.api.strings.TruffleString)39 Test (org.junit.Test)26 MutableTruffleString (com.oracle.truffle.api.strings.MutableTruffleString)16 AbstractTruffleString (com.oracle.truffle.api.strings.AbstractTruffleString)9 RootCallTarget (com.oracle.truffle.api.RootCallTarget)3 SLExpressionNode (com.oracle.truffle.sl.nodes.SLExpressionNode)3 SLRootNode (com.oracle.truffle.sl.nodes.SLRootNode)3 Encoding (org.graalvm.shadowed.org.jcodings.Encoding)3 FrameDescriptor (com.oracle.truffle.api.frame.FrameDescriptor)2 RootNode (com.oracle.truffle.api.nodes.RootNode)2 TruffleStringBuilder (com.oracle.truffle.api.strings.TruffleStringBuilder)2 TruffleStringIterator (com.oracle.truffle.api.strings.TruffleStringIterator)2 SLEvalRootNode (com.oracle.truffle.sl.nodes.SLEvalRootNode)2 SLStringLiteralNode (com.oracle.truffle.sl.nodes.expression.SLStringLiteralNode)2 BigInteger (java.math.BigInteger)2 CallTarget (com.oracle.truffle.api.CallTarget)1 TruffleBoundary (com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)1 Specialization (com.oracle.truffle.api.dsl.Specialization)1 Frame (com.oracle.truffle.api.frame.Frame)1 FrameInstance (com.oracle.truffle.api.frame.FrameInstance)1