-
Notifications
You must be signed in to change notification settings - Fork 1
/
Type.js
59 lines (57 loc) · 2.37 KB
/
Type.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
const { compileEncoder, compileDecoder } = require('./Compiler');
/**
* @class
* Base class for any transcodable type definition.
*/
class TranscodableType {
constructor(){
this._tmp_encoder_compiled = null;
this._tmp_decoder_compiled = null;
}
/**
* Encode an object into a binary representation.
* @param {*} object The object/value to be encoded.
* @param {Buffer} [buffer] The buffer to write to. If the buffer is too small an exception will be thrown.
* @param {Number} [offset] The offset in buffer to write at.
* @param {Number} [non_alloc_size] If _buffer_ is not passed then allocate a buffer of this size.
* @returns {Buffer} The buffer that contains the encoded data. If a new buffer is allocated then the data was written from the begining.
*/
encode(object, buffer, offset, non_alloc_size=null){
if(!this._tmp_encoder_compiled)
this._tmp_encoder_compiled = compileEncoder(this);
return this._tmp_encoder_compiled(object, buffer, offset, non_alloc_size);
}
/**
* Decode an object from a binary representation.
* @param {Buffer} buffer The buffer to read from.
* @param {Number} [offset] The offset in buffer to start reading.
*/
decode(buffer, offset){
if(!this._tmp_decoder_compiled)
this._tmp_decoder_compiled = compileDecoder(this);
return this._tmp_decoder_compiled(buffer, offset);
}
/**
* Generate code that encodes the value in variable of name _source\_var_.
* The code is pasted with other generated code in a single function.
* Refer to {@link Compiler.js} for usable local variables.
* @param {String} source_var The variable name to read from.
* @param {function} tmp_var_alloc A function that allocates a unique temporary variable. Returns its name.
* @returns {String} JavaScript code
*/
compiledEncoder(source_var, tmp_var_alloc){
throw new Error('Abstract compiledEncoder called');
}
/**
* Generate code that decodes the local _buffer_ variable at _position_.
* Save the decoded value to _target\_var_ variable.
* Refer to {@link Compiler.js} for usable local variables.
* @param {String} target_var The variable name to save to.
* @param {function} tmp_var_alloc A function that allocates a unique temporary variable. Returns its name.
* @returns {String} JavaScript code
*/
compiledDecoder(target_var, tmp_var_alloc){
throw new Error('Abstract compiledDecoder called');
}
}
module.exports = TranscodableType;