Skip to content

Commit 233723a

Browse files
committed
[Import as member] Add executable test for C struct members
1 parent 82509cb commit 233723a

File tree

4 files changed

+54
-0
lines changed

4 files changed

+54
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include "IAMVec.h"
2+
#include <math.h>
3+
4+
#ifdef __cplusplus
5+
extern "C" {
6+
#endif
7+
8+
extern double IAMVec3GetNorm(IAMVec3Ref self) {
9+
double x = self->x, y = self->y, z = self->z;
10+
return sqrt(x * x + y * y + z * z);
11+
}
12+
13+
#ifdef __cplusplus
14+
}
15+
#endif
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#ifndef IAMVEC_H
2+
#define IAMVEC_H
3+
4+
#ifdef __cplusplus
5+
extern "C" {
6+
#endif
7+
8+
struct __attribute__((swift_name("Vec3"))) IAMVec3 {
9+
double x, y, z;
10+
};
11+
typedef struct IAMVec3 *IAMVec3Ref;
12+
13+
extern double IAMVec3GetNorm(IAMVec3Ref)
14+
__attribute__((swift_name("getter:Vec3.norm(self:)")));
15+
16+
#ifdef __cplusplus
17+
}
18+
#endif
19+
20+
21+
#endif // IAMVEC_H
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module ImportAsMember {
2+
header "IAMVec.h"
3+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// RUN: rm -rf %t
2+
// RUN: mkdir -p %t
3+
// RUN: %clang %S/Inputs/ImportAsMember/IAMVec.c -c -o %t/IAMVec.o
4+
// RUN: %target-build-swift -I %S/Inputs/ImportAsMember/ -Xlinker %t/IAMVec.o %s -o %t/a.out
5+
// RUN: %target-run %t/a.out | FileCheck %s
6+
7+
// REQUIRES: swift_interpreter
8+
9+
import ImportAsMember
10+
11+
var v3 = Vec3(x: 1.0, y: 2.0, z: 2.0)
12+
print("Norm: \(v3.norm)") // CHECK: Norm: 3.0
13+
14+
// TODO: Test protocols
15+

0 commit comments

Comments
 (0)