Skip to content
Permalink

Comparing changes

This is a direct comparison between two commits made in this repository or its related repositories. View the default comparison for this range or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: kaitai-io/kaitai_struct_compiler
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 6efa01a6b9e1181ce59724e8386b066bc7c61cd0
Choose a base ref
..
head repository: kaitai-io/kaitai_struct_compiler
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 3e68dcc7ec6a1c1439352a9421ae65e661333ce7
Choose a head ref
Original file line number Diff line number Diff line change
@@ -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 -> "\"\"",
@@ -854,12 +854,12 @@ class TranslatorSpec extends AnyFunSpec {
describe("to do type enforcement") {
// type enforcement: casting to non-literal byte array
full("[0 + 1, 5].as<bytes>", CalcIntType, CalcBytesType, ResultMap(
CppCompiler -> "???",
CppCompiler -> "std::string({static_cast<char>(0 + 1), static_cast<char>(5)})",
CSharpCompiler -> "new byte[] { 0 + 1, 5 }",
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)",
Original file line number Diff line number Diff line change
@@ -77,16 +77,16 @@ class ResolveTypes(specs: ClassSpecs, topClass: ClassSpec, opaqueTypes: Boolean)
et.enumSpec = Some(ty)
None
} catch {
case _: TypeNotFoundError =>
Log.typeResolve.info(() => " => ??? (while resolving enum)")
Log.enumResolve.info(() => " => ??? (enclosing type not found)")
case ex: TypeNotFoundError =>
Log.typeResolve.info(() => s" => ??? (while resolving enum '${et.name}'): $ex")
Log.enumResolve.info(() => s" => ??? (enclosing type not found, enum '${et.name}'): $ex")
Some(TypeNotFoundErr(typePath, curClass, path :+ "enum"))
case _: EnumNotFoundError =>
Log.enumResolve.info(() => " => ???")
case ex: EnumNotFoundError =>
Log.enumResolve.info(() => s" => ??? (enum '${et.name}'): $ex")
Some(EnumNotFoundErr(et.name, curClass, path :+ "enum"))
}
case _ =>
Log.enumResolve.info(() => " => ??? (enum without name)")
Log.enumResolve.info(() => s" => ??? (enum '${et.name}' without name)")
// TODO: Maybe more specific error about empty name?
Some(EnumNotFoundErr(et.name, curClass, path :+ "enum"))
}
Original file line number Diff line number Diff line change
@@ -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)
Original file line number Diff line number Diff line change
@@ -113,12 +113,23 @@ class CppTranslator(provider: TypeProvider, importListSrc: CppImportList, import
// TODO: C++14
}
} else {
throw new RuntimeException("C++ literal arrays are not implemented yet")
throw new RuntimeException("literal arrays are not yet implemented for C++98 (pass `--cpp-standard 11` to target C++11)")
}
}

override def doByteArrayLiteral(arr: Seq[Byte]): String =
"std::string(\"" + Utils.hexEscapeByteArray(arr) + "\", " + arr.length + ")"
override def doByteArrayNonLiteral(values: Seq[Ast.expr]): String = {
// It is assumed that every expression produces integer in the range [0; 255]
if (config.cppConfig.useListInitializers) {
"std::string({" + values.map(value => s"static_cast<char>(${translate(value)})").mkString(", ") + "})"
} else {
// TODO: We need to produce an expression, but this is only possible using
// initializer lists or variadic templates (if we use a helper function),
// both of which are only available since C++11
throw new RuntimeException("non-literal byte arrays are not yet implemented for C++98 (pass `--cpp-standard 11` to target C++11)")
}
}

override def genericBinOp(left: Ast.expr, op: Ast.operator, right: Ast.expr, extPrec: Int) = {
(detectType(left), detectType(right), op) match {
Original file line number Diff line number Diff line change
@@ -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(", ")}])"

Original file line number Diff line number Diff line change
@@ -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 => "_"
Original file line number Diff line number Diff line change
@@ -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*')"