Generate SSD vision bridge from typed Jerboa

ober

93ea3d51743613249b58edc7b28784bc055a79b8

diff --git a/.build.yml b/.build.yml
index b67de89..2bb9185 100644
--- a/.build.yml
+++ b/.build.yml
@@ -5,7 +5,7 @@ packages:
   - make=4.4.1-r4
 sources:
   # Build dependency: full immutable commit, mirrored in dependencies.lock.json.
-  - "https://git.sr.ht/~lisp/jerboa#4128d613222aa16f9d2cb8b7c311c73fb4760ecf"
+  - "https://git.sr.ht/~lisp/jerboa#1a1b380b52cb6eeaf1397adfffd73e1c4c8d035c"
   # The second source is the build subject selected by the SourceHut submitter.
   - https://git.sr.ht/~lisp/jerboa-android
 tasks:
@@ -14,6 +14,6 @@ tasks:
       test "$(apk info -v chez-scheme)" = chez-scheme-10.3.0-r2
       test "$(apk info -v git)" = git-2.54.0-r0
       test "$(apk info -v make)" = make-4.4.1-r4
-      test "$(git -C ../jerboa rev-parse HEAD)" = 4128d613222aa16f9d2cb8b7c311c73fb4760ecf
-      test "$(git -C ../jerboa rev-parse 'HEAD^{tree}')" = 0100bf29761be1324a8e3f811c03702a9c609fb8
+      test "$(git -C ../jerboa rev-parse HEAD)" = 1a1b380b52cb6eeaf1397adfffd73e1c4c8d035c
+      test "$(git -C ../jerboa rev-parse 'HEAD^{tree}')" = d01e22a97edcd50394e707154cb8c45e50dfd683
       JERBOA="chez --libdirs .:../jerboa/lib --script" make test
diff --git a/dependencies.lock.json b/dependencies.lock.json
index 18a7884..7208bee 100644
--- a/dependencies.lock.json
+++ b/dependencies.lock.json
@@ -11,8 +11,8 @@
   "generator_runtime": {
     "name": "jerboa",
     "repository": "https://git.sr.ht/~lisp/jerboa",
-    "commit": "4128d613222aa16f9d2cb8b7c311c73fb4760ecf",
-    "tree": "0100bf29761be1324a8e3f811c03702a9c609fb8"
+    "commit": "1a1b380b52cb6eeaf1397adfffd73e1c4c8d035c",
+    "tree": "d01e22a97edcd50394e707154cb8c45e50dfd683"
   },
   "assurance_tools": {
     "osv_scanner": {
diff --git a/scripts/verify-supply-chain.sh b/scripts/verify-supply-chain.sh
index 36003a5..3b0d92c 100755
--- a/scripts/verify-supply-chain.sh
+++ b/scripts/verify-supply-chain.sh
@@ -3,8 +3,8 @@ set -eu
 
 repo=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd -P)
 lock="$repo/dependencies.lock.json"
-jerboa_commit=4128d613222aa16f9d2cb8b7c311c73fb4760ecf # gitsafe:ignore
-jerboa_tree=0100bf29761be1324a8e3f811c03702a9c609fb8 # gitsafe:ignore
+jerboa_commit=1a1b380b52cb6eeaf1397adfffd73e1c4c8d035c # gitsafe:ignore
+jerboa_tree=d01e22a97edcd50394e707154cb8c45e50dfd683 # gitsafe:ignore
 gradle_sha=20f1b1176237254a6fc204d8434196fa11a4cfb387567519c61556e8710aed78
 jdk_macos_sha=8fa1eff40bb637a33613b2ccb8b12c70dc3661cc22cf8e784943715769a05336
 jdk_linux_sha=d8afc263758141a66e0e3aafc321e783f7016696f4eaea067d340a269037d331
diff --git a/templates/ssd-review.ss b/templates/ssd-review.ss
index 5857391..2e04cf0 100644
--- a/templates/ssd-review.ss
+++ b/templates/ssd-review.ss
@@ -11315,47 +11315,99 @@
           (ssdVisionLoadErrorMessageRaw))
         (def (ssdOcrRecognizeWords (bitmap : Bitmap)) : (List OcrWord)
           (ssdOcrRecognizeWordsRaw bitmap))))
