react-native-nitro-modules 0.5.0 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/NitroModules.podspec +3 -1
- package/README.md +33 -14
- package/android/gradle.properties +5 -5
- package/android/src/main/cpp/JNIOnLoad.cpp +8 -0
- package/android/src/main/cpp/core/ByteBufferArrayBuffer.hpp +47 -0
- package/android/src/main/cpp/core/JAnyMap.hpp +193 -0
- package/android/src/main/cpp/core/JAnyValue.hpp +191 -0
- package/android/src/main/cpp/core/JArrayBuffer.hpp +119 -0
- package/android/src/main/cpp/core/JHybridObject.hpp +11 -4
- package/android/src/main/cpp/core/JPromise.hpp +88 -0
- package/android/src/main/cpp/platform/NitroLogger.cpp +36 -0
- package/android/src/main/cpp/registry/JHybridObjectInitializer.hpp +1 -1
- package/android/src/main/cpp/registry/JHybridObjectRegistry.cpp +1 -1
- package/android/src/main/cpp/registry/JHybridObjectRegistry.hpp +1 -1
- package/android/src/main/cpp/utils/{JNISharedPtr.h → JNISharedPtr.hpp} +13 -1
- package/android/src/main/java/com/margelo/nitro/JNIOnLoad.java +27 -0
- package/android/src/main/java/com/margelo/nitro/NitroModulesPackage.java +1 -9
- package/android/src/main/java/com/margelo/nitro/core/AnyMap.kt +61 -0
- package/android/src/main/java/com/margelo/nitro/core/AnyValue.kt +153 -0
- package/android/src/main/java/com/margelo/nitro/core/ArrayBuffer.kt +84 -0
- package/android/src/main/java/com/margelo/nitro/{HybridObject.kt → core/HybridObject.kt} +12 -15
- package/android/src/main/java/com/margelo/nitro/{HybridObjectInitializer.java → core/HybridObjectInitializer.java} +1 -1
- package/android/src/main/java/com/margelo/nitro/{HybridObjectRegistry.java → core/HybridObjectRegistry.java} +6 -4
- package/android/src/main/java/com/margelo/nitro/core/Promise.kt +115 -0
- package/cpp/core/AnyMap.hpp +2 -2
- package/cpp/core/ArrayBuffer.cpp +5 -5
- package/cpp/core/ArrayBuffer.hpp +7 -9
- package/cpp/core/HybridFunction.hpp +119 -45
- package/cpp/core/HybridObject.cpp +17 -6
- package/cpp/core/HybridObject.hpp +32 -8
- package/cpp/jsi/JSICache.cpp +5 -5
- package/cpp/jsi/JSICache.hpp +1 -3
- package/cpp/jsi/JSIConverter+AnyMap.hpp +2 -2
- package/cpp/jsi/JSIConverter+ArrayBuffer.hpp +2 -2
- package/cpp/jsi/JSIConverter+Function.hpp +6 -5
- package/cpp/jsi/JSIConverter+HybridObject.hpp +6 -5
- package/cpp/jsi/JSIConverter+Optional.hpp +1 -1
- package/cpp/jsi/JSIConverter+Promise.hpp +4 -3
- package/cpp/jsi/JSIConverter+Tuple.hpp +1 -1
- package/cpp/jsi/JSIConverter+UnorderedMap.hpp +1 -1
- package/cpp/jsi/JSIConverter+Variant.hpp +1 -3
- package/cpp/jsi/JSIConverter+Vector.hpp +1 -1
- package/cpp/jsi/JSIConverter.hpp +11 -11
- package/cpp/jsi/JSPromise.cpp +2 -2
- package/cpp/platform/NitroLogger.hpp +67 -0
- package/cpp/prototype/HybridObjectPrototype.cpp +2 -2
- package/cpp/prototype/Prototype.hpp +43 -22
- package/cpp/registry/HybridObjectRegistry.cpp +4 -4
- package/cpp/threading/Dispatcher.cpp +4 -3
- package/cpp/threading/ThreadPool.cpp +2 -2
- package/cpp/turbomodule/NativeNitroModules.cpp +26 -8
- package/cpp/turbomodule/NativeNitroModules.h +1 -0
- package/cpp/turbomodule/NativeNitroModules.hpp +2 -0
- package/cpp/utils/TypeInfo.hpp +14 -0
- package/ios/core/AnyMapHolder.hpp +91 -0
- package/ios/core/AnyMapHolder.swift +316 -0
- package/ios/core/ArrayBufferHolder.hpp +6 -4
- package/ios/core/ArrayBufferHolder.swift +6 -5
- package/ios/core/HybridContext.hpp +3 -3
- package/ios/core/Promise.swift +20 -0
- package/ios/core/PromiseHolder.hpp +15 -11
- package/ios/platform/NitroLogger.mm +36 -0
- package/ios/platform/ThreadUtils.cpp +1 -1
- package/ios/turbomodule/NitroModuleOnLoad.mm +2 -1
- package/ios/utils/SwiftClosure.hpp +63 -0
- package/ios/utils/SwiftClosure.swift +58 -0
- package/lib/AnyMap.d.ts +3 -0
- package/lib/HybridObject.d.ts +15 -0
- package/lib/NativeNitroModules.d.ts +2 -0
- package/lib/NitroModules.d.ts +18 -0
- package/lib/NitroModules.js +24 -0
- package/lib/commonjs/NativeNitroModules.js.map +1 -1
- package/lib/commonjs/NitroModules.js +24 -0
- package/lib/commonjs/NitroModules.js.map +1 -1
- package/lib/commonjs/package.json +1 -0
- package/lib/module/AnyMap.js +1 -1
- package/lib/module/HybridObject.js +1 -1
- package/lib/module/ModuleNotFoundError.js +2 -0
- package/lib/module/ModuleNotFoundError.js.map +1 -1
- package/lib/module/NativeNitroModules.js +2 -0
- package/lib/module/NativeNitroModules.js.map +1 -1
- package/lib/module/NativeNitroModules.web.js +2 -0
- package/lib/module/NativeNitroModules.web.js.map +1 -1
- package/lib/module/NitroModules.js +26 -0
- package/lib/module/NitroModules.js.map +1 -1
- package/lib/module/index.js +2 -0
- package/lib/module/index.js.map +1 -1
- package/lib/module/package.json +1 -0
- package/lib/tsconfig.tsbuildinfo +1 -1
- package/lib/typescript/AnyMap.d.ts +20 -0
- package/lib/typescript/AnyMap.d.ts.map +1 -0
- package/lib/typescript/HybridObject.d.ts +98 -0
- package/lib/typescript/HybridObject.d.ts.map +1 -0
- package/lib/typescript/ModuleNotFoundError.d.ts.map +1 -0
- package/lib/typescript/NativeNitroModules.d.ts +17 -0
- package/lib/typescript/NativeNitroModules.d.ts.map +1 -0
- package/lib/typescript/NativeNitroModules.web.d.ts.map +1 -0
- package/lib/typescript/NitroModules.d.ts +44 -0
- package/lib/typescript/NitroModules.d.ts.map +1 -0
- package/lib/typescript/__tests__/index.test.d.ts.map +1 -0
- package/lib/typescript/index.d.ts.map +1 -0
- package/package.json +13 -12
- package/src/AnyMap.ts +3 -0
- package/src/HybridObject.ts +15 -0
- package/src/NativeNitroModules.ts +5 -0
- package/src/NitroModules.ts +24 -0
- package/android/src/main/cpp/core/JHybridObject.cpp +0 -8
- package/cpp/templates/IsInPack.hpp +0 -21
- package/cpp/utils/NitroLogger.hpp +0 -58
- package/ios/utils/ClosureWrapper.swift +0 -45
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-native-nitro-modules",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"description": "Insanely fast native C++, Swift or Kotlin modules with a statically compiled binding layer to JSI.",
|
|
5
5
|
"main": "lib/index",
|
|
6
6
|
"module": "lib/index",
|
|
@@ -28,6 +28,7 @@
|
|
|
28
28
|
"typecheck": "tsc --noEmit",
|
|
29
29
|
"clean": "del-cli android/build node_modules/**/android/build lib",
|
|
30
30
|
"lint": "eslint \"**/*.{js,ts,tsx}\" --fix",
|
|
31
|
+
"lint-ci": "eslint \"**/*.{js,ts,tsx}\" -f @jamesacarr/github-actions",
|
|
31
32
|
"release": "bob build && release-it",
|
|
32
33
|
"typescript": "tsc --noEmit false"
|
|
33
34
|
},
|
|
@@ -51,36 +52,33 @@
|
|
|
51
52
|
],
|
|
52
53
|
"repository": {
|
|
53
54
|
"type": "git",
|
|
54
|
-
"url": "git+https://github.com/mrousavy/
|
|
55
|
+
"url": "git+https://github.com/mrousavy/nitro.git"
|
|
55
56
|
},
|
|
56
57
|
"author": "Marc Rousavy <me@mrousavy.com> (https://github.com/mrousavy)",
|
|
57
58
|
"license": "MIT",
|
|
58
59
|
"bugs": {
|
|
59
|
-
"url": "https://github.com/mrousavy/
|
|
60
|
+
"url": "https://github.com/mrousavy/nitro/issues"
|
|
60
61
|
},
|
|
61
|
-
"homepage": "https://github.com/mrousavy/
|
|
62
|
+
"homepage": "https://github.com/mrousavy/nitro#readme",
|
|
62
63
|
"publishConfig": {
|
|
63
64
|
"registry": "https://registry.npmjs.org/"
|
|
64
65
|
},
|
|
65
66
|
"devDependencies": {
|
|
66
|
-
"@react-native/eslint-config": "^0.
|
|
67
|
+
"@react-native/eslint-config": "^0.75.2",
|
|
67
68
|
"@types/jest": "^29.5.12",
|
|
68
|
-
"@types/react": "^18.3.
|
|
69
|
+
"@types/react": "^18.3.4",
|
|
69
70
|
"del-cli": "^5.1.0",
|
|
70
71
|
"eslint": "^8.57.0",
|
|
71
72
|
"eslint-config-prettier": "^9.1.0",
|
|
72
73
|
"eslint-plugin-prettier": "^5.2.1",
|
|
73
74
|
"jest": "^29.7.0",
|
|
74
75
|
"prettier": "^3.3.3",
|
|
75
|
-
"react": "18.3.1",
|
|
76
|
-
"react-native": "0.
|
|
77
|
-
"react-native-builder-bob": "^0.
|
|
76
|
+
"react": "^18.3.1",
|
|
77
|
+
"react-native": "^0.75.2",
|
|
78
|
+
"react-native-builder-bob": "^0.30.0",
|
|
78
79
|
"release-it": "^17.6.0",
|
|
79
80
|
"typescript": "^5.5.4"
|
|
80
81
|
},
|
|
81
|
-
"resolutions": {
|
|
82
|
-
"@types/react": "^18.2.44"
|
|
83
|
-
},
|
|
84
82
|
"peerDependencies": {
|
|
85
83
|
"react": "*",
|
|
86
84
|
"react-native": "*"
|
|
@@ -108,6 +106,9 @@
|
|
|
108
106
|
"@react-native",
|
|
109
107
|
"prettier"
|
|
110
108
|
],
|
|
109
|
+
"plugins": [
|
|
110
|
+
"prettier"
|
|
111
|
+
],
|
|
111
112
|
"rules": {
|
|
112
113
|
"prettier/prettier": [
|
|
113
114
|
"warn",
|
package/src/AnyMap.ts
CHANGED
|
@@ -18,5 +18,8 @@ export type ValueType =
|
|
|
18
18
|
* - Objects of primitives (`Record<string, ValueType>`)
|
|
19
19
|
* - Arrays of arrays or objects
|
|
20
20
|
* - Objects of arrays or objects
|
|
21
|
+
*
|
|
22
|
+
* @note It is recommended to always use typed `interface`s instead of `AnyMap` for
|
|
23
|
+
* both type safety, as well as better performance.
|
|
21
24
|
*/
|
|
22
25
|
export type AnyMap = Record<string, ValueType>
|
package/src/HybridObject.ts
CHANGED
|
@@ -80,4 +80,19 @@ export interface HybridObject<Platforms extends PlatformSpec = {}> {
|
|
|
80
80
|
* ```
|
|
81
81
|
*/
|
|
82
82
|
equals(other: HybridObject<Platforms>): boolean
|
|
83
|
+
/**
|
|
84
|
+
* Disposes any resources this {@linkcode HybridObject} might hold natively,
|
|
85
|
+
* and finally releases this {@linkcode HybridObject}'s `NativeState`.
|
|
86
|
+
*
|
|
87
|
+
* After calling {@linkcode dispose()}, this object can no longer be used.
|
|
88
|
+
*
|
|
89
|
+
* Eagerly disposing a {@linkcode HybridObject} could be beneficial for a queue-/handler-architecture
|
|
90
|
+
* where a bunch of Hybrid Objects are allocated, and later deallocated once a callback (e.g. a render function)
|
|
91
|
+
* completes.
|
|
92
|
+
*
|
|
93
|
+
* @note It is **NOT** required to call {@linkcode dispose()} manually, as the JavaScript
|
|
94
|
+
* Garbage Collector automatically disposes and releases any resources when needed.
|
|
95
|
+
* It is purely optional to eagerly-, and manually-, call {@linkcode dispose()} here - **use with caution!**
|
|
96
|
+
*/
|
|
97
|
+
dispose(): void
|
|
83
98
|
}
|
|
@@ -4,10 +4,15 @@ import type { UnsafeObject } from 'react-native/Libraries/Types/CodegenTypes'
|
|
|
4
4
|
import { ModuleNotFoundError } from './ModuleNotFoundError'
|
|
5
5
|
|
|
6
6
|
export interface Spec extends TurboModule {
|
|
7
|
+
// Set up
|
|
7
8
|
install(): void
|
|
9
|
+
// Hybrid Objects stuff
|
|
8
10
|
createHybridObject(name: string, args?: UnsafeObject): UnsafeObject
|
|
9
11
|
hasHybridObject(name: string): boolean
|
|
10
12
|
getAllHybridObjectNames(): string[]
|
|
13
|
+
// JSI Helpers
|
|
14
|
+
hasNativeState(obj: UnsafeObject): boolean
|
|
15
|
+
removeNativeState(obj: UnsafeObject): void
|
|
11
16
|
}
|
|
12
17
|
|
|
13
18
|
let turboModule: Spec | undefined
|
package/src/NitroModules.ts
CHANGED
|
@@ -41,4 +41,28 @@ export const NitroModules = {
|
|
|
41
41
|
const nitro = getNativeNitroModules()
|
|
42
42
|
return nitro.hasHybridObject(name)
|
|
43
43
|
},
|
|
44
|
+
/**
|
|
45
|
+
* Returns whether the given {@linkcode object} has a `NativeState` or not.
|
|
46
|
+
*
|
|
47
|
+
* This can be a quick way to check if an object is a valid {@linkcode HybridObject},
|
|
48
|
+
* and has not yet been disposed.
|
|
49
|
+
* @example
|
|
50
|
+
* ```ts
|
|
51
|
+
* const someObject = NitroModules.createHybridObject<Some>('Some')
|
|
52
|
+
* console.log(NitroModules.hasNativeState(someObject)) // -> true
|
|
53
|
+
* someObject.dispose()
|
|
54
|
+
* console.log(NitroModules.hasNativeState(someObject)) // -> false
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
57
|
+
hasNativeState(object: object): boolean {
|
|
58
|
+
const nitro = getNativeNitroModules()
|
|
59
|
+
return nitro.hasNativeState(object)
|
|
60
|
+
},
|
|
61
|
+
/**
|
|
62
|
+
* Forcefully removes the `NativeState` of the given {@linkcode object}.
|
|
63
|
+
*/
|
|
64
|
+
removeNativeState(object: object): void {
|
|
65
|
+
const nitro = getNativeNitroModules()
|
|
66
|
+
nitro.removeNativeState(object)
|
|
67
|
+
},
|
|
44
68
|
}
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// BorrowingReference.hpp
|
|
3
|
-
// NitroModules
|
|
4
|
-
//
|
|
5
|
-
// Created by Marc Rousavy on 21.06.24.
|
|
6
|
-
//
|
|
7
|
-
|
|
8
|
-
#pragma once
|
|
9
|
-
|
|
10
|
-
#include <type_traits>
|
|
11
|
-
|
|
12
|
-
namespace margelo::nitro {
|
|
13
|
-
|
|
14
|
-
// Returns whether the given type `T` is inside `Types...`
|
|
15
|
-
template <typename T, typename... Types>
|
|
16
|
-
struct is_in_pack : std::disjunction<std::is_same<T, Types>...> {};
|
|
17
|
-
|
|
18
|
-
template <typename T, typename... Types>
|
|
19
|
-
inline constexpr bool is_in_pack_v = is_in_pack<T, Types...>::value;
|
|
20
|
-
|
|
21
|
-
} // namespace margelo::nitro
|
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Created by Marc Rousavy on 05.03.24.
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
#pragma once
|
|
6
|
-
|
|
7
|
-
#include <cstdarg>
|
|
8
|
-
#include <cstdio>
|
|
9
|
-
#include <iostream>
|
|
10
|
-
#include <string>
|
|
11
|
-
#include <vector>
|
|
12
|
-
|
|
13
|
-
namespace margelo::nitro {
|
|
14
|
-
|
|
15
|
-
class Logger final {
|
|
16
|
-
private:
|
|
17
|
-
Logger() = delete;
|
|
18
|
-
|
|
19
|
-
public:
|
|
20
|
-
template <typename... Args>
|
|
21
|
-
static void log(const char* tag, const char* message, Args&&... args) {
|
|
22
|
-
std::string formattedMessage = format(message, std::forward<Args>(args)...);
|
|
23
|
-
std::cout << "[Nitro." << tag << "] " << formattedMessage << std::endl;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
private:
|
|
27
|
-
template <typename... Args>
|
|
28
|
-
static inline std::string format(const char* formatString, Args&&... args) {
|
|
29
|
-
#pragma clang diagnostic push
|
|
30
|
-
#pragma clang diagnostic ignored "-Wformat-security"
|
|
31
|
-
int size_s = std::snprintf(nullptr, 0, formatString, toCString(args)...) + 1; // Extra space for '\0'
|
|
32
|
-
if (size_s <= 0) [[unlikely]] {
|
|
33
|
-
throw std::runtime_error("Failed to format log message \"" + std::string(formatString) + "\"!");
|
|
34
|
-
}
|
|
35
|
-
auto size = static_cast<size_t>(size_s);
|
|
36
|
-
std::vector<char> buf(size);
|
|
37
|
-
std::snprintf(buf.data(), size, formatString, toCString(args)...);
|
|
38
|
-
return std::string(buf.data(), buf.data() + size - 1); // We don't want the '\0' inside
|
|
39
|
-
#pragma clang diagnostic pop
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
private:
|
|
43
|
-
// When the user passes an `std::string`, it will automatically be converted to a `const char*`
|
|
44
|
-
static inline const char* toCString(const std::string& s) {
|
|
45
|
-
return s.c_str();
|
|
46
|
-
}
|
|
47
|
-
// When the user passes a `const char*`, we don't want to implicitly convert to `std::string`, so block that
|
|
48
|
-
static inline const char* toCString(const char* s) {
|
|
49
|
-
return s;
|
|
50
|
-
}
|
|
51
|
-
// When the user passes any other type, just return that directly.
|
|
52
|
-
template <typename T>
|
|
53
|
-
static inline T toCString(T value) {
|
|
54
|
-
return value;
|
|
55
|
-
}
|
|
56
|
-
};
|
|
57
|
-
|
|
58
|
-
} // namespace margelo::nitro
|
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// ClosureWrapper.swift
|
|
3
|
-
// NitroModules
|
|
4
|
-
//
|
|
5
|
-
// Created by Marc Rousavy on 14.08.24.
|
|
6
|
-
//
|
|
7
|
-
|
|
8
|
-
import Foundation
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* Wraps a closure in a Swift class.
|
|
12
|
-
* This can be used to create unmanaged pointers (`void*`) and
|
|
13
|
-
* passed to C-style function pointers via `void* context` parameters.
|
|
14
|
-
*
|
|
15
|
-
* To create a `ClosureWrapper`, use `ClosureWrapper.wrap(...)`.
|
|
16
|
-
*/
|
|
17
|
-
public final class ClosureWrapper {
|
|
18
|
-
private let closure: () -> Void
|
|
19
|
-
|
|
20
|
-
private init(closure: @escaping () -> Void) {
|
|
21
|
-
self.closure = closure
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
private func invoke() {
|
|
25
|
-
closure()
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* Wraps the given Swift closure in a C-style function pointer with `void* context` associated to it.
|
|
30
|
-
* This way it can be passed to C/C++ and called without worrying about context/binding.
|
|
31
|
-
*/
|
|
32
|
-
public static func wrap(closure: @escaping () -> Void) -> (@convention(c) (UnsafeMutableRawPointer?) -> Void, UnsafeMutableRawPointer) {
|
|
33
|
-
// Wrap closure in void*
|
|
34
|
-
let context = Unmanaged.passRetained(ClosureWrapper(closure: closure)).toOpaque()
|
|
35
|
-
// Create C-style Function Pointer
|
|
36
|
-
let cFunc: @convention(c) (UnsafeMutableRawPointer?) -> Void = { context in
|
|
37
|
-
guard let context else { fatalError("Context was null, even though we created one!") }
|
|
38
|
-
// Unwrap context from void* to closure again
|
|
39
|
-
let closure = Unmanaged<ClosureWrapper>.fromOpaque(context).takeRetainedValue()
|
|
40
|
-
// Call it!
|
|
41
|
-
closure.invoke()
|
|
42
|
-
}
|
|
43
|
-
return (cFunc, context)
|
|
44
|
-
}
|
|
45
|
-
}
|