Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Android] Update bridge to handle long values #43158

Closed
wants to merge 2 commits into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ public int getInt(int index) {
return ((Number) mBackingList.get(index)).intValue();
}

@Override
public long getLong(int index) {
return ((Number) mBackingList.get(index)).longValue();
}

@Override
public @Nullable String getString(int index) {
return (String) mBackingList.get(index);
Expand Down Expand Up @@ -129,7 +134,7 @@ public ReadableMap getMap(int index) {
return ReadableType.Null;
} else if (object instanceof Boolean) {
return ReadableType.Boolean;
} else if (object instanceof Double || object instanceof Float || object instanceof Integer) {
} else if (object instanceof Double || object instanceof Float || object instanceof Integer || object instanceof Long) {
return ReadableType.Number;
} else if (object instanceof String) {
return ReadableType.String;
Expand All @@ -156,6 +161,11 @@ public void pushInt(int value) {
mBackingList.add(new Double(value));
}

@Override
public void pushLong(long value) {
mBackingList.add(value);
}

@Override
public void pushString(@Nullable String value) {
mBackingList.add(value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ public int getInt(@NonNull String name) {
return ((Number) mBackingMap.get(name)).intValue();
}

@Override
public long getLong(@NonNull String name) {
return ((Number) mBackingMap.get(name)).longValue();
}

@Override
public String getString(@NonNull String name) {
return (String) mBackingMap.get(name);
Expand Down Expand Up @@ -190,6 +195,11 @@ public void putInt(@NonNull String key, int value) {
mBackingMap.put(key, new Double(value));
}

@Override
public void putLong(@NonNull String key, long value) {
mBackingMap.put(key, value);
}

@Override
public void putString(@NonNull String key, @Nullable String value) {
mBackingMap.put(key, value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public interface ReadableArray {

int getInt(int index);

long getLong(int index);

@NonNull
String getString(int index);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public interface ReadableMap {

int getInt(@NonNull String name);

long getLong(@NonNull String name);

@Nullable
String getString(@NonNull String name);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ public int getInt(int index) {
return ((Double) getLocalArray()[index]).intValue();
}

@Override
public long getLong(int index) {
return ((Long) getLocalArray()[index]).longValue();
}

@Override
public @NonNull String getString(int index) {
return (String) getLocalArray()[index];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,11 @@ public int getInt(@NonNull String name) {
return getValue(name, Double.class).intValue();
}

@Override
public long getLong(@NonNull String name) {
return getValue(name, Long.class).longValue();
}

@Override
public @Nullable String getString(@NonNull String name) {
return getNullableValue(name, String.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public interface WritableArray extends ReadableArray {

void pushInt(int value);

void pushLong(long value);

void pushString(@Nullable String value);

void pushArray(@Nullable ReadableArray array);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public interface WritableMap extends ReadableMap {

void putInt(@NonNull String key, int value);

void putLong(@NonNull String key, long value);

void putString(@NonNull String key, @Nullable String value);

void putArray(@NonNull String key, @Nullable ReadableArray value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ public WritableNativeArray() {
@Override
public native void pushInt(int value);

@Override
public native void pushLong(long value);

@Override
public native void pushString(@Nullable String value);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ public class WritableNativeMap extends ReadableNativeMap implements WritableMap
@Override
public native void putInt(@NonNull String key, int value);

@Override
public native void putLong(@NonNull String key, long value);

@Override
public native void putNull(@NonNull String key);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,23 @@ class JavaOnlyArrayTest {
@Test
fun testGetType() {
val values =
JavaOnlyArray.of(1, 2f, 3.0, "4", false, JavaOnlyArray.of(), JavaOnlyMap.of(), null)
JavaOnlyArray.of(1, 2f, 3.0, 4L, "5", false, JavaOnlyArray.of(), JavaOnlyMap.of(), null)

assertThat(values.getType(0)).isEqualTo(ReadableType.Number)
assertThat(values.getType(1)).isEqualTo(ReadableType.Number)
assertThat(values.getType(2)).isEqualTo(ReadableType.Number)
assertThat(values.getType(3)).isEqualTo(ReadableType.String)
assertThat(values.getType(4)).isEqualTo(ReadableType.Boolean)
assertThat(values.getType(5)).isEqualTo(ReadableType.Array)
assertThat(values.getType(6)).isEqualTo(ReadableType.Map)
assertThat(values.getType(7)).isEqualTo(ReadableType.Null)
assertThat(values.getType(3)).isEqualTo(ReadableType.Number)
assertThat(values.getType(4)).isEqualTo(ReadableType.String)
assertThat(values.getType(5)).isEqualTo(ReadableType.Boolean)
assertThat(values.getType(6)).isEqualTo(ReadableType.Array)
assertThat(values.getType(7)).isEqualTo(ReadableType.Map)
assertThat(values.getType(8)).isEqualTo(ReadableType.Null)
}

@Test
fun testLongValueNotTruncated() {
val values = JavaOnlyArray.of(1125899906842623L)

assertThat(values.getLong(0)).isEqualTo(1125899906842623L)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

package com.facebook.react.bridge

import org.assertj.core.api.Assertions.assertThat
import org.junit.Test

/** Tests for [JavaOnlyMap] */
class JavaOnlyMapTest {
@Test
fun testGetType() {
val values =
JavaOnlyMap.of("int", 1, "float", 2f, "double", 3.0, "long", 4L, "string", "5", "boolean", false, "array", JavaOnlyArray.of(), "map", JavaOnlyMap.of(), "null", null)

assertThat(values.getType("int")).isEqualTo(ReadableType.Number)
assertThat(values.getType("float")).isEqualTo(ReadableType.Number)
assertThat(values.getType("double")).isEqualTo(ReadableType.Number)
assertThat(values.getType("long")).isEqualTo(ReadableType.Number)
assertThat(values.getType("string")).isEqualTo(ReadableType.String)
assertThat(values.getType("boolean")).isEqualTo(ReadableType.Boolean)
assertThat(values.getType("array")).isEqualTo(ReadableType.Array)
assertThat(values.getType("map")).isEqualTo(ReadableType.Map)
assertThat(values.getType("null")).isEqualTo(ReadableType.Null)
}

@Test
fun testLongValueNotTruncated() {
val values = JavaOnlyMap.of("long", 1125899906842623L)

assertThat(values.getLong("long")).isEqualTo(1125899906842623L)
}
}
Loading