Adds Javascript Support
Summary: - As mentioned in the title, this PR adds Javascript support to Yoga. Two different builds are included in this PR thanks to [nbind](https://github.com/charto/nbind), which conveniently allow to target both Node.js' native addons and browser environments via asmjs with approximately the same codebase. That should solve #215. - All tests successfully pass on both codepaths. You can run `yarn test:all` inside the `javascript` directory to test it. - Because of a bug in nbind, the [following PR](https://github.com/charto/nbind/pull/57) needs to be merged and a new version released before this one can be safely merged as well. - I had to use `double` types instead of `float` in the C++ bindings because of an Emscripten [bug](https://github.com/kripken/emscripten/issues/3592) where symbols aren't correctly exported when using floats. - There's some tweaks to do before this PR is 100% ready to merge, but I wanted to have your opinion first. What do you think of this? --- To do: - [x] Ensure th Closes https://github.com/facebook/yoga/pull/304 Reviewed By: mikearmstrong001 Differential Revision: D4375187 Pulled By: emilsjolander fbshipit-source-id: 47248558a9506b7c512b5ef281cd12fe1a60cab7
This commit is contained in:
committed by
Facebook Github Bot
parent
352f592767
commit
6f462a72bf
30
javascript/sources/Layout.hh
Normal file
30
javascript/sources/Layout.hh
Normal file
@@ -0,0 +1,30 @@
|
||||
/**
|
||||
* Copyright (c) 2014-present, Facebook, Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the BSD-style license found in the
|
||||
* LICENSE file in the root directory of this source tree. An additional grant
|
||||
* of patent rights can be found in the PATENTS file in the same directory.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <nbind/api.h>
|
||||
#include <nbind/BindDefiner.h>
|
||||
|
||||
struct Layout
|
||||
{
|
||||
double left;
|
||||
double right;
|
||||
|
||||
double top;
|
||||
double bottom;
|
||||
|
||||
double width;
|
||||
double height;
|
||||
|
||||
void toJS(nbind::cbOutput expose) const
|
||||
{
|
||||
expose(left, right, top, bottom, width, height);
|
||||
}
|
||||
};
|
405
javascript/sources/Node.cc
Normal file
405
javascript/sources/Node.cc
Normal file
@@ -0,0 +1,405 @@
|
||||
/**
|
||||
* Copyright (c) 2014-present, Facebook, Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the BSD-style license found in the
|
||||
* LICENSE file in the root directory of this source tree. An additional grant
|
||||
* of patent rights can be found in the PATENTS file in the same directory.
|
||||
*/
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
#include <yoga/Yoga.h>
|
||||
|
||||
#include "./Node.hh"
|
||||
#include "./Layout.hh"
|
||||
#include "./Size.hh"
|
||||
|
||||
static YGSize globalMeasureFunc(YGNodeRef nodeRef, float width, YGMeasureMode widthMode, float height, YGMeasureMode heightMode)
|
||||
{
|
||||
Node const & node = *reinterpret_cast<Node const *>(YGNodeGetContext(nodeRef));
|
||||
|
||||
Size size = node.callMeasureFunc(width, widthMode, height, heightMode);
|
||||
YGSize ygSize = { static_cast<float>(size.width), static_cast<float>(size.height) };
|
||||
|
||||
return ygSize;
|
||||
}
|
||||
|
||||
/* static */ Node * Node::create(void)
|
||||
{
|
||||
return new Node();
|
||||
}
|
||||
|
||||
/* static */ void Node::destroy(Node * node)
|
||||
{
|
||||
delete node;
|
||||
}
|
||||
|
||||
/* static */ Node * Node::fromYGNode(YGNodeRef nodeRef)
|
||||
{
|
||||
return reinterpret_cast<Node *>(YGNodeGetContext(nodeRef));
|
||||
}
|
||||
|
||||
Node::Node(void)
|
||||
: m_node(YGNodeNew())
|
||||
, m_measureFunc(nullptr)
|
||||
{
|
||||
YGNodeSetContext(m_node, reinterpret_cast<void *>(this));
|
||||
}
|
||||
|
||||
Node::~Node(void)
|
||||
{
|
||||
YGNodeFree(m_node);
|
||||
}
|
||||
|
||||
void Node::reset(void)
|
||||
{
|
||||
m_measureFunc.reset(nullptr);
|
||||
|
||||
YGNodeReset(m_node);
|
||||
}
|
||||
|
||||
void Node::copyStyle(Node const & other)
|
||||
{
|
||||
YGNodeCopyStyle(m_node, other.m_node);
|
||||
}
|
||||
|
||||
void Node::setPositionType(int positionType)
|
||||
{
|
||||
YGNodeStyleSetPositionType(m_node, static_cast<YGPositionType>(positionType));
|
||||
}
|
||||
|
||||
void Node::setPosition(int edge, double position)
|
||||
{
|
||||
YGNodeStyleSetPosition(m_node, static_cast<YGEdge>(edge), position);
|
||||
}
|
||||
|
||||
void Node::setAlignContent(int alignContent)
|
||||
{
|
||||
YGNodeStyleSetAlignContent(m_node, static_cast<YGAlign>(alignContent));
|
||||
}
|
||||
|
||||
void Node::setAlignItems(int alignItems)
|
||||
{
|
||||
YGNodeStyleSetAlignItems(m_node, static_cast<YGAlign>(alignItems));
|
||||
}
|
||||
|
||||
void Node::setAlignSelf(int alignSelf)
|
||||
{
|
||||
YGNodeStyleSetAlignSelf(m_node, static_cast<YGAlign>(alignSelf));
|
||||
}
|
||||
|
||||
void Node::setFlexDirection(int flexDirection)
|
||||
{
|
||||
YGNodeStyleSetFlexDirection(m_node, static_cast<YGFlexDirection>(flexDirection));
|
||||
}
|
||||
|
||||
void Node::setFlexWrap(int flexWrap)
|
||||
{
|
||||
YGNodeStyleSetFlexWrap(m_node, static_cast<YGWrap>(flexWrap));
|
||||
}
|
||||
|
||||
void Node::setJustifyContent(int justifyContent)
|
||||
{
|
||||
YGNodeStyleSetJustifyContent(m_node, static_cast<YGJustify>(justifyContent));
|
||||
}
|
||||
|
||||
void Node::setMargin(int edge, double margin)
|
||||
{
|
||||
YGNodeStyleSetMargin(m_node, static_cast<YGEdge>(edge), margin);
|
||||
}
|
||||
|
||||
void Node::setOverflow(int overflow)
|
||||
{
|
||||
YGNodeStyleSetOverflow(m_node, static_cast<YGOverflow>(overflow));
|
||||
}
|
||||
|
||||
void Node::setFlex(double flex)
|
||||
{
|
||||
YGNodeStyleSetFlex(m_node, flex);
|
||||
}
|
||||
|
||||
void Node::setFlexBasis(double flexBasis)
|
||||
{
|
||||
YGNodeStyleSetFlexBasis(m_node, flexBasis);
|
||||
}
|
||||
|
||||
void Node::setFlexGrow(double flexGrow)
|
||||
{
|
||||
YGNodeStyleSetFlexGrow(m_node, flexGrow);
|
||||
}
|
||||
|
||||
void Node::setFlexShrink(double flexShrink)
|
||||
{
|
||||
YGNodeStyleSetFlexShrink(m_node, flexShrink);
|
||||
}
|
||||
|
||||
void Node::setWidth(double width)
|
||||
{
|
||||
YGNodeStyleSetWidth(m_node, width);
|
||||
}
|
||||
|
||||
void Node::setHeight(double height)
|
||||
{
|
||||
YGNodeStyleSetHeight(m_node, height);
|
||||
}
|
||||
|
||||
void Node::setMinWidth(double minWidth)
|
||||
{
|
||||
YGNodeStyleSetMinWidth(m_node, minWidth);
|
||||
}
|
||||
|
||||
void Node::setMinHeight(double minHeight)
|
||||
{
|
||||
YGNodeStyleSetMinHeight(m_node, minHeight);
|
||||
}
|
||||
|
||||
void Node::setMaxWidth(double maxWidth)
|
||||
{
|
||||
YGNodeStyleSetMaxWidth(m_node, maxWidth);
|
||||
}
|
||||
|
||||
void Node::setMaxHeight(double maxHeight)
|
||||
{
|
||||
YGNodeStyleSetMaxHeight(m_node, maxHeight);
|
||||
}
|
||||
|
||||
void Node::setAspectRatio(double aspectRatio)
|
||||
{
|
||||
YGNodeStyleSetAspectRatio(m_node, aspectRatio);
|
||||
}
|
||||
|
||||
void Node::setBorder(int edge, double border)
|
||||
{
|
||||
YGNodeStyleSetBorder(m_node, static_cast<YGEdge>(edge), border);
|
||||
}
|
||||
|
||||
void Node::setPadding(int edge, double padding)
|
||||
{
|
||||
YGNodeStyleSetPadding(m_node, static_cast<YGEdge>(edge), padding);
|
||||
}
|
||||
|
||||
int Node::getPositionType(void) const
|
||||
{
|
||||
return YGNodeStyleGetPositionType(m_node);
|
||||
}
|
||||
|
||||
double Node::getPosition(int edge) const
|
||||
{
|
||||
return YGNodeStyleGetPosition(m_node, static_cast<YGEdge>(edge));
|
||||
}
|
||||
|
||||
int Node::getAlignContent(void) const
|
||||
{
|
||||
return YGNodeStyleGetAlignContent(m_node);
|
||||
}
|
||||
|
||||
int Node::getAlignItems(void) const
|
||||
{
|
||||
return YGNodeStyleGetAlignItems(m_node);
|
||||
}
|
||||
|
||||
int Node::getAlignSelf(void) const
|
||||
{
|
||||
return YGNodeStyleGetAlignSelf(m_node);
|
||||
}
|
||||
|
||||
int Node::getFlexDirection(void) const
|
||||
{
|
||||
return YGNodeStyleGetFlexDirection(m_node);
|
||||
}
|
||||
|
||||
int Node::getFlexWrap(void) const
|
||||
{
|
||||
return YGNodeStyleGetFlexWrap(m_node);
|
||||
}
|
||||
|
||||
int Node::getJustifyContent(void) const
|
||||
{
|
||||
return YGNodeStyleGetJustifyContent(m_node);
|
||||
}
|
||||
|
||||
double Node::getMargin(int edge) const
|
||||
{
|
||||
return YGNodeStyleGetMargin(m_node, static_cast<YGEdge>(edge));
|
||||
}
|
||||
|
||||
int Node::getOverflow(void) const
|
||||
{
|
||||
return YGNodeStyleGetOverflow(m_node);
|
||||
}
|
||||
|
||||
double Node::getFlexBasis(void) const
|
||||
{
|
||||
return YGNodeStyleGetFlexBasis(m_node);
|
||||
}
|
||||
|
||||
double Node::getFlexGrow(void) const
|
||||
{
|
||||
return YGNodeStyleGetFlexGrow(m_node);
|
||||
}
|
||||
|
||||
double Node::getFlexShrink(void) const
|
||||
{
|
||||
return YGNodeStyleGetFlexShrink(m_node);
|
||||
}
|
||||
|
||||
double Node::getWidth(void) const
|
||||
{
|
||||
return YGNodeStyleGetWidth(m_node);
|
||||
}
|
||||
|
||||
double Node::getHeight(void) const
|
||||
{
|
||||
return YGNodeStyleGetHeight(m_node);
|
||||
}
|
||||
|
||||
double Node::getMinWidth(void) const
|
||||
{
|
||||
return YGNodeStyleGetMinWidth(m_node);
|
||||
}
|
||||
|
||||
double Node::getMinHeight(void) const
|
||||
{
|
||||
return YGNodeStyleGetMinHeight(m_node);
|
||||
}
|
||||
|
||||
double Node::getMaxWidth(void) const
|
||||
{
|
||||
return YGNodeStyleGetMaxWidth(m_node);
|
||||
}
|
||||
|
||||
double Node::getMaxHeight(void) const
|
||||
{
|
||||
return YGNodeStyleGetMaxHeight(m_node);
|
||||
}
|
||||
|
||||
double Node::getAspectRatio(void) const
|
||||
{
|
||||
return YGNodeStyleGetAspectRatio(m_node);
|
||||
}
|
||||
|
||||
double Node::getBorder(int edge) const
|
||||
{
|
||||
return YGNodeStyleGetBorder(m_node, static_cast<YGEdge>(edge));
|
||||
}
|
||||
|
||||
double Node::getPadding(int edge) const
|
||||
{
|
||||
return YGNodeStyleGetPadding(m_node, static_cast<YGEdge>(edge));
|
||||
}
|
||||
|
||||
void Node::insertChild(Node * child, unsigned index)
|
||||
{
|
||||
YGNodeInsertChild(m_node, child->m_node, index);
|
||||
}
|
||||
|
||||
void Node::removeChild(Node * child)
|
||||
{
|
||||
YGNodeRemoveChild(m_node, child->m_node);
|
||||
}
|
||||
|
||||
unsigned Node::getChildCount(void) const
|
||||
{
|
||||
return YGNodeGetChildCount(m_node);
|
||||
}
|
||||
|
||||
Node * Node::getParent(void)
|
||||
{
|
||||
auto nodePtr = YGNodeGetParent(m_node);
|
||||
|
||||
if (nodePtr == nullptr)
|
||||
return nullptr;
|
||||
|
||||
return Node::fromYGNode(nodePtr);
|
||||
}
|
||||
|
||||
Node * Node::getChild(unsigned index)
|
||||
{
|
||||
auto nodePtr = YGNodeGetChild(m_node, index);
|
||||
|
||||
if (nodePtr == nullptr)
|
||||
return nullptr;
|
||||
|
||||
return Node::fromYGNode(nodePtr);
|
||||
}
|
||||
|
||||
void Node::setMeasureFunc(nbind::cbFunction & measureFunc)
|
||||
{
|
||||
m_measureFunc.reset(new nbind::cbFunction(measureFunc));
|
||||
|
||||
YGNodeSetMeasureFunc(m_node, &globalMeasureFunc);
|
||||
}
|
||||
|
||||
void Node::unsetMeasureFunc(void)
|
||||
{
|
||||
m_measureFunc.reset(nullptr);
|
||||
|
||||
YGNodeSetMeasureFunc(m_node, nullptr);
|
||||
}
|
||||
|
||||
Size Node::callMeasureFunc(double width, int widthMode, double height, int heightMode) const
|
||||
{
|
||||
return m_measureFunc->call<Size>(width, widthMode, height, heightMode);
|
||||
}
|
||||
|
||||
void Node::markDirty(void)
|
||||
{
|
||||
YGNodeMarkDirty(m_node);
|
||||
}
|
||||
|
||||
bool Node::isDirty(void) const
|
||||
{
|
||||
return YGNodeIsDirty(m_node);
|
||||
}
|
||||
|
||||
void Node::calculateLayout(double width, double height, int direction)
|
||||
{
|
||||
YGNodeCalculateLayout(m_node, width, height, static_cast<YGDirection>(direction));
|
||||
}
|
||||
|
||||
double Node::getComputedLeft(void) const
|
||||
{
|
||||
return YGNodeLayoutGetLeft(m_node);
|
||||
}
|
||||
|
||||
double Node::getComputedRight(void) const
|
||||
{
|
||||
return YGNodeLayoutGetRight(m_node);
|
||||
}
|
||||
|
||||
double Node::getComputedTop(void) const
|
||||
{
|
||||
return YGNodeLayoutGetTop(m_node);
|
||||
}
|
||||
|
||||
double Node::getComputedBottom(void) const
|
||||
{
|
||||
return YGNodeLayoutGetBottom(m_node);
|
||||
}
|
||||
|
||||
double Node::getComputedWidth(void) const
|
||||
{
|
||||
return YGNodeLayoutGetWidth(m_node);
|
||||
}
|
||||
|
||||
double Node::getComputedHeight(void) const
|
||||
{
|
||||
return YGNodeLayoutGetHeight(m_node);
|
||||
}
|
||||
|
||||
Layout Node::getComputedLayout(void) const
|
||||
{
|
||||
Layout layout;
|
||||
|
||||
layout.left = YGNodeLayoutGetLeft(m_node);
|
||||
layout.right = YGNodeLayoutGetRight(m_node);
|
||||
|
||||
layout.top = YGNodeLayoutGetTop(m_node);
|
||||
layout.bottom = YGNodeLayoutGetBottom(m_node);
|
||||
|
||||
layout.width = YGNodeLayoutGetWidth(m_node);
|
||||
layout.height = YGNodeLayoutGetHeight(m_node);
|
||||
|
||||
return layout;
|
||||
}
|
174
javascript/sources/Node.hh
Normal file
174
javascript/sources/Node.hh
Normal file
@@ -0,0 +1,174 @@
|
||||
/**
|
||||
* Copyright (c) 2014-present, Facebook, Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the BSD-style license found in the
|
||||
* LICENSE file in the root directory of this source tree. An additional grant
|
||||
* of patent rights can be found in the PATENTS file in the same directory.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include <nbind/api.h>
|
||||
#include <nbind/BindDefiner.h>
|
||||
#include <yoga/Yoga.h>
|
||||
|
||||
#include "./Layout.hh"
|
||||
#include "./Size.hh"
|
||||
|
||||
class Node {
|
||||
|
||||
public:
|
||||
|
||||
static Node * create(void);
|
||||
static void destroy(Node * node);
|
||||
|
||||
public:
|
||||
|
||||
static Node * fromYGNode(YGNodeRef nodeRef);
|
||||
|
||||
private:
|
||||
|
||||
Node(void);
|
||||
|
||||
public:
|
||||
|
||||
~Node(void);
|
||||
|
||||
public: // Prevent accidental copy
|
||||
|
||||
Node(Node const &) = delete;
|
||||
|
||||
Node const & operator=(Node const &) = delete;
|
||||
|
||||
public:
|
||||
|
||||
void reset(void);
|
||||
|
||||
public: // Style setters
|
||||
|
||||
void copyStyle(Node const & other);
|
||||
|
||||
void setPositionType(int positionType);
|
||||
void setPosition(int edge, double position);
|
||||
|
||||
void setAlignContent(int alignContent);
|
||||
void setAlignItems(int alignItems);
|
||||
void setAlignSelf(int alignSelf);
|
||||
void setFlexDirection(int flexDirection);
|
||||
void setFlexWrap(int flexWrap);
|
||||
void setJustifyContent(int justifyContent);
|
||||
|
||||
void setMargin(int edge, double margin);
|
||||
|
||||
void setOverflow(int overflow);
|
||||
|
||||
void setFlex(double flex);
|
||||
void setFlexBasis(double flexBasis);
|
||||
void setFlexGrow(double flexGrow);
|
||||
void setFlexShrink(double flexShrink);
|
||||
|
||||
void setWidth(double width);
|
||||
void setHeight(double height);
|
||||
|
||||
void setMinWidth(double minWidth);
|
||||
void setMinHeight(double minHeight);
|
||||
|
||||
void setMaxWidth(double maxWidth);
|
||||
void setMaxHeight(double maxHeight);
|
||||
|
||||
void setAspectRatio(double aspectRatio);
|
||||
|
||||
void setBorder(int edge, double border);
|
||||
|
||||
void setPadding(int edge, double padding);
|
||||
|
||||
public: // Style getters
|
||||
|
||||
int getPositionType(void) const;
|
||||
double getPosition(int edge) const;
|
||||
|
||||
int getAlignContent(void) const;
|
||||
int getAlignItems(void) const;
|
||||
int getAlignSelf(void) const;
|
||||
int getFlexDirection(void) const;
|
||||
int getFlexWrap(void) const;
|
||||
int getJustifyContent(void) const;
|
||||
|
||||
double getMargin(int edge) const;
|
||||
|
||||
int getOverflow(void) const;
|
||||
|
||||
double getFlexBasis(void) const;
|
||||
double getFlexGrow(void) const;
|
||||
double getFlexShrink(void) const;
|
||||
|
||||
double getWidth(void) const;
|
||||
double getHeight(void) const;
|
||||
|
||||
double getMinWidth(void) const;
|
||||
double getMinHeight(void) const;
|
||||
|
||||
double getMaxWidth(void) const;
|
||||
double getMaxHeight(void) const;
|
||||
|
||||
double getAspectRatio(void) const;
|
||||
|
||||
double getBorder(int edge) const;
|
||||
|
||||
double getPadding(int edge) const;
|
||||
|
||||
public: // Tree hierarchy mutators
|
||||
|
||||
void insertChild(Node * child, unsigned index);
|
||||
void removeChild(Node * child);
|
||||
|
||||
public: // Tree hierarchy inspectors
|
||||
|
||||
unsigned getChildCount(void) const;
|
||||
|
||||
// The following functions cannot be const because they could discard const qualifiers (ex: constNode->getChild(0)->getParent() wouldn't be const)
|
||||
|
||||
Node * getParent(void);
|
||||
Node * getChild(unsigned index);
|
||||
|
||||
public: // Measure func mutators
|
||||
|
||||
void setMeasureFunc(nbind::cbFunction & measureFunc);
|
||||
void unsetMeasureFunc(void);
|
||||
|
||||
public: // Measure func inspectors
|
||||
|
||||
Size callMeasureFunc(double width, int widthMode, double height, int heightMode) const;
|
||||
|
||||
public: // Dirtiness accessors
|
||||
|
||||
void markDirty(void);
|
||||
bool isDirty(void) const;
|
||||
|
||||
public: // Layout mutators
|
||||
|
||||
void calculateLayout(double width, double height, int direction);
|
||||
|
||||
public: // Layout inspectors
|
||||
|
||||
double getComputedLeft(void) const;
|
||||
double getComputedRight(void) const;
|
||||
|
||||
double getComputedTop(void) const;
|
||||
double getComputedBottom(void) const;
|
||||
|
||||
double getComputedWidth(void) const;
|
||||
double getComputedHeight(void) const;
|
||||
|
||||
Layout getComputedLayout(void) const;
|
||||
|
||||
private:
|
||||
|
||||
YGNodeRef m_node;
|
||||
|
||||
std::unique_ptr<nbind::cbFunction> m_measureFunc;
|
||||
|
||||
};
|
36
javascript/sources/Size.hh
Normal file
36
javascript/sources/Size.hh
Normal file
@@ -0,0 +1,36 @@
|
||||
/**
|
||||
* Copyright (c) 2014-present, Facebook, Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the BSD-style license found in the
|
||||
* LICENSE file in the root directory of this source tree. An additional grant
|
||||
* of patent rights can be found in the PATENTS file in the same directory.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <nbind/api.h>
|
||||
#include <nbind/BindDefiner.h>
|
||||
|
||||
struct Size
|
||||
{
|
||||
double width;
|
||||
double height;
|
||||
|
||||
Size(void)
|
||||
: width(0.0)
|
||||
, height(0.0)
|
||||
{
|
||||
}
|
||||
|
||||
Size(double width, double height)
|
||||
: width(width)
|
||||
, height(height)
|
||||
{
|
||||
}
|
||||
|
||||
void toJS(nbind::cbOutput expose) const
|
||||
{
|
||||
expose(width, height);
|
||||
}
|
||||
};
|
32
javascript/sources/entry-browser.js
Normal file
32
javascript/sources/entry-browser.js
Normal file
@@ -0,0 +1,32 @@
|
||||
/**
|
||||
* Copyright (c) 2014-present, Facebook, Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the BSD-style license found in the
|
||||
* LICENSE file in the root directory of this source tree. An additional grant
|
||||
* of patent rights can be found in the PATENTS file in the same directory.
|
||||
*/
|
||||
|
||||
var nbind = require('../build/Release/nbind.js');
|
||||
|
||||
var ran = false;
|
||||
var ret = null;
|
||||
|
||||
nbind({}, function (err, result) {
|
||||
|
||||
if (ran)
|
||||
return;
|
||||
|
||||
ran = true;
|
||||
|
||||
if (err)
|
||||
throw err;
|
||||
|
||||
ret = result;
|
||||
|
||||
});
|
||||
|
||||
if (!ran)
|
||||
throw new Error('Failed to load the yoga module - it needed to be loaded synchronously, but didn\'t');
|
||||
|
||||
module.exports = require('./entry-common')(ret.bind, ret.lib);
|
205
javascript/sources/entry-common.js
Normal file
205
javascript/sources/entry-common.js
Normal file
@@ -0,0 +1,205 @@
|
||||
/**
|
||||
* Copyright (c) 2014-present, Facebook, Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the BSD-style license found in the
|
||||
* LICENSE file in the root directory of this source tree. An additional grant
|
||||
* of patent rights can be found in the PATENTS file in the same directory.
|
||||
*/
|
||||
|
||||
function patch(prototype, name, fn) {
|
||||
|
||||
let original = prototype[name];
|
||||
|
||||
prototype[name] = function (... args) {
|
||||
return fn.call(this, original, ... args);
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
module.exports = function (bind, lib) {
|
||||
|
||||
let constants = {};
|
||||
|
||||
constants.ALIGN_AUTO = 0;
|
||||
constants.ALIGN_CENTER = 2;
|
||||
constants.ALIGN_FLEX_END = 3;
|
||||
constants.ALIGN_FLEX_START = 1;
|
||||
constants.ALIGN_STRETCH = 4;
|
||||
|
||||
constants.DIRECTION_INHERIT = 0;
|
||||
constants.DIRECTION_LTR = 1;
|
||||
constants.DIRECTION_RTL = 2;
|
||||
|
||||
constants.EDGE_ALL = 8;
|
||||
constants.EDGE_BOTTOM = 3;
|
||||
constants.EDGE_END = 5;
|
||||
constants.EDGE_HORIZONTAL = 6;
|
||||
constants.EDGE_LEFT = 0;
|
||||
constants.EDGE_RIGHT = 2;
|
||||
constants.EDGE_START = 4;
|
||||
constants.EDGE_TOP = 1;
|
||||
constants.EDGE_VERTICAL = 7;
|
||||
|
||||
constants.FEATURE_ROUNDING = 0;
|
||||
constants.FEATURE_WEB_FLEX_BASIS = 1;
|
||||
|
||||
constants.FLEX_DIRECTION_COLUMN = 0;
|
||||
constants.FLEX_DIRECTION_COLUMN_REVERSE = 1;
|
||||
constants.FLEX_DIRECTION_ROW = 2;
|
||||
constants.FLEX_DIRECTION_ROW_REVERSE = 3;
|
||||
|
||||
constants.JUSTIFY_CENTER = 1;
|
||||
constants.JUSTIFY_FLEX_END = 2;
|
||||
constants.JUSTIFY_FLEX_START = 0;
|
||||
constants.JUSTIFY_SPACE_AROUND = 4;
|
||||
constants.JUSTIFY_SPACE_BETWEEN = 3;
|
||||
|
||||
constants.MEASURE_MODE_UNDEFINED = 0;
|
||||
constants.MEASURE_MODE_EXACTLY = 1;
|
||||
constants.MEASURE_MODE_AT_MOST = 2;
|
||||
|
||||
constants.OVERFLOW_HIDDEN = 1;
|
||||
constants.OVERFLOW_VISIBLE = 0;
|
||||
constants.OVERFLOW_SCROLL = 2;
|
||||
|
||||
constants.POSITION_TYPE_ABSOLUTE = 1;
|
||||
constants.POSITION_TYPE_RELATIVE = 0;
|
||||
|
||||
constants.WRAP_NO_WRAP = 0;
|
||||
constants.WRAP_WRAP = 1;
|
||||
|
||||
constants.UNDEFINED = NaN;
|
||||
|
||||
class Layout {
|
||||
|
||||
constructor(left, right, top, bottom, width, height) {
|
||||
|
||||
this.left = left;
|
||||
this.right = right;
|
||||
|
||||
this.top = top;
|
||||
this.bottom = bottom;
|
||||
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
|
||||
}
|
||||
|
||||
fromJS(expose) {
|
||||
|
||||
expose(this.left, this.right, this.top, this.bottom, this.width, this.height);
|
||||
|
||||
}
|
||||
|
||||
toString() {
|
||||
|
||||
return `<Layout#${this.left}:${this.right};${this.top}:${this.bottom};${this.width}:${this.height}>`;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class Size {
|
||||
|
||||
static fromJS({ width, height }) {
|
||||
|
||||
return new Size(width, height);
|
||||
|
||||
}
|
||||
|
||||
constructor(width, height) {
|
||||
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
|
||||
}
|
||||
|
||||
fromJS(expose) {
|
||||
|
||||
expose(this.width, this.height);
|
||||
|
||||
}
|
||||
|
||||
toString() {
|
||||
|
||||
return `<Size#${this.width}x${this.height}>`;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
patch(lib.Node.prototype, `free`, function () {
|
||||
|
||||
// Since we handle the memory allocation ourselves (via lib.Node.create), we also need to handle the deallocation
|
||||
|
||||
lib.Node.destroy(this);
|
||||
|
||||
});
|
||||
|
||||
patch(lib.Node.prototype, `freeRecursive`, function () {
|
||||
|
||||
for (let t = 0, T = this.getChildCount(); t < T; ++t)
|
||||
this.getChild(0).freeRecursive();
|
||||
|
||||
this.free();
|
||||
|
||||
});
|
||||
|
||||
patch(lib.Node.prototype, `setMeasureFunc`, function (original, measureFunc) {
|
||||
|
||||
// This patch is just a convenience patch, since it helps write more idiomatic source code (such as .setMeasureFunc(null))
|
||||
// We also automatically convert the return value of the measureFunc to a Size object, so that we can return anything that has .width and .height properties
|
||||
|
||||
if (measureFunc) {
|
||||
return original.call(this, (... args) => Size.fromJS(measureFunc(... args)));
|
||||
} else {
|
||||
return this.unsetMeasureFunc();
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
patch(lib.Node.prototype, `calculateLayout`, function (original, width = constants.UNDEFINED, height = constants.UNDEFINED, direction = constants.DIRECTION_LTR) {
|
||||
|
||||
// Just a small patch to add support for the function default parameters
|
||||
|
||||
return original.call(this, width, height, direction);
|
||||
|
||||
});
|
||||
|
||||
function setExperimentalFeatureEnabled(... args) {
|
||||
|
||||
return lib.setExperimentalFeatureEnabled(... args);
|
||||
|
||||
}
|
||||
|
||||
function isExperimentalFeatureEnabled(... args) {
|
||||
|
||||
return lib.isExperimentalFeatureEnabled(... args);
|
||||
|
||||
}
|
||||
|
||||
function getInstanceCount(... args) {
|
||||
|
||||
return lib.getInstanceCount(... args);
|
||||
|
||||
}
|
||||
|
||||
bind(`Layout`, Layout);
|
||||
bind(`Size`, Size);
|
||||
|
||||
return Object.assign({
|
||||
|
||||
Node: lib.Node,
|
||||
|
||||
Layout,
|
||||
Size,
|
||||
|
||||
setExperimentalFeatureEnabled,
|
||||
isExperimentalFeatureEnabled,
|
||||
|
||||
getInstanceCount
|
||||
|
||||
}, constants);
|
||||
|
||||
};
|
13
javascript/sources/entry-node.js
Normal file
13
javascript/sources/entry-node.js
Normal file
@@ -0,0 +1,13 @@
|
||||
/**
|
||||
* Copyright (c) 2014-present, Facebook, Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the BSD-style license found in the
|
||||
* LICENSE file in the root directory of this source tree. An additional grant
|
||||
* of patent rights can be found in the PATENTS file in the same directory.
|
||||
*/
|
||||
|
||||
var nbind = require('nbind');
|
||||
var ret = nbind.init(__dirname + '/../');
|
||||
|
||||
module.exports = require('./entry-common')(ret.bind, ret.lib);
|
27
javascript/sources/global.cc
Normal file
27
javascript/sources/global.cc
Normal file
@@ -0,0 +1,27 @@
|
||||
/**
|
||||
* Copyright (c) 2014-present, Facebook, Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the BSD-style license found in the
|
||||
* LICENSE file in the root directory of this source tree. An additional grant
|
||||
* of patent rights can be found in the PATENTS file in the same directory.
|
||||
*/
|
||||
|
||||
#include <yoga/Yoga.h>
|
||||
|
||||
#include "./global.hh"
|
||||
|
||||
void setExperimentalFeatureEnabled(int feature, bool enabled)
|
||||
{
|
||||
YGSetExperimentalFeatureEnabled(static_cast<YGExperimentalFeature>(feature), enabled);
|
||||
}
|
||||
|
||||
bool isExperimentalFeatureEnabled(int feature)
|
||||
{
|
||||
return YGIsExperimentalFeatureEnabled(static_cast<YGExperimentalFeature>(feature));
|
||||
}
|
||||
|
||||
unsigned getInstanceCount(void)
|
||||
{
|
||||
return YGNodeGetInstanceCount();
|
||||
}
|
15
javascript/sources/global.hh
Normal file
15
javascript/sources/global.hh
Normal file
@@ -0,0 +1,15 @@
|
||||
/**
|
||||
* Copyright (c) 2014-present, Facebook, Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the BSD-style license found in the
|
||||
* LICENSE file in the root directory of this source tree. An additional grant
|
||||
* of patent rights can be found in the PATENTS file in the same directory.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
void setExperimentalFeatureEnabled(int feature, bool enabled);
|
||||
bool isExperimentalFeatureEnabled(int feature);
|
||||
|
||||
unsigned getInstanceCount(void);
|
139
javascript/sources/nbind.cc
Normal file
139
javascript/sources/nbind.cc
Normal file
@@ -0,0 +1,139 @@
|
||||
/**
|
||||
* Copyright (c) 2014-present, Facebook, Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the BSD-style license found in the
|
||||
* LICENSE file in the root directory of this source tree. An additional grant
|
||||
* of patent rights can be found in the PATENTS file in the same directory.
|
||||
*/
|
||||
|
||||
#include <yoga/Yoga.h>
|
||||
|
||||
#include "./Node.hh"
|
||||
#include "./Layout.hh"
|
||||
#include "./Size.hh"
|
||||
#include "./global.hh"
|
||||
|
||||
#define NBIND_DUPLICATE_POINTERS true
|
||||
|
||||
#include <nbind/nbind.h>
|
||||
|
||||
NBIND_GLOBAL()
|
||||
{
|
||||
function(setExperimentalFeatureEnabled);
|
||||
function(isExperimentalFeatureEnabled);
|
||||
function(getInstanceCount);
|
||||
}
|
||||
|
||||
NBIND_CLASS(Size)
|
||||
{
|
||||
construct<>();
|
||||
construct<double, double>();
|
||||
}
|
||||
|
||||
NBIND_CLASS(Layout)
|
||||
{
|
||||
construct<>();
|
||||
}
|
||||
|
||||
NBIND_CLASS(Node)
|
||||
{
|
||||
method(create);
|
||||
method(destroy);
|
||||
|
||||
method(reset);
|
||||
|
||||
method(copyStyle);
|
||||
|
||||
method(setPositionType);
|
||||
method(setPosition);
|
||||
|
||||
method(setAlignContent);
|
||||
method(setAlignItems);
|
||||
method(setAlignSelf);
|
||||
method(setFlexDirection);
|
||||
method(setFlexWrap);
|
||||
method(setJustifyContent);
|
||||
|
||||
method(setMargin);
|
||||
|
||||
method(setOverflow);
|
||||
|
||||
method(setFlex);
|
||||
method(setFlexBasis);
|
||||
method(setFlexGrow);
|
||||
method(setFlexShrink);
|
||||
|
||||
method(setWidth);
|
||||
method(setHeight);
|
||||
|
||||
method(setMinWidth);
|
||||
method(setMinHeight);
|
||||
|
||||
method(setMaxWidth);
|
||||
method(setMaxHeight);
|
||||
|
||||
method(setAspectRatio);
|
||||
|
||||
method(setBorder);
|
||||
|
||||
method(setPadding);
|
||||
|
||||
method(getPositionType);
|
||||
method(getPosition);
|
||||
|
||||
method(getAlignContent);
|
||||
method(getAlignItems);
|
||||
method(getAlignSelf);
|
||||
method(getFlexDirection);
|
||||
method(getFlexWrap);
|
||||
method(getJustifyContent);
|
||||
|
||||
method(getMargin);
|
||||
|
||||
method(getFlexBasis);
|
||||
method(getFlexGrow);
|
||||
method(getFlexShrink);
|
||||
|
||||
method(getWidth);
|
||||
method(getHeight);
|
||||
|
||||
method(getMinWidth);
|
||||
method(getMinHeight);
|
||||
|
||||
method(getMaxWidth);
|
||||
method(getMaxHeight);
|
||||
|
||||
method(getAspectRatio);
|
||||
|
||||
method(getBorder);
|
||||
|
||||
method(getPadding);
|
||||
|
||||
method(insertChild);
|
||||
method(removeChild);
|
||||
|
||||
method(getChildCount);
|
||||
|
||||
method(getParent);
|
||||
method(getChild);
|
||||
|
||||
method(setMeasureFunc);
|
||||
method(unsetMeasureFunc);
|
||||
|
||||
method(markDirty);
|
||||
method(isDirty);
|
||||
|
||||
method(calculateLayout);
|
||||
|
||||
method(getComputedLeft);
|
||||
method(getComputedRight);
|
||||
|
||||
method(getComputedTop);
|
||||
method(getComputedBottom);
|
||||
|
||||
method(getComputedWidth);
|
||||
method(getComputedHeight);
|
||||
|
||||
method(getComputedLayout);
|
||||
}
|
Reference in New Issue
Block a user