Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gentest/gentest-java.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ JavaEmitter.prototype = Object.create(Emitter.prototype, {
}},

YGNodeStyleSetDisplay:{value:function(nodeName, value) {
this.push(nodeName + '.setDisplay(' + toValueJavascript(value) + ');');
this.push(nodeName + '.setDisplay(' + toValueJava(value) + ');');
}},

YGNodeStyleSetFlexBasis:{value:function(nodeName, value) {
Expand Down
13 changes: 5 additions & 8 deletions gentest/gentest-javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,12 @@ JavascriptEmitter.prototype = Object.create(Emitter.prototype, {
emitTestPrologue:{value:function(name, experiments) {
this.push('it(' + JSON.stringify(name) + ', function () {');
this.pushIndent();
this.push('var config = Yoga.Config.create();');
this.push('');

if (experiments.length > 0) {
for (var i in experiments) {
this.push('Yoga.setExperimentalFeatureEnabled(Yoga.EXPERIMENTAL_FEATURE_' + toJavascriptUpper(experiments[i]) + ', true);');
this.push('config.setExperimentalFeatureEnabled(Yoga.EXPERIMENTAL_FEATURE_' + toJavascriptUpper(experiments[i]) + ', true);');
}
this.push('');
}
Expand All @@ -69,13 +71,8 @@ JavascriptEmitter.prototype = Object.create(Emitter.prototype, {
this.push('root.freeRecursive();');
this.popIndent();
this.push('}');

if (experiments.length > 0) {
this.push('');
for (var i in experiments) {
this.push('Yoga.setExperimentalFeatureEnabled(Yoga.EXPERIMENTAL_FEATURE_' + toJavascriptUpper(experiments[i]) + ', false);');
}
}
this.push('');
this.push('config.free();');

this.popIndent();
this.push('}');
Expand Down
2 changes: 2 additions & 0 deletions gentest/gentest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
f.write eval(logs[2].message.sub(/^[^"]*/, '')).sub('YogaTest', name)
f.close

print logs[4]

f = File.open("../javascript/tests/Facebook.Yoga/#{name}.js", 'w')
f.write eval(logs[3].message.sub(/^[^"]*/, '')).sub('YogaTest', name)
f.close
Expand Down
1 change: 1 addition & 0 deletions javascript/binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"sources": [
"sources/yoga/YGNodeList.c",
"sources/yoga/Yoga.c",
"sources/Config.cc",
"sources/Node.cc",
"sources/global.cc",
"sources/nbind.cc"
Expand Down
3 changes: 2 additions & 1 deletion javascript/final-flags.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
[ "1==1", {

"cflags_cc": [
"-std=c++14"
"-std=c++14",
"-DNBIND_DUPLICATE_POINTERS"
],

"xcode_settings": {
Expand Down
42 changes: 42 additions & 0 deletions javascript/sources/Config.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* 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 "./Config.hh"

/* static */ Config * Config::create(void)
{
return new Config();
}

/* static */ void Config::destroy(Config * node)
{
delete node;
}

Config::Config(void)
: m_config(YGConfigNew())
{
}

Config::~Config(void)
{
YGConfigFree(m_config);
}

void Config::setExperimentalFeatureEnabled(int feature, bool enabled)
{
YGConfigSetExperimentalFeatureEnabled(m_config, static_cast<YGExperimentalFeature>(feature), enabled);
}

bool Config::isExperimentalFeatureEnabled(int feature) const
{
return YGConfigIsExperimentalFeatureEnabled(m_config, static_cast<YGExperimentalFeature>(feature));
}
51 changes: 27 additions & 24 deletions javascript/sources/Config.hh
Original file line number Diff line number Diff line change
Expand Up @@ -14,36 +14,39 @@
#include <yoga/Yoga.h>

class Config {
private:
YGConfigRef m_config;

Config(void)
: m_config(YGConfigNew())
{}
friend class Node;

public:
public:

static Config * create(void)
{
return new Config();
}
static Config * create(void);

static void destroy(Config * config)
{
delete config;
}
static void destroy(Config * config);

~Config(void)
{
YGConfigFree(m_config);
}
private:

void setExperimentalFeatureEnabled(int feature, bool enabled)
{
YGConfigSetExperimentalFeatureEnabled(m_config, static_cast<YGExperimentalFeature>(feature), enabled);
}
Config(void);

Config(Config const &) = delete;
public:

~Config(void);

public: // Prevent accidental copy

Config(Config const &) = delete;

Config const & operator=(Config const &) = delete;

public: // Setters

void setExperimentalFeatureEnabled(int feature, bool enabled);

public: // Getters

bool isExperimentalFeatureEnabled(int feature) const;

private:

YGConfigRef m_config;

Config const & operator=(Config const &) = delete;
};
13 changes: 9 additions & 4 deletions javascript/sources/Node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,14 @@ static YGSize globalMeasureFunc(YGNodeRef nodeRef, float width, YGMeasureMode wi
return ygSize;
}

/* static */ Node * Node::create(void)
/* static */ Node * Node::createDefault(void)
{
return new Node();
return new Node(nullptr);
}

/* static */ Node * Node::createWithConfig(Config * config)
{
return new Node(config);
}

/* static */ void Node::destroy(Node * node)
Expand All @@ -41,8 +46,8 @@ static YGSize globalMeasureFunc(YGNodeRef nodeRef, float width, YGMeasureMode wi
return reinterpret_cast<Node *>(YGNodeGetContext(nodeRef));
}

Node::Node(void)
: m_node(YGNodeNew())
Node::Node(Config * config)
: m_node(config != nullptr ? YGNodeNewWithConfig(config->m_config) : YGNodeNew())
, m_measureFunc(nullptr)
{
YGNodeSetContext(m_node, reinterpret_cast<void *>(this));
Expand Down
7 changes: 4 additions & 3 deletions javascript/sources/Node.hh
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ class Node {

public:

static Node * create(void);
static Node * create(Config * config);
static Node * createDefault(void);
static Node * createWithConfig(Config * config);

static void destroy(Node * node);

public:
Expand All @@ -34,7 +35,7 @@ class Node {

private:

Node(void);
Node(Config * config);

public:

Expand Down
32 changes: 17 additions & 15 deletions javascript/sources/entry-common.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,22 @@ module.exports = function (bind, lib) {

}

patch(lib.Config.prototype, `free`, function () {

// Since we handle the memory allocation ourselves (via lib.Config.create), we also need to handle the deallocation

lib.Config.destroy(this);

});

patch(lib.Node, `create`, function (_, config) {

// We decide the constructor we want to call depending on the parameters

return config ? lib.Node.createWithConfig(config) : lib.Node.createDefault();

});

patch(lib.Node.prototype, `free`, function () {

// Since we handle the memory allocation ourselves (via lib.Node.create), we also need to handle the deallocation
Expand Down Expand Up @@ -207,18 +223,6 @@ module.exports = function (bind, lib) {

});

function setExperimentalFeatureEnabled(... args) {

return lib.setExperimentalFeatureEnabled(... args);

}

function isExperimentalFeatureEnabled(... args) {

return lib.isExperimentalFeatureEnabled(... args);

}

function getInstanceCount(... args) {

return lib.getInstanceCount(... args);
Expand All @@ -231,15 +235,13 @@ module.exports = function (bind, lib) {

return Object.assign({

Config: lib.Config,
Node: lib.Node,

Layout,
Size,
Value,

setExperimentalFeatureEnabled,
isExperimentalFeatureEnabled,

getInstanceCount

}, constants);
Expand Down
9 changes: 6 additions & 3 deletions javascript/sources/nbind.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
#include "./Config.hh"
#include "./global.hh"

#define NBIND_DUPLICATE_POINTERS true

#include <nbind/nbind.h>

NBIND_GLOBAL()
Expand Down Expand Up @@ -45,13 +43,18 @@ NBIND_CLASS(Value)
NBIND_CLASS(Config)
{
method(create);

method(destroy);

method(setExperimentalFeatureEnabled);

method(isExperimentalFeatureEnabled);
}

NBIND_CLASS(Node)
{
method(create);
method(createDefault);
method(createWithConfig);
method(destroy);

method(reset);
Expand Down
Loading