Closed achenging closed 5 months ago
The person struct in c environment has different memory address with person object which defined in javascript.
If you want to modify data in situ modification. You can use createPointer
to create a pointer pointed to person struct.
And then, pass pointer to c function. After ffi call, restore person struct data by restorePointer
thanks for your reply, i try to use createPointer to create person struct pointer ,but i don't known it correctly in my case like above up.
//create person pointer ?
const pType = createPointer({
paramsType: [PersonType],
paramsValue: [person]
});
//call c function
const result1 = load({
library: 'test',
funcName: 'createPerson',
paramsType: [DataType.External],
paramsValue: pType,
retType: DataType.I32
});
but it output person->no = 49348832 in C function , not a 100
can you give me a simple sample to test.
const p = createPointer({
paramsType: [{
no: DataType.I32
}],
paramsValue: [{
no: 0
}]
})
load({
library: "libsum",
funcName: "createPerson",
retType: DataType.I32,
paramsType: [DataType.External],
paramsValue: unwrapPointer(p)
})
console.log(
restorePointer({
retType: [{
no: DataType.I32
}],
paramsValue: p
})
)
this is ok! thanks.
HI,i have some problem to use,I can't get value from complex struct value.
typedef struct Test {
int test;
} Test;
typedef struct Person {
struct Test test;
char *name;
int no;
} Person;
int createPerson(Person *person) {
printf("origin no = %d\r\n", person->no);
printf("origin name = %s\r\n", person->name);
printf("origin test = %d\r\n", person->test.test);
printf("--------------------------\r\n");
person->no = 200;
person->name = "ffi-rs-test";
person->test.test = 10086;
printf("modify person, no = %d\r\n", person->no);
printf("modify person, name = %s\r\n", person->name);
printf("modify person, test = %d\r\n", person->test.test);
return 1;
}
const TestType = {
test: DataType.I32
}
const testObj = {test: 10}
const PersonType = {
test: TestType,
name: DataType.String,
no: DataType.I32
};
const person = {
test: testObj,
name: 'ffi-rs',
no: 100
}
const pType = createPointer({
paramsType: [PersonType],
paramsValue: [person]
});
const result1 = load({
library: 'test',
funcName: 'createPerson',
paramsType: [DataType.External],
paramsValue: unwrapPointer(pType),
retType: DataType.I32
});
const a = restorePointer({
retType: [PersonType],
paramsValue: pType
})
console.log(`in js result1 person = ${JSON.stringify(a)}`)
console.log(`result 1 = ${JSON.stringify(person)}`)
console.log("---------------------------\r\n");
output result:
origin no = 100
origin name = ffi-rs
origin test = 57590592 // why is the test value not 10 ?
modify person, no = 200
modify person, name = ffi-rs-test
modify person, test = 10086
in js result1 person = [{"test":{"test":0},"name":"ffi-rs-test","no":200}]
result 1 = {"test":{"test":10},"name":"ffi-rs","no":100}
Use struct pointer if you want to set field of struct is another struct.
typedef struct Test {
int test;
} Test;
typedef struct Person2 {
Test* test;
char *name;
int no;
} Person2;
this struct is not define by me, this define by third part library, i can't change struct Test to struct Test* type. 😂😂😂
ffi-rs has not support this type at present,i will implememt this in the future
ffi-rs support stack struct in 1.0.78
, developer can set ffiTypeTag
field to DataType.StackStruct
to indicate that the struct memory is allocated in stack. ref test.ts
it works good, thanks.
Current ffi-rs version
$ cat node_modules/ffi-rs/package.json
$ ls node_modules/@yuuang
Current Node.js arch
Print current Node.js info with the following code
$ node -e "console.log(process.arch, process.platform)"
Descibe your problem in detail
What's your expect result
it can change person no properties in c function it may be output