embedded-react 0.21.0 → 0.22.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/aot/compile.mjs +1 -1
- package/aot/compiler/alerts.mjs +604 -0
- package/aot/compiler/check.mjs +1 -1
- package/aot/compiler/collect.mjs +1 -1
- package/aot/compiler/elements/button.mjs +3 -3
- package/aot/compiler/elements/image-background.mjs +1 -1
- package/aot/compiler/elements/props.mjs +10 -6
- package/aot/compiler/elements/section-list.mjs +1 -1
- package/aot/compiler/expressions.mjs +29 -2
- package/aot/compiler/handlers.mjs +73 -16
- package/aot/compiler/keyboard-dismiss.mjs +99 -0
- package/aot/compiler/keyboard.mjs +1 -1
- package/aot/compiler/lower.mjs +12 -0
- package/aot/compiler/nodes.mjs +17 -9
- package/aot/compiler/out.mjs +4 -0
- package/aot/compiler/package/codegen.mjs +36 -2
- package/aot/compiler/package/format.mjs +5 -3
- package/aot/compiler/package/lower.mjs +53 -19
- package/aot/compiler/scroll-to.mjs +112 -0
- package/aot/compiler/style-text.mjs +2 -2
- package/aot/compiler/svg.mjs +6 -3
- package/aot/compiler/updaters.mjs +1 -0
- package/aot/style-map.mjs +44 -1
- package/assets/bake-font.mjs +187 -244
- package/assets/bake-image.mjs +47 -71
- package/assets/bake-svg.mjs +465 -533
- package/assets/build-builtin-font.mjs +14 -27
- package/assets/emit-c.mjs +124 -204
- package/assets/emit-container.mjs +129 -197
- package/assets/emit-pack.mjs +92 -136
- package/assets/font-config.mjs +19 -27
- package/assets/font-sizes.mjs +432 -507
- package/assets/glyph-coverage.mjs +170 -214
- package/assets/index.mjs +52 -97
- package/assets/rasterize.mjs +173 -194
- package/assets/svg-loader.mjs +42 -48
- package/build.mjs +84 -101
- package/cli.mjs +482 -621
- package/compile-bin.mjs +24 -36
- package/flash.mjs +297 -401
- package/hotreload/dev-device.mjs +229 -270
- package/hotreload/pack-app.mjs +118 -159
- package/hotreload/pack-split.mjs +144 -200
- package/hotreload/package-transfer.mjs +128 -131
- package/hotreload/protocol.mjs +118 -118
- package/hotreload/uploader.mjs +132 -159
- package/hotreload/watch-tree.mjs +56 -65
- package/pack-container.mjs +93 -123
- package/package.json +19 -16
- package/persist-transform.mjs +89 -105
- package/qjsc-wasm.mjs +47 -72
- package/sim/embedded-react.wasm +0 -0
- package/sim/index.html +1 -1
- package/sim-server.mjs +260 -354
- package/src/embedded-react/Alert.mjs +112 -0
- package/src/embedded-react/Animated.mjs +412 -0
- package/src/embedded-react/{AppRegistry.js → AppRegistry.mjs} +26 -25
- package/src/embedded-react/{Button.js → Button.mjs} +29 -53
- package/src/embedded-react/{Dimensions.js → Dimensions.mjs} +12 -17
- package/src/embedded-react/Easing.mjs +34 -0
- package/src/embedded-react/{FlatList.js → FlatList.mjs} +25 -42
- package/src/embedded-react/{ImageBackground.js → ImageBackground.mjs} +11 -22
- package/src/embedded-react/Keyboard.mjs +82 -0
- package/src/embedded-react/{LayoutAnimation.js → LayoutAnimation.mjs} +17 -28
- package/src/embedded-react/PanResponder.mjs +256 -0
- package/src/embedded-react/{Platform.js → Platform.mjs} +8 -9
- package/src/embedded-react/{Pressable.js → Pressable.mjs} +12 -25
- package/src/embedded-react/SectionList.mjs +103 -0
- package/src/embedded-react/{StyleSheet.js → StyleSheet.mjs} +16 -20
- package/src/embedded-react/{TouchableOpacity.js → TouchableOpacity.mjs} +47 -83
- package/src/embedded-react/background-image.mjs +321 -0
- package/src/embedded-react/{components.js → components.mjs} +1 -3
- package/src/embedded-react/{imperative.js → imperative.mjs} +27 -38
- package/src/embedded-react/index.d.ts +155 -2
- package/src/embedded-react/index.js +26 -19
- package/src/embedded-react/layout-anim-config.mjs +85 -0
- package/src/embedded-react/{list-child.js → list-child.mjs} +11 -20
- package/src/embedded-react/{named-colors.js → named-colors.mjs} +2 -6
- package/src/embedded-react/nativeModule.mjs +371 -0
- package/src/embedded-react/split-style.mjs +57 -0
- package/src/embedded-react/svg-ops.mjs +1075 -0
- package/src/embedded-react/{useHostValue.js → useHostValue.mjs} +2 -4
- package/src/embedded-react/{usePersistentState.js → usePersistentState.mjs} +34 -38
- package/src/embedded-react/{warn-props.js → warn-props.mjs} +32 -39
- package/src/host-config.mjs +512 -0
- package/src/{native-ui.js → native-ui.mjs} +1 -8
- package/src/props.mjs +341 -0
- package/src/{renderer.js → renderer.mjs} +30 -44
- package/src/embedded-react/Animated.js +0 -440
- package/src/embedded-react/Easing.js +0 -39
- package/src/embedded-react/PanResponder.js +0 -330
- package/src/embedded-react/SectionList.js +0 -146
- package/src/embedded-react/layout-anim-config.js +0 -101
- package/src/embedded-react/nativeModule.js +0 -462
- package/src/embedded-react/split-style.js +0 -58
- package/src/embedded-react/svg-ops.js +0 -1295
- package/src/host-config.js +0 -548
- package/src/props.js +0 -306
|
@@ -0,0 +1,512 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2026 Cory Lamming
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
import { DefaultEventPriority } from 'react-reconciler/constants.js';
|
|
17
|
+
import { NativeUI } from './native-ui.mjs';
|
|
18
|
+
import { buildProps, buildTextSpans, deepEqualProps, isEventProp, isTextContent, } from './props.mjs';
|
|
19
|
+
import { flattenSvg, PAINT_PROPS, warnVectorCaps, scaleVectorArtifact, encodeVectorGradients, } from './embedded-react/svg-ops.mjs';
|
|
20
|
+
import { splitAnimatedStyle } from './embedded-react/split-style.mjs';
|
|
21
|
+
// --- Animated bindings ---------------------------------------------------------------------------
|
|
22
|
+
// A bound prop is owned by the engine from then on: it re-pushes the animated value over the static
|
|
23
|
+
// props of every later setProps (er_animation_reapply_bound), which is what keeps a re-render from jumping
|
|
24
|
+
// a running animation back to its declared value. Nothing in React says "this prop stopped being
|
|
25
|
+
// animated", so the binding has to be released explicitly or it keeps writing the prop forever — and a
|
|
26
|
+
// prop that swapped Animated.Values ends up written by both. Instances are bare node handles with no
|
|
27
|
+
// JS object to hang state on, so what we bound is remembered here: handle → (prop → animated value).
|
|
28
|
+
const boundValues = new Map();
|
|
29
|
+
/**
|
|
30
|
+
* True when two animated values bind a prop the same way, so a re-render can leave the live binding
|
|
31
|
+
* alone. Identity settles an Animated.Value; an interpolation is a throwaway object that
|
|
32
|
+
* `value.interpolate(...)` rebuilds on every render, so it answers __bindEq for itself.
|
|
33
|
+
*/
|
|
34
|
+
function sameBinding(previous, next) {
|
|
35
|
+
if (previous === next)
|
|
36
|
+
return true;
|
|
37
|
+
if (previous == null || next == null)
|
|
38
|
+
return false;
|
|
39
|
+
return typeof previous.__bindEq === 'function' && previous.__bindEq(next);
|
|
40
|
+
}
|
|
41
|
+
/** The value `bindings` (from splitAnimatedStyle) drives `prop` with, or undefined if none does. */
|
|
42
|
+
function bindingFor(bindings, prop) {
|
|
43
|
+
for (const binding of bindings) {
|
|
44
|
+
if (binding.prop === prop)
|
|
45
|
+
return binding.value;
|
|
46
|
+
}
|
|
47
|
+
return undefined;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Releases the bindings a node has that its new props no longer ask for — a prop that went back to a
|
|
51
|
+
* static value, or moved to a different Animated.Value.
|
|
52
|
+
*
|
|
53
|
+
* Runs BEFORE the node's setProps, and that order is the whole point: setProps re-pushes every value
|
|
54
|
+
* still bound to the node over the static props it just wrote, so a binding released afterwards would
|
|
55
|
+
* leave its last animated float sitting on the node instead of the value React just set.
|
|
56
|
+
*/
|
|
57
|
+
function releaseStaleBindings(handle, bindings) {
|
|
58
|
+
const bound = boundValues.get(handle);
|
|
59
|
+
if (bound === undefined)
|
|
60
|
+
return;
|
|
61
|
+
for (const [prop, value] of bound) {
|
|
62
|
+
if (sameBinding(value, bindingFor(bindings, prop)))
|
|
63
|
+
continue;
|
|
64
|
+
NativeUI.animUnbind(handle, prop);
|
|
65
|
+
bound.delete(prop);
|
|
66
|
+
}
|
|
67
|
+
if (bound.size === 0)
|
|
68
|
+
boundValues.delete(handle);
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Binds the props that aren't already bound to the value asking for them. The engine ignores a
|
|
72
|
+
* duplicate bind, but the bridge crossing to find that out isn't free on an MCU.
|
|
73
|
+
*/
|
|
74
|
+
function applyBindings(handle, bindings) {
|
|
75
|
+
if (bindings.length === 0)
|
|
76
|
+
return;
|
|
77
|
+
let bound = boundValues.get(handle);
|
|
78
|
+
for (const binding of bindings) {
|
|
79
|
+
if (bound !== undefined &&
|
|
80
|
+
sameBinding(bound.get(binding.prop), binding.value))
|
|
81
|
+
continue;
|
|
82
|
+
binding.value.__bind(handle, binding.prop);
|
|
83
|
+
if (bound === undefined) {
|
|
84
|
+
bound = new Map();
|
|
85
|
+
boundValues.set(handle, bound);
|
|
86
|
+
}
|
|
87
|
+
bound.set(binding.prop, binding.value);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Forgets what a handle was bound to. The engine releases the bindings itself when a node is destroyed
|
|
92
|
+
* (er_animation_unbind_node), so this only drops our record — but it has to happen, or the map keeps a
|
|
93
|
+
* record for every destroyed node. Called on destroy for the node React hands us, and again from
|
|
94
|
+
* detachDeletedInstance because destroyNode reclaims a whole SUBTREE in C — React never hands
|
|
95
|
+
* removeChild the descendants, and detaching is where it names them.
|
|
96
|
+
*/
|
|
97
|
+
function forgetBindings(handle) {
|
|
98
|
+
boundValues.delete(handle);
|
|
99
|
+
}
|
|
100
|
+
// --- Failed creates ------------------------------------------------------------------------------
|
|
101
|
+
// createNode returns 0 when the node pool is full. React creates children before their parent, so the
|
|
102
|
+
// children of a parent that did not fit are never attached to anything, and removing that parent frees
|
|
103
|
+
// nothing in C. Each failed create gets its own negative instance (the bridge ignores handles <= 0), and
|
|
104
|
+
// the children appended to it are kept here so they are freed when it is deleted.
|
|
105
|
+
const FAILED_CREATE = 0; // createNode's handle when the pool is full
|
|
106
|
+
const orphansOf = new Map(); // failed instance → Set of child handles
|
|
107
|
+
let nextFailedInstance = -1;
|
|
108
|
+
/** Records `child` as held by `parent` when `parent` is a failed create. */
|
|
109
|
+
function adoptIfOrphan(parent, child) {
|
|
110
|
+
if (parent >= 0 || child <= 0)
|
|
111
|
+
return;
|
|
112
|
+
let orphans = orphansOf.get(parent);
|
|
113
|
+
if (orphans === undefined) {
|
|
114
|
+
orphans = new Set();
|
|
115
|
+
orphansOf.set(parent, orphans);
|
|
116
|
+
}
|
|
117
|
+
orphans.add(child);
|
|
118
|
+
}
|
|
119
|
+
/** Frees the children a failed create was holding. */
|
|
120
|
+
function freeOrphans(instance) {
|
|
121
|
+
const orphans = orphansOf.get(instance);
|
|
122
|
+
if (orphans === undefined)
|
|
123
|
+
return;
|
|
124
|
+
orphansOf.delete(instance);
|
|
125
|
+
for (const child of orphans) {
|
|
126
|
+
NativeUI.destroyNode(child);
|
|
127
|
+
forgetBindings(child);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Applies a node's resolved props, binding any Animated.Value found in its `style` to the matching
|
|
132
|
+
* node prop (native driver). This makes animated styles work on ANY host element — `<Pressable
|
|
133
|
+
* style={{ transform: [{ scale: v }] }}>` binds without an Animated.* wrapper — which is what the
|
|
134
|
+
* Flow B AOT compiler does too, so the two render paths stay in parity (and what an Animated.* wrapper
|
|
135
|
+
* relies on: it forwards its style untouched and is bound here like any other element).
|
|
136
|
+
*
|
|
137
|
+
* Returns the resolved flat style so a paired applyTextSpans call can reuse it instead of
|
|
138
|
+
* re-flattening props.style from scratch — style flattening (recursive array walk and object merge)
|
|
139
|
+
* is one of the costlier steps in commitUpdate's JS-side work on a bytecode-interpreted MCU target.
|
|
140
|
+
*/
|
|
141
|
+
function applyProps(type, handle, props) {
|
|
142
|
+
const { staticStyle, bindings } = splitAnimatedStyle(props.style);
|
|
143
|
+
// <Dial value={animatedValue}>: the value is a native-driver binding (ER_PROP_ARC_VALUE), not a prop —
|
|
144
|
+
// the engine ramps it with no JS per frame and damages only the swept sliver.
|
|
145
|
+
if (type === 'Dial') {
|
|
146
|
+
if (props.value && props.value.__animated) {
|
|
147
|
+
bindings.push({ prop: 'value', value: props.value });
|
|
148
|
+
props = { ...props, value: undefined };
|
|
149
|
+
}
|
|
150
|
+
if (props.valueStart && props.valueStart.__animated) {
|
|
151
|
+
bindings.push({ prop: 'valueStart', value: props.valueStart });
|
|
152
|
+
props = { ...props, valueStart: undefined };
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
releaseStaleBindings(handle, bindings);
|
|
156
|
+
NativeUI.setProps(handle, buildProps(type, props, staticStyle));
|
|
157
|
+
applyBindings(handle, bindings);
|
|
158
|
+
return staticStyle;
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Applies inline-styled text spans for a <Text> node (no-op for other types). buildTextSpans returns
|
|
162
|
+
* [] for uniform text, which reverts the node to plain-text rendering — so this also clears stale
|
|
163
|
+
* spans when a Text changes from styled-runs to a single style across renders. `flatStyle`, when
|
|
164
|
+
* given, is the already-resolved style from a paired applyProps call on the same props (avoids a
|
|
165
|
+
* second full flattening of props.style).
|
|
166
|
+
*/
|
|
167
|
+
function applyTextSpans(type, handle, props, flatStyle) {
|
|
168
|
+
if (type === 'Text')
|
|
169
|
+
NativeUI.setTextSpans(handle, buildTextSpans(props, flatStyle));
|
|
170
|
+
}
|
|
171
|
+
/** Resolves an <Svg>'s render-box dimension from style/props, falling back to the source's intrinsic size. */
|
|
172
|
+
function svgBoxSize(props, dimension, intrinsic) {
|
|
173
|
+
const styleSize = props.style && typeof props.style[dimension] === 'number'
|
|
174
|
+
? props.style[dimension]
|
|
175
|
+
: undefined;
|
|
176
|
+
const propSize = typeof props[dimension] === 'number' ? props[dimension] : undefined;
|
|
177
|
+
return styleSize ?? propSize ?? intrinsic;
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* A `<Svg source={imported}>` whose imported .svg fell back to a RASTER image at build time (the SVG used
|
|
181
|
+
* features the vector baker can't represent). Returns the artifact, or null for a vector/declarative
|
|
182
|
+
* Svg. Such a Svg is rendered as an IMAGE node, not a vector node — `props.source.kind` is the discriminator
|
|
183
|
+
* (an imported artifact's kind is fixed at build time, so it never flips for a given element).
|
|
184
|
+
*/
|
|
185
|
+
function rasterSvgArtifact(type, props) {
|
|
186
|
+
const source = props && props.source;
|
|
187
|
+
return type === 'Svg' && source && source.kind === 'raster' ? source : null;
|
|
188
|
+
}
|
|
189
|
+
/** Maps a raster `<Svg source>` to equivalent `<Image>` props: the baked asset name + the resolved box size. */
|
|
190
|
+
function rasterImageProps(props, artifact) {
|
|
191
|
+
const width = svgBoxSize(props, 'width', artifact.width);
|
|
192
|
+
const height = svgBoxSize(props, 'height', artifact.height);
|
|
193
|
+
return {
|
|
194
|
+
...props,
|
|
195
|
+
source: artifact.name,
|
|
196
|
+
style: { ...(props.style || {}), width, height },
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Sets an <Svg>'s vector op-tape. Two sources, source prop wins:
|
|
201
|
+
* <Svg source={imported}> — an imported .svg's baked artifact ({kind:'vector', ops, paints, width,
|
|
202
|
+
* height}); we scale its op-tape from intrinsic px to the node's box.
|
|
203
|
+
* <Svg><Path/>...</Svg> — declarative children flattened here (the Svg owns its subtree; React does
|
|
204
|
+
* not mount the shape children, so we compile them on create + every update).
|
|
205
|
+
*/
|
|
206
|
+
function applyVectorOps(type, handle, props) {
|
|
207
|
+
if (type !== 'Svg')
|
|
208
|
+
return;
|
|
209
|
+
let ops;
|
|
210
|
+
let paints;
|
|
211
|
+
let gradients;
|
|
212
|
+
const source = props.source;
|
|
213
|
+
if (source && source.kind === 'vector' && Array.isArray(source.ops)) {
|
|
214
|
+
({ ops, paints, gradients } = scaleVectorArtifact(source, svgBoxSize(props, 'width', source.width), svgBoxSize(props, 'height', source.height)));
|
|
215
|
+
}
|
|
216
|
+
else {
|
|
217
|
+
({ ops, paints, gradients } = flattenSvg(props));
|
|
218
|
+
}
|
|
219
|
+
warnVectorCaps(ops.length, paints.length, NativeUI.maxVectorOps, NativeUI.maxVectorPaints, gradients ? gradients.length : 0, NativeUI.maxVectorGrads);
|
|
220
|
+
NativeUI.setVectorOps(handle, ops, paints, gradients && gradients.length
|
|
221
|
+
? encodeVectorGradients(gradients)
|
|
222
|
+
: undefined);
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* True when a `<Svg source={vectorArtifact}>`'s resolved render box changed between renders — the
|
|
226
|
+
* one style-driven input to the op-tape (the tape is scaled from the artifact's intrinsic px to the
|
|
227
|
+
* box, see scaleVectorArtifact). A style change that leaves the box alone (a position move during a
|
|
228
|
+
* drag, a color tweak) does not need the tape re-uploaded.
|
|
229
|
+
*/
|
|
230
|
+
function svgSourceBoxChanged(prevProps, nextProps) {
|
|
231
|
+
const source = nextProps.source;
|
|
232
|
+
const intrinsicWidth = source && source.kind === 'vector' ? source.width : undefined;
|
|
233
|
+
const intrinsicHeight = source && source.kind === 'vector' ? source.height : undefined;
|
|
234
|
+
return (svgBoxSize(prevProps, 'width', intrinsicWidth) !==
|
|
235
|
+
svgBoxSize(nextProps, 'width', intrinsicWidth) ||
|
|
236
|
+
svgBoxSize(prevProps, 'height', intrinsicHeight) !==
|
|
237
|
+
svgBoxSize(nextProps, 'height', intrinsicHeight));
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* Diffs a re-rendered host element's props and returns what commitUpdate must re-apply — or null
|
|
241
|
+
* when nothing observable changed, which makes React skip the commit entirely (no Update effect, no
|
|
242
|
+
* bridge traffic). Re-marshaling props across the JS->C bridge is the dominant cost of a Flow A
|
|
243
|
+
* re-render, and a parent's state change re-renders every non-memoized child with identical props,
|
|
244
|
+
* so the null case is the common one on interactive screens.
|
|
245
|
+
*
|
|
246
|
+
* The payload is a set of section flags, so a partial change re-applies only its section:
|
|
247
|
+
* props — NativeUI.setProps (resolved style + passthrough props + text)
|
|
248
|
+
* spans — inline text spans (<Text> only; derived from children + style)
|
|
249
|
+
* vector — the <Svg> op-tape upload (flattenSvg/scaleVectorArtifact + setVectorOps)
|
|
250
|
+
* events — on* handler registration (an inline closure has a new identity every render and must be
|
|
251
|
+
* re-stored — its captures went stale — but that alone shouldn't re-serialize the props)
|
|
252
|
+
*
|
|
253
|
+
* The vector flag tracks the op-tape's actual inputs. A source artifact tape depends on the source
|
|
254
|
+
* reference and the resolved box; a declarative <Svg><Path/></Svg> tape depends on the shape
|
|
255
|
+
* children, viewBox, the direct width/height props, and its own paint props, which cascade to its
|
|
256
|
+
* shapes (flattenSvg never reads style). So a position-only style move of either kind — the
|
|
257
|
+
* interactive-drag hot path — skips the upload.
|
|
258
|
+
*
|
|
259
|
+
* Props buildProps never reads still set the props flag when they change — conservative, and only
|
|
260
|
+
* paid when such a value actually changed.
|
|
261
|
+
*/
|
|
262
|
+
function diffUpdate(type, prevProps, nextProps) {
|
|
263
|
+
let props = false;
|
|
264
|
+
let spans = false;
|
|
265
|
+
let events = false;
|
|
266
|
+
let vector = false;
|
|
267
|
+
const diffKey = (key) => {
|
|
268
|
+
const previousValue = prevProps[key];
|
|
269
|
+
const nextValue = nextProps[key];
|
|
270
|
+
if (Object.is(previousValue, nextValue))
|
|
271
|
+
return;
|
|
272
|
+
if (isEventProp(key, previousValue) || isEventProp(key, nextValue)) {
|
|
273
|
+
events = true;
|
|
274
|
+
return;
|
|
275
|
+
}
|
|
276
|
+
switch (key) {
|
|
277
|
+
case 'children':
|
|
278
|
+
// Only subtree-owning types render children through their OWN props (shouldSetTextContent);
|
|
279
|
+
// everywhere else children are separate host instances React mutates directly.
|
|
280
|
+
if (type === 'Text') {
|
|
281
|
+
if (!deepEqualProps(previousValue, nextValue)) {
|
|
282
|
+
props = true; // flat.text is built from children
|
|
283
|
+
spans = true;
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
else if (type === 'Svg') {
|
|
287
|
+
if (!deepEqualProps(previousValue, nextValue))
|
|
288
|
+
vector = true;
|
|
289
|
+
}
|
|
290
|
+
return;
|
|
291
|
+
case 'style':
|
|
292
|
+
if (!deepEqualProps(previousValue, nextValue)) {
|
|
293
|
+
props = true;
|
|
294
|
+
if (type === 'Text')
|
|
295
|
+
spans = true;
|
|
296
|
+
if (type === 'Svg' && nextProps.source)
|
|
297
|
+
vector = vector || svgSourceBoxChanged(prevProps, nextProps);
|
|
298
|
+
}
|
|
299
|
+
return;
|
|
300
|
+
case 'source':
|
|
301
|
+
props = true; // imageName (Image / raster Svg fallback)
|
|
302
|
+
if (type === 'Svg')
|
|
303
|
+
vector = true; // artifact swap → new tape
|
|
304
|
+
return;
|
|
305
|
+
case 'width':
|
|
306
|
+
case 'height':
|
|
307
|
+
case 'viewBox':
|
|
308
|
+
if (type === 'Svg') {
|
|
309
|
+
props = true; // width/height fold into the resolved style box
|
|
310
|
+
vector = true; // and drive the tape's root transform / artifact scale
|
|
311
|
+
return;
|
|
312
|
+
}
|
|
313
|
+
// fall through: on other types these are ordinary (ignored or passthrough) props
|
|
314
|
+
default:
|
|
315
|
+
if (!deepEqualProps(previousValue, nextValue)) {
|
|
316
|
+
props = true;
|
|
317
|
+
// A source tape ignores root paint.
|
|
318
|
+
if (type === 'Svg' &&
|
|
319
|
+
!nextProps.source &&
|
|
320
|
+
PAINT_PROPS.includes(key))
|
|
321
|
+
vector = true;
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
};
|
|
325
|
+
for (const key in prevProps)
|
|
326
|
+
diffKey(key);
|
|
327
|
+
for (const key in nextProps) {
|
|
328
|
+
if (!Object.prototype.hasOwnProperty.call(prevProps, key))
|
|
329
|
+
diffKey(key);
|
|
330
|
+
}
|
|
331
|
+
return props || spans || events || vector
|
|
332
|
+
? { props, spans, events, vector }
|
|
333
|
+
: null;
|
|
334
|
+
}
|
|
335
|
+
/**
|
|
336
|
+
* Registers/clears on* event handlers. A handler present in old but not new props is cleared.
|
|
337
|
+
*/
|
|
338
|
+
function applyEvents(handle, prevProps, nextProps) {
|
|
339
|
+
if (prevProps) {
|
|
340
|
+
for (const key in prevProps) {
|
|
341
|
+
if (isEventProp(key, prevProps[key]) &&
|
|
342
|
+
!(nextProps && isEventProp(key, nextProps[key]))) {
|
|
343
|
+
NativeUI.setEvent(handle, key, null);
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
for (const key in nextProps) {
|
|
348
|
+
if (isEventProp(key, nextProps[key])) {
|
|
349
|
+
NativeUI.setEvent(handle, key, nextProps[key]);
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
export const hostConfig = {
|
|
354
|
+
supportsMutation: true,
|
|
355
|
+
supportsPersistence: false,
|
|
356
|
+
supportsHydration: false,
|
|
357
|
+
isPrimaryRenderer: true,
|
|
358
|
+
noTimeout: -1,
|
|
359
|
+
warnsIfNotActing: false,
|
|
360
|
+
// --- Context (we carry none) ---
|
|
361
|
+
getRootHostContext() {
|
|
362
|
+
return {};
|
|
363
|
+
},
|
|
364
|
+
getChildHostContext(parentContext) {
|
|
365
|
+
return parentContext;
|
|
366
|
+
},
|
|
367
|
+
getPublicInstance(instance) {
|
|
368
|
+
return instance;
|
|
369
|
+
},
|
|
370
|
+
// --- Commit lifecycle ---
|
|
371
|
+
// These two bracket React's mutation phase exactly, which is the only place the reconciler touches
|
|
372
|
+
// the bridge — so on an instrumented build they are what makes the marshaling bucket cover the JS
|
|
373
|
+
// glue (applyProps, style flattening) and not just the NativeUI calls it ends up making.
|
|
374
|
+
prepareForCommit() {
|
|
375
|
+
NativeUI.perfMarshalBegin?.();
|
|
376
|
+
return null;
|
|
377
|
+
},
|
|
378
|
+
resetAfterCommit() {
|
|
379
|
+
NativeUI.perfMarshalEnd?.();
|
|
380
|
+
NativeUI.commit();
|
|
381
|
+
},
|
|
382
|
+
// --- Creation ---
|
|
383
|
+
createInstance(type, props) {
|
|
384
|
+
// A raster-fallback <Svg source> becomes a real Image node (the SVG was rasterized at build time).
|
|
385
|
+
const raster = rasterSvgArtifact(type, props);
|
|
386
|
+
if (raster) {
|
|
387
|
+
const handle = NativeUI.createNode('Image');
|
|
388
|
+
if (handle === FAILED_CREATE)
|
|
389
|
+
return nextFailedInstance--;
|
|
390
|
+
applyProps('Image', handle, rasterImageProps(props, raster));
|
|
391
|
+
applyEvents(handle, null, props);
|
|
392
|
+
return handle;
|
|
393
|
+
}
|
|
394
|
+
const handle = NativeUI.createNode(type);
|
|
395
|
+
if (handle === FAILED_CREATE)
|
|
396
|
+
return nextFailedInstance--;
|
|
397
|
+
const flatStyle = applyProps(type, handle, props);
|
|
398
|
+
applyTextSpans(type, handle, props, flatStyle);
|
|
399
|
+
applyVectorOps(type, handle, props);
|
|
400
|
+
applyEvents(handle, null, props);
|
|
401
|
+
return handle;
|
|
402
|
+
},
|
|
403
|
+
createTextInstance(text) {
|
|
404
|
+
// Raw text is only legal inside <Text> (handled via shouldSetTextContent). This fallback
|
|
405
|
+
// wraps stray text in a Text node so it still renders rather than crashing.
|
|
406
|
+
const handle = NativeUI.createNode('Text');
|
|
407
|
+
NativeUI.setProps(handle, { text: String(text) });
|
|
408
|
+
return handle;
|
|
409
|
+
},
|
|
410
|
+
appendInitialChild(parent, child) {
|
|
411
|
+
NativeUI.appendChild(parent, child);
|
|
412
|
+
adoptIfOrphan(parent, child);
|
|
413
|
+
},
|
|
414
|
+
finalizeInitialChildren() {
|
|
415
|
+
return false;
|
|
416
|
+
},
|
|
417
|
+
shouldSetTextContent(type, props) {
|
|
418
|
+
// Own the whole subtree for any flattenable <Text> (strings, interpolation, nested <Text>): React
|
|
419
|
+
// skips mounting children and we render them via the node's text + spans. Non-flattenable content
|
|
420
|
+
// (e.g. a <View> inside <Text>) returns false, falling back to mounted child instances.
|
|
421
|
+
// <Svg> also owns its subtree: the shape children are flattened into the vector op-tape
|
|
422
|
+
// (applyVectorOps), never mounted as host nodes.
|
|
423
|
+
return type === 'Svg' || (type === 'Text' && isTextContent(props.children));
|
|
424
|
+
},
|
|
425
|
+
// --- Mutation ---
|
|
426
|
+
appendChild(parent, child) {
|
|
427
|
+
NativeUI.appendChild(parent, child);
|
|
428
|
+
adoptIfOrphan(parent, child);
|
|
429
|
+
},
|
|
430
|
+
appendChildToContainer(container, child) {
|
|
431
|
+
NativeUI.appendChild(container, child);
|
|
432
|
+
},
|
|
433
|
+
insertBefore(parent, child, beforeChild) {
|
|
434
|
+
NativeUI.insertBefore(parent, child, beforeChild);
|
|
435
|
+
adoptIfOrphan(parent, child);
|
|
436
|
+
},
|
|
437
|
+
insertInContainerBefore(container, child, beforeChild) {
|
|
438
|
+
NativeUI.insertBefore(container, child, beforeChild);
|
|
439
|
+
},
|
|
440
|
+
removeChild(parent, child) {
|
|
441
|
+
NativeUI.removeChild(parent, child);
|
|
442
|
+
orphansOf.get(parent)?.delete(child);
|
|
443
|
+
NativeUI.destroyNode(child);
|
|
444
|
+
forgetBindings(child);
|
|
445
|
+
},
|
|
446
|
+
removeChildFromContainer(container, child) {
|
|
447
|
+
NativeUI.removeChild(container, child);
|
|
448
|
+
NativeUI.destroyNode(child);
|
|
449
|
+
forgetBindings(child);
|
|
450
|
+
},
|
|
451
|
+
clearContainer() {
|
|
452
|
+
// Children are removed individually via removeChildFromContainer.
|
|
453
|
+
},
|
|
454
|
+
prepareUpdate(instance, type, oldProps, newProps) {
|
|
455
|
+
// Runs in the render phase. null → React schedules no Update effect and commitUpdate never
|
|
456
|
+
// fires for this node — the win that makes an unchanged re-render bridge-free.
|
|
457
|
+
return diffUpdate(type, oldProps, newProps);
|
|
458
|
+
},
|
|
459
|
+
commitUpdate(instance, payload, type, prevProps, nextProps) {
|
|
460
|
+
const raster = rasterSvgArtifact(type, nextProps);
|
|
461
|
+
if (raster) {
|
|
462
|
+
// The Svg instance is an Image node (raster fallback); re-apply as image props, never vector ops.
|
|
463
|
+
if (payload.props)
|
|
464
|
+
applyProps('Image', instance, rasterImageProps(nextProps, raster));
|
|
465
|
+
if (payload.events)
|
|
466
|
+
applyEvents(instance, prevProps, nextProps);
|
|
467
|
+
return;
|
|
468
|
+
}
|
|
469
|
+
// payload.spans is only ever set alongside payload.props for Text nodes (see diffUpdate), so
|
|
470
|
+
// flatStyle is defined whenever applyTextSpans actually needs it.
|
|
471
|
+
let flatStyle;
|
|
472
|
+
if (payload.props)
|
|
473
|
+
flatStyle = applyProps(type, instance, nextProps);
|
|
474
|
+
if (payload.spans)
|
|
475
|
+
applyTextSpans(type, instance, nextProps, flatStyle);
|
|
476
|
+
if (payload.vector)
|
|
477
|
+
applyVectorOps(type, instance, nextProps);
|
|
478
|
+
if (payload.events)
|
|
479
|
+
applyEvents(instance, prevProps, nextProps);
|
|
480
|
+
},
|
|
481
|
+
commitTextUpdate(textInstance, _oldText, newText) {
|
|
482
|
+
NativeUI.setProps(textInstance, { text: String(newText) });
|
|
483
|
+
},
|
|
484
|
+
// --- Misc required hooks (no-ops for our renderer) ---
|
|
485
|
+
detachDeletedInstance(instance) {
|
|
486
|
+
// Called for every host instance in a deleted subtree, including failed creates below its top.
|
|
487
|
+
if (instance < 0)
|
|
488
|
+
freeOrphans(instance);
|
|
489
|
+
else
|
|
490
|
+
forgetBindings(instance);
|
|
491
|
+
},
|
|
492
|
+
getCurrentEventPriority() {
|
|
493
|
+
return DefaultEventPriority;
|
|
494
|
+
},
|
|
495
|
+
getInstanceFromNode() {
|
|
496
|
+
return null;
|
|
497
|
+
},
|
|
498
|
+
beforeActiveInstanceBlur() { },
|
|
499
|
+
afterActiveInstanceBlur() { },
|
|
500
|
+
prepareScopeUpdate() { },
|
|
501
|
+
getInstanceFromScope() {
|
|
502
|
+
return null;
|
|
503
|
+
},
|
|
504
|
+
// --- Scheduling ---
|
|
505
|
+
scheduleTimeout: (callback, delay) => setTimeout(callback, delay),
|
|
506
|
+
cancelTimeout: timeoutId => clearTimeout(timeoutId),
|
|
507
|
+
supportsMicrotasks: true,
|
|
508
|
+
scheduleMicrotask: typeof queueMicrotask === 'function'
|
|
509
|
+
? queueMicrotask
|
|
510
|
+
: callback => Promise.resolve().then(callback),
|
|
511
|
+
now: () => NativeUI.now(),
|
|
512
|
+
};
|
|
@@ -13,14 +13,7 @@
|
|
|
13
13
|
* See the License for the specific language governing permissions and
|
|
14
14
|
* limitations under the License.
|
|
15
15
|
*/
|
|
16
|
-
|
|
17
|
-
// The QuickJS C bridge (native_ui_bridge.c) installs a global `NativeUI` object before the
|
|
18
|
-
// bundle runs. Re-export it so the rest of the JS imports it cleanly instead of touching the
|
|
19
|
-
// global directly.
|
|
20
16
|
export const NativeUI = globalThis.NativeUI;
|
|
21
|
-
|
|
22
17
|
if (!NativeUI) {
|
|
23
|
-
|
|
24
|
-
'NativeUI global is missing — the QuickJS bridge must be installed before the bundle runs.',
|
|
25
|
-
);
|
|
18
|
+
throw new Error('NativeUI global is missing — the QuickJS bridge must be installed before the bundle runs.');
|
|
26
19
|
}
|