Code Monkey home page Code Monkey logo

kotlin-native-ios-consoleasync's Introduction

Kotlin Native iOS - Console Async | video

Console Async

Simulator session started with process 8450

pthread[0x604000004f88] -> application : <UIApplication: 0x7fbcb1900750>
pthread[0x604000005088] -> viewDidLoad 
pthread[0x60000000f728] -> applicationDidBecomeActive : <UIApplication: 0x7fbcb1900750>
pthread[0x60000000f798] -> prepare : Optional("Victor, Bolin")
pthread[0x60000000fd58] -> viewDidLoad : Hello from Kotlin!
pthread[0x604000005028] -> applicationWillResignActive : <UIApplication: 0x7fbcb1900750>
pthread[0x60000000f728] -> applicationDidEnterBackground : <UIApplication: 0x7fbcb1900750>
pthread[0x6000000047c8] -> applicationWillEnterForeground : <UIApplication: 0x7fbcb1900750>
pthread[0x604000007378] -> applicationDidBecomeActive : <UIApplication: 0x7fbcb1900750>

Framework Kotlin

import kotlinx.cinterop.*
import platform.posix.*
import platform.posix.errno_t

class KotlinNativeFramework( ) {
    fun hi() = "Hello from Kotlin!"
    fun fullName(f: String, s: String) = "$s, $f"

    fun printAsync(str : String) = processor<String> { str }

    private inline fun <reified T> processor(crossinline action : ()-> T ) {
        val arena = Arena()
        memScoped {
            val thread = alloc<pthread_tVar>()
            val rc = pthread_create(thread.ptr, null, staticCFunction {
                _argC ->
                initRuntimeIfNeeded()
                val arg = konan.worker.attachObjectGraph<T>(_argC)
                println(arg)
                null as COpaquePointer?
            },konan.worker.detachObjectGraph { "pthread[${thread.rawPtr}] -> ${action()}" } )
            pthread_join(thread.value, null)

            if (rc != 0)
                errExitEN(rc, "pthread_create")

            pthread_join(thread.value, null)
        }
        arena.clear()
    }

    private fun errExitEN(en: Int, msg: String) {
        do {
            h_errno = en;
            perror(msg);
            exit(EXIT_FAILURE);
        } while (true)
    }

}

Parse to Objective-C

#import <Foundation/Foundation.h>

@class KNFKotlinNativeFramework;

NS_ASSUME_NONNULL_BEGIN

@interface KotlinBase : NSObject
-(instancetype) init __attribute__((unavailable));
+(instancetype) new __attribute__((unavailable));
+(void)initialize __attribute__((objc_requires_super));
@end;

@interface KotlinBase (KotlinBaseCopying) <NSCopying>
@end;

__attribute__((objc_runtime_name("KotlinMutableSet")))
@interface KNFMutableSet<ObjectType> : NSMutableSet<ObjectType>
@end;

__attribute__((objc_runtime_name("KotlinMutableDictionary")))
@interface KNFMutableDictionary<KeyType, ObjectType> : NSMutableDictionary<KeyType, ObjectType>
@end;

@interface NSError (NSErrorKotlinException)
@property (readonly) id _Nullable kotlinException;
@end;

__attribute__((objc_subclassing_restricted))
@interface KNFKotlinNativeFramework : KotlinBase
-(instancetype)init NS_SWIFT_NAME(init()) NS_DESIGNATED_INITIALIZER;
+ (instancetype)new OBJC_SWIFT_UNAVAILABLE("use object initializers instead");

-(NSString *)hi NS_SWIFT_NAME(hi());
-(NSString *)fullNameF:(NSString *)f s:(NSString *)s NS_SWIFT_NAME(fullName(f:s:));
@end;

NS_ASSUME_NONNULL_END

--->>> Repo: Kotlin Koans <<<---

--->>> Repo: Getting Started Kotlin <<<---

--->>> Repo: Problems Kotlin <<<---

--->>> Repo: GameBoy Emulator Enviroment <<<---

--->>> Repo: Kotlin Mobile <<<---

--->>> Repo: Kotlin JavaScript <<<---

--->>> Repo: Ktor Examples <<<---

kotlin-native-ios-consoleasync's People

Contributors

vicboma1 avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.