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

c-for-node

v1.4.0

Published

C-node is a module that allows invoking C code from your nodeJs application. It does not use node-gyp but the TCC compiler.

Downloads

2

Readme

C-node is a module that allows invoking C code from your nodeJs application. The module uses the TCC compiler. It does not use node-gyp. It does not use Python.

Installation

npm install @thimpat/c-node

Usage

CJS

const cNode = require("@thimpat/c-node");

ESM

import cNode from "@thimpat/c-node";

From a shell

// C-node uses the TCC compiler under the hood.
$> c-node [options] 

Quick start

Invoking a function from a .dll in Node

dll.c ↴
#include <windows.h>

// Export function "hello_func" to .dll
__declspec(dllexport) char* hello_func (char* name)
{
    char str[100];
    sprintf(str, "From DLL: %d - %s", 10000, name);
    MessageBox (0, "Hi world!", str, MB_ICONINFORMATION);
    return "All okay";
}

dll.c will be compiled automatically when loadFunctions is invoked

index.js ↴
const {loadFunctions} = require("@thimpat/c-node");

// Import c function "hello_func()" from c library
const {hello_func} = loadFunctions("dll.c", {
    hello_func: {
        // hello_func prototype from dll.c without names (only types)
        prototype: "char* hello_func (char*)",
    }
});

// Invoke dll function
const result = hello_func("My name is awesome");

console.log(result);           // All okay
node index.js

💻 ↴

Execute a c function from Node

💻 ↴

Output result


Examples

All examples are part of the TCC library.

Compilation

Example 1 - Compile a C program to an executable

const {compileSource} = require("@thimpat/c-node");
const {success, status} = compileSource("examples/hello_win.c");
if (!success)
{
    console.error(`Compilation failed. Status: ${status}`)
}

Example 2 - Compile a C source to a shared library (dll)

const {compileLibrary} = require("@thimpat/c-node");
const {success, status} = compileLibrary("examples/dll.c")

Execution

Example 3 - Compile if target nonexistent, then run source

example.js ↴
const {runFile} = require("@thimpat/c-node");
runFile("examples/hello_win.c");

💻 ↴

Message Box Hello World


Run source using JIT (Just in time compilation)

example.js ↴
const {runLive} = require("@thimpat/c-node");

// Runs from source code
runLive("examples/hello_win.c");

API

runFile

Compile then run a source code

Usage

runFile(sourcePath, options);

Options

| Properties | Description | Type | Default | | |------------|-----------------------------------------|--------|---------------------------------------|-----| | outputDir | Directory path for generated the binary | string | "" | | | output | File path for generated binary | string | current location + target binary name | | | | | | | |

Examples

example.js ↴
const {runFile} = require("@thimpat/c-node");

// Generate ./demo/ex1.exe
runFile("examples/ex1.c", {outputDir: "demo/"});

loadBinaryFunctions

Load functions from a .dll

Usage

loadBinaryFunctions("<your-lib>.dll", {
    [funcName]: {
        prototype: "...",
    },
})

Examples

example.js ↴
const {hello_func} = loadBinaryFunctions("dll.dll", {
    hello_func: {
        prototype: "char* hello_func (char*)",
    }
});

const res = hello_func("My name is awesome");
console.log({lid: "NC6452"}, res);

invokeFunction

Call a function from a c source code

Usage

invokeFunction(functionCallString, filePath, {outputDir});

Options

| Properties | Description | Type | Default | | |------------|-----------------------------------------|--------|---------------------------------------|-----| | outputDir | Directory path for generated the binary | string | "" | |

Examples

example.js ↴
const {invokeFunction} = require("@thimpat/c-node");

const result = invokeFunction("hello_func()", "examples/dll.c", {outputDir: "demo/"});
console.log(result);

invokeBinaryFunction

Call a function from a .dll

Examples

example.js ↴
const {invokeFunction} = require("@thimpat/c-node");

const result = invokeBinaryFunction("hello_func()", "examples/dll.dll", {outputDir: "demo/"});
console.log(result);