Skip to content

Commit

Permalink
[#616] Fix java generator to check parameters in packed write
Browse files Browse the repository at this point in the history
  • Loading branch information
Mi-La committed May 30, 2024
1 parent 473db83 commit 73be22d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
27 changes: 16 additions & 11 deletions compiler/extensions/java/freemarker/CompoundField.inc.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,8 @@ ${I}<@field_member_name field/> = new ${field.typeInfo.typeFullName}(in<#rt>
</#macro>


<#macro compound_write_field_offset_check field compoundName indent>
<#macro compound_check_offset_field field compoundName indent>
<#local I>${""?left_pad(indent * 4)}</#local>
${I}out.alignTo(java.lang.Byte.SIZE);
${I}if (out.getBytePosition() != <@compound_field_get_offset field/>)
${I}{
${I} throw new zserio.runtime.ZserioError("Write: Wrong offset for field ${compoundName}.${field.name}: " +
Expand Down Expand Up @@ -227,8 +226,11 @@ ${I}}
${I}out.alignTo(${field.alignmentValue});
</#if>
<#if field.offset?? && !field.offset.containsIndex>
<@compound_write_field_offset_check field, compoundName, indent/>
${I}out.alignTo(java.lang.Byte.SIZE);
<@compound_check_offset_field field, compoundName, indent/>
</#if>
<@compound_check_array_length_field field, compoundName, indent/>
<@compound_check_parameterized_field field, compoundName, indent/>
<#if packed && field.isPackable && !field.array??>
<#if field.typeInfo.isIntegral>
${I}zserioContext.${field.getterName}().write(<@array_traits field/>, out,
Expand All @@ -237,19 +239,11 @@ ${I} new ${field.typeInfo.arrayableInfo.arrayElement}(<@compound_get_fiel
${I}<@compound_get_field field/>.write(zserioContext.${field.getterName}(), out);
</#if>
<#elseif field.array??>
<#if field.array.length??>
${I}if (<@compound_get_field field/>.size() != (int)(${field.array.length}))
${I}{
${I} throw new zserio.runtime.ZserioError("Write: Wrong array length for field ${compoundName}.${field.name}: " +
${I} <@compound_get_field field/>.size() + " != " + (int)(${field.array.length}) + "!");
${I}}
</#if>
${I}<@compound_get_field field/>.write<@array_field_packed_suffix field, packed/>(out);
<#elseif field.runtimeFunction??>
${I}out.write${field.runtimeFunction.suffix}(<@compound_get_field field/><#if field.runtimeFunction.arg??>, ${field.runtimeFunction.arg}</#if>);
<#else>
<#-- enum or compound -->
<@compound_check_parameterized_field field, compoundName, indent/>
${I}<@compound_get_field field/>.write(out);
</#if>
</#macro>
Expand All @@ -262,6 +256,17 @@ ${I} throw new zserio.runtime.ConstraintError("Constraint violated at ${compo
</#if>
</#macro>

<#macro compound_check_array_length_field field compoundName indent>
<#local I>${""?left_pad(indent * 4)}</#local>
<#if field.array?? && field.array.length??>
${I}if (<@compound_get_field field/>.size() != (int)(${field.array.length}))
${I}{
${I} throw new zserio.runtime.ZserioError("Write: Wrong array length for field ${compoundName}.${field.name}: " +
${I} <@compound_get_field field/>.size() + " != " + (int)(${field.array.length}) + "!");
${I}}
</#if>
</#macro>

<#macro compound_check_parameterized_field field compoundName indent>
<#local I>${""?left_pad(indent * 4)}</#local>
<#if field.compound?? && field.compound.instantiatedParameters?has_content>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ ${I}zserio_writer.alignto(${field.alignmentValue})
</#if>
<#if field.offset?? && !field.offset.containsIndex>
${I}zserio_writer.alignto(8)
<@compound_check_offset_field field, compoundName, "zserio_writer.bitposition", indent/>
<@compound_check_offset_field field, compoundName, "zserio_writer.bitposition", indent/>
</#if>
<@compound_check_constraint_field field, compoundName, indent/>
<@compound_check_array_length_field field, compoundName, indent/>
Expand Down

0 comments on commit 73be22d

Please sign in to comment.