Summary: In D17439957, I noted that YogaLogger#log throws a NoMethodFoundException when called from C++ b/c C++ and Java's signatures of that method don't match. C++ uses YogaNodeJNIBase for the first param, Java uses YogaNode. Both my attempts to fix this failed. Attempt #1 - Make Java use YogaNodeJNIBase. This doesn't work because the :java-interface target includes YogaLogger but not YogaNodeJNIBase. Moving YogaLogger to the impl target doesn't work either b/c other files in :java-interface reference YogaLogger. Attempt #2 - Make C++ use YogaNode. This doesn't work b/c we try to call the log method with objects of fbjni type YogaNodeJNIBase. This would be fine in Java since YogaNodeJNIBase extends YogaNode. But fbjni's typing isn't advanced enough to know this, so the Yoga C++ fails to compile. At this point, I was wondering what the value of having this param in the log function at all was. None of the implementations in our codebase use it today. It might be easier to just remove it all together. This also removes a bug with YGNodePrint where we pass a null layout context that eventually causes a SIG_ABRT when we use it to try to find a YogaNode to pass to this function. (https://fburl.com/diffusion/ssw9h8lv). Reviewed By: amir-shalem Differential Revision: D17470379 fbshipit-source-id: 8fc2d95505971a52af2399a9fbb60b63f27f0ec2
61 lines
1.8 KiB
C++
61 lines
1.8 KiB
C++
/*
|
|
* 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.
|
|
*/
|
|
#include <fbjni/fbjni.h>
|
|
#include <yoga/YGValue.h>
|
|
#include <yoga/Yoga.h>
|
|
#include <map>
|
|
|
|
using namespace facebook::jni;
|
|
using namespace std;
|
|
|
|
struct JYogaNode : public facebook::jni::JavaClass<JYogaNode> {
|
|
static constexpr auto kJavaDescriptor = "Lcom/facebook/yoga/YogaNodeJNIBase;";
|
|
|
|
jfloat baseline(jfloat width, jfloat height);
|
|
jlong measure(jfloat width, jint widthMode, jfloat height, jint heightMode);
|
|
};
|
|
|
|
struct JYogaLogLevel : public facebook::jni::JavaClass<JYogaLogLevel> {
|
|
static constexpr auto kJavaDescriptor = "Lcom/facebook/yoga/YogaLogLevel;";
|
|
|
|
static facebook::jni::local_ref<JYogaLogLevel> fromInt(jint);
|
|
};
|
|
|
|
struct JYogaLogger : public facebook::jni::JavaClass<JYogaLogger> {
|
|
static constexpr auto kJavaDescriptor = "Lcom/facebook/yoga/YogaLogger;";
|
|
|
|
void log(facebook::jni::alias_ref<JYogaLogLevel>, jstring);
|
|
};
|
|
|
|
class PtrJNodeMap {
|
|
using JNodeArray = JArrayClass<JYogaNode::javaobject>;
|
|
std::map<YGNodeRef, size_t> ptrsToIdxs_;
|
|
alias_ref<JNodeArray> javaNodes_;
|
|
|
|
public:
|
|
PtrJNodeMap() : ptrsToIdxs_{}, javaNodes_{} {}
|
|
PtrJNodeMap(
|
|
alias_ref<JArrayLong> nativePointers,
|
|
alias_ref<JNodeArray> javaNodes)
|
|
: javaNodes_{javaNodes} {
|
|
auto pin = nativePointers->pinCritical();
|
|
auto ptrs = pin.get();
|
|
for (size_t i = 0, n = pin.size(); i < n; ++i) {
|
|
ptrsToIdxs_[(YGNodeRef) ptrs[i]] = i;
|
|
}
|
|
}
|
|
|
|
local_ref<JYogaNode> ref(YGNodeRef node) {
|
|
auto idx = ptrsToIdxs_.find(node);
|
|
if (idx == ptrsToIdxs_.end()) {
|
|
return local_ref<JYogaNode>{};
|
|
} else {
|
|
return javaNodes_->getElement(idx->second);
|
|
}
|
|
}
|
|
};
|