-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a test code that imports C structs with array
- Loading branch information
1 parent
9ee4b8b
commit c1828fa
Showing
2 changed files
with
51 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// C header file for testing. | ||
|
||
struct CStruct { | ||
int field[10]; | ||
}; | ||
|
||
struct CStruct2 { | ||
struct CStruct cs; | ||
struct CStruct cs2[2]; | ||
}; | ||
|
||
void setCStruct(struct CStruct* cs) { | ||
cs->field[0] = 567; | ||
cs->field[1] = 8910; | ||
} | ||
|
||
void setCStruct2(struct CStruct2* cs2) { | ||
cs2->cs.field[0] = 111; | ||
cs2->cs.field[1] = 222; | ||
cs2->cs2[0].field[0] = 333; | ||
cs2->cs2[0].field[1] = 4444; | ||
cs2->cs2[1].field[0] = 55555; | ||
cs2->cs2[1].field[1] = 666666; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
type | ||
CInt = int32 | ||
CStruct {.importc: "struct CStruct", header: "ctestarray.h".} = object | ||
field {.importc: "field".}: array[10, CInt] | ||
CStruct2 {.importc: "struct CStruct2", header: "ctestarray.h".} = object | ||
cs {.importc: "cs".}: CStruct | ||
cs2 {.importc: "cs2".}: array[2, CStruct] | ||
|
||
proc setCStruct(cs: ptr CStruct) {.importc: "setCStruct", header: "ctestarray.h"} | ||
proc setCStruct2(cs2: ptr CStruct2) {.importc: "setCStruct2", header: "ctestarray.h"} | ||
|
||
block: | ||
var cstruct: CStruct | ||
setCStruct(addr cstruct) | ||
# TODO: Use doAssert when it become available | ||
discard cstruct.field[0] == 567 | ||
discard cstruct.field[1] == 8910 | ||
|
||
block: | ||
var cstruct2: CStruct2 | ||
setCStruct2(addr cstruct2) | ||
discard cstruct2.cs.field[0] == 111 | ||
discard cstruct2.cs.field[1] == 222 | ||
discard cstruct2.cs2[0].field[0] == 333 | ||
discard cstruct2.cs2[0].field[1] == 444 | ||
discard cstruct2.cs2[1].field[0] == 55555 | ||
discard cstruct2.cs2[1].field[1] == 666666 |