Move everything under kotlin-native folder

I was forced to manually do update the following files, because otherwise
they would be ignored according .gitignore settings. Probably they
should be deleted from repo.

Interop/.idea/compiler.xml
Interop/.idea/gradle.xml
Interop/.idea/libraries/Gradle__org_jetbrains_kotlin_kotlin_runtime_1_0_3.xml
Interop/.idea/libraries/Gradle__org_jetbrains_kotlin_kotlin_stdlib_1_0_3.xml
Interop/.idea/modules.xml
Interop/.idea/modules/Indexer/Indexer.iml
Interop/.idea/modules/Runtime/Runtime.iml
Interop/.idea/modules/StubGenerator/StubGenerator.iml
backend.native/backend.native.iml
backend.native/bc.frontend/bc.frontend.iml
backend.native/cli.bc/cli.bc.iml
backend.native/cli.bc/src/org/jetbrains/kotlin/cli/bc/K2Native.kt
backend.native/cli.bc/src/org/jetbrains/kotlin/cli/bc/K2NativeCompilerArguments.kt
backend.native/tests/link/lib/foo.kt
backend.native/tests/link/lib/foo2.kt
backend.native/tests/teamcity-test.property
This commit is contained in:
Stanislav Erokhin
2020-10-27 21:00:28 +03:00
parent 91e4162dad
commit f624800b84
2830 changed files with 0 additions and 0 deletions
@@ -0,0 +1,14 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the LICENSE file.
*/
import sysstat.*
import kotlinx.cinterop.*
fun main(args: Array<String>) {
val statBuf = nativeHeap.alloc<stat>()
val res = stat("/", statBuf.ptr)
println(res)
println(statBuf.st_uid)
}
@@ -0,0 +1,17 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the LICENSE file.
*/
import cstdlib.*
import kotlinx.cinterop.*
fun main(args: Array<String>) {
println(atoi("257"))
val divResult = div(-5, 3)
val (quot, rem) = divResult.useContents { Pair(quot, rem) }
println(quot)
println(rem)
}
@@ -0,0 +1,10 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the LICENSE file.
*/
import cstdio.*
fun main(args: Array<String>) {
puts("Hello")
}
@@ -0,0 +1,24 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the LICENSE file.
*/
import kotlinx.cinterop.*
fun main(args: Array<String>) {
val values = intArrayOf(14, 12, 9, 13, 8)
val count = values.size
cstdlib.qsort(values.refTo(0), count.convert(), IntVar.size.convert(), staticCFunction { a, b ->
val aValue = a!!.reinterpret<IntVar>()[0]
val bValue = b!!.reinterpret<IntVar>()[0]
(aValue - bValue)
})
for (i in 0..count - 1) {
print(values[i])
print(" ")
}
println()
}
@@ -0,0 +1,22 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the LICENSE file.
*/
import cstdio.*
import kotlinx.cinterop.*
val stdout
get() = getStdout()
fun main(args: Array<String>) {
fprintf(stdout, "%s %s %d %d %d %lld %.1f %.1lf %d %d\n",
"a", "b".cstr, (-1).toByte(), 2.toShort(), 3, Long.MAX_VALUE, 0.1.toFloat(), 0.2, true, false)
memScoped {
val aVar = alloc<IntVar>()
val bVar = alloc<IntVar>()
val sscanfResult = sscanf("42", "%d%d", aVar.ptr, bVar.ptr)
printf("%d %d\n", sscanfResult, aVar.value)
}
}
@@ -0,0 +1,12 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the LICENSE file.
*/
import platform.posix.printf
val golden = immutableBlobOf(0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x21, 0x00).asCPointer(0)
fun main(args: Array<String>) {
printf("%s\n", golden)
}
@@ -0,0 +1,16 @@
import carrayPointers.*
import kotlin.test.*
import kotlinx.cinterop.*
fun main() {
arrayPointer = globalArray
assertEquals(globalArray[0], arrayPointer!![0])
arrayPointer!![0] = 15
assertEquals(15, globalArray[0])
memScoped {
val struct = alloc<StructWithArrayPtr>()
struct.arrayPointer = globalArray
assertEquals(globalArray[0], struct.arrayPointer!![0])
}
}
@@ -0,0 +1,91 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the LICENSE file.
*/
import bitfields.*
import kotlinx.cinterop.*
fun assertEquals(value1: Any?, value2: Any?) {
if (value1 != value2)
throw AssertionError("Expected $value1, got $value2")
}
fun check(s: S, x1: Long, x2: B2, x3: UShort, x4: UInt, x5: Int, x6: Long, x7: E, x8: Boolean) {
assertEquals(x1, s.x1)
assertEquals(x1, getX1(s.ptr))
assertEquals(x2, s.x2)
assertEquals(x2, getX2(s.ptr))
assertEquals(x3, s.x3)
assertEquals(x3, getX3(s.ptr))
assertEquals(x4, s.x4)
assertEquals(x4, getX4(s.ptr))
assertEquals(x5, s.x5)
assertEquals(x5, getX5(s.ptr))
assertEquals(x6, s.x6)
assertEquals(x6, getX6(s.ptr))
assertEquals(x7, s.x7)
assertEquals(x7, getX7(s.ptr))
assertEquals(x8, s.x8)
assertEquals(x8, getX8(s.ptr))
}
fun assign(s: S, x1: Long, x2: B2, x3: UShort, x4: UInt, x5: Int, x6: Long, x7: E, x8: Boolean) {
s.x1 = x1
s.x2 = x2
s.x3 = x3
s.x4 = x4
s.x5 = x5
s.x6 = x6
s.x7 = x7
s.x8 = x8
}
fun assignReversed(s: S, x1: Long, x2: B2, x3: UShort, x4: UInt, x5: Int, x6: Long, x7: E, x8: Boolean) {
s.x8 = x8
s.x7 = x7
s.x6 = x6
s.x5 = x5
s.x4 = x4
s.x3 = x3
s.x2 = x2
s.x1 = x1
}
fun test(s: S, x1: Long, x2: B2, x3: UShort, x4: UInt, x5: Int, x6: Long, x7: E, x8: Boolean) {
assign(s, x1, x2, x3, x4, x5, x6, x7, x8)
check(s, x1, x2, x3, x4, x5, x6, x7, x8)
assignReversed(s, x1, x2, x3, x4, x5, x6, x7, x8)
check(s, x1, x2, x3, x4, x5, x6, x7, x8)
// Also check with some insignificant bits modified:
assign(s, x1 + 2, x2, (x3 + 8u).toUShort(), x4 - 16u, x5 + 32, x6 + Long.MIN_VALUE, x7, x8)
check(s, x1, x2, x3, x4, x5, x6, x7, x8)
assignReversed(s, x1 + 2, x2, (x3 + 8u).toUShort(), x4 - 16u, x5 + 32, x6 + Long.MIN_VALUE, x7, x8)
check(s, x1, x2, x3, x4, x5, x6, x7, x8)
}
fun main(args: Array<String>) {
memScoped {
val s = alloc<S>()
for (x1 in -1L..0L)
for (x2 in B2.values())
for (x3 in 0..7)
for (x4 in uintArrayOf(0u, 6u, 15u))
for (x5 in intArrayOf(-16, -2, -1, 0, 5, 15))
for (x6 in longArrayOf(Long.MIN_VALUE/2, -1L shl 36, -325L, 0, 1L shl 48, Long.MAX_VALUE/2))
for (x7 in E.values())
for (x8 in arrayOf(false, true))
test(s, x1, x2, x3.toUShort(), x4, x5, x6, x7, x8)
}
}
@@ -0,0 +1,28 @@
---
enum B2 {
ZERO, ONE, TWO, THREE
};
enum E : short {
A, B
};
struct __attribute__((packed)) S {
long long x1 : 1;
enum B2 x2 : 2;
unsigned short x3 : 3;
unsigned int x4 : 4;
int x5 : 5;
long long x6 : 63;
enum E x7: 2;
_Bool x8 : 1;
};
static long long getX1(struct S* s) { return s->x1; }
static enum B2 getX2(struct S* s) { return s->x2; }
static unsigned short getX3(struct S* s) { return s->x3; }
static unsigned int getX4(struct S* s) { return s->x4; }
static int getX5(struct S* s) { return s->x5; }
static long long getX6(struct S* s) { return s->x6; }
static enum E getX7(struct S* s) { return s->x7; }
static _Bool getX8(struct S* s) { return s->x8; }
@@ -0,0 +1,70 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the LICENSE file.
*/
import kotlin.test.*
import kotlinx.cinterop.*
import ccallbacksAndVarargs.*
fun main() {
testStructCallbacks()
testVarargs()
testCallableReferences()
}
fun testStructCallbacks() {
assertEquals(42, getX(staticCFunction { -> cValue<S> { x = 42 } }))
applyCallback(cValue { x = 17 }, staticCFunction { it: CValue<S> ->
assertEquals(17, it.useContents { x })
})
assertEquals(66, makeS(66, 111).useContents { x })
}
fun testVarargs() {
assertEquals(E.ONE, makeE(1))
getVarargs(
0,
true,
2.toByte(),
Short.MIN_VALUE,
42,
Long.MAX_VALUE,
3.14f,
2.71,
0x1234ABCDL.toCPointer<COpaque>(),
UByte.MAX_VALUE,
22.toUShort(),
111u,
ULong.MAX_VALUE,
E.TWO,
cValue<S> { x = 15 },
null
).useContents {
assertEquals(1, a1)
assertEquals(2.toByte(), a2)
assertEquals(Short.MIN_VALUE, a3)
assertEquals(42, a4)
assertEquals(Long.MAX_VALUE, a5)
assertEquals(3.14f, a6)
assertEquals(2.71, a7)
assertEquals(0x1234ABCDL, a8.toLong())
assertEquals(UByte.MAX_VALUE, a9)
assertEquals(22.toUShort(), a10)
assertEquals(111u, a11)
assertEquals(ULong.MAX_VALUE, a12)
assertEquals(E.TWO, a13)
assertEquals(15, a14.x)
assertEquals(null, a15)
}
}
fun testCallableReferences() {
val sumRef = ::sum
assertEquals(3, sumRef(1, 2))
val sumPtr = staticCFunction(::sum)
assertEquals(7, sumPtr(3, 4))
}
@@ -0,0 +1,8 @@
---
int (*arrayPointer)[1];
int globalArray[3] = {1, 2, 3};
struct StructWithArrayPtr {
int (*arrayPointer)[1];
};
@@ -0,0 +1,78 @@
---
#include <stdarg.h>
struct S {
int x;
};
static int getX(struct S (*callback)(void)) {
return callback().x;
}
static void applyCallback(struct S s, void (*callback)(struct S)) {
callback(s);
}
static struct S makeS(int x, ...) {
return (struct S){ x };
}
enum E {
ZERO, ONE, TWO
};
static enum E makeE(int ordinal, ...) {
return ordinal;
}
struct Args {
char a1;
char a2;
short a3;
int a4;
long long a5;
float a6;
double a7;
void* a8;
unsigned char a9;
unsigned short a10;
unsigned int a11;
unsigned long long a12;
enum E a13;
struct S a14;
void* a15;
};
static struct Args getVarargs(int ignore, ...) {
va_list args;
va_start(args, ignore);
struct Args result = {
va_arg(args, char),
va_arg(args, char),
va_arg(args, short),
va_arg(args, int),
va_arg(args, long long),
va_arg(args, double),
va_arg(args, double),
va_arg(args, void*),
va_arg(args, unsigned char),
va_arg(args, unsigned short),
va_arg(args, unsigned int),
va_arg(args, unsigned long long),
va_arg(args, enum E),
va_arg(args, struct S),
va_arg(args, void*)
};
va_end(args);
return result;
}
static int sum(int first, int second) {
return first + second;
}
@@ -0,0 +1,4 @@
---
enum E {
A, B, C
};
@@ -0,0 +1,94 @@
headerFilter = NOTHING
---
#include <stdio.h>
#include <stdlib.h>
typedef int (*atoiPtrType)(const char*);
static atoiPtrType getAtoiPtr() {
return &atoi;
}
static void __printInt(int x) {
printf("%d\n", x);
}
static void* __getPrintIntPtr() {
return &__printInt;
}
typedef void* (*getPrintIntPtrPtrType)(void);
static getPrintIntPtrPtrType getGetPrintIntPtrPtr() {
return &__getPrintIntPtr;
}
static double __add(double x, double y) {
return x + y;
}
typedef double (*addPtrType)(double, double);
static addPtrType getAddPtr() {
return &__add;
}
static int __doubleToInt(double x) {
return (int) x;
}
typedef int (*doubleToIntPtrType)(double);
static doubleToIntPtrType getDoubleToIntPtr() {
return &__doubleToInt;
}
static _Bool __isIntPositive(int x) {
return x > 0;
}
typedef _Bool (*isIntPositivePtrType)(int);
static isIntPositivePtrType getIsIntPositivePtr() {
return &__isIntPositive;
}
static unsigned int getMaxUInt(void) {
return 0xffffffff;
}
static typeof(&getMaxUInt) getMaxUIntGetter() {
return &getMaxUInt;
}
typedef int (*longSignatureFunctionPtrType)(
int, int, int, int, int, int, int, int, int, int, int, int,
int, int, int, int, int, int, int, int, int, int, int
);
typedef int (*notSoLongSignatureFunctionPtrType)(
int, int, int, int, int, int, int, int, int, int, int,
int, int, int, int, int, int, int, int, int, int, int
);
static int longSignatureFunction(
int p1, int p2, int p3, int p4, int p5, int p6, int p7, int p8, int p9, int p10, int p11, int p12,
int p13, int p14, int p15, int p16, int p17, int p18, int p19, int p20, int p21, int p22, int p23
) {
return 42;
}
static int notSoLongSignatureFunction(
int p1, int p2, int p3, int p4, int p5, int p6, int p7, int p8, int p9, int p10, int p11, int p12,
int p13, int p14, int p15, int p16, int p17, int p18, int p19, int p20, int p21, int p22
) {
return 42;
}
static longSignatureFunctionPtrType getLongSignatureFunctionPtr() {
return &longSignatureFunction;
}
static notSoLongSignatureFunctionPtrType getNotSoLongSignatureFunctionPtr() {
return &notSoLongSignatureFunction;
}
@@ -0,0 +1,33 @@
---
const int g1 = 42;
int g2 = 17;
struct S {
int x;
} g3 = { 128 };
int g4[2] = { 13, 14 };
int g5[2][2] = { 15, 16, 17, 18 };
struct S* const g6 = &g3;
void foo() {
// Test that local vars are not treated as global ones.
float g1;
}
// Test non-compilable variable:
typedef int MyInt;
MyInt g7;
#define g7 bad macro
// Test property name mangling:
struct g1 {};
struct g1_ {};
typedef void* voidptr;
_Pragma("clang assume_nonnull begin")
const voidptr g8 = 0x1, g9 = 0x2;
_Pragma("clang assume_nonnull end")
@@ -0,0 +1,78 @@
excludedMacros = EXCLUDED
---
int* ptr_call() {
return (int*) 1;
}
int int_call() {
return 42;
}
void void_call() {}
int arg_call(int x) {
return x;
}
typedef struct {
int value
} struct_t;
struct_t getStruct() {
return (struct_t){ 1 };
}
int global_var = 5;
#define TOO_MANY_ERRORS x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x
#define LEFT_BRACE {
#define ZERO 0
#define ONE 1
#define RIGHT_BRACE }
#define MAX_LONG 9223372036854775807
#define FOO_STRING "foo"
// This one should be ignored:
#define WIDE_FOO_STRING L"foo"
#define FOURTY_TWO 42
#define SEVENTEEN ((long long) 17)
#define ONE_POINT_ZERO 1.0
#define ONE_POINT_FIVE 1.5f
#define LEFT_PAREN (
#define NULL_PTR ((void*)0)
#define VOID_PTR ((void*)1)
#define INT_PTR ((int*)1)
#define PTR_SUM (INT_PTR + 1)
#define PTR_SUM_EXPECTED (sizeof(int) + 1)
#define RIGHT_PAREN )
enum {
INT_CALL = 1 // Should be replaced by macro below.
};
#define PTR_CALL ptr_call()
#define INT_CALL int_call()
#define CALL_SUM (int_call() + int_call())
#define GLOBAL_VAR (global_var)
// This one should be excluded:
#define EXCLUDED 42
// These ones should be ignored:
#define VOID_CALL (void_call())
#define ARG_CALL(x) (arg_call(x))
#define GET_STRUCT (getStruct())
#define UNDECLARED (undeclared())
#define BAD1 bar
#define BAD2 5;
#define BAD3 { foo(); }
void increment(int* counter);
#define increment(counter) { (*(counter))++; }
@@ -0,0 +1,8 @@
headers = stdio.h
compilerOpts.osx = -D_ANSI_SOURCE
---
static inline FILE* getStdout() {
return stdout;
}
@@ -0,0 +1,41 @@
nonStrictEnums = NonStrict
---
typedef struct {
int i;
} Trivial;
enum E {
R, G, B
};
enum NonStrict {
N, S, K
};
struct Complex {
unsigned int ui;
Trivial t;
struct Complex* next;
enum E e;
enum NonStrict nonStrict;
int arr[2];
_Bool b;
};
struct __attribute__((packed)) Packed {
int i : 1;
enum E e : 2;
};
struct Complex produceComplex() {
struct Complex complex = {
.ui = 128,
.t = {1},
.next = 0,
.e = R,
.nonStrict = K,
.arr = {-51, -19},
.b = 1
};
return complex;
};
@@ -0,0 +1,89 @@
---
// KT-28065
struct StructWithConstFields {
int x;
const int y;
};
struct StructWithConstFields getStructWithConstFields() {
struct StructWithConstFields result = { 111, 222 };
return result;
}
enum ForwardDeclaredEnum;
enum ForwardDeclaredEnum {
ZERO, ONE, TWO
};
static int vlaSum(int size, int array[size]) {
int result = 0;
for (int i = 0; i < size; ++i) {
result += array[i];
}
return result;
}
static int vlaSum2D(int size, int array[][size]) {
int result = 0;
for (int i = 0; i < size; ++i) {
for (int j = 0; j < size; ++j) {
result += array[i][j];
}
}
return result;
}
static int vlaSum2DBothDimensions(int rows, int columns, int array[rows][columns]) {
int result = 0;
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < columns; ++j) {
result += array[i][j];
}
}
return result;
}
/*
// Not supported by clang:
static int vlaSum2DForward(int size; int array[][size], int size) {
return vlaSum2D(size, array);
}
*/
// "Strict" enums heuristic based on whether enum constants are defined explicitly:
enum StrictEnum1 {
StrictEnum1A,
StrictEnum1B
};
enum StrictEnum2 {
StrictEnum2A __attribute__((unused)),
StrictEnum2B __attribute__((unused))
};
enum NonStrictEnum1 {
NonStrictEnum1A = 0,
NonStrictEnum1B __attribute__((unused))
};
enum NonStrictEnum2 {
NonStrictEnum2A,
NonStrictEnum2B __attribute__((unused)) = 1
};
// KT-34025
typedef char Char;
enum EnumCharBase : Char {
EnumCharBaseA,
EnumCharBaseB
};
static int sendEnum(enum EnumCharBase x) {
return (int)x + 2;
}
enum EnumExplicitChar : char {
EnumExplicitCharA = 'a',
EnumExplicitCharB = 'b',
EnumExplicitCharDup = 'a'
};
@@ -0,0 +1,17 @@
---
typedef union {
short s;
long long ll;
} BasicUnion;
typedef struct {
union {
int i;
float f;
} as;
} StructWithUnion;
typedef union {
unsigned int i : 31;
unsigned char b : 1;
} Packed;
@@ -0,0 +1,27 @@
compilerOpts = -mno-xsave
---
static void noAttr() {}
__attribute__((always_inline)) noTargetAttr() {}
__attribute__((always_inline, __target__("xsave"))) void plainAttrs1() {}
__attribute__((__target__("xsave"), always_inline)) void plainAttrs2() {}
#define TARGET __target__
__attribute__((always_inline, TARGET("xsave"))) void macroAttr1() {}
__attribute__((TARGET("xsave"), always_inline)) void macroAttr2() {}
#define TARGET_ATTR __target__("xsave")
__attribute__((TARGET_ATTR, always_inline)) void macroAttr3() {}
__attribute__((always_inline, TARGET_ATTR)) void macroAttr4() {}
#define ALL_ATTRS1 __attribute__((always_inline, __target__("xsave")))
ALL_ATTRS1 void macroAttr5() {}
#define ALL_ATTRS2 __attribute__((always_inline, __target__("xsave")))
ALL_ATTRS2 void macroAttr6() {}
@@ -0,0 +1,7 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int custom_strcmp(const char* str1, const char* str2) {
return strcmp(str1, str2);
}
@@ -0,0 +1,10 @@
---
_Bool isNullString(const char* str) {
return str == (const char*)0;
}
typedef const short* LPCWSTR;
_Bool isNullWString(LPCWSTR str) {
return str == (LPCWSTR)0;
}
@@ -0,0 +1,28 @@
---
typedef float __attribute__ ((__vector_size__ (16))) KVector4f;
typedef int __attribute__ ((__vector_size__ (16))) KVector4i32;
struct Complex {
unsigned int ui;
KVector4f vec4f;
struct Complex* next;
int arr[2];
};
struct Complex produceComplex() {
struct Complex complex = {
.ui = 128,
.vec4f = {1.0, 1.0, 1.0, 1.0},
.next = 0,
.arr = {-51, -19}
};
return complex;
};
static float sendV4F(KVector4f v) {
return v[0] + 2 * v[1] + 4 * v[2] + 8 * v[3];
}
static int sendV4I(KVector4i32 v) {
return v[0] + 2 * v[1] + 4 * v[2] + 8 * v[3];
}
@@ -0,0 +1,69 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the LICENSE file.
*/
import kotlinx.cinterop.*
import sockets.*
fun main(args: Array<String>) {
if (args.size < 1) {
println("Usage: ./echo_server <port>")
return
}
val port = atoi(args[0]).toUShort()
memScoped {
val bufferLength = 100L
val buffer = allocArray<ByteVar>(bufferLength)
val serverAddr = alloc<sockaddr_in>()
val listenFd = socket(AF_INET, SOCK_STREAM, 0)
.toInt().ensureUnixCallResult { it >= 0 }
with(serverAddr) {
memset(this.ptr, 0, sockaddr_in.size.convert())
sin_family = AF_INET.convert()
sin_addr.s_addr = htons(0u).convert()
sin_port = htons(port)
}
bind(listenFd, serverAddr.ptr.reinterpret(), sockaddr_in.size.toUInt())
.toInt().ensureUnixCallResult { it == 0 }
listen(listenFd, 10)
.toInt().ensureUnixCallResult { it == 0 }
val commFd = accept(listenFd, null, null)
.toInt().ensureUnixCallResult { it >= 0 }
while (true) {
val length = read(commFd, buffer, bufferLength.convert())
.toInt().ensureUnixCallResult { it >= 0 }
if (length == 0) {
break
}
write(commFd, buffer, length.convert())
.toInt().ensureUnixCallResult { it >= 0 }
}
}
}
// Not available through interop because declared as macro:
fun htons(value: UShort) = ((value.toInt() ushr 8) or (value.toInt() shl 8)).toUShort()
fun throwUnixError(): Nothing {
perror(null) // TODO: store error message to exception instead.
throw Error("UNIX call failed")
}
inline fun Int.ensureUnixCallResult(predicate: (Int) -> Boolean): Int {
if (!predicate(this)) {
throwUnixError()
}
return this
}
@@ -0,0 +1,15 @@
import cenums.*
import kotlinx.cinterop.*
import kotlin.test.*
fun main() {
memScoped {
val e = alloc<E.Var>()
e.value = E.C
assertEquals(E.C, e.value)
assertFailsWith<NotImplementedError> {
e.value = TODO()
}
}
}
@@ -0,0 +1,46 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the LICENSE file.
*/
import kotlinx.cinterop.*
import cfunptr.*
import kotlin.test.*
typealias NotSoLongSignatureFunction = (
Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int,
Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int
) -> Int
fun main(args: Array<String>) {
val atoiPtr = getAtoiPtr()!!
val getPrintIntPtrPtr = getGetPrintIntPtrPtr()!!
val printIntPtr = getPrintIntPtrPtr()!!.reinterpret<CFunction<(Int) -> Unit>>()
val fortyTwo = memScoped {
atoiPtr("42".cstr.getPointer(memScope))
}
printIntPtr(fortyTwo)
printIntPtr(
getDoubleToIntPtr()!!(
getAddPtr()!!(5.1, 12.2)
)
)
val isIntPositivePtr = getIsIntPositivePtr()!!
printIntPtr(isIntPositivePtr(42).ifThenOneElseZero())
printIntPtr(isIntPositivePtr(-42).ifThenOneElseZero())
assertEquals(getMaxUIntGetter()!!(), UInt.MAX_VALUE)
val longSignaturePtr: COpaquePointer? = getLongSignatureFunctionPtr()
val notSoLongSignaturePtr: CPointer<CFunction<NotSoLongSignatureFunction>>? = getNotSoLongSignatureFunctionPtr()
printIntPtr(notSoLongSignaturePtr!!.invoke(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0))
printIntPtr(notSoLongSignatureFunction(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0))
}
fun Boolean.ifThenOneElseZero() = if (this) 1 else 0
@@ -0,0 +1,33 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the LICENSE file.
*/
import kotlinx.cinterop.*
import cglobals.*
fun main(args: Array<String>) {
assert(g1__ == 42)
assert(g2 == 17)
g2 = 42
assert(g2 == 42)
assert(g3.x == 128)
g3.x = 7
assert(g3.x == 7)
assert(g4[1] == 14)
g4[1] = 15
assert(g4[1] == 15)
assert(g5[0] == 15)
assert(g5[3] == 18)
g5[0] = 16
assert(g5[0] == 16)
assert(g6 == g3.ptr)
assert(g8.toLong() == 0x1L)
assert(g9.toLong() == 0x2L)
}
@@ -0,0 +1,48 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the LICENSE file.
*/
import kotlin.test.*
import cmacros.*
import kotlinx.cinterop.*
fun main(args: Array<String>) {
assertEquals("foo", FOO_STRING)
assertEquals(0, ZERO)
assertEquals(1, ONE)
assertEquals(Long.MAX_VALUE, MAX_LONG)
assertEquals(42, FOURTY_TWO)
val seventeen: Long = SEVENTEEN
assertEquals(17L, seventeen)
val onePointFive: Float = ONE_POINT_FIVE
val onePointZero: Double = ONE_POINT_ZERO
assertEquals(1.5f, onePointFive)
assertEquals(1.0, onePointZero)
val nullPtr: COpaquePointer? = NULL_PTR
val voidPtr: COpaquePointer? = VOID_PTR
val intPtr: CPointer<IntVar>? = INT_PTR
val ptrSum: CPointer<IntVar>? = PTR_SUM
val ptrCall: CPointer<IntVar>? = PTR_CALL
assertEquals(null, nullPtr)
assertEquals(1L, voidPtr.rawValue.toLong())
assertEquals(1L, intPtr.rawValue.toLong())
assertEquals(PTR_SUM_EXPECTED.toLong(), ptrSum.rawValue.toLong())
assertEquals(1L, ptrCall.rawValue.toLong())
assertEquals(42, INT_CALL)
assertEquals(84, CALL_SUM)
assertEquals(5, GLOBAL_VAR)
memScoped {
val counter = alloc<IntVar>()
counter.value = 42
increment(counter.ptr)
assertEquals(43, counter.value)
}
}
@@ -0,0 +1,6 @@
---
// test mangling of special names
enum _Companion {Companion, Any};
enum _Companion companion = Companion;
@@ -0,0 +1,9 @@
import kotlinx.cinterop.*
import kotlin.test.*
import mangling.*
fun main() {
companion = _Companion.`Companion$`
assertEquals(_Companion.`Companion$`, companion)
}
@@ -0,0 +1,6 @@
---
// test mangling of special names
enum Companion {One, Two};
@@ -0,0 +1,9 @@
import kotlinx.cinterop.*
import kotlin.test.*
import mangling2.*
fun main() {
val mangled = `Companion$`.Two
assertEquals(`Companion$`.Two, mangled)
}
@@ -0,0 +1,21 @@
---
#define as "as"
#define class "class"
#define dynamic "dynamic"
#define false "false"
#define fun "fun"
#define in "in"
#define interface "interface"
#define is "is"
#define null "null"
#define object "object"
#define package "package"
#define super "super"
#define this "this"
#define throw "throw"
#define true "true"
#define try "try"
#define typealias "typealias"
#define val "val"
#define var "var"
#define when "when"
@@ -0,0 +1,27 @@
import kotlin.test.*
import mangling_keywords.*
fun main() {
// Check that all Kotlin keywords are imported and mangled.
assertEquals("as", `as`)
assertEquals("class", `class`)
assertEquals("dynamic", `dynamic`)
assertEquals("false", `false`)
assertEquals("fun", `fun`)
assertEquals("in", `in`)
assertEquals("interface", `interface`)
assertEquals("is", `is`)
assertEquals("null", `null`)
assertEquals("object", `object`)
assertEquals("package", `package`)
assertEquals("super", `super`)
assertEquals("this", `this`)
assertEquals("throw", `throw`)
assertEquals("true", `true`)
assertEquals("try", `try`)
assertEquals("typealias", `typealias`)
assertEquals("val", `val`)
assertEquals("var", `var`)
assertEquals("when", `when`)
}
@@ -0,0 +1,72 @@
---
enum KotlinKeywordsEnum {
as,
class,
dynamic,
false,
fun,
in,
interface,
is,
null,
object,
package,
super,
this,
throw,
true,
try,
typealias,
val,
var,
when,
};
struct KotlinKeywordsStruct {
int as;
int class;
int dynamic;
int false;
int fun;
int in;
int interface;
int is;
int null;
int object;
int package;
int super;
int this;
int throw;
int true;
int try;
int typealias;
int val;
int var;
int when;
};
struct KotlinKeywordsStruct createKotlinKeywordsStruct() {
struct KotlinKeywordsStruct s = {
.as = 0,
.class = 0,
.dynamic = 0,
.false = 0,
.fun = 0,
.in = 0,
.interface = 0,
.is = 0,
.null = 0,
.object = 0,
.package = 0,
.super = 0,
.this = 0,
.throw = 0,
.true = 0,
.try = 0,
.typealias = 0,
.val = 0,
.var = 0,
.when = 0,
};
return s;
}
@@ -0,0 +1,50 @@
import kotlin.test.*
import mangling_keywords2.*
import kotlinx.cinterop.useContents
fun main() {
// Check that all Kotlin keywords are imported and mangled.
createKotlinKeywordsStruct().useContents {
assertEquals(0, `as`)
assertEquals(0, `class`)
assertEquals(0, `dynamic`)
assertEquals(0, `false`)
assertEquals(0, `fun`)
assertEquals(0, `in`)
assertEquals(0, `interface`)
assertEquals(0, `is`)
assertEquals(0, `null`)
assertEquals(0, `object`)
assertEquals(0, `package`)
assertEquals(0, `super`)
assertEquals(0, `this`)
assertEquals(0, `throw`)
assertEquals(0, `true`)
assertEquals(0, `try`)
assertEquals(0, `typealias`)
assertEquals(0, `val`)
assertEquals(0, `var`)
assertEquals(0, `when`)
}
assertEquals(KotlinKeywordsEnum.`as`, KotlinKeywordsEnum.`as`)
assertEquals(KotlinKeywordsEnum.`class`, KotlinKeywordsEnum.`class`)
assertEquals(KotlinKeywordsEnum.`dynamic`, KotlinKeywordsEnum.`dynamic`)
assertEquals(KotlinKeywordsEnum.`false`, KotlinKeywordsEnum.`false`)
assertEquals(KotlinKeywordsEnum.`fun`, KotlinKeywordsEnum.`fun`)
assertEquals(KotlinKeywordsEnum.`in`, KotlinKeywordsEnum.`in`)
assertEquals(KotlinKeywordsEnum.`interface`, KotlinKeywordsEnum.`interface`)
assertEquals(KotlinKeywordsEnum.`is`, KotlinKeywordsEnum.`is`)
assertEquals(KotlinKeywordsEnum.`null`, KotlinKeywordsEnum.`null`)
assertEquals(KotlinKeywordsEnum.`object`, KotlinKeywordsEnum.`object`)
assertEquals(KotlinKeywordsEnum.`package`, KotlinKeywordsEnum.`package`)
assertEquals(KotlinKeywordsEnum.`super`, KotlinKeywordsEnum.`super`)
assertEquals(KotlinKeywordsEnum.`this`, KotlinKeywordsEnum.`this`)
assertEquals(KotlinKeywordsEnum.`throw`, KotlinKeywordsEnum.`throw`)
assertEquals(KotlinKeywordsEnum.`true`, KotlinKeywordsEnum.`true`)
assertEquals(KotlinKeywordsEnum.`try`, KotlinKeywordsEnum.`try`)
assertEquals(KotlinKeywordsEnum.`typealias`, KotlinKeywordsEnum.`typealias`)
assertEquals(KotlinKeywordsEnum.`val`, KotlinKeywordsEnum.`val`)
assertEquals(KotlinKeywordsEnum.`var`, KotlinKeywordsEnum.`var`)
assertEquals(KotlinKeywordsEnum.`when`, KotlinKeywordsEnum.`when`)
}
@@ -0,0 +1,116 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the LICENSE file.
*/
import kotlinx.cinterop.*
import platform.GLUT.*
import platform.OpenGL.*
import platform.OpenGLCommon.*
// Ported from http://openglsamples.sourceforge.net/projects/index.php/blog/index/
private var rotation: GLfloat = 0.0f
private val rotationSpeed: GLfloat = 0.2f
private val windowWidth = 640
private val windowHeight = 480
fun display() {
// Clear Screen and Depth Buffer
glClear((GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT).convert())
glLoadIdentity()
// Define a viewing transformation
gluLookAt(4.0, 2.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0)
// Push and pop the current matrix stack.
// This causes that translations and rotations on this matrix wont influence others.
glPushMatrix()
glColor3f(1.0f, 0.0f, 0.0f)
glTranslatef(0.0f, 0.0f, 0.0f)
glRotatef(rotation, 0.0f, 1.0f, 0.0f)
glRotatef(90.0f, 0.0f, 1.0f, 0.0f)
// Draw the teapot
glutSolidTeapot(1.0)
glPopMatrix()
rotation += rotationSpeed
glutSwapBuffers()
}
fun initialize() {
// select projection matrix
glMatrixMode(GL_PROJECTION)
// set the viewport
glViewport(0, 0, windowWidth, windowHeight)
// set matrix mode
glMatrixMode(GL_PROJECTION)
// reset projection matrix
glLoadIdentity()
val aspect = windowWidth.toDouble() / windowHeight
// set up a perspective projection matrix
gluPerspective(45.0, aspect, 1.0, 500.0)
// specify which matrix is the current matrix
glMatrixMode(GL_MODELVIEW)
glShadeModel(GL_SMOOTH)
// specify the clear value for the depth buffer
glClearDepth(1.0)
glEnable(GL_DEPTH_TEST)
glDepthFunc(GL_LEQUAL)
// specify implementation-specific hints
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, cValuesOf(0.1f, 0.1f, 0.1f, 1.0f))
glLightfv(GL_LIGHT0, GL_DIFFUSE, cValuesOf(0.6f, 0.6f, 0.6f, 1.0f))
glLightfv(GL_LIGHT0, GL_SPECULAR, cValuesOf(0.7f, 0.7f, 0.3f, 1.0f))
glEnable(GL_LIGHT0)
glEnable(GL_COLOR_MATERIAL)
glShadeModel(GL_SMOOTH)
glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE)
glDepthFunc(GL_LEQUAL)
glEnable(GL_DEPTH_TEST)
glEnable(GL_LIGHTING)
glEnable(GL_LIGHT0)
glClearColor(0.0f, 0.0f, 0.0f, 1.0f)
}
fun main(args: Array<String>) {
// initialize and run program
memScoped {
val argc = alloc<IntVar>().apply { value = 0 }
glutInit(argc.ptr, null) // TODO: pass real args
}
// Display Mode
glutInitDisplayMode((GLUT_RGB or GLUT_DOUBLE or GLUT_DEPTH).convert())
// Set window size
glutInitWindowSize(windowWidth, windowHeight)
// create Window
glutCreateWindow("The GLUT Teapot")
// register Display Function
glutDisplayFunc(staticCFunction(::display))
// register Idle Function
glutIdleFunc(staticCFunction(::display))
initialize()
// run GLUT mainloop
glutMainLoop()
}
@@ -0,0 +1,247 @@
/*
* Copyright 2010-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the LICENSE file.
*/
import kotlin.test.*
import kotlinx.cinterop.*
@Test fun pinnedByteArrayAddressOf() {
val arr = ByteArray(10) { 0 }
arr.usePinned {
assertEquals(0, it.addressOf(0).pointed.value)
assertEquals(0, it.addressOf(9).pointed.value)
assertFailsWith<ArrayIndexOutOfBoundsException> {
it.addressOf(10)
}
assertFailsWith<ArrayIndexOutOfBoundsException> {
it.addressOf(-1)
}
assertFailsWith<ArrayIndexOutOfBoundsException> {
it.addressOf(Int.MAX_VALUE)
}
assertFailsWith<ArrayIndexOutOfBoundsException> {
it.addressOf(Int.MIN_VALUE)
}
}
}
@Test fun pinnedStringAddressOf() {
val str = "0000000000"
str.usePinned {
it.addressOf(0)
it.addressOf(9)
assertFailsWith<ArrayIndexOutOfBoundsException> {
it.addressOf(10)
}
assertFailsWith<ArrayIndexOutOfBoundsException> {
it.addressOf(-1)
}
assertFailsWith<ArrayIndexOutOfBoundsException> {
it.addressOf(Int.MAX_VALUE)
}
assertFailsWith<ArrayIndexOutOfBoundsException> {
it.addressOf(Int.MIN_VALUE)
}
}
}
@Test fun pinnedCharArrayAddressOf() {
val arr = CharArray(10) { '0' }
arr.usePinned {
it.addressOf(0)
it.addressOf(9)
assertFailsWith<ArrayIndexOutOfBoundsException> {
it.addressOf(10)
}
assertFailsWith<ArrayIndexOutOfBoundsException> {
it.addressOf(-1)
}
assertFailsWith<ArrayIndexOutOfBoundsException> {
it.addressOf(Int.MAX_VALUE)
}
assertFailsWith<ArrayIndexOutOfBoundsException> {
it.addressOf(Int.MIN_VALUE)
}
}
}
@Test fun pinnedShortArrayAddressOf() {
val arr = ShortArray(10) { 0 }
arr.usePinned {
assertEquals(0, it.addressOf(0).pointed.value)
assertEquals(0, it.addressOf(9).pointed.value)
assertFailsWith<ArrayIndexOutOfBoundsException> {
it.addressOf(10)
}
assertFailsWith<ArrayIndexOutOfBoundsException> {
it.addressOf(-1)
}
assertFailsWith<ArrayIndexOutOfBoundsException> {
it.addressOf(Int.MAX_VALUE)
}
assertFailsWith<ArrayIndexOutOfBoundsException> {
it.addressOf(Int.MIN_VALUE)
}
}
}
@Test fun pinnedIntArrayAddressOf() {
val arr = IntArray(10) { 0 }
arr.usePinned {
assertEquals(0, it.addressOf(0).pointed.value)
assertEquals(0, it.addressOf(9).pointed.value)
assertFailsWith<ArrayIndexOutOfBoundsException> {
it.addressOf(10)
}
assertFailsWith<ArrayIndexOutOfBoundsException> {
it.addressOf(-1)
}
assertFailsWith<ArrayIndexOutOfBoundsException> {
it.addressOf(Int.MAX_VALUE)
}
assertFailsWith<ArrayIndexOutOfBoundsException> {
it.addressOf(Int.MIN_VALUE)
}
}
}
@Test fun pinnedLongArrayAddressOf() {
val arr = LongArray(10) { 0 }
arr.usePinned {
assertEquals(0, it.addressOf(0).pointed.value)
assertEquals(0, it.addressOf(9).pointed.value)
assertFailsWith<ArrayIndexOutOfBoundsException> {
it.addressOf(10)
}
assertFailsWith<ArrayIndexOutOfBoundsException> {
it.addressOf(-1)
}
assertFailsWith<ArrayIndexOutOfBoundsException> {
it.addressOf(Int.MAX_VALUE)
}
assertFailsWith<ArrayIndexOutOfBoundsException> {
it.addressOf(Int.MIN_VALUE)
}
}
}
@Test fun pinnedUByteArrayAddressOf() {
val arr = UByteArray(10) { 0U }
arr.usePinned {
assertEquals(0U, it.addressOf(0).pointed.value)
assertEquals(0U, it.addressOf(9).pointed.value)
assertFailsWith<ArrayIndexOutOfBoundsException> {
it.addressOf(10)
}
assertFailsWith<ArrayIndexOutOfBoundsException> {
it.addressOf(-1)
}
assertFailsWith<ArrayIndexOutOfBoundsException> {
it.addressOf(Int.MAX_VALUE)
}
assertFailsWith<ArrayIndexOutOfBoundsException> {
it.addressOf(Int.MIN_VALUE)
}
}
}
@Test fun pinnedUShortArrayAddressOf() {
val arr = UShortArray(10) { 0U }
arr.usePinned {
assertEquals(0U, it.addressOf(0).pointed.value)
assertEquals(0U, it.addressOf(9).pointed.value)
assertFailsWith<ArrayIndexOutOfBoundsException> {
it.addressOf(10)
}
assertFailsWith<ArrayIndexOutOfBoundsException> {
it.addressOf(-1)
}
assertFailsWith<ArrayIndexOutOfBoundsException> {
it.addressOf(Int.MAX_VALUE)
}
assertFailsWith<ArrayIndexOutOfBoundsException> {
it.addressOf(Int.MIN_VALUE)
}
}
}
@Test fun pinnedUIntArrayAddressOf() {
val arr = UIntArray(10) { 0U }
arr.usePinned {
assertEquals(0U, it.addressOf(0).pointed.value)
assertEquals(0U, it.addressOf(9).pointed.value)
assertFailsWith<ArrayIndexOutOfBoundsException> {
it.addressOf(10)
}
assertFailsWith<ArrayIndexOutOfBoundsException> {
it.addressOf(-1)
}
assertFailsWith<ArrayIndexOutOfBoundsException> {
it.addressOf(Int.MAX_VALUE)
}
assertFailsWith<ArrayIndexOutOfBoundsException> {
it.addressOf(Int.MIN_VALUE)
}
}
}
@Test fun pinnedULongArrayAddressOf() {
val arr = ULongArray(10) { 0U }
arr.usePinned {
assertEquals(0U, it.addressOf(0).pointed.value)
assertEquals(0U, it.addressOf(9).pointed.value)
assertFailsWith<ArrayIndexOutOfBoundsException> {
it.addressOf(10)
}
assertFailsWith<ArrayIndexOutOfBoundsException> {
it.addressOf(-1)
}
assertFailsWith<ArrayIndexOutOfBoundsException> {
it.addressOf(Int.MAX_VALUE)
}
assertFailsWith<ArrayIndexOutOfBoundsException> {
it.addressOf(Int.MIN_VALUE)
}
}
}
@Test fun pinnedFloatArrayAddressOf() {
val arr = FloatArray(10) { 0.0f }
arr.usePinned {
assertEquals(0.0f, it.addressOf(0).pointed.value)
assertEquals(0.0f, it.addressOf(9).pointed.value)
assertFailsWith<ArrayIndexOutOfBoundsException> {
it.addressOf(10)
}
assertFailsWith<ArrayIndexOutOfBoundsException> {
it.addressOf(-1)
}
assertFailsWith<ArrayIndexOutOfBoundsException> {
it.addressOf(Int.MAX_VALUE)
}
assertFailsWith<ArrayIndexOutOfBoundsException> {
it.addressOf(Int.MIN_VALUE)
}
}
}
@Test fun pinnedDoubleArrayAddressOf() {
val arr = DoubleArray(10) { 0.0 }
arr.usePinned {
assertEquals(0.0, it.addressOf(0).pointed.value)
assertEquals(0.0, it.addressOf(9).pointed.value)
assertFailsWith<ArrayIndexOutOfBoundsException> {
it.addressOf(10)
}
assertFailsWith<ArrayIndexOutOfBoundsException> {
it.addressOf(-1)
}
assertFailsWith<ArrayIndexOutOfBoundsException> {
it.addressOf(Int.MAX_VALUE)
}
assertFailsWith<ArrayIndexOutOfBoundsException> {
it.addressOf(Int.MIN_VALUE)
}
}
}
@@ -0,0 +1,12 @@
headers = sys/socket.h sys/errno.h netdb.h stdio.h string.h unistd.h stdlib.h netinet/in.h
compilerOpts.osx = -D_ANSI_SOURCE -D_POSIX_SOURCE
excludeFunctions.osx = addrsel_policy_init
---
static int interop_errno() {
return errno;
}
static short interop_htons(short x) {
return htons(x);
}
@@ -0,0 +1,76 @@
import cstructs.*
import kotlinx.cinterop.*
import kotlin.test.*
fun main() {
produceComplex().useContents {
assertEquals(ui, 128u)
ui = 333u
assertEquals(ui, 333u)
assertEquals(t.i, 1)
t.i += 15
assertEquals(t.i, 16)
assertEquals(next, null)
next = this.ptr
assertEquals(next, this.ptr)
// Check null pointer because it has Nothing? type.
next = null
assertEquals(next, null)
assertEquals(e, E.R)
e = E.G
assertEquals(e, E.G)
assertEquals(K, nonStrict)
nonStrict = S
assertEquals(S, nonStrict)
assertEquals(arr[0], -51)
assertEquals(arr[1], -19)
arr[0] = 51
arr[1] = 19
assertEquals(arr[0], 51)
assertEquals(arr[1], 19)
assertEquals(true, b)
b = false
assertEquals(false, b)
// Check that subtyping via Nothing-returning functions does not break compiler.
assertFailsWith<NotImplementedError> {
ui = TODO()
t.i = TODO()
next = TODO()
e = TODO()
nonStrict = TODO()
b = TODO()
}
}
memScoped {
val packed = alloc<Packed>()
packed.i = -1
assertEquals(-1, packed.i)
packed.e = E.R
assertEquals(E.R, packed.e)
// Check that subtyping via Nothing-returning functions does not break compiler.
assertFailsWith<NotImplementedError> {
packed.i = TODO()
packed.e = TODO()
}
}
// Check that generics doesn't break anything.
checkEnumSubTyping(E.R)
checkIntSubTyping(630090)
}
fun <T : E> checkEnumSubTyping(e: T) = memScoped {
val s = alloc<Complex>()
s.e = e
}
fun <T : Int> checkIntSubTyping(x: T) = memScoped {
val s = alloc<Trivial>()
s.i = x
}
@@ -0,0 +1,2 @@
---
typedef int my_int;
@@ -0,0 +1,31 @@
import kotlinx.cinterop.*
import kotlin.native.*
import kotlin.test.*
import ctypes.*
fun main() {
getStructWithConstFields().useContents {
assertEquals(111, x)
assertEquals(222, y)
}
assertEquals(1u, ForwardDeclaredEnum.ONE.value)
assertEquals(6, vlaSum(3, cValuesOf(1, 2, 3)))
assertEquals(10, vlaSum2D(2, cValuesOf(1, 2, 3, 4)))
assertEquals(21, vlaSum2DBothDimensions(2, 3, cValuesOf(1, 2, 3, 4, 5, 6)))
// Not supported by clang:
// assertEquals(10, vlaSum2DForward(cValuesOf(1, 2, 3, 4), 2))
assertEquals(0u, StrictEnum1.StrictEnum1A.value)
assertEquals(1u, StrictEnum2.StrictEnum2B.value)
assertEquals(0u, NonStrictEnum1A)
assertEquals(1u, NonStrictEnum2B)
assertEquals(1, EnumCharBase.EnumCharBaseB.value)
assertEquals(3, sendEnum(EnumCharBase.EnumCharBaseB))
assertEquals('a'.toByte(), EnumExplicitCharA)
assertEquals('b'.toByte(), EnumExplicitCharB)
assertEquals(EnumExplicitCharA, EnumExplicitCharDup)
}
@@ -0,0 +1,10 @@
---
int global;
#define global bad macro
void foo(void);
#define foo <
// Setter for x should not be generated.
typedef const int cint;
cint x;
@@ -0,0 +1,36 @@
import cunion.*
import kotlinx.cinterop.*
import kotlin.native.*
import kotlin.test.*
fun main() {
memScoped {
val basicUnion = alloc<BasicUnion>()
for (value in Short.MIN_VALUE..Short.MAX_VALUE) {
basicUnion.ll = value.toLong()
val expected = if (Platform.isLittleEndian) {
value
} else {
value.toLong() ushr (Long.SIZE_BITS - Short.SIZE_BITS)
}
assertEquals(expected.toShort(), basicUnion.s)
}
}
memScoped {
val struct = alloc<StructWithUnion>()
struct.`as`.i = Float.NaN.toRawBits()
assertEquals(Float.NaN, struct.`as`.f)
}
memScoped {
val union = alloc<Packed>()
union.b = 1u
var expected = if (Platform.isLittleEndian) {
1u
} else {
1u shl (Int.SIZE_BITS - Byte.SIZE_BITS)
}
assertEquals(expected, union.i)
union.i = 0u
assertEquals(0u, union.b)
}
}
@@ -0,0 +1,12 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the LICENSE file.
*/
import kotlin.test.*
import cunsupported.*
fun main(args: Array<String>) {
noAttr()
noTargetAttr()
}
@@ -0,0 +1,10 @@
import kotlinx.cinterop.*
import kotlin.test.*
import cvalues.*
fun main() {
assertTrue(isNullString(null))
assertTrue(isNullWString(null))
assertFalse(isNullString("a"))
assertFalse(isNullWString("b"))
}
@@ -0,0 +1,35 @@
import kotlinx.cinterop.*
import kotlin.native.*
import kotlin.test.*
import cvectors.*
fun isWin32() = Platform.osFamily == OsFamily.WINDOWS && Platform.cpuArchitecture == CpuArchitecture.X86
fun main(args: Array<String>) {
val mimalloc = args.takeIf { it.size == 1 }?.get(0) == "mimalloc"
// See KT-37272. Fixed in mimalloc
if (mimalloc || !isWin32()) {
produceComplex().useContents {
assertEquals(vec4f, vectorOf(1.0f, 1.0f, 1.0f, 1.0f))
vec4f = vectorOf(0.0f, 0.0f, 0.0f, 0.0f)
assertEquals(vec4f, vectorOf(0.0f, 0.0f, 0.0f, 0.0f))
}
}
// FIXME: KT-36285
if (Platform.osFamily != OsFamily.LINUX || Platform.cpuArchitecture != CpuArchitecture.ARM32) {
assertEquals(49, sendV4I(vectorOf(1, 2, 3, 4)))
}
assertEquals(49, (sendV4F(vectorOf(1f, 2f, 3f, 4f)) + 0.00001).toInt())
if (mimalloc || !isWin32()) {
memScoped {
val vector = alloc<KVector4i32Var>().also {
it.value = vectorOf(1, 2, 3, 4)
}
assertEquals(vector.value, vectorOf(1, 2, 3, 4))
}
}
}
@@ -0,0 +1,12 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the LICENSE file.
*/
import kotlin.test.*
import kotlinx.cinterop.*
import withSpaces.*
fun main(args: Array<String>) {
customCompare("first", "second")
}