diff --git a/language.js b/language.js index 73fcbd1c..61219c76 100644 --- a/language.js +++ b/language.js @@ -545,12 +545,13 @@ module.exports = function (packets) { // conditionals to implement optional fields. function absent (value) { return [{ - ...extra, type: 'absent', vivify: value == null ? 'variant' : 'array', value: value, + dotted: '', bits: 0, - fixed: true + fixed: true, + ...extra }] } // @@ -1041,14 +1042,14 @@ module.exports = function (packets) { : 'variant' }, cases[0].fields[0].vivify) return [{ - ...extra, type: 'switch', select: { ...args([ packet[0] ]) }, vivify: vivify == 'object' ? 'variant' : vivify, stringify: ! Array.isArray(packet[1]), bits: bits < 0 ? 0 : bits, fixed: bits > 0, - cases: cases + cases: cases, + ...extra }] } // diff --git a/test/cycle/absent.t.js b/test/cycle/absent.t.js index 5f0aa358..1ea7de00 100644 --- a/test/cycle/absent.t.js +++ b/test/cycle/absent.t.js @@ -19,4 +19,16 @@ function prove (okay) { }, objects: [{ value: [] }] }) + require('./cycle')(okay, { + name: 'absent/switch', + define: { + object: { + value: [ $ => 0, [ + { $_: 0 }, null, + {}, null + ]] + } + }, + objects: [{ value: null }] + }) } diff --git a/test/generated/absent/switch.lookup.js b/test/generated/absent/switch.lookup.js new file mode 100644 index 00000000..d7faf17f --- /dev/null +++ b/test/generated/absent/switch.lookup.js @@ -0,0 +1 @@ +module.exports = [] diff --git a/test/generated/absent/switch.parser.all.js b/test/generated/absent/switch.parser.all.js new file mode 100644 index 00000000..eab36e6c --- /dev/null +++ b/test/generated/absent/switch.parser.all.js @@ -0,0 +1,25 @@ +module.exports = function ({ $lookup }) { + return { + object: function () { + return function ($buffer, $start) { + let object = { + value: null + } + + switch (($ => 0)(object)) { + case 0: + object.value = null + + break + + case undefined: + object.value = null + + break + } + + return object + } + } () + } +} diff --git a/test/generated/absent/switch.parser.bff.js b/test/generated/absent/switch.parser.bff.js new file mode 100644 index 00000000..443553ea --- /dev/null +++ b/test/generated/absent/switch.parser.bff.js @@ -0,0 +1,27 @@ +module.exports = function ({ $incremental, $lookup }) { + return { + object: function () { + return function () { + return function ($buffer, $start, $end) { + let object = { + value: null + } + + switch (($ => 0)(object)) { + case 0: + object.value = null + + break + + case undefined: + object.value = null + + break + } + + return { start: $start, object: object, parse: null } + } + } () + } + } +} diff --git a/test/generated/absent/switch.parser.chk.js b/test/generated/absent/switch.parser.chk.js new file mode 100644 index 00000000..443553ea --- /dev/null +++ b/test/generated/absent/switch.parser.chk.js @@ -0,0 +1,27 @@ +module.exports = function ({ $incremental, $lookup }) { + return { + object: function () { + return function () { + return function ($buffer, $start, $end) { + let object = { + value: null + } + + switch (($ => 0)(object)) { + case 0: + object.value = null + + break + + case undefined: + object.value = null + + break + } + + return { start: $start, object: object, parse: null } + } + } () + } + } +} diff --git a/test/generated/absent/switch.parser.inc.js b/test/generated/absent/switch.parser.inc.js new file mode 100644 index 00000000..3090cf55 --- /dev/null +++ b/test/generated/absent/switch.parser.inc.js @@ -0,0 +1,48 @@ +module.exports = function ({ $lookup }) { + return { + object: function () { + return function (object, $step = 0) { + + return function $parse ($buffer, $start, $end) { + for (;;) { + switch ($step) { + case 0: + + object = { + value: null + } + + case 1: + + switch (($ => 0)(object)) { + case 0: + + $step = 2 + continue + + case undefined: + + $step = 3 + continue + } + + case 2: + + object.value = null + $step = 4 + continue + + case 3: + + object.value = null + + } + + return { start: $start, object: object, parse: null } + break + } + } + } + } () + } +} diff --git a/test/generated/absent/switch.serializer.all.js b/test/generated/absent/switch.serializer.all.js new file mode 100644 index 00000000..a239e0ca --- /dev/null +++ b/test/generated/absent/switch.serializer.all.js @@ -0,0 +1,21 @@ +module.exports = function ({ $lookup }) { + return { + object: function () { + return function (object, $buffer, $start) { + switch (($ => 0)(object)) { + case 0: + + + break + + case undefined: + + + break + } + + return { start: $start, serialize: null } + } + } () + } +} diff --git a/test/generated/absent/switch.serializer.bff.js b/test/generated/absent/switch.serializer.bff.js new file mode 100644 index 00000000..1254d12b --- /dev/null +++ b/test/generated/absent/switch.serializer.bff.js @@ -0,0 +1,23 @@ +module.exports = function ({ $incremental, $lookup }) { + return { + object: function () { + return function (object) { + return function ($buffer, $start, $end) { + switch (($ => 0)(object)) { + case 0: + + + break + + case undefined: + + + break + } + + return { start: $start, serialize: null } + } + } + } () + } +} diff --git a/test/generated/absent/switch.serializer.chk.js b/test/generated/absent/switch.serializer.chk.js new file mode 100644 index 00000000..1254d12b --- /dev/null +++ b/test/generated/absent/switch.serializer.chk.js @@ -0,0 +1,23 @@ +module.exports = function ({ $incremental, $lookup }) { + return { + object: function () { + return function (object) { + return function ($buffer, $start, $end) { + switch (($ => 0)(object)) { + case 0: + + + break + + case undefined: + + + break + } + + return { start: $start, serialize: null } + } + } + } () + } +} diff --git a/test/generated/absent/switch.serializer.inc.js b/test/generated/absent/switch.serializer.inc.js new file mode 100644 index 00000000..b01b2d53 --- /dev/null +++ b/test/generated/absent/switch.serializer.inc.js @@ -0,0 +1,38 @@ +module.exports = function ({ $lookup }) { + return { + object: function () { + return function (object, $step = 0) { + return function $serialize ($buffer, $start, $end) { + for (;;) { + switch ($step) { + case 0: + + switch (($ => 0)(object)) { + case 0: + + $step = 1 + continue + + case undefined: + + $step = 2 + continue + } + + case 1: + $step = 3 + continue + + case 2: + + } + + break + } + + return { start: $start, serialize: null } + } + } + } () + } +} diff --git a/test/generated/absent/switch.sizeof.js b/test/generated/absent/switch.sizeof.js new file mode 100644 index 00000000..3966ac6b --- /dev/null +++ b/test/generated/absent/switch.sizeof.js @@ -0,0 +1,21 @@ +module.exports = { + object: function () { + return function (object) { + let $start = 0 + + switch (($ => 0)(object)) { + case 0: + + + break + + case undefined: + + + break + } + + return $start + } + } () +} diff --git a/test/language/absent.t.js b/test/language/absent.t.js index 5ca2b4bf..86bbb34b 100644 --- a/test/language/absent.t.js +++ b/test/language/absent.t.js @@ -1,4 +1,4 @@ -require('proof')(2, okay => { +require('proof')(3, okay => { const language = require('../../language') okay(language({ packet: { @@ -42,4 +42,47 @@ require('proof')(2, okay => { fixed: true }] }], 'array') + okay(language({ + packet: { + value: [ $ => 0, [ + { $_: 0 }, null + ]] + } + }), [{ + name: 'packet', + type: 'structure', + vivify: 'object', + dotted: '', + fixed: false, + bits: 0, + fields: [{ + type: 'switch', + name: 'value', + dotted: '.value', + select: { + defaulted: [], + positional: true, + properties: [], + source: '$ => 0', + arity: 1, + vargs: [] + }, + vivify: 'variant', + stringify: false, + bits: 0, + fixed: false, + cases: [{ + value: 0, + otherwise: false, + fields: [{ + type: 'absent', + dotted: '', + vivify: 'variant', + value: null, + bits: 0, + fixed: true + }] + }] + }] + }], 'switch') })