Files
yoga/javascript/src_js/wrapAsm.js

148 lines
4.1 KiB
JavaScript
Raw Normal View History

2022-12-23 02:43:45 -08:00
/**
* 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.
*
* @format
*/
2022-12-24 01:46:20 -08:00
const CONSTANTS = require("./generated/YGEnums");
2022-12-23 02:43:45 -08:00
module.exports = (lib) => {
function patch(prototype, name, fn) {
2022-12-24 02:17:10 -08:00
const original = prototype[name];
2022-12-23 02:43:45 -08:00
2022-12-24 01:46:20 -08:00
prototype[name] = function (...args) {
2022-12-23 02:43:45 -08:00
return fn.call(this, original, ...args);
};
}
2022-12-24 02:17:10 -08:00
for (const fnName of [
2022-12-24 01:46:20 -08:00
"setPosition",
"setMargin",
"setFlexBasis",
"setWidth",
"setHeight",
"setMinWidth",
"setMinHeight",
"setMaxWidth",
"setMaxHeight",
"setPadding",
2022-12-23 02:43:45 -08:00
]) {
2022-12-24 02:17:10 -08:00
const methods = {
2022-12-23 02:43:45 -08:00
[CONSTANTS.UNIT_POINT]: lib.Node.prototype[fnName],
[CONSTANTS.UNIT_PERCENT]: lib.Node.prototype[`${fnName}Percent`],
[CONSTANTS.UNIT_AUTO]: lib.Node.prototype[`${fnName}Auto`],
};
2022-12-24 01:46:20 -08:00
patch(lib.Node.prototype, fnName, function (original, ...args) {
2022-12-23 02:43:45 -08:00
// We patch all these functions to add support for the following calls:
// .setWidth(100) / .setWidth("100%") / .setWidth(.getWidth()) / .setWidth("auto")
2022-12-24 02:17:10 -08:00
const value = args.pop();
2022-12-23 02:43:45 -08:00
let unit, asNumber;
2022-12-24 01:46:20 -08:00
if (value === "auto") {
2022-12-23 02:43:45 -08:00
unit = CONSTANTS.UNIT_AUTO;
asNumber = undefined;
2022-12-24 01:46:20 -08:00
} else if (typeof value === "object") {
2022-12-23 02:43:45 -08:00
unit = value.unit;
asNumber = value.valueOf();
} else {
unit =
2022-12-24 01:46:20 -08:00
typeof value === "string" && value.endsWith("%")
2022-12-23 02:43:45 -08:00
? CONSTANTS.UNIT_PERCENT
: CONSTANTS.UNIT_POINT;
asNumber = parseFloat(value);
if (!Number.isNaN(value) && Number.isNaN(asNumber)) {
throw new Error(`Invalid value ${value} for ${fnName}`);
}
}
if (!methods[unit])
throw new Error(
2022-12-24 01:46:20 -08:00
`Failed to execute "${fnName}": Unsupported unit '${value}'`
2022-12-23 02:43:45 -08:00
);
if (asNumber !== undefined) {
return methods[unit].call(this, ...args, asNumber);
} else {
return methods[unit].call(this, ...args);
}
});
}
function wrapMeasureFunction(measureFunction) {
2022-12-24 01:46:20 -08:00
return lib.MeasureCallback.implement({ measure: measureFunction });
2022-12-23 02:43:45 -08:00
}
2022-12-24 01:46:20 -08:00
patch(lib.Node.prototype, "setMeasureFunc", function (original, measureFunc) {
original.call(this, wrapMeasureFunction(measureFunc));
});
2022-12-23 02:43:45 -08:00
function wrapDirtiedFunc(dirtiedFunction) {
2022-12-24 01:46:20 -08:00
return lib.DirtiedCallback.implement({ dirtied: dirtiedFunction });
2022-12-23 02:43:45 -08:00
}
2022-12-24 01:46:20 -08:00
patch(lib.Node.prototype, "setDirtiedFunc", function (original, dirtiedFunc) {
original.call(this, wrapDirtiedFunc(dirtiedFunc));
});
2022-12-23 02:43:45 -08:00
2022-12-24 01:46:20 -08:00
patch(lib.Config.prototype, "free", function () {
2022-12-23 02:43:45 -08:00
// Since we handle the memory allocation ourselves (via lib.Config.create),
// we also need to handle the deallocation
lib.Config.destroy(this);
});
2022-12-24 02:17:10 -08:00
patch(lib.Node, "create", (_, config) => {
2022-12-23 02:43:45 -08:00
// We decide the constructor we want to call depending on the parameters
return config
? lib.Node.createWithConfig(config)
: lib.Node.createDefault();
});
2022-12-24 01:46:20 -08:00
patch(lib.Node.prototype, "free", function () {
2022-12-23 02:43:45 -08:00
// Since we handle the memory allocation ourselves (via lib.Node.create),
// we also need to handle the deallocation
lib.Node.destroy(this);
});
2022-12-24 01:46:20 -08:00
patch(lib.Node.prototype, "freeRecursive", function () {
2022-12-23 02:43:45 -08:00
for (let t = 0, T = this.getChildCount(); t < T; ++t) {
this.getChild(0).freeRecursive();
}
this.free();
});
2022-12-24 01:46:20 -08:00
patch(lib.Node.prototype, "setMeasureFunc", function (original, measureFunc) {
2022-12-23 02:43:45 -08:00
// This patch is just a convenience patch, since it helps write more
// idiomatic source code (such as .setMeasureFunc(null))
if (measureFunc) {
2022-12-24 01:46:20 -08:00
return original.call(this, (...args) => measureFunc(...args));
2022-12-23 02:43:45 -08:00
} else {
return this.unsetMeasureFunc();
}
});
2022-12-24 01:46:20 -08:00
patch(
lib.Node.prototype,
"calculateLayout",
function (
original,
width = NaN,
height = NaN,
direction = CONSTANTS.DIRECTION_LTR
) {
// Just a small patch to add support for the function default parameters
return original.call(this, width, height, direction);
}
);
2022-12-23 02:43:45 -08:00
return {
Config: lib.Config,
Node: lib.Node,
...CONSTANTS,
};
};