-    (kotlin-file-lines "com/sfb/ssdreview/SsdVisionBridge.kt"
-      (
-       "package com.sfb.ssdreview"
-       ""
-       "import android.graphics.Bitmap"
-       "import org.json.JSONObject"
-       ""
-       "object SsdVisionBridge {"
-       "    private var loadError: Throwable? = null"
-       "    private val loaded: Boolean = try {"
-       "        System.loadLibrary(\"jerboa_vision\")"
-       "        true"
-       "    } catch (error: Throwable) {"
-       "        loadError = error"
-       "        false"
-       "    }"
-       ""
-       "    fun isAvailable(): Boolean = loaded"
-       ""
-       "    fun loadErrorMessage(): String = throwableMessageOrEmpty(loadError)"
-       ""
-       "    fun detect(bitmap: Bitmap): NativeDetection? {"
-       "        if (bitmapNotReadyForNativeDetection(loaded, bitmap)) return null"
-       "        val rgba = bitmapToRgba(bitmap)"
-       "        val jsonText = try {"
-       "            nativeDetectRgbaJson(bitmapPixelWidth(bitmap), bitmapPixelHeight(bitmap), rgba)"
-       "        } catch (error: Throwable) {"
-       "            loadError = error"
-       "            return null"
-       "        }"
-       "        val root = try { jsonObjectFromText(jsonText) } catch (_: Exception) { return null }"
-       "        if (nativeDetectionRootFailed(root)) {"
-       "            loadError = IllegalStateException(nativeDetectionRootError(root))"
-       "            return null"
-       "        }"
-       "        return nativeDetectionFromJsonRoot(root)"
-       "    }"
-       ""
-       "    private external fun nativeDetectRgbaJson(width: Int, height: Int, rgba: ByteArray): String"
-       "}"
-       ))
+    (typed-kotlin-file "com/sfb/ssdreview/SsdVisionBridge.kt"
+      (kotlin-imports (android graphics Bitmap)
+                      (org json JSONObject))
+      (typed-library (com sfb ssdreview)
+        (export SsdVisionBridge)
+        (type Bitmap)
+        (type Exception)
+        (type Int32)
+        (type JSONObject)
+        (type NativeDetection)
+        (type Throwable)
+        (object SsdVisionBridge
+          (extern (systemLoadLibrary (name : String)) : Unit
+            (kotlin-call System loadLibrary))
+          (extern (nativeDetectRgbaJson
+                    (width : Int32)
+                    (height : Int32)
+                    (rgba : Bytes)) : String
+            (kotlin-external))
+          (extern (illegalStateException (message : String)) : Throwable
+            (kotlin-call IllegalStateException))
+          (extern (bitmapPixelWidth (bitmap : Bitmap)) : Int32
+            (kotlin-call bitmapPixelWidth))
+          (extern (bitmapPixelHeight (bitmap : Bitmap)) : Int32
+            (kotlin-call bitmapPixelHeight))
+          (extern (bitmapNotReadyForNativeDetection
+                    (loaded : Bool)
+                    (bitmap : Bitmap)) : Bool
+            (kotlin-call bitmapNotReadyForNativeDetection))
+          (extern (bitmapToRgba (bitmap : Bitmap)) : Bytes
+            (kotlin-call bitmapToRgba))
+          (extern (jsonObjectFromText (text : String)) : JSONObject
+            (kotlin-call jsonObjectFromText))
+          (extern (nativeDetectionRootFailed (root : JSONObject)) : Bool
+            (kotlin-call nativeDetectionRootFailed))
+          (extern (nativeDetectionRootError (root : JSONObject)) : String
+            (kotlin-call nativeDetectionRootError))
+          (extern (nativeDetectionFromJsonRoot
+                    (root : JSONObject)) : (Nullable NativeDetection)
+            (kotlin-call nativeDetectionFromJsonRoot))
+          (extern (throwableMessageOrEmpty
+                    (error : (Nullable Throwable))) : String
+            (kotlin-call throwableMessageOrEmpty))
+          (var loadError : (Nullable Throwable)
+            (nullable-none Throwable))
+          (val loaded : Bool
+            (try
+              (begin
+                (systemLoadLibrary "jerboa_vision")
+                #t)
+              (catch (error : Throwable)
+                (begin
+                  (set! loadError (nullable-some error))
+                  #f))))
+          (def (isAvailable) : Bool
+            loaded)
+          (def (loadErrorMessage) : String
+            (throwableMessageOrEmpty loadError))
+          (def (detect (bitmap : Bitmap)) : (Nullable NativeDetection)
+            (if (bitmapNotReadyForNativeDetection loaded bitmap)
+                (nullable-none NativeDetection)
+                (let ((rgba (bitmapToRgba bitmap)))
+                  (let ((jsonText
+                          (try
+                            (nullable-some
+                              (nativeDetectRgbaJson
+                                (bitmapPixelWidth bitmap)
+                                (bitmapPixelHeight bitmap)
+                                rgba))
+                            (catch (error : Throwable)
+                              (begin
+                                (set! loadError (nullable-some error))
+                                (nullable-none String))))))
+                    (if (nullable-null? jsonText)
+                        (nullable-none NativeDetection)
+                        (let ((root
+                                (try
+                                  (nullable-some
+                                    (jsonObjectFromText
+                                      (nullable-get jsonText)))
+                                  (catch (error : Exception)
+                                    (nullable-none JSONObject)))))
+                          (if (nullable-null? root)
+                              (nullable-none NativeDetection)
+                              (let ((rootJson (nullable-get root)))
+                                (if (nativeDetectionRootFailed rootJson)
+                                    (begin
+                                      (set! loadError
+                                        (nullable-some
+                                          (illegalStateException
+                                            (nativeDetectionRootError rootJson))))
+                                      (nullable-none NativeDetection))
+                                    (nativeDetectionFromJsonRoot rootJson)))))))))))))
     (kotlin-file-lines "com/sfb/ssdreview/SsdOcr.kt"
       (
        "package com.sfb.ssdreview"