[Kotlin/Native][Interop] Provide pure c wrappers over cpp for skia interop

This commit is contained in:
Vladimir Ivanov
2019-08-19 11:44:20 +03:00
committed by Alexander Gorshenev
parent 61825e9aec
commit 5f582ad28a
31 changed files with 1525 additions and 66 deletions
@@ -0,0 +1,42 @@
language = C++
compilerOpts = -std=c++14
---
class CppTest {
public:
CppTest() { ++counter; }
CppTest(const CppTest&) = default;
explicit CppTest(int i, double j = 3.14) : iPub(i + int(j + 0.5)) {}
~CppTest() { --counter; }
int iPub = 42;
virtual int foo() { return iPub; }
static int counter;
static int s_fun() { return counter; }
class Nested {
public:
int nestedFoo() { return -2; }
} nested;
// not supported yet
operator CppTest::Nested() const;
template <class X> void fooTmplMember() const;
private:
CppTest* funPrivate() const;
static int s_funPrivate();
int iPriv;
};
int CppTest::counter;
CppTest retByValue(CppTest* s) {
return s ? *s : CppTest();
}
@@ -0,0 +1,133 @@
import kotlinx.cinterop.*
import kotlin.test.*
import cppClass.*
class FeatureTest {
@Test fun ctorDefault() {
memScoped {
val x = alloc<CppTest>()
CppTest.__init__(x.ptr)
assertEquals(42, x.iPub)
assertEquals(42, x.foo())
// dtor is not called, leak is intentional for the purpose of UT
}
}
@Test fun ctorWithParam() {
memScoped {
val x = alloc<CppTest>()
CppTest.__init__(x.ptr, 10, 3.8)
assertEquals(14, x.iPub)
}
}
@Test fun copyCtor(y: CppTest) {
val x = nativeHeap.alloc<CppTest>() {}
CppTest.__init__(x.ptr, y.ptr)
assertEquals(y.iPub, x.iPub)
nativeHeap.free(x)
}
/*
@Test fun reinitWithCtorAndDtor(y: CppTest) {
val count = CppTest.getCount()
val x = nativeHeap.alloc< CppTest>() {}
CppTest.__init__(x.ptr, y.ptr)
assertEquals( CppTest.getCount(), count + 1)
assertEquals(y.iPub, x.iPub)
CppTest.__destroy__(x.ptr)
y.iPub = -11
assertEquals(y.iPub, -11)
CppTest.__init__(x.ptr, y.ptr)
assertEquals(x.iPub, -11)
CppTest.__destroy__(x.ptr)
assertEquals( CppTest.getCount(), count)
nativeHeap.free(x)
}
@Test fun publicField(x : CppTest) {
x.iPub = 21
assertEquals(22, x.foo(x.ptr))
}
@Test fun lvRefParameter() {
memScoped {
val x = alloc<ns__NoName>()
var i = alloc<IntVar>()
i.value = 758
assertEquals(x.noNameMember(i.ptr), 759)
assertEquals(i.value, 759)
}
}
@Test fun staticField() {
val save = CppTest.getCount()
assertEquals( CppTest.getCount(), CppTest.counter)
CppTest.counter = 654
assertEquals( CppTest.getCount(), 654)
assertEquals( CppTest.getCount(), CppTest.counter)
CppTest.counter = save
assertEquals( CppTest.getCount(), save)
}
*/
}
fun main() {
val testRun = FeatureTest()
testRun.ctorDefault()
testRun.ctorWithParam()
val a0 = retByValue(null)
println("a0.useContents {iPub} = ${a0.useContents {iPub}}" )
println("a0.useContents { foo() } = ${a0.useContents { foo() }}" )
// retByValue(null)!!.getValue().foo()
// val a1 = interpretPointed<CppTest>(retByValue(null).rawValue)
// println(a1.foo())
/*
val a1 = interpretPointed< CppTest>(ns__create().rawValue)
testRun.publicField(a1)
testRun.staticField()
testRun.lvRefParameter()
a1.iPub = 112
testRun.copyCtor(a1)
testRun.reinitWithCtorAndDtor(a1)
*/
}
/*
fun testStatic() {
println(" CppTest.s_fun() returns ${ CppTest.s_fun()}")
println(" CppTest.s_fun() returns ${ CppTest.s_fun()}")
println(" CppTest.s_fun() returns ${ CppTest.s_fun()}")
}
fun testCtor() {
println("testCtor")
val cxx = nativeHeap.alloc< CppTest>() {
memcpy(ptr, ns__create(), typeOf< CppTest>().size.convert()) // use placement new here
}
cxx.foo(null)
nativeHeap.free(cxx)
}
fun test2() {
println("test2")
val x = ns__bar(null)
// val theS = interpretPointed< CppTest>(ns__bar(null).rawValue)
// theS.foo(null)
println("x.useContents {iPub} = ${x.useContents {iPub}}" )
}
*/
@@ -0,0 +1,24 @@
language = C++
compilerOpts = -std=c++14
package = cpptypes
---
class CppTest {
public:
explicit CppTest(int m) : n(m) {}
int n = 11;
virtual int get() const { return n; }
virtual void set(int m) { n = m; }
};
CppTest retByValue(int k) { return CppTest(k); }
CppTest* retByPtr(int k) { return new CppTest(k); } // Leak is intentional
CppTest const* retByPtrConst(int k) { return new CppTest(k); } // Leak is intentional
CppTest& retByRef(int k) { return * new CppTest(k); } // Leak is intentional
CppTest const& retByRefConst(int k) { return * new CppTest(k); } // Leak is intentional
int paramByValue(CppTest x) { return x.get(); }
int paramByPtr(CppTest* x) { return x? x->get() : 0; }
int paramByPtrConst(CppTest const* x) { return x? x->get() : 0; }
int paramByRef(CppTest& x) { return x.get(); }
int paramByRefConst(CppTest const& x) { return x.get(); }
@@ -0,0 +1,93 @@
import kotlinx.cinterop.*
import kotlin.test.*
import kotlin.random.*
import cpptypes.*
@Test
fun test_retByValue(k: Int) {
memScoped {
val x: CppTest = retByValue(k).getPointer(memScope).pointed
assertEquals(k, x.get())
}
}
@Test
fun test_retByPtr(k: Int) {
val x = interpretPointed<CppTest>(retByPtr(k).rawValue)
assertEquals(k, x.get())
}
@Test
fun test_retByPtrConst(k: Int) {
val x = interpretPointed<CppTest>(retByPtrConst(k).rawValue)
assertEquals(k, x.get())
}
@Test
fun test_retByRef(k: Int) {
val x = interpretPointed<CppTest>(retByRef(k).rawValue)
assertEquals(k, x.get())
}
@Test
fun test_retByRefConst(k: Int) {
val x = interpretPointed<CppTest>(retByRefConst(k).rawValue)
assertEquals(k, x.get())
}
@Test
fun test_paramByValue(k: Int) {
val x = nativeHeap.alloc<CppTest>() {}
CppTest.__init__(x.ptr, k)
assertEquals(k, paramByValue(x.readValue()))
nativeHeap.free(x)
}
@Test
fun test_paramByPtr(k: Int) {
val x = nativeHeap.alloc<CppTest>() {}
CppTest.__init__(x.ptr, k)
assertEquals(k, paramByPtr(x.ptr))
nativeHeap.free(x)
}
@Test
fun test_paramByPtrConst(k: Int) {
val x = nativeHeap.alloc<CppTest>() {}
CppTest.__init__(x.ptr, k)
assertEquals(k, paramByPtrConst(x.ptr))
nativeHeap.free(x)
}
@Test
fun test_paramByRef(k: Int) {
val x = nativeHeap.alloc<CppTest>() {}
CppTest.__init__(x.ptr, k)
assertEquals(k, paramByRef(x.ptr))
nativeHeap.free(x)
}
@Test
fun test_paramByRefConst(k: Int) {
val x = nativeHeap.alloc<CppTest>() {}
CppTest.__init__(x.ptr, k)
assertEquals(k, paramByRefConst(x.ptr))
nativeHeap.free(x)
}
fun main() {
val seed = Random.nextInt()
val r = Random(seed)
test_retByValue(r.nextInt())
test_retByPtr(r.nextInt())
test_retByPtrConst(r.nextInt())
test_retByRef(r.nextInt())
test_retByRefConst(r.nextInt())
test_paramByValue(r.nextInt())
test_paramByPtr(r.nextInt())
test_paramByPtrConst(r.nextInt())
test_paramByRef(r.nextInt())
test_paramByRefConst(r.nextInt())
}