Skip to content

Commit

Permalink
Fix 'undefined' absent field name in switch.
Browse files Browse the repository at this point in the history
The string 'undefined' was being appended to field names when an absent
field was used as part of a switch statment.

Closes #609.
  • Loading branch information
flatheadmill committed Sep 27, 2021
1 parent e2971e9 commit c902d92
Show file tree
Hide file tree
Showing 13 changed files with 315 additions and 5 deletions.
9 changes: 5 additions & 4 deletions language.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}]
}
//
Expand Down Expand Up @@ -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
}]
}
//
Expand Down
12 changes: 12 additions & 0 deletions test/cycle/absent.t.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }]
})
}
1 change: 1 addition & 0 deletions test/generated/absent/switch.lookup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = []
25 changes: 25 additions & 0 deletions test/generated/absent/switch.parser.all.js
Original file line number Diff line number Diff line change
@@ -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
}
} ()
}
}
27 changes: 27 additions & 0 deletions test/generated/absent/switch.parser.bff.js
Original file line number Diff line number Diff line change
@@ -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 }
}
} ()
}
}
}
27 changes: 27 additions & 0 deletions test/generated/absent/switch.parser.chk.js
Original file line number Diff line number Diff line change
@@ -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 }
}
} ()
}
}
}
48 changes: 48 additions & 0 deletions test/generated/absent/switch.parser.inc.js
Original file line number Diff line number Diff line change
@@ -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
}
}
}
} ()
}
}
21 changes: 21 additions & 0 deletions test/generated/absent/switch.serializer.all.js
Original file line number Diff line number Diff line change
@@ -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 }
}
} ()
}
}
23 changes: 23 additions & 0 deletions test/generated/absent/switch.serializer.bff.js
Original file line number Diff line number Diff line change
@@ -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 }
}
}
} ()
}
}
23 changes: 23 additions & 0 deletions test/generated/absent/switch.serializer.chk.js
Original file line number Diff line number Diff line change
@@ -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 }
}
}
} ()
}
}
38 changes: 38 additions & 0 deletions test/generated/absent/switch.serializer.inc.js
Original file line number Diff line number Diff line change
@@ -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 }
}
}
} ()
}
}
21 changes: 21 additions & 0 deletions test/generated/absent/switch.sizeof.js
Original file line number Diff line number Diff line change
@@ -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
}
} ()
}
45 changes: 44 additions & 1 deletion test/language/absent.t.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require('proof')(2, okay => {
require('proof')(3, okay => {
const language = require('../../language')
okay(language({
packet: {
Expand Down Expand Up @@ -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')
})

0 comments on commit c902d92

Please sign in to comment.