The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 22, 03:15 UTC
perf(mobile): call the highlights lexer through Nitro, up to 10x faster calls on Android (#31188)
Cameron Chisholm committed
Nov 6, 2026 at 00:20 UTC
6b53b3dc31179966f3f4c4272ffc93e1b281c4af
46 files changed
+980
-165
.macroscope/ignore.md
+4
@@ -279,3 +279,7 @@ native/libghostty-vt/include/**
279
# it needs. Regenerated by apps/mobile/scripts/generate-highlights.mts; review
280
# that script and cpp/t3_highlights.c instead.
281
apps/mobile/modules/t3-highlights/Vendor/**
282
+
283
+# Nitro bindings for T3Highlights. Regenerated by `vp run generate:nitro` in
284
+# apps/mobile from modules/t3-highlights/src/T3Highlights.nitro.ts.
285
+apps/mobile/modules/t3-highlights/nitrogen/generated/**
apps/mobile/.swiftlint.yml
-1
@@ -1,7 +1,6 @@
1
included:
2
- ios/T3Code
3
- modules/t3-composer-editor/ios
4
- - modules/t3-highlights/ios
4
- modules/t3-native-controls/ios
5
- modules/t3-terminal/ios
6
- modules/t3-review-diff/ios
apps/mobile/modules/t3-highlights/T3HighlightsNative.podspec
+11
-8
@@ -10,21 +10,24 @@ Pod::Spec.new do |s|
10
s.homepage = 'https://t3tools.com'
11
s.license = { :type => 'UNLICENSED' }
12
s.author = { 'T3 Tools' => 'hello@t3tools.com' }
13
- s.platforms = { :ios => '16.1' }
13
+ s.platforms = { :ios => min_ios_version_supported }
14
s.source = { :path => '.' }
15
- s.source_files = 'ios/**/*.swift', 'cpp/**/*.{c,h,inc}', 'Vendor/**/*.{c,h,inc}'
16
- # Swift sees only the wrapper; the generated and runtime headers stay private.
17
- s.public_header_files = 'cpp/t3_highlights.h'
15
+ s.source_files = 'cpp/**/*.{c,cpp,h,hpp,inc}', 'Vendor/**/*.{c,h,inc}'
16
+ s.public_header_files = []
17
# Debug builds compile at -O0 by default, which makes the lexers several times
18
# slower. They are generated code, so optimize them in every configuration.
19
s.compiler_flags = '-O2'
20
s.pod_target_xcconfig = {
22
- 'DEFINES_MODULE' => 'YES',
21
# Plain malloc memory with explicit bounds checks: no guard-page signal
22
# handlers competing with the app's crash reporting.
23
'GCC_PREPROCESSOR_DEFINITIONS' => '$(inherited) WASM_RT_USE_MMAP=0 WASM_RT_MEMCHECK_BOUNDS_CHECK=1 WASM_RT_SKIP_SIGNAL_RECOVERY=1',
26
- 'HEADER_SEARCH_PATHS' => '$(inherited) "${PODS_TARGET_SRCROOT}/Vendor" "${PODS_TARGET_SRCROOT}/Vendor/highlights" "${PODS_TARGET_SRCROOT}/Vendor/wasm2c"',
24
+ 'HEADER_SEARCH_PATHS' => '$(inherited) "${PODS_TARGET_SRCROOT}/cpp" "${PODS_TARGET_SRCROOT}/Vendor" "${PODS_TARGET_SRCROOT}/Vendor/highlights" "${PODS_TARGET_SRCROOT}/Vendor/wasm2c"',
25
}
28
- s.swift_version = '5.9'
29
- s.dependency 'ExpoModulesCore'
26
+
27
+ load 'nitrogen/generated/ios/T3HighlightsNative+autolinking.rb'
28
+ add_nitrogen_files(s)
29
+
30
+ s.dependency 'React-jsi'
31
+ s.dependency 'React-callinvoker'
32
+ install_modules_dependencies(s)
33
end
apps/mobile/modules/t3-highlights/android/CMakeLists.txt
renamed
+9
-3
@@ -1,18 +1,24 @@
1
cmake_minimum_required(VERSION 3.22.1)
2
3
-project(t3highlights LANGUAGES C)
3
+project(t3highlights LANGUAGES C CXX)
4
5
-set(T3_HIGHLIGHTS_ROOT "${CMAKE_CURRENT_SOURCE_DIR}/../../../..")
5
+set(CMAKE_CXX_STANDARD 20)
6
+set(CMAKE_CXX_STANDARD_REQUIRED ON)
7
+set(T3_HIGHLIGHTS_ROOT "${CMAKE_SOURCE_DIR}/..")
8
9
add_library(
10
t3highlights SHARED
9
- t3_highlights_jni.c
11
+ src/main/cpp/cpp-adapter.cpp
12
+ "${T3_HIGHLIGHTS_ROOT}/cpp/HybridT3Highlights.cpp"
13
+ "${T3_HIGHLIGHTS_ROOT}/cpp/Utf16ToUtf8.cpp"
14
"${T3_HIGHLIGHTS_ROOT}/cpp/t3_highlights.c"
15
"${T3_HIGHLIGHTS_ROOT}/Vendor/highlights/highlights.c"
16
"${T3_HIGHLIGHTS_ROOT}/Vendor/wasm2c/wasm-rt-impl.c"
17
"${T3_HIGHLIGHTS_ROOT}/Vendor/wasm2c/wasm-rt-mem-impl.c"
18
)
19
20
+include(${CMAKE_SOURCE_DIR}/../nitrogen/generated/android/t3highlights+autolinking.cmake)
21
+
22
target_include_directories(
23
t3highlights
24
PRIVATE "${T3_HIGHLIGHTS_ROOT}/cpp" "${T3_HIGHLIGHTS_ROOT}/Vendor"
apps/mobile/modules/t3-highlights/android/build.gradle
+33
-3
@@ -1,11 +1,12 @@
1
apply plugin: 'com.android.library'
2
apply plugin: 'org.jetbrains.kotlin.android'
3
+apply from: '../nitrogen/generated/android/t3highlights+autolinking.gradle'
4
5
group = 'com.t3tools.highlights'
6
version = '0.0.0'
7
8
android {
8
- namespace 'expo.modules.t3highlights'
9
+ namespace 'com.margelo.nitro.t3highlights'
10
ndkVersion rootProject.ext.ndkVersion
11
compileSdk rootProject.ext.compileSdkVersion
12
@@ -19,16 +20,45 @@ android {
20
abiFilters(*requestedArchitectures.split(','))
21
}
22
}
23
+
24
+ externalNativeBuild {
25
+ cmake {
26
+ cppFlags '-frtti -fexceptions -Wall -Wextra'
27
+ arguments '-DANDROID_STL=c++_shared', '-DANDROID_SUPPORT_FLEXIBLE_PAGE_SIZES=ON'
28
+ }
29
+ }
30
}
31
32
externalNativeBuild {
33
cmake {
26
- path 'src/main/cpp/CMakeLists.txt'
34
+ path 'CMakeLists.txt'
35
version '3.22.1'
36
}
37
}
38
+
39
+ buildFeatures {
40
+ prefab true
41
+ }
42
+
43
+ packagingOptions {
44
+ excludes = [
45
+ '**/libc++_shared.so',
46
+ '**/libfbjni.so',
47
+ '**/libjsi.so',
48
+ '**/libreactnative.so',
49
+ '**/libNitroModules.so',
50
+ ]
51
+ }
52
}
53
54
dependencies {
33
- implementation project(':expo-modules-core')
55
+ implementation 'com.facebook.react:react-android'
56
+ implementation project(':react-native-nitro-modules')
57
+}
58
+
59
+// CMake reads Nitro's prefab headers, which its prepareHeaders task writes.
60
+tasks.configureEach { task ->
61
+ if (task.name.contains('configureCMake') || task.name.contains('buildCMake')) {
62
+ task.dependsOn(':react-native-nitro-modules:prepareHeaders')
63
+ }
64
}
apps/mobile/modules/t3-highlights/android/src/main/cpp/cpp-adapter.cpp
new
+8
@@ -0,0 +1,8 @@
1
+#include <fbjni/fbjni.h>
2
+#include <jni.h>
3
+
4
+#include "t3highlightsOnLoad.hpp"
5
+
6
+JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void*) {
7
+ return facebook::jni::initialize(vm, [] { margelo::nitro::t3highlights::registerAllNatives(); });
8
+}
apps/mobile/modules/t3-highlights/android/src/main/cpp/t3_highlights_jni.c
deleted
-41
@@ -1,41 +0,0 @@
1
-#include <jni.h>
2
-#include <string.h>
3
-
4
-#include "t3_highlights.h"
5
-
6
-// HighlightsNative serializes both calls, so the copy reads the records the
7
-// tokenize call just produced.
8
-static const uint32_t* last_records = NULL;
9
-static int64_t last_count = 0;
10
-
11
-JNIEXPORT jint JNICALL Java_expo_modules_t3highlights_HighlightsNative_nativeTokenize(
12
- JNIEnv* env, jclass clazz, jbyteArray source, jint language) {
13
- (void)clazz;
14
- last_records = NULL;
15
- last_count = 0;
16
- if (language < 0 || language > UINT8_MAX) return -1;
17
-
18
- jsize length = (*env)->GetArrayLength(env, source);
19
- jbyte* bytes = (*env)->GetByteArrayElements(env, source, NULL);
20
- if (bytes == NULL) return -1;
21
- int64_t count = t3_highlights_tokenize((const uint8_t*)bytes, (size_t)length,
22
- (uint8_t)language, &last_records);
23
- (*env)->ReleaseByteArrayElements(env, source, bytes, JNI_ABORT);
24
-
25
- if (count < 0 || count > INT32_MAX / 4) {
26
- last_records = NULL;
27
- return -1;
28
- }
29
- last_count = count;
30
- return (jint)count;
31
-}
32
-
33
-JNIEXPORT void JNICALL Java_expo_modules_t3highlights_HighlightsNative_nativeCopyRecords(
34
- JNIEnv* env, jclass clazz, jobject destination) {
35
- (void)clazz;
36
- void* address = (*env)->GetDirectBufferAddress(env, destination);
37
- jlong capacity = (*env)->GetDirectBufferCapacity(env, destination);
38
- size_t byte_count = (size_t)last_count * sizeof(uint32_t);
39
- if (address == NULL || last_records == NULL || capacity < (jlong)byte_count) return;
40
- memcpy(address, last_records, byte_count);
41
-}
apps/mobile/modules/t3-highlights/android/src/main/java/com/margelo/nitro/t3highlights/T3HighlightsPackage.kt
new
+19
@@ -0,0 +1,19 @@
1
+package com.margelo.nitro.t3highlights
2
+
3
+import com.facebook.react.BaseReactPackage
4
+import com.facebook.react.bridge.NativeModule
5
+import com.facebook.react.bridge.ReactApplicationContext
6
+import com.facebook.react.module.model.ReactModuleInfoProvider
7
+
8
+// Autolinking instantiates this package, which loads the library that registers T3Highlights.
9
+class T3HighlightsPackage : BaseReactPackage() {
10
+ override fun getModule(name: String, reactContext: ReactApplicationContext): NativeModule? = null
11
+
12
+ override fun getReactModuleInfoProvider() = ReactModuleInfoProvider { emptyMap() }
13
+
14
+ companion object {
15
+ init {
16
+ t3highlightsOnLoad.initializeNative()
17
+ }
18
+ }
19
+}
apps/mobile/modules/t3-highlights/android/src/main/java/expo/modules/t3highlights/T3HighlightsModule.kt
deleted
-37
@@ -1,37 +0,0 @@
1
-package expo.modules.t3highlights
2
-
3
-import expo.modules.kotlin.jni.ArrayBuffer
4
-import expo.modules.kotlin.modules.Module
5
-import expo.modules.kotlin.modules.ModuleDefinition
6
-import java.nio.ByteBuffer
7
-import java.nio.ByteOrder
8
-
9
-class T3HighlightsModule : Module() {
10
- override fun definition() = ModuleDefinition {
11
- Name("T3Highlights")
12
-
13
- Function("tokenize") { source: String, language: Int ->
14
- HighlightsNative.tokenize(source, language)
15
- }
16
- }
17
-}
18
-
19
-internal object HighlightsNative {
20
- init {
21
- System.loadLibrary("t3highlights")
22
- }
23
-
24
- // The records live in the lexer's memory until its next run.
25
- @Synchronized
26
- fun tokenize(source: String, language: Int): ArrayBuffer? {
27
- val count = nativeTokenize(source.toByteArray(Charsets.UTF_8), language)
28
- if (count < 0) return null
29
- val records = ByteBuffer.allocateDirect(count * Int.SIZE_BYTES).order(ByteOrder.nativeOrder())
30
- nativeCopyRecords(records)
31
- return ArrayBuffer(records)
32
- }
33
-
34
- @JvmStatic private external fun nativeTokenize(source: ByteArray, language: Int): Int
35
-
36
- @JvmStatic private external fun nativeCopyRecords(destination: ByteBuffer)
37
-}
apps/mobile/modules/t3-highlights/cpp/HybridT3Highlights.cpp
new
+32
@@ -0,0 +1,32 @@
1
+#include "HybridT3Highlights.hpp"
2
+
3
+#include <cstdint>
4
+#include <mutex>
5
+
6
+#include "t3_highlights.h"
7
+
8
+namespace margelo::nitro::t3highlights {
9
+
10
+namespace {
11
+// The lexer's memory is process-global and every JS runtime shares it.
12
+std::mutex lexerMutex;
13
+} // namespace
14
+
15
+std::optional<std::shared_ptr<ArrayBuffer>> HybridT3Highlights::tokenize(SourceText source,
16
+ double language) {
17
+ if (!(language >= 0 && language <= UINT8_MAX)) return std::nullopt;
18
+ const auto languageId = static_cast<uint8_t>(language);
19
+ if (languageId != language) return std::nullopt;
20
+
21
+ std::lock_guard lock(lexerMutex);
22
+ const uint32_t* records = nullptr;
23
+ const int64_t count = t3_highlights_tokenize(reinterpret_cast<const uint8_t*>(source.utf8.data()),
24
+ source.utf8.size(), languageId, &records);
25
+ if (count < 0) return std::nullopt;
26
+ const auto byteCount = static_cast<size_t>(count) * sizeof(uint32_t);
27
+ if (records == nullptr || byteCount == 0) return ArrayBuffer::allocate(0);
28
+ // The records live in the lexer's memory until its next run.
29
+ return ArrayBuffer::copy(reinterpret_cast<const uint8_t*>(records), byteCount);
30
+}
31
+
32
+} // namespace margelo::nitro::t3highlights
apps/mobile/modules/t3-highlights/cpp/HybridT3Highlights.hpp
new
+15
@@ -0,0 +1,15 @@
1
+#pragma once
2
+
3
+#include "HybridT3HighlightsSpec.hpp"
4
+
5
+namespace margelo::nitro::t3highlights {
6
+
7
+class HybridT3Highlights : public HybridT3HighlightsSpec {
8
+public:
9
+ HybridT3Highlights() : HybridObject(TAG) {}
10
+
11
+ std::optional<std::shared_ptr<ArrayBuffer>> tokenize(SourceText source,
12
+ double language) override;
13
+};
14
+
15
+} // namespace margelo::nitro::t3highlights
apps/mobile/modules/t3-highlights/cpp/SourceText.hpp
new
+43
@@ -0,0 +1,43 @@
1
+#pragma once
2
+
3
+#include <NitroModules/JSIConverter.hpp>
4
+#include <string>
5
+
6
+#include "Utf16ToUtf8.hpp"
7
+
8
+namespace margelo::nitro::t3highlights {
9
+
10
+/** A JS string as the UTF-8 bytes the lexer reads. */
11
+struct SourceText {
12
+ std::string utf8;
13
+};
14
+
15
+} // namespace margelo::nitro::t3highlights
16
+
17
+namespace margelo::nitro {
18
+
19
+// Hermes' own utf8() is several times slower than this for strings it stores as
20
+// UTF-16, which is any string with one non-ASCII character in it.
21
+template <>
22
+struct JSIConverter<t3highlights::SourceText> final {
23
+ static inline t3highlights::SourceText fromJSI(jsi::Runtime& runtime, const jsi::Value& arg) {
24
+ t3highlights::SourceText source;
25
+ auto read = [&source](bool ascii, const void* data, size_t length) {
26
+ if (ascii) {
27
+ source.utf8.append(static_cast<const char*>(data), length);
28
+ } else {
29
+ t3highlights::appendUtf16AsUtf8(source.utf8, static_cast<const char16_t*>(data), length);
30
+ }
31
+ };
32
+ arg.asString(runtime).getStringData(runtime, read);
33
+ return source;
34
+ }
35
+ static inline jsi::Value toJSI(jsi::Runtime& runtime, const t3highlights::SourceText& arg) {
36
+ return jsi::String::createFromUtf8(runtime, arg.utf8);
37
+ }
38
+ static inline bool canConvert(jsi::Runtime&, const jsi::Value& value) {
39
+ return value.isString();
40
+ }
41
+};
42
+
43
+} // namespace margelo::nitro
apps/mobile/modules/t3-highlights/cpp/Utf16ToUtf8.cpp
new
+58
@@ -0,0 +1,58 @@
1
+#include "Utf16ToUtf8.hpp"
2
+
3
+#include <cstdint>
4
+#include <cstring>
5
+
6
+namespace margelo::nitro::t3highlights {
7
+
8
+void appendUtf16AsUtf8(std::string& out, const char16_t* data, size_t length) {
9
+ const size_t start = out.size();
10
+ out.resize(start + length * 3);
11
+ auto* const begin = reinterpret_cast<uint8_t*>(out.data());
12
+ auto* write = begin + start;
13
+ size_t at = 0;
14
+ while (at < length) {
15
+ // Runs of four ASCII code units are the common case in source code.
16
+ if (at + 4 <= length) {
17
+ uint64_t chunk;
18
+ std::memcpy(&chunk, data + at, sizeof(chunk));
19
+ if ((chunk & 0xff80ff80ff80ff80ULL) == 0) {
20
+ write[0] = static_cast<uint8_t>(data[at]);
21
+ write[1] = static_cast<uint8_t>(data[at + 1]);
22
+ write[2] = static_cast<uint8_t>(data[at + 2]);
23
+ write[3] = static_cast<uint8_t>(data[at + 3]);
24
+ write += 4;
25
+ at += 4;
26
+ continue;
27
+ }
28
+ }
29
+ uint32_t unit = data[at++];
30
+ if (unit < 0x80) {
31
+ *write++ = static_cast<uint8_t>(unit);
32
+ } else if (unit < 0x800) {
33
+ *write++ = static_cast<uint8_t>(0xc0 | (unit >> 6));
34
+ *write++ = static_cast<uint8_t>(0x80 | (unit & 0x3f));
35
+ } else {
36
+ uint32_t codePoint = unit;
37
+ if (unit >= 0xd800 && unit <= 0xdfff) {
38
+ codePoint = 0xfffd;
39
+ if (unit <= 0xdbff && at < length && data[at] >= 0xdc00 && data[at] <= 0xdfff) {
40
+ codePoint = 0x10000 + ((unit - 0xd800) << 10) + (data[at++] - 0xdc00);
41
+ }
42
+ }
43
+ if (codePoint < 0x10000) {
44
+ *write++ = static_cast<uint8_t>(0xe0 | (codePoint >> 12));
45
+ *write++ = static_cast<uint8_t>(0x80 | ((codePoint >> 6) & 0x3f));
46
+ *write++ = static_cast<uint8_t>(0x80 | (codePoint & 0x3f));
47
+ } else {
48
+ *write++ = static_cast<uint8_t>(0xf0 | (codePoint >> 18));
49
+ *write++ = static_cast<uint8_t>(0x80 | ((codePoint >> 12) & 0x3f));
50
+ *write++ = static_cast<uint8_t>(0x80 | ((codePoint >> 6) & 0x3f));
51
+ *write++ = static_cast<uint8_t>(0x80 | (codePoint & 0x3f));
52
+ }
53
+ }
54
+ }
55
+ out.resize(static_cast<size_t>(write - begin));
56
+}
57
+
58
+} // namespace margelo::nitro::t3highlights
apps/mobile/modules/t3-highlights/cpp/Utf16ToUtf8.hpp
new
+11
@@ -0,0 +1,11 @@
1
+#pragma once
2
+
3
+#include <cstddef>
4
+#include <string>
5
+
6
+namespace margelo::nitro::t3highlights {
7
+
8
+/** Lone surrogates become U+FFFD, as with `jsi::String::utf8`. */
9
+void appendUtf16AsUtf8(std::string& out, const char16_t* data, size_t length);
10
+
11
+} // namespace margelo::nitro::t3highlights
apps/mobile/modules/t3-highlights/expo-module.config.json
deleted
-10
@@ -1,10 +0,0 @@
1
-{
2
- "platforms": ["apple", "android"],
3
- "apple": {
4
- "modules": ["T3HighlightsModule"],
5
- "podspecPath": "T3HighlightsNative.podspec"
6
- },
7
- "android": {
8
- "modules": ["expo.modules.t3highlights.T3HighlightsModule"]
9
- }
10
-}
apps/mobile/modules/t3-highlights/ios/T3HighlightsModule.swift
deleted
-29
@@ -1,29 +0,0 @@
1
-import ExpoModulesCore
2
-
3
-public final class T3HighlightsModule: Module {
4
- private let lock = NSLock()
5
-
6
- public func definition() -> ModuleDefinition {
7
- Name("T3Highlights")
8
-
9
- Function("tokenize") { (source: String, language: Int) -> ArrayBuffer? in
10
- var source = source
11
- return source.withUTF8 { utf8 in
12
- self.lock.withLock { Self.tokenize(utf8, language: language) }
13
- }
14
- }
15
- }
16
-
17
- private static func tokenize(_ utf8: UnsafeBufferPointer<UInt8>, language: Int) -> ArrayBuffer? {
18
- guard let languageId = UInt8(exactly: language) else { return nil }
19
- var records: UnsafePointer<UInt32>?
20
- let count = t3_highlights_tokenize(utf8.baseAddress, utf8.count, languageId, &records)
21
- guard count >= 0 else { return nil }
22
- let byteCount = Int(count) * MemoryLayout<UInt32>.size
23
- let buffer = ArrayBuffer(size: byteCount, initializeToZero: false)
24
- if let records, byteCount > 0 {
25
- buffer.withUnsafeMutableBytes { $0.copyMemory(from: UnsafeRawBufferPointer(start: records, count: byteCount)) }
26
- }
27
- return buffer
28
- }
29
-}
apps/mobile/modules/t3-highlights/nitro.json
new
+20
@@ -0,0 +1,20 @@
1
+{
2
+ "$schema": "https://nitro.margelo.com/nitro.schema.json",
3
+ "cxxNamespace": ["t3highlights"],
4
+ "ios": {
5
+ "iosModuleName": "T3HighlightsNative"
6
+ },
7
+ "android": {
8
+ "androidNamespace": ["t3highlights"],
9
+ "androidCxxLibName": "t3highlights"
10
+ },
11
+ "autolinking": {
12
+ "T3Highlights": {
13
+ "all": {
14
+ "language": "c++",
15
+ "implementationClassName": "HybridT3Highlights"
16
+ }
17
+ }
18
+ },
19
+ "ignorePaths": ["node_modules", "Vendor"]
20
+}
apps/mobile/modules/t3-highlights/nitrogen/generated/.gitattributes
new
+1
@@ -0,0 +1 @@
1
+** linguist-generated=true
apps/mobile/modules/t3-highlights/nitrogen/generated/android/kotlin/com/margelo/nitro/t3highlights/t3highlightsOnLoad.kt
new
+35
@@ -0,0 +1,35 @@
1
+///
2
+/// t3highlightsOnLoad.kt
3
+/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
4
+/// https://github.com/mrousavy/nitro
5
+/// Copyright © Marc Rousavy @ Margelo
6
+///
7
+
8
+package com.margelo.nitro.t3highlights
9
+
10
+import android.util.Log
11
+
12
+internal class t3highlightsOnLoad {
13
+ companion object {
14
+ private const val TAG = "t3highlightsOnLoad"
15
+ private var didLoad = false
16
+ /**
17
+ * Initializes the native part of "t3highlights".
18
+ * This method is idempotent and can be called more than once.
19
+ */
20
+ @JvmStatic
21
+ fun initializeNative() {
22
+ if (didLoad) return
23
+ try {
24
+ Log.i(TAG, "Loading t3highlights C++ library...")
25
+ System.loadLibrary("t3highlights")
26
+ Log.i(TAG, "Successfully loaded t3highlights C++ library!")
27
+ didLoad = true
28
+ } catch (e: Error) {
29
+ Log.e(TAG, "Failed to load t3highlights C++ library! Is it properly installed and linked? " +
30
+ "Is the name correct? (see `CMakeLists.txt`, at `add_library(...)`)", e)
31
+ throw e
32
+ }
33
+ }
34
+ }
35
+}
apps/mobile/modules/t3-highlights/nitrogen/generated/android/t3highlights+autolinking.cmake
new
+81
@@ -0,0 +1,81 @@
1
+#
2
+# t3highlights+autolinking.cmake
3
+# This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
4
+# https://github.com/mrousavy/nitro
5
+# Copyright © Marc Rousavy @ Margelo
6
+#
7
+
8
+# This is a CMake file that adds all files generated by Nitrogen
9
+# to the current CMake project.
10
+#
11
+# To use it, add this to your CMakeLists.txt:
12
+# ```cmake
13
+# include(${CMAKE_SOURCE_DIR}/../nitrogen/generated/android/t3highlights+autolinking.cmake)
14
+# ```
15
+
16
+# Define a flag to check if we are building properly
17
+add_definitions(-DBUILDING_T3HIGHLIGHTS_WITH_GENERATED_CMAKE_PROJECT)
18
+
19
+# Enable Raw Props parsing in react-native (for Nitro Views)
20
+add_definitions(-DRN_SERIALIZABLE_STATE)
21
+
22
+# Add all headers that were generated by Nitrogen
23
+include_directories(
24
+ "../nitrogen/generated/shared/c++"
25
+ "../nitrogen/generated/android/c++"
26
+ "../nitrogen/generated/android/"
27
+)
28
+
29
+# Add all .cpp sources that were generated by Nitrogen
30
+target_sources(
31
+ # CMake project name (Android C++ library name)
32
+ t3highlights PRIVATE
33
+ # Autolinking Setup
34
+ ../nitrogen/generated/android/t3highlightsOnLoad.cpp
35
+ # Shared Nitrogen C++ sources
36
+ ../nitrogen/generated/shared/c++/HybridT3HighlightsSpec.cpp
37
+ # Android-specific Nitrogen C++ sources
38
+
39
+)
40
+
41
+# From node_modules/react-native/ReactAndroid/cmake-utils/folly-flags.cmake
42
+# Used in node_modules/react-native/ReactAndroid/cmake-utils/ReactNative-application.cmake
43
+target_compile_definitions(
44
+ t3highlights PRIVATE
45
+ -DFOLLY_NO_CONFIG=1
46
+ -DFOLLY_HAVE_CLOCK_GETTIME=1
47
+ -DFOLLY_USE_LIBCPP=1
48
+ -DFOLLY_CFG_NO_COROUTINES=1
49
+ -DFOLLY_MOBILE=1
50
+ -DFOLLY_HAVE_RECVMMSG=1
51
+ -DFOLLY_HAVE_PTHREAD=1
52
+ # Once we target android-23 above, we can comment
53
+ # the following line. NDK uses GNU style stderror_r() after API 23.
54
+ -DFOLLY_HAVE_XSI_STRERROR_R=1
55
+)
56
+
57
+# Add all libraries required by the generated specs
58
+find_package(fbjni REQUIRED) # <-- Used for communication between Java <-> C++
59
+find_package(ReactAndroid REQUIRED) # <-- Used to set up React Native bindings (e.g. CallInvoker/TurboModule)
60
+find_package(react-native-nitro-modules REQUIRED) # <-- Used to create all HybridObjects and use the Nitro core library
61
+
62
+# Link all libraries together
63
+target_link_libraries(
64
+ t3highlights
65
+ fbjni::fbjni # <-- Facebook C++ JNI helpers
66
+ ReactAndroid::jsi # <-- RN: JSI
67
+ react-native-nitro-modules::NitroModules # <-- NitroModules Core :)
68
+)
69
+
70
+# Link react-native (different prefab between RN 0.75 and RN 0.76)
71
+if(ReactAndroid_VERSION_MINOR GREATER_EQUAL 76)
72
+ target_link_libraries(
73
+ t3highlights
74
+ ReactAndroid::reactnative # <-- RN: Native Modules umbrella prefab
75
+ )
76
+else()
77
+ target_link_libraries(
78
+ t3highlights
79
+ ReactAndroid::react_nativemodule_core # <-- RN: TurboModules Core
80
+ )
81
+endif()
apps/mobile/modules/t3-highlights/nitrogen/generated/android/t3highlights+autolinking.gradle
new
+27
@@ -0,0 +1,27 @@
1
+///
2
+/// t3highlights+autolinking.gradle
3
+/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
4
+/// https://github.com/mrousavy/nitro
5
+/// Copyright © Marc Rousavy @ Margelo
6
+///
7
+
8
+/// This is a Gradle file that adds all files generated by Nitrogen
9
+/// to the current Gradle project.
10
+///
11
+/// To use it, add this to your build.gradle:
12
+/// ```gradle
13
+/// apply from: '../nitrogen/generated/android/t3highlights+autolinking.gradle'
14
+/// ```
15
+
16
+logger.warn("[NitroModules] 🔥 t3highlights is boosted by nitro!")
17
+
18
+android {
19
+ sourceSets {
20
+ main {
21
+ java.srcDirs += [
22
+ // Nitrogen files
23
+ "${project.projectDir}/../nitrogen/generated/android/kotlin"
24
+ ]
25
+ }
26
+ }
27
+}
apps/mobile/modules/t3-highlights/nitrogen/generated/android/t3highlightsOnLoad.cpp
new
+49
@@ -0,0 +1,49 @@
1
+///
2
+/// t3highlightsOnLoad.cpp
3
+/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
4
+/// https://github.com/mrousavy/nitro
5
+/// Copyright © Marc Rousavy @ Margelo
6
+///
7
+
8
+#ifndef BUILDING_T3HIGHLIGHTS_WITH_GENERATED_CMAKE_PROJECT
9
+#error t3highlightsOnLoad.cpp is not being built with the autogenerated CMakeLists.txt project. Is a different CMakeLists.txt building this?
10
+#endif
11
+
12
+#include "t3highlightsOnLoad.hpp"
13
+
14
+#include <jni.h>
15
+#include <fbjni/fbjni.h>
16
+#include <NitroModules/HybridObjectRegistry.hpp>
17
+
18
+#include "HybridT3Highlights.hpp"
19
+
20
+namespace margelo::nitro::t3highlights {
21
+
22
+int initialize(JavaVM* vm) {
23
+ return facebook::jni::initialize(vm, []() {
24
+ ::margelo::nitro::t3highlights::registerAllNatives();
25
+ });
26
+}
27
+
28
+
29
+
30
+void registerAllNatives() {
31
+ using namespace margelo::nitro;
32
+ using namespace margelo::nitro::t3highlights;
33
+
34
+ // Register native JNI methods
35
+
36
+
37
+ // Register Nitro Hybrid Objects
38
+ HybridObjectRegistry::registerHybridObjectConstructor(
39
+ "T3Highlights",
40
+ []() -> std::shared_ptr<HybridObject> {
41
+ static_assert(std::is_default_constructible_v<HybridT3Highlights>,
42
+ "The HybridObject \"HybridT3Highlights\" is not default-constructible! "
43
+ "Create a public constructor that takes zero arguments to be able to autolink this HybridObject.");
44
+ return std::make_shared<HybridT3Highlights>();
45
+ }
46
+ );
47
+}
48
+
49
+} // namespace margelo::nitro::t3highlights
apps/mobile/modules/t3-highlights/nitrogen/generated/android/t3highlightsOnLoad.hpp
new
+34
@@ -0,0 +1,34 @@
1
+///
2
+/// t3highlightsOnLoad.hpp
3
+/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
4
+/// https://github.com/mrousavy/nitro
5
+/// Copyright © Marc Rousavy @ Margelo
6
+///
7
+
8
+#include <jni.h>
9
+#include <functional>
10
+#include <NitroModules/NitroDefines.hpp>
11
+
12
+namespace margelo::nitro::t3highlights {
13
+
14
+ [[deprecated("Use registerNatives() instead.")]]
15
+ int initialize(JavaVM* vm);
16
+
17
+ /**
18
+ * Register the native (C++) part of t3highlights, and autolinks all Hybrid Objects.
19
+ * Call this in your `JNI_OnLoad` function (probably inside `cpp-adapter.cpp`),
20
+ * inside a `facebook::jni::initialize(vm, ...)` call.
21
+ * Example:
22
+ * ```cpp (cpp-adapter.cpp)
23
+ * JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void*) {
24
+ * return facebook::jni::initialize(vm, []() {
25
+ * // register all t3highlights HybridObjects
26
+ * margelo::nitro::t3highlights::registerNatives();
27
+ * // any other custom registrations go here.
28
+ * });
29
+ * }
30
+ * ```
31
+ */
32
+ void registerAllNatives();
33
+
34
+} // namespace margelo::nitro::t3highlights
apps/mobile/modules/t3-highlights/nitrogen/generated/ios/T3HighlightsNative+autolinking.rb
new
+62
@@ -0,0 +1,62 @@
1
+#
2
+# T3HighlightsNative+autolinking.rb
3
+# This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
4
+# https://github.com/mrousavy/nitro
5
+# Copyright © Marc Rousavy @ Margelo
6
+#
7
+
8
+# This is a Ruby script that adds all files generated by Nitrogen
9
+# to the given podspec.
10
+#
11
+# To use it, add this to your .podspec:
12
+# ```ruby
13
+# Pod::Spec.new do |spec|
14
+# # ...
15
+#
16
+# # Add all files generated by Nitrogen
17
+# load 'nitrogen/generated/ios/T3HighlightsNative+autolinking.rb'
18
+# add_nitrogen_files(spec)
19
+# end
20
+# ```
21
+
22
+def add_nitrogen_files(spec)
23
+ Pod::UI.puts "[NitroModules] 🔥 T3HighlightsNative is boosted by nitro!"
24
+
25
+ spec.dependency "NitroModules"
26
+
27
+ current_source_files = Array(spec.attributes_hash['source_files'])
28
+ spec.source_files = current_source_files + [
29
+ # Generated cross-platform specs
30
+ "nitrogen/generated/shared/**/*.{h,hpp,c,cpp,swift}",
31
+ # Generated bridges for the cross-platform specs
32
+ "nitrogen/generated/ios/**/*.{h,hpp,c,cpp,mm,swift}",
33
+ ]
34
+
35
+ current_public_header_files = Array(spec.attributes_hash['public_header_files'])
36
+ spec.public_header_files = current_public_header_files + [
37
+ # Generated specs
38
+ "nitrogen/generated/shared/**/*.{h,hpp}",
39
+ # Swift to C++ bridging helpers
40
+ "nitrogen/generated/ios/T3HighlightsNative-Swift-Cxx-Bridge.hpp"
41
+ ]
42
+
43
+ current_private_header_files = Array(spec.attributes_hash['private_header_files'])
44
+ spec.private_header_files = current_private_header_files + [
45
+ # iOS specific specs
46
+ "nitrogen/generated/ios/c++/**/*.{h,hpp}",
47
+ # Views are framework-specific and should be private
48
+ "nitrogen/generated/shared/**/views/**/*"
49
+ ]
50
+
51
+ current_pod_target_xcconfig = spec.attributes_hash['pod_target_xcconfig'] || {}
52
+ spec.pod_target_xcconfig = current_pod_target_xcconfig.merge({
53
+ # Use C++ 20
54
+ "CLANG_CXX_LANGUAGE_STANDARD" => "c++20",
55
+ # Enables C++ <-> Swift interop (by default it's only ObjC)
56
+ "SWIFT_OBJC_INTEROP_MODE" => "objcxx",
57
+ # Enables stricter modular headers
58
+ "DEFINES_MODULE" => "YES",
59
+ # Disable auto-generated ObjC header for Swift (Static linkage on Xcode 26.4 breaks here)
60
+ "SWIFT_INSTALL_OBJC_HEADER" => "NO",
61
+ })
62
+end
apps/mobile/modules/t3-highlights/nitrogen/generated/ios/T3HighlightsNative-Swift-Cxx-Bridge.cpp
new
+17
@@ -0,0 +1,17 @@
1
+///
2
+/// T3HighlightsNative-Swift-Cxx-Bridge.cpp
3
+/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
4
+/// https://github.com/mrousavy/nitro
5
+/// Copyright © Marc Rousavy @ Margelo
6
+///
7
+
8
+#include "T3HighlightsNative-Swift-Cxx-Bridge.hpp"
9
+
10
+// Include C++ implementation defined types
11
+
12
+
13
+namespace margelo::nitro::t3highlights::bridge::swift {
14
+
15
+
16
+
17
+} // namespace margelo::nitro::t3highlights::bridge::swift
apps/mobile/modules/t3-highlights/nitrogen/generated/ios/T3HighlightsNative-Swift-Cxx-Bridge.hpp
new
+27
@@ -0,0 +1,27 @@
1
+///
2
+/// T3HighlightsNative-Swift-Cxx-Bridge.hpp
3
+/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
4
+/// https://github.com/mrousavy/nitro
5
+/// Copyright © Marc Rousavy @ Margelo
6
+///
7
+
8
+#pragma once
9
+
10
+// Forward declarations of C++ defined types
11
+
12
+
13
+// Forward declarations of Swift defined types
14
+
15
+
16
+// Include C++ defined types
17
+
18
+
19
+/**
20
+ * Contains specialized versions of C++ templated types so they can be accessed from Swift,
21
+ * as well as helper functions to interact with those C++ types from Swift.
22
+ */
23
+namespace margelo::nitro::t3highlights::bridge::swift {
24
+
25
+
26
+
27
+} // namespace margelo::nitro::t3highlights::bridge::swift
apps/mobile/modules/t3-highlights/nitrogen/generated/ios/T3HighlightsNative-Swift-Cxx-Umbrella.hpp
new
+38
@@ -0,0 +1,38 @@
1
+///
2
+/// T3HighlightsNative-Swift-Cxx-Umbrella.hpp
3
+/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
4
+/// https://github.com/mrousavy/nitro
5
+/// Copyright © Marc Rousavy @ Margelo
6
+///
7
+
8
+#pragma once
9
+
10
+// Forward declarations of C++ defined types
11
+
12
+
13
+// Include C++ defined types
14
+
15
+
16
+// C++ helpers for Swift
17
+#include "T3HighlightsNative-Swift-Cxx-Bridge.hpp"
18
+
19
+// Common C++ types used in Swift
20
+#include <NitroModules/ArrayBufferHolder.hpp>
21
+#include <NitroModules/AnyMapUtils.hpp>
22
+#include <NitroModules/RuntimeError.hpp>
23
+#include <NitroModules/DateToChronoDate.hpp>
24
+
25
+// Forward declarations of Swift defined types
26
+
27
+
28
+// Include Swift defined types
29
+#if __has_include("T3HighlightsNative-Swift.h")
30
+// This header is generated by Xcode/Swift on every app build.
31
+// If it cannot be found, make sure the Swift module's name (= podspec name) is actually "T3HighlightsNative".
32
+#include "T3HighlightsNative-Swift.h"
33
+// Same as above, but used when building with frameworks (`use_frameworks`)
34
+#elif __has_include(<T3HighlightsNative/T3HighlightsNative-Swift.h>)
35
+#include <T3HighlightsNative/T3HighlightsNative-Swift.h>
36
+#else
37
+#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.
38
+#endif
apps/mobile/modules/t3-highlights/nitrogen/generated/ios/T3HighlightsNativeAutolinking.mm
new
+35
@@ -0,0 +1,35 @@
1
+///
2
+/// T3HighlightsNativeAutolinking.mm
3
+/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
4
+/// https://github.com/mrousavy/nitro
5
+/// Copyright © Marc Rousavy @ Margelo
6
+///
7
+
8
+#import <Foundation/Foundation.h>
9
+#import <NitroModules/HybridObjectRegistry.hpp>
10
+
11
+#import <type_traits>
12
+
13
+#include "HybridT3Highlights.hpp"
14
+
15
+@interface T3HighlightsNativeAutolinking : NSObject
16
+@end
17
+
18
+@implementation T3HighlightsNativeAutolinking
19
+
20
++ (void) load {
21
+ using namespace margelo::nitro;
22
+ using namespace margelo::nitro::t3highlights;
23
+
24
+ HybridObjectRegistry::registerHybridObjectConstructor(
25
+ "T3Highlights",
26
+ []() -> std::shared_ptr<HybridObject> {
27
+ static_assert(std::is_default_constructible_v<HybridT3Highlights>,
28
+ "The HybridObject \"HybridT3Highlights\" is not default-constructible! "
29
+ "Create a public constructor that takes zero arguments to be able to autolink this HybridObject.");
30
+ return std::make_shared<HybridT3Highlights>();
31
+ }
32
+ );
33
+}
34
+
35
+@end
apps/mobile/modules/t3-highlights/nitrogen/generated/ios/T3HighlightsNativeAutolinking.swift
new
+16
@@ -0,0 +1,16 @@
1
+///
2
+/// T3HighlightsNativeAutolinking.swift
3
+/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
4
+/// https://github.com/mrousavy/nitro
5
+/// Copyright © Marc Rousavy @ Margelo
6
+///
7
+
8
+import NitroModules
9
+
10
+// TODO: Use empty enums once Swift supports exporting them as namespaces
11
+// See: https://github.com/swiftlang/swift/pull/83616
12
+public final class T3HighlightsNativeAutolinking {
13
+ public typealias bridge = margelo.nitro.t3highlights.bridge.swift
14
+
15
+
16
+}
apps/mobile/modules/t3-highlights/nitrogen/generated/shared/c++/HybridT3HighlightsSpec.cpp
new
+21
@@ -0,0 +1,21 @@
1
+///
2
+/// HybridT3HighlightsSpec.cpp
3
+/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
4
+/// https://github.com/mrousavy/nitro
5
+/// Copyright © Marc Rousavy @ Margelo
6
+///
7
+
8
+#include "HybridT3HighlightsSpec.hpp"
9
+
10
+namespace margelo::nitro::t3highlights {
11
+
12
+ void HybridT3HighlightsSpec::loadHybridMethods() {
13
+ // load base methods/properties
14
+ HybridObject::loadHybridMethods();
15
+ // load custom methods/properties
16
+ registerHybrids(this, [](Prototype& prototype) {
17
+ prototype.registerHybridMethod("tokenize", &HybridT3HighlightsSpec::tokenize);
18
+ });
19
+ }
20
+
21
+} // namespace margelo::nitro::t3highlights
apps/mobile/modules/t3-highlights/nitrogen/generated/shared/c++/HybridT3HighlightsSpec.hpp
new
+64
@@ -0,0 +1,64 @@
1
+///
2
+/// HybridT3HighlightsSpec.hpp
3
+/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
4
+/// https://github.com/mrousavy/nitro
5
+/// Copyright © Marc Rousavy @ Margelo
6
+///
7
+
8
+#pragma once
9
+
10
+#if __has_include(<NitroModules/HybridObject.hpp>)
11
+#include <NitroModules/HybridObject.hpp>
12
+#else
13
+#error NitroModules cannot be found! Are you sure you installed NitroModules properly?
14
+#endif
15
+
16
+
17
+
18
+#include <NitroModules/ArrayBuffer.hpp>
19
+#include <optional>
20
+#include "SourceText.hpp"
21
+
22
+namespace margelo::nitro::t3highlights {
23
+
24
+ using namespace margelo::nitro;
25
+
26
+ /**
27
+ * An abstract base class for `T3Highlights`
28
+ * Inherit this class to create instances of `HybridT3HighlightsSpec` in C++.
29
+ * You must explicitly call `HybridObject`'s constructor yourself, because it is virtual.
30
+ * @example
31
+ * ```cpp
32
+ * class HybridT3Highlights: public HybridT3HighlightsSpec {
33
+ * public:
34
+ * HybridT3Highlights(...): HybridObject(TAG) { ... }
35
+ * // ...
36
+ * };
37
+ * ```
38
+ */
39
+ class HybridT3HighlightsSpec: public virtual HybridObject {
40
+ public:
41
+ // Constructor
42
+ explicit HybridT3HighlightsSpec(): HybridObject(TAG) { }
43
+
44
+ // Destructor
45
+ ~HybridT3HighlightsSpec() override = default;
46
+
47
+ public:
48
+ // Properties
49
+
50
+
51
+ public:
52
+ // Methods
53
+ virtual std::optional<std::shared_ptr<ArrayBuffer>> tokenize(SourceText source, double language) = 0;
54
+
55
+ protected:
56
+ // Hybrid Setup
57
+ void loadHybridMethods() override;
58
+
59
+ protected:
60
+ // Tag for logging
61
+ static constexpr auto TAG = "T3Highlights";
62
+ };
63
+
64
+} // namespace margelo::nitro::t3highlights
apps/mobile/modules/t3-highlights/package.json
+3
-11
@@ -2,16 +2,8 @@
2
"name": "@t3tools/mobile-highlights-native",
3
"version": "0.0.0",
4
"private": true,
5
- "expo-module": {
6
- "ios": {
7
- "modules": [
8
- "T3HighlightsModule"
9
- ]
10
- },
11
- "android": {
12
- "modules": [
13
- "expo.modules.t3highlights.T3HighlightsModule"
14
- ]
15
- }
5
+ "types": "./src/T3Highlights.nitro.ts",
6
+ "peerDependencies": {
7
+ "react-native-nitro-modules": "*"
8
}
9
}
apps/mobile/modules/t3-highlights/src/T3Highlights.nitro.ts
new
+13
@@ -0,0 +1,13 @@
1
+import type { CustomType, HybridObject } from "react-native-nitro-modules";
2
+
3
+// Converted by cpp/SourceText.hpp, which reads the string without Hermes' slow UTF-16 to UTF-8 path.
4
+type SourceText = CustomType<string, "SourceText", { include: "SourceText.hpp" }>;
5
+
6
+export interface T3Highlights extends HybridObject<{ ios: "c++"; android: "c++" }> {
7
+ /**
8
+ * Lexes `source` with @pierre/highlights language `language`. Returns
9
+ * `(endUtf16, tokenId)` uint32 pairs, where token ID 0xffffffff ends a line,
10
+ * or undefined when the lexer could not run.
11
+ */
12
+ tokenize(source: SourceText, language: number): ArrayBuffer | undefined;
13
+}
apps/mobile/modules/t3-highlights/tests/run.sh
new
+8
@@ -0,0 +1,8 @@
1
+#!/bin/bash
2
+set -euo pipefail
3
+module_dir=$(cd "$(dirname "$0")/.." && pwd)
4
+output=$(mktemp -d)
5
+trap 'rm -rf "$output"' EXIT
6
+c++ -std=c++20 -O2 -Wall -Wextra -I "$module_dir/cpp" \
7
+ "$module_dir/cpp/Utf16ToUtf8.cpp" "$module_dir/tests/utf16-to-utf8.cpp" -o "$output/utf16-to-utf8"
8
+"$output/utf16-to-utf8"
apps/mobile/modules/t3-highlights/tests/utf16-to-utf8.cpp
new
+94
@@ -0,0 +1,94 @@
1
+#include <cstdint>
2
+#include <cstdio>
3
+#include <random>
4
+#include <string>
5
+
6
+#include "Utf16ToUtf8.hpp"
7
+
8
+using margelo::nitro::t3highlights::appendUtf16AsUtf8;
9
+
10
+static int failures = 0;
11
+
12
+static std::string encode(const std::u16string& source, std::string out = {}) {
13
+ appendUtf16AsUtf8(out, source.data(), source.size());
14
+ return out;
15
+}
16
+
17
+static void expectBytes(const char* name, const std::string& actual, const std::string& expected) {
18
+ if (actual == expected) return;
19
+ failures++;
20
+ std::fprintf(stderr, "%s: expected", name);
21
+ for (unsigned char byte : expected) std::fprintf(stderr, " %02x", byte);
22
+ std::fprintf(stderr, ", got");
23
+ for (unsigned char byte : actual) std::fprintf(stderr, " %02x", byte);
24
+ std::fprintf(stderr, "\n");
25
+}
26
+
27
+// One code unit or valid pair at a time, as a slow reference.
28
+static std::string reference(const std::u16string& source) {
29
+ std::string out;
30
+ for (size_t at = 0; at < source.size(); at++) {
31
+ uint32_t point = source[at];
32
+ if (point >= 0xd800 && point <= 0xdbff && at + 1 < source.size() && source[at + 1] >= 0xdc00 &&
33
+ source[at + 1] <= 0xdfff) {
34
+ point = 0x10000 + ((point - 0xd800) << 10) + (source[++at] - 0xdc00);
35
+ } else if (point >= 0xd800 && point <= 0xdfff) {
36
+ point = 0xfffd;
37
+ }
38
+ if (point < 0x80) {
39
+ out += static_cast<char>(point);
40
+ } else if (point < 0x800) {
41
+ out += static_cast<char>(0xc0 | (point >> 6));
42
+ out += static_cast<char>(0x80 | (point & 0x3f));
43
+ } else if (point < 0x10000) {
44
+ out += static_cast<char>(0xe0 | (point >> 12));
45
+ out += static_cast<char>(0x80 | ((point >> 6) & 0x3f));
46
+ out += static_cast<char>(0x80 | (point & 0x3f));
47
+ } else {
48
+ out += static_cast<char>(0xf0 | (point >> 18));
49
+ out += static_cast<char>(0x80 | ((point >> 12) & 0x3f));
50
+ out += static_cast<char>(0x80 | ((point >> 6) & 0x3f));
51
+ out += static_cast<char>(0x80 | (point & 0x3f));
52
+ }
53
+ }
54
+ return out;
55
+}
56
+
57
+int main() {
58
+ expectBytes("empty", encode(u""), "");
59
+ expectBytes("nul", encode(std::u16string(u"a\0b", 3)), std::string("a\0b", 3));
60
+ expectBytes("two-byte", encode(u"\u00e9\u07ff"), "\xc3\xa9\xdf\xbf");
61
+ expectBytes("three-byte", encode(u"\u0800\u65e5\uffff"), "\xe0\xa0\x80\xe6\x97\xa5\xef\xbf\xbf");
62
+ expectBytes("pair", encode(u"\U0001F389"), "\xf0\x9f\x8e\x89");
63
+ expectBytes("lone high at end", encode(u"a\xd83c"), "a\xef\xbf\xbd");
64
+ expectBytes("lone high before text", encode(u"\xd83cz"), "\xef\xbf\xbdz");
65
+ expectBytes("high high low", encode(u"\xd83c\xd83c\xdf89"), "\xef\xbf\xbd\xf0\x9f\x8e\x89");
66
+ expectBytes("lone low", encode(u"\xdf89"), "\xef\xbf\xbd");
67
+ expectBytes("appends", encode(u"\u00e9", "ok "), "ok \xc3\xa9");
68
+ // A high surrogate at the end of the input must not pair with the unit after it.
69
+ const std::u16string pair = u"\U0001F389";
70
+ std::string split;
71
+ appendUtf16AsUtf8(split, pair.data(), 1);
72
+ expectBytes("high at input end", split, "\xef\xbf\xbd");
73
+
74
+ // Runs either side of the four-unit ASCII fast path, with a non-ASCII unit at every offset.
75
+ for (size_t length = 0; length <= 12; length++) {
76
+ for (size_t at = 0; at <= length; at++) {
77
+ std::u16string source(length, u'x');
78
+ if (at < length) source[at] = 0x00e9;
79
+ expectBytes("fast path boundary", encode(source), reference(source));
80
+ }
81
+ }
82
+
83
+ const char16_t pool[] = {u'a', u'\n', 0x7f, 0x80, 0x7ff, 0x800, 0xd7ff, 0xd800, 0xdbff, 0xdc00, 0xdfff, 0xe000, 0xffff};
84
+ std::mt19937 random(7);
85
+ for (int run = 0; run < 20000; run++) {
86
+ std::u16string source(random() % 24, u' ');
87
+ for (auto& unit : source) unit = pool[random() % (sizeof(pool) / sizeof(pool[0]))];
88
+ expectBytes("random", encode(source), reference(source));
89
+ }
90
+
91
+ if (failures > 0) return 1;
92
+ std::printf("utf16-to-utf8: ok\n");
93
+ return 0;
94
+}
apps/mobile/modules/t3-highlights/tests/utf16-to-utf8.test.ts
new
+14
@@ -0,0 +1,14 @@
1
+import * as NodeChildProcess from "node:child_process";
2
+import * as NodeURL from "node:url";
3
+import { it } from "vite-plus/test";
4
+
5
+// The converter is C++, so its test compiles and runs on the host.
6
+it("transcodes UTF-16 the way jsi::String::utf8 does", () => {
7
+ NodeChildProcess.execFileSync(
8
+ "bash",
9
+ [NodeURL.fileURLToPath(new URL("./run.sh", import.meta.url))],
10
+ {
11
+ stdio: "pipe",
12
+ },
13
+ );
14
+});
apps/mobile/package.json
+2
@@ -46,6 +46,7 @@
46
"measure-renders": "node scripts/measure-renders.mts",
47
"generate": "node --disable-warning=MODULE_TYPELESS_PACKAGE_JSON scripts/generate-uniwind-themes.mts",
48
"generate:highlights": "node scripts/generate-highlights.mts",
49
+ "generate:nitro": "cd modules/t3-highlights && nitrogen",
50
"eas-build-on-success": "node scripts/upload-sourcemaps.mts build",
51
"test": "vp test run --project unit --project react && T3_MOBILE_TEST_PLATFORM=android vp test run --project react src/components/FilePreviewModal.test.tsx",
52
"typecheck": "effect-tsgo tsrs --noEmit"
@@ -150,6 +151,7 @@
151
"aws4fetch": "1.0.20",
152
"babel-preset-expo": "~58.0.17",
153
"jsdom": "^30.0.1",
154
+ "nitrogen": "0.37.1",
155
"tailwindcss": "^4.0.0"
156
},
157
"enriched-markdown": {
apps/mobile/src/features/diffs/nativeReviewDiffHighlighter.test.ts
+12
-9
@@ -13,18 +13,21 @@ const tokenization = vi.hoisted(() => ({
13
afterCall: undefined as (() => void) | undefined,
14
}));
15
16
-vi.mock("expo", async () => {
16
+vi.mock("react-native-nitro-modules", async () => {
17
const { createWasmHighlightsModule } = await import("../../lib/highlights.testing");
18
const module = createWasmHighlightsModule();
19
return {
20
- requireOptionalNativeModule: () => ({
21
- tokenize(code: string, language: number) {
22
- tokenization.calls.push(code);
23
- const records = module.tokenize(code, language);
24
- tokenization.afterCall?.();
25
- return records;
26
- },
27
- }),
20
+ NitroModules: {
21
+ hasHybridObject: () => true,
22
+ createHybridObject: () => ({
23
+ tokenize(code: string, language: number) {
24
+ tokenization.calls.push(code);
25
+ const records = module.tokenize(code, language);
26
+ tokenization.afterCall?.();
27
+ return records;
28
+ },
29
+ }),
30
+ },
31
};
32
});
33
apps/mobile/src/features/pull-requests/pullRequestDiffHighlightFiles.test.ts
+3
-2
@@ -14,9 +14,10 @@ import {
14
createPullRequestDiffHighlightFiles,
15
} from "./pullRequestDiffHighlightFiles";
16
17
-vi.mock("expo", async () => {
17
+vi.mock("react-native-nitro-modules", async () => {
18
const { createWasmHighlightsModule } = await import("../../lib/highlights.testing");
19
- return { requireOptionalNativeModule: () => createWasmHighlightsModule() };
19
+ const highlights = createWasmHighlightsModule();
20
+ return { NitroModules: { hasHybridObject: () => true, createHybridObject: () => highlights } };
21
});
22
23
const target = {
apps/mobile/src/lib/highlights.test.ts
+2
-2
@@ -5,10 +5,10 @@ import { describe, expect, it, vi } from "vite-plus/test";
5
import { resolveHighlightLanguage } from "./highlightLanguage";
6
import { highlightCode, tokenizeCode } from "./highlights";
7
8
-vi.mock("expo", async () => {
8
+vi.mock("react-native-nitro-modules", async () => {
9
const { createWasmHighlightsModule } = await import("./highlights.testing");
10
const highlights = createWasmHighlightsModule();
11
- return { requireOptionalNativeModule: () => highlights };
11
+ return { NitroModules: { hasHybridObject: () => true, createHybridObject: () => highlights } };
12
});
13
14
describe("highlightCode", () => {
apps/mobile/src/lib/highlights.testing.ts
+2
-2
@@ -7,8 +7,8 @@ interface LineRecordHighlighter {
7
}
8
9
/**
10
- * Stands in for the T3Highlights native module with the WebAssembly build it is
11
- * compiled from. Mock `expo`'s `requireOptionalNativeModule` with this.
10
+ * Stands in for the T3Highlights hybrid object with the WebAssembly build it is
11
+ * compiled from. Return it from a mocked `NitroModules.createHybridObject`.
12
*/
13
export function createWasmHighlightsModule() {
14
const highlighter = createHighlighter() as unknown as LineRecordHighlighter;
apps/mobile/src/lib/highlights.ts
+5
-4
@@ -1,4 +1,5 @@
1
-import { requireOptionalNativeModule } from "expo";
1
+import type { T3Highlights } from "@t3tools/mobile-highlights-native";
2
+import { NitroModules } from "react-native-nitro-modules";
3
4
import { highlightLanguageId } from "./highlightLanguage";
5
import { HIGHLIGHTS_THEME_STYLES } from "./highlights.generated";
@@ -20,9 +21,9 @@ const MAX_HIGHLIGHTED_LINE_LENGTH = 1_000;
21
22
// @pierre/highlights compiled to native code (modules/t3-highlights). Missing
23
// from binaries built before it shipped, where code renders as plain text.
23
-const nativeHighlights = requireOptionalNativeModule<{
24
- tokenize(source: string, language: number): ArrayBuffer | null;
25
-}>("T3Highlights");
24
+const nativeHighlights = NitroModules.hasHybridObject("T3Highlights")
25
+ ? NitroModules.createHybridObject<T3Highlights>("T3Highlights")
26
+ : undefined;
27
28
export interface TokenizedCode {
29
/** Retained UTF-16 source, compact native records, and line indexes. */
knip.jsonc
+4
-1
@@ -119,11 +119,14 @@
119
// Fonts are configured as asset paths; Expo autolinks the native packages.
120
"ignoreDependencies": [
121
"@expo-google-fonts/dm-sans",
122
- "@t3tools/mobile-highlights-native",
122
"@t3tools/mobile-review-diff-native",
123
"@t3tools/mobile-terminal-native",
124
],
125
},
126
+ "apps/mobile/modules/t3-highlights": {
127
+ // Nitrogen discovers HybridObject specifications by filename.
128
+ "entry": ["src/*.nitro.ts!"],
129
+ },
130
"apps/mobile/modules/t3-source-text": {
131
// React Native codegen discovers component specifications by filename.
132
"entry": ["src/*NativeComponent.ts!"],
pnpm-lock.yaml
+46
-2
@@ -583,7 +583,7 @@ importers:
583
version: link:../../packages/contracts
584
'@t3tools/mobile-highlights-native':
585
specifier: file:./modules/t3-highlights
586
- version: file:apps/mobile/modules/t3-highlights
586
+ version: file:apps/mobile/modules/t3-highlights(react-native-nitro-modules@0.37.1(react-native@0.88.0(@babel/core@7.29.7(supports-color@8.1.1))(@react-native/metro-config@0.88.0(@babel/core@7.29.7(supports-color@8.1.1))(bufferutil@4.1.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(@types/react@19.3.0)(bufferutil@4.1.0)(react@19.3.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(react@19.3.0))
587
'@t3tools/mobile-review-diff-native':
588
specifier: file:./modules/t3-review-diff
589
version: file:apps/mobile/modules/t3-review-diff
@@ -816,6 +816,9 @@ importers:
816
jsdom:
817
specifier: ^30.0.1
818
version: 30.1.0(@noble/hashes@1.8.0)
819
+ nitrogen:
820
+ specifier: 0.37.1
821
+ version: 0.37.1(react-native@0.88.0(@babel/core@7.29.7(supports-color@8.1.1))(@react-native/metro-config@0.88.0(@babel/core@7.29.7(supports-color@8.1.1))(bufferutil@4.1.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(@types/react@19.3.0)(bufferutil@4.1.0)(react@19.3.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(react@19.3.0)
822
tailwindcss:
823
specifier: 4.3.3
824
version: 4.3.3
@@ -6520,6 +6523,8 @@ packages:
6523
6524
'@t3tools/mobile-highlights-native@file:apps/mobile/modules/t3-highlights':
6525
resolution: {directory: apps/mobile/modules/t3-highlights, type: directory}
6526
+ peerDependencies:
6527
+ react-native-nitro-modules: '*'
6528
6529
'@t3tools/mobile-review-diff-native@file:apps/mobile/modules/t3-review-diff':
6530
resolution: {directory: apps/mobile/modules/t3-review-diff, type: directory}
@@ -6918,6 +6923,9 @@ packages:
6923
cpu: [x64]
6924
os: [win32]
6925
6926
+ '@ts-morph/common@0.29.0':
6927
+ resolution: {integrity: sha512-35oUmphHbJvQ/+UTwFNme/t2p3FoKiGJ5auTjjpNTop2dyREspirjMy82PLSC1pnDJ8ah1GU98hwpVt64YXQsg==}
6928
+
6929
'@tweenjs/tween.js@23.1.3':
6930
resolution: {integrity: sha512-vJmvvwFxYuGnF2axRtPYocag6Clbb5YS7kLL+SO/TeVFzHqDIWrNKYtcsPMibjDx9O+bu+psAy9NKfWklassUA==}
6931
@@ -7999,6 +8007,9 @@ packages:
8007
engines: {node: '>=20'}
8008
hasBin: true
8009
8010
+ code-block-writer@13.0.3:
8011
+ resolution: {integrity: sha512-Oofo0pq3IKnsFtuHqSF7TqBfr71aeyZDVJ0HpmqB7FBM2qEigL0iPONSCZSO9pE9dZTAxANe5XHG9Uy0YMv8cg==}
8012
+
8013
color-convert@1.9.3:
8014
resolution: {integrity: sha512-QfAUtd+vFdAtFQcC8CCyYt1fYWxSqAiK2cSD6zDB8N3cpsEBAvRxp9zOGg6G/SHHJYAT88/az/IuDGALsNVbGg==}
8015
@@ -10299,6 +10310,10 @@ packages:
10310
resolution: {integrity: sha512-WmmLty1YWwJl9yZi77v2dVIV6X2kuYV8YYBI/G3LWGKdGHmHUvL1z7FW0iDvEvGAwNEoc5x1tOOOyDnf5jJw/w==}
10311
engines: {node: '>= 10'}
10312
10313
+ nitrogen@0.37.1:
10314
+ resolution: {integrity: sha512-RMK1n8xV7B38JorfLCFexgPEDp7/4CnM/AR0R3UfikufXo8qB8TecgtY+sW0xjOjZJYwW5bJJI/kyZjusYk4JA==}
10315
+ hasBin: true
10316
+
10317
nlcst-to-string@4.0.0:
10318
resolution: {integrity: sha512-YKLBCcUYKAg0FNlOBT6aI91qFmSiFKiluk655WzPF+DDMA02qIyy8uiRqI8QXtcFpEvll12LpL5MXqEmAZ+dcA==}
10319
@@ -11741,6 +11756,9 @@ packages:
11756
ts-algebra@2.0.0:
11757
resolution: {integrity: sha512-FPAhNPFMrkwz76P7cdjdmiShwMynZYN6SgOujD1urY4oNm80Ou9oMdmbR45LotcKOXoy7wSmHkRFE6Mxbrhefw==}
11758
11759
+ ts-morph@28.0.0:
11760
+ resolution: {integrity: sha512-Wp3tnZ2bzwxyTZMtgWVzXDfm7lB1Drz+y9DmmYH/L702PQhPyVrp3pkou3yIz4qjS14GY9kcpmLiOOMvl8oG1g==}
11761
+
11762
tslib@2.8.1:
11763
resolution: {integrity: sha512-oJFu94HQb+KVduSUQL7wnpmqnfmLsOA/nAh6b6EH0wCEoK0/mPeXU6c3wKDV83MkOuHPRHtSXKKU99IBazS/2w==}
11764
@@ -16081,7 +16099,9 @@ snapshots:
16099
dependencies:
16100
defer-to-connect: 2.0.1
16101
16084
- '@t3tools/mobile-highlights-native@file:apps/mobile/modules/t3-highlights': {}
16102
+ '@t3tools/mobile-highlights-native@file:apps/mobile/modules/t3-highlights(react-native-nitro-modules@0.37.1(react-native@0.88.0(@babel/core@7.29.7(supports-color@8.1.1))(@react-native/metro-config@0.88.0(@babel/core@7.29.7(supports-color@8.1.1))(bufferutil@4.1.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(@types/react@19.3.0)(bufferutil@4.1.0)(react@19.3.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(react@19.3.0))':
16103
+ dependencies:
16104
+ react-native-nitro-modules: 0.37.1(react-native@0.88.0(@babel/core@7.29.7(supports-color@8.1.1))(@react-native/metro-config@0.88.0(@babel/core@7.29.7(supports-color@8.1.1))(bufferutil@4.1.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(@types/react@19.3.0)(bufferutil@4.1.0)(react@19.3.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(react@19.3.0)
16105
16106
'@t3tools/mobile-review-diff-native@file:apps/mobile/modules/t3-review-diff': {}
16107
@@ -16481,6 +16501,12 @@ snapshots:
16501
'@transcribe-cpp/win32-x64-cpu-vulkan@0.4.1':
16502
optional: true
16503
16504
+ '@ts-morph/common@0.29.0':
16505
+ dependencies:
16506
+ minimatch: 10.2.5
16507
+ path-browserify: 1.0.1
16508
+ tinyglobby: 0.2.17
16509
+
16510
'@tweenjs/tween.js@23.1.3': {}
16511
16512
'@tybys/wasm-util@0.10.4':
@@ -17724,6 +17750,8 @@ snapshots:
17750
17751
cn@0.3.2: {}
17752
17753
+ code-block-writer@13.0.3: {}
17754
+
17755
color-convert@1.9.3:
17756
dependencies:
17757
color-name: 1.1.3
@@ -20393,6 +20421,17 @@ snapshots:
20421
20422
neotraverse@1.0.1: {}
20423
20424
+ nitrogen@0.37.1(react-native@0.88.0(@babel/core@7.29.7(supports-color@8.1.1))(@react-native/metro-config@0.88.0(@babel/core@7.29.7(supports-color@8.1.1))(bufferutil@4.1.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(@types/react@19.3.0)(bufferutil@4.1.0)(react@19.3.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(react@19.3.0):
20425
+ dependencies:
20426
+ chalk: 5.6.2
20427
+ react-native-nitro-modules: 0.37.1(react-native@0.88.0(@babel/core@7.29.7(supports-color@8.1.1))(@react-native/metro-config@0.88.0(@babel/core@7.29.7(supports-color@8.1.1))(bufferutil@4.1.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(@types/react@19.3.0)(bufferutil@4.1.0)(react@19.3.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(react@19.3.0)
20428
+ ts-morph: 28.0.0
20429
+ yargs: 18.1.0
20430
+ zod: 4.6.5
20431
+ transitivePeerDependencies:
20432
+ - react
20433
+ - react-native
20434
+
20435
nlcst-to-string@4.0.0:
20436
dependencies:
20437
'@types/nlcst': 2.0.3
@@ -22211,6 +22250,11 @@ snapshots:
22250
22251
ts-algebra@2.0.0: {}
22252
22253
+ ts-morph@28.0.0:
22254
+ dependencies:
22255
+ '@ts-morph/common': 0.29.0
22256
+ code-block-writer: 13.0.3
22257
+
22258
tslib@2.8.1: {}
22259
22260
type-fest@0.13.1:
scripts/mobile-native-static-check.ts
+1
@@ -99,6 +99,7 @@ const excludedDirectories = new Set([
99
".git",
100
"build",
101
"DerivedData",
102
+ "nitrogen",
103
"node_modules",
104
"Pods",
105
"Vendor",
vite.config.ts
+1
@@ -177,6 +177,7 @@ export default defineConfig({
177
// Vendored and generated C.
178
"apps/mobile/modules/t3-highlights/Vendor/**",
179
"apps/mobile/modules/t3-highlights/cpp/generated/**",
180
+ "apps/mobile/modules/t3-highlights/nitrogen/generated/**",
181
"*.icon/**",
182
],
183
sortPackageJson: {},