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

ref-struct-bitfield

v25.9.1

Published

Creates C `struct` instances on `Buffer`s. Supports `union`s and bitfields.

Downloads

7

Readme

ref-struct-bitfield

Forked from ref-struct-di and ref-union-di.

Rewrites ref-struct-di, supporting fields redefining and bitfields.

Rewrites ref-union-di, supporting fields redefining, toObject() and toJSON().

Remark

This module offers a "struct" implementation on top of Node.js Buffers using the ref "type" interface.

Note: Currently, supports the bitfield same as VC only. gcc style is not supported.

Installation

Install with npm:

$ npm install ref-struct-bitfield

Examples

Say you wanted to emulate the following struct:

typedef struct {
    __int64 a : 7;
    __int64 b : 2;
    __int64 c : 2;
    char d : 7;
    char e : 2;
    char f : 2;
    int g : 2;
    char h;
    unsigned __int64 i : 2;
    unsigned __int64 j : 62;
    unsigned __int64 k : 1;
} S;

Write the javascript code:

const ref = require("ref-napi");
const struct = require("ref-struct-bitfield")(ref);
const bitfield = struct.bitfield;

const S = struct({
	a: bitfield(ref.types.int64, 7),
	b: bitfield("int64", 2),
	c: bitfield("int64", 2),
	d: bitfield("char", 7),
	e: bitfield("char", 2),
	f: bitfield("char", 2),
	g: bitfield("int", 2),
	h: "short",
	i: bitfield("uint64", 2),
	j: bitfield("int64", 62),
	k: bitfield("uint64", 1),
});

// we can redefine an existing field
S.defineProperty("h", ref.types.char);

// and also fields
S.defineProperties({
	i: bitfield("uint64", 2),
	j: bitfield("int64", 62),
	k: bitfield("uint64", 1),
});

// and something interesting you love :)
S
	.defineProperty("h", ref.types.char)
	.defineProperties({
		i: bitfield("uint64", 2),
		j: bitfield("int64", 62),
		k: bitfield("uint64", 1),
	});

// now we can create instances of it
const s = new S;


// once an instance of the struct type is created,
// the struct type is frozen,
// and no more properties may be added to it.
S.defineProperty('l', ref.types.int);
// AssertionError: an instance of this Struct type has already been created, cannot add new "fields" anymore
//      at Function.defineProperty (/path/to/ref-struct-bitfield/src/struct.js:180:3)
printf("size: %d\n", sizeof(S));    // 40
console.log(S.size);        // 40
console.log(S.alignment);   // 8

Example of union

const ref = require("ref-napi");
const struct = require("ref-struct-bitfield")(ref);
const bitfield = struct.bitfield;
const union = require("ref-struct-bitfield/union")(ref);

const U = union({
	s: struct({
		a: bitfield("int", 7),
		b: bitfield("int", 2),
		c: bitfield("int", 1),
	}),
	us: struct({
		a: bitfield("uint", 7),
		b: bitfield("uint", 2),
		c: bitfield("uint", 1),
	}),
});

console.log(U.size);    // 4

const u = new U;
u.s.a = 0xFF;
u.s.b = 0xFF;
u.s.c = 1;
console.log(u.toObject());
// {
//      s: { a: -1, b: -1, c: -1 },  // because they are signed integers
//      us: { a: 127, b: 3, c: 1 }
// }

License

MIT