Skip to content

Commit

Permalink
use DynamicFromObject to avoid maps
Browse files Browse the repository at this point in the history
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
  • Loading branch information
sahrens authored and facebook-github-bot committed Mar 25, 2019
1 parent af38a0c commit a46fba5
Show file tree
Hide file tree
Showing 8 changed files with 419 additions and 243 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/**
* Copyright (c) Facebook, Inc. and its 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 com.facebook.common.logging.FLog;
import com.facebook.react.common.ReactConstants;
import javax.annotation.Nullable;

/**
* Implementation of Dynamic wrapping a ReadableArray.
*/
public class DynamicFromObject implements Dynamic {
private @Nullable Object mObject;

public DynamicFromObject(@Nullable Object obj) {
mObject = obj;
}

@Override
public void recycle() {
// Noop - nothing to recycle since there is no pooling
}

@Override
public boolean isNull() {
return mObject == null;
}

@Override
public boolean asBoolean() {
return (boolean)mObject;
}

@Override
public double asDouble() {
return (double)mObject;
}

@Override
public int asInt() {
// Numbers from JS are always Doubles
return ((Double)mObject).intValue();
}

@Override
public String asString() {
return (String)mObject;
}

@Override
public ReadableArray asArray() {
return (ReadableArray)mObject;
}

@Override
public ReadableMap asMap() {
return (ReadableMap)mObject;
}

@Override
public ReadableType getType() {
if (isNull()) {
return ReadableType.Null;
}
if (mObject instanceof Boolean) {
return ReadableType.Boolean;
}
if (mObject instanceof Number) {
return ReadableType.Number;
}
if (mObject instanceof String) {
return ReadableType.String;
}
if (mObject instanceof ReadableMap) {
return ReadableType.Map;
}
if (mObject instanceof ReadableArray) {
return ReadableType.Array;
}
FLog.e(ReactConstants.TAG, "Unmapped object type " + mObject.getClass().getName());
return ReadableType.Null;
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
* <p>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 java.util.HashMap;
Expand All @@ -20,9 +19,9 @@
* of {@link WritableNativeMap} created via {@link Arguments#createMap} or just {@link ReadableMap}
* interface if you want your "native" module method to take a map from JS as an argument.
*
* Main purpose for this class is to be used in java-only unit tests, but could also be used outside
* of tests in the code that operates only in java and needs to communicate with RN modules via
* their JS-exposed API.
* <p>Main purpose for this class is to be used in java-only unit tests, but could also be used
* outside of tests in the code that operates only in java and needs to communicate with RN modules
* via their JS-exposed API.
*/
public class JavaOnlyMap implements ReadableMap, WritableMap {

Expand Down Expand Up @@ -62,16 +61,19 @@ public static JavaOnlyMap deepClone(ReadableMap map) {
return res;
}

/**
* @param keysAndValues keys and values, interleaved
*/
/** @param keysAndValues keys and values, interleaved */
private JavaOnlyMap(Object... keysAndValues) {
if (keysAndValues.length % 2 != 0) {
throw new IllegalArgumentException("You must provide the same number of keys and values");
}
mBackingMap = new HashMap();
for (int i = 0; i < keysAndValues.length; i += 2) {
mBackingMap.put(keysAndValues[i], keysAndValues[i + 1]);
Object val = keysAndValues[i + 1];
if (val instanceof Number) {
// all values from JS are doubles, so emulate that here for tests.
val = ((Number)val).doubleValue();
}
mBackingMap.put(keysAndValues[i], val);
}
}

Expand Down Expand Up @@ -142,15 +144,20 @@ public JavaOnlyArray getArray(@Nonnull String name) {
} else if (value instanceof Dynamic) {
return ((Dynamic) value).getType();
} else {
throw new IllegalArgumentException("Invalid value " + value.toString() + " for key " + name +
"contained in JavaOnlyMap");
throw new IllegalArgumentException(
"Invalid value " + value.toString() + " for key " + name + "contained in JavaOnlyMap");
}
}

@Override
public @Nonnull Iterator<Map.Entry<String, Object>> getEntryIterator() {
return mBackingMap.entrySet().iterator();
}

@Override
public @Nonnull ReadableMapKeySetIterator keySetIterator() {
return new ReadableMapKeySetIterator() {
Iterator<String> mIterator = mBackingMap.keySet().iterator();
Iterator<Map.Entry<String, Object>> mIterator = mBackingMap.entrySet().iterator();

@Override
public boolean hasNextKey() {
Expand All @@ -159,7 +166,7 @@ public boolean hasNextKey() {

@Override
public String nextKey() {
return mIterator.next();
return mIterator.next().getKey();
}
};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
* <p>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 java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
Expand All @@ -19,16 +20,36 @@
public interface ReadableMap {

boolean hasKey(@Nonnull String name);

boolean isNull(@Nonnull String name);

boolean getBoolean(@Nonnull String name);

double getDouble(@Nonnull String name);

int getInt(@Nonnull String name);
@Nullable String getString(@Nonnull String name);
@Nullable ReadableArray getArray(@Nonnull String name);
@Nullable ReadableMap getMap(@Nonnull String name);
@Nonnull Dynamic getDynamic(@Nonnull String name);
@Nonnull ReadableType getType(@Nonnull String name);
@Nonnull ReadableMapKeySetIterator keySetIterator();
@Nonnull HashMap<String, Object> toHashMap();

@Nullable
String getString(@Nonnull String name);

@Nullable
ReadableArray getArray(@Nonnull String name);

@Nullable
ReadableMap getMap(@Nonnull String name);

@Nonnull
Dynamic getDynamic(@Nonnull String name);

@Nonnull
ReadableType getType(@Nonnull String name);

@Nonnull
Iterator<Map.Entry<String, Object>> getEntryIterator();

@Nonnull
ReadableMapKeySetIterator keySetIterator();

@Nonnull
HashMap<String, Object> toHashMap();
}
Loading

0 comments on commit a46fba5

Please sign in to comment.