From 22745d540dae6a8b263c7deabcca74287ba78cea Mon Sep 17 00:00:00 2001 From: Alan Gutierrez Date: Wed, 17 Jun 2020 18:31:46 -0500 Subject: [PATCH] Implement nested structures. Closes #341. Closes #340. Closes #339. --- test/cycle/structure.t.js | 16 +++++ test/generated/array/words.parser.all.js | 19 ++---- test/generated/array/words.parser.bff.js | 25 ++----- test/generated/array/words.parser.inc.js | 69 +++++++------------- test/generated/array/words.serializer.all.js | 10 +-- test/generated/array/words.serializer.bff.js | 19 +----- test/generated/array/words.serializer.inc.js | 68 ++++++++----------- test/generated/array/words.sizeof.js | 2 - test/language/structure.t.js | 45 +++++++++++++ 9 files changed, 133 insertions(+), 140 deletions(-) create mode 100644 test/cycle/structure.t.js create mode 100644 test/language/structure.t.js diff --git a/test/cycle/structure.t.js b/test/cycle/structure.t.js new file mode 100644 index 00000000..d6f28e53 --- /dev/null +++ b/test/cycle/structure.t.js @@ -0,0 +1,16 @@ +require('proof')(0, prove) + +function prove (okay) { + require('./cycle')(okay, { + name: 'array/words', + define: { + object: { + value: { + first: 8, + second: 8 + } + } + }, + objects: [{ value: { first: 1, second: 2 } }] + }) +} diff --git a/test/generated/array/words.parser.all.js b/test/generated/array/words.parser.all.js index d2aa0241..a54a8445 100644 --- a/test/generated/array/words.parser.all.js +++ b/test/generated/array/words.parser.all.js @@ -1,22 +1,15 @@ module.exports = function (parsers) { parsers.all.object = function ($buffer, $start) { - let $i = [], $I = [] - const object = { - array: [] + value: { + first: 0, + second: 0 + } } - $i[0] = 0 - $I[0] = - ($buffer[$start++]) * 0x100 + - ($buffer[$start++]) - - for (; $i[0] < $I[0]; $i[0]++) { + object.value.first = ($buffer[$start++]) - object.array[$i[0]] = - ($buffer[$start++]) * 0x100 + - ($buffer[$start++]) - } + object.value.second = ($buffer[$start++]) return object } diff --git a/test/generated/array/words.parser.bff.js b/test/generated/array/words.parser.bff.js index 87f03633..b1e05fec 100644 --- a/test/generated/array/words.parser.bff.js +++ b/test/generated/array/words.parser.bff.js @@ -1,31 +1,20 @@ module.exports = function (parsers) { parsers.bff.object = function () { return function parse ($buffer, $start, $end) { - let $i = [], $I = [] - const object = { - array: [] + value: { + first: 0, + second: 0 + } } if ($end - $start < 2) { - return parsers.inc.object(object, 1, $i, $I)($buffer, $start, $end) + return parsers.inc.object(object, 1)($buffer, $start, $end) } - $i[0] = 0 - $I[0] = - ($buffer[$start++]) * 0x100 + - ($buffer[$start++]) - - for (; $i[0] < $I[0]; $i[0]++) { + object.value.first = ($buffer[$start++]) - if ($end - $start < 2) { - return parsers.inc.object(object, 4, $i, $I)($buffer, $start, $end) - } - - object.array[$i[0]] = - ($buffer[$start++]) * 0x100 + - ($buffer[$start++]) - } + object.value.second = ($buffer[$start++]) return { start: $start, object: object, parse: null } } diff --git a/test/generated/array/words.parser.inc.js b/test/generated/array/words.parser.inc.js index 55131dcf..44a53f29 100644 --- a/test/generated/array/words.parser.inc.js +++ b/test/generated/array/words.parser.inc.js @@ -1,67 +1,48 @@ module.exports = function (parsers) { - parsers.inc.object = function (object = {}, $step = 0, $i = [], $I = []) { + parsers.inc.object = function (object = {}, $step = 0) { let $_, $bite return function parse ($buffer, $start, $end) { - for (;;) { - switch ($step) { - case 0: + switch ($step) { + case 0: - object = { - array: [] + object = { + value: { + first: 0, + second: 0 } + } - $step = 1 - - case 1: + $step = 1 - $_ = 0 - $step = 2 - $bite = 1 + case 1: - case 2: + $step = 2 - while ($bite != -1) { - if ($start == $end) { - return { start: $start, object: null, parse } - } - $_ += ($buffer[$start++]) << $bite * 8 >>> 0 - $bite-- - } + case 2: - $I[0] = $_ + if ($start == $end) { + return { start: $start, object: null, parse } + } - $i[0] = 0 - case 3: + object.value.first = $buffer[$start++] - case 4: + case 3: - $_ = 0 - $step = 5 - $bite = 1 + $step = 4 - case 5: + case 4: - while ($bite != -1) { - if ($start == $end) { - return { start: $start, object: null, parse } - } - $_ += ($buffer[$start++]) << $bite * 8 >>> 0 - $bite-- - } + if ($start == $end) { + return { start: $start, object: null, parse } + } - object.array[$i[0]] = $_ + object.value.second = $buffer[$start++] - if (++$i[0] != $I[0]) { - $step = 3 - continue - } - case 6: + case 5: - return { start: $start, object: object, parse: null } - } - break + return { start: $start, object: object, parse: null } } } } diff --git a/test/generated/array/words.serializer.all.js b/test/generated/array/words.serializer.all.js index 71a24f91..c7c7fbd5 100644 --- a/test/generated/array/words.serializer.all.js +++ b/test/generated/array/words.serializer.all.js @@ -1,15 +1,9 @@ module.exports = function (serializers) { serializers.all.object = function (object) { return function ($buffer, $start, $end) { - let $i = [] + $buffer[$start++] = (object.value.first & 0xff) - $buffer[$start++] = (object.array.length >>> 8 & 0xff) - $buffer[$start++] = (object.array.length & 0xff) - - for ($i[0] = 0; $i[0] < object.array.length; $i[0]++) { - $buffer[$start++] = (object.array[$i[0]] >>> 8 & 0xff) - $buffer[$start++] = (object.array[$i[0]] & 0xff) - } + $buffer[$start++] = (object.value.second & 0xff) return { start: $start, serialize: null } } diff --git a/test/generated/array/words.serializer.bff.js b/test/generated/array/words.serializer.bff.js index ad3841da..ddf5a994 100644 --- a/test/generated/array/words.serializer.bff.js +++ b/test/generated/array/words.serializer.bff.js @@ -1,29 +1,16 @@ module.exports = function (serializers) { serializers.bff.object = function (object) { return function ($buffer, $start, $end) { - let $i = [] - - if ($end - $start < 2) { + if ($end - $start < 2 * object.value.length) { return { start: $start, serialize: serializers.inc.object(object, 0) } } - $buffer[$start++] = (object.array.length >>> 8 & 0xff) - $buffer[$start++] = (object.array.length & 0xff) - - for ($i[0] = 0; $i[0] < object.array.length; $i[0]++) { - if ($end - $start < 2) { - return { - start: $start, - serialize: serializers.inc.object(object, 2, $i) - } - } + $buffer[$start++] = (object.value.first & 0xff) - $buffer[$start++] = (object.array[$i[0]] >>> 8 & 0xff) - $buffer[$start++] = (object.array[$i[0]] & 0xff) - } + $buffer[$start++] = (object.value.second & 0xff) return { start: $start, serialize: null } } diff --git a/test/generated/array/words.serializer.inc.js b/test/generated/array/words.serializer.inc.js index a8d743e5..32333b87 100644 --- a/test/generated/array/words.serializer.inc.js +++ b/test/generated/array/words.serializer.inc.js @@ -1,59 +1,49 @@ module.exports = function (serializers) { - serializers.inc.object = function (object, $step = 0, $i = []) { + serializers.inc.object = function (object, $step = 0) { let $bite, $stop, $_ return function serialize ($buffer, $start, $end) { - for (;;) { - switch ($step) { - case 0: - - $step = 1 - $bite = 1 - $_ = object.array.length - - case 1: - - while ($bite != -1) { - if ($start == $end) { - return { start: $start, serialize } - } - $buffer[$start++] = ($_ >>> $bite * 8 & 0xff) - $bite-- - } + switch ($step) { + case 0: - $i[0] = 0 + $step = 1 + $bite = 0 + $_ = object.value.first - case 2: + case 1: - $step = 3 - $bite = 1 - $_ = object.array[$i[0]] + while ($bite != -1) { + if ($start == $end) { + return { start: $start, serialize } + } + $buffer[$start++] = ($_ >>> $bite * 8 & 0xff) + $bite-- + } - case 3: - while ($bite != -1) { - if ($start == $end) { - return { start: $start, serialize } - } - $buffer[$start++] = ($_ >>> $bite * 8 & 0xff) - $bite-- - } + case 2: + $step = 3 + $bite = 0 + $_ = object.value.second - if (++$i[0] != object.array.length) { - $step = 2 - continue - } + case 3: - $step = 4 + while ($bite != -1) { + if ($start == $end) { + return { start: $start, serialize } + } + $buffer[$start++] = ($_ >>> $bite * 8 & 0xff) + $bite-- + } - case 4: - break + $step = 4 - } + case 4: break + } return { start: $start, serialize: null } diff --git a/test/generated/array/words.sizeof.js b/test/generated/array/words.sizeof.js index f7bda3ea..5fc953b8 100644 --- a/test/generated/array/words.sizeof.js +++ b/test/generated/array/words.sizeof.js @@ -4,8 +4,6 @@ module.exports = function (sizeOf) { $_ += 2 - $_ += 2 * object.array.length - return $_ } } diff --git a/test/language/structure.t.js b/test/language/structure.t.js new file mode 100644 index 00000000..c4d17151 --- /dev/null +++ b/test/language/structure.t.js @@ -0,0 +1,45 @@ +require('proof')(1, okay => { + const simplified = require('../../simplified') + okay(simplified({ + object: { + value: { + first: 8, + second: 8 + } + } + }), [{ + type: 'structure', + vivify: 'object', + dotted: '', + fixed: true, + bits: 16, + fields: [{ + type: 'structure', + vivify: 'object', + dotted: '.value', + fixed: true, + bits: 16, + fields: [{ + type: 'integer', + vivify: 'number', + dotted: '.first', + fixed: true, + bits: 8, + endianness: 'big', + compliment: false, + name: 'first' + }, { + type: 'integer', + vivify: 'number', + dotted: '.second', + fixed: true, + bits: 8, + endianness: 'big', + compliment: false, + name: 'second' + }], + name: 'value' + }], + name: 'object' + }], 'structure') +})