Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/mason-android/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ crate-type = ["cdylib"]
mason-core = { path = "../mason-core" }
#jni = { git = "https://github.com/jni-rs/jni-rs.git", rev = "1a4a345b1e65aca405e856046ff2661d37edde6d" }
jni = "0.21.1"
smallvec = "1.15.1"
log = "0.4.20"
android_logger = "0.14.1"
once_cell = "1.16.0"
Expand Down
45 changes: 39 additions & 6 deletions crates/mason-android/src/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,46 @@ pub(crate) const JAVA_ARRAY_TYPE: ReturnType = ReturnType::Array;
pub(crate) const JAVA_LONG_TYPE: ReturnType =
ReturnType::Primitive(jni::signature::Primitive::Long);

pub(crate) fn get_string_lossy(env: &mut JNIEnv, value: &JString) -> Option<String> {
/// A Java string as `&str`, read into a stack buffer; only NUL or non-BMP text is converted.
pub(crate) enum JStr {
Utf8(smallvec::SmallVec<[u8; 256]>),
Owned(String),
}

impl std::ops::Deref for JStr {
type Target = str;
fn deref(&self) -> &str {
match self {
// Checked as UTF-8 when the guard was made.
JStr::Utf8(bytes) => unsafe { std::str::from_utf8_unchecked(bytes) },
JStr::Owned(s) => s,
}
}
}

/// Every caller is a native declared with a String parameter, so the String class check
/// `get_string` makes is skipped, and GetStringUTFRegion fills our buffer instead of
/// GetStringUTFChars allocating one.
pub(crate) fn get_string_lossy(env: &JNIEnv, value: &JString) -> Option<JStr> {
if value.is_null() {
None
} else {
env.get_string(value)
.ok()
.map(|java_str| java_str.to_string_lossy().to_string())
return None;
}
unsafe {
let raw = env.get_raw();
let functions = &**raw;
let s = value.as_raw();
let chars = (functions.GetStringLength?)(raw, s);
let bytes = (functions.GetStringUTFLength?)(raw, s).max(0) as usize;
let mut buf = smallvec::SmallVec::<[u8; 256]>::with_capacity(bytes + 1);
(functions.GetStringUTFRegion?)(raw, s, 0, chars, buf.as_mut_ptr() as *mut std::ffi::c_char);
buf.set_len(bytes);
if std::str::from_utf8(&buf).is_ok() {
Some(JStr::Utf8(buf))
} else {
buf.push(0);
let cstr = jni::strings::JNIStr::from_ptr(buf.as_ptr() as *const std::ffi::c_char);
Some(JStr::Owned(String::from(std::borrow::Cow::<str>::from(cstr))))
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.nativescript.mason.masonkit

import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
class JniStringInstrumentedTest {
private val context = InstrumentationRegistry.getInstrumentation().targetContext

@Test
fun gridStringsRoundTripThroughNative() {
val style = View(context, Mason().apply { setDeviceScale(1f) }).style
style.gridTemplateColumns = "[a] 100px repeat(2, 1fr)"
style.gridArea = "1 / 2 / 3 / 4"
style.updateNativeStyle()
val columns = style.gridTemplateColumns
assertTrue(columns, columns.contains("100px") && columns.contains("1fr"))
assertEquals("1 / 2 / 3 / 4", style.gridArea)
}

@Test
fun nonBmpTextIsConverted() {
val style = View(context, Mason().apply { setDeviceScale(1f) }).style
style.gridTemplateColumns = "[x\uD83D\uDE00] 100px"
style.updateNativeStyle()
assertTrue(style.gridTemplateColumns, style.gridTemplateColumns.contains("x\uD83D\uDE00"))
}

@Test
fun uaDefaultsForATag() {
assertTrue(Mason.nativeUaDefaultForTag("h1").isNotEmpty())
}
}
Loading