Skip to content

Commit 73f6b8e

Browse files
extremeheatrom1504
andauthored
Expose compiler, use let instead of const, map updates (#118)
* compiler: convert map string values to numeric/bool * expose CompiledProtodef * const -> let for some compiler structures This allows the variables to be updated later on by parameterizable types * Keep values of mapper type if not matched If a mapper type isn't mapped, default to the value itself to avoid losing data when reading rather than returning undefined * document CompiledProtodef Co-authored-by: Romain Beaumont <romain.rom1@gmail.com>
1 parent 01f7555 commit 73f6b8e

File tree

4 files changed

+22
-8
lines changed

4 files changed

+22
-8
lines changed

doc/api.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,16 @@ The `path` is an array of namespace keys which select a path of namespaces to be
8484

8585
Compile and return a `ProtoDef` object, optionaly print the generated javascript code.
8686

87+
## CompiledProtodef
88+
89+
The class of which an instance is returned by compileProtoDefSync
90+
91+
It follows the same interface as ProtoDef : read, write, sizeOf, createPacketBuffer, parsePacketBuffer
92+
Its constructor is CompiledProtodef(sizeOfCtx, writeCtx, readCtx).
93+
sizeOfCtx, writeCtx and readCtx are the compiled version of sizeOf, write and read. They are produced by Compiler.compile
94+
95+
It can be used directly for easier debugging/using already compiled js.
96+
8797
## utils
8898

8999
Some functions that can be useful to build new datatypes reader and writer.

src/compiler.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,5 +418,6 @@ module.exports = {
418418
ReadCompiler,
419419
WriteCompiler,
420420
SizeOfCompiler,
421-
ProtoDefCompiler
421+
ProtoDefCompiler,
422+
CompiledProtodef
422423
}

src/datatypes/compiler-structures.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ module.exports = {
5151
if (name === trueName) names.push(name)
5252
else names.push(`${name}: ${trueName}`)
5353
}
54-
code += `const { value: ${trueName}, size: ${sizeName} } = ` + compiler.callType(type, offsetExpr) + '\n'
54+
code += `let { value: ${trueName}, size: ${sizeName} } = ` + compiler.callType(type, offsetExpr) + '\n'
5555
offsetExpr += ` + ${sizeName}`
5656
}
5757
const sizes = offsetExpr.split(' + ')
@@ -97,7 +97,7 @@ module.exports = {
9797
trueName = '{' + names.join(', ') + '}'
9898
} else {
9999
trueName = compiler.getField(name)
100-
code += `const ${trueName} = value.${name}\n`
100+
code += `let ${trueName} = value.${name}\n`
101101
}
102102
code += 'offset = ' + compiler.callType(trueName, type) + '\n'
103103
}
@@ -147,7 +147,7 @@ module.exports = {
147147
trueName = '{' + names.join(', ') + '}'
148148
} else {
149149
trueName = compiler.getField(name)
150-
code += `const ${trueName} = value.${name}\n`
150+
code += `let ${trueName} = value.${name}\n`
151151
}
152152
code += 'size += ' + compiler.callType(trueName, type) + '\n'
153153
}

src/datatypes/compiler-utils.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ module.exports = {
6060
}],
6161
mapper: ['parametrizable', (compiler, mapper) => {
6262
let code = 'const { value, size } = ' + compiler.callType(mapper.type) + '\n'
63-
code += 'return { value: ' + JSON.stringify(sanitizeMappings(mapper.mappings)) + '[value], size }'
63+
code += 'return { value: ' + JSON.stringify(sanitizeMappings(mapper.mappings)) + '[value] || value, size }'
6464
return compiler.wrapCode(code)
6565
}]
6666
},
@@ -118,7 +118,7 @@ module.exports = {
118118
}],
119119
mapper: ['parametrizable', (compiler, mapper) => {
120120
const mappings = JSON.stringify(swapMappings(mapper.mappings))
121-
const code = 'return ' + compiler.callType(`${mappings}[value]`, mapper.type)
121+
const code = 'return ' + compiler.callType(`${mappings}[value] || value`, mapper.type)
122122
return compiler.wrapCode(code)
123123
}]
124124
},
@@ -150,7 +150,7 @@ module.exports = {
150150
}],
151151
mapper: ['parametrizable', (compiler, mapper) => {
152152
const mappings = JSON.stringify(swapMappings(mapper.mappings))
153-
const code = 'return ' + compiler.callType(`${mappings}[value]`, mapper.type)
153+
const code = 'return ' + compiler.callType(`${mappings}[value] || value`, mapper.type)
154154
return compiler.wrapCode(code)
155155
}]
156156
}
@@ -160,8 +160,11 @@ module.exports = {
160160
function sanitizeMappings (json) {
161161
const ret = {}
162162
for (let key in json) {
163-
const val = json[key]
163+
let val = json[key]
164164
key = hex2dec(key)
165+
if (!isNaN(val)) val = Number(val)
166+
if (val === 'true') val = true
167+
if (val === 'false') val = false
165168
ret[key] = val
166169
}
167170
return ret

0 commit comments

Comments
 (0)