shared-structs
Share a struct backed by the same underlying buffer between C and JavaScript
npm install shared-struct
Useful for doing bulk updates of data in native modules with no context switching cost.
Usage
const sharedStructs = require('shared-structs')
const structs = sharedStructs(`
struct aStruct {
int32_t i;
char buf[1024];
char someChar;
int someInt;
}
`)
const struct = structs.aStruct()
struct.i = 42
struct.buf[0] = 42
console.log(struct.rawBuffer)
Also supports nested structs and most other things you'd normally use in c!
See example/example.js for more.
API
structs = sharedStructs(src, [options])
Parses the structs specified in the global scope of src
and returns JavaScript implementations of each.
Each property is exposed as a normal JavaScript property you can
get/set.
All changes are reflected in .rawBuffer which you can pass to a c program
and parse with the same struct.
Options include:
{
defines: {
CUSTOM_DEFINE_HERE: 42
},
sizes: {
foo: 1024
},
alignment: {
foo: 8
}
}
Writing strings
There is a small helper included in require('shared-structs/strings') that
allows you to encode/decode c style strings into char buffers
const strings = require('shared-structs/strings')
strings.encode('hello world', struct.buf)
console.log(strings.decode(struct.buf))
License
MIT