Skip to content

Commit

Permalink
Java: supply absolute offsets to streams, so they can correctly repor…
Browse files Browse the repository at this point in the history
…t positions for position-enabled code
  • Loading branch information
Mingun committed Oct 24, 2023
1 parent f4e1adb commit 71d1ed3
Show file tree
Hide file tree
Showing 16 changed files with 33 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ class CSharpCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig)
handleAssignment(varDest, expr, rep, false)
}

override def allocateIO(varName: Identifier, rep: RepeatSpec): String = {
override def allocateIO(varName: Identifier, rep: RepeatSpec, currentIo: String): String = {
val privateVarName = privateMemberName(varName)

val ioName = s"io_$privateVarName"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ class CppCompiler(
handleAssignment(varDest, expr, rep, false)
}

override def allocateIO(id: Identifier, rep: RepeatSpec): String = {
override def allocateIO(id: Identifier, rep: RepeatSpec, currentIo: String): String = {
val memberName = privateMemberName(id)
val ioId = IoStorageIdentifier(id)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ class GoCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig)
handleAssignment(varDest, ResultString(expr), rep, false)
}

override def allocateIO(varName: Identifier, rep: RepeatSpec): String = {
override def allocateIO(varName: Identifier, rep: RepeatSpec, currentIo: String): String = {
val javaName = privateMemberName(varName)

val ioName = idToStr(IoStorageIdentifier(varName))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ class JavaCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig)
handleAssignment(varDest, expr, rep, false)
}

override def allocateIO(varName: Identifier, rep: RepeatSpec): String = {
override def allocateIO(varName: Identifier, rep: RepeatSpec, currentIo: String): String = {
val ioName = idToStr(IoStorageIdentifier(varName))

val args = rep match {
Expand All @@ -285,7 +285,7 @@ class JavaCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig)
}

importList.add("io.kaitai.struct.ByteBufferKaitaiStream")
out.puts(s"$kstreamName $ioName = new ByteBufferKaitaiStream($args);")
out.puts(s"$kstreamName $ioName = new ByteBufferKaitaiStream($args, $currentIo.offset());")
ioName
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ class JavaScriptCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig)
handleAssignment(varDest, expr, rep, false)
}

override def allocateIO(varName: Identifier, rep: RepeatSpec): String = {
override def allocateIO(varName: Identifier, rep: RepeatSpec, currentIo: String): String = {
val langName = idToStr(varName)
val memberCall = privateMemberName(varName)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ class LuaCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig)
override def switchIfEnd(): Unit =
out.puts("end")

override def allocateIO(varName: Identifier, rep: RepeatSpec): String = {
override def allocateIO(varName: Identifier, rep: RepeatSpec, currentIo: String): String = {
val varStr = privateMemberName(varName)

val args = getRawIdExpr(varName, rep)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class NimCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig)
override def universalFooter: Unit = {
out.dec
}
override def allocateIO(id: Identifier, rep: RepeatSpec): String = {
override def allocateIO(id: Identifier, rep: RepeatSpec, currentIo: String): String = {
val ioName = s"${idToStr(id)}Io"
val arg = rep match {
case NoRepeat => idToStr(id) + "Expr"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ class PHPCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig)
handleAssignment(varDest, expr, rep, false)
}

override def allocateIO(id: Identifier, rep: RepeatSpec): String = {
override def allocateIO(id: Identifier, rep: RepeatSpec, currentIo: String): String = {
val memberName = privateMemberName(id)
val ioName = s"$$_io_${idToStr(id)}"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ class PerlCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig)
handleAssignment(varDest, expr, rep, false)
}

override def allocateIO(id: Identifier, rep: RepeatSpec): String = {
override def allocateIO(id: Identifier, rep: RepeatSpec, currentIo: String): String = {
val memberName = privateMemberName(id)

val args = rep match {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ class PythonCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig)

override def normalIO: String = "self._io"

override def allocateIO(varName: Identifier, rep: RepeatSpec): String = {
override def allocateIO(varName: Identifier, rep: RepeatSpec, currentIo: String): String = {
val varStr = privateMemberName(varName)
val ioName = s"_io_${idToStr(varName)}"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ class RubyCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig)
handleAssignment(varDest, expr, rep, false)
}

override def allocateIO(id: Identifier, rep: RepeatSpec): String = {
override def allocateIO(id: Identifier, rep: RepeatSpec, currentIo: String): String = {
val memberName = privateMemberName(id)
val ioName = s"_io_${idToStr(id)}"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ class RustCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig)
handleAssignment(varDest, expr, rep, false)
}

override def allocateIO(id: Identifier, rep: RepeatSpec): String = {
override def allocateIO(id: Identifier, rep: RepeatSpec, currentIo: String): String = {
val memberName = privateMemberName(id)

val args = rep match {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,14 @@ import io.kaitai.struct.format._
* keep track of allocated IOs.
*/
trait AllocateAndStoreIO extends ExtraAttrs {
def allocateIO(id: Identifier, rep: RepeatSpec): String
/**
* Generates code that create new input stream for read specified field content
*
* @param id Name of variable that used to store field for which stream is created
* @param rep Repeat specification for the [[id]] field
* @param currentIo Name of current stream variable, that used to read data currently
*/
def allocateIO(id: Identifier, rep: RepeatSpec, currentIo: String): String

override def extraAttrForIO(id: Identifier, rep: RepeatSpec): List[AttrSpec] =
List(AttrSpec(List(), IoStorageIdentifier(id), OwnedKaitaiStreamType, ConditionalSpec(None, rep)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@ import io.kaitai.struct.format.{AttrSpec, Identifier, RepeatSpec}
* probably garbage collector will deal with them.
*/
trait AllocateIOLocalVar extends ExtraAttrs {
def allocateIO(varName: Identifier, rep: RepeatSpec): String
/**
* Generates code that create new input stream for read specified field content
*
* @param id Name of variable that used to store field for which stream is created
* @param rep Repeat specification for the [[id]] field
* @param currentIo Name of current stream variable, that used to read data currently
*/
def allocateIO(varName: Identifier, rep: RepeatSpec, currentIo: String): String

override def extraAttrForIO(id: Identifier, rep: RepeatSpec): List[AttrSpec] = List()
}
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,9 @@ trait EveryReadIsExpression

this match {
case thisStore: AllocateAndStoreIO =>
thisStore.allocateIO(rawId, rep)
thisStore.allocateIO(rawId, rep, io)
case thisLocal: AllocateIOLocalVar =>
thisLocal.allocateIO(rawId, rep)
thisLocal.allocateIO(rawId, rep, io)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,9 @@ trait GoReads extends CommonReads with ObjectOrientedLanguage with GoSwitchOps {

this match {
case thisStore: AllocateAndStoreIO =>
thisStore.allocateIO(rawId, rep)
thisStore.allocateIO(rawId, rep, io)
case thisLocal: AllocateIOLocalVar =>
thisLocal.allocateIO(rawId, rep)
thisLocal.allocateIO(rawId, rep, io)
}
case _: UserTypeInstream =>
// no fixed buffer, just use regular IO
Expand Down

0 comments on commit 71d1ed3

Please sign in to comment.