Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update KaitaiStruct compiler and runtime to 0.9 #16

Merged
merged 1 commit into from
Oct 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<groupId>groupId</groupId>
<artifactId>kaitai_struct_visualizer_java</artifactId>
<packaging>jar</packaging>
<version>0.8-SNAPSHOT</version>
<version>0.10-SNAPSHOT</version>

<url>http://kaitai.io</url>

Expand All @@ -32,12 +32,12 @@
<dependency>
<groupId>io.kaitai</groupId>
<artifactId>kaitai-struct-runtime</artifactId>
<version>0.8</version>
<version>0.9</version>
</dependency>
<dependency>
<groupId>io.kaitai</groupId>
<artifactId>kaitai-struct-compiler_2.12</artifactId>
<version>0.8</version>
<version>0.9</version>
</dependency>
<dependency>
<groupId>com.github.Mingun</groupId>
Expand Down
19 changes: 15 additions & 4 deletions src/main/java/io/kaitai/struct/visualizer/VisualizerPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import io.kaitai.struct.ByteBufferKaitaiStream;
import io.kaitai.struct.CompileLog;
import io.kaitai.struct.JavaRuntimeConfig;
import io.kaitai.struct.KaitaiStream;
import io.kaitai.struct.KaitaiStruct;
import io.kaitai.struct.Main;
Expand Down Expand Up @@ -39,6 +40,7 @@
import tv.porst.jhexview.SimpleDataProvider;

public class VisualizerPanel extends JPanel {
/** Package to generate classes in. */
private static final String DEST_PACKAGE = "io.kaitai.struct.visualized";
/**
* Regexp with 2 groups: class name and type parameters. Type parameters
Expand Down Expand Up @@ -127,14 +129,23 @@ private static String compileKSY(String ksyFileName) {
final JavaClassSpecs specs = new JavaClassSpecs(null, null, spec);

final RuntimeConfig config = new RuntimeConfig(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like this entire invocation, but I understand that Java does not support default parameter values, so it cannot be made much better. Assigning null to everything can cause errors if there would be an attempt to use the config for some other compiler class than JavaCompiler, but so far, so good.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The config is intended to use with the JavaCompiler only, there is not supposed to call it for other languages, we need it is only for getting the Java code for compilation.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The config is intended to use with the JavaCompiler only, there is not supposed to call it for other languages, we need it is only for getting the Java code for compilation.

I understand, and the actual form looks kind of OK given the circumstances, but there is a hidden assumption that the RuntimeConfig object will tolerate being in partially-invalid state (for example in Scala, it would not be possible to pass null to a param with type String or CppRuntimeConfig).

We know this is not a problem because we can inspect the compiler code and find out that the JavaCompiler only reads properties config.{java,readStoresPos,autoRead}, Main.importAndPrecompile() only reads config.opaqueTypes and Main.compile() only passes the config to JavaCompiler (evenvutally overriding debug if /meta/ks-debug is set). But I don't like that these assumptions are hidden. If you could write a short comment that such initialized RuntimeConfig can only be used to initialize JavaCompiler, I would be more happy about that.

In an ideal world, Java would support default parameter values and named parameters, so you could do just new RuntimeConfig(autoRead = false, readStoresPos = true, opaqueTypes = true, java = new JavaRuntimeConfig(...)), not care about other values and they would be filled with sensible defaults set in RuntimeConfig.scala:91-103 - so it would be safe. But this is unfortunately not the case.

true, // debug - required for existing _attrStart/_attrEnd/_arrStart/_arrEnd fields
false,// autoRead - do not call `_read` automatically in constructor
true, // readStoresPos - enable generation of a position info which is accessed in DebugAids later
true, // opaqueTypes
null, // cppConfig
null, // goPackage
DEST_PACKAGE,
"io.kaitai.struct.ByteBufferKaitaiStream",
new JavaRuntimeConfig(
DEST_PACKAGE,
// Class to be invoked in `fromFile` helper methods
"io.kaitai.struct.ByteBufferKaitaiStream",
// Exception class expected to be thrown on end-of-stream errors
"java.nio.BufferUnderflowException"
),
null, // dotNetNamespace
null, // phpNamespace
null // pythonPackage
null, // pythonPackage
null, // nimModule
null // nimOpaque
);

Main.importAndPrecompile(specs, config).value();
Expand Down