-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
demonstrate and test C-repr structs in FFI example
- Loading branch information
Showing
9 changed files
with
118 additions
and
19 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
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,28 @@ | ||
#include "types.h" | ||
|
||
struct Outer returns() { | ||
struct Outer outer; | ||
outer.a = 1; | ||
|
||
struct Inner inner; | ||
inner.a = 2; | ||
inner.b = 3; | ||
inner.c = 4; | ||
inner.d = 5; | ||
outer.b = inner; | ||
|
||
outer.c = 6; | ||
|
||
return outer; | ||
} | ||
|
||
unsigned int takes(struct Outer outer) { | ||
if (outer.a != 1) return 10; | ||
if (outer.b.a != 2) return 20; | ||
if (outer.b.b != 3) return 30; | ||
if (outer.b.c != 4) return 40; | ||
if (outer.b.d != 5) return 50; | ||
if (outer.c != 6) return 60; | ||
|
||
return 100; | ||
} |
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 |
---|---|---|
@@ -1,8 +1,31 @@ | ||
use std:io | ||
|
||
@[extern "exit", platform ["linux-gnu", "linux-musl"]] | ||
fn my_libc_exit_call as i64 -> () | ||
fn libc_exit as i32 -> () | ||
|
||
fn main = | ||
let exit_code = 5 | ||
in my_libc_exit_call exit_code | ||
let outer = returns in | ||
let n = takes outer in | ||
if n == 100 then | ||
libc_exit 0 | ||
else | ||
libc_exit n | ||
|
||
@[repr "C"] | ||
type Inner { | ||
a u32 | ||
b u8 | ||
c u64 | ||
d u16 | ||
} | ||
|
||
@[repr "C"] | ||
type Outer { | ||
a u16 | ||
b Inner | ||
c u8 | ||
} | ||
|
||
@[extern "takes"] | ||
fn takes as Outer -> i32 | ||
|
||
@[extern "returns"] | ||
fn returns as Outer |
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,16 @@ | ||
struct Inner { | ||
unsigned int a; | ||
unsigned char b; | ||
unsigned long long c; | ||
unsigned short d; | ||
}; | ||
|
||
struct Outer { | ||
unsigned short a; | ||
struct Inner b; | ||
unsigned char c; | ||
}; | ||
|
||
|
||
struct Outer returns(); | ||
unsigned int takes(struct Outer outer); |
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
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
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
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
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