npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

shared-structs

v1.4.1

Published

Share a struct backed by the same underlying buffer between C and JavaScript

Downloads

171

Readme

shared-structs

Share a struct backed by the same underlying buffer between C and JavaScript

npm install shared-structs

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

// pass this to c, and it will be able to parse it
console.log(struct.rawBuffer)

Also supports nested structs, multidimensional arrays, defines, constant expressions, 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.

If you want to pass a nested struct to c, use the .rawBufferSlice to get a pointer directly to this struct instead of .rawBuffer.

If you are using this with a native module, make sure to keep a reference to the allocated struct in JavaScript (unless you know what you are doing) to avoid the buffer getting garbage collected, while you are still using it in your native code.

If you are compiling a struct, that has a field that shared-structs cannot determine the size and alignment of deterministicly, it will throw an error. If you use the napi-macros module, you can easily export these from your native code using the NAPI_EXPORT_SIZEOF and NAPI_EXPORT_ALIGNMENT macros and pass them in as options.

Options include:

{
  defines: {
    CUSTOM_DEFINE_HERE: 42 // add a custom define is defined elsewhere
  },
  sizes: {
    foo: 1024 // set the size of struct foo if defined elsewhere
  },
  alignment: {
    foo: 8 // set the alignment of struct foo if defined elsewhere
  }
}

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')

// encode
strings.encode('hello world', struct.buf)

// decode
console.log(strings.decode(struct.buf))

Requiring .h files

If you have your structs defined in a .h (or any file) there is a helper included in require('shared-structs/require') that can require these and parse them then as you would any other .js file

const structs = require('shared-structs/require')('file.h')

console.log(structs) // same as loading the src of file.h and parsing it

If you want to pass options, pass them after the filename.

License

MIT