diff --git a/pkl-core/src/main/java/org/pkl/core/ast/ByteConstantValueNode.java b/pkl-core/src/main/java/org/pkl/core/ast/ByteConstantValueNode.java index 314da9604..2b3d45852 100644 --- a/pkl-core/src/main/java/org/pkl/core/ast/ByteConstantValueNode.java +++ b/pkl-core/src/main/java/org/pkl/core/ast/ByteConstantValueNode.java @@ -1,5 +1,5 @@ /* - * Copyright © 2025 Apple Inc. and the Pkl project authors. All rights reserved. + * Copyright © 2025-2026 Apple Inc. and the Pkl project authors. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -27,7 +27,7 @@ public class ByteConstantValueNode extends ExpressionNode implements ConstantNod @Override public Long getValue() { - return (long) value; + return Byte.toUnsignedLong(value); } public byte getByteValue() { diff --git a/pkl-core/src/main/java/org/pkl/core/ast/expression/generator/GeneratorForNode.java b/pkl-core/src/main/java/org/pkl/core/ast/expression/generator/GeneratorForNode.java index 9a685be61..b67dfc63a 100644 --- a/pkl-core/src/main/java/org/pkl/core/ast/expression/generator/GeneratorForNode.java +++ b/pkl-core/src/main/java/org/pkl/core/ast/expression/generator/GeneratorForNode.java @@ -153,7 +153,7 @@ public abstract class GeneratorForNode extends GeneratorMemberNode { var bytes = iterable.getBytes(); loopConditionProfile.profileCounted(bytes.length); for (var idx = 0; loopConditionProfile.inject(idx < bytes.length); idx++) { - executeIteration(frame, parent, data, (long) idx, (long) bytes[idx]); + executeIteration(frame, parent, data, (long) idx, Byte.toUnsignedLong(bytes[idx])); } } diff --git a/pkl-core/src/main/java/org/pkl/core/runtime/VmBytes.java b/pkl-core/src/main/java/org/pkl/core/runtime/VmBytes.java index b274aa0b5..70f97c7a0 100644 --- a/pkl-core/src/main/java/org/pkl/core/runtime/VmBytes.java +++ b/pkl-core/src/main/java/org/pkl/core/runtime/VmBytes.java @@ -106,7 +106,7 @@ public final class VmBytes extends VmValue implements Iterable { } public long get(long index) { - return getBytes()[(int) index]; + return Byte.toUnsignedLong(getBytes()[(int) index]); } public VmBytes concatenate(VmBytes right) { diff --git a/pkl-core/src/main/java/org/pkl/core/stdlib/base/StringNodes.java b/pkl-core/src/main/java/org/pkl/core/stdlib/base/StringNodes.java index 5b84e7fe7..03731872e 100644 --- a/pkl-core/src/main/java/org/pkl/core/stdlib/base/StringNodes.java +++ b/pkl-core/src/main/java/org/pkl/core/stdlib/base/StringNodes.java @@ -221,6 +221,7 @@ public final class StringNodes { @TruffleBoundary protected VmList eval(String self) { var builder = VmList.EMPTY.builder(); + // signed cast is okay as 0x10ffff is max codepoint value: self.codePoints().forEach(cp -> builder.add((long) cp)); return builder.build(); } diff --git a/pkl-core/src/test/files/LanguageSnippetTests/input/basic/bytes.pkl b/pkl-core/src/test/files/LanguageSnippetTests/input/basic/bytes.pkl index 44c898c42..9fd682350 100644 --- a/pkl-core/src/test/files/LanguageSnippetTests/input/basic/bytes.pkl +++ b/pkl-core/src/test/files/LanguageSnippetTests/input/basic/bytes.pkl @@ -18,7 +18,7 @@ examples { 0b100, 0b101, 0b110, - 0b111 + 0b111, ) Bytes(0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7) Bytes(0o0, 0o1, 0o2, 0o3, 0o4, 0o5, 0o6, 0o7) @@ -62,9 +62,16 @@ examples { bytes1[1] bytes1[2] module.catch(() -> bytes1[3]) + Bytes(0xff)[0] == 0xff } ["all bytes"] { IntSeq(0, 255).toList().toBytes() } + + ["for generator"] { + for (byte in Bytes(0, 1, 0x7f, 0x80, 0xff)) { + byte.toString() + } + } } diff --git a/pkl-core/src/test/files/LanguageSnippetTests/output/basic/bytes.pcf b/pkl-core/src/test/files/LanguageSnippetTests/output/basic/bytes.pcf index e925f7861..dc2096cf2 100644 --- a/pkl-core/src/test/files/LanguageSnippetTests/output/basic/bytes.pcf +++ b/pkl-core/src/test/files/LanguageSnippetTests/output/basic/bytes.pcf @@ -39,8 +39,16 @@ examples { 2 3 "Element index `3` is out of range `0`..`2`. Value: Bytes(1, 2, 3)" + true } ["all bytes"] { Bytes(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255) } + ["for generator"] { + "0" + "1" + "127" + "128" + "255" + } }