Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions bench/src/sjsonnet/bench/RegressionBenchmark.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ object RegressionBenchmark {

@BenchmarkMode(Array(Mode.AverageTime))
@Fork(1)
@Warmup(iterations = 1, time = 2)
@Measurement(iterations = 1)
@Warmup(iterations = 3, time = 2)
@Measurement(iterations = 3)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@State(Scope.Benchmark)
class RegressionBenchmark {
Expand Down
11 changes: 10 additions & 1 deletion sjsonnet/src/sjsonnet/stdlib/EncodingModule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,16 @@ object EncodingModule extends AbstractFunctionModule {
},
builtin("base64DecodeBytes", "str") { (pos, _, str: String) =>
try {
Val.Arr(pos, Base64.getDecoder.decode(str).map(i => Val.Num(pos, i)))
val decoded = Base64.getDecoder.decode(str)
val result = new Array[Eval](decoded.length)
var i = 0
while (i < decoded.length) {
// Mask with 0xff to convert signed Java byte (-128..127) to unsigned int (0..255),
// matching the Jsonnet specification that byte values are in the range 0-255.
result(i) = Val.Num(pos, decoded(i) & 0xff)
i += 1
}
Val.Arr(pos, result)
} catch {
case e: IllegalArgumentException =>
Error.fail("Invalid base64 string: " + e.getMessage)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Regression test: base64DecodeBytes must return unsigned byte values (0-255).
// Java's byte type is signed (-128..127), so without masking with 0xff,
// bytes >= 128 would appear as negative numbers.
local bytes = std.base64DecodeBytes("gIA="); // decodes to [0x80, 0x80] = [128, 128]

std.assertEqual(bytes[0], 128) &&
std.assertEqual(bytes[1], 128) &&

// Test full range: 0xFF = 255
local highByte = std.base64DecodeBytes("/w=="); // decodes to [0xFF] = [255]
std.assertEqual(highByte[0], 255) &&

// Test zero byte
local zeroByte = std.base64DecodeBytes("AA=="); // decodes to [0x00] = [0]
std.assertEqual(zeroByte[0], 0) &&

// Round-trip test: encode then decode should preserve all byte values including high bytes
local mixed = [0, 127, 128, 200, 255];
std.assertEqual(std.base64DecodeBytes(std.base64(mixed)), mixed) &&

true
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
true