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

cmem_helpers

v0.0.12

Published

simple & light helpers for working with C-memory

Downloads

21

Readme

cmem_helpers

This provides a few simple & light helpers for working with C-memory. It should be useful for FFI, native node-modules, and browser/node wasm, and has no dependencies. It should also work for other runtimes like bun, deno, or quickjs.

Use it to pass and work with strings, and structs. It is very light and intended for no-emscripten host-code, or situations where you want to do your own thing, a bit.

I also wrote a couple medium posts about how it works:

usage

installation

You can add it to your project like this:

npm i cmem_helpers

And then import or require it:

import memhelpers from 'cmem_helpers'

// OR

const memhelpers = require('cmem_helpers')

You can also use it on the web:

<script type=module>
import memhelpers from 'https://cdn.jsdelivr.net/npm/cmem_helpers/+esm'
</script>

You can also use an importmap to make your code look the same:

<script type="importmap">
{
  "imports": {
    "cmem_helpers": "https://cdn.jsdelivr.net/npm/cmem_helpers/+esm"
  }
}
</script>
<script type=module>
import memhelpers from 'cmem_helpers'
// YOUR CODE HERE
</script>

getting started

Here is an example with WASM, in the browser/nodejs:

import memhelpers from 'cmem_helpers'

// define this to pass functions to WASM
const env = {
  demo(namePtr){
    console.log(`Hello ${getString(namePtr)}!`)
  }
}

// load your bytes in wasmBytes however you do that
const wasmBytes = '...'

const mod = (await WebAssembly.instantiate(wasmBytes, { env })).instance.exports

// here is the actual setup
const { struct, structClass, setString, getString } = memhelpers(mod.memory.buffer, mod.malloc)

The first param is a buffer associated with the memory, and the second is optional, and it's a way to allocate bytes, and get a pointer. In this example, I exposed a function called malloc in my wasm, so I can allocate bytes, in the host. You can see an example in the test wasm.

strings

These are for basic C-style null-terminated UTF-8 strings.

// get a string from a pointer, using /0 termination (standard c-string)
getString(strPtr)

// explicitly tell it the length
getString(strPtr, 100)

// set a string in memory, with length (remember the last /0 char)
setString("Hello", address, 6)

// set a string in memory, without length
setString("Hello", address)

// get a pointer to a new string (if you setup malloc earlier)
const ptr = setString("Hello")

structs

This very simple helper uses DataView to interact directly with the memory.

Valid types are:

  • BigInt64
  • BigUint64
  • Float32
  • Float64
  • Int16
  • Int32
  • Int8
  • Uint16
  • Uint32
  • Uint8

You can define a struct like this:

const Color = struct({
  r: 'Uint8',
  g: 'Uint8',
  b: 'Uint8',
  a: 'Uint8'
})

And now you can make Color objects, with an address, and/or intiial value:

const color = Color({r: 0, g: 0, b: , a: 255}, address)

If you provided a malloc function earlier, when you set it up, you can also do this:

const color = Color()
const color = Color({r: 0, g: 0, b: , a: 255})

And it will allocate it for you. It will have a couple members: _size and _address that you can use in other things, for example to pass the pointer to a function:

mod.useMyColor(color._address)

You can also access the underlying bytes, if you need them:

console.log(color._bytes)

structClass

You can also use structClass, if you like to use them more like classes, and they will work the same:

const Color = structClass({
  r: 'Uint8',
  g: 'Uint8',
  b: 'Uint8',
  a: 'Uint8'
})
const color = new Color()

planned

I have a few ideas for the future:

  • Nested struct fields as pointers (with param for bit-size to support wasm/ffi) or inline-bytes
  • Tool to parse C header and pull out structs, in this format