forked from Blendman974/faceclaw
Kotlin on iOS
This commit is contained in:
1 parent
63b2a7d545
commit
bc8e80c533
12 files changed
+149
-57
No files matched your search
+16
-27
@@ -1,47 +1,36 @@
|
||||
import { isAndroid } from "@nativescript/core";
|
||||
|
||||
// Handwritten declarations for this small native surface. NativeScript builds
|
||||
// the runtime metadata from Kotlin's compiled classes, not these declarations.
|
||||
declare namespace com.faceclaw.shared {
|
||||
class KotlinBridge extends java.lang.Object {
|
||||
constructor();
|
||||
greet(name: string): string;
|
||||
roundTrip(message: string, listener: KotlinBridgeListener): string;
|
||||
}
|
||||
class KotlinBridgeListener extends java.lang.Object {
|
||||
constructor(implementation: { transform(message: string): string });
|
||||
}
|
||||
}
|
||||
import { createKotlinBridge } from "./kotlin-platform";
|
||||
|
||||
/** Exercise the real native metadata and both directions of return values. */
|
||||
export function runKotlinBridgeSmokeTest(): void {
|
||||
if (!isAndroid) return;
|
||||
|
||||
try {
|
||||
const bridge = new com.faceclaw.shared.KotlinBridge();
|
||||
const bridge = createKotlinBridge();
|
||||
const platform = bridge.platform();
|
||||
const thread = bridge.threadName();
|
||||
const callerOnMain = bridge.isMainThread();
|
||||
const greeting = bridge.greet("Faceclaw");
|
||||
const message = "Faceclaw \u2194 Kotlin \ud83d\udc53";
|
||||
let callbackCount = 0;
|
||||
let callbackMessage = "";
|
||||
// Keep the proxy in scope through the synchronous native call. Do not
|
||||
// throw from the callback; validate after returning across the boundary.
|
||||
const listener = new com.faceclaw.shared.KotlinBridgeListener({
|
||||
transform(value: string): string {
|
||||
callbackCount++;
|
||||
callbackMessage = value;
|
||||
return `TypeScript received: ${value}`;
|
||||
},
|
||||
let callbackOnMain = false;
|
||||
// Validate after returning; never throw from a native callback.
|
||||
const reply = bridge.roundTrip(message, (value: string): string => {
|
||||
callbackCount++;
|
||||
callbackMessage = value;
|
||||
callbackOnMain = bridge.isMainThread();
|
||||
return `TypeScript received: ${value}`;
|
||||
});
|
||||
const reply = bridge.roundTrip(message, listener);
|
||||
if (greeting !== "Hello, Faceclaw, from Kotlin"
|
||||
|| platform !== (isAndroid ? "Android" : "iOS")
|
||||
|| !thread || !callerOnMain || !callbackOnMain
|
||||
|| callbackCount !== 1
|
||||
|| callbackMessage !== `Kotlin received: ${message}`
|
||||
|| reply !== `Kotlin received callback: TypeScript received: Kotlin received: ${message}`) {
|
||||
throw new Error(`Unexpected Kotlin bridge result: ${JSON.stringify({
|
||||
greeting, callbackCount, callbackMessage, reply,
|
||||
platform, thread, callerOnMain, callbackOnMain, greeting, callbackCount, callbackMessage, reply,
|
||||
})}`);
|
||||
}
|
||||
console.log("FACECLAW_KOTLIN_BRIDGE_PASS " + reply);
|
||||
console.log("FACECLAW_KOTLIN_BRIDGE_PASS " + JSON.stringify({ platform, thread, reply }));
|
||||
} catch (error) {
|
||||
// Diagnostics must not prevent the rest of the app from launching.
|
||||
console.error("FACECLAW_KOTLIN_BRIDGE_FAIL " + error);
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
import type { KotlinBridge } from './kotlin-platform';
|
||||
|
||||
// Handwritten declarations for the small native surface, checked against the
|
||||
// compiled API. NativeScript runtime metadata is generated from the AAR.
|
||||
declare namespace com.faceclaw.shared {
|
||||
class KotlinBridge extends java.lang.Object {
|
||||
constructor();
|
||||
platform(): string;
|
||||
threadName(): string;
|
||||
isMainThread(): boolean;
|
||||
greet(name: string): string;
|
||||
roundTrip(message: string, listener: KotlinBridgeListener): string;
|
||||
}
|
||||
class KotlinBridgeListener extends java.lang.Object {
|
||||
constructor(implementation: { transform(message: string): string });
|
||||
}
|
||||
}
|
||||
|
||||
export function createKotlinBridge(): KotlinBridge {
|
||||
const bridge = new com.faceclaw.shared.KotlinBridge();
|
||||
return {
|
||||
platform: () => bridge.platform(),
|
||||
threadName: () => bridge.threadName(),
|
||||
isMainThread: () => bridge.isMainThread(),
|
||||
greet: name => bridge.greet(name),
|
||||
roundTrip(message, transform) {
|
||||
const listener = new com.faceclaw.shared.KotlinBridgeListener({ transform });
|
||||
return bridge.roundTrip(message, listener);
|
||||
},
|
||||
};
|
||||
}
|
||||
Vendored
+11
@@ -0,0 +1,11 @@
|
||||
/** Exported native API normalized across Java interfaces and Obj-C protocols. */
|
||||
export interface KotlinBridge {
|
||||
platform(): string;
|
||||
threadName(): string;
|
||||
isMainThread(): boolean;
|
||||
greet(name: string): string;
|
||||
/** Synchronous callback; must not throw across the native boundary. */
|
||||
roundTrip(message: string, transform: (message: string) => string): string;
|
||||
}
|
||||
|
||||
export function createKotlinBridge(): KotlinBridge;
|
||||
@@ -0,0 +1,42 @@
|
||||
import type { KotlinBridge } from './kotlin-platform';
|
||||
|
||||
// Handwritten declarations matching FaceclawKit's Objective-C header.
|
||||
declare class FaceclawKitKotlinBridge extends NSObject {
|
||||
static alloc(): FaceclawKitKotlinBridge;
|
||||
init(): this;
|
||||
platform(): string;
|
||||
threadName(): string;
|
||||
isMainThread(): boolean;
|
||||
greetName(name: string): string;
|
||||
roundTripMessageListener(message: string, listener: FaceclawKitKotlinBridgeListener): string;
|
||||
}
|
||||
interface FaceclawKitKotlinBridgeListener {
|
||||
transformMessage(message: string): string;
|
||||
}
|
||||
declare const FaceclawKitKotlinBridgeListener: {
|
||||
prototype: FaceclawKitKotlinBridgeListener;
|
||||
};
|
||||
|
||||
@NativeClass()
|
||||
class KotlinCallback extends NSObject implements FaceclawKitKotlinBridgeListener {
|
||||
static ObjCProtocols = [FaceclawKitKotlinBridgeListener];
|
||||
transform!: (message: string) => string;
|
||||
|
||||
transformMessage(message: string): string { return this.transform(message); }
|
||||
}
|
||||
|
||||
export function createKotlinBridge(): KotlinBridge {
|
||||
const bridge = FaceclawKitKotlinBridge.alloc().init();
|
||||
return {
|
||||
platform: () => bridge.platform(),
|
||||
threadName: () => bridge.threadName(),
|
||||
isMainThread: () => bridge.isMainThread(),
|
||||
greet: name => bridge.greetName(name),
|
||||
roundTrip(message, transform) {
|
||||
const listener = KotlinCallback.new() as KotlinCallback;
|
||||
listener.transform = transform;
|
||||
// Kotlin uses this listener synchronously and never retains it.
|
||||
return bridge.roundTripMessageListener(message, listener);
|
||||
},
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user