@socketsupply/ltp
Advanced tools
Comparing version 2.0.2 to 2.1.0
155
generate.js
@@ -23,6 +23,48 @@ var path = require('path') | ||
} | ||
var args = [], ops = [] | ||
var args = [], ops_direct = [], ops_pointed = [] | ||
schema.forEach(function (field) { | ||
var {type, direct, pointed, position} = field | ||
var v_name = `v_${field.name}` | ||
//Direct to Pointed - (variable size value) | ||
if(direct && pointed) { | ||
//returns a pointer to the field type | ||
//decode_${name}_${field.name} function returns a pointer to the input type. | ||
s +=(` | ||
${pointed.type}* ${decode(field)} (byte* buf) { | ||
return (${pointed.type}*)ltp_decode_relp__${direct.type}(buf+${field.position}); | ||
} | ||
`) | ||
s += (` | ||
size_t ${encode(field)} (byte* buf, int ${v_name}_length, ${pointed.type}* ${v_name}, byte* free) { | ||
ltp_encode_relp__${direct.type}(buf+${position}, free); | ||
return ltp_encode__${pointed.type}(free, ${v_name}_length, ${v_name}); | ||
}`) | ||
args.push(`int v_${field.name}__length, ${pointed.type}* ${v_name}`) | ||
ops_pointed.push(`free += ${encode(field)}(buf, ${v_name}__length, ${v_name}, free)`) | ||
} | ||
// Pointed only (implicit pointer, A SINGLE fixed position variable sized value) | ||
else if(!direct && pointed) { | ||
//generate encode/decode | ||
s +=(` | ||
${pointed.type}* ${decode(field)} (byte* buf) { | ||
return (${pointed.type}*)(buf+${field.position}); | ||
}`) | ||
s += (` | ||
size_t ${encode(field)} (byte* buf, int ${v_name}_length, ${pointed.type}* ${v_name}, byte* free) { | ||
return ltp_encode__${pointed.type}((byte*)(buf+${field.position}), ${v_name}_length, ${v_name}); | ||
}`) | ||
args.push(`int ${v_name}_length, ${field.pointed.type}* ${v_name}`) | ||
ops_pointed.push(`free += ${encode(field)}(buf, ${v_name}_length, ${v_name}, free)`) | ||
} | ||
//note, this sort of encode function, must copy another type data in. | ||
//would be best to return the bytes used (or, new pointer to next free space) | ||
//abstract-encoding returns the buffer, enabling allocating the buffer but that's not a great usecase) | ||
if(field.pointed && 'array' === field.pointed.type) { | ||
@@ -35,5 +77,2 @@ //generate array access methods | ||
var {type, direct, pointed, position} = field | ||
var v_name = `v_${field.name}` | ||
//Direct value (fixed size only) | ||
@@ -43,4 +82,9 @@ if(direct && !pointed) { | ||
//for example, it's a fixed size array. | ||
if(direct.pointer) { | ||
if(field.isLength) { | ||
s += (` | ||
void ${encode(field)} (byte* buf, byte* free) { | ||
ltp_encode__${direct.type}((byte*)(buf+${position}), (${direct.type})(free-buf)); | ||
} | ||
`) | ||
s += (` | ||
@@ -52,17 +96,16 @@ ${direct.type}* ${decode(field)} (byte* buf) { | ||
ops_direct.push(`${encode(field)}(buf, free);`) | ||
} | ||
else if(field.isType) { | ||
//encoding the type, does not take args, because the schema defines the value | ||
s += (` | ||
void ${encode(field)} (byte* buf, ${direct.type}* ${v_name}) { | ||
ltp_encode__${direct.type}((byte*)(buf+${position}), ${v_name}); | ||
void ${encode(field)} (byte* buf) { | ||
ltp_encode__${direct.type}((byte*)(buf+${position}), ${field.typeValue}); | ||
} | ||
`) | ||
args.push(`${direct.type}* ${v_name}`) | ||
ops.push(`${encode(field)}(buf, v_${field.name})`) | ||
} | ||
else { | ||
s += (` | ||
${direct.type} ${decode(field)} (byte* buf) { | ||
${direct.type}* ${decode(field)} (byte* buf) { | ||
return ltp_decode__${direct.type}((byte*)(buf+${position})); | ||
@@ -72,53 +115,48 @@ } | ||
s += (` | ||
void ${encode(field)} (byte* buf, ${direct.type} ${v_name}) { | ||
ltp_encode__${direct.type}((byte*)(buf+${position}), ${v_name}); | ||
} | ||
`) | ||
//type does not appear in the args | ||
ops_direct.push(`${encode(field)}(buf)`) | ||
args.push(`${direct.type} ${v_name}`) | ||
ops.push(`${encode(field)}(buf, v_${field.name})`) | ||
} | ||
else { | ||
if(direct.pointer) { | ||
} | ||
s += (` | ||
${direct.type}* ${decode(field)} (byte* buf) { | ||
return ltp_decode__${direct.type}((byte*)(buf+${position})); | ||
} | ||
`) | ||
//Direct to Pointed - (variable size value) | ||
s += (` | ||
void ${encode(field)} (byte* buf, ${direct.type}* ${v_name}) { | ||
ltp_encode__${direct.type}((byte*)(buf+${position}), ${v_name}); | ||
} | ||
`) | ||
else if(direct && pointed) { | ||
//returns a pointer to the field type | ||
//decode_${name}_${field.name} function returns a pointer to the input type. | ||
s +=(` | ||
${pointed.type}* ${decode(field)} (byte* buf) { | ||
return (${pointed.type}*)ltp_decode_relp__${direct.type}(buf+${field.position}); | ||
} | ||
`) | ||
s += (` | ||
size_t ${encode(field)} (byte* buf, int ${v_name}_length, ${pointed.type}* ${v_name}, byte* free) { | ||
ltp_encode_relp__${direct.type}(buf+${position}, free); | ||
return ltp_encode__${pointed.type}(free, ${v_name}_length, ${v_name}); | ||
}`) | ||
args.push(`int v_${field.name}__length, ${pointed.type}* v_${field.name}`) | ||
ops.push(`free += ${encode(field)}(buf, v_${field.name}__length, v_${field.name}, free)`) | ||
args.push(`${direct.type}* ${v_name}`) | ||
ops_direct.push(`${encode(field)}(buf, v_${field.name})`) | ||
} | ||
else { | ||
s += (` | ||
${direct.type} ${decode(field)} (byte* buf) { | ||
return ltp_decode__${direct.type}((byte*)(buf+${position})); | ||
} | ||
`) | ||
// Pointed only (implicit pointer, A SINGLE fixed position variable sized value) | ||
else if(!direct && pointed) { | ||
//generate encode/decode | ||
s +=(` | ||
${pointed.type}* ${decode(field)} (byte* buf) { | ||
return (${pointed.type}*)(buf+${field.position}); | ||
}`) | ||
s += (` | ||
void ${encode(field)} (byte* buf, ${direct.type} ${v_name}) { | ||
ltp_encode__${direct.type}((byte*)(buf+${position}), ${v_name}); | ||
} | ||
`) | ||
s += (` | ||
size_t ${encode(field)} (byte* buf, int ${v_name}_length, ${pointed.type}* ${v_name}, byte* free) { | ||
return ltp_encode__${pointed.type}((byte*)(buf+${field.position}), ${v_name}_length, ${v_name}); | ||
}`) | ||
args.push(`${direct.type} ${v_name}`) | ||
args.push(`int ${v_name}_length, ${field.pointed.type}* v_${field.name}`) | ||
ops.push(`free += ${encode(field)}(buf, v_${field.name}_length, v_${field.name}, free)`) | ||
ops_direct.push(`${encode(field)}(buf, v_${field.name})`) | ||
} | ||
} | ||
} | ||
//note, this sort of encode function, must copy another type data in. | ||
//would be best to return the bytes used (or, new pointer to next free space) | ||
//abstract-encoding returns the buffer, enabling allocating the buffer but that's not a great usecase) | ||
@@ -133,3 +171,4 @@ }) | ||
byte* free = buf+${min}; | ||
${ops.map(e=>e+';').join('\n ')} | ||
${ops_pointed.map(e=>e+';').join('\n ')} | ||
${ops_direct.map(e=>e+';').join('\n ')} | ||
return (size_t)(free - buf); | ||
@@ -142,4 +181,4 @@ }` | ||
module.exports = function (schemas, prefix='') { | ||
var s = require('fs').readFileSync(path.join(__dirname, 'ltp.h'), 'utf8') | ||
module.exports = function (schemas, prefix='', includeHeader=true) { | ||
var s = includeHeader ? require('fs').readFileSync(path.join(__dirname, 'ltp.h'), 'utf8') : '' | ||
for(var name in schemas) | ||
@@ -146,0 +185,0 @@ s += generateObjectCodec(prefix, name, schemas[name]) |
{ | ||
"name": "@socketsupply/ltp", | ||
"version": "2.0.2", | ||
"version": "2.1.0", | ||
"description": "A schemaful parseless binary format, like a simpler version of captnproto. fast access to fields without allocating memory", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -24,2 +24,8 @@ var schema = { | ||
{name: 'signature', position: 0, direct: {type: 'fixed_64', pointer: true}} | ||
], | ||
typeLengthBuf: [ | ||
{name: 'type', position: 0, direct: {type:'u8'}, isType: true, typeValue: 0x99}, | ||
{name: 'length', position: 1, direct: {type:'u16'}, isLength: true}, | ||
{name: 'text', position: 3, /*direct: {type: 'u8'}, */pointed: {type: 'string_u16'}}, | ||
] | ||
@@ -26,0 +32,0 @@ |
@@ -285,2 +285,15 @@ var fs = require('fs') | ||
}) | ||
tape('encode/decode type/length', function (t) { | ||
var start4 = start+1000 | ||
var string = 'LTP\x00' | ||
memory.write(string, cstring2=start4+100, 'utf8') | ||
var bytes = wasm.encode__typeLengthBuf(start4, 4, cstring2) | ||
t.equal(wasm.decode__typeLengthBuf_type(start4), 0x99) | ||
t.equal(wasm.decode__typeLengthBuf_length(start4), 1+2+2+4) | ||
t.equal(bytes, 1+2 + 2+4) | ||
t.end() | ||
}) |
@@ -92,2 +92,3 @@ var codex = require('./codex') | ||
fixed_4: 4, | ||
fixed_16: 16, | ||
@@ -104,6 +105,8 @@ fixed_20: 20, | ||
return sizes[codec.type] | ||
throw new Error('codec must be fixed size') | ||
throw new Error('codec must be fixed size:'+JSON.stringify(codec)) | ||
} | ||
function getMinimumSize(schema) { | ||
if(!Array.isArray(schema)) | ||
throw new Error('expected schema object, got:'+schema) | ||
if(!schema.length) return 0 //or should an empty schema be a throw? | ||
@@ -119,3 +122,3 @@ var size = 0, fpvs = null | ||
if(fpvs && fpvs.position != size) { | ||
throw new Error('fpvs must be in last position') | ||
throw new Error('fpvs must be in last position: '+JSON.stringify(fpvs)) | ||
} | ||
@@ -122,0 +125,0 @@ return size |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
135357
1656