Skip to content

Commit a28bcfc

Browse files
committed
renaming
1 parent e900566 commit a28bcfc

File tree

4 files changed

+23
-27
lines changed

4 files changed

+23
-27
lines changed

changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Change log
99
- Added JBBPCustomFieldTypeProcessorAggregator, auxiliary class to join several JBBPCustomFieldTypeProcessors
1010
- Fixed JBBPTextWriter, added support of logging for JBBPAbstractField objects
1111
- Added support of payload objects in JBBPAbstractField
12+
- Improved inside script compiler and interpreter to support future extensions.
1213

1314
1.1.0
1415
- Added support to write mapped classes into JBBPOut

src/main/java/com/igormaznitsa/jbbp/JBBPParser.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ private List<JBBPAbstractField> parseStruct(final JBBPBitInputStream inStream, f
133133
}
134134

135135
final int c = compiled[positionAtCompiledBlock.getAndIncrement()] & 0xFF;
136-
final boolean noExtraField = (c & JBBPCompiler.FLAG_EXTRA_FLAGS) == 0;
136+
final boolean noExtraField = (c & JBBPCompiler.FLAG_WIDE) == 0;
137137
final int ec = noExtraField ? 0 : compiled[positionAtCompiledBlock.getAndIncrement()] & 0xFF;
138138

