Skip to content

Commit 3688d13

Browse files
committed
feat: support DataType.u32
1 parent a7d830d commit 3688d13

File tree

19 files changed

+1485
-1144
lines changed

19 files changed

+1485
-1144
lines changed

.github/workflows/CI.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ jobs:
2424
fail-fast: false
2525
matrix:
2626
settings:
27-
- host: macos-latest
27+
- host: macos-13
2828
target: x86_64-apple-darwin
2929
build: |
3030
rustup -V
@@ -50,7 +50,7 @@ jobs:
5050
target=x86_64-unknown-linux-gnu yarn build &&
5151
strip *.node && yarn test &&
5252
ls
53-
- host: macos-latest
53+
- host: macos-13
5454
target: aarch64-apple-darwin
5555
build: |
5656
uname -a &&

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ dlopen = "0.1.8"
1919
libffi = { version = "3.2.0" }
2020
libffi-sys = { version = "^2.3.0" }
2121
libc = "0.2"
22-
indexmap = "2.7.0"
22+
indexmap = "=2.7.0"
2323
widestring = "1.1.0"
2424
strum = "0.26"
2525
strum_macros = "0.26"

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ Currently, ffi-rs only supports these types of parameters and return values. How
9090
* [i64](#basic-types)
9191
* [bigInt](#basic-types)
9292
* [u64](#basic-types)
93+
* [u32](#basic-types)
9394
* [void](#basic-types) (like js undefined)
9495
* [float](#basic-types) (can only be used as paramsType instead of retType)
9596
* [double](#basic-types)
@@ -159,6 +160,7 @@ extern "C" const char *concatenateStrings(const char *str1, const char *str2) {
159160

160161
extern "C" void noRet() { printf("%s", "hello world"); }
161162
extern "C" bool return_opposite(bool input) { return !input; }
163+
extern "C" uint32_t testU32(uint32_t a, uint32_t b) { return a + b; }
162164
```
163165
164166
### Compile C Code into a Dynamic Library
@@ -288,6 +290,15 @@ equal(!bool_val, load({
288290
paramsType: [DataType.Boolean],
289291
paramsValue: [bool_val],
290292
}))
293+
294+
// U32 type usage
295+
equal(3147483647, load({
296+
library: 'libsum',
297+
funcName: 'testU32',
298+
retType: DataType.U32,
299+
paramsType: [DataType.U32, DataType.U32],
300+
paramsValue: [2147483647, 1000000000],
301+
}))
291302
```
292303

293304
### Buffer

README_Zh.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ $ npm i ffi-rs
7777
* [i64](#基本类型)
7878
* [bigInt](#基本类型)
7979
* [u64](#基本类型)
80+
* [u32](#基本类型)
8081
* [void](#基本类型)(类似js的undefined)
8182
* [float](#基本类型)
8283
* [double](#基本类型)
@@ -156,6 +157,7 @@ extern "C" const char *concatenateStrings(const char *str1, const char *str2) {
156157

157158
extern "C" void noRet() { printf("%s", "hello world"); }
158159
extern "C" bool return_opposite(bool input) { return !input; }
160+
extern "C" uint32_t testU32(uint32_t a, uint32_t b) { return a + b; }
159161

160162
```
161163
@@ -287,6 +289,15 @@ equal(!bool_val, load({
287289
paramsType: [DataType.Boolean],
288290
paramsValue: [bool_val],
289291
}))
292+
293+
// U32 类型用法
294+
equal(3147483647, load({
295+
library: 'libsum',
296+
funcName: 'testU32',
297+
retType: DataType.U32,
298+
paramsType: [DataType.U32, DataType.U32],
299+
paramsValue: [2147483647, 1000000000],
300+
}))
290301
```
291302

292303
### 缓冲区

build.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
11
extern crate napi_build;
2-
macro_rules! p {
3-
($($tokens: tt)*) => {
4-
println!("cargo:warning={}", format!($($tokens)*))
5-
}
6-
}
72

83
fn main() {
94
napi_build::setup();

cpp/sum.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,16 @@ typedef struct {
294294
{
295295
return 0;
296296
}
297+
298+
extern "C" uint32_t testU32(uint32_t a, uint32_t b)
299+
{
300+
return a + b;
301+
}
302+
303+
extern "C" uint32_t doubleU32(uint32_t input)
304+
{
305+
return input * 2;
306+
}
297307
// typedef void (*CallbackType)(const char *);
298308
// extern "C" void call_callback_async() {
299309
// dispatch_async(dispatch_get_main_queue(), ^{

cpp/sum.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#ifndef EXAMPLE_H
22
#define EXAMPLE_H
33
#include <stdbool.h>
4+
#include <stdint.h>
45

56
#ifdef __cplusplus
67
extern "C" {
@@ -42,6 +43,8 @@ typedef struct Person {
4243
} Person;
4344

4445
extern Person *getStruct(Person *person) { return person; };
46+
extern uint32_t testU32(uint32_t a, uint32_t b);
47+
extern uint32_t doubleU32(uint32_t input);
4548
#ifdef __cplusplus
4649
}
4750
#endif

0 commit comments

Comments
 (0)