Skip to content

Commit

Permalink
move common code to YGJNI.h header class
Browse files Browse the repository at this point in the history
Summary: Moving common code to YGJNI.h header class so that same can be used in fbjni and vanilla jni implementations

Reviewed By: amir-shalem

Differential Revision: D17666457

fbshipit-source-id: 1e6cd2506fb773b8a17ebef277a2c7ef9728e66b
  • Loading branch information
SidharthGuglani-zz authored and facebook-github-bot committed Oct 8, 2019
1 parent 3fce27c commit 25b8c94
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 50 deletions.
51 changes: 1 addition & 50 deletions java/jni/YGJNI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include "YGJTypes.h"
#include "YGJNIVanilla.h"
#include "YGJNI.h"

using namespace facebook::jni;
using namespace std;
Expand Down Expand Up @@ -76,59 +77,9 @@ const short int LAYOUT_BORDER_START_INDEX = 14;

namespace {

union YGNodeContext {
uintptr_t edgesSet = 0;
void* asVoidPtr;
};

const int DOES_LEGACY_STRETCH_BEHAVIOUR = 8;
const int HAS_NEW_LAYOUT = 16;

class YGNodeEdges {
uintptr_t edges_;

public:
enum Edge {
MARGIN = 1,
PADDING = 2,
BORDER = 4,
};

YGNodeEdges(YGNodeRef node) {
auto context = YGNodeContext{};
context.asVoidPtr = node->getContext();
edges_ = context.edgesSet;
}

void setOn(YGNodeRef node) {
auto context = YGNodeContext{};
context.edgesSet = edges_;
node->setContext(context.asVoidPtr);
}

bool has(Edge edge) { return (edges_ & edge) == edge; }

YGNodeEdges& add(Edge edge) {
edges_ |= edge;
return *this;
}

int get() { return edges_; }
};

struct YogaValue {
static constexpr jint NAN_BYTES = 0x7fc00000;

static jlong asJavaLong(const YGValue& value) {
uint32_t valueBytes = 0;
memcpy(&valueBytes, &value.value, sizeof valueBytes);
return ((jlong) value.unit) << 32 | valueBytes;
}
constexpr static jlong undefinedAsJavaLong() {
return ((jlong) YGUnitUndefined) << 32 | NAN_BYTES;
}
};

} // namespace

static inline local_ref<JYogaNode> YGNodeJobject(
Expand Down
58 changes: 58 additions & 0 deletions java/jni/YGJNI.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* 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.
*/
namespace {

union YGNodeContext {
uintptr_t edgesSet = 0;
void* asVoidPtr;
};

class YGNodeEdges {
uintptr_t edges_;

public:
enum Edge {
MARGIN = 1,
PADDING = 2,
BORDER = 4,
};

YGNodeEdges(YGNodeRef node) {
auto context = YGNodeContext{};
context.asVoidPtr = node->getContext();
edges_ = context.edgesSet;
}

void setOn(YGNodeRef node) {
auto context = YGNodeContext{};
context.edgesSet = edges_;
node->setContext(context.asVoidPtr);
}

bool has(Edge edge) { return (edges_ & edge) == edge; }

YGNodeEdges& add(Edge edge) {
edges_ |= edge;
return *this;
}

int get() { return edges_; }
};

struct YogaValue {
static constexpr jint NAN_BYTES = 0x7fc00000;

static jlong asJavaLong(const YGValue& value) {
uint32_t valueBytes = 0;
memcpy(&valueBytes, &value.value, sizeof valueBytes);
return ((jlong) value.unit) << 32 | valueBytes;
}
constexpr static jlong undefinedAsJavaLong() {
return ((jlong) YGUnitUndefined) << 32 | NAN_BYTES;
}
};
} // namespace

0 comments on commit 25b8c94

Please sign in to comment.