Skip to content

Introducing https://github.com/dmazzella/node-ctypes #298

@dmazzella

Description

@dmazzella

Hi Falks,
I wanted to introduce you to a new library I have implemented.

node-ctypes

The main objectives are:

  • Full Python ctypes compatibility
  • High Performance (Up to 50x faster than ffi-napi and very similar koffi performance)

Python ctypes:

from ctypes import CDLL, c_int, Structure

libc = CDLL("libc.so.6") # Linux
# libc = CDLL("msvcrt.dll") # Windows
# libc = CDLL("libc.dylib")  # macOS

abs_func = libc.abs
abs_func.argtypes = [c_int]
abs_func.restype = c_int
print(abs_func(-42))  # 42

class Point(Structure):
    _fields_ = [("x", c_int), ("y", c_int)]

p = Point(10, 20)
print(p.x, p.y)  # 10 20

node-ctypes:

import { CDLL, c_int, Structure } from 'node-ctypes';

// Traditional syntax (always available)
const libc = new CDLL("libc.so.6"); // Linux
// const libc = new CDLL('msvcrt.dll');  // Windows
// const libc = new CDLL('libc.dylib');  // macOS

const abs_func = libc.abs;
abs_func.argtypes = [c_int];
abs_func.restype = c_int;
console.log(abs_func(-42));  // 42

class Point extends Structure {
    static _fields_ = [["x", c_int], ["y", c_int]];
}

const p = new Point(10, 20);
console.log(p.x, p.y);  // 10 20

Although it was only recently created, it is in a stable phase.
I am, of course, open to suggestions and support so that we can improve it together.

Regards,
D.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions