Skip to content

Commit

Permalink
Add JavaScript bindings for Errata API
Browse files Browse the repository at this point in the history
Summary: Wire C ABI to embind to expose to JS

Differential Revision: https://internalfb.com/D45297215

fbshipit-source-id: bf1bb8761d764062169f48efa7675841b0941db1
  • Loading branch information
NickGerleman authored and facebook-github-bot committed Apr 27, 2023
1 parent c9fbf45 commit a9e4a02
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 4 deletions.
13 changes: 13 additions & 0 deletions javascript/src_js/wrapAsm.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type {
Direction,
Display,
Edge,
Errata,
ExperimentalFeature,
FlexDirection,
Gutter,
Expand Down Expand Up @@ -52,8 +53,20 @@ export type Config = {
enabled: boolean
): void;
setPointScaleFactor(factor: number): void;
/**
* @deprecated Please use "getErrata()"
*/
useLegacyStretchBehaviour(): boolean;
/**
* @deprecated "setUseLegacyStretchBehaviour" will be removed in the next
* release. Usage should be replaced with "setErrata(ERRATA_ALL)" to opt out
* of all future breaking conformance fixes, or
* "setErrata(ERRATA_STRETCH_FLEX_BASIS)" to opt out of the specific
* conformance fix previously disabled by "UseLegacyStretchBehaviour".
*/
setUseLegacyStretchBehaviour(useLegacyStretchBehaviour: boolean): void;
getErrata(): Errata,
setErrata(errata: Errata): void,
useWebDefaults(): boolean;
setUseWebDefaults(useWebDefaults: boolean): void;
};
Expand Down
12 changes: 10 additions & 2 deletions javascript/src_native/Config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ void Config::setUseLegacyStretchBehaviour(bool useLegacyStretchBehaviour) {
YGConfigSetUseLegacyStretchBehaviour(m_config, useLegacyStretchBehaviour);
}

void Config::setErrata(int errata) {
YGConfigSetErrata(m_config, static_cast<YGErrata>(errata));
}

void Config::setUseWebDefaults(bool useWebDefaults) {
YGConfigSetUseWebDefaults(m_config, useWebDefaults);
}
Expand All @@ -45,10 +49,14 @@ bool Config::isExperimentalFeatureEnabled(int feature) const {
m_config, static_cast<YGExperimentalFeature>(feature));
}

bool Config::useLegacyStretchBehaviour() {
bool Config::useLegacyStretchBehaviour() const {
return YGConfigGetUseLegacyStretchBehaviour(m_config);
}

bool Config::useWebDefaults() {
int Config::getErrata() const {
return static_cast<int>(YGConfigGetErrata(m_config));
}

bool Config::useWebDefaults() const {
return YGConfigGetUseWebDefaults(m_config);
}
6 changes: 4 additions & 2 deletions javascript/src_native/Config.hh
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,14 @@ public: // Setters
void setExperimentalFeatureEnabled(int feature, bool enabled);
void setPointScaleFactor(float pixelsInPoint);
void setUseLegacyStretchBehaviour(bool useLegacyStretchBehaviour);
void setErrata(int errata);
void setUseWebDefaults(bool useWebDefaults);

public: // Getters
bool isExperimentalFeatureEnabled(int feature) const;
bool useLegacyStretchBehaviour();
bool useWebDefaults();
bool useLegacyStretchBehaviour() const;
int getErrata() const;
bool useWebDefaults() const;

private:
YGConfigRef m_config;
Expand Down
2 changes: 2 additions & 0 deletions javascript/src_native/embind.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@ EMSCRIPTEN_BINDINGS(YOGA_LAYOUT) {
.function("setPointScaleFactor", &Config::setPointScaleFactor)
.function(
"setUseLegacyStretchBehaviour", &Config::setUseLegacyStretchBehaviour)
.function("setErrata", &Config::setErrata)
.function("setUseWebDefaults", &Config::setUseWebDefaults)
.function(
"isExperimentalFeatureEnabled", &Config::isExperimentalFeatureEnabled)
.function("useLegacyStretchBehaviour", &Config::useLegacyStretchBehaviour)
.function("getErrata", &Config::getErrata)
.function("useWebDefaults", &Config::useWebDefaults);

value_object<Layout>("Layout")
Expand Down
38 changes: 38 additions & 0 deletions javascript/tests/YGErrataTest.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* 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.
*/

test("errata_all_contains_example_errata", () => {
const config = Yoga.Config.create();
config.setErrata(Yoga.ERRATA_ALL);

expect(config.getErrata()).toBe(Yoga.ERRATA_ALL);
expect(config.getErrata() & Yoga.ERRATA_STRETCH_FLEX_BASIS).not.toBe(0);

config.free();
});

test("errata_none_omits_example_errata", () => {
const config = Yoga.Config.create();
config.setErrata(Yoga.ERRATA_NONE);

expect(config.getErrata()).toBe(Yoga.ERRATA_NONE);
expect(config.getErrata() & Yoga.ERRATA_STRETCH_FLEX_BASIS).toBe(0);

config.free();
});

test("errata_is_settable", () => {
const config = Yoga.Config.create();

config.setErrata(Yoga.ERRATA_ALL);
expect(config.getErrata()).toBe(Yoga.ERRATA_ALL);

config.setErrata(Yoga.ERRATA_NONE);
expect(config.getErrata()).toBe(Yoga.ERRATA_NONE);

config.free();
});

0 comments on commit a9e4a02

Please sign in to comment.