Skip to content

Commit a46fba5

Browse files
sahrensfacebook-github-bot
authored andcommitted
use DynamicFromObject to avoid maps
Summary: Changes our property access pattern to iterate through props once and pass the Object value directly rather than looking the value up in the map with the key. Note some ViewManagers methods (especially yoga related ones on shadow nodes) expect a `Dyanamic`, so this diff also creates Dynamic's only when needed by the hand-written code, and introduces a new `DynamicWithObject` to create them that simply wraps the underlying object (as opposed to `DynamicWithMap` which wraps the map and does a lookup any time the `Dynamic` is accessed. Reviewed By: mdvacca Differential Revision: D14453300 fbshipit-source-id: df98567b6eff1e6b7c611f179eb11e413fb94e5d
1 parent af38a0c commit a46fba5

File tree

8 files changed

+419
-243
lines changed

8 files changed

+419
-243
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
package com.facebook.react.bridge;
9+
10+
import com.facebook.common.logging.FLog;
11+
import com.facebook.react.common.ReactConstants;
12+
import javax.annotation.Nullable;
13+
14+
/**
15+
* Implementation of Dynamic wrapping a ReadableArray.
16+
*/
17+
public class DynamicFromObject implements Dynamic {
18+
private @Nullable Object mObject;
19+
20+
public DynamicFromObject(@Nullable Object obj) {
21+
mObject = obj;
22+
}
23+
24+
@Override
25+
public void recycle() {
26+
// Noop - nothing to recycle since there is no pooling
27+
}
28+
29+
@Override
30+
public boolean isNull() {
31+
return mObject == null;
32+
}
33+
34+
@Override
35+
public boolean asBoolean() {
36+
return (boolean)mObject;
37+
}
38+
39+
@Override
40+
public double asDouble() {
41+
return (double)mObject;
42+
}
43+
44+
@Override
45+
public int asInt() {
46+
// Numbers from JS are always Doubles
47+
return ((Double)mObject).intValue();
48+
}
49+
50+
@Override
51+
public String asString() {
52+
return (String)mObject;
53+
}
54+
55+
@Override
56+
public ReadableArray asArray() {
57+
return (ReadableArray)mObject;
58+
}
59+
60+
@Override
61+
public ReadableMap asMap() {
62+
return (ReadableMap)mObject;
63+
}
64+
65+
@Override
66+
public ReadableType getType() {
67+
if (isNull()) {
68+
return ReadableType.Null;
69+
}
70+
if (mObject instanceof Boolean) {
71+
return ReadableType.Boolean;
72+
}
73+
if (mObject instanceof Number) {
74+
return ReadableType.Number;
75+
}
76+
if (mObject instanceof String) {
77+
return ReadableType.String;
78+
}
79+
if (mObject instanceof ReadableMap) {
80+
return ReadableType.Map;
81+
}
82+
if (mObject instanceof ReadableArray) {
83+
return ReadableType.Array;
84+
}
85+
FLog.e(ReactConstants.TAG, "Unmapped object type " + mObject.getClass().getName());
86+
return ReadableType.Null;
87+
}
88+
}

ReactAndroid/src/main/java/com/facebook/react/bridge/JavaOnlyMap.java

+21-14
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
/**
22
* Copyright (c) Facebook, Inc. and its affiliates.
33
*
4-
* This source code is licensed under the MIT license found in the
5-
* LICENSE file in the root directory of this source tree.
4+
* <p>This source code is licensed under the MIT license found in the LICENSE file in the root
5+
* directory of this source tree.
66
*/
7-
87
package com.facebook.react.bridge;
98

