Files
yoga/java/jni/YGJTypesVanilla.h
Nick Gerleman b1e0140aaa Remove JNI Binding usage of layoutContext (#1377)
Summary:
X-link: https://github.com/facebook/react-native/pull/39402

Pull Request resolved: https://github.com/facebook/yoga/pull/1377

To avoid keeping a per-node mapping on native Yoga nodes to Java nodes, a per-layout context was added, to be able to pass information from the start of the layout, to measure functions, log functions, etc.

The way this was done was super invasive, and added quite a few private APIs used only by the JNI functions.

This change removes the context-using functions from the JNI bindings in favor of it managing its own context. Next diff removes all the cruft.

Reviewed By: javache

Differential Revision: D49179243

fbshipit-source-id: 7e4944bead864e6b73fd2208a47c5725c18ff2b0
2023-09-12 19:08:55 -07:00

53 lines
1.4 KiB
C++

/*
* 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.
*/
#pragma once
#include <map>
#include <vector>
#include <yoga/Yoga.h>
#include "common.h"
#include "jni.h"
class PtrJNodeMapVanilla {
std::map<YGNodeConstRef, jsize> ptrsToIdxs_{};
jobjectArray javaNodes_{};
public:
PtrJNodeMapVanilla() = default;
PtrJNodeMapVanilla(jlongArray javaNativePointers, jobjectArray javaNodes)
: javaNodes_{javaNodes} {
using namespace facebook::yoga::vanillajni;
JNIEnv* env = getCurrentEnv();
jsize nativePointersSize = env->GetArrayLength(javaNativePointers);
std::vector<jlong> nativePointers(static_cast<size_t>(nativePointersSize));
env->GetLongArrayRegion(
javaNativePointers, 0, nativePointersSize, nativePointers.data());
for (jsize i = 0; i < nativePointersSize; ++i) {
ptrsToIdxs_[(YGNodeConstRef) nativePointers[static_cast<size_t>(i)]] = i;
}
}
facebook::yoga::vanillajni::ScopedLocalRef<jobject> ref(YGNodeConstRef node) {
using namespace facebook::yoga::vanillajni;
JNIEnv* env = getCurrentEnv();
auto idx = ptrsToIdxs_.find(node);
if (idx == ptrsToIdxs_.end()) {
return ScopedLocalRef<jobject>(env);
} else {
return make_local_ref(
env, env->GetObjectArrayElement(javaNodes_, idx->second));
}
}
};