Skip to content

Commit

Permalink
docs: update docs.md
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangyuang committed Apr 10, 2024
1 parent a3ee1ba commit d56ba28
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 30 deletions.
76 changes: 49 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ A module written in Rust and N-API provides interface (FFI) features for Node.js

<div align="">
<a href="https://github.com/zhangyuang/node-ffi-rs/actions" target="_blank"><img src="https://github.com/zhangyuang/ssr/workflows/CI/badge.svg" alt="githubActions" />
<a href="https://github.com/zhangyuang/node-ffi-rs" target="_blank"><img src="https://img.shields.io/badge/node-%3E=14-green.svg?color=4dc71f" alt="Node" ></a>
<a href="https://github.com/zhangyuang/node-ffi-rs" target="_blank"><img src="https://badge.fury.io/js/ffi-rs.svg" alt="Node" ></a>
</div>

## Description
Expand All @@ -22,9 +20,9 @@ This module aims to provide similar functionality to the node-ffi module but wit

- High performance ✨
- Simpler data description and API interface 💗
- Support more data types between `Node.js` and `c type` 😊
- Support more different data types between `Node.js` and `c` 😊
- Support modify data in place 🥸
- Provide many functions to handle pointer type
- Provide many ways to handle pointer type directly

## benchmark

Expand Down Expand Up @@ -406,7 +404,7 @@ The two pieces of code above are equivalent

#### restorePointer

Similarly, you can use `restorePointer` to restore data from `pointer` which is wrapped by `createPointer`
Similarly, you can use `restorePointer` to restore data from `pointer` which is wrapped by `createPointer` or as a return value of foreign function

```js
const pointerArr = createPointer({
Expand Down Expand Up @@ -683,15 +681,16 @@ deepStrictEqual(createdPerson, person);
`ffi-rs` supports passing js function to c, like this

```cpp
typedef void (*FunctionPointer)(int a, bool b, char *c, char **d, int *e,
Person *p);
typedef const void (*FunctionPointer)(int a, bool b, char *c, double d,
char **e, int *f, Person *g);

extern "C" void callFunction(FunctionPointer func) {
printf("callFunction\n");

for (int i = 0; i < 2; i++) {
int a = 100;
bool b = false;
double d = 100.11;
char *c = (char *)malloc(14 * sizeof(char));
strcpy(c, "Hello, World!");

Expand All @@ -705,7 +704,7 @@ extern "C" void callFunction(FunctionPointer func) {
i32Array[2] = 303;

Person *p = createPerson();
func(a, b, c, stringArray, i32Array, p);
func(a, b, c, d, stringArray, i32Array, p);
}
}
```
Expand All @@ -714,39 +713,62 @@ Corresponds to the code above,you can use `ffi-rs` like
```js
let count = 0;
const func = (a, b, c, d, e, f) => {
const func = (a, b, c, d, e, f, g) => {
equal(a, 100);
equal(b, false);
equal(c, "Hello, World!");
deepStrictEqual(d, ["Hello", "world"]);
deepStrictEqual(e, [101, 202, 303]);
deepStrictEqual(f, person);
equal(d, "100.11");
deepStrictEqual(e, ["Hello", "world"]);
deepStrictEqual(f, [101, 202, 303]);
deepStrictEqual(g, person);
console.log("callback called");
count++;
if (count === 2) {
console.log("test succeed");
if (count === 4) {
logGreen("test succeed");
process.exit(0);
}
};
const funcExternal = createPointer({
paramsType: [funcConstructor({
paramsType: [
DataType.I32,
DataType.Boolean,
DataType.String,
DataType.Double,
arrayConstructor({ type: DataType.StringArray, length: 2 }),
arrayConstructor({ type: DataType.I32Array, length: 3 }),
personType,
],
retType: DataType.Void,
})],
paramsValue: [func]
})
load({
library: "libsum",
funcName: "callFunction",
retType: DataType.Void,
paramsType: [funcConstructor({
paramsType: [
DataType.I32,
DataType.Boolean,
DataType.String,
DataType.Double,
arrayConstructor({ type: DataType.StringArray, length: 2 }),
arrayConstructor({ type: DataType.I32Array, length: 3 }),
personType,
],
retType: DataType.Void,
})],
paramsValue: [func]
});
load({
library: "libsum",
funcName: "callFunction",
retType: DataType.Void,
paramsType: [
funcConstructor({
paramsType: [
DataType.I32,
DataType.Boolean,
DataType.String,
arrayConstructor({ type: DataType.StringArray, length: 2 }),
arrayConstructor({ type: DataType.I32Array, length: 3 }),
personType,
],
retType: DataType.Void,
}),
DataType.External,
],
paramsValue: [func],
paramsValue: unwrapPointer(funcExternal),
});
```

Expand Down
3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@
"esno": "^4.7.0",
"shelljs": "^0.8.5"
},
"engines": {
"node": ">= 16"
},
"scripts": {
"artifacts": "napi artifacts",
"build:c": "node scripts/compile.js",
Expand Down

0 comments on commit d56ba28

Please sign in to comment.