Skip to content

Commit 6089781

Browse files
committed
Refactored all types to the new compiled strategy, rewrote tests
1 parent 64fff94 commit 6089781

64 files changed

Lines changed: 6159 additions & 1014 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Compiler.js

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,27 @@ module.exports = {
88
* @param {Number} non_alloc_size If no buffer is provided when encoding then allocate this size. If too small then will throw a runtime exception.
99
* @returns {Function} (source, buffer=null), where _source_ is the object to be encoded, _buffer_ is the buffer to write to, returns a raw Buffer
1010
*/
11-
compileEncoder(structure, non_alloc_size=512){
11+
compileEncoder(structure, non_alloc_size=4096){
12+
let custom_vars = [];
13+
const alloc_tmp_var = () => {
14+
const name = 'tmp'+custom_vars.length;
15+
custom_vars.push(name);
16+
return name;
17+
}
18+
const compiled = structure.compiledEncoder('source', alloc_tmp_var);
1219
const code = `
13-
(source, buffer=null) => {
14-
let position = 0;
20+
(source, buffer=null, offset=0) => {
21+
let position = offset;
1522
let buffer_flexible = false;
1623
let i = 0;
1724
let tmp;
25+
${custom_vars.length > 0 ? 'let '+custom_vars.join(', ')+';' : ''}
1826
if(!buffer){
1927
buffer = Buffer.alloc(${non_alloc_size});
2028
buffer_flexible = true;
2129
}
22-
${structure.compiledEncoder('source')}
23-
if(buffer_flexible){
24-
return buffer.slice(0, position);
25-
}
26-
return buffer;
30+
${compiled}
31+
return buffer.slice(0, position);
2732
}
2833
`
2934
return eval(code);
@@ -34,12 +39,20 @@ module.exports = {
3439
* @returns {Function} (buffer), where _buffer_ is the data to decode, returns a the decoded _structure_ or throws on errors.
3540
*/
3641
compileDecoder(structure){
42+
let custom_vars = [];
43+
const alloc_tmp_var = () => {
44+
const name = 'tmp'+custom_vars.length;
45+
custom_vars.push(name);
46+
return name;
47+
}
48+
const compiled = structure.compiledDecoder('result', alloc_tmp_var);
3749
const code = `
38-
(buffer) => {
50+
(buffer, offset=0) => {
3951
let result;
40-
let position = 0;
41-
let tmp, tmp2;
42-
${structure.compiledDecoder('result')}
52+
let position = offset;
53+
let tmp;
54+
${custom_vars.length > 0 ? 'let '+custom_vars.join(', ')+';' : ''}
55+
${compiled}
4356
return result;
4457
}
4558
`

Type.js

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1+
const { compileEncoder, compileDecoder } = require('./Compiler');
2+
13
/**
24
* @class
35
* Base class for any transcodable type definition.
46
*/
57
class TranscodableType {
68
constructor(){
7-
this.last_bytes_encoded = -1;
8-
this.last_bytes_decoded = -1;
9+
this._tmp_encoder_compiled = null;
10+
this._tmp_decoder_compiled = null;
911
}
1012
/**
1113
* Encode an object into a binary representation.
@@ -15,15 +17,41 @@ class TranscodableType {
1517
* @returns {Buffer} The buffer that contains the encoded data. If a new buffer is allocated then the data was written from the begining.
1618
*/
1719
encode(object, buffer, offset){
18-
throw Error('Called abstract encode');
20+
if(!this._tmp_encoder_compiled)
21+
this._tmp_encoder_compiled = compileEncoder(this);
22+
return this._tmp_encoder_compiled(object, buffer, offset);
1923
}
2024
/**
2125
* Decode an object from a binary representation.
2226
* @param {Buffer} buffer The buffer to read from.
2327
* @param {Number} [offset] The offset in buffer to start reading.
2428
*/
2529
decode(buffer, offset){
26-
throw Error('Called abstract decode');
30+
if(!this._tmp_decoder_compiled)
31+
this._tmp_decoder_compiled = compileDecoder(this);
32+
return this._tmp_decoder_compiled(buffer, offset);
33+
}
34+
/**
35+
* Generate code that encodes the value in variable of name _source\_var_.
36+
* The code is pasted with other generated code in a single function.
37+
* Refer to {@link Compiler.js} for usable local variables.
38+
* @param {String} source_var The variable name to read from.
39+
* @param {function} tmp_var_alloc A function that allocates a unique temporary variable. Returns its name.
40+
* @returns {String} JavaScript code
41+
*/
42+
compiledEncoder(source_var, tmp_var_alloc){
43+
throw new Error('Abstract compiledEncoder called');
44+
}
45+
/**
46+
* Generate code that decodes the local _buffer_ variable at _position_.
47+
* Save the decoded value to _target\_var_ variable.
48+
* Refer to {@link Compiler.js} for usable local variables.
49+
* @param {String} target_var The variable name to save to.
50+
* @param {function} tmp_var_alloc A function that allocates a unique temporary variable. Returns its name.
51+
* @returns {String} JavaScript code
52+
*/
53+
compiledDecoder(target_var, tmp_var_alloc){
54+
throw new Error('Abstract compiledDecoder called');
2755
}
2856
}
2957

0 commit comments

Comments
 (0)