Alternative Instance Handle Approach without JSWeakRef

Reviewed By: fkgozali

Differential Revision: D8003736

fbshipit-source-id: 597378555cc3f9c0ae95e8927460a3c813ebfb45
This commit is contained in:
Sebastian Markbage
2018-05-18 20:19:03 -07:00
committed by Facebook Github Bot
parent f59e5a8d28
commit 6cc597e6e4
7 changed files with 73 additions and 32 deletions

View File

@@ -74,7 +74,7 @@ public class FabricUIManager implements UIManager {
/** Creates a new {@link ReactShadowNode} */
@Nullable
public ReactShadowNode createNode(
int reactTag, String viewName, int rootTag, ReadableNativeMap props, int instanceHandle) {
int reactTag, String viewName, int rootTag, ReadableNativeMap props, long instanceHandle) {
if (DEBUG) {
Log.d(TAG, "createNode \n\ttag: " + reactTag +
"\n\tviewName: " + viewName +
@@ -123,11 +123,12 @@ public class FabricUIManager implements UIManager {
* including its children set (note that the children nodes will not be cloned).
*/
@Nullable
public ReactShadowNode cloneNode(ReactShadowNode node) {
public ReactShadowNode cloneNode(ReactShadowNode node, long instanceHandle) {
if (DEBUG) {
Log.d(TAG, "cloneNode \n\tnode: " + node);
}
try {
// TODO: Pass new instanceHandle
ReactShadowNode clone = node.mutableCopy();
assertReactShadowNodeCopy(node, clone);
return clone;
@@ -143,11 +144,12 @@ public class FabricUIManager implements UIManager {
* children set will be empty.
*/
@Nullable
public ReactShadowNode cloneNodeWithNewChildren(ReactShadowNode node) {
public ReactShadowNode cloneNodeWithNewChildren(ReactShadowNode node, long instanceHandle) {
if (DEBUG) {
Log.d(TAG, "cloneNodeWithNewChildren \n\tnode: " + node);
}
try {
// TODO: Pass new instanceHandle
ReactShadowNode clone = node.mutableCopyWithNewChildren();
assertReactShadowNodeCopy(node, clone);
return clone;
@@ -164,11 +166,12 @@ public class FabricUIManager implements UIManager {
*/
@Nullable
public ReactShadowNode cloneNodeWithNewProps(
ReactShadowNode node, @Nullable ReadableNativeMap newProps) {
ReactShadowNode node, @Nullable ReadableNativeMap newProps, long instanceHandle) {
if (DEBUG) {
Log.d(TAG, "cloneNodeWithNewProps \n\tnode: " + node + "\n\tprops: " + newProps);
}
try {
// TODO: Pass new instanceHandle
ReactShadowNode clone =
node.mutableCopyWithNewProps(newProps == null ? null : new ReactStylesDiffMap(newProps));
assertReactShadowNodeCopy(node, clone);
@@ -187,11 +190,12 @@ public class FabricUIManager implements UIManager {
*/
@Nullable
public ReactShadowNode cloneNodeWithNewChildrenAndProps(
ReactShadowNode node, ReadableNativeMap newProps) {
ReactShadowNode node, ReadableNativeMap newProps, long instanceHandle) {
if (DEBUG) {
Log.d(TAG, "cloneNodeWithNewChildrenAndProps \n\tnode: " + node + "\n\tnewProps: " + newProps);
}
try {
// TODO: Pass new instanceHandle
ReactShadowNode clone =
node.mutableCopyWithNewChildrenAndProps(
newProps == null ? null : new ReactStylesDiffMap(newProps));

View File

@@ -27,6 +27,10 @@ public class FabricJSCBinding implements FabricBinding {
private static native HybridData initHybrid();
private native long createEventTarget(long jsContextNativePointer, long instanceHandlePointer);
private native void releaseEventTarget(long jsContextNativePointer, long eventTargetPointer);
private native void installFabric(long jsContextNativePointer, Object fabricModule);
public FabricJSCBinding() {

View File

@@ -88,16 +88,16 @@ JSValueRef createNode(JSContextRef ctx, JSObjectRef function, JSObjectRef thisOb
static auto createNode =
jni::findClassStatic("com/facebook/react/fabric/FabricUIManager")
->getMethod<alias_ref<JShadowNode>(jint, jstring, jint, ReadableNativeMap::javaobject, jint)>("createNode");
->getMethod<alias_ref<JShadowNode>(jint, jstring, jint, ReadableNativeMap::javaobject, jlong)>("createNode");
int reactTag = (int)JSC_JSValueToNumber(ctx, arguments[0], NULL);
auto viewName = JSValueToJString(ctx, arguments[1]);
int rootTag = (int)JSC_JSValueToNumber(ctx, arguments[2], NULL);
auto props = JSC_JSValueIsNull(ctx, arguments[3]) ? local_ref<ReadableNativeMap::jhybridobject>(nullptr) :
JSValueToReadableMapViaJSON(ctx, arguments[3]);;
int instanceHandle = (int)JSC_JSValueToNumber(ctx, arguments[4], NULL);
auto instanceHandle = (void *)arguments[4];
auto node = createNode(manager, reactTag, viewName.get(), rootTag, props.get(), instanceHandle);
auto node = createNode(manager, reactTag, viewName.get(), rootTag, props.get(), (jlong)instanceHandle);
return JSC_JSObjectMake(ctx, classRef, makePlainGlobalRef(node.get()));
}
@@ -109,10 +109,11 @@ JSValueRef cloneNode(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObj
static auto cloneNode =
jni::findClassStatic("com/facebook/react/fabric/FabricUIManager")
->getMethod<alias_ref<JShadowNode>(JShadowNode::javaobject)>("cloneNode");
->getMethod<alias_ref<JShadowNode>(JShadowNode::javaobject, jlong)>("cloneNode");
auto previousNode = JSValueToJShadowNode(ctx, arguments[0]);
auto newNode = cloneNode(manager, previousNode.get());
auto instanceHandle = (void *)arguments[1];
auto newNode = cloneNode(manager, previousNode.get(), (jlong)instanceHandle);
return JSC_JSObjectMake(ctx, classRef, makePlainGlobalRef(newNode.get()));
}
@@ -124,10 +125,11 @@ JSValueRef cloneNodeWithNewChildren(JSContextRef ctx, JSObjectRef function, JSOb
static auto cloneNodeWithNewChildren =
jni::findClassStatic("com/facebook/react/fabric/FabricUIManager")
->getMethod<alias_ref<JShadowNode>(JShadowNode::javaobject)>("cloneNodeWithNewChildren");
->getMethod<alias_ref<JShadowNode>(JShadowNode::javaobject, jlong)>("cloneNodeWithNewChildren");
auto previousNode = JSValueToJShadowNode(ctx, arguments[0]);
auto newNode = cloneNodeWithNewChildren(manager, previousNode.get());
auto instanceHandle = (void *)arguments[1];
auto newNode = cloneNodeWithNewChildren(manager, previousNode.get(), (jlong)instanceHandle);
return JSC_JSObjectMake(ctx, classRef, makePlainGlobalRef(newNode.get()));
}
@@ -139,11 +141,12 @@ JSValueRef cloneNodeWithNewProps(JSContextRef ctx, JSObjectRef function, JSObjec
static auto cloneNodeWithNewProps =
jni::findClassStatic("com/facebook/react/fabric/FabricUIManager")
->getMethod<alias_ref<JShadowNode>(JShadowNode::javaobject, ReadableNativeMap::javaobject)>("cloneNodeWithNewProps");
->getMethod<alias_ref<JShadowNode>(JShadowNode::javaobject, ReadableNativeMap::javaobject, jlong)>("cloneNodeWithNewProps");
auto previousNode = JSValueToJShadowNode(ctx, arguments[0]);
auto props = JSValueToReadableMapViaJSON(ctx, arguments[1]);
auto newNode = cloneNodeWithNewProps(manager, previousNode.get(), props.get());
auto instanceHandle = (void *)arguments[2];
auto newNode = cloneNodeWithNewProps(manager, previousNode.get(), props.get(), (jlong)instanceHandle);
return JSC_JSObjectMake(ctx, classRef, makePlainGlobalRef(newNode.get()));
}
@@ -155,11 +158,12 @@ JSValueRef cloneNodeWithNewChildrenAndProps(JSContextRef ctx, JSObjectRef functi
static auto cloneNodeWithNewChildrenAndProps =
jni::findClassStatic("com/facebook/react/fabric/FabricUIManager")
->getMethod<alias_ref<JShadowNode>(JShadowNode::javaobject, ReadableNativeMap::javaobject)>("cloneNodeWithNewChildrenAndProps");
->getMethod<alias_ref<JShadowNode>(JShadowNode::javaobject, ReadableNativeMap::javaobject, jlong)>("cloneNodeWithNewChildrenAndProps");
auto previousNode = JSValueToJShadowNode(ctx, arguments[0]);
auto props = JSValueToReadableMapViaJSON(ctx, arguments[1]);
auto newNode = cloneNodeWithNewChildrenAndProps(manager, previousNode.get(), props.get());
auto instanceHandle = (void *)arguments[2];
auto newNode = cloneNodeWithNewChildrenAndProps(manager, previousNode.get(), props.get(), (jlong)instanceHandle);
return JSC_JSObjectMake(ctx, classRef, makePlainGlobalRef(newNode.get()));
}
@@ -266,6 +270,27 @@ jni::local_ref<FabricJSCBinding::jhybriddata> FabricJSCBinding::initHybrid(
return makeCxxInstance();
}
jlong FabricJSCBinding::createEventTarget(
jlong jsContextNativePointer,
jlong instanceHandlePointer
) {
JSContextRef context = (JSContextRef)jsContextNativePointer;
JSValueRef value = (JSValueRef)instanceHandlePointer;
// Retain a strong reference to this object.
JSC_JSValueProtect(context, value);
return (jlong)((void *)value);
}
void FabricJSCBinding::releaseEventTarget(
jlong jsContextNativePointer,
jlong eventTargetPointer
) {
JSContextRef context = (JSContextRef)jsContextNativePointer;
JSValueRef value = (JSValueRef)((void *)eventTargetPointer);
// Release this object.
JSC_JSValueUnprotect(context, value);
}
void FabricJSCBinding::installFabric(jlong jsContextNativePointer,
jni::alias_ref<jobject> fabricModule) {
JSContextRef context = (JSContextRef)jsContextNativePointer;

View File

@@ -26,6 +26,10 @@ private:
static jni::local_ref<jhybriddata> initHybrid(jni::alias_ref<jclass>);
jlong createEventTarget(jlong jsContextNativePointer, jlong instanceHandlePointer);
void releaseEventTarget(jlong jsContextNativePointer, jlong eventTargetPointer);
void installFabric(jlong jsContextNativePointer, jni::alias_ref<jobject> fabricModule);
};