diff --git a/.gitignore b/.gitignore index 6d3aefcb..9efee48b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,8 @@ # NativeScript -hooks/ +hooks/* +!hooks/before-prepare/ +hooks/before-prepare/* +!hooks/before-prepare/faceclaw-kotlin.js node_modules/ platforms/ @@ -44,3 +47,15 @@ faceclaw_settings.ios.json # Developer configs build_paths.sh **/xcuserdata/ + +# Kotlin build outputs and staged NativeScript artifacts +native/kotlin/.gradle/ +native/kotlin/.kotlin/ +native/kotlin/build/ +native/kotlin/shared/build/ +native/kotlin/local.properties +native/kotlin/plugin/platforms/android/*.aar +native/kotlin/plugin/platforms/ios/ +!native/kotlin/plugin/platforms/ +!native/kotlin/plugin/platforms/android/ +!native/kotlin/plugin/platforms/android/include.gradle diff --git a/App_Resources/Android/before-plugins.gradle b/App_Resources/Android/before-plugins.gradle index f643993d..8ebcb235 100644 --- a/App_Resources/Android/before-plugins.gradle +++ b/App_Resources/Android/before-plugins.gradle @@ -10,7 +10,7 @@ project.ext { // androidXTransition = "1.4.1" // androidXViewPager = "1.0.0" - // NativeScript 9 already applies kotlin-android. Pin its current compiler - // for our .kt sources; no separate Kotlin installation is required. - kotlinVersion = "2.2.20" + // Keep the host compiler/runtime compatible with native/kotlin/build.gradle.kts. + // NativeScript 9 already applies kotlin-android. + kotlinVersion = "2.4.20" } diff --git a/App_Resources/Android/src/main/java/com/faceclaw/shared/KotlinBridge.kt b/App_Resources/Android/src/main/java/com/faceclaw/shared/KotlinBridge.kt deleted file mode 100644 index 809537dd..00000000 --- a/App_Resources/Android/src/main/java/com/faceclaw/shared/KotlinBridge.kt +++ /dev/null @@ -1,21 +0,0 @@ -package com.faceclaw.shared - -/** NativeScript implements this Kotlin-owned contract in TypeScript. */ -interface KotlinBridgeListener { - fun transform(message: String): String -} - -/** - * First step toward commonMain: only portable Kotlin, with no Android/JVM APIs. - * Calls and callbacks are synchronous on the caller's thread. The listener is - * used only during roundTrip and is never retained. - */ -class KotlinBridge { - fun greet(name: String): String = "Hello, $name, from Kotlin" - - fun roundTrip(message: String, listener: KotlinBridgeListener): String { - val reply = listener.transform("Kotlin received: $message") - // Consume the TypeScript return value, then return it across the bridge. - return "Kotlin received callback: $reply" - } -} diff --git a/App_Resources/iOS/build.xcconfig b/App_Resources/iOS/build.xcconfig index 950bebe4..15e50f3e 100644 --- a/App_Resources/iOS/build.xcconfig +++ b/App_Resources/iOS/build.xcconfig @@ -8,3 +8,6 @@ OTHER_LDFLAGS = $(inherited) -framework CoreBluetooth -framework CoreText -frame ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; HEADER_SEARCH_PATHS = $(inherited) "$(SRCROOT)/../../App_Resources/iOS/src/lc3" + +// FaceclawKit ships arm64 device and Apple-silicon simulator slices. +EXCLUDED_ARCHS[sdk=iphonesimulator*] = x86_64; diff --git a/app/bootstrap.ios.ts b/app/bootstrap.ios.ts index 1878ffce..599004a1 100644 --- a/app/bootstrap.ios.ts +++ b/app/bootstrap.ios.ts @@ -1,5 +1,9 @@ import { Application } from '@nativescript/core' +import { runKotlinBridgeSmokeTest } from './native/kotlin-bridge' declare const FaceclawConfigPort: any +declare const __DEV__: boolean; + +if (__DEV__) runKotlinBridgeSmokeTest() // Apply transferred preferences before settings getters or app workers load. FaceclawConfigPort.processPendingRequest() diff --git a/app/native/kotlin-bridge.ts b/app/native/kotlin-bridge.ts index 6baa866e..6b463807 100644 --- a/app/native/kotlin-bridge.ts +++ b/app/native/kotlin-bridge.ts @@ -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); diff --git a/app/native/kotlin-platform.android.ts b/app/native/kotlin-platform.android.ts new file mode 100644 index 00000000..7ca6ba1c --- /dev/null +++ b/app/native/kotlin-platform.android.ts @@ -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); + }, + }; +} diff --git a/app/native/kotlin-platform.d.ts b/app/native/kotlin-platform.d.ts new file mode 100644 index 00000000..0322448c --- /dev/null +++ b/app/native/kotlin-platform.d.ts @@ -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; diff --git a/app/native/kotlin-platform.ios.ts b/app/native/kotlin-platform.ios.ts new file mode 100644 index 00000000..24e4ea2c --- /dev/null +++ b/app/native/kotlin-platform.ios.ts @@ -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); + }, + }; +} diff --git a/package-lock.json b/package-lock.json index 677da298..8446d8ae 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,6 +9,7 @@ "version": "1.0.0", "dependencies": { "@evenrealities/pretext": "^0.1.4", + "@faceclaw/kotlin": "file:native/kotlin/plugin", "@nativescript/core": "~9.0.0", "@nativescript/theme": "^3.1.0", "@xterm/headless": "^6.0.0", @@ -19,13 +20,17 @@ "upng-js": "^2.1.0" }, "devDependencies": { - "@nativescript/android": "9.0.4", + "@nativescript/android": "9.1.1", "@nativescript/ios": "9.0.3", "@nativescript/types": "~9.0.0", "@nativescript/webpack": "~5.0.25", "typescript": "~5.4.0" } }, + "native/kotlin/plugin": { + "name": "@faceclaw/kotlin", + "version": "0.1.0" + }, "node_modules/@babel/code-frame": { "version": "7.29.0", "resolved": "https://registry.npmjs.org/@babel/code-frame/-/code-frame-7.29.0.tgz", @@ -412,6 +417,10 @@ "integrity": "sha512-gSZk/uxIhZW3geOM5bP1IS0s1sg7cA7KLLpBDgH9T1+ZNHMIFTMcgQN1qw4+/Xi/rcxsDsWZ7UZndIS/s4fRcA==", "license": "MIT" }, + "node_modules/@faceclaw/kotlin": { + "resolved": "native/kotlin/plugin", + "link": true + }, "node_modules/@jridgewell/gen-mapping": { "version": "0.3.13", "resolved": "https://registry.npmjs.org/@jridgewell/gen-mapping/-/gen-mapping-0.3.13.tgz", @@ -474,9 +483,9 @@ } }, "node_modules/@nativescript/android": { - "version": "9.0.4", - "resolved": "https://registry.npmjs.org/@nativescript/android/-/android-9.0.4.tgz", - "integrity": "sha512-htfv7Ku/M6Tq6fQjhV0nSwxa9agNYEs9x4zBdOW8mRye5CZS7xTJq83MffYkItrtN/jJeeaxiSAgu0F+L4oHgA==", + "version": "9.1.1", + "resolved": "https://registry.npmjs.org/@nativescript/android/-/android-9.1.1.tgz", + "integrity": "sha512-oDWfVnQtgQ0ctbfFWSugqGEZnYVjls82FqlO5UbA9HNwTNqdP4gqWwqHR80WKQngrbxlRHYDIuYv5S9kQfKhcw==", "dev": true }, "node_modules/@nativescript/core": { diff --git a/package.json b/package.json index 210a9ed8..a9779a9a 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,9 @@ "version": "1.0.0", "private": false, "scripts": { + "native:android": "node scripts/kotlin-build.cjs android", + "native:ios": "node scripts/kotlin-build.cjs ios", + "test:kotlin": "cd native/kotlin && ./gradlew :shared:testAndroidHostTest :shared:iosSimulatorArm64Test", "build:ios": "scripts/ios.sh build ios --for-device", "run:ios": "scripts/ios.sh run ios", "prepare:ios": "scripts/ios.sh prepare ios", @@ -16,6 +19,7 @@ }, "dependencies": { "@evenrealities/pretext": "^0.1.4", + "@faceclaw/kotlin": "file:native/kotlin/plugin", "@nativescript/core": "~9.0.0", "@nativescript/theme": "^3.1.0", "@xterm/headless": "^6.0.0", @@ -26,7 +30,7 @@ "upng-js": "^2.1.0" }, "devDependencies": { - "@nativescript/android": "9.0.4", + "@nativescript/android": "9.1.1", "@nativescript/ios": "9.0.3", "@nativescript/types": "~9.0.0", "@nativescript/webpack": "~5.0.25", diff --git a/scripts/ios.sh b/scripts/ios.sh index 3ebb3f65..e86bc302 100755 --- a/scripts/ios.sh +++ b/scripts/ios.sh @@ -2,6 +2,11 @@ set -euo pipefail cd "$(dirname "$0")/.." +# Kotlin/Native also needs the JDK; share the existing local build environment. +if [[ -f build_paths.sh ]]; then + source ./build_paths.sh +fi + # Prefer Homebrew Ruby over the older macOS system Ruby when available. for ruby_bin in /opt/homebrew/opt/ruby/bin /usr/local/opt/ruby/bin; do if [[ -x "$ruby_bin/ruby" ]]; then