Skip to content

Commit

Permalink
BaseTranslator: remove default doByteArrayLiteral() implementation
Browse files Browse the repository at this point in the history
Now that I've fixed the translation of byte array literals in
`JavaScriptTranslator` in the previous commit, which was the only place
where this default implementation of `doByteArrayLiteral()` was actually
used without being overridden (and it turned out to be a bug - it should
have been overridden there as well), I think it's best to remove it and
require each language-specific translator to provide the correct
implementation. The fact that all languages override
`BaseTranslator.doByteArrayLiteral()` proves that it is not a useful
implementation. On the contrary, it seems rather harmful: if
`BaseTranslator` had never implemented `doByteArrayLiteral()`, whoever
implemented `JavaScriptTranslator` would have been forced to implement it
explicitly, and thus the bug described and fixed in the previous commit
probably wouldn't have occurred at all.
  • Loading branch information
generalmimon committed Sep 28, 2024
1 parent ec064e3 commit cc4e73b
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ abstract class BaseTranslator(val provider: TypeProvider)
doIntLiteral(CommonSizeOf.getByteSizeOfClassSpec(cs))

def doArrayLiteral(t: DataType, value: Seq[Ast.expr]): String = "[" + value.map((v) => translate(v)).mkString(", ") + "]"
def doByteArrayLiteral(arr: Seq[Byte]): String = "[" + arr.map(_ & 0xff).mkString(", ") + "]"
def doByteArrayLiteral(arr: Seq[Byte]): String
def doByteArrayNonLiteral(elts: Seq[Ast.expr]): String = ???

def doLocalName(s: String): String = doName(s)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import io.kaitai.struct.languages.RubyCompiler
class RubyTranslator(provider: TypeProvider) extends BaseTranslator(provider)
with ByteArraysAsTrueArrays[String] {
override def doByteArrayLiteral(arr: Seq[Byte]): String =
s"${super.doByteArrayLiteral(arr)}.pack('C*')"
s"[${arr.map(_ & 0xff).mkString(", ")}].pack('C*')"
override def doByteArrayNonLiteral(elts: Seq[Ast.expr]): String =
s"[${elts.map(translate).mkString(", ")}].pack('C*')"

Expand Down

0 comments on commit cc4e73b

Please sign in to comment.