@nativue/core 0.0.0-stage → 0.2.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/LICENSE +21 -0
- package/Package.swift +37 -0
- package/README.md +20 -2
- package/android/build.gradle.kts +74 -0
- package/android/consumer-rules.pro +8 -0
- package/android/src/main/AndroidManifest.xml +4 -0
- package/android/src/main/cpp/CMakeLists.txt +11 -0
- package/android/src/main/cpp/bridge.cpp +395 -0
- package/android/src/main/cpp/jsi/LICENSE +21 -0
- package/android/src/main/cpp/jsi/jsi/hermes-interfaces.h +242 -0
- package/android/src/main/cpp/jsi/jsi/instrumentation.h +132 -0
- package/android/src/main/cpp/jsi/jsi/jsi-inl.h +434 -0
- package/android/src/main/cpp/jsi/jsi/jsi.cpp +1109 -0
- package/android/src/main/cpp/jsi/jsi/jsi.h +2266 -0
- package/android/src/main/java/dev/nativue/AnimatedGraph.kt +238 -0
- package/android/src/main/java/dev/nativue/BoxDrawable.kt +77 -0
- package/android/src/main/java/dev/nativue/Bridge.kt +904 -0
- package/android/src/main/java/dev/nativue/ErrorOverlay.kt +118 -0
- package/android/src/main/java/dev/nativue/Gestures.kt +332 -0
- package/android/src/main/java/dev/nativue/JavaScriptRuntime.kt +47 -0
- package/android/src/main/java/dev/nativue/LayoutAnimation.kt +134 -0
- package/android/src/main/java/dev/nativue/ModalHost.kt +164 -0
- package/android/src/main/java/dev/nativue/NativeNode.kt +156 -0
- package/android/src/main/java/dev/nativue/NativueActivity.kt +76 -0
- package/android/src/main/java/dev/nativue/Navigation.kt +688 -0
- package/android/src/main/java/dev/nativue/Style.kt +282 -0
- package/android/src/main/java/dev/nativue/SvgView.kt +443 -0
- package/android/src/main/java/dev/nativue/Views.kt +484 -0
- package/cli/nativue.mjs +233 -0
- package/cli/run.mjs +194 -0
- package/cli/symbolicate.mjs +66 -0
- package/ios/AnimatedGraph.swift +246 -0
- package/ios/Bridge.swift +779 -0
- package/ios/ErrorOverlay.swift +122 -0
- package/ios/GestureReporter.swift +87 -0
- package/ios/ImageView.swift +43 -0
- package/ios/JavaScriptRuntime/JavaScriptRuntime.mm +231 -0
- package/ios/JavaScriptRuntime/include/NativueJavaScriptRuntime.h +29 -0
- package/ios/JavaScriptRuntime/vendor/hermes/LICENSE +21 -0
- package/ios/JavaScriptRuntime/vendor/hermes/Public/CrashManager.h +107 -0
- package/ios/JavaScriptRuntime/vendor/hermes/Public/CtorConfig.h +145 -0
- package/ios/JavaScriptRuntime/vendor/hermes/Public/GCConfig.h +211 -0
- package/ios/JavaScriptRuntime/vendor/hermes/Public/GCTripwireContext.h +40 -0
- package/ios/JavaScriptRuntime/vendor/hermes/Public/HermesExport.h +14 -0
- package/ios/JavaScriptRuntime/vendor/hermes/Public/RuntimeConfig.h +141 -0
- package/ios/JavaScriptRuntime/vendor/hermes/Public/SamplingProfiler.h +270 -0
- package/ios/JavaScriptRuntime/vendor/hermes/hermes.h +242 -0
- package/ios/KeyValueStorage.swift +66 -0
- package/ios/KeyboardAvoidingView.swift +41 -0
- package/ios/LayoutAnimation.swift +30 -0
- package/ios/ModalController.swift +85 -0
- package/ios/NativeNode.swift +200 -0
- package/ios/NativueView.swift +38 -0
- package/ios/PressableView.swift +63 -0
- package/ios/SafeAreaView.swift +36 -0
- package/ios/ScreenController.swift +122 -0
- package/ios/ScreenStackView.swift +299 -0
- package/ios/Style.swift +357 -0
- package/ios/SvgView.swift +464 -0
- package/ios/SwipeableView.swift +134 -0
- package/ios/TabBarView.swift +143 -0
- package/ios/TextInputView.swift +50 -0
- package/ios/VueScrollView.swift +90 -0
- package/package.json +56 -4
- package/src/animated.ts +206 -0
- package/src/colors.ts +198 -0
- package/src/components.ts +153 -0
- package/src/entry.ts +5 -0
- package/src/fetch.ts +114 -0
- package/src/globals.ts +38 -0
- package/src/native-components.ts +84 -0
- package/src/prelude.ts +52 -0
- package/src/renderer.ts +345 -0
- package/src/router.ts +814 -0
- package/src/storage.ts +38 -0
- package/src/types.ts +136 -0
- package/tsconfig.base.json +20 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 nativue contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/Package.swift
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
// swift-tools-version: 5.9
|
|
2
|
+
import PackageDescription
|
|
3
|
+
|
|
4
|
+
// The native side of nativue, consumed by the app's Xcode project as a local package.
|
|
5
|
+
let package = Package(
|
|
6
|
+
name: "Nativue",
|
|
7
|
+
platforms: [.iOS(.v17)],
|
|
8
|
+
products: [.library(name: "Nativue", targets: ["Nativue"])],
|
|
9
|
+
dependencies: [.package(url: "https://github.com/facebook/yoga.git", .upToNextMinor(from: "3.2.2"))],
|
|
10
|
+
targets: [
|
|
11
|
+
.target(
|
|
12
|
+
name: "Nativue",
|
|
13
|
+
dependencies: ["JavaScriptRuntime", .product(name: "yoga", package: "yoga")],
|
|
14
|
+
path: "ios",
|
|
15
|
+
exclude: ["JavaScriptRuntime"]
|
|
16
|
+
),
|
|
17
|
+
.target(
|
|
18
|
+
name: "JavaScriptRuntime",
|
|
19
|
+
dependencies: ["hermesvm"],
|
|
20
|
+
path: "ios/JavaScriptRuntime",
|
|
21
|
+
exclude: ["vendor/hermes/LICENSE"],
|
|
22
|
+
cxxSettings: [
|
|
23
|
+
.headerSearchPath("vendor"),
|
|
24
|
+
.headerSearchPath("../../android/src/main/cpp/jsi"),
|
|
25
|
+
]
|
|
26
|
+
),
|
|
27
|
+
// The iOS slices of hermes-ios (Maven Central), the same Hermes version as hermes-android in
|
|
28
|
+
// android/build.gradle.kts and hermes-compiler in package.json: bytecode only runs on the Hermes
|
|
29
|
+
// that compiled it.
|
|
30
|
+
.binaryTarget(
|
|
31
|
+
name: "hermesvm",
|
|
32
|
+
url: "https://github.com/nativue/nativue/releases/download/hermes-260318099.0.8/hermesvm.xcframework.zip",
|
|
33
|
+
checksum: "9147f0187b5790e6c478cfd3de4de585666fa2af34a67dc8a0e8243ff0ff580d"
|
|
34
|
+
),
|
|
35
|
+
],
|
|
36
|
+
cxxLanguageStandard: .cxx20
|
|
37
|
+
)
|
package/README.md
CHANGED
|
@@ -1,3 +1,21 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @nativue/core
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Vue rendering real native views on iOS and Android: UIKit on iOS, Android views with Material 3 on
|
|
4
|
+
Android, laid out with Yoga.
|
|
5
|
+
|
|
6
|
+
Start a new app with:
|
|
7
|
+
|
|
8
|
+
```sh
|
|
9
|
+
npm create nativue@latest my-app
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
This package holds the runtime (renderer, router, Animated, AsyncStorage, fetch), the native side
|
|
13
|
+
for iOS (a Swift package) and Android (a Gradle library), and the `nativue` CLI:
|
|
14
|
+
|
|
15
|
+
```sh
|
|
16
|
+
nativue run ios # or android: builds, opens the app on a simulator or emulator, then dev
|
|
17
|
+
nativue dev # rebuilds on save and reloads the running app
|
|
18
|
+
nativue build # bundles the app into ios/ and android/ (--production for releases)
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Docs and source: https://github.com/nativue/nativue
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
// The nativue Android library: Hermes runs the bundle, Yoga lays out the native views.
|
|
2
|
+
import java.net.Inet4Address
|
|
3
|
+
import java.net.NetworkInterface
|
|
4
|
+
|
|
5
|
+
plugins {
|
|
6
|
+
id("com.android.library")
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* This computer's address on the local network (Wi-Fi or Ethernet first), for a physical device to
|
|
11
|
+
* reach the dev server.
|
|
12
|
+
*/
|
|
13
|
+
// ponytail: fixed at build time, like the iOS app's host; rebuild after switching networks.
|
|
14
|
+
fun localNetworkAddress(): String? = NetworkInterface.getNetworkInterfaces().toList()
|
|
15
|
+
.filter { it.isUp && !it.isLoopback && !it.isVirtual }
|
|
16
|
+
.sortedBy { if (it.name.startsWith("en") || it.name.startsWith("eth") || it.name.startsWith("wlan")) 0 else 1 }
|
|
17
|
+
.flatMap { it.inetAddresses.toList() }
|
|
18
|
+
.firstOrNull { it is Inet4Address && it.isSiteLocalAddress }
|
|
19
|
+
?.hostAddress
|
|
20
|
+
|
|
21
|
+
android {
|
|
22
|
+
namespace = "dev.nativue"
|
|
23
|
+
compileSdk = 36
|
|
24
|
+
ndkVersion = "27.1.12297006"
|
|
25
|
+
|
|
26
|
+
defaultConfig {
|
|
27
|
+
// ponytail: 30 for the WindowInsets.Type API (safe area, keyboard); lower it with the compat insets if needed.
|
|
28
|
+
minSdk = 30
|
|
29
|
+
consumerProguardFiles("consumer-rules.pro")
|
|
30
|
+
|
|
31
|
+
externalNativeBuild {
|
|
32
|
+
cmake {
|
|
33
|
+
arguments += "-DANDROID_STL=c++_shared"
|
|
34
|
+
cppFlags += listOf("-std=c++20", "-fexceptions", "-frtti")
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
externalNativeBuild {
|
|
40
|
+
cmake {
|
|
41
|
+
path = file("src/main/cpp/CMakeLists.txt")
|
|
42
|
+
version = "3.22.1"
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
buildFeatures {
|
|
47
|
+
prefab = true
|
|
48
|
+
resValues = true
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
buildTypes {
|
|
52
|
+
debug {
|
|
53
|
+
localNetworkAddress()?.let { resValue("string", "nativue_dev_server_host", it) }
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// The Hermes package already ships these; apps get them from there.
|
|
58
|
+
packaging {
|
|
59
|
+
jniLibs.excludes += listOf("**/libhermesvm.so", "**/libc++_shared.so")
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
compileOptions {
|
|
63
|
+
sourceCompatibility = JavaVersion.VERSION_17
|
|
64
|
+
targetCompatibility = JavaVersion.VERSION_17
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
dependencies {
|
|
69
|
+
// Same version as the vendored JSI headers in src/main/cpp/jsi, hermes-compiler in package.json
|
|
70
|
+
// and hermesvm in Package.swift: bytecode only runs on the Hermes that compiled it.
|
|
71
|
+
implementation("com.facebook.hermes:hermes-android:260318099.0.8")
|
|
72
|
+
implementation("com.facebook.yoga:yoga:3.2.1")
|
|
73
|
+
implementation("com.facebook.soloader:soloader:0.10.5")
|
|
74
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
# bridge.cpp calls these through JNI.
|
|
2
|
+
-keep class dev.nativue.JavaScriptRuntime { *; }
|
|
3
|
+
|
|
4
|
+
# Hermes (its Intl is written in Java), Yoga and fbjni look their Java classes, fields and methods up
|
|
5
|
+
# by name from native code, and their AARs ship no rules of their own.
|
|
6
|
+
-keep class com.facebook.hermes.** { *; }
|
|
7
|
+
-keep class com.facebook.yoga.** { *; }
|
|
8
|
+
-keep class com.facebook.jni.** { *; }
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
cmake_minimum_required(VERSION 3.22.1)
|
|
2
|
+
project(nativue CXX)
|
|
3
|
+
|
|
4
|
+
find_package(hermes-engine REQUIRED CONFIG)
|
|
5
|
+
|
|
6
|
+
# Hermes links against a libjsi.so it doesn't ship.
|
|
7
|
+
add_library(jsi SHARED jsi/jsi/jsi.cpp)
|
|
8
|
+
target_include_directories(jsi PUBLIC jsi)
|
|
9
|
+
|
|
10
|
+
add_library(nativue SHARED bridge.cpp)
|
|
11
|
+
target_link_libraries(nativue hermes-engine::hermesvm jsi log)
|
|
@@ -0,0 +1,395 @@
|
|
|
1
|
+
// JNI bridge between dev.nativue.JavaScriptRuntime (Kotlin) and Hermes, through JSI. Values cross as
|
|
2
|
+
// plain Java objects: null, Boolean, Double, String, ArrayList and HashMap (and back from any Number,
|
|
3
|
+
// List, Map or Object[]). Every call happens on the JS thread.
|
|
4
|
+
#include <hermes/hermes.h>
|
|
5
|
+
#include <jni.h>
|
|
6
|
+
#include <jsi/jsi.h>
|
|
7
|
+
|
|
8
|
+
#include <memory>
|
|
9
|
+
#include <string>
|
|
10
|
+
#include <vector>
|
|
11
|
+
|
|
12
|
+
using namespace facebook;
|
|
13
|
+
|
|
14
|
+
namespace {
|
|
15
|
+
|
|
16
|
+
JavaVM* javaVM;
|
|
17
|
+
|
|
18
|
+
struct JavaTypes {
|
|
19
|
+
jclass object, boolean, number, doubleClass, string, list, arrayList, map, hashMap, objectArray;
|
|
20
|
+
jmethodID booleanValueOf, booleanValue, doubleValueOf, numberDoubleValue;
|
|
21
|
+
jmethodID listSize, listGet, arrayListInit, arrayListAdd;
|
|
22
|
+
jmethodID mapKeys, mapGet, hashMapInit, hashMapPut, setToArray;
|
|
23
|
+
jmethodID callNative, reportError;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
JavaTypes types;
|
|
27
|
+
|
|
28
|
+
jclass globalClass(JNIEnv* env, const char* name) {
|
|
29
|
+
jclass localClass = env->FindClass(name);
|
|
30
|
+
auto globalClass = static_cast<jclass>(env->NewGlobalRef(localClass));
|
|
31
|
+
env->DeleteLocalRef(localClass);
|
|
32
|
+
|
|
33
|
+
return globalClass;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
JNIEnv* currentEnvironment() {
|
|
37
|
+
JNIEnv* env = nullptr;
|
|
38
|
+
javaVM->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6);
|
|
39
|
+
|
|
40
|
+
return env;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// One per JavaScriptRuntime: the Hermes runtime and the Kotlin object that receives its calls.
|
|
44
|
+
struct Runtime {
|
|
45
|
+
std::unique_ptr<facebook::hermes::HermesRuntime> hermes;
|
|
46
|
+
jobject owner;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
// ---------- Strings (UTF-16 both ways, so emoji survive) ----------
|
|
50
|
+
|
|
51
|
+
jstring toJavaString(JNIEnv* env, jsi::Runtime& runtime, const jsi::String& string) {
|
|
52
|
+
std::u16string text = string.utf16(runtime);
|
|
53
|
+
|
|
54
|
+
return env->NewString(reinterpret_cast<const jchar*>(text.data()), static_cast<jsize>(text.size()));
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
jsi::String toJavaScriptString(JNIEnv* env, jsi::Runtime& runtime, jstring string) {
|
|
58
|
+
const jchar* characters = env->GetStringChars(string, nullptr);
|
|
59
|
+
auto text = jsi::String::createFromUtf16(
|
|
60
|
+
runtime, reinterpret_cast<const char16_t*>(characters), env->GetStringLength(string));
|
|
61
|
+
env->ReleaseStringChars(string, characters);
|
|
62
|
+
|
|
63
|
+
return text;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
std::string toStdString(JNIEnv* env, jstring string) {
|
|
67
|
+
const char* characters = env->GetStringUTFChars(string, nullptr);
|
|
68
|
+
std::string text(characters);
|
|
69
|
+
env->ReleaseStringUTFChars(string, characters);
|
|
70
|
+
|
|
71
|
+
return text;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// ---------- Values ----------
|
|
75
|
+
|
|
76
|
+
jobject toJava(JNIEnv* env, jsi::Runtime& runtime, const jsi::Value& value);
|
|
77
|
+
|
|
78
|
+
jobject arrayToJava(JNIEnv* env, jsi::Runtime& runtime, const jsi::Array& array) {
|
|
79
|
+
size_t length = array.size(runtime);
|
|
80
|
+
jobject list = env->NewObject(types.arrayList, types.arrayListInit, static_cast<jint>(length));
|
|
81
|
+
|
|
82
|
+
for (size_t index = 0; index < length; index++) {
|
|
83
|
+
jobject element = toJava(env, runtime, array.getValueAtIndex(runtime, index));
|
|
84
|
+
env->CallBooleanMethod(list, types.arrayListAdd, element);
|
|
85
|
+
env->DeleteLocalRef(element);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
return list;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
jobject objectToJava(JNIEnv* env, jsi::Runtime& runtime, const jsi::Object& object) {
|
|
92
|
+
jsi::Array names = object.getPropertyNames(runtime);
|
|
93
|
+
size_t count = names.size(runtime);
|
|
94
|
+
jobject map = env->NewObject(types.hashMap, types.hashMapInit, static_cast<jint>(count));
|
|
95
|
+
|
|
96
|
+
for (size_t index = 0; index < count; index++) {
|
|
97
|
+
jsi::String name = names.getValueAtIndex(runtime, index).getString(runtime);
|
|
98
|
+
jstring key = toJavaString(env, runtime, name);
|
|
99
|
+
jobject value = toJava(env, runtime, object.getProperty(runtime, jsi::PropNameID::forString(runtime, name)));
|
|
100
|
+
env->DeleteLocalRef(env->CallObjectMethod(map, types.hashMapPut, key, value));
|
|
101
|
+
env->DeleteLocalRef(key);
|
|
102
|
+
env->DeleteLocalRef(value);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
return map;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// Functions and other non-data values become null.
|
|
109
|
+
jobject toJava(JNIEnv* env, jsi::Runtime& runtime, const jsi::Value& value) {
|
|
110
|
+
if (value.isBool()) return env->CallStaticObjectMethod(types.boolean, types.booleanValueOf, value.getBool());
|
|
111
|
+
if (value.isNumber()) return env->CallStaticObjectMethod(types.doubleClass, types.doubleValueOf, value.getNumber());
|
|
112
|
+
if (value.isString()) return toJavaString(env, runtime, value.getString(runtime));
|
|
113
|
+
if (!value.isObject()) return nullptr;
|
|
114
|
+
|
|
115
|
+
jsi::Object object = value.getObject(runtime);
|
|
116
|
+
if (object.isFunction(runtime)) return nullptr;
|
|
117
|
+
if (object.isArray(runtime)) return arrayToJava(env, runtime, object.getArray(runtime));
|
|
118
|
+
|
|
119
|
+
return objectToJava(env, runtime, object);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
jsi::Value toJavaScript(JNIEnv* env, jsi::Runtime& runtime, jobject value);
|
|
123
|
+
|
|
124
|
+
jsi::Value listToJavaScript(JNIEnv* env, jsi::Runtime& runtime, jobject list) {
|
|
125
|
+
jint length = env->CallIntMethod(list, types.listSize);
|
|
126
|
+
jsi::Array array(runtime, length);
|
|
127
|
+
|
|
128
|
+
for (jint index = 0; index < length; index++) {
|
|
129
|
+
jobject element = env->CallObjectMethod(list, types.listGet, index);
|
|
130
|
+
array.setValueAtIndex(runtime, index, toJavaScript(env, runtime, element));
|
|
131
|
+
env->DeleteLocalRef(element);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
return array;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
jsi::Value objectArrayToJavaScript(JNIEnv* env, jsi::Runtime& runtime, jobjectArray elements) {
|
|
138
|
+
jsize length = env->GetArrayLength(elements);
|
|
139
|
+
jsi::Array array(runtime, length);
|
|
140
|
+
|
|
141
|
+
for (jsize index = 0; index < length; index++) {
|
|
142
|
+
jobject element = env->GetObjectArrayElement(elements, index);
|
|
143
|
+
array.setValueAtIndex(runtime, index, toJavaScript(env, runtime, element));
|
|
144
|
+
env->DeleteLocalRef(element);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
return array;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
jsi::Value mapToJavaScript(JNIEnv* env, jsi::Runtime& runtime, jobject map) {
|
|
151
|
+
jsi::Object object(runtime);
|
|
152
|
+
jobject keySet = env->CallObjectMethod(map, types.mapKeys);
|
|
153
|
+
auto keys = static_cast<jobjectArray>(env->CallObjectMethod(keySet, types.setToArray));
|
|
154
|
+
|
|
155
|
+
for (jsize index = 0; index < env->GetArrayLength(keys); index++) {
|
|
156
|
+
auto key = static_cast<jstring>(env->GetObjectArrayElement(keys, index));
|
|
157
|
+
jobject value = env->CallObjectMethod(map, types.mapGet, key);
|
|
158
|
+
object.setProperty(runtime, toJavaScriptString(env, runtime, key), toJavaScript(env, runtime, value));
|
|
159
|
+
env->DeleteLocalRef(key);
|
|
160
|
+
env->DeleteLocalRef(value);
|
|
161
|
+
}
|
|
162
|
+
env->DeleteLocalRef(keys);
|
|
163
|
+
env->DeleteLocalRef(keySet);
|
|
164
|
+
|
|
165
|
+
return object;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
jsi::Value toJavaScript(JNIEnv* env, jsi::Runtime& runtime, jobject value) {
|
|
169
|
+
if (value == nullptr) return jsi::Value::null();
|
|
170
|
+
if (env->IsInstanceOf(value, types.boolean)) {
|
|
171
|
+
return jsi::Value(static_cast<bool>(env->CallBooleanMethod(value, types.booleanValue)));
|
|
172
|
+
}
|
|
173
|
+
if (env->IsInstanceOf(value, types.number)) {
|
|
174
|
+
return jsi::Value(env->CallDoubleMethod(value, types.numberDoubleValue));
|
|
175
|
+
}
|
|
176
|
+
if (env->IsInstanceOf(value, types.string)) {
|
|
177
|
+
return toJavaScriptString(env, runtime, static_cast<jstring>(value));
|
|
178
|
+
}
|
|
179
|
+
if (env->IsInstanceOf(value, types.list)) return listToJavaScript(env, runtime, value);
|
|
180
|
+
if (env->IsInstanceOf(value, types.map)) return mapToJavaScript(env, runtime, value);
|
|
181
|
+
if (env->IsInstanceOf(value, types.objectArray)) {
|
|
182
|
+
return objectArrayToJavaScript(env, runtime, static_cast<jobjectArray>(value));
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
return jsi::Value::undefined();
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
// ---------- Errors ----------
|
|
189
|
+
|
|
190
|
+
void reportError(Runtime& runtime, const std::string& message, const std::string& stack) {
|
|
191
|
+
JNIEnv* env = currentEnvironment();
|
|
192
|
+
jstring javaMessage = env->NewStringUTF(message.c_str());
|
|
193
|
+
jstring javaStack = env->NewStringUTF(stack.c_str());
|
|
194
|
+
env->CallVoidMethod(runtime.owner, types.reportError, javaMessage, javaStack);
|
|
195
|
+
env->DeleteLocalRef(javaMessage);
|
|
196
|
+
env->DeleteLocalRef(javaStack);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
// Runs `work` and the promise jobs it queued, reporting errors instead of throwing them into Java. A
|
|
200
|
+
// failing job doesn't stop the others.
|
|
201
|
+
template <typename Work>
|
|
202
|
+
void runReportingErrors(Runtime& runtime, Work work) {
|
|
203
|
+
auto attempt = [&runtime](auto step) {
|
|
204
|
+
try {
|
|
205
|
+
step();
|
|
206
|
+
return true;
|
|
207
|
+
} catch (const jsi::JSError& error) {
|
|
208
|
+
reportError(runtime, error.getMessage(), error.getStack());
|
|
209
|
+
} catch (const std::exception& error) {
|
|
210
|
+
reportError(runtime, error.what(), "");
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
return false;
|
|
214
|
+
};
|
|
215
|
+
|
|
216
|
+
attempt(work);
|
|
217
|
+
while (!attempt([&runtime] { runtime.hermes->drainMicrotasks(); })) {
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
// ---------- __native ----------
|
|
222
|
+
|
|
223
|
+
// A Kotlin exception thrown by a native function becomes a JS error at the call site.
|
|
224
|
+
void rethrowJavaException(JNIEnv* env, jsi::Runtime& runtime) {
|
|
225
|
+
jthrowable exception = env->ExceptionOccurred();
|
|
226
|
+
if (exception == nullptr) return;
|
|
227
|
+
|
|
228
|
+
env->ExceptionClear();
|
|
229
|
+
jclass throwableClass = env->GetObjectClass(exception);
|
|
230
|
+
jmethodID toString = env->GetMethodID(throwableClass, "toString", "()Ljava/lang/String;");
|
|
231
|
+
auto description = static_cast<jstring>(env->CallObjectMethod(exception, toString));
|
|
232
|
+
std::string message = toStdString(env, description);
|
|
233
|
+
env->DeleteLocalRef(description);
|
|
234
|
+
env->DeleteLocalRef(throwableClass);
|
|
235
|
+
env->DeleteLocalRef(exception);
|
|
236
|
+
|
|
237
|
+
throw jsi::JSError(runtime, message);
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
jsi::Function nativeFunction(jsi::Runtime& runtime, jobject owner, const std::string& name, jint index) {
|
|
241
|
+
auto call = [owner, index](jsi::Runtime& runtime, const jsi::Value&, const jsi::Value* arguments, size_t count) {
|
|
242
|
+
JNIEnv* env = currentEnvironment();
|
|
243
|
+
// One frame per call: the bundle makes thousands of calls within one JNI call.
|
|
244
|
+
env->PushLocalFrame(static_cast<jint>(count) * 4 + 16);
|
|
245
|
+
|
|
246
|
+
jobjectArray javaArguments = env->NewObjectArray(static_cast<jsize>(count), types.object, nullptr);
|
|
247
|
+
for (size_t position = 0; position < count; position++) {
|
|
248
|
+
jobject argument = toJava(env, runtime, arguments[position]);
|
|
249
|
+
env->SetObjectArrayElement(javaArguments, static_cast<jsize>(position), argument);
|
|
250
|
+
env->DeleteLocalRef(argument);
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
jobject result = env->CallObjectMethod(owner, types.callNative, index, javaArguments);
|
|
254
|
+
if (env->ExceptionCheck()) {
|
|
255
|
+
env->PopLocalFrame(nullptr);
|
|
256
|
+
rethrowJavaException(currentEnvironment(), runtime);
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
jsi::Value value = toJavaScript(env, runtime, result);
|
|
260
|
+
env->PopLocalFrame(nullptr);
|
|
261
|
+
|
|
262
|
+
return value;
|
|
263
|
+
};
|
|
264
|
+
|
|
265
|
+
return jsi::Function::createFromHostFunction(runtime, jsi::PropNameID::forUtf8(runtime, name), 0, call);
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
void installNativeModule(Runtime& runtime, JNIEnv* env, jobjectArray functionNames) {
|
|
269
|
+
jsi::Runtime& hermes = *runtime.hermes;
|
|
270
|
+
jsi::Object nativeModule(hermes);
|
|
271
|
+
|
|
272
|
+
for (jsize index = 0; index < env->GetArrayLength(functionNames); index++) {
|
|
273
|
+
auto name = static_cast<jstring>(env->GetObjectArrayElement(functionNames, index));
|
|
274
|
+
std::string functionName = toStdString(env, name);
|
|
275
|
+
nativeModule.setProperty(hermes, functionName.c_str(), nativeFunction(hermes, runtime.owner, functionName, index));
|
|
276
|
+
env->DeleteLocalRef(name);
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
hermes.global().setProperty(hermes, "__native", nativeModule);
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
Runtime& runtimeOf(jlong handle) {
|
|
283
|
+
return *reinterpret_cast<Runtime*>(handle);
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
// A copy of a Java byte[]: JavaScript source or Hermes bytecode. Hermes runs bytecode straight from
|
|
287
|
+
// the buffer, so it keeps the buffer alive as long as it needs it.
|
|
288
|
+
class ByteBuffer : public jsi::Buffer {
|
|
289
|
+
public:
|
|
290
|
+
ByteBuffer(JNIEnv* env, jbyteArray array) : bytes_(env->GetArrayLength(array)) {
|
|
291
|
+
env->GetByteArrayRegion(array, 0, static_cast<jsize>(bytes_.size()), reinterpret_cast<jbyte*>(bytes_.data()));
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
size_t size() const override {
|
|
295
|
+
return bytes_.size();
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
const uint8_t* data() const override {
|
|
299
|
+
return bytes_.data();
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
private:
|
|
303
|
+
std::vector<uint8_t> bytes_;
|
|
304
|
+
};
|
|
305
|
+
|
|
306
|
+
} // namespace
|
|
307
|
+
|
|
308
|
+
// ---------- JNI entry points (dev.nativue.JavaScriptRuntime) ----------
|
|
309
|
+
|
|
310
|
+
extern "C" JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void*) {
|
|
311
|
+
javaVM = vm;
|
|
312
|
+
JNIEnv* env = currentEnvironment();
|
|
313
|
+
|
|
314
|
+
types.object = globalClass(env, "java/lang/Object");
|
|
315
|
+
types.boolean = globalClass(env, "java/lang/Boolean");
|
|
316
|
+
types.number = globalClass(env, "java/lang/Number");
|
|
317
|
+
types.doubleClass = globalClass(env, "java/lang/Double");
|
|
318
|
+
types.string = globalClass(env, "java/lang/String");
|
|
319
|
+
types.list = globalClass(env, "java/util/List");
|
|
320
|
+
types.arrayList = globalClass(env, "java/util/ArrayList");
|
|
321
|
+
types.map = globalClass(env, "java/util/Map");
|
|
322
|
+
types.hashMap = globalClass(env, "java/util/HashMap");
|
|
323
|
+
types.objectArray = globalClass(env, "[Ljava/lang/Object;");
|
|
324
|
+
|
|
325
|
+
types.booleanValueOf = env->GetStaticMethodID(types.boolean, "valueOf", "(Z)Ljava/lang/Boolean;");
|
|
326
|
+
types.booleanValue = env->GetMethodID(types.boolean, "booleanValue", "()Z");
|
|
327
|
+
types.doubleValueOf = env->GetStaticMethodID(types.doubleClass, "valueOf", "(D)Ljava/lang/Double;");
|
|
328
|
+
types.numberDoubleValue = env->GetMethodID(types.number, "doubleValue", "()D");
|
|
329
|
+
types.listSize = env->GetMethodID(types.list, "size", "()I");
|
|
330
|
+
types.listGet = env->GetMethodID(types.list, "get", "(I)Ljava/lang/Object;");
|
|
331
|
+
types.arrayListInit = env->GetMethodID(types.arrayList, "<init>", "(I)V");
|
|
332
|
+
types.arrayListAdd = env->GetMethodID(types.arrayList, "add", "(Ljava/lang/Object;)Z");
|
|
333
|
+
types.mapKeys = env->GetMethodID(types.map, "keySet", "()Ljava/util/Set;");
|
|
334
|
+
types.mapGet = env->GetMethodID(types.map, "get", "(Ljava/lang/Object;)Ljava/lang/Object;");
|
|
335
|
+
types.hashMapInit = env->GetMethodID(types.hashMap, "<init>", "(I)V");
|
|
336
|
+
types.hashMapPut =
|
|
337
|
+
env->GetMethodID(types.hashMap, "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
|
|
338
|
+
|
|
339
|
+
jclass setClass = env->FindClass("java/util/Set");
|
|
340
|
+
types.setToArray = env->GetMethodID(setClass, "toArray", "()[Ljava/lang/Object;");
|
|
341
|
+
env->DeleteLocalRef(setClass);
|
|
342
|
+
|
|
343
|
+
jclass runtimeClass = env->FindClass("dev/nativue/JavaScriptRuntime");
|
|
344
|
+
types.callNative = env->GetMethodID(runtimeClass, "callNative", "(I[Ljava/lang/Object;)Ljava/lang/Object;");
|
|
345
|
+
types.reportError = env->GetMethodID(runtimeClass, "reportError", "(Ljava/lang/String;Ljava/lang/String;)V");
|
|
346
|
+
env->DeleteLocalRef(runtimeClass);
|
|
347
|
+
|
|
348
|
+
return JNI_VERSION_1_6;
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
extern "C" JNIEXPORT jlong JNICALL
|
|
352
|
+
Java_dev_nativue_JavaScriptRuntime_create(JNIEnv* env, jobject owner, jobjectArray functionNames) {
|
|
353
|
+
auto config = ::hermes::vm::RuntimeConfig::Builder().withMicrotaskQueue(true).build();
|
|
354
|
+
auto runtime = new Runtime{facebook::hermes::makeHermesRuntime(config), env->NewGlobalRef(owner)};
|
|
355
|
+
installNativeModule(*runtime, env, functionNames);
|
|
356
|
+
|
|
357
|
+
return reinterpret_cast<jlong>(runtime);
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
extern "C" JNIEXPORT void JNICALL
|
|
361
|
+
Java_dev_nativue_JavaScriptRuntime_evaluate(JNIEnv* env, jobject, jlong handle, jbyteArray bundle, jstring sourceURL) {
|
|
362
|
+
Runtime& runtime = runtimeOf(handle);
|
|
363
|
+
auto buffer = std::make_shared<ByteBuffer>(env, bundle);
|
|
364
|
+
std::string url = toStdString(env, sourceURL);
|
|
365
|
+
|
|
366
|
+
runReportingErrors(runtime, [&] { runtime.hermes->evaluateJavaScript(buffer, url); });
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
extern "C" JNIEXPORT void JNICALL Java_dev_nativue_JavaScriptRuntime_call(
|
|
370
|
+
JNIEnv* env, jobject, jlong handle, jstring functionName, jobjectArray arguments) {
|
|
371
|
+
Runtime& runtime = runtimeOf(handle);
|
|
372
|
+
jsi::Runtime& hermes = *runtime.hermes;
|
|
373
|
+
std::string name = toStdString(env, functionName);
|
|
374
|
+
|
|
375
|
+
runReportingErrors(runtime, [&] {
|
|
376
|
+
jsi::Value function = hermes.global().getProperty(hermes, name.c_str());
|
|
377
|
+
if (!function.isObject() || !function.getObject(hermes).isFunction(hermes)) return;
|
|
378
|
+
|
|
379
|
+
std::vector<jsi::Value> values;
|
|
380
|
+
for (jsize index = 0; index < env->GetArrayLength(arguments); index++) {
|
|
381
|
+
jobject argument = env->GetObjectArrayElement(arguments, index);
|
|
382
|
+
values.push_back(toJavaScript(env, hermes, argument));
|
|
383
|
+
env->DeleteLocalRef(argument);
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
function.getObject(hermes).getFunction(hermes).call(
|
|
387
|
+
hermes, static_cast<const jsi::Value*>(values.data()), values.size());
|
|
388
|
+
});
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
extern "C" JNIEXPORT void JNICALL Java_dev_nativue_JavaScriptRuntime_destroy(JNIEnv* env, jobject, jlong handle) {
|
|
392
|
+
Runtime* runtime = reinterpret_cast<Runtime*>(handle);
|
|
393
|
+
env->DeleteGlobalRef(runtime->owner);
|
|
394
|
+
delete runtime;
|
|
395
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|