-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.ts
170 lines (150 loc) · 4.05 KB
/
mod.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/**
* 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.
*/
// deno-lint-ignore-file no-explicit-any
import type { Layout, Node, Yoga } from "./src/yoga.ts";
import { constants, Direction, Unit } from "./src/yoga_const.ts";
// hack: for tests
Object.defineProperty(globalThis, "YGBENCHMARK", {
get: () => Deno.test,
});
let module!: { exports: Yoga };
if (typeof Deno === "object") {
const { dlopen } = await import("node:process");
module = { exports: {} } as any;
dlopen(
module,
new URL("./build/libyoga_node_api.dylib", import.meta.url).pathname,
);
} else if (typeof require !== "undefined") {
const r = require;
module = r(
"NSBundle" in globalThis
? "./Contents/Frameworks/libyoga_node_api.dylib"
: "./build/libyoga_node_api.dylib",
);
} else {
throw new Error("Unhandled runtime");
}
Object.assign(module.exports, constants);
function patch(prototype: any, name: string, fn: any) {
const original = prototype[name];
prototype[name] = function (...args: any[]) {
return fn.call(this, original, ...args);
};
}
const lib = module.exports as any;
for (
const fnName of [
"setPosition",
"setMargin",
"setFlexBasis",
"setWidth",
"setHeight",
"setMinWidth",
"setMinHeight",
"setMaxWidth",
"setMaxHeight",
"setPadding",
"setGap",
]
) {
const methods: Record<
Unit,
(this: any, ...args: any[]) => unknown
> = {
[Unit.Point]: lib.Node.prototype[fnName],
[Unit.Percent]: lib.Node.prototype[`${fnName}Percent`],
[Unit.Auto]: lib.Node.prototype[`${fnName}Auto`],
[Unit.Undefined]: lib.Node.prototype[`${fnName}Auto`],
};
patch(
lib.Node.prototype,
fnName,
function (this: any, _original: unknown, ...args: any[]) {
// We patch all these functions to add support for the following calls:
// .setWidth(100) / .setWidth("100%") / .setWidth(.getWidth()) / .setWidth("auto")
const value = args.pop();
let unit: Unit, asNumber;
if (value === "auto") {
unit = Unit.Auto;
asNumber = undefined;
} else if (typeof value === "object") {
unit = value.unit;
asNumber = value.valueOf();
} else {
unit = typeof value === "string" && value.endsWith("%")
? Unit.Percent
: Unit.Point;
asNumber = parseFloat(value);
if (
value !== undefined &&
!Number.isNaN(value) &&
Number.isNaN(asNumber)
) {
throw new Error(`Invalid value ${value} for ${fnName}`);
}
}
if (!methods[unit as Unit]) {
throw new Error(
`Failed to execute "${fnName}": Unsupported unit '${value}'`,
);
}
if (asNumber !== undefined) {
return methods[unit].call(this, ...args, asNumber);
} else {
return methods[unit].call(this, ...args);
}
},
);
}
patch(
lib.Node.prototype,
"calculateLayout",
function (
this: Node,
original: (
this: Node,
width: number,
height: number,
direction: Direction,
) => Layout,
width = NaN,
height = NaN,
direction = Direction.LTR,
) {
// Just a small patch to add support for the function default parameters
return original.call(this, width, height, direction);
},
);
function wrapMeasureFunction(
measureFunction: (...args: any[]) => Layout,
) {
return (...args: any[]) => {
const { width, height } = measureFunction(...args);
return {
width: width ?? NaN,
height: height ?? NaN,
};
};
}
patch(
lib.Node.prototype,
"setMeasureFunc",
function (
this: Node,
original: (
this: Node,
measureFunc: (width: number, height: number) => Layout,
) => void,
measureFunc: (width: number, height: number) => Layout,
) {
return original.call(this, wrapMeasureFunction(measureFunc) as any);
},
);
export * from "./src/yoga_const.ts";
export * from "./src/yoga.ts";
export default module.exports;