1.3.0 ā€¢ Published 1 year ago

@thimpat/c-node v1.3.0

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

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

PropertiesDescriptionTypeDefault
outputDirDirectory path for generated the binarystring""
outputFile path for generated binarystringcurrent 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

PropertiesDescriptionTypeDefault
outputDirDirectory path for generated the binarystring""

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);
1.3.0

1 year ago

1.2.2

1 year ago

1.2.1

1 year ago

1.2.0

1 year ago

1.1.2

1 year ago

1.1.1

1 year ago

1.1.0

1 year ago

1.0.0

1 year ago