Skip to content

Commit

Permalink
feat: support struct array
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangyuang committed Dec 23, 2024
1 parent 070b3db commit 3e01fea
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 51 deletions.
6 changes: 1 addition & 5 deletions src/utils/object_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,7 @@ pub fn calculate_struct_size(struct_type: &IndexMap<String, RsArgsValue>) -> (us
RefDataType::FloatArray => get_size_align::<f32>(),
RefDataType::StringArray => get_size_align::<*const c_char>(),
RefDataType::DoubleArray => get_size_align::<f64>(),
RefDataType::StructArray => {
let (type_size, type_align) =
calculate_struct_size(struct_item_type.as_ref().unwrap());
(type_size, type_align)
}
RefDataType::StructArray => calculate_struct_size(struct_item_type.as_ref().unwrap()),
};
type_size = type_size * array_len;
let align = align.max(type_align);
Expand Down
92 changes: 46 additions & 46 deletions tests/struct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,52 @@ import { logGreen } from "./utils"
import { person, personType } from "./types"

export const testObject = () => {
const personObj = load({
library: "libsum",
funcName: "getStruct",
retType: personType,
paramsType: [
personType
],
paramsValue: [person],
freeResultMemory: false
});
deepStrictEqual(person, personObj);
logGreen('test getStruct succeed')
const createdPerson = load({
library: "libsum",
funcName: "createPerson",
retType: personType,
paramsType: [],
paramsValue: [],
});
deepStrictEqual(createdPerson, person);
logGreen('test createdPerson succeed')
let personPointer = createPointer({
paramsType: [personType],
paramsValue: [person]
})
const personObjByPointer = load({
library: "libsum",
funcName: "getStruct",
retType: personType,
paramsType: [
DataType.External
],
freeResultMemory: false,
paramsValue: unwrapPointer(personPointer),
});
deepStrictEqual(person, personObjByPointer);
logGreen('test getStructByPointer succeed')
personPointer = createPointer({
paramsType: [personType],
paramsValue: [person]
})
const restorePersonObjByPointer = restorePointer({
paramsValue: personPointer,
retType: [personType]
})
deepStrictEqual(person, restorePersonObjByPointer[0])
const structArray = [{
x: 1,
y: 2,
Expand Down Expand Up @@ -72,50 +118,4 @@ export const testObject = () => {
nNumber: 3,
item: structArray,
})
const personObj = load({
library: "libsum",
funcName: "getStruct",
retType: personType,
paramsType: [
personType
],
paramsValue: [person],
freeResultMemory: false
});
deepStrictEqual(person, personObj);
logGreen('test getStruct succeed')
const createdPerson = load({
library: "libsum",
funcName: "createPerson",
retType: personType,
paramsType: [],
paramsValue: [],
});
deepStrictEqual(createdPerson, person);
logGreen('test createdPerson succeed')
let personPointer = createPointer({
paramsType: [personType],
paramsValue: [person]
})
const personObjByPointer = load({
library: "libsum",
funcName: "getStruct",
retType: personType,
paramsType: [
DataType.External
],
freeResultMemory: false,
paramsValue: unwrapPointer(personPointer),
});
deepStrictEqual(person, personObjByPointer);
logGreen('test getStructByPointer succeed')
personPointer = createPointer({
paramsType: [personType],
paramsValue: [person]
})
const restorePersonObjByPointer = restorePointer({
paramsValue: personPointer,
retType: [personType]
})
deepStrictEqual(person, restorePersonObjByPointer[0])
}

0 comments on commit 3e01fea

Please sign in to comment.