109
import java.util.HashMap;
@@ -20,9 +19,9 @@
2019
* of {@link WritableNativeMap} created via {@link Arguments#createMap} or just {@link ReadableMap}
2120
* interface if you want your "native" module method to take a map from JS as an argument.
2221
*
23-
* Main purpose for this class is to be used in java-only unit tests, but could also be used outside
24-
* of tests in the code that operates only in java and needs to communicate with RN modules via
25-
* their JS-exposed API.
22+
* <p>Main purpose for this class is to be used in java-only unit tests, but could also be used
23+
* outside of tests in the code that operates only in java and needs to communicate with RN modules
24+
* via their JS-exposed API.
2625
*/
2726
public class JavaOnlyMap implements ReadableMap, WritableMap {
2827

@@ -62,16 +61,19 @@ public static JavaOnlyMap deepClone(ReadableMap map) {
6261
return res;
6362
}
6463

65-
/**
66-
* @param keysAndValues keys and values, interleaved
67-
*/
64+
/** @param keysAndValues keys and values, interleaved */
6865
private JavaOnlyMap(Object... keysAndValues) {
6966
if (keysAndValues.length % 2 != 0) {
7067
throw new IllegalArgumentException("You must provide the same number of keys and values");
7168
}
7269
mBackingMap = new HashMap();
7370
for (int i = 0; i < keysAndValues.length; i += 2) {
74-
mBackingMap.put(keysAndValues[i], keysAndValues[i + 1]);
71+
Object val = keysAndValues[i + 1];
72+
if (val instanceof Number) {
73+
// all values from JS are doubles, so emulate that here for tests.
74+
val = ((Number)val).doubleValue();
75+
}
76+
mBackingMap.put(keysAndValues[i], val);
7577
}
7678
}
7779

@@ -142,15 +144,20 @@ public JavaOnlyArray getArray(@Nonnull String name) {
142144
} else if (value instanceof Dynamic) {
143145
return ((Dynamic) value).getType();
144146
} else {
145-
throw new IllegalArgumentException("Invalid value " + value.toString() + " for key " + name +
146-
"contained in JavaOnlyMap");
147+
throw new IllegalArgumentException(
148+
"Invalid value " + value.toString() + " for key " + name + "contained in JavaOnlyMap");
147149
}
148150
}
149151

152+
@Override
153+
public @Nonnull Iterator<Map.Entry<String, Object>> getEntryIterator() {
154+
return mBackingMap.entrySet().iterator();
155+
}
156+
150157
@Override
151158
public @Nonnull ReadableMapKeySetIterator keySetIterator() {
152159
return new ReadableMapKeySetIterator() {
153-
Iterator<String> mIterator = mBackingMap.keySet().iterator();
160+
Iterator<Map.Entry<String, Object>> mIterator = mBackingMap.entrySet().iterator();
154161

155162
@Override
156163
public boolean hasNextKey() {
@@ -159,7 +166,7 @@ public boolean hasNextKey() {
159166

160167
@Override
161168
public String nextKey() {
162-
return mIterator.next();
169+
return mIterator.next().getKey();
163170
}
164171
};
165172
}
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
/**
22
* Copyright (c) Facebook, Inc. and its affiliates.
33
*
4-
* This source code is licensed under the MIT license found in the
5-
* LICENSE file in the root directory of this source tree.
4+
* <p>This source code is licensed under the MIT license found in the LICENSE file in the root
5+
* directory of this source tree.
66
*/
7-
87
package com.facebook.react.bridge;
98

109
import java.util.HashMap;
10+
import java.util.Iterator;
11+
import java.util.Map;
1112

1213
import javax.annotation.Nonnull;
1314
import javax.annotation.Nullable;
@@ -19,16 +20,36 @@
1920
public interface ReadableMap {
2021

2122
boolean hasKey(@Nonnull String name);
23+
2224
boolean isNull(@Nonnull String name);
25+
2326
boolean getBoolean(@Nonnull String name);
27+
2428
double getDouble(@Nonnull String name);
29+
2530
int getInt(@Nonnull String name);
26-
@Nullable String getString(@Nonnull String name);
27-
@Nullable ReadableArray getArray(@Nonnull String name);
28-
@Nullable ReadableMap getMap(@Nonnull String name);
29-
@Nonnull Dynamic getDynamic(@Nonnull String name);
30-
@Nonnull ReadableType getType(@Nonnull String name);
31-
@Nonnull ReadableMapKeySetIterator keySetIterator();
32-
@Nonnull HashMap<String, Object> toHashMap();
3331

32+
@Nullable
33+
String getString(@Nonnull String name);
34+
35+
@Nullable
36+
ReadableArray getArray(@Nonnull String name);
37+
38+
@Nullable
39+
ReadableMap getMap(@Nonnull String name);
40+
41+
@Nonnull
42+
Dynamic getDynamic(@Nonnull String name);
43+
44+
@Nonnull
45+
ReadableType getType(@Nonnull String name);
46+
47+
@Nonnull
48+
Iterator<Map.Entry<String, Object>> getEntryIterator();
49+
50+
@Nonnull
51+
ReadableMapKeySetIterator keySetIterator();
52+
53+
@Nonnull
54+
HashMap<String, Object> toHashMap();
3455
}

0 commit comments

Comments
 (0)