diff --git a/jvm/src/test/scala/io/kaitai/struct/translators/TranslatorSpec.scala b/jvm/src/test/scala/io/kaitai/struct/translators/TranslatorSpec.scala index 8db8d279a..353e36418 100644 --- a/jvm/src/test/scala/io/kaitai/struct/translators/TranslatorSpec.scala +++ b/jvm/src/test/scala/io/kaitai/struct/translators/TranslatorSpec.scala @@ -406,7 +406,7 @@ class TranslatorSpec extends AnyFunSpec { CSharpCompiler -> "new byte[] { 34, 0, 10, 64, 65, 66, 92 }", GoCompiler -> "[]uint8{34, 0, 10, 64, 65, 66, 92}", JavaCompiler -> "new byte[] { 34, 0, 10, 64, 65, 66, 92 }", - JavaScriptCompiler -> "[34, 0, 10, 64, 65, 66, 92]", + JavaScriptCompiler -> "new Uint8Array([34, 0, 10, 64, 65, 66, 92])", LuaCompiler -> "\"\\034\\000\\010\\064\\065\\066\\092\"", PerlCompiler -> "pack('C*', (34, 0, 10, 64, 65, 66, 92))", PHPCompiler -> "\"\\x22\\x00\\x0A\\x40\\x41\\x42\\x5C\"", @@ -419,7 +419,7 @@ class TranslatorSpec extends AnyFunSpec { CSharpCompiler -> "new byte[] { 255, 0, 255 }", GoCompiler -> "[]uint8{255, 0, 255}", JavaCompiler -> "new byte[] { -1, 0, -1 }", - JavaScriptCompiler -> "[255, 0, 255]", + JavaScriptCompiler -> "new Uint8Array([255, 0, 255])", LuaCompiler -> "\"\\255\\000\\255\"", PerlCompiler -> "pack('C*', (255, 0, 255))", PHPCompiler -> "\"\\xFF\\x00\\xFF\"", @@ -434,7 +434,7 @@ class TranslatorSpec extends AnyFunSpec { CSharpCompiler -> "new byte[] { 0, 1, 2 }.Length", GoCompiler -> "len([]uint8{0, 1, 2})", JavaCompiler -> "new byte[] { 0, 1, 2 }.length", - JavaScriptCompiler -> "[0, 1, 2].length", + JavaScriptCompiler -> "new Uint8Array([0, 1, 2]).length", LuaCompiler -> "#\"\\000\\001\\002\"", PerlCompiler -> "length(pack('C*', (0, 1, 2)))", PHPCompiler -> "strlen(\"\\x00\\x01\\x02\")", @@ -816,7 +816,7 @@ class TranslatorSpec extends AnyFunSpec { CSharpCompiler -> "new byte[] { }", GoCompiler -> "[]uint8{}", JavaCompiler -> "new byte[] { }", - JavaScriptCompiler -> "[]", + JavaScriptCompiler -> "new Uint8Array([])", LuaCompiler -> "\"\"", PerlCompiler -> "pack('C*', ())", PHPCompiler -> "\"\"", diff --git a/shared/src/main/scala/io/kaitai/struct/translators/JavaScriptTranslator.scala b/shared/src/main/scala/io/kaitai/struct/translators/JavaScriptTranslator.scala index 867282002..d05440f8d 100644 --- a/shared/src/main/scala/io/kaitai/struct/translators/JavaScriptTranslator.scala +++ b/shared/src/main/scala/io/kaitai/struct/translators/JavaScriptTranslator.scala @@ -8,6 +8,8 @@ import io.kaitai.struct.format.{EnumSpec, Identifier} import io.kaitai.struct.languages.JavaScriptCompiler class JavaScriptTranslator(provider: TypeProvider, importList: ImportList) extends BaseTranslator(provider) { + override def doByteArrayLiteral(arr: Seq[Byte]): String = + s"new Uint8Array([${arr.map(_ & 0xff).mkString(", ")}])" override def doByteArrayNonLiteral(elts: Seq[Ast.expr]): String = s"new Uint8Array([${elts.map(translate).mkString(", ")}])"