139139
final int code = (ec<<8) | c;
@@ -145,21 +145,21 @@ private List<JBBPAbstractField> parseStruct(final JBBPBitInputStream inStream, f
145145
final boolean wholeStreamArray;
146146
final int arrayLength;
147147
final int packedArraySizeOffset;
148-
switch (code & (JBBPCompiler.FLAG_ARRAY | (JBBPCompiler.EXTRAFLAG_EXPRESSION_OR_WHOLESTREAM<<8))) {
148+
switch (code & (JBBPCompiler.FLAG_ARRAY | (JBBPCompiler.EXT_FLAG_EXPRESSION_OR_WHOLESTREAM<<8))) {
149149
case JBBPCompiler.FLAG_ARRAY: {
150150
final int pos = positionAtCompiledBlock.get();
151151
arrayLength = JBBPUtils.unpackInt(compiled, positionAtCompiledBlock);
152152
packedArraySizeOffset = positionAtCompiledBlock.get() - pos;
153153
wholeStreamArray = false;
154154
}
155155
break;
156-
case (JBBPCompiler.EXTRAFLAG_EXPRESSION_OR_WHOLESTREAM << 8): {
156+
case (JBBPCompiler.EXT_FLAG_EXPRESSION_OR_WHOLESTREAM << 8): {
157157
wholeStreamArray = resultNotIgnored;
158158
packedArraySizeOffset = 0;
159159
arrayLength = 0;
160160
}
161161
break;
162-
case JBBPCompiler.FLAG_ARRAY | (JBBPCompiler.EXTRAFLAG_EXPRESSION_OR_WHOLESTREAM << 8): {
162+
case JBBPCompiler.FLAG_ARRAY | (JBBPCompiler.EXT_FLAG_EXPRESSION_OR_WHOLESTREAM << 8): {
163163
final JBBPIntegerValueEvaluator evaluator = this.compiledBlock.getArraySizeEvaluators()[positionAtVarLengthProcessors.getAndIncrement()];
164164
arrayLength = resultNotIgnored ? evaluator.eval(inStream, positionAtCompiledBlock.get(), this.compiledBlock, namedNumericFieldMap) : 0;
165165
packedArraySizeOffset = 0;

src/main/java/com/igormaznitsa/jbbp/compiler/JBBPCompiler.java

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -165,16 +165,15 @@ private StructStackItem(final int namedFieldCounter, final int startStructureOff
165165
public static final int FLAG_LITTLE_ENDIAN = 0x40;
166166

167167
/**
168-
* The Flag shows that next byte contains extra flags.
168+
* The Flag shows that the byte code is wide and contains extra byte in the next position of compiled block.
169169
*/
170-
public static final int FLAG_EXTRA_FLAGS = 0x80;
170+
public static final int FLAG_WIDE = 0x80;
171171

172172
/**
173-
* The Extra flag shows that the field is an array which size is defined
174-
* by an expression or the array is unsized and must be read till the end of a
173+
* The flag (placed only in the second byte of wide codes) shows that the field is an array which calculated size or unlimited and must be read till the end of a
175174
* stream.
176175
*/
177-
public static final int EXTRAFLAG_EXPRESSION_OR_WHOLESTREAM = 0x01;
176+
public static final int EXT_FLAG_EXPRESSION_OR_WHOLESTREAM = 0x01;
178177

179178
public static JBBPCompiledBlock compile(final String script) throws IOException {
180179
return compile(script, null);
@@ -223,13 +222,9 @@ public static JBBPCompiledBlock compile(final String script, final JBBPCustomFie
223222
out.write(code);
224223
offset++;
225224

226-
final int backOffset;
227-
if ((code & FLAG_EXTRA_FLAGS)!=0){
225+
if ((code & FLAG_WIDE)!=0){
228226
out.write(extracode);
229227
offset++;
230-
backOffset = 2;
231-
}else{
232-
backOffset = 1;
233228
}
234229

235230
StructStackItem currentClosedStructure = null;
@@ -393,7 +388,7 @@ public static JBBPCompiledBlock compile(final String script, final JBBPCustomFie
393388
}
394389

395390
if ((code & FLAG_ARRAY) != 0) {
396-
if ((extracode & EXTRAFLAG_EXPRESSION_OR_WHOLESTREAM) != 0) {
391+
if ((extracode & EXT_FLAG_EXPRESSION_OR_WHOLESTREAM) != 0) {
397392
if ("_".equals(token.getArraySizeAsString())) {
398393
if (fieldUnrestrictedArrayOffset >= 0) {
399394
throw new JBBPCompilationException("Detected two or more unlimited arrays [" + script + ']', token);
@@ -538,7 +533,7 @@ private static int prepareCodeForToken(final JBBPToken token, final JBBPCustomFi
538533
final JBBPFieldTypeParameterContainer descriptor = token.getFieldTypeParameters();
539534

540535
result = descriptor.getByteOrder() == JBBPByteOrder.LITTLE_ENDIAN ? FLAG_LITTLE_ENDIAN : 0;
541-
result |= token.getArraySizeAsString() == null ? 0 : (token.isVarArrayLength() ? FLAG_ARRAY | FLAG_EXTRA_FLAGS | (EXTRAFLAG_EXPRESSION_OR_WHOLESTREAM<<8) : FLAG_ARRAY);
536+
result |= token.getArraySizeAsString() == null ? 0 : (token.isVarArrayLength() ? FLAG_ARRAY | FLAG_WIDE | (EXT_FLAG_EXPRESSION_OR_WHOLESTREAM<<8) : FLAG_ARRAY);
542537
result |= token.getFieldName() == null ? 0 : FLAG_NAMED;
543538

544539
final String name = descriptor.getTypeName().toLowerCase(Locale.ENGLISH);
@@ -600,7 +595,7 @@ else if ("reset$$".equals(name)) {
600595
}
601596
break;
602597
case STRUCT_START: {
603-
result = token.getArraySizeAsString() == null ? 0 : (token.isVarArrayLength() ? FLAG_ARRAY | FLAG_EXTRA_FLAGS | (EXTRAFLAG_EXPRESSION_OR_WHOLESTREAM << 8) : FLAG_ARRAY);
598+
result = token.getArraySizeAsString() == null ? 0 : (token.isVarArrayLength() ? FLAG_ARRAY | FLAG_WIDE | (EXT_FLAG_EXPRESSION_OR_WHOLESTREAM << 8) : FLAG_ARRAY);
604599
result |= token.getFieldName() == null ? 0 : FLAG_NAMED;
605600
result |= CODE_STRUCT_START;
606601
}

src/test/java/com/igormaznitsa/jbbp/compiler/JBBPCompilerTest.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ public void testCompile_StructForWholeStreamAsSecondField() throws Exception {
4242
final JBBPCompiledBlock block = JBBPCompiler.compile("byte;test [_] {byte;}");
4343
assertEquals(6, block.getCompiledData().length);
4444
assertEquals(JBBPCompiler.CODE_BYTE, block.getCompiledData()[0]);
45-
assertEquals(JBBPCompiler.CODE_STRUCT_START | JBBPCompiler.FLAG_EXTRA_FLAGS | JBBPCompiler.FLAG_NAMED, block.getCompiledData()[1] & 0xFF);
46-
assertEquals(JBBPCompiler.EXTRAFLAG_EXPRESSION_OR_WHOLESTREAM, block.getCompiledData()[2] & 0xFF);
45+
assertEquals(JBBPCompiler.CODE_STRUCT_START | JBBPCompiler.FLAG_WIDE | JBBPCompiler.FLAG_NAMED, block.getCompiledData()[1] & 0xFF);
46+
assertEquals(JBBPCompiler.EXT_FLAG_EXPRESSION_OR_WHOLESTREAM, block.getCompiledData()[2] & 0xFF);
4747
assertEquals(JBBPCompiler.CODE_BYTE, block.getCompiledData()[3]);
4848
assertEquals(JBBPCompiler.CODE_STRUCT_END, block.getCompiledData()[4]);
4949
assertEquals(1, block.getCompiledData()[5]);
@@ -65,8 +65,8 @@ public void testCompile_WholeStreamArrayInsideStructure() throws Exception {
6565
final JBBPCompiledBlock block = JBBPCompiler.compile("test {byte [_];}");
6666
assertEquals(5, block.getCompiledData().length);
6767
assertEquals(JBBPCompiler.CODE_STRUCT_START | JBBPCompiler.FLAG_NAMED, block.getCompiledData()[0]);
68-
assertEquals(JBBPCompiler.CODE_BYTE | JBBPCompiler.FLAG_EXTRA_FLAGS, block.getCompiledData()[1] & 0xFF);
69-
assertEquals(JBBPCompiler.EXTRAFLAG_EXPRESSION_OR_WHOLESTREAM, block.getCompiledData()[2] & 0xFF);
68+
assertEquals(JBBPCompiler.CODE_BYTE | JBBPCompiler.FLAG_WIDE, block.getCompiledData()[1] & 0xFF);
69+
assertEquals(JBBPCompiler.EXT_FLAG_EXPRESSION_OR_WHOLESTREAM, block.getCompiledData()[2] & 0xFF);
7070
assertEquals(JBBPCompiler.CODE_STRUCT_END, block.getCompiledData()[3]);
7171
assertEquals(0, block.getCompiledData()[4]);
7272
}
@@ -76,8 +76,8 @@ public void testCompile_WholeStreamStructureArrayInsideStructure() throws Except
7676
final JBBPCompiledBlock block = JBBPCompiler.compile("test { whole[_]{ byte;}}");
7777
assertEquals(8, block.getCompiledData().length);
7878
assertEquals(JBBPCompiler.CODE_STRUCT_START | JBBPCompiler.FLAG_NAMED, block.getCompiledData()[0]);
79-
assertEquals(JBBPCompiler.CODE_STRUCT_START | JBBPCompiler.FLAG_NAMED | JBBPCompiler.FLAG_EXTRA_FLAGS, block.getCompiledData()[1] & 0xFF);
80-
assertEquals(JBBPCompiler.EXTRAFLAG_EXPRESSION_OR_WHOLESTREAM, block.getCompiledData()[2] & 0xFF);
79+
assertEquals(JBBPCompiler.CODE_STRUCT_START | JBBPCompiler.FLAG_NAMED | JBBPCompiler.FLAG_WIDE, block.getCompiledData()[1] & 0xFF);
80+
assertEquals(JBBPCompiler.EXT_FLAG_EXPRESSION_OR_WHOLESTREAM, block.getCompiledData()[2] & 0xFF);
8181
assertEquals(JBBPCompiler.CODE_BYTE, block.getCompiledData()[3]);
8282
assertEquals(JBBPCompiler.CODE_STRUCT_END, block.getCompiledData()[4]);
8383
assertEquals(1, block.getCompiledData()[5]);
@@ -466,8 +466,8 @@ public void testCompile_VarLengthByteArrayInStructure() throws Exception {
466466
assertEquals(JBBPCompiler.CODE_STRUCT_START | JBBPCompiler.FLAG_NAMED, compiled[0]);
467467
assertEquals(JBBPCompiler.CODE_INT | JBBPCompiler.FLAG_NAMED, compiled[1] & 0xFF);
468468
assertEquals(JBBPCompiler.CODE_STRUCT_START | JBBPCompiler.FLAG_NAMED, compiled[2]);
469-
assertEquals(JBBPCompiler.CODE_BYTE | JBBPCompiler.FLAG_NAMED | JBBPCompiler.FLAG_ARRAY | JBBPCompiler.FLAG_EXTRA_FLAGS, compiled[3] & 0xFF);
470-
assertEquals(JBBPCompiler.EXTRAFLAG_EXPRESSION_OR_WHOLESTREAM, compiled[4] & 0xFF);
469+
assertEquals(JBBPCompiler.CODE_BYTE | JBBPCompiler.FLAG_NAMED | JBBPCompiler.FLAG_ARRAY | JBBPCompiler.FLAG_WIDE, compiled[3] & 0xFF);
470+
assertEquals(JBBPCompiler.EXT_FLAG_EXPRESSION_OR_WHOLESTREAM, compiled[4] & 0xFF);
471471
assertEquals(JBBPCompiler.CODE_STRUCT_END, compiled[5]);
472472
assertEquals(2, compiled[6]);
473473
assertEquals(JBBPCompiler.CODE_STRUCT_END, compiled[7]);
@@ -506,8 +506,8 @@ public void testCompile_ArrayWithUndefinedLength() throws Exception {
506506
assertEquals(2, compiled.getCompiledData().length);
507507
assertNotNull(field);
508508
assertEquals(0, field.getFieldOffsetInCompiledBlock());
509-
assertEquals(JBBPCompiler.CODE_BYTE | JBBPCompiler.FLAG_NAMED | JBBPCompiler.FLAG_EXTRA_FLAGS, compiled.getCompiledData()[0] & 0xFF);
510-
assertEquals(JBBPCompiler.EXTRAFLAG_EXPRESSION_OR_WHOLESTREAM, compiled.getCompiledData()[1] & 0xFF);
509+
assertEquals(JBBPCompiler.CODE_BYTE | JBBPCompiler.FLAG_NAMED | JBBPCompiler.FLAG_WIDE, compiled.getCompiledData()[0] & 0xFF);
510+
assertEquals(JBBPCompiler.EXT_FLAG_EXPRESSION_OR_WHOLESTREAM, compiled.getCompiledData()[1] & 0xFF);
511511
}
512512

513513
@Test

0 commit comments

Comments
 (0)