Skip to content

Commit

Permalink
Lua: implement doByteArrayNonLiteral - create array from expression o…
Browse files Browse the repository at this point in the history
…f `bytes` type

Expressions of this type are created using:
- omitting the `type:` field in attributes and parse instances
- declaring parameters with `type: bytes`
- using `.as<bytes>` cast

Fixes test
```
[info]     - lua:[0 + 1, 5].as<bytes> *** FAILED ***
[info]       scala.NotImplementedError: an implementation is missing
[info]       at scala.Predef$.$qmark$qmark$qmark(Predef.scala:344)
[info]       at io.kaitai.struct.translators.BaseTranslator.doByteArrayNonLiteral(BaseTranslator.scala:179)
[info]       at io.kaitai.struct.translators.BaseTranslator.doByteArrayNonLiteral(BaseTranslator.scala:28)
[info]       at io.kaitai.struct.translators.CommonArraysAndCast.doByteArray(CommonArraysAndCast.scala:85)
[info]       at io.kaitai.struct.translators.CommonArraysAndCast.doCastOrArray(CommonArraysAndCast.scala:62)
[info]       at io.kaitai.struct.translators.CommonArraysAndCast.doCastOrArray$(CommonArraysAndCast.scala:53)
[info]       at io.kaitai.struct.translators.BaseTranslator.doCastOrArray(BaseTranslator.scala:28)
[info]       at io.kaitai.struct.translators.BaseTranslator.translate(BaseTranslator.scala:147)
[info]       at io.kaitai.struct.translators.AbstractTranslator.translate(AbstractTranslator.scala:25)
[info]       at io.kaitai.struct.translators.AbstractTranslator.translate$(AbstractTranslator.scala:25)
[info]       ...
```
  • Loading branch information
Mingun committed Aug 25, 2024
1 parent 75026ee commit 619d05d
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -859,7 +859,7 @@ class TranslatorSpec extends AnyFunSpec {
GoCompiler -> "[]uint8{0 + 1, 5}",
JavaCompiler -> "new byte[] { 0 + 1, 5 }",
JavaScriptCompiler -> "new Uint8Array([0 + 1, 5])",
LuaCompiler -> "???",
LuaCompiler -> "string.char(0 + 1, 5)",
PerlCompiler -> "pack('C*', (0 + 1, 5))",
PHPCompiler -> "pack('C*', 0 + 1, 5)",
PythonCompiler -> "struct.pack('2B', 0 + 1, 5)",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,14 @@ class LuaTranslator(provider: TypeProvider, importList: ImportList) extends Base

override def doBoolLiteral(n: Boolean): String =
if (n) "true" else "false"

override def doArrayLiteral(t: DataType, value: Seq[Ast.expr]): String =
"{" + value.map((v) => translate(v)).mkString(", ") + "}"
override def doByteArrayLiteral(arr: Seq[Byte]): String =
"\"" + decEscapeByteArray(arr) + "\""
override def doByteArrayNonLiteral(values: Seq[Ast.expr]): String =
// It is assumed that every expression produces integer in the range [0; 255]
"string.char(" + values.map(translate).mkString(", ") + ")"

override def doLocalName(s: String) = s match {
case Identifier.ITERATOR => "_"
Expand Down

0 comments on commit 619d05d

Please sign in to comment.