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:
@@ -0,0 +1,7 @@
|
||||
import auxiliaryCppSources.*
|
||||
import kotlin.test.*
|
||||
import kotlinx.cinterop.*
|
||||
|
||||
fun main() {
|
||||
assertEquals(name()!!.toKString(), "Hello from C++")
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
#include <string>
|
||||
#include "name.h"
|
||||
|
||||
static std::string _name = "Hello from C++";
|
||||
|
||||
const char* name() {
|
||||
return _name.c_str();
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
const char* name();
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -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 ¬SoLongSignatureFunction;
|
||||
}
|
||||
@@ -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")
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
@file:OptIn(ExperimentalStdlibApi::class)
|
||||
|
||||
import kotlin.native.internal.*
|
||||
|
||||
fun createCleaner() {
|
||||
createCleaner(42) {
|
||||
println(it)
|
||||
}
|
||||
}
|
||||
|
||||
fun performGC() {
|
||||
GC.collect()
|
||||
performGCOnCleanerWorker()
|
||||
}
|
||||
|
||||
private var globalCleaner: Cleaner? = null
|
||||
|
||||
fun initializeGlobalCleaner() {
|
||||
globalCleaner = createCleaner(11) {
|
||||
println(it)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include "testlib_api.h"
|
||||
|
||||
int main() {
|
||||
testlib_symbols()->kotlin.root.initializeGlobalCleaner();
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include "testlib_api.h"
|
||||
|
||||
int main() {
|
||||
testlib_symbols()->kotlin.root.createCleaner();
|
||||
testlib_symbols()->kotlin.root.performGC();
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include "testlib_api.h"
|
||||
|
||||
#include <thread>
|
||||
|
||||
int main() {
|
||||
std::thread t([]() { testlib_symbols()->kotlin.root.createCleaner(); });
|
||||
t.join();
|
||||
testlib_symbols()->kotlin.root.performGC();
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
#include <thread>
|
||||
#include <future>
|
||||
#include <chrono>
|
||||
#include <vector>
|
||||
#include <csignal> // signal.h
|
||||
|
||||
#include "async.h"
|
||||
|
||||
int test_ConcurrentTerminate() {
|
||||
signal(SIGABRT, *[](int){ exit(99); }); // Windows does not have sigaction
|
||||
|
||||
std::vector<std::future<void>> futures;
|
||||
#ifdef __linux__
|
||||
// TODO: invalid terminate handler called from bridge on non-main thread on Linux X64
|
||||
throw std::runtime_error("Reporting error!");
|
||||
#endif
|
||||
|
||||
for (size_t i = 0; i < 100; ++i) {
|
||||
futures.emplace_back(std::async(std::launch::async,
|
||||
[](size_t param) {
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(param));
|
||||
throw std::runtime_error("Reporting error!");
|
||||
},
|
||||
200 - i));
|
||||
}
|
||||
|
||||
for (auto &future : futures) future.get();
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
int test_ConcurrentTerminate();
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,5 @@
|
||||
package async
|
||||
|
||||
---
|
||||
|
||||
int test_ConcurrentTerminate();
|
||||
@@ -0,0 +1,44 @@
|
||||
#include "testlib_api.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
#include <future>
|
||||
#include <chrono>
|
||||
#include <vector>
|
||||
#include <csignal> // signal.h
|
||||
|
||||
using namespace std;
|
||||
|
||||
static
|
||||
int runConcurrent() {
|
||||
|
||||
std::vector<std::future<void>> futures;
|
||||
|
||||
for (size_t i = 0; i < 100; ++i) {
|
||||
futures.emplace_back(std::async(std::launch::async,
|
||||
[](auto delay) {
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(delay));
|
||||
testlib_symbols()->kotlin.root.testTerminate();
|
||||
},
|
||||
100));
|
||||
}
|
||||
|
||||
for (auto &future : futures) future.get();
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main() {
|
||||
signal(SIGABRT, *[](int){ exit(99); }); // Windows does not have sigaction
|
||||
|
||||
set_terminate([](){
|
||||
cout << "This is the original terminate handler\n" << flush;
|
||||
std::abort();
|
||||
});
|
||||
|
||||
try {
|
||||
runConcurrent();
|
||||
} catch(...) {
|
||||
std::cerr << "Unknown exception caught\n" << std::flush;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
import async.*
|
||||
import kotlinx.cinterop.*
|
||||
|
||||
fun main() {
|
||||
test_ConcurrentTerminate()
|
||||
println("This is not expected.")
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
import kotlin.system.exitProcess
|
||||
|
||||
fun testTerminate() {
|
||||
throw RuntimeException("Example")
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
int get1() {
|
||||
return 1;
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
int get2() {
|
||||
return 2;
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
int get3() {
|
||||
return 3;
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
int get4() {
|
||||
return 4;
|
||||
}
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
libraryPaths = backend.native/tests/build/embedStaticLibraries
|
||||
staticLibraries = 3.a 4.a
|
||||
@@ -0,0 +1,4 @@
|
||||
int get1(void);
|
||||
int get2(void);
|
||||
int get3(void);
|
||||
int get4(void);
|
||||
@@ -0,0 +1,9 @@
|
||||
import kotlin.test.*
|
||||
import embedStaticLibraries.*
|
||||
|
||||
fun main() {
|
||||
assertEquals(1, get1())
|
||||
assertEquals(2, get2())
|
||||
assertEquals(3, get3())
|
||||
assertEquals(4, get4())
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
#include "library.h"
|
||||
|
||||
extern "C" {
|
||||
|
||||
struct S {
|
||||
const char* name;
|
||||
};
|
||||
|
||||
struct S s = {
|
||||
.name = "initial"
|
||||
};
|
||||
|
||||
void setContent(struct S* s, const char* name) {
|
||||
s->name = name;
|
||||
}
|
||||
|
||||
const char* getContent(struct S* s) {
|
||||
return s->name;
|
||||
}
|
||||
|
||||
union U {
|
||||
float f;
|
||||
double d;
|
||||
};
|
||||
|
||||
void setDouble(union U* u, double value) {
|
||||
u->d = value;
|
||||
}
|
||||
|
||||
double getDouble(union U* u) {
|
||||
return u->d;
|
||||
}
|
||||
|
||||
union U u = {
|
||||
.d = 0.0
|
||||
};
|
||||
|
||||
char array[5] = { 0, 0, 0, 0, 0 };
|
||||
|
||||
void setArrayValue(char* array, char value) {
|
||||
for (int i = 0; i < 5; ++i) {
|
||||
array[i] = value;
|
||||
}
|
||||
}
|
||||
|
||||
int arrayLength() {
|
||||
return sizeof(array) / sizeof(char);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
// Forward declaration.
|
||||
struct S;
|
||||
extern struct S s;
|
||||
|
||||
const char* getContent(struct S* s);
|
||||
void setContent(struct S* s, const char* name);
|
||||
|
||||
union U;
|
||||
extern union U u;
|
||||
|
||||
double getDouble(union U* u);
|
||||
void setDouble(union U* u, double value);
|
||||
|
||||
// Global array of unknown size.
|
||||
extern char array[];
|
||||
|
||||
|
||||
int arrayLength();
|
||||
void setArrayValue(char* array, char value);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,25 @@
|
||||
import library.*
|
||||
import kotlinx.cinterop.*
|
||||
import kotlin.test.*
|
||||
|
||||
fun main() {
|
||||
assertNotNull(s.ptr)
|
||||
assertNotNull(u.ptr)
|
||||
assertNotNull(array)
|
||||
|
||||
assertEquals("initial", getContent(s.ptr)?.toKString())
|
||||
setContent(s.ptr, "yo")
|
||||
assertEquals("yo", getContent(s.ptr)?.toKString())
|
||||
|
||||
assertEquals(0.0, getDouble(u.ptr))
|
||||
setDouble(u.ptr, Double.MIN_VALUE)
|
||||
assertEquals(Double.MIN_VALUE, getDouble(u.ptr))
|
||||
|
||||
for (i in 0 until arrayLength()) {
|
||||
assertEquals(0x0, array[i])
|
||||
}
|
||||
setArrayValue(array, 0x1)
|
||||
for (i in 0 until arrayLength()) {
|
||||
assertEquals(0x1, array[i])
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package knlibrary
|
||||
|
||||
// The following 2 singletons are unused. However, since we are generating C bindings for them,
|
||||
// they should be marked as used, so that the code generator emits their deinitialization.
|
||||
|
||||
object A {}
|
||||
|
||||
class B {
|
||||
companion object {}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
#include "testlib_api.h"
|
||||
|
||||
#include <thread>
|
||||
|
||||
int main() {
|
||||
auto t = std::thread([] {
|
||||
auto lib = testlib_symbols();
|
||||
|
||||
// Initialize A and B.Companion and get their stable pointers.
|
||||
auto a = lib->kotlin.root.knlibrary.A._instance();
|
||||
auto bCompanion = lib->kotlin.root.knlibrary.B.Companion._instance();
|
||||
|
||||
// Now, dispose of the stable pointers.
|
||||
lib->DisposeStablePointer(bCompanion.pinned);
|
||||
lib->DisposeStablePointer(a.pinned);
|
||||
|
||||
// A and B.Companion now are owned by the global references only.
|
||||
});
|
||||
|
||||
// This causes Kotlin runtime full deinitialization, because `t` is the only thread
|
||||
// with the Kotlin runtime. So, all the globals will get deinitialized and memory
|
||||
// leak checker will get executed (because .kt code is compiled with -g).
|
||||
t.join();
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* 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.iconv.*
|
||||
import platform.posix.size_tVar
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
|
||||
val sourceByteArray = "Hello!".encodeToByteArray()
|
||||
|
||||
val golden = listOf(0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x21)
|
||||
|
||||
memScoped {
|
||||
|
||||
val sourceLength = alloc<size_tVar>()
|
||||
val destLength = alloc<size_tVar>()
|
||||
|
||||
val sourceBytes = allocArrayOf(sourceByteArray)
|
||||
val destBytes = allocArray<ByteVar>(golden.size)
|
||||
|
||||
val sourcePtr = alloc<CArrayPointerVar<ByteVar>>()
|
||||
sourcePtr.value = sourceBytes
|
||||
|
||||
val destPtr = alloc<CArrayPointerVar<ByteVar>>()
|
||||
destPtr.value = destBytes
|
||||
|
||||
sourceLength.value = sourceByteArray.size.convert()
|
||||
destLength.value = golden.size.convert()
|
||||
|
||||
val conversion = iconv_open("UTF-8", "LATIN1")
|
||||
|
||||
iconv(conversion, sourcePtr.ptr, sourceLength.ptr, destPtr.ptr, destLength.ptr)
|
||||
|
||||
golden.forEachIndexed { index, it ->
|
||||
println("$it ${destBytes[index]}")
|
||||
it == destBytes[index].toInt()
|
||||
}
|
||||
|
||||
iconv_close(conversion)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!-- AUTO-GENERATED FILE. DO NOT MODIFY. -->
|
||||
<!-- This file was automatically generated by the LocoLaser tool. -->
|
||||
<!-- It should not be modified by hand. -->
|
||||
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
<key>screen_main_plural_string</key>
|
||||
<dict>
|
||||
<key>NSStringLocalizedFormatKey</key>
|
||||
<string>%#@value@</string>
|
||||
<key>value</key>
|
||||
<dict>
|
||||
<key>NSStringFormatSpecTypeKey</key>
|
||||
<string>NSStringPluralRuleType</string>
|
||||
<key>NSStringFormatValueTypeKey</key>
|
||||
<string>d</string>
|
||||
<key>other</key>
|
||||
<string>Plural: %d apples</string>
|
||||
<key>one</key>
|
||||
<string>Plural: one apple</string>
|
||||
<key>zero</key>
|
||||
<string>Plural: no apples</string>
|
||||
</dict>
|
||||
</dict>
|
||||
</dict>
|
||||
</plist>
|
||||
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
* Copyright 2010-2019 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.Foundation.*
|
||||
import platform.objc.*
|
||||
import kotlin.test.*
|
||||
|
||||
import kotlinx.cinterop.*
|
||||
import platform.posix.*
|
||||
import kotlin.system.exitProcess
|
||||
|
||||
fun exc_handler(x: Any?) : Unit {
|
||||
println("Uncaught exception handler")
|
||||
println(x.toString())
|
||||
exitProcess(0)
|
||||
}
|
||||
|
||||
fun main() {
|
||||
// This does not work anymore, as NSException propagated as ForeignException
|
||||
// so we got `Uncaught Kotlin exception` instead. Use normal try/catch.
|
||||
objc_setUncaughtExceptionHandler(staticCFunction(::exc_handler))
|
||||
|
||||
try {
|
||||
println(NSJSONSerialization())
|
||||
} catch (e: Exception) { // ForeignException expected
|
||||
println(e)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
language = Objective-C
|
||||
headerFilter = **/objc_wrap.h
|
||||
linkerOpts = -lobjcexception
|
||||
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* Test different behavior depending on foreignExceptionMode option
|
||||
*/
|
||||
|
||||
import kotlin.test.*
|
||||
import objcExceptionMode.*
|
||||
import kotlinx.cinterop.*
|
||||
import platform.objc.*
|
||||
import kotlin.system.exitProcess
|
||||
|
||||
@Suppress("VARIABLE_WITH_REDUNDANT_INITIALIZER")
|
||||
@Test fun testKT35056() {
|
||||
val name = "Some native exception"
|
||||
val reason = "Illegal value"
|
||||
var finallyBlockTest = "FAILED"
|
||||
var catchBlockTest = "FAILED"
|
||||
try {
|
||||
raiseExc(name, reason)
|
||||
assertNotEquals("FAILED", catchBlockTest) // shall not get here anyway
|
||||
} catch (e: ForeignException) {
|
||||
val ret = logExc(e.nativeException) // return NSException name
|
||||
assertEquals(name, ret)
|
||||
assertEquals("$name:: $reason", e.message)
|
||||
println("OK: ForeignException")
|
||||
catchBlockTest = "PASSED"
|
||||
} finally {
|
||||
finallyBlockTest = "PASSED"
|
||||
}
|
||||
assertEquals("PASSED", catchBlockTest)
|
||||
assertEquals("PASSED", finallyBlockTest)
|
||||
}
|
||||
|
||||
@Suppress("UNUSED_PARAMETER")
|
||||
fun abnormal_handler(x: Any?) : Unit {
|
||||
println("OK: Ends with uncaught exception handler")
|
||||
exitProcess(0)
|
||||
}
|
||||
|
||||
fun main() {
|
||||
// Depending on the `foreignxceptionMode` option (def file or cinterop cli) this test should ends
|
||||
// normally with `ForeignException` handled or abnormally with `abnormal_handler`.
|
||||
// Test shall validate output (golden value) from `abnormal_handler`.
|
||||
|
||||
objc_setUncaughtExceptionHandler(staticCFunction(::abnormal_handler))
|
||||
|
||||
testKT35056()
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
language = Objective-C
|
||||
headerFilter = **/objc_wrap.h
|
||||
linkerOpts = -lobjcexception
|
||||
foreignExceptionMode = objc-wrap
|
||||
@@ -0,0 +1,9 @@
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
void raiseExc(id name, id reason);
|
||||
id logExc(id exception);
|
||||
|
||||
@interface Foo : NSObject
|
||||
- (void)instanceMethodThrow:(id)name reason:(id)reason;
|
||||
+ (void)classMethodThrow:(id)name reason:(id)reason;
|
||||
@end
|
||||
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
* Test different types of callable with foreignExceptionMode=objc-wrap
|
||||
*/
|
||||
|
||||
import kotlin.test.*
|
||||
//import objcTests.*
|
||||
import objc_wrap.*
|
||||
import kotlinx.cinterop.*
|
||||
|
||||
fun testInner(name: String, reason: String) {
|
||||
var finallyBlockTest = "FAILED"
|
||||
var catchBlockTest = "NOT EXPECTED"
|
||||
try {
|
||||
raiseExc(name, reason)
|
||||
} catch (e: RuntimeException) {
|
||||
catchBlockTest = "This shouldn't happen"
|
||||
} finally {
|
||||
finallyBlockTest = "PASSED"
|
||||
}
|
||||
assertEquals("NOT EXPECTED", catchBlockTest)
|
||||
assertEquals("PASSED", finallyBlockTest)
|
||||
}
|
||||
|
||||
typealias CallMe = (String, String) -> Unit
|
||||
|
||||
@Test fun testExceptionWrap(raise: CallMe) {
|
||||
val name = "Some native exception"
|
||||
val reason = "Illegal value"
|
||||
var finallyBlockTest = "FAILED"
|
||||
var catchBlockTest = "FAILED"
|
||||
try {
|
||||
raise(name, reason)
|
||||
} catch (e: ForeignException) {
|
||||
val ret = logExc(e.nativeException) // return NSException name
|
||||
assertEquals(name, ret)
|
||||
assertEquals("$name:: $reason", e.message)
|
||||
catchBlockTest = "PASSED"
|
||||
} finally {
|
||||
finallyBlockTest = "PASSED"
|
||||
}
|
||||
assertEquals("PASSED", catchBlockTest)
|
||||
assertEquals("PASSED", finallyBlockTest)
|
||||
}
|
||||
|
||||
class Bar() : Foo()
|
||||
|
||||
fun main() {
|
||||
testExceptionWrap(::raiseExc) // simple
|
||||
testExceptionWrap(::testInner) // nested try block
|
||||
testExceptionWrap(Foo::classMethodThrow) // class method
|
||||
testExceptionWrap(Foo()::instanceMethodThrow) // instance method
|
||||
testExceptionWrap(Bar()::instanceMethodThrow) // fake override
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
#include "objc_wrap.h"
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
void raiseExc(id name, id reason) {
|
||||
[NSException raise:name format:@"%@", reason];
|
||||
}
|
||||
|
||||
id logExc(id exc) {
|
||||
assert([exc isKindOfClass:[NSException class]]);
|
||||
return ((NSException*)exc).name;
|
||||
}
|
||||
|
||||
@implementation Foo : NSObject
|
||||
- (void) instanceMethodThrow:(id)name reason:(id)reason {
|
||||
raiseExc(name, reason);
|
||||
}
|
||||
+ (void) classMethodThrow:(id)name reason:(id)reason {
|
||||
raiseExc(name, reason);
|
||||
}
|
||||
@end
|
||||
@@ -0,0 +1,30 @@
|
||||
import kotlin.native.concurrent.*
|
||||
import kotlin.test.*
|
||||
import platform.Foundation.*
|
||||
import platform.darwin.NSObject
|
||||
|
||||
fun Worker.runInWorker(block: () -> Unit) {
|
||||
this.execute(TransferMode.SAFE, { block.freeze() }) {
|
||||
it()
|
||||
}.result
|
||||
}
|
||||
|
||||
private class NSObjectImpl : NSObject() {
|
||||
var x = 111
|
||||
}
|
||||
|
||||
// Also see counterpart in interop/objc/tests/sharing.kt
|
||||
fun main() = withWorker {
|
||||
val obj = NSObjectImpl()
|
||||
val array: NSArray = NSMutableArray().apply {
|
||||
addObject(obj)
|
||||
}
|
||||
|
||||
assertFalse(obj.isFrozen)
|
||||
|
||||
println("Before")
|
||||
runInWorker {
|
||||
array.objectAtIndex(0)
|
||||
}
|
||||
println("After")
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
import kotlin.native.concurrent.*
|
||||
import kotlinx.cinterop.autoreleasepool
|
||||
import objclib.*
|
||||
|
||||
fun main() {
|
||||
autoreleasepool {
|
||||
run()
|
||||
}
|
||||
}
|
||||
|
||||
private class NSObjectImpl : NSObject() {
|
||||
var x = 111
|
||||
}
|
||||
|
||||
fun run() = withWorker {
|
||||
val obj = NSObjectImpl()
|
||||
setObject(obj)
|
||||
|
||||
println("Before")
|
||||
val isAlive = try {
|
||||
execute(TransferMode.SAFE, {}) {
|
||||
isObjectAliveShouldCrash()
|
||||
}.result
|
||||
} catch (e: Throwable) {
|
||||
false
|
||||
}
|
||||
println("After $isAlive")
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
language = Objective-C
|
||||
headerFilter = **/objclib.h
|
||||
@@ -0,0 +1,13 @@
|
||||
#import <objc/NSObject.h>
|
||||
|
||||
static NSObject* __weak globalObject = nil;
|
||||
|
||||
void setObject(NSObject* obj) {
|
||||
globalObject = obj;
|
||||
}
|
||||
|
||||
// Make sure this function persists, because the test expects to find this function in the stack trace.
|
||||
__attribute__((noinline))
|
||||
bool isObjectAliveShouldCrash() {
|
||||
return globalObject != nil;
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
#define ANSWER 42
|
||||
@@ -0,0 +1,6 @@
|
||||
import module_library.*
|
||||
|
||||
fun main() {
|
||||
println("OK")
|
||||
println(ANSWER)
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
language = Objective-C
|
||||
modules = module_library
|
||||
@@ -0,0 +1,6 @@
|
||||
module module_library {
|
||||
umbrella header "module_library_umbrella.h"
|
||||
|
||||
export *
|
||||
module * { export * }
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
#import <foo.h>
|
||||
@@ -0,0 +1,58 @@
|
||||
import messaging.*
|
||||
import kotlinx.cinterop.*
|
||||
import kotlin.test.*
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
autoreleasepool {
|
||||
primitives()
|
||||
aggregates()
|
||||
}
|
||||
}
|
||||
|
||||
private fun primitives() {
|
||||
assertEquals(3.14f, PrimitiveTestSubject.floatFn())
|
||||
assertEquals(3.14, PrimitiveTestSubject.doubleFn())
|
||||
assertEquals(42, PrimitiveTestSubject.intFn())
|
||||
assertEquals(vectorOf(2f, 4f, 5f, 8f), PrimitiveTestSubject.simdFn())
|
||||
}
|
||||
|
||||
private fun aggregates() {
|
||||
AggregateTestSubject.singleFloatFn().useContents {
|
||||
assertEquals(3.14f, f)
|
||||
}
|
||||
AggregateTestSubject.simplePackedFn().useContents {
|
||||
assertEquals('0'.toByte(), f1)
|
||||
assertEquals(111, f2)
|
||||
}
|
||||
AggregateTestSubject.evenSmallerPackedFn().useContents {
|
||||
assertEquals('x'.toByte(), x)
|
||||
assertEquals(169, y)
|
||||
assertEquals('z'.toByte(), z)
|
||||
}
|
||||
AggregateTestSubject.homogeneousBigFn().useContents {
|
||||
assertEquals(1.0f, f1)
|
||||
assertEquals(2.0f, f2)
|
||||
assertEquals(3.0f, f3)
|
||||
assertEquals(4.0f, f4)
|
||||
assertEquals(5.0f, f5)
|
||||
assertEquals(6.0f, f6)
|
||||
assertEquals(7.0f, f7)
|
||||
assertEquals(8.0f, f8)
|
||||
}
|
||||
AggregateTestSubject.homogeneousSmallFn().useContents {
|
||||
assertEquals(1.0f, f1)
|
||||
assertEquals(2.0f, f2)
|
||||
assertEquals(3.0f, f3)
|
||||
assertEquals(4.0f, f4)
|
||||
}
|
||||
AggregateTestSubject.simd_quatfFn().useContents {
|
||||
assertEquals(vectorOf(1f, 4f, 9f, 25f), vector)
|
||||
}
|
||||
AggregateTestSubject.geterogeneousSmallFn().useContents {
|
||||
assertEquals(1, s1)
|
||||
assertEquals(vectorOf(1f, 4f, 9f, 25f), v2)
|
||||
assertEquals(3f, f3)
|
||||
assertEquals(4, i4)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
language = Objective-C
|
||||
headerFilter = **/messaging.h
|
||||
linkerOpts = -lobjcmessaging
|
||||
@@ -0,0 +1,68 @@
|
||||
#import <objc/NSObject.h>
|
||||
#include <simd/simd.h>
|
||||
|
||||
@interface PrimitiveTestSubject : NSObject
|
||||
|
||||
+ (int)intFn;
|
||||
+ (float)floatFn;
|
||||
+ (double)doubleFn;
|
||||
+ (simd_float4)simdFn;
|
||||
|
||||
@end;
|
||||
|
||||
typedef struct {
|
||||
float f;
|
||||
} SingleFloat;
|
||||
|
||||
typedef struct __attribute__((packed)) {
|
||||
char f1;
|
||||
short f2;
|
||||
char f3;
|
||||
char f4;
|
||||
} SimplePacked;
|
||||
|
||||
typedef struct __attribute__((packed)) {
|
||||
char x;
|
||||
short y;
|
||||
char z;
|
||||
} EvenSmallerPacked;
|
||||
|
||||
typedef struct {
|
||||
float f1;
|
||||
float f2;
|
||||
float f3;
|
||||
float f4;
|
||||
} HomogeneousSmall;
|
||||
|
||||
typedef struct {
|
||||
float f1;
|
||||
float f2;
|
||||
float f3;
|
||||
float f4;
|
||||
float f5;
|
||||
float f6;
|
||||
float f7;
|
||||
float f8;
|
||||
} HomogeneousBig;
|
||||
|
||||
// TODO: Add more cases later: SIMD, bitfields.
|
||||
|
||||
typedef struct {
|
||||
short s1;
|
||||
simd_float4 v2;
|
||||
float f3;
|
||||
int i4;
|
||||
} GeterogeneousSmall;
|
||||
|
||||
@interface AggregateTestSubject : NSObject
|
||||
|
||||
+ (SingleFloat)singleFloatFn;
|
||||
+ (SimplePacked)simplePackedFn;
|
||||
+ (EvenSmallerPacked)evenSmallerPackedFn;
|
||||
+ (HomogeneousSmall)homogeneousSmallFn;
|
||||
+ (HomogeneousBig)homogeneousBigFn;
|
||||
+ (simd_quatf)simd_quatfFn;
|
||||
+ (GeterogeneousSmall)geterogeneousSmallFn;
|
||||
|
||||
|
||||
@end;
|
||||
@@ -0,0 +1,81 @@
|
||||
#import "messaging.h"
|
||||
|
||||
@implementation PrimitiveTestSubject
|
||||
|
||||
+ (int)intFn {
|
||||
return 42;
|
||||
}
|
||||
|
||||
+ (float)floatFn {
|
||||
return 3.14f;
|
||||
}
|
||||
|
||||
+ (double)doubleFn {
|
||||
return 3.14;
|
||||
}
|
||||
|
||||
+ (simd_float4)simdFn {
|
||||
simd_float4 v;
|
||||
v.x = 2;
|
||||
v.y = 4;
|
||||
v.z = 5;
|
||||
v.w = 8;
|
||||
return v;
|
||||
}
|
||||
|
||||
@end;
|
||||
|
||||
@implementation AggregateTestSubject
|
||||
|
||||
+ (SingleFloat)singleFloatFn {
|
||||
SingleFloat s;
|
||||
s.f = 3.14f;
|
||||
return s;
|
||||
}
|
||||
|
||||
+ (SimplePacked)simplePackedFn {
|
||||
SimplePacked s;
|
||||
s.f1 = '0';
|
||||
s.f2 = 111;
|
||||
return s;
|
||||
}
|
||||
|
||||
+ (EvenSmallerPacked)evenSmallerPackedFn {
|
||||
EvenSmallerPacked s;
|
||||
s.x = 'x';
|
||||
s.y = 169;
|
||||
s.z = 'z';
|
||||
return s;
|
||||
}
|
||||
|
||||
+ (HomogeneousSmall)homogeneousSmallFn {
|
||||
HomogeneousSmall s;
|
||||
s.f1 = 1.0f;
|
||||
s.f2 = 2.0f;
|
||||
s.f3 = 3.0f;
|
||||
s.f4 = 4.0f;
|
||||
return s;
|
||||
}
|
||||
|
||||
+ (HomogeneousBig)homogeneousBigFn {
|
||||
HomogeneousBig s;
|
||||
s.f1 = 1.0f;
|
||||
s.f2 = 2.0f;
|
||||
s.f3 = 3.0f;
|
||||
s.f4 = 4.0f;
|
||||
s.f5 = 5.0f;
|
||||
s.f6 = 6.0f;
|
||||
s.f7 = 7.0f;
|
||||
s.f8 = 8.0f;
|
||||
return s;
|
||||
}
|
||||
|
||||
+ (GeterogeneousSmall)geterogeneousSmallFn {
|
||||
return (GeterogeneousSmall){1, {1, 4, 9, 25}, 3, 4};
|
||||
}
|
||||
|
||||
+ (simd_quatf)simd_quatfFn {
|
||||
return (simd_quatf){ {1, 4, 9, 25} };
|
||||
}
|
||||
|
||||
@end;
|
||||
@@ -0,0 +1,5 @@
|
||||
language = Objective-C
|
||||
headers = Foundation/NSArray.h Foundation/NSValue.h Foundation/NSString.h Foundation/NSStream.h Foundation/NSBundle.h
|
||||
headerFilter = **/smoke.h Foundation/NSArray.h Foundation/NSValue.h Foundation/NSString.h Foundation/NSStream.h \
|
||||
objc/objc.h Foundation/NSBundle.h
|
||||
linkerOpts = -lobjcsmoke
|
||||
@@ -0,0 +1,6 @@
|
||||
language = Objective-C
|
||||
headers = Foundation/NSArray.h Foundation/NSValue.h Foundation/NSString.h Foundation/NSStream.h Foundation/NSBundle.h
|
||||
headerFilter = **/interop/objc/tests/**.h \
|
||||
Foundation/NSArray.h Foundation/NSValue.h Foundation/NSString.h Foundation/NSStream.h \
|
||||
objc/objc.h Foundation/NSBundle.h
|
||||
linkerOpts = -lobjctests
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user