Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions src/datatypes/compiler-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,11 @@ return (ctx.${type})(val, buffer, offset)
}],
mapper: ['parametrizable', (compiler, mapper) => {
const mappings = JSON.stringify(swapMappings(mapper.mappings))
let code = `const mapped = ${mappings}[value]\n`
code += 'if (mapped === undefined) throw new Error(value + \' is not in the mappings value\')\n'
let code = `let mapped = ${mappings}[value]\n`
code += 'if (mapped === undefined) {\n'
code += ' if (typeof value !== \'number\') throw new Error(value + \' is not in the mappings value\')\n'
code += ' mapped = value\n'
code += '}\n'
code += 'return ' + compiler.callType('mapped', mapper.type)
return compiler.wrapCode(code)
}]
Expand Down Expand Up @@ -215,8 +218,11 @@ return (ctx.${type})(val)
}],
mapper: ['parametrizable', (compiler, mapper) => {
const mappings = JSON.stringify(swapMappings(mapper.mappings))
let code = `const mapped = ${mappings}[value]\n`
code += 'if (mapped === undefined) throw new Error(value + \' is not in the mappings value\')\n'
let code = `let mapped = ${mappings}[value]\n`
code += 'if (mapped === undefined) {\n'
code += ' if (typeof value !== \'number\') throw new Error(value + \' is not in the mappings value\')\n'
code += ' mapped = value\n'
code += '}\n'
code += 'return ' + compiler.callType('mapped', mapper.type)
return compiler.wrapCode(code)
}]
Expand Down
6 changes: 6 additions & 0 deletions test/misc.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ describe('mapper', () => {
assert.throws(() => p.createPacketBuffer('name', 'nope'), /nope is not in the mappings value/)
})
}

// The compiled read returns the raw id for a value not in the mappings, so writing it back must work
it('writes a raw integer not in the mappings (compiled)', () => {
assert.deepStrictEqual(compiled.createPacketBuffer('name', 5), Buffer.from([5]))
assert.strictEqual(compiled.parsePacketBuffer('name', Buffer.from([5])).data, 5)
})
})

describe('bitflags', () => {
Expand Down
Loading