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

node-c-struct

v1.0.1

Published

Provides a bi-directional mapping between c data and javascript objects

Downloads

3

Readme

Fundamental types

| struct api | c type | | ---------- | ------------------ | | bool | bool | | char | signed char | | uchar | unsigned char | | short | short | | ushort | unsigned short | | int | int | | uint | unsigned int | | long | long | | ulong | unsigned long | | longlong | long long | | ulonglong | unsigned long long | | float | float | | double | double | | ssize_t | ssize_t | | size_t | size_t | | int8_t | int8_t | | uint8_t | uint8_t | | int16_t | int16_t | | uint16_t | uint16_t | | int32_t | int32_t | | uint32_t | uint32_t | | int64_t | int64_t | | uint64_t | uint64_t | | struct | struct | | union | union |

Inheritance diagram

pic

built-in methods

  • get $buffer ()

    Return a node buffer

  • get $arrayBuffer ()

    Return an ArrayBuffer

  • get $address ()

    Return real address of current object

  • get $value ()

    Get value

  • set $value (value)

    Set value

Static methods

  • get byteSize ()

    Similar with sizeof()

  • times (length)

    Return an instance of BaseArrayType

Example

int

const struct = require('node-c-struct')

const a = new struct.int()

a.$value = 100

console.log(a) // <Buffer 64 00 00 00>
console.log(a.$value) // 100

array

const struct = require('node-c-struct')

const arr = new (struct.int.times(3))()

for (let i = 0; i < arr.length; i++) {
  arr[i].$value = i + 1
}

console.log(arr) // <Buffer 01 00 00 00 02 00 00 00 03 00 00 00>
console.log(arr[1].$value) // 2

union

const struct = require('node-c-struct')

class MyUnion extends struct.union {
  static get fields () {
    return [
      [ 'x', struct.char ],
      [ 'y', struct.int ]
    ]
  }
}

const a = new MyUnion()

a.$value = {
  'x': 266
}

console.log(a) // <Buffer 0a 00 00 00>
console.log(a.x.$value) // 10
console.log(a.y.$value) // 10

a.$value = {
  'y': 267
}

console.log(a) // <Buffer 0b 01 00 00>
console.log(a.x.$value) // 11
console.log(a.y.$value) // 267

struct

const struct = require('node-c-struct')

class Point extends struct.struct {
  static get fields () {
    return [
      [ 'x', struct.int ],
      [ 'y', struct.int ]
    ]
  }
}

const point = new Point({
  x: 1,
  y: 10
})

point.y.$value = 2

console.log(point) // <Buffer 01 00 00 00 02 00 00 00>

complex struct

const struct = require('node-c-struct')

class Point extends struct.struct {
  static get fields () {
    return [
      [ 'x', struct.int ],
      [ 'y', struct.int ]
    ]
  }
}

class MyStruct extends struct.struct {
  static get fields () {
    return [
      [ 'a', struct.char ],
      [ 's', Point ],
      [ 'b', struct.char.times(3) ]
    ]
  }
}

const s = new MyStruct()

s.a.$value = 1
s.s.x.$value = 2
s.s.y.$value = 3
s.b[0].$value = 4
s.b[1].$value = 5
s.b[2].$value = 6

console.log(s) // <Buffer 01 00 00 00 02 00 00 00 03 00 00 00 04 05 06 00>