Product
Introducing Ruby Support in Socket
Socket is launching Ruby support for all users. Enhance your Rails projects with AI-powered security scans for vulnerabilities and supply chain threats. Now in Beta!
@yuuang/ffi-rs-linux-arm64-musl
Advanced tools
A module written in Rust and N-APi provides interface (FFI) features for Node.js
ffi-rs is a module written in Rust and N-API that provides FFI (Foreign Function Interface) features for Node.js. It allows developers to call functions written in other languages such as C++, C, and Rust directly from JavaScript without writing any C++ code.
This module aims to provide similar functionality to the node-ffi module, but with a completely rewritten underlying codebase. The node-ffi module has been unmaintained for several years and is no longer usable, which is why ffi-rs was developed.
Currently, ffi-rs only supports there types of parameters and return values. However, support for more types will be added in the future based on actual usage scenarios.
Here is an example of how to use ffi-rs:
For below c++ code, we compile this file into a dynamic library
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
extern "C" int sum(int a, int b) { return a + b; }
extern "C" double doubleSum(double a, double b) { return a + b; }
extern "C" const char *concatenateStrings(const char *str1, const char *str2) {
std::string result = std::string(str1) + std::string(str2);
char *cstr = new char[result.length() + 1];
strcpy(cstr, result.c_str());
return cstr;
}
extern "C" void noRet() { printf("%s", "hello world"); }
extern "C" int *createArrayi32(const int *arr, int size) {
int *vec = (int *)malloc((size) * sizeof(int));
for (int i = 0; i < size; i++) {
vec[i] = arr[i];
}
return vec;
}
$ g++ -dynamiclib -o libsum.so cpp/sum.cpp # macos
$ g++ -shared -o libsum.so cpp/sum.cpp # linux
$ g++ -shared -o sum.dll cpp/sum.cpp # win
Then can use ffi-rs
invoke the dynamic library file contains functions.
const { equal } = require('assert')
const { load, RetType, ParamsType } = require('ffi-rs')
const a = 1
const b = 100
const dynamicLib = platform === 'win32' ? './sum.dll' : "./libsum.so"
const r = load({
library: "./libsum.so", // path to the dynamic library file
funcName: 'sum', // the name of the function to call
retType: RetType.I32, // the return value type
paramsType: [ParamsType.I32, ParamsType.I32], // the parameter types
paramsValue: [a, b] // the actual parameter values
})
equal(r, a + b)
const c = "foo"
const d = "bar"
equal(c + d, load({
library: dynamicLib,
funcName: 'concatenateStrings',
retType: RetType.String,
paramsType: [ParamsType.String, ParamsType.String],
paramsValue: [c, d]
}))
equal(undefined, load({
library: dynamicLib,
funcName: 'noRet',
retType: RetType.Void,
paramsType: [],
paramsValue: []
}))
equal(1.1 + 2.2, load({
library: dynamicLib,
funcName: 'doubleSum',
retType: RetType.Double,
paramsType: [ParamsType.Double, ParamsType.Double],
paramsValue: [1.1, 2.2]
}))
let bigArr = new Array(100000).fill(100)
equal(Math.max(bigArr), Math.max(load({
library: dynamicLib,
funcName: 'createArrayi32',
retType: RetType.I32Array,
paramsType: [ParamsType.I32Array, ParamsType.I32],
paramsValue: [bigArr, bigArr.length],
retTypeLen: bigArr.length
})))
FAQs
English | 简体中文
The npm package @yuuang/ffi-rs-linux-arm64-musl receives a total of 16,071 weekly downloads. As such, @yuuang/ffi-rs-linux-arm64-musl popularity was classified as popular.
We found that @yuuang/ffi-rs-linux-arm64-musl demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket is launching Ruby support for all users. Enhance your Rails projects with AI-powered security scans for vulnerabilities and supply chain threats. Now in Beta!
Product
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
Product
We're launching a new set of license analysis and compliance features for analyzing, managing, and complying with licenses across a range of supported languages and ecosystems.