TypeScript
perf(mobile): call the highlights lexer through Nitro, up to 10x faster calls on Android (#31188)
Cameron Chisholm6b53b3dc3117
diff --git a/.macroscope/ignore.md b/.macroscope/ignore.mdindex 66e5434c5ed..7e67d1d0594 100644--- a/.macroscope/ignore.md+++ b/.macroscope/ignore.md@@ -279,3 +279,7 @@ native/libghostty-vt/include/**# it needs. Regenerated by apps/mobile/scripts/generate-highlights.mts; review# that script and cpp/t3_highlights.c instead.apps/mobile/modules/t3-highlights/Vendor/**++# Nitro bindings for T3Highlights. Regenerated by `vp run generate:nitro` in+# apps/mobile from modules/t3-highlights/src/T3Highlights.nitro.ts.+apps/mobile/modules/t3-highlights/nitrogen/generated/**diff --git a/apps/mobile/.swiftlint.yml b/apps/mobile/.swiftlint.ymlindex 532a13e37ed..2dd29c067dc 100644--- a/apps/mobile/.swiftlint.yml+++ b/apps/mobile/.swiftlint.yml@@ -1,7 +1,6 @@included:- ios/T3Code- modules/t3-composer-editor/ios- - modules/t3-highlights/ios- modules/t3-native-controls/ios- modules/t3-terminal/ios- modules/t3-review-diff/iosdiff --git a/apps/mobile/modules/t3-highlights/T3HighlightsNative.podspec b/apps/mobile/modules/t3-highlights/T3HighlightsNative.podspecindex 0cc380369f0..df5e00814f7 100644--- a/apps/mobile/modules/t3-highlights/T3HighlightsNative.podspec+++ b/apps/mobile/modules/t3-highlights/T3HighlightsNative.podspec@@ -10,21 +10,24 @@ Pod::Spec.new do |s|s.homepage = 'https://t3tools.com's.license = { :type => 'UNLICENSED' }s.author = { 'T3 Tools' => '[email protected]' }- s.platforms = { :ios => '16.1' }+ s.platforms = { :ios => min_ios_version_supported }s.source = { :path => '.' }- s.source_files = 'ios/**/*.swift', 'cpp/**/*.{c,h,inc}', 'Vendor/**/*.{c,h,inc}'- # Swift sees only the wrapper; the generated and runtime headers stay private.- s.public_header_files = 'cpp/t3_highlights.h'+ s.source_files = 'cpp/**/*.{c,cpp,h,hpp,inc}', 'Vendor/**/*.{c,h,inc}'+ s.public_header_files = []# Debug builds compile at -O0 by default, which makes the lexers several times# slower. They are generated code, so optimize them in every configuration.s.compiler_flags = '-O2's.pod_target_xcconfig = {- 'DEFINES_MODULE' => 'YES',# Plain malloc memory with explicit bounds checks: no guard-page signal# handlers competing with the app's crash reporting.'GCC_PREPROCESSOR_DEFINITIONS' => '$(inherited) WASM_RT_USE_MMAP=0 WASM_RT_MEMCHECK_BOUNDS_CHECK=1 WASM_RT_SKIP_SIGNAL_RECOVERY=1',- 'HEADER_SEARCH_PATHS' => '$(inherited) "${PODS_TARGET_SRCROOT}/Vendor" "${PODS_TARGET_SRCROOT}/Vendor/highlights" "${PODS_TARGET_SRCROOT}/Vendor/wasm2c"',+ 'HEADER_SEARCH_PATHS' => '$(inherited) "${PODS_TARGET_SRCROOT}/cpp" "${PODS_TARGET_SRCROOT}/Vendor" "${PODS_TARGET_SRCROOT}/Vendor/highlights" "${PODS_TARGET_SRCROOT}/Vendor/wasm2c"',}- s.swift_version = '5.9'- s.dependency 'ExpoModulesCore'++ load 'nitrogen/generated/ios/T3HighlightsNative+autolinking.rb'+ add_nitrogen_files(s)++ s.dependency 'React-jsi'+ s.dependency 'React-callinvoker'+ install_modules_dependencies(s)enddiff --git a/apps/mobile/modules/t3-highlights/android/src/main/cpp/CMakeLists.txt b/apps/mobile/modules/t3-highlights/android/CMakeLists.txtsimilarity index 72%rename from apps/mobile/modules/t3-highlights/android/src/main/cpp/CMakeLists.txtrename to apps/mobile/modules/t3-highlights/android/CMakeLists.txtindex 297ac3d2cb2..28e3f7bc361 100644--- a/apps/mobile/modules/t3-highlights/android/src/main/cpp/CMakeLists.txt+++ b/apps/mobile/modules/t3-highlights/android/CMakeLists.txt@@ -1,18 +1,24 @@cmake_minimum_required(VERSION 3.22.1)-project(t3highlights LANGUAGES C)+project(t3highlights LANGUAGES C CXX)-set(T3_HIGHLIGHTS_ROOT "${CMAKE_CURRENT_SOURCE_DIR}/../../../..")+set(CMAKE_CXX_STANDARD 20)+set(CMAKE_CXX_STANDARD_REQUIRED ON)+set(T3_HIGHLIGHTS_ROOT "${CMAKE_SOURCE_DIR}/..")add_library(t3highlights SHARED- t3_highlights_jni.c+ src/main/cpp/cpp-adapter.cpp+ "${T3_HIGHLIGHTS_ROOT}/cpp/HybridT3Highlights.cpp"+ "${T3_HIGHLIGHTS_ROOT}/cpp/Utf16ToUtf8.cpp""${T3_HIGHLIGHTS_ROOT}/cpp/t3_highlights.c""${T3_HIGHLIGHTS_ROOT}/Vendor/highlights/highlights.c""${T3_HIGHLIGHTS_ROOT}/Vendor/wasm2c/wasm-rt-impl.c""${T3_HIGHLIGHTS_ROOT}/Vendor/wasm2c/wasm-rt-mem-impl.c")+include(${CMAKE_SOURCE_DIR}/../nitrogen/generated/android/t3highlights+autolinking.cmake)+target_include_directories(t3highlightsPRIVATE "${T3_HIGHLIGHTS_ROOT}/cpp" "${T3_HIGHLIGHTS_ROOT}/Vendor"diff --git a/apps/mobile/modules/t3-highlights/android/build.gradle b/apps/mobile/modules/t3-highlights/android/build.gradleindex 58f05125d3f..a24e1cc6730 100644--- a/apps/mobile/modules/t3-highlights/android/build.gradle+++ b/apps/mobile/modules/t3-highlights/android/build.gradle@@ -1,11 +1,12 @@apply plugin: 'com.android.library'apply plugin: 'org.jetbrains.kotlin.android'+apply from: '../nitrogen/generated/android/t3highlights+autolinking.gradle'group = 'com.t3tools.highlights'version = '0.0.0'android {- namespace 'expo.modules.t3highlights'+ namespace 'com.margelo.nitro.t3highlights'ndkVersion rootProject.ext.ndkVersioncompileSdk rootProject.ext.compileSdkVersion@@ -19,16 +20,45 @@ android {abiFilters(*requestedArchitectures.split(','))}}++ externalNativeBuild {+ cmake {+ cppFlags '-frtti -fexceptions -Wall -Wextra'+ arguments '-DANDROID_STL=c++_shared', '-DANDROID_SUPPORT_FLEXIBLE_PAGE_SIZES=ON'+ }+ }}externalNativeBuild {cmake {- path 'src/main/cpp/CMakeLists.txt'+ path 'CMakeLists.txt'version '3.22.1'}}++ buildFeatures {+ prefab true+ }++ packagingOptions {+ excludes = [+ '**/libc++_shared.so',+ '**/libfbjni.so',+ '**/libjsi.so',+ '**/libreactnative.so',+ '**/libNitroModules.so',+ ]+ }}dependencies {- implementation project(':expo-modules-core')+ implementation 'com.facebook.react:react-android'+ implementation project(':react-native-nitro-modules')+}++// CMake reads Nitro's prefab headers, which its prepareHeaders task writes.+tasks.configureEach { task ->+ if (task.name.contains('configureCMake') || task.name.contains('buildCMake')) {+ task.dependsOn(':react-native-nitro-modules:prepareHeaders')+ }}diff --git a/apps/mobile/modules/t3-highlights/android/src/main/cpp/cpp-adapter.cpp b/apps/mobile/modules/t3-highlights/android/src/main/cpp/cpp-adapter.cppnew file mode 100644index 00000000000..4901f2f8c66--- /dev/null+++ b/apps/mobile/modules/t3-highlights/android/src/main/cpp/cpp-adapter.cpp@@ -0,0 +1,8 @@+#include <fbjni/fbjni.h>+#include <jni.h>++#include "t3highlightsOnLoad.hpp"++JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void*) {+ return facebook::jni::initialize(vm, [] { margelo::nitro::t3highlights::registerAllNatives(); });+}diff --git a/apps/mobile/modules/t3-highlights/android/src/main/cpp/t3_highlights_jni.c b/apps/mobile/modules/t3-highlights/android/src/main/cpp/t3_highlights_jni.cdeleted file mode 100644index 2e45d618d22..00000000000--- a/apps/mobile/modules/t3-highlights/android/src/main/cpp/t3_highlights_jni.c+++ /dev/null@@ -1,41 +0,0 @@-#include <jni.h>-#include <string.h>--#include "t3_highlights.h"--// HighlightsNative serializes both calls, so the copy reads the records the-// tokenize call just produced.-static const uint32_t* last_records = NULL;-static int64_t last_count = 0;--JNIEXPORT jint JNICALL Java_expo_modules_t3highlights_HighlightsNative_nativeTokenize(- JNIEnv* env, jclass clazz, jbyteArray source, jint language) {- (void)clazz;- last_records = NULL;- last_count = 0;- if (language < 0 || language > UINT8_MAX) return -1;-- jsize length = (*env)->GetArrayLength(env, source);- jbyte* bytes = (*env)->GetByteArrayElements(env, source, NULL);- if (bytes == NULL) return -1;- int64_t count = t3_highlights_tokenize((const uint8_t*)bytes, (size_t)length,- (uint8_t)language, &last_records);- (*env)->ReleaseByteArrayElements(env, source, bytes, JNI_ABORT);-- if (count < 0 || count > INT32_MAX / 4) {- last_records = NULL;- return -1;- }- last_count = count;- return (jint)count;-}--JNIEXPORT void JNICALL Java_expo_modules_t3highlights_HighlightsNative_nativeCopyRecords(- JNIEnv* env, jclass clazz, jobject destination) {- (void)clazz;- void* address = (*env)->GetDirectBufferAddress(env, destination);- jlong capacity = (*env)->GetDirectBufferCapacity(env, destination);- size_t byte_count = (size_t)last_count * sizeof(uint32_t);- if (address == NULL || last_records == NULL || capacity < (jlong)byte_count) return;- memcpy(address, last_records, byte_count);-}diff --git a/apps/mobile/modules/t3-highlights/android/src/main/java/com/margelo/nitro/t3highlights/T3HighlightsPackage.kt b/apps/mobile/modules/t3-highlights/android/src/main/java/com/margelo/nitro/t3highlights/T3HighlightsPackage.ktnew file mode 100644index 00000000000..3fd2aba8678--- /dev/null+++ b/apps/mobile/modules/t3-highlights/android/src/main/java/com/margelo/nitro/t3highlights/T3HighlightsPackage.kt@@ -0,0 +1,19 @@+package com.margelo.nitro.t3highlights++import com.facebook.react.BaseReactPackage+import com.facebook.react.bridge.NativeModule+import com.facebook.react.bridge.ReactApplicationContext+import com.facebook.react.module.model.ReactModuleInfoProvider++// Autolinking instantiates this package, which loads the library that registers T3Highlights.+class T3HighlightsPackage : BaseReactPackage() {+ override fun getModule(name: String, reactContext: ReactApplicationContext): NativeModule? = null++ override fun getReactModuleInfoProvider() = ReactModuleInfoProvider { emptyMap() }++ companion object {+ init {+ t3highlightsOnLoad.initializeNative()+ }+ }+}diff --git a/apps/mobile/modules/t3-highlights/android/src/main/java/expo/modules/t3highlights/T3HighlightsModule.kt b/apps/mobile/modules/t3-highlights/android/src/main/java/expo/modules/t3highlights/T3HighlightsModule.ktdeleted file mode 100644index 3b5fcaddd0c..00000000000--- a/apps/mobile/modules/t3-highlights/android/src/main/java/expo/modules/t3highlights/T3HighlightsModule.kt+++ /dev/null@@ -1,37 +0,0 @@-package expo.modules.t3highlights--import expo.modules.kotlin.jni.ArrayBuffer-import expo.modules.kotlin.modules.Module-import expo.modules.kotlin.modules.ModuleDefinition-import java.nio.ByteBuffer-import java.nio.ByteOrder--class T3HighlightsModule : Module() {- override fun definition() = ModuleDefinition {- Name("T3Highlights")-- Function("tokenize") { source: String, language: Int ->- HighlightsNative.tokenize(source, language)- }- }-}--internal object HighlightsNative {- init {- System.loadLibrary("t3highlights")- }-- // The records live in the lexer's memory until its next run.- @Synchronized- fun tokenize(source: String, language: Int): ArrayBuffer? {- val count = nativeTokenize(source.toByteArray(Charsets.UTF_8), language)- if (count < 0) return null- val records = ByteBuffer.allocateDirect(count * Int.SIZE_BYTES).order(ByteOrder.nativeOrder())- nativeCopyRecords(records)- return ArrayBuffer(records)- }-- @JvmStatic private external fun nativeTokenize(source: ByteArray, language: Int): Int-- @JvmStatic private external fun nativeCopyRecords(destination: ByteBuffer)-}diff --git a/apps/mobile/modules/t3-highlights/cpp/HybridT3Highlights.cpp b/apps/mobile/modules/t3-highlights/cpp/HybridT3Highlights.cppnew file mode 100644index 00000000000..22097f98969--- /dev/null+++ b/apps/mobile/modules/t3-highlights/cpp/HybridT3Highlights.cpp@@ -0,0 +1,32 @@+#include "HybridT3Highlights.hpp"++#include <cstdint>+#include <mutex>++#include "t3_highlights.h"++namespace margelo::nitro::t3highlights {++namespace {+// The lexer's memory is process-global and every JS runtime shares it.+std::mutex lexerMutex;+} // namespace++std::optional<std::shared_ptr<ArrayBuffer>> HybridT3Highlights::tokenize(SourceText source,+ double language) {+ if (!(language >= 0 && language <= UINT8_MAX)) return std::nullopt;+ const auto languageId = static_cast<uint8_t>(language);+ if (languageId != language) return std::nullopt;++ std::lock_guard lock(lexerMutex);+ const uint32_t* records = nullptr;+ const int64_t count = t3_highlights_tokenize(reinterpret_cast<const uint8_t*>(source.utf8.data()),+ source.utf8.size(), languageId, &records);+ if (count < 0) return std::nullopt;+ const auto byteCount = static_cast<size_t>(count) * sizeof(uint32_t);+ if (records == nullptr || byteCount == 0) return ArrayBuffer::allocate(0);+ // The records live in the lexer's memory until its next run.+ return ArrayBuffer::copy(reinterpret_cast<const uint8_t*>(records), byteCount);+}++} // namespace margelo::nitro::t3highlightsdiff --git a/apps/mobile/modules/t3-highlights/cpp/HybridT3Highlights.hpp b/apps/mobile/modules/t3-highlights/cpp/HybridT3Highlights.hppnew file mode 100644index 00000000000..c7a6f5f8279--- /dev/null+++ b/apps/mobile/modules/t3-highlights/cpp/HybridT3Highlights.hpp@@ -0,0 +1,15 @@+#pragma once++#include "HybridT3HighlightsSpec.hpp"++namespace margelo::nitro::t3highlights {++class HybridT3Highlights : public HybridT3HighlightsSpec {+public:+ HybridT3Highlights() : HybridObject(TAG) {}++ std::optional<std::shared_ptr<ArrayBuffer>> tokenize(SourceText source,+ double language) override;+};++} // namespace margelo::nitro::t3highlightsdiff --git a/apps/mobile/modules/t3-highlights/cpp/SourceText.hpp b/apps/mobile/modules/t3-highlights/cpp/SourceText.hppnew file mode 100644index 00000000000..87f96a85176--- /dev/null+++ b/apps/mobile/modules/t3-highlights/cpp/SourceText.hpp@@ -0,0 +1,43 @@+#pragma once++#include <NitroModules/JSIConverter.hpp>+#include <string>++#include "Utf16ToUtf8.hpp"++namespace margelo::nitro::t3highlights {++/** A JS string as the UTF-8 bytes the lexer reads. */+struct SourceText {+ std::string utf8;+};++} // namespace margelo::nitro::t3highlights++namespace margelo::nitro {++// Hermes' own utf8() is several times slower than this for strings it stores as+// UTF-16, which is any string with one non-ASCII character in it.+template <>+struct JSIConverter<t3highlights::SourceText> final {+ static inline t3highlights::SourceText fromJSI(jsi::Runtime& runtime, const jsi::Value& arg) {+ t3highlights::SourceText source;+ auto read = [&source](bool ascii, const void* data, size_t length) {+ if (ascii) {+ source.utf8.append(static_cast<const char*>(data), length);+ } else {+ t3highlights::appendUtf16AsUtf8(source.utf8, static_cast<const char16_t*>(data), length);+ }+ };+ arg.asString(runtime).getStringData(runtime, read);+ return source;+ }+ static inline jsi::Value toJSI(jsi::Runtime& runtime, const t3highlights::SourceText& arg) {+ return jsi::String::createFromUtf8(runtime, arg.utf8);+ }+ static inline bool canConvert(jsi::Runtime&, const jsi::Value& value) {+ return value.isString();+ }+};++} // namespace margelo::nitrodiff --git a/apps/mobile/modules/t3-highlights/cpp/Utf16ToUtf8.cpp b/apps/mobile/modules/t3-highlights/cpp/Utf16ToUtf8.cppnew file mode 100644index 00000000000..f9e7ed54ed9--- /dev/null+++ b/apps/mobile/modules/t3-highlights/cpp/Utf16ToUtf8.cpp@@ -0,0 +1,58 @@+#include "Utf16ToUtf8.hpp"++#include <cstdint>+#include <cstring>++namespace margelo::nitro::t3highlights {++void appendUtf16AsUtf8(std::string& out, const char16_t* data, size_t length) {+ const size_t start = out.size();+ out.resize(start + length * 3);+ auto* const begin = reinterpret_cast<uint8_t*>(out.data());+ auto* write = begin + start;+ size_t at = 0;+ while (at < length) {+ // Runs of four ASCII code units are the common case in source code.+ if (at + 4 <= length) {+ uint64_t chunk;+ std::memcpy(&chunk, data + at, sizeof(chunk));+ if ((chunk & 0xff80ff80ff80ff80ULL) == 0) {+ write[0] = static_cast<uint8_t>(data[at]);+ write[1] = static_cast<uint8_t>(data[at + 1]);+ write[2] = static_cast<uint8_t>(data[at + 2]);+ write[3] = static_cast<uint8_t>(data[at + 3]);+ write += 4;+ at += 4;+ continue;+ }+ }+ uint32_t unit = data[at++];+ if (unit < 0x80) {+ *write++ = static_cast<uint8_t>(unit);+ } else if (unit < 0x800) {+ *write++ = static_cast<uint8_t>(0xc0 | (unit >> 6));+ *write++ = static_cast<uint8_t>(0x80 | (unit & 0x3f));+ } else {+ uint32_t codePoint = unit;+ if (unit >= 0xd800 && unit <= 0xdfff) {+ codePoint = 0xfffd;+ if (unit <= 0xdbff && at < length && data[at] >= 0xdc00 && data[at] <= 0xdfff) {+ codePoint = 0x10000 + ((unit - 0xd800) << 10) + (data[at++] - 0xdc00);+ }+ }+ if (codePoint < 0x10000) {+ *write++ = static_cast<uint8_t>(0xe0 | (codePoint >> 12));+ *write++ = static_cast<uint8_t>(0x80 | ((codePoint >> 6) & 0x3f));+ *write++ = static_cast<uint8_t>(0x80 | (codePoint & 0x3f));+ } else {+ *write++ = static_cast<uint8_t>(0xf0 | (codePoint >> 18));+ *write++ = static_cast<uint8_t>(0x80 | ((codePoint >> 12) & 0x3f));+ *write++ = static_cast<uint8_t>(0x80 | ((codePoint >> 6) & 0x3f));+ *write++ = static_cast<uint8_t>(0x80 | (codePoint & 0x3f));+ }+ }+ }+ out.resize(static_cast<size_t>(write - begin));+}++} // namespace margelo::nitro::t3highlightsdiff --git a/apps/mobile/modules/t3-highlights/cpp/Utf16ToUtf8.hpp b/apps/mobile/modules/t3-highlights/cpp/Utf16ToUtf8.hppnew file mode 100644index 00000000000..b79ab0b6605--- /dev/null+++ b/apps/mobile/modules/t3-highlights/cpp/Utf16ToUtf8.hpp@@ -0,0 +1,11 @@+#pragma once++#include <cstddef>+#include <string>++namespace margelo::nitro::t3highlights {++/** Lone surrogates become U+FFFD, as with `jsi::String::utf8`. */+void appendUtf16AsUtf8(std::string& out, const char16_t* data, size_t length);++} // namespace margelo::nitro::t3highlightsdiff --git a/apps/mobile/modules/t3-highlights/expo-module.config.json b/apps/mobile/modules/t3-highlights/expo-module.config.jsondeleted file mode 100644index 247e2a76a89..00000000000--- a/apps/mobile/modules/t3-highlights/expo-module.config.json+++ /dev/null@@ -1,10 +0,0 @@-{- "platforms": ["apple", "android"],- "apple": {- "modules": ["T3HighlightsModule"],- "podspecPath": "T3HighlightsNative.podspec"- },- "android": {- "modules": ["expo.modules.t3highlights.T3HighlightsModule"]- }-}diff --git a/apps/mobile/modules/t3-highlights/ios/T3HighlightsModule.swift b/apps/mobile/modules/t3-highlights/ios/T3HighlightsModule.swiftdeleted file mode 100644index 32ee8b6f062..00000000000--- a/apps/mobile/modules/t3-highlights/ios/T3HighlightsModule.swift+++ /dev/null@@ -1,29 +0,0 @@-import ExpoModulesCore--public final class T3HighlightsModule: Module {- private let lock = NSLock()-- public func definition() -> ModuleDefinition {- Name("T3Highlights")-- Function("tokenize") { (source: String, language: Int) -> ArrayBuffer? in- var source = source- return source.withUTF8 { utf8 in- self.lock.withLock { Self.tokenize(utf8, language: language) }- }- }- }-- private static func tokenize(_ utf8: UnsafeBufferPointer<UInt8>, language: Int) -> ArrayBuffer? {- guard let languageId = UInt8(exactly: language) else { return nil }- var records: UnsafePointer<UInt32>?- let count = t3_highlights_tokenize(utf8.baseAddress, utf8.count, languageId, &records)- guard count >= 0 else { return nil }- let byteCount = Int(count) * MemoryLayout<UInt32>.size- let buffer = ArrayBuffer(size: byteCount, initializeToZero: false)- if let records, byteCount > 0 {- buffer.withUnsafeMutableBytes { $0.copyMemory(from: UnsafeRawBufferPointer(start: records, count: byteCount)) }- }- return buffer- }-}diff --git a/apps/mobile/modules/t3-highlights/nitro.json b/apps/mobile/modules/t3-highlights/nitro.jsonnew file mode 100644index 00000000000..55879db6a82--- /dev/null+++ b/apps/mobile/modules/t3-highlights/nitro.json@@ -0,0 +1,20 @@+{+ "$schema": "https://nitro.margelo.com/nitro.schema.json",+ "cxxNamespace": ["t3highlights"],+ "ios": {+ "iosModuleName": "T3HighlightsNative"+ },+ "android": {+ "androidNamespace": ["t3highlights"],+ "androidCxxLibName": "t3highlights"+ },+ "autolinking": {+ "T3Highlights": {+ "all": {+ "language": "c++",+ "implementationClassName": "HybridT3Highlights"+ }+ }+ },+ "ignorePaths": ["node_modules", "Vendor"]+}diff --git a/apps/mobile/modules/t3-highlights/nitrogen/generated/.gitattributes b/apps/mobile/modules/t3-highlights/nitrogen/generated/.gitattributesnew file mode 100644index 00000000000..fb7a0d5a3f6--- /dev/null+++ b/apps/mobile/modules/t3-highlights/nitrogen/generated/.gitattributes@@ -0,0 +1 @@+** linguist-generated=truediff --git a/apps/mobile/modules/t3-highlights/nitrogen/generated/android/kotlin/com/margelo/nitro/t3highlights/t3highlightsOnLoad.kt b/apps/mobile/modules/t3-highlights/nitrogen/generated/android/kotlin/com/margelo/nitro/t3highlights/t3highlightsOnLoad.ktnew file mode 100644index 00000000000..4dad2cee444--- /dev/null+++ b/apps/mobile/modules/t3-highlights/nitrogen/generated/android/kotlin/com/margelo/nitro/t3highlights/t3highlightsOnLoad.kt@@ -0,0 +1,35 @@+///+/// t3highlightsOnLoad.kt+/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.+/// https://github.com/mrousavy/nitro+/// Copyright © Marc Rousavy @ Margelo+///++package com.margelo.nitro.t3highlights++import android.util.Log++internal class t3highlightsOnLoad {+ companion object {+ private const val TAG = "t3highlightsOnLoad"+ private var didLoad = false+ /**+ * Initializes the native part of "t3highlights".+ * This method is idempotent and can be called more than once.+ */+ @JvmStatic+ fun initializeNative() {+ if (didLoad) return+ try {+ Log.i(TAG, "Loading t3highlights C++ library...")+ System.loadLibrary("t3highlights")+ Log.i(TAG, "Successfully loaded t3highlights C++ library!")+ didLoad = true+ } catch (e: Error) {+ Log.e(TAG, "Failed to load t3highlights C++ library! Is it properly installed and linked? " ++ "Is the name correct? (see `CMakeLists.txt`, at `add_library(...)`)", e)+ throw e+ }+ }+ }+}diff --git a/apps/mobile/modules/t3-highlights/nitrogen/generated/android/t3highlights+autolinking.cmake b/apps/mobile/modules/t3-highlights/nitrogen/generated/android/t3highlights+autolinking.cmakenew file mode 100644index 00000000000..597d9f24545--- /dev/null+++ b/apps/mobile/modules/t3-highlights/nitrogen/generated/android/t3highlights+autolinking.cmake@@ -0,0 +1,81 @@+#+# t3highlights+autolinking.cmake+# This file was generated by nitrogen. DO NOT MODIFY THIS FILE.+# https://github.com/mrousavy/nitro+# Copyright © Marc Rousavy @ Margelo+#++# This is a CMake file that adds all files generated by Nitrogen+# to the current CMake project.+#+# To use it, add this to your CMakeLists.txt:+# ```cmake+# include(${CMAKE_SOURCE_DIR}/../nitrogen/generated/android/t3highlights+autolinking.cmake)+# ```++# Define a flag to check if we are building properly+add_definitions(-DBUILDING_T3HIGHLIGHTS_WITH_GENERATED_CMAKE_PROJECT)++# Enable Raw Props parsing in react-native (for Nitro Views)+add_definitions(-DRN_SERIALIZABLE_STATE)++# Add all headers that were generated by Nitrogen+include_directories(+ "../nitrogen/generated/shared/c++"+ "../nitrogen/generated/android/c++"+ "../nitrogen/generated/android/"+)++# Add all .cpp sources that were generated by Nitrogen+target_sources(+ # CMake project name (Android C++ library name)+ t3highlights PRIVATE+ # Autolinking Setup+ ../nitrogen/generated/android/t3highlightsOnLoad.cpp+ # Shared Nitrogen C++ sources+ ../nitrogen/generated/shared/c++/HybridT3HighlightsSpec.cpp+ # Android-specific Nitrogen C++ sources++)++# From node_modules/react-native/ReactAndroid/cmake-utils/folly-flags.cmake+# Used in node_modules/react-native/ReactAndroid/cmake-utils/ReactNative-application.cmake+target_compile_definitions(+ t3highlights PRIVATE+ -DFOLLY_NO_CONFIG=1+ -DFOLLY_HAVE_CLOCK_GETTIME=1+ -DFOLLY_USE_LIBCPP=1+ -DFOLLY_CFG_NO_COROUTINES=1+ -DFOLLY_MOBILE=1+ -DFOLLY_HAVE_RECVMMSG=1+ -DFOLLY_HAVE_PTHREAD=1+ # Once we target android-23 above, we can comment+ # the following line. NDK uses GNU style stderror_r() after API 23.+ -DFOLLY_HAVE_XSI_STRERROR_R=1+)++# Add all libraries required by the generated specs+find_package(fbjni REQUIRED) # <-- Used for communication between Java <-> C+++find_package(ReactAndroid REQUIRED) # <-- Used to set up React Native bindings (e.g. CallInvoker/TurboModule)+find_package(react-native-nitro-modules REQUIRED) # <-- Used to create all HybridObjects and use the Nitro core library++# Link all libraries together+target_link_libraries(+ t3highlights+ fbjni::fbjni # <-- Facebook C++ JNI helpers+ ReactAndroid::jsi # <-- RN: JSI+ react-native-nitro-modules::NitroModules # <-- NitroModules Core :)+)++# Link react-native (different prefab between RN 0.75 and RN 0.76)+if(ReactAndroid_VERSION_MINOR GREATER_EQUAL 76)+ target_link_libraries(+ t3highlights+ ReactAndroid::reactnative # <-- RN: Native Modules umbrella prefab+ )+else()+ target_link_libraries(+ t3highlights+ ReactAndroid::react_nativemodule_core # <-- RN: TurboModules Core+ )+endif()diff --git a/apps/mobile/modules/t3-highlights/nitrogen/generated/android/t3highlights+autolinking.gradle b/apps/mobile/modules/t3-highlights/nitrogen/generated/android/t3highlights+autolinking.gradlenew file mode 100644index 00000000000..f2ecc7dab4a--- /dev/null+++ b/apps/mobile/modules/t3-highlights/nitrogen/generated/android/t3highlights+autolinking.gradle@@ -0,0 +1,27 @@+///+/// t3highlights+autolinking.gradle+/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.+/// https://github.com/mrousavy/nitro+/// Copyright © Marc Rousavy @ Margelo+///++/// This is a Gradle file that adds all files generated by Nitrogen+/// to the current Gradle project.+///+/// To use it, add this to your build.gradle:+/// ```gradle+/// apply from: '../nitrogen/generated/android/t3highlights+autolinking.gradle'+/// ```++logger.warn("[NitroModules] 🔥 t3highlights is boosted by nitro!")++android {+ sourceSets {+ main {+ java.srcDirs += [+ // Nitrogen files+ "${project.projectDir}/../nitrogen/generated/android/kotlin"+ ]+ }+ }+}diff --git a/apps/mobile/modules/t3-highlights/nitrogen/generated/android/t3highlightsOnLoad.cpp b/apps/mobile/modules/t3-highlights/nitrogen/generated/android/t3highlightsOnLoad.cppnew file mode 100644index 00000000000..3d9100ff5aa--- /dev/null+++ b/apps/mobile/modules/t3-highlights/nitrogen/generated/android/t3highlightsOnLoad.cpp@@ -0,0 +1,49 @@+///+/// t3highlightsOnLoad.cpp+/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.+/// https://github.com/mrousavy/nitro+/// Copyright © Marc Rousavy @ Margelo+///++#ifndef BUILDING_T3HIGHLIGHTS_WITH_GENERATED_CMAKE_PROJECT+#error t3highlightsOnLoad.cpp is not being built with the autogenerated CMakeLists.txt project. Is a different CMakeLists.txt building this?+#endif++#include "t3highlightsOnLoad.hpp"++#include <jni.h>+#include <fbjni/fbjni.h>+#include <NitroModules/HybridObjectRegistry.hpp>++#include "HybridT3Highlights.hpp"++namespace margelo::nitro::t3highlights {++int initialize(JavaVM* vm) {+ return facebook::jni::initialize(vm, []() {+ ::margelo::nitro::t3highlights::registerAllNatives();+ });+}++++void registerAllNatives() {+ using namespace margelo::nitro;+ using namespace margelo::nitro::t3highlights;++ // Register native JNI methods+++ // Register Nitro Hybrid Objects+ HybridObjectRegistry::registerHybridObjectConstructor(+ "T3Highlights",+ []() -> std::shared_ptr<HybridObject> {+ static_assert(std::is_default_constructible_v<HybridT3Highlights>,+ "The HybridObject \"HybridT3Highlights\" is not default-constructible! "+ "Create a public constructor that takes zero arguments to be able to autolink this HybridObject.");+ return std::make_shared<HybridT3Highlights>();+ }+ );+}++} // namespace margelo::nitro::t3highlightsdiff --git a/apps/mobile/modules/t3-highlights/nitrogen/generated/android/t3highlightsOnLoad.hpp b/apps/mobile/modules/t3-highlights/nitrogen/generated/android/t3highlightsOnLoad.hppnew file mode 100644index 00000000000..640ddbd3c9c--- /dev/null+++ b/apps/mobile/modules/t3-highlights/nitrogen/generated/android/t3highlightsOnLoad.hpp@@ -0,0 +1,34 @@+///+/// t3highlightsOnLoad.hpp+/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.+/// https://github.com/mrousavy/nitro+/// Copyright © Marc Rousavy @ Margelo+///++#include <jni.h>+#include <functional>+#include <NitroModules/NitroDefines.hpp>++namespace margelo::nitro::t3highlights {++ [[deprecated("Use registerNatives() instead.")]]+ int initialize(JavaVM* vm);++ /**+ * Register the native (C++) part of t3highlights, and autolinks all Hybrid Objects.+ * Call this in your `JNI_OnLoad` function (probably inside `cpp-adapter.cpp`),+ * inside a `facebook::jni::initialize(vm, ...)` call.+ * Example:+ * ```cpp (cpp-adapter.cpp)+ * JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void*) {+ * return facebook::jni::initialize(vm, []() {+ * // register all t3highlights HybridObjects+ * margelo::nitro::t3highlights::registerNatives();+ * // any other custom registrations go here.+ * });+ * }+ * ```+ */+ void registerAllNatives();++} // namespace margelo::nitro::t3highlightsdiff --git a/apps/mobile/modules/t3-highlights/nitrogen/generated/ios/T3HighlightsNative+autolinking.rb b/apps/mobile/modules/t3-highlights/nitrogen/generated/ios/T3HighlightsNative+autolinking.rbnew file mode 100644index 00000000000..353434bdf8f--- /dev/null+++ b/apps/mobile/modules/t3-highlights/nitrogen/generated/ios/T3HighlightsNative+autolinking.rb@@ -0,0 +1,62 @@+#+# T3HighlightsNative+autolinking.rb+# This file was generated by nitrogen. DO NOT MODIFY THIS FILE.+# https://github.com/mrousavy/nitro+# Copyright © Marc Rousavy @ Margelo+#++# This is a Ruby script that adds all files generated by Nitrogen+# to the given podspec.+#+# To use it, add this to your .podspec:+# ```ruby+# Pod::Spec.new do |spec|+# # ...+#+# # Add all files generated by Nitrogen+# load 'nitrogen/generated/ios/T3HighlightsNative+autolinking.rb'+# add_nitrogen_files(spec)+# end+# ```++def add_nitrogen_files(spec)+ Pod::UI.puts "[NitroModules] 🔥 T3HighlightsNative is boosted by nitro!"++ spec.dependency "NitroModules"++ current_source_files = Array(spec.attributes_hash['source_files'])+ spec.source_files = current_source_files + [+ # Generated cross-platform specs+ "nitrogen/generated/shared/**/*.{h,hpp,c,cpp,swift}",+ # Generated bridges for the cross-platform specs+ "nitrogen/generated/ios/**/*.{h,hpp,c,cpp,mm,swift}",+ ]++ current_public_header_files = Array(spec.attributes_hash['public_header_files'])+ spec.public_header_files = current_public_header_files + [+ # Generated specs+ "nitrogen/generated/shared/**/*.{h,hpp}",+ # Swift to C++ bridging helpers+ "nitrogen/generated/ios/T3HighlightsNative-Swift-Cxx-Bridge.hpp"+ ]++ current_private_header_files = Array(spec.attributes_hash['private_header_files'])+ spec.private_header_files = current_private_header_files + [+ # iOS specific specs+ "nitrogen/generated/ios/c++/**/*.{h,hpp}",+ # Views are framework-specific and should be private+ "nitrogen/generated/shared/**/views/**/*"+ ]++ current_pod_target_xcconfig = spec.attributes_hash['pod_target_xcconfig'] || {}+ spec.pod_target_xcconfig = current_pod_target_xcconfig.merge({+ # Use C++ 20+ "CLANG_CXX_LANGUAGE_STANDARD" => "c++20",+ # Enables C++ <-> Swift interop (by default it's only ObjC)+ "SWIFT_OBJC_INTEROP_MODE" => "objcxx",+ # Enables stricter modular headers+ "DEFINES_MODULE" => "YES",+ # Disable auto-generated ObjC header for Swift (Static linkage on Xcode 26.4 breaks here)+ "SWIFT_INSTALL_OBJC_HEADER" => "NO",+ })+enddiff --git a/apps/mobile/modules/t3-highlights/nitrogen/generated/ios/T3HighlightsNative-Swift-Cxx-Bridge.cpp b/apps/mobile/modules/t3-highlights/nitrogen/generated/ios/T3HighlightsNative-Swift-Cxx-Bridge.cppnew file mode 100644index 00000000000..0777bcf6181--- /dev/null+++ b/apps/mobile/modules/t3-highlights/nitrogen/generated/ios/T3HighlightsNative-Swift-Cxx-Bridge.cpp@@ -0,0 +1,17 @@+///+/// T3HighlightsNative-Swift-Cxx-Bridge.cpp+/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.+/// https://github.com/mrousavy/nitro+/// Copyright © Marc Rousavy @ Margelo+///++#include "T3HighlightsNative-Swift-Cxx-Bridge.hpp"++// Include C++ implementation defined types+++namespace margelo::nitro::t3highlights::bridge::swift {++++} // namespace margelo::nitro::t3highlights::bridge::swiftdiff --git a/apps/mobile/modules/t3-highlights/nitrogen/generated/ios/T3HighlightsNative-Swift-Cxx-Bridge.hpp b/apps/mobile/modules/t3-highlights/nitrogen/generated/ios/T3HighlightsNative-Swift-Cxx-Bridge.hppnew file mode 100644index 00000000000..ab5df2d7c2a--- /dev/null+++ b/apps/mobile/modules/t3-highlights/nitrogen/generated/ios/T3HighlightsNative-Swift-Cxx-Bridge.hpp@@ -0,0 +1,27 @@+///+/// T3HighlightsNative-Swift-Cxx-Bridge.hpp+/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.+/// https://github.com/mrousavy/nitro+/// Copyright © Marc Rousavy @ Margelo+///++#pragma once++// Forward declarations of C++ defined types+++// Forward declarations of Swift defined types+++// Include C++ defined types+++/**+ * Contains specialized versions of C++ templated types so they can be accessed from Swift,+ * as well as helper functions to interact with those C++ types from Swift.+ */+namespace margelo::nitro::t3highlights::bridge::swift {++++} // namespace margelo::nitro::t3highlights::bridge::swiftdiff --git a/apps/mobile/modules/t3-highlights/nitrogen/generated/ios/T3HighlightsNative-Swift-Cxx-Umbrella.hpp b/apps/mobile/modules/t3-highlights/nitrogen/generated/ios/T3HighlightsNative-Swift-Cxx-Umbrella.hppnew file mode 100644index 00000000000..f6c94700f08--- /dev/null+++ b/apps/mobile/modules/t3-highlights/nitrogen/generated/ios/T3HighlightsNative-Swift-Cxx-Umbrella.hpp@@ -0,0 +1,38 @@+///+/// T3HighlightsNative-Swift-Cxx-Umbrella.hpp+/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.+/// https://github.com/mrousavy/nitro+/// Copyright © Marc Rousavy @ Margelo+///++#pragma once++// Forward declarations of C++ defined types+++// Include C++ defined types+++// C++ helpers for Swift+#include "T3HighlightsNative-Swift-Cxx-Bridge.hpp"++// Common C++ types used in Swift+#include <NitroModules/ArrayBufferHolder.hpp>+#include <NitroModules/AnyMapUtils.hpp>+#include <NitroModules/RuntimeError.hpp>+#include <NitroModules/DateToChronoDate.hpp>++// Forward declarations of Swift defined types+++// Include Swift defined types+#if __has_include("T3HighlightsNative-Swift.h")+// This header is generated by Xcode/Swift on every app build.+// If it cannot be found, make sure the Swift module's name (= podspec name) is actually "T3HighlightsNative".+#include "T3HighlightsNative-Swift.h"+// Same as above, but used when building with frameworks (`use_frameworks`)+#elif __has_include(<T3HighlightsNative/T3HighlightsNative-Swift.h>)+#include <T3HighlightsNative/T3HighlightsNative-Swift.h>+#else+#error T3HighlightsNative's autogenerated Swift header cannot be found! Make sure the Swift module's name (= podspec name) is actually "T3HighlightsNative", and try building the app first.+#endifdiff --git a/apps/mobile/modules/t3-highlights/nitrogen/generated/ios/T3HighlightsNativeAutolinking.mm b/apps/mobile/modules/t3-highlights/nitrogen/generated/ios/T3HighlightsNativeAutolinking.mmnew file mode 100644index 00000000000..aeb1e552b04--- /dev/null+++ b/apps/mobile/modules/t3-highlights/nitrogen/generated/ios/T3HighlightsNativeAutolinking.mm@@ -0,0 +1,35 @@+///+/// T3HighlightsNativeAutolinking.mm+/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.+/// https://github.com/mrousavy/nitro+/// Copyright © Marc Rousavy @ Margelo+///++#import <Foundation/Foundation.h>+#import <NitroModules/HybridObjectRegistry.hpp>++#import <type_traits>++#include "HybridT3Highlights.hpp"++@interface T3HighlightsNativeAutolinking : NSObject+@end++@implementation T3HighlightsNativeAutolinking+++ (void) load {+ using namespace margelo::nitro;+ using namespace margelo::nitro::t3highlights;++ HybridObjectRegistry::registerHybridObjectConstructor(+ "T3Highlights",+ []() -> std::shared_ptr<HybridObject> {+ static_assert(std::is_default_constructible_v<HybridT3Highlights>,+ "The HybridObject \"HybridT3Highlights\" is not default-constructible! "+ "Create a public constructor that takes zero arguments to be able to autolink this HybridObject.");+ return std::make_shared<HybridT3Highlights>();+ }+ );+}++@enddiff --git a/apps/mobile/modules/t3-highlights/nitrogen/generated/ios/T3HighlightsNativeAutolinking.swift b/apps/mobile/modules/t3-highlights/nitrogen/generated/ios/T3HighlightsNativeAutolinking.swiftnew file mode 100644index 00000000000..f5079023caf--- /dev/null+++ b/apps/mobile/modules/t3-highlights/nitrogen/generated/ios/T3HighlightsNativeAutolinking.swift@@ -0,0 +1,16 @@+///+/// T3HighlightsNativeAutolinking.swift+/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.+/// https://github.com/mrousavy/nitro+/// Copyright © Marc Rousavy @ Margelo+///++import NitroModules++// TODO: Use empty enums once Swift supports exporting them as namespaces+// See: https://github.com/swiftlang/swift/pull/83616+public final class T3HighlightsNativeAutolinking {+ public typealias bridge = margelo.nitro.t3highlights.bridge.swift+++}diff --git a/apps/mobile/modules/t3-highlights/nitrogen/generated/shared/c++/HybridT3HighlightsSpec.cpp b/apps/mobile/modules/t3-highlights/nitrogen/generated/shared/c++/HybridT3HighlightsSpec.cppnew file mode 100644index 00000000000..feae1896c7b--- /dev/null+++ b/apps/mobile/modules/t3-highlights/nitrogen/generated/shared/c++/HybridT3HighlightsSpec.cpp@@ -0,0 +1,21 @@+///+/// HybridT3HighlightsSpec.cpp+/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.+/// https://github.com/mrousavy/nitro+/// Copyright © Marc Rousavy @ Margelo+///++#include "HybridT3HighlightsSpec.hpp"++namespace margelo::nitro::t3highlights {++ void HybridT3HighlightsSpec::loadHybridMethods() {+ // load base methods/properties+ HybridObject::loadHybridMethods();+ // load custom methods/properties+ registerHybrids(this, [](Prototype& prototype) {+ prototype.registerHybridMethod("tokenize", &HybridT3HighlightsSpec::tokenize);+ });+ }++} // namespace margelo::nitro::t3highlightsdiff --git a/apps/mobile/modules/t3-highlights/nitrogen/generated/shared/c++/HybridT3HighlightsSpec.hpp b/apps/mobile/modules/t3-highlights/nitrogen/generated/shared/c++/HybridT3HighlightsSpec.hppnew file mode 100644index 00000000000..07158b9fd12--- /dev/null+++ b/apps/mobile/modules/t3-highlights/nitrogen/generated/shared/c++/HybridT3HighlightsSpec.hpp@@ -0,0 +1,64 @@+///+/// HybridT3HighlightsSpec.hpp+/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.+/// https://github.com/mrousavy/nitro+/// Copyright © Marc Rousavy @ Margelo+///++#pragma once++#if __has_include(<NitroModules/HybridObject.hpp>)+#include <NitroModules/HybridObject.hpp>+#else+#error NitroModules cannot be found! Are you sure you installed NitroModules properly?+#endif++++#include <NitroModules/ArrayBuffer.hpp>+#include <optional>+#include "SourceText.hpp"++namespace margelo::nitro::t3highlights {++ using namespace margelo::nitro;++ /**+ * An abstract base class for `T3Highlights`+ * Inherit this class to create instances of `HybridT3HighlightsSpec` in C++.+ * You must explicitly call `HybridObject`'s constructor yourself, because it is virtual.+ * @example+ * ```cpp+ * class HybridT3Highlights: public HybridT3HighlightsSpec {+ * public:+ * HybridT3Highlights(...): HybridObject(TAG) { ... }+ * // ...+ * };+ * ```+ */+ class HybridT3HighlightsSpec: public virtual HybridObject {+ public:+ // Constructor+ explicit HybridT3HighlightsSpec(): HybridObject(TAG) { }++ // Destructor+ ~HybridT3HighlightsSpec() override = default;++ public:+ // Properties+++ public:+ // Methods+ virtual std::optional<std::shared_ptr<ArrayBuffer>> tokenize(SourceText source, double language) = 0;++ protected:+ // Hybrid Setup+ void loadHybridMethods() override;++ protected:+ // Tag for logging+ static constexpr auto TAG = "T3Highlights";+ };++} // namespace margelo::nitro::t3highlightsdiff --git a/apps/mobile/modules/t3-highlights/package.json b/apps/mobile/modules/t3-highlights/package.jsonindex 81ebd561155..88fec302b74 100644--- a/apps/mobile/modules/t3-highlights/package.json+++ b/apps/mobile/modules/t3-highlights/package.json@@ -2,16 +2,8 @@"name": "@t3tools/mobile-highlights-native","version": "0.0.0","private": true,- "expo-module": {- "ios": {- "modules": [- "T3HighlightsModule"- ]- },- "android": {- "modules": [- "expo.modules.t3highlights.T3HighlightsModule"- ]- }+ "types": "./src/T3Highlights.nitro.ts",+ "peerDependencies": {+ "react-native-nitro-modules": "*"}}diff --git a/apps/mobile/modules/t3-highlights/src/T3Highlights.nitro.ts b/apps/mobile/modules/t3-highlights/src/T3Highlights.nitro.tsnew file mode 100644index 00000000000..4bb305e029b--- /dev/null+++ b/apps/mobile/modules/t3-highlights/src/T3Highlights.nitro.ts@@ -0,0 +1,13 @@+import type { CustomType, HybridObject } from "react-native-nitro-modules";++// Converted by cpp/SourceText.hpp, which reads the string without Hermes' slow UTF-16 to UTF-8 path.+type SourceText = CustomType<string, "SourceText", { include: "SourceText.hpp" }>;++export interface T3Highlights extends HybridObject<{ ios: "c++"; android: "c++" }> {+ /**+ * Lexes `source` with @pierre/highlights language `language`. Returns+ * `(endUtf16, tokenId)` uint32 pairs, where token ID 0xffffffff ends a line,+ * or undefined when the lexer could not run.+ */+ tokenize(source: SourceText, language: number): ArrayBuffer | undefined;+}diff --git a/apps/mobile/modules/t3-highlights/tests/run.sh b/apps/mobile/modules/t3-highlights/tests/run.shnew file mode 100755index 00000000000..0be43fe22a2--- /dev/null+++ b/apps/mobile/modules/t3-highlights/tests/run.sh@@ -0,0 +1,8 @@+#!/bin/bash+set -euo pipefail+module_dir=$(cd "$(dirname "$0")/.." && pwd)+output=$(mktemp -d)+trap 'rm -rf "$output"' EXIT+c++ -std=c++20 -O2 -Wall -Wextra -I "$module_dir/cpp" \+ "$module_dir/cpp/Utf16ToUtf8.cpp" "$module_dir/tests/utf16-to-utf8.cpp" -o "$output/utf16-to-utf8"+"$output/utf16-to-utf8"diff --git a/apps/mobile/modules/t3-highlights/tests/utf16-to-utf8.cpp b/apps/mobile/modules/t3-highlights/tests/utf16-to-utf8.cppnew file mode 100644index 00000000000..98fdba8db84--- /dev/null+++ b/apps/mobile/modules/t3-highlights/tests/utf16-to-utf8.cpp@@ -0,0 +1,94 @@+#include <cstdint>+#include <cstdio>+#include <random>+#include <string>++#include "Utf16ToUtf8.hpp"++using margelo::nitro::t3highlights::appendUtf16AsUtf8;++static int failures = 0;++static std::string encode(const std::u16string& source, std::string out = {}) {+ appendUtf16AsUtf8(out, source.data(), source.size());+ return out;+}++static void expectBytes(const char* name, const std::string& actual, const std::string& expected) {+ if (actual == expected) return;+ failures++;+ std::fprintf(stderr, "%s: expected", name);+ for (unsigned char byte : expected) std::fprintf(stderr, " %02x", byte);+ std::fprintf(stderr, ", got");+ for (unsigned char byte : actual) std::fprintf(stderr, " %02x", byte);+ std::fprintf(stderr, "\n");+}++// One code unit or valid pair at a time, as a slow reference.+static std::string reference(const std::u16string& source) {+ std::string out;+ for (size_t at = 0; at < source.size(); at++) {+ uint32_t point = source[at];+ if (point >= 0xd800 && point <= 0xdbff && at + 1 < source.size() && source[at + 1] >= 0xdc00 &&+ source[at + 1] <= 0xdfff) {+ point = 0x10000 + ((point - 0xd800) << 10) + (source[++at] - 0xdc00);+ } else if (point >= 0xd800 && point <= 0xdfff) {+ point = 0xfffd;+ }+ if (point < 0x80) {+ out += static_cast<char>(point);+ } else if (point < 0x800) {+ out += static_cast<char>(0xc0 | (point >> 6));+ out += static_cast<char>(0x80 | (point & 0x3f));+ } else if (point < 0x10000) {+ out += static_cast<char>(0xe0 | (point >> 12));+ out += static_cast<char>(0x80 | ((point >> 6) & 0x3f));+ out += static_cast<char>(0x80 | (point & 0x3f));+ } else {+ out += static_cast<char>(0xf0 | (point >> 18));+ out += static_cast<char>(0x80 | ((point >> 12) & 0x3f));+ out += static_cast<char>(0x80 | ((point >> 6) & 0x3f));+ out += static_cast<char>(0x80 | (point & 0x3f));+ }+ }+ return out;+}++int main() {+ expectBytes("empty", encode(u""), "");+ expectBytes("nul", encode(std::u16string(u"a\0b", 3)), std::string("a\0b", 3));+ expectBytes("two-byte", encode(u"\u00e9\u07ff"), "\xc3\xa9\xdf\xbf");+ expectBytes("three-byte", encode(u"\u0800\u65e5\uffff"), "\xe0\xa0\x80\xe6\x97\xa5\xef\xbf\xbf");+ expectBytes("pair", encode(u"\U0001F389"), "\xf0\x9f\x8e\x89");+ expectBytes("lone high at end", encode(u"a\xd83c"), "a\xef\xbf\xbd");+ expectBytes("lone high before text", encode(u"\xd83cz"), "\xef\xbf\xbdz");+ expectBytes("high high low", encode(u"\xd83c\xd83c\xdf89"), "\xef\xbf\xbd\xf0\x9f\x8e\x89");+ expectBytes("lone low", encode(u"\xdf89"), "\xef\xbf\xbd");+ expectBytes("appends", encode(u"\u00e9", "ok "), "ok \xc3\xa9");+ // A high surrogate at the end of the input must not pair with the unit after it.+ const std::u16string pair = u"\U0001F389";+ std::string split;+ appendUtf16AsUtf8(split, pair.data(), 1);+ expectBytes("high at input end", split, "\xef\xbf\xbd");++ // Runs either side of the four-unit ASCII fast path, with a non-ASCII unit at every offset.+ for (size_t length = 0; length <= 12; length++) {+ for (size_t at = 0; at <= length; at++) {+ std::u16string source(length, u'x');+ if (at < length) source[at] = 0x00e9;+ expectBytes("fast path boundary", encode(source), reference(source));+ }+ }++ const char16_t pool[] = {u'a', u'\n', 0x7f, 0x80, 0x7ff, 0x800, 0xd7ff, 0xd800, 0xdbff, 0xdc00, 0xdfff, 0xe000, 0xffff};+ std::mt19937 random(7);+ for (int run = 0; run < 20000; run++) {+ std::u16string source(random() % 24, u' ');+ for (auto& unit : source) unit = pool[random() % (sizeof(pool) / sizeof(pool[0]))];+ expectBytes("random", encode(source), reference(source));+ }++ if (failures > 0) return 1;+ std::printf("utf16-to-utf8: ok\n");+ return 0;+}diff --git a/apps/mobile/modules/t3-highlights/tests/utf16-to-utf8.test.ts b/apps/mobile/modules/t3-highlights/tests/utf16-to-utf8.test.tsnew file mode 100644index 00000000000..d7a18effe6b--- /dev/null+++ b/apps/mobile/modules/t3-highlights/tests/utf16-to-utf8.test.ts@@ -0,0 +1,14 @@+import * as NodeChildProcess from "node:child_process";+import * as NodeURL from "node:url";+import { it } from "vite-plus/test";++// The converter is C++, so its test compiles and runs on the host.+it("transcodes UTF-16 the way jsi::String::utf8 does", () => {+ NodeChildProcess.execFileSync(+ "bash",+ [NodeURL.fileURLToPath(new URL("./run.sh", import.meta.url))],+ {+ stdio: "pipe",+ },+ );+});diff --git a/apps/mobile/package.json b/apps/mobile/package.jsonindex a58e0e50360..e93c0733fa0 100644--- a/apps/mobile/package.json+++ b/apps/mobile/package.json@@ -46,6 +46,7 @@"measure-renders": "node scripts/measure-renders.mts","generate": "node --disable-warning=MODULE_TYPELESS_PACKAGE_JSON scripts/generate-uniwind-themes.mts","generate:highlights": "node scripts/generate-highlights.mts",+ "generate:nitro": "cd modules/t3-highlights && nitrogen","eas-build-on-success": "node scripts/upload-sourcemaps.mts build","test": "vp test run --project unit --project react && T3_MOBILE_TEST_PLATFORM=android vp test run --project react src/components/FilePreviewModal.test.tsx","typecheck": "effect-tsgo tsrs --noEmit"@@ -150,6 +151,7 @@"aws4fetch": "1.0.20","babel-preset-expo": "~58.0.17","jsdom": "^30.0.1",+ "nitrogen": "0.37.1","tailwindcss": "^4.0.0"},"enriched-markdown": {diff --git a/apps/mobile/src/features/diffs/nativeReviewDiffHighlighter.test.ts b/apps/mobile/src/features/diffs/nativeReviewDiffHighlighter.test.tsindex 36773f8bdc4..5cf79deae29 100644--- a/apps/mobile/src/features/diffs/nativeReviewDiffHighlighter.test.ts+++ b/apps/mobile/src/features/diffs/nativeReviewDiffHighlighter.test.ts@@ -13,18 +13,21 @@ const tokenization = vi.hoisted(() => ({afterCall: undefined as (() => void) | undefined,}));-vi.mock("expo", async () => {+vi.mock("react-native-nitro-modules", async () => {const { createWasmHighlightsModule } = await import("../../lib/highlights.testing");const module = createWasmHighlightsModule();return {- requireOptionalNativeModule: () => ({- tokenize(code: string, language: number) {- tokenization.calls.push(code);- const records = module.tokenize(code, language);- tokenization.afterCall?.();- return records;- },- }),+ NitroModules: {+ hasHybridObject: () => true,+ createHybridObject: () => ({+ tokenize(code: string, language: number) {+ tokenization.calls.push(code);+ const records = module.tokenize(code, language);+ tokenization.afterCall?.();+ return records;+ },+ }),+ },};});diff --git a/apps/mobile/src/features/pull-requests/pullRequestDiffHighlightFiles.test.ts b/apps/mobile/src/features/pull-requests/pullRequestDiffHighlightFiles.test.tsindex b48933573f3..6bd6310358c 100644--- a/apps/mobile/src/features/pull-requests/pullRequestDiffHighlightFiles.test.ts+++ b/apps/mobile/src/features/pull-requests/pullRequestDiffHighlightFiles.test.ts@@ -14,9 +14,10 @@ import {createPullRequestDiffHighlightFiles,} from "./pullRequestDiffHighlightFiles";-vi.mock("expo", async () => {+vi.mock("react-native-nitro-modules", async () => {const { createWasmHighlightsModule } = await import("../../lib/highlights.testing");- return { requireOptionalNativeModule: () => createWasmHighlightsModule() };+ const highlights = createWasmHighlightsModule();+ return { NitroModules: { hasHybridObject: () => true, createHybridObject: () => highlights } };});const target = {diff --git a/apps/mobile/src/lib/highlights.test.ts b/apps/mobile/src/lib/highlights.test.tsindex 0867b293846..58604e8b8f8 100644--- a/apps/mobile/src/lib/highlights.test.ts+++ b/apps/mobile/src/lib/highlights.test.ts@@ -5,10 +5,10 @@ import { describe, expect, it, vi } from "vite-plus/test";import { resolveHighlightLanguage } from "./highlightLanguage";import { highlightCode, tokenizeCode } from "./highlights";-vi.mock("expo", async () => {+vi.mock("react-native-nitro-modules", async () => {const { createWasmHighlightsModule } = await import("./highlights.testing");const highlights = createWasmHighlightsModule();- return { requireOptionalNativeModule: () => highlights };+ return { NitroModules: { hasHybridObject: () => true, createHybridObject: () => highlights } };});describe("highlightCode", () => {diff --git a/apps/mobile/src/lib/highlights.testing.ts b/apps/mobile/src/lib/highlights.testing.tsindex ec1582c9953..a8fb1856e9f 100644--- a/apps/mobile/src/lib/highlights.testing.ts+++ b/apps/mobile/src/lib/highlights.testing.ts@@ -7,8 +7,8 @@ interface LineRecordHighlighter {}/**- * Stands in for the T3Highlights native module with the WebAssembly build it is- * compiled from. Mock `expo`'s `requireOptionalNativeModule` with this.+ * Stands in for the T3Highlights hybrid object with the WebAssembly build it is+ * compiled from. Return it from a mocked `NitroModules.createHybridObject`.*/export function createWasmHighlightsModule() {const highlighter = createHighlighter() as unknown as LineRecordHighlighter;diff --git a/apps/mobile/src/lib/highlights.ts b/apps/mobile/src/lib/highlights.tsindex 184f203ca87..2fe982ef3fc 100644--- a/apps/mobile/src/lib/highlights.ts+++ b/apps/mobile/src/lib/highlights.ts@@ -1,4 +1,5 @@-import { requireOptionalNativeModule } from "expo";+import type { T3Highlights } from "@t3tools/mobile-highlights-native";+import { NitroModules } from "react-native-nitro-modules";import { highlightLanguageId } from "./highlightLanguage";import { HIGHLIGHTS_THEME_STYLES } from "./highlights.generated";@@ -20,9 +21,9 @@ const MAX_HIGHLIGHTED_LINE_LENGTH = 1_000;// @pierre/highlights compiled to native code (modules/t3-highlights). Missing// from binaries built before it shipped, where code renders as plain text.-const nativeHighlights = requireOptionalNativeModule<{- tokenize(source: string, language: number): ArrayBuffer | null;-}>("T3Highlights");+const nativeHighlights = NitroModules.hasHybridObject("T3Highlights")+ ? NitroModules.createHybridObject<T3Highlights>("T3Highlights")+ : undefined;export interface TokenizedCode {/** Retained UTF-16 source, compact native records, and line indexes. */diff --git a/knip.jsonc b/knip.jsoncindex 5b2dbf63a46..ed4bf191296 100644--- a/knip.jsonc+++ b/knip.jsonc@@ -119,11 +119,14 @@// Fonts are configured as asset paths; Expo autolinks the native packages."ignoreDependencies": ["@expo-google-fonts/dm-sans",- "@t3tools/mobile-highlights-native","@t3tools/mobile-review-diff-native","@t3tools/mobile-terminal-native",],},+ "apps/mobile/modules/t3-highlights": {+ // Nitrogen discovers HybridObject specifications by filename.+ "entry": ["src/*.nitro.ts!"],+ },"apps/mobile/modules/t3-source-text": {// React Native codegen discovers component specifications by filename."entry": ["src/*NativeComponent.ts!"],diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex a5188fdee03..cba3664e0b4 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -583,7 +583,7 @@ importers:version: link:../../packages/contracts'@t3tools/mobile-highlights-native':specifier: file:./modules/t3-highlights- version: file:apps/mobile/modules/t3-highlights+ version: file:apps/mobile/modules/t3-highlights([email protected]([email protected](@babel/[email protected]([email protected]))(@react-native/[email protected](@babel/[email protected]([email protected]))([email protected])([email protected])([email protected]))(@types/[email protected])([email protected])([email protected])([email protected])([email protected]))([email protected]))'@t3tools/mobile-review-diff-native':specifier: file:./modules/t3-review-diffversion: file:apps/mobile/modules/t3-review-diff@@ -816,6 +816,9 @@ importers:jsdom:specifier: ^30.0.1version: 30.1.0(@noble/[email protected])+ nitrogen:+ specifier: 0.37.1+ version: 0.37.1([email protected](@babel/[email protected]([email protected]))(@react-native/[email protected](@babel/[email protected]([email protected]))([email protected])([email protected])([email protected]))(@types/[email protected])([email protected])([email protected])([email protected])([email protected]))([email protected])tailwindcss:specifier: 4.3.3version: 4.3.3@@ -6520,6 +6523,8 @@ packages:'@t3tools/mobile-highlights-native@file:apps/mobile/modules/t3-highlights':resolution: {directory: apps/mobile/modules/t3-highlights, type: directory}+ peerDependencies:+ react-native-nitro-modules: '*''@t3tools/mobile-review-diff-native@file:apps/mobile/modules/t3-review-diff':resolution: {directory: apps/mobile/modules/t3-review-diff, type: directory}@@ -6918,6 +6923,9 @@ packages:cpu: [x64]os: [win32]+ '@ts-morph/[email protected]':+ resolution: {integrity: sha512-35oUmphHbJvQ/+UTwFNme/t2p3FoKiGJ5auTjjpNTop2dyREspirjMy82PLSC1pnDJ8ah1GU98hwpVt64YXQsg==}+'@tweenjs/[email protected]':resolution: {integrity: sha512-vJmvvwFxYuGnF2axRtPYocag6Clbb5YS7kLL+SO/TeVFzHqDIWrNKYtcsPMibjDx9O+bu+psAy9NKfWklassUA==}@@ -7999,6 +8007,9 @@ packages:engines: {node: '>=20'}hasBin: true+ resolution: {integrity: sha512-Oofo0pq3IKnsFtuHqSF7TqBfr71aeyZDVJ0HpmqB7FBM2qEigL0iPONSCZSO9pE9dZTAxANe5XHG9Uy0YMv8cg==}+resolution: {integrity: sha512-QfAUtd+vFdAtFQcC8CCyYt1fYWxSqAiK2cSD6zDB8N3cpsEBAvRxp9zOGg6G/SHHJYAT88/az/IuDGALsNVbGg==}@@ -10299,6 +10310,10 @@ packages:resolution: {integrity: sha512-WmmLty1YWwJl9yZi77v2dVIV6X2kuYV8YYBI/G3LWGKdGHmHUvL1z7FW0iDvEvGAwNEoc5x1tOOOyDnf5jJw/w==}engines: {node: '>= 10'}+ resolution: {integrity: sha512-RMK1n8xV7B38JorfLCFexgPEDp7/4CnM/AR0R3UfikufXo8qB8TecgtY+sW0xjOjZJYwW5bJJI/kyZjusYk4JA==}+ hasBin: true+resolution: {integrity: sha512-YKLBCcUYKAg0FNlOBT6aI91qFmSiFKiluk655WzPF+DDMA02qIyy8uiRqI8QXtcFpEvll12LpL5MXqEmAZ+dcA==}@@ -11741,6 +11756,9 @@ packages:resolution: {integrity: sha512-FPAhNPFMrkwz76P7cdjdmiShwMynZYN6SgOujD1urY4oNm80Ou9oMdmbR45LotcKOXoy7wSmHkRFE6Mxbrhefw==}+ resolution: {integrity: sha512-Wp3tnZ2bzwxyTZMtgWVzXDfm7lB1Drz+y9DmmYH/L702PQhPyVrp3pkou3yIz4qjS14GY9kcpmLiOOMvl8oG1g==}+resolution: {integrity: sha512-oJFu94HQb+KVduSUQL7wnpmqnfmLsOA/nAh6b6EH0wCEoK0/mPeXU6c3wKDV83MkOuHPRHtSXKKU99IBazS/2w==}@@ -16081,7 +16099,9 @@ snapshots:dependencies:defer-to-connect: 2.0.1- '@t3tools/mobile-highlights-native@file:apps/mobile/modules/t3-highlights': {}+ '@t3tools/mobile-highlights-native@file:apps/mobile/modules/t3-highlights([email protected]([email protected](@babel/[email protected]([email protected]))(@react-native/[email protected](@babel/[email protected]([email protected]))([email protected])([email protected])([email protected]))(@types/[email protected])([email protected])([email protected])([email protected])([email protected]))([email protected]))':+ dependencies:+ react-native-nitro-modules: 0.37.1([email protected](@babel/[email protected]([email protected]))(@react-native/[email protected](@babel/[email protected]([email protected]))([email protected])([email protected])([email protected]))(@types/[email protected])([email protected])([email protected])([email protected])([email protected]))([email protected])'@t3tools/mobile-review-diff-native@file:apps/mobile/modules/t3-review-diff': {}@@ -16481,6 +16501,12 @@ snapshots:'@transcribe-cpp/[email protected]':optional: true+ '@ts-morph/[email protected]':+ dependencies:+ minimatch: 10.2.5+ path-browserify: 1.0.1+ tinyglobby: 0.2.17+'@tweenjs/[email protected]': {}'@tybys/[email protected]':@@ -17724,6 +17750,8 @@ snapshots:+ [email protected]: {}+dependencies:color-name: 1.1.3@@ -20393,6 +20421,17 @@ snapshots:+ [email protected]([email protected](@babel/[email protected]([email protected]))(@react-native/[email protected](@babel/[email protected]([email protected]))([email protected])([email protected])([email protected]))(@types/[email protected])([email protected])([email protected])([email protected])([email protected]))([email protected]):+ dependencies:+ chalk: 5.6.2+ react-native-nitro-modules: 0.37.1([email protected](@babel/[email protected]([email protected]))(@react-native/[email protected](@babel/[email protected]([email protected]))([email protected])([email protected])([email protected]))(@types/[email protected])([email protected])([email protected])([email protected])([email protected]))([email protected])+ ts-morph: 28.0.0+ yargs: 18.1.0+ zod: 4.6.5+ transitivePeerDependencies:+ - react+ - react-native+dependencies:'@types/nlcst': 2.0.3@@ -22211,6 +22250,11 @@ snapshots:+ dependencies:+ '@ts-morph/common': 0.29.0+ code-block-writer: 13.0.3+diff --git a/scripts/mobile-native-static-check.ts b/scripts/mobile-native-static-check.tsindex 489432f0d50..6d311856055 100644--- a/scripts/mobile-native-static-check.ts+++ b/scripts/mobile-native-static-check.ts@@ -99,6 +99,7 @@ const excludedDirectories = new Set([".git","build","DerivedData",+ "nitrogen","node_modules","Pods","Vendor",diff --git a/vite.config.ts b/vite.config.tsindex d5ee6c9d0df..70c6f16f16e 100644--- a/vite.config.ts+++ b/vite.config.ts@@ -177,6 +177,7 @@ export default defineConfig({// Vendored and generated C."apps/mobile/modules/t3-highlights/Vendor/**","apps/mobile/modules/t3-highlights/cpp/generated/**",+ "apps/mobile/modules/t3-highlights/nitrogen/generated/**","*.icon/**",],sortPackageJson: {},