DEV Community

Jeck Christopher Anog
Jeck Christopher Anog

Posted on

New shared objects linker on JavaScript nodejs u all waiting for!!!

solijs

solijs is an npm package that allows you to dynamically link and execute functions from shared object (.so) files directly within your JavaScript projects. It provides a fast, simple, and efficient way to call C/C++ functions without the need for complex build tools like node-gyp.

Features

  • No Complex Build Tools: Link .so files to your Node.js project without needing node-gyp, ffi, or other build tools.
  • Cross-Platform Support: Works on Linux, macOS, and Windows.
  • Easy to Use API: Call C/C++ functions from .so files with minimal code.
  • Fast Execution: Optimized for performance and minimal overhead.

Installation

To install solijs, use npm or yarn:

npm install solijs
Enter fullscreen mode Exit fullscreen mode

or

yarn add solijs
Enter fullscreen mode Exit fullscreen mode

Usage

1. Create a .so File (e.g., libhello.so)

First, create a shared object file (.so). For example, write a simple C function that returns a value:

// hello.c
#include <stdio.h>

int give_number() {
    return 3 + 2;
}
Enter fullscreen mode Exit fullscreen mode

Compile it into a .so file:

gcc -shared -o libhello.so -fPIC hello.c
Enter fullscreen mode Exit fullscreen mode

2. Create a JavaScript File to Call the Function

Now, use solijs to call the function inside the .so file. Here's an example:

const solijs = require('solijs');
const path = require('path');

const soPath = path.join(__dirname, 'libhello.so');
const symbol = 'give_number'; // The function name in the .so file

solijs.runValue(soPath, symbol)
  .then(result => {
    console.log('Result from .so:', result);
  })
  .catch(err => {
    console.error('Error:', err);
  });
Enter fullscreen mode Exit fullscreen mode

API Reference

runText(soPath, symbol)

This method runs a function from the .so file that doesn't return a value but outputs text to the console.

  • soPath (string): Path to the .so file.
  • symbol (string): The name of the function to execute.

runValue(soPath, symbol)

This method executes a function from the .so file that returns an integer value.

  • soPath (string): Path to the .so file.
  • symbol (string): The name of the function to execute.

inspect(soPath)

This method retrieves the list of symbols available in the .so file.

  • soPath (string): Path to the .so file.

Example Project

You can check out a sample project with the usage examples and the .so files in the GitHub repository:

GitHub: solijs Example

Why You Should Try solijs

If you're looking for a fast and easy way to integrate C/C++ code with JavaScript, solijs is a game-changer. It eliminates the need for complex build processes and gives you the flexibility to use native code directly from your JavaScript environment. Plus, it's optimized for performance, making it ideal for performance-critical applications.

License

MIT

link: https://npmjs.com/package/solijs

Top comments (0)