react-compiler-runtime 0.0.0 → 1.0.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 ADDED
@@ -0,0 +1,5 @@
1
+ # react-compiler-runtime
2
+
3
+ Backwards compatible shim for runtime APIs used by React Compiler. Primarily meant for React versions prior to 19, but it will also work on > 19.
4
+
5
+ See also https://github.com/reactwg/react-compiler/discussions/6.
@@ -0,0 +1,20 @@
1
+ type MemoCache = Array<number | typeof $empty>;
2
+ declare const $empty: unique symbol;
3
+ declare const c: any;
4
+ declare enum GuardKind {
5
+ PushGuardContext = 0,
6
+ PopGuardContext = 1,
7
+ PushExpectHook = 2,
8
+ PopExpectHook = 3
9
+ }
10
+ declare function $dispatcherGuard(kind: GuardKind): void;
11
+ declare function $reset($: MemoCache): void;
12
+ declare function $makeReadOnly(): void;
13
+ declare const renderCounterRegistry: Map<string, Set<{
14
+ count: number;
15
+ }>>;
16
+ declare function clearRenderCounterRegistry(): void;
17
+ declare function useRenderCounter(name: string): void;
18
+ declare function $structuralCheck(oldValue: any, newValue: any, variableName: string, fnName: string, kind: string, loc: string): void;
19
+
20
+ export { $dispatcherGuard, $makeReadOnly, $reset, $structuralCheck, c, clearRenderCounterRegistry, renderCounterRegistry, useRenderCounter };
package/dist/index.js ADDED
@@ -0,0 +1,385 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ * @lightSyntaxTransform
8
+ * @noflow
9
+ * @nolint
10
+ * @preventMunge
11
+ * @preserve-invariant-messages
12
+ */
13
+
14
+ "use no memo";
15
+ "use strict";
16
+ var __create = Object.create;
17
+ var __defProp = Object.defineProperty;
18
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
19
+ var __getOwnPropNames = Object.getOwnPropertyNames;
20
+ var __getProtoOf = Object.getPrototypeOf;
21
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
22
+ var __export = (target, all) => {
23
+ for (var name in all)
24
+ __defProp(target, name, { get: all[name], enumerable: true });
25
+ };
26
+ var __copyProps = (to, from, except, desc) => {
27
+ if (from && typeof from === "object" || typeof from === "function") {
28
+ for (let key of __getOwnPropNames(from))
29
+ if (!__hasOwnProp.call(to, key) && key !== except)
30
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
31
+ }
32
+ return to;
33
+ };
34
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
35
+ // If the importer is in node compatibility mode or this is not an ESM
36
+ // file that has been converted to a CommonJS file using a Babel-
37
+ // compatible transform (i.e. "__esModule" has not been set), then set
38
+ // "default" to the CommonJS "module.exports" for node compatibility.
39
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
40
+ mod
41
+ ));
42
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
43
+
44
+ // src/index.ts
45
+ var index_exports = {};
46
+ __export(index_exports, {
47
+ $dispatcherGuard: () => $dispatcherGuard,
48
+ $makeReadOnly: () => $makeReadOnly,
49
+ $reset: () => $reset,
50
+ $structuralCheck: () => $structuralCheck,
51
+ c: () => c,
52
+ clearRenderCounterRegistry: () => clearRenderCounterRegistry,
53
+ renderCounterRegistry: () => renderCounterRegistry,
54
+ useRenderCounter: () => useRenderCounter
55
+ });
56
+ module.exports = __toCommonJS(index_exports);
57
+ var React = __toESM(require("react"));
58
+ var { useRef, useEffect, isValidElement } = React;
59
+ var _a;
60
+ var ReactSecretInternals = (
61
+ //@ts-ignore
62
+ (_a = React.__CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE) != null ? _a : React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED
63
+ );
64
+ var $empty = Symbol.for("react.memo_cache_sentinel");
65
+ var _a2;
66
+ var c = (
67
+ // @ts-expect-error
68
+ typeof ((_a2 = React.__COMPILER_RUNTIME) == null ? void 0 : _a2.c) === "function" ? (
69
+ // @ts-expect-error
70
+ React.__COMPILER_RUNTIME.c
71
+ ) : function c2(size) {
72
+ return React.useMemo(() => {
73
+ const $ = new Array(size);
74
+ for (let ii = 0; ii < size; ii++) {
75
+ $[ii] = $empty;
76
+ }
77
+ $[$empty] = true;
78
+ return $;
79
+ }, []);
80
+ }
81
+ );
82
+ var LazyGuardDispatcher = {};
83
+ [
84
+ "readContext",
85
+ "useCallback",
86
+ "useContext",
87
+ "useEffect",
88
+ "useImperativeHandle",
89
+ "useInsertionEffect",
90
+ "useLayoutEffect",
91
+ "useMemo",
92
+ "useReducer",
93
+ "useRef",
94
+ "useState",
95
+ "useDebugValue",
96
+ "useDeferredValue",
97
+ "useTransition",
98
+ "useMutableSource",
99
+ "useSyncExternalStore",
100
+ "useId",
101
+ "unstable_isNewReconciler",
102
+ "getCacheSignal",
103
+ "getCacheForType",
104
+ "useCacheRefresh"
105
+ ].forEach((name) => {
106
+ LazyGuardDispatcher[name] = () => {
107
+ throw new Error(
108
+ `[React] Unexpected React hook call (${name}) from a React compiled function. Check that all hooks are called directly and named according to convention ('use[A-Z]') `
109
+ );
110
+ };
111
+ });
112
+ var originalDispatcher = null;
113
+ LazyGuardDispatcher["useMemoCache"] = (count) => {
114
+ if (originalDispatcher == null) {
115
+ throw new Error(
116
+ "React Compiler internal invariant violation: unexpected null dispatcher"
117
+ );
118
+ } else {
119
+ return originalDispatcher.useMemoCache(count);
120
+ }
121
+ };
122
+ function setCurrent(newDispatcher) {
123
+ ReactSecretInternals.ReactCurrentDispatcher.current = newDispatcher;
124
+ return ReactSecretInternals.ReactCurrentDispatcher.current;
125
+ }
126
+ var guardFrames = [];
127
+ function $dispatcherGuard(kind) {
128
+ const curr = ReactSecretInternals.ReactCurrentDispatcher.current;
129
+ if (kind === 0 /* PushGuardContext */) {
130
+ guardFrames.push(curr);
131
+ if (guardFrames.length === 1) {
132
+ originalDispatcher = curr;
133
+ }
134
+ if (curr === LazyGuardDispatcher) {
135
+ throw new Error(
136
+ `[React] Unexpected call to custom hook or component from a React compiled function. Check that (1) all hooks are called directly and named according to convention ('use[A-Z]') and (2) components are returned as JSX instead of being directly invoked.`
137
+ );
138
+ }
139
+ setCurrent(LazyGuardDispatcher);
140
+ } else if (kind === 1 /* PopGuardContext */) {
141
+ const lastFrame = guardFrames.pop();
142
+ if (lastFrame == null) {
143
+ throw new Error(
144
+ "React Compiler internal error: unexpected null in guard stack"
145
+ );
146
+ }
147
+ if (guardFrames.length === 0) {
148
+ originalDispatcher = null;
149
+ }
150
+ setCurrent(lastFrame);
151
+ } else if (kind === 2 /* PushExpectHook */) {
152
+ guardFrames.push(curr);
153
+ setCurrent(originalDispatcher);
154
+ } else if (kind === 3 /* PopExpectHook */) {
155
+ const lastFrame = guardFrames.pop();
156
+ if (lastFrame == null) {
157
+ throw new Error(
158
+ "React Compiler internal error: unexpected null in guard stack"
159
+ );
160
+ }
161
+ setCurrent(lastFrame);
162
+ } else {
163
+ throw new Error("React Compiler internal error: unreachable block" + kind);
164
+ }
165
+ }
166
+ function $reset($) {
167
+ for (let ii = 0; ii < $.length; ii++) {
168
+ $[ii] = $empty;
169
+ }
170
+ }
171
+ function $makeReadOnly() {
172
+ throw new Error("TODO: implement $makeReadOnly in react-compiler-runtime");
173
+ }
174
+ var renderCounterRegistry = /* @__PURE__ */ new Map();
175
+ function clearRenderCounterRegistry() {
176
+ for (const counters of renderCounterRegistry.values()) {
177
+ counters.forEach((counter) => {
178
+ counter.count = 0;
179
+ });
180
+ }
181
+ }
182
+ function registerRenderCounter(name, val) {
183
+ let counters = renderCounterRegistry.get(name);
184
+ if (counters == null) {
185
+ counters = /* @__PURE__ */ new Set();
186
+ renderCounterRegistry.set(name, counters);
187
+ }
188
+ counters.add(val);
189
+ }
190
+ function removeRenderCounter(name, val) {
191
+ const counters = renderCounterRegistry.get(name);
192
+ if (counters == null) {
193
+ return;
194
+ }
195
+ counters.delete(val);
196
+ }
197
+ function useRenderCounter(name) {
198
+ const val = useRef(null);
199
+ if (val.current != null) {
200
+ val.current.count += 1;
201
+ }
202
+ useEffect(() => {
203
+ if (val.current == null) {
204
+ const counter = { count: 0 };
205
+ registerRenderCounter(name, counter);
206
+ val.current = counter;
207
+ }
208
+ return () => {
209
+ if (val.current !== null) {
210
+ removeRenderCounter(name, val.current);
211
+ }
212
+ };
213
+ });
214
+ }
215
+ var seenErrors = /* @__PURE__ */ new Set();
216
+ function $structuralCheck(oldValue, newValue, variableName, fnName, kind, loc) {
217
+ function error(l, r, path, depth) {
218
+ const str = `${fnName}:${loc} [${kind}] ${variableName}${path} changed from ${l} to ${r} at depth ${depth}`;
219
+ if (seenErrors.has(str)) {
220
+ return;
221
+ }
222
+ seenErrors.add(str);
223
+ console.error(str);
224
+ }
225
+ const depthLimit = 2;
226
+ function recur(oldValue2, newValue2, path, depth) {
227
+ if (depth > depthLimit) {
228
+ return;
229
+ } else if (oldValue2 === newValue2) {
230
+ return;
231
+ } else if (typeof oldValue2 !== typeof newValue2) {
232
+ error(`type ${typeof oldValue2}`, `type ${typeof newValue2}`, path, depth);
233
+ } else if (typeof oldValue2 === "object") {
234
+ const oldArray = Array.isArray(oldValue2);
235
+ const newArray = Array.isArray(newValue2);
236
+ if (oldValue2 === null && newValue2 !== null) {
237
+ error("null", `type ${typeof newValue2}`, path, depth);
238
+ } else if (newValue2 === null) {
239
+ error(`type ${typeof oldValue2}`, "null", path, depth);
240
+ } else if (oldValue2 instanceof Map) {
241
+ if (!(newValue2 instanceof Map)) {
242
+ error(`Map instance`, `other value`, path, depth);
243
+ } else if (oldValue2.size !== newValue2.size) {
244
+ error(
245
+ `Map instance with size ${oldValue2.size}`,
246
+ `Map instance with size ${newValue2.size}`,
247
+ path,
248
+ depth
249
+ );
250
+ } else {
251
+ for (const [k, v] of oldValue2) {
252
+ if (!newValue2.has(k)) {
253
+ error(
254
+ `Map instance with key ${k}`,
255
+ `Map instance without key ${k}`,
256
+ path,
257
+ depth
258
+ );
259
+ } else {
260
+ recur(v, newValue2.get(k), `${path}.get(${k})`, depth + 1);
261
+ }
262
+ }
263
+ }
264
+ } else if (newValue2 instanceof Map) {
265
+ error("other value", `Map instance`, path, depth);
266
+ } else if (oldValue2 instanceof Set) {
267
+ if (!(newValue2 instanceof Set)) {
268
+ error(`Set instance`, `other value`, path, depth);
269
+ } else if (oldValue2.size !== newValue2.size) {
270
+ error(
271
+ `Set instance with size ${oldValue2.size}`,
272
+ `Set instance with size ${newValue2.size}`,
273
+ path,
274
+ depth
275
+ );
276
+ } else {
277
+ for (const v of newValue2) {
278
+ if (!oldValue2.has(v)) {
279
+ error(
280
+ `Set instance without element ${v}`,
281
+ `Set instance with element ${v}`,
282
+ path,
283
+ depth
284
+ );
285
+ }
286
+ }
287
+ }
288
+ } else if (newValue2 instanceof Set) {
289
+ error("other value", `Set instance`, path, depth);
290
+ } else if (oldArray || newArray) {
291
+ if (oldArray !== newArray) {
292
+ error(
293
+ `type ${oldArray ? "array" : "object"}`,
294
+ `type ${newArray ? "array" : "object"}`,
295
+ path,
296
+ depth
297
+ );
298
+ } else if (oldValue2.length !== newValue2.length) {
299
+ error(
300
+ `array with length ${oldValue2.length}`,
301
+ `array with length ${newValue2.length}`,
302
+ path,
303
+ depth
304
+ );
305
+ } else {
306
+ for (let ii = 0; ii < oldValue2.length; ii++) {
307
+ recur(oldValue2[ii], newValue2[ii], `${path}[${ii}]`, depth + 1);
308
+ }
309
+ }
310
+ } else if (isValidElement(oldValue2) || isValidElement(newValue2)) {
311
+ if (isValidElement(oldValue2) !== isValidElement(newValue2)) {
312
+ error(
313
+ `type ${isValidElement(oldValue2) ? "React element" : "object"}`,
314
+ `type ${isValidElement(newValue2) ? "React element" : "object"}`,
315
+ path,
316
+ depth
317
+ );
318
+ } else if (oldValue2.type !== newValue2.type) {
319
+ error(
320
+ `React element of type ${oldValue2.type}`,
321
+ `React element of type ${newValue2.type}`,
322
+ path,
323
+ depth
324
+ );
325
+ } else {
326
+ recur(
327
+ oldValue2.props,
328
+ newValue2.props,
329
+ `[props of ${path}]`,
330
+ depth + 1
331
+ );
332
+ }
333
+ } else {
334
+ for (const key in newValue2) {
335
+ if (!(key in oldValue2)) {
336
+ error(
337
+ `object without key ${key}`,
338
+ `object with key ${key}`,
339
+ path,
340
+ depth
341
+ );
342
+ }
343
+ }
344
+ for (const key in oldValue2) {
345
+ if (!(key in newValue2)) {
346
+ error(
347
+ `object with key ${key}`,
348
+ `object without key ${key}`,
349
+ path,
350
+ depth
351
+ );
352
+ } else {
353
+ recur(oldValue2[key], newValue2[key], `${path}.${key}`, depth + 1);
354
+ }
355
+ }
356
+ }
357
+ } else if (typeof oldValue2 === "function") {
358
+ return;
359
+ } else if (isNaN(oldValue2) || isNaN(newValue2)) {
360
+ if (isNaN(oldValue2) !== isNaN(newValue2)) {
361
+ error(
362
+ `${isNaN(oldValue2) ? "NaN" : "non-NaN value"}`,
363
+ `${isNaN(newValue2) ? "NaN" : "non-NaN value"}`,
364
+ path,
365
+ depth
366
+ );
367
+ }
368
+ } else if (oldValue2 !== newValue2) {
369
+ error(oldValue2, newValue2, path, depth);
370
+ }
371
+ }
372
+ recur(oldValue, newValue, "", 0);
373
+ }
374
+ // Annotate the CommonJS export names for ESM import in node:
375
+ 0 && (module.exports = {
376
+ $dispatcherGuard,
377
+ $makeReadOnly,
378
+ $reset,
379
+ $structuralCheck,
380
+ c,
381
+ clearRenderCounterRegistry,
382
+ renderCounterRegistry,
383
+ useRenderCounter
384
+ });
385
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["/**\n * Copyright (c) Meta Platforms, Inc. and affiliates.\n *\n * This source code is licensed under the MIT license found in the\n * LICENSE file in the root directory of this source tree.\n */\n\nimport * as React from 'react';\n\nconst {useRef, useEffect, isValidElement} = React;\nconst ReactSecretInternals =\n //@ts-ignore\n React.__CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE ??\n //@ts-ignore\n React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED;\n\ntype MemoCache = Array<number | typeof $empty>;\n\nconst $empty = Symbol.for('react.memo_cache_sentinel');\n\n// Re-export React.c if present, otherwise fallback to the userspace polyfill for versions of React\n// < 19.\nexport const c =\n // @ts-expect-error\n typeof React.__COMPILER_RUNTIME?.c === 'function'\n ? // @ts-expect-error\n React.__COMPILER_RUNTIME.c\n : function c(size: number) {\n return React.useMemo<Array<unknown>>(() => {\n const $ = new Array(size);\n for (let ii = 0; ii < size; ii++) {\n $[ii] = $empty;\n }\n // This symbol is added to tell the react devtools that this array is from\n // useMemoCache.\n // @ts-ignore\n $[$empty] = true;\n return $;\n }, []);\n };\n\nconst LazyGuardDispatcher: {[key: string]: (...args: Array<any>) => any} = {};\n[\n 'readContext',\n 'useCallback',\n 'useContext',\n 'useEffect',\n 'useImperativeHandle',\n 'useInsertionEffect',\n 'useLayoutEffect',\n 'useMemo',\n 'useReducer',\n 'useRef',\n 'useState',\n 'useDebugValue',\n 'useDeferredValue',\n 'useTransition',\n 'useMutableSource',\n 'useSyncExternalStore',\n 'useId',\n 'unstable_isNewReconciler',\n 'getCacheSignal',\n 'getCacheForType',\n 'useCacheRefresh',\n].forEach(name => {\n LazyGuardDispatcher[name] = () => {\n throw new Error(\n `[React] Unexpected React hook call (${name}) from a React compiled function. ` +\n \"Check that all hooks are called directly and named according to convention ('use[A-Z]') \",\n );\n };\n});\n\nlet originalDispatcher: unknown = null;\n\n// Allow guards are not emitted for useMemoCache\nLazyGuardDispatcher['useMemoCache'] = (count: number) => {\n if (originalDispatcher == null) {\n throw new Error(\n 'React Compiler internal invariant violation: unexpected null dispatcher',\n );\n } else {\n return (originalDispatcher as any).useMemoCache(count);\n }\n};\n\nenum GuardKind {\n PushGuardContext = 0,\n PopGuardContext = 1,\n PushExpectHook = 2,\n PopExpectHook = 3,\n}\n\nfunction setCurrent(newDispatcher: any) {\n ReactSecretInternals.ReactCurrentDispatcher.current = newDispatcher;\n return ReactSecretInternals.ReactCurrentDispatcher.current;\n}\n\nconst guardFrames: Array<unknown> = [];\n\n/**\n * When `enableEmitHookGuards` is set, this does runtime validation\n * of the no-conditional-hook-calls rule.\n * As React Compiler needs to statically understand which calls to move out of\n * conditional branches (i.e. React Compiler cannot memoize the results of hook\n * calls), its understanding of \"the rules of React\" are more restrictive.\n * This validation throws on unsound inputs at runtime.\n *\n * Components should only be invoked through React as React Compiler could memoize\n * the call to AnotherComponent, introducing conditional hook calls in its\n * compiled output.\n * ```js\n * function Invalid(props) {\n * const myJsx = AnotherComponent(props);\n * return <div> { myJsx } </div>;\n * }\n *\n * Hooks must be named as hooks.\n * ```js\n * const renamedHook = useState;\n * function Invalid() {\n * const [state, setState] = renamedHook(0);\n * }\n * ```\n *\n * Hooks must be directly called.\n * ```\n * function call(fn) {\n * return fn();\n * }\n * function Invalid() {\n * const result = call(useMyHook);\n * }\n * ```\n */\nexport function $dispatcherGuard(kind: GuardKind) {\n const curr = ReactSecretInternals.ReactCurrentDispatcher.current;\n if (kind === GuardKind.PushGuardContext) {\n // Push before checking invariant or errors\n guardFrames.push(curr);\n\n if (guardFrames.length === 1) {\n // save if we're the first guard on the stack\n originalDispatcher = curr;\n }\n\n if (curr === LazyGuardDispatcher) {\n throw new Error(\n `[React] Unexpected call to custom hook or component from a React compiled function. ` +\n \"Check that (1) all hooks are called directly and named according to convention ('use[A-Z]') \" +\n 'and (2) components are returned as JSX instead of being directly invoked.',\n );\n }\n setCurrent(LazyGuardDispatcher);\n } else if (kind === GuardKind.PopGuardContext) {\n // Pop before checking invariant or errors\n const lastFrame = guardFrames.pop();\n\n if (lastFrame == null) {\n throw new Error(\n 'React Compiler internal error: unexpected null in guard stack',\n );\n }\n if (guardFrames.length === 0) {\n originalDispatcher = null;\n }\n setCurrent(lastFrame);\n } else if (kind === GuardKind.PushExpectHook) {\n // ExpectHooks could be nested, so we save the current dispatcher\n // for the matching PopExpectHook to restore.\n guardFrames.push(curr);\n setCurrent(originalDispatcher);\n } else if (kind === GuardKind.PopExpectHook) {\n const lastFrame = guardFrames.pop();\n if (lastFrame == null) {\n throw new Error(\n 'React Compiler internal error: unexpected null in guard stack',\n );\n }\n setCurrent(lastFrame);\n } else {\n throw new Error('React Compiler internal error: unreachable block' + kind);\n }\n}\n\nexport function $reset($: MemoCache) {\n for (let ii = 0; ii < $.length; ii++) {\n $[ii] = $empty;\n }\n}\n\nexport function $makeReadOnly() {\n throw new Error('TODO: implement $makeReadOnly in react-compiler-runtime');\n}\n\n/**\n * Instrumentation to count rerenders in React components\n */\nexport const renderCounterRegistry: Map<\n string,\n Set<{count: number}>\n> = new Map();\nexport function clearRenderCounterRegistry() {\n for (const counters of renderCounterRegistry.values()) {\n counters.forEach(counter => {\n counter.count = 0;\n });\n }\n}\n\nfunction registerRenderCounter(name: string, val: {count: number}) {\n let counters = renderCounterRegistry.get(name);\n if (counters == null) {\n counters = new Set();\n renderCounterRegistry.set(name, counters);\n }\n counters.add(val);\n}\n\nfunction removeRenderCounter(name: string, val: {count: number}): void {\n const counters = renderCounterRegistry.get(name);\n if (counters == null) {\n return;\n }\n counters.delete(val);\n}\n\nexport function useRenderCounter(name: string): void {\n const val = useRef<{count: number}>(null);\n\n if (val.current != null) {\n val.current.count += 1;\n }\n useEffect(() => {\n // Not counting initial render shouldn't be a problem\n if (val.current == null) {\n const counter = {count: 0};\n registerRenderCounter(name, counter);\n // @ts-ignore\n val.current = counter;\n }\n return () => {\n if (val.current !== null) {\n removeRenderCounter(name, val.current);\n }\n };\n });\n}\n\nconst seenErrors = new Set();\n\nexport function $structuralCheck(\n oldValue: any,\n newValue: any,\n variableName: string,\n fnName: string,\n kind: string,\n loc: string,\n): void {\n function error(l: string, r: string, path: string, depth: number) {\n const str = `${fnName}:${loc} [${kind}] ${variableName}${path} changed from ${l} to ${r} at depth ${depth}`;\n if (seenErrors.has(str)) {\n return;\n }\n seenErrors.add(str);\n console.error(str);\n }\n const depthLimit = 2;\n function recur(oldValue: any, newValue: any, path: string, depth: number) {\n if (depth > depthLimit) {\n return;\n } else if (oldValue === newValue) {\n return;\n } else if (typeof oldValue !== typeof newValue) {\n error(`type ${typeof oldValue}`, `type ${typeof newValue}`, path, depth);\n } else if (typeof oldValue === 'object') {\n const oldArray = Array.isArray(oldValue);\n const newArray = Array.isArray(newValue);\n if (oldValue === null && newValue !== null) {\n error('null', `type ${typeof newValue}`, path, depth);\n } else if (newValue === null) {\n error(`type ${typeof oldValue}`, 'null', path, depth);\n } else if (oldValue instanceof Map) {\n if (!(newValue instanceof Map)) {\n error(`Map instance`, `other value`, path, depth);\n } else if (oldValue.size !== newValue.size) {\n error(\n `Map instance with size ${oldValue.size}`,\n `Map instance with size ${newValue.size}`,\n path,\n depth,\n );\n } else {\n for (const [k, v] of oldValue) {\n if (!newValue.has(k)) {\n error(\n `Map instance with key ${k}`,\n `Map instance without key ${k}`,\n path,\n depth,\n );\n } else {\n recur(v, newValue.get(k), `${path}.get(${k})`, depth + 1);\n }\n }\n }\n } else if (newValue instanceof Map) {\n error('other value', `Map instance`, path, depth);\n } else if (oldValue instanceof Set) {\n if (!(newValue instanceof Set)) {\n error(`Set instance`, `other value`, path, depth);\n } else if (oldValue.size !== newValue.size) {\n error(\n `Set instance with size ${oldValue.size}`,\n `Set instance with size ${newValue.size}`,\n path,\n depth,\n );\n } else {\n for (const v of newValue) {\n if (!oldValue.has(v)) {\n error(\n `Set instance without element ${v}`,\n `Set instance with element ${v}`,\n path,\n depth,\n );\n }\n }\n }\n } else if (newValue instanceof Set) {\n error('other value', `Set instance`, path, depth);\n } else if (oldArray || newArray) {\n if (oldArray !== newArray) {\n error(\n `type ${oldArray ? 'array' : 'object'}`,\n `type ${newArray ? 'array' : 'object'}`,\n path,\n depth,\n );\n } else if (oldValue.length !== newValue.length) {\n error(\n `array with length ${oldValue.length}`,\n `array with length ${newValue.length}`,\n path,\n depth,\n );\n } else {\n for (let ii = 0; ii < oldValue.length; ii++) {\n recur(oldValue[ii], newValue[ii], `${path}[${ii}]`, depth + 1);\n }\n }\n } else if (isValidElement(oldValue) || isValidElement(newValue)) {\n if (isValidElement(oldValue) !== isValidElement(newValue)) {\n error(\n `type ${isValidElement(oldValue) ? 'React element' : 'object'}`,\n `type ${isValidElement(newValue) ? 'React element' : 'object'}`,\n path,\n depth,\n );\n } else if (oldValue.type !== newValue.type) {\n error(\n `React element of type ${oldValue.type}`,\n `React element of type ${newValue.type}`,\n path,\n depth,\n );\n } else {\n recur(\n oldValue.props,\n newValue.props,\n `[props of ${path}]`,\n depth + 1,\n );\n }\n } else {\n for (const key in newValue) {\n if (!(key in oldValue)) {\n error(\n `object without key ${key}`,\n `object with key ${key}`,\n path,\n depth,\n );\n }\n }\n for (const key in oldValue) {\n if (!(key in newValue)) {\n error(\n `object with key ${key}`,\n `object without key ${key}`,\n path,\n depth,\n );\n } else {\n recur(oldValue[key], newValue[key], `${path}.${key}`, depth + 1);\n }\n }\n }\n } else if (typeof oldValue === 'function') {\n // Bail on functions for now\n return;\n } else if (isNaN(oldValue) || isNaN(newValue)) {\n if (isNaN(oldValue) !== isNaN(newValue)) {\n error(\n `${isNaN(oldValue) ? 'NaN' : 'non-NaN value'}`,\n `${isNaN(newValue) ? 'NaN' : 'non-NaN value'}`,\n path,\n depth,\n );\n }\n } else if (oldValue !== newValue) {\n error(oldValue, newValue, path, depth);\n }\n }\n recur(oldValue, newValue, '', 0);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAOA,YAAuB;AAEvB,IAAM,EAAC,QAAQ,WAAW,eAAc,IAAI;AAT5C;AAUA,IAAM;AAAA;AAAA,GAEJ,KAAM,0EAAN,YAEM;AAAA;AAIR,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AACL,mBAAW,OAAOA,WAAU;AAC1B,cAAI,EAAE,OAAOD,YAAW;AACtB;AAAA,cACE,sBAAsB,GAAG;AAAA,cACzB,mBAAmB,GAAG;AAAA,cACtB;AAAA,cACA;AAAA,YACF;AAAA,UACF;AAAA,QACF;AACA,mBAAW,OAAOA,WAAU;AAC1B,cAAI,EAAE,OAAOC,YAAW;AACtB;AAAA,cACE,mBAAmB,GAAG;AAAA,cACtB,sBAAsB,GAAG;AAAA,cACzB;AAAA,cACA;AAAA,YACF;AAAA,UACF,OAAO;AACL,kBAAMD,UAAS,GAAG,GAAGC,UAAS,GAAG,GAAG,GAAG,IAAI,IAAI,GAAG,IAAI,QAAQ,CAAC;AAAA,UACjE;AAAA,QACF;AAAA,MACF;AAAA,IACF,WAAW,OAAOD,cAAa,YAAY;AAEzC;AAAA,IACF,WAAW,MAAMA,SAAQ,KAAK,MAAMC,SAAQ,GAAG;AAC7C,UAAI,MAAMD,SAAQ,MAAM,MAAMC,SAAQ,GAAG;AACvC;AAAA,UACE,GAAG,MAAMD,SAAQ,IAAI,QAAQ,eAAe;AAAA,UAC5C,GAAG,MAAMC,SAAQ,IAAI,QAAQ,eAAe;AAAA,UAC5C;AAAA,UACA;AAAA,QACF;AAAA,MACF;AAAA,IACF,WAAWD,cAAaC,WAAU;AAChC,YAAMD,WAAUC,WAAU,MAAM,KAAK;AAAA,IACvC;AAAA,EACF;AACA,QAAM,UAAU,UAAU,IAAI,CAAC;AACjC;","names":["_a","c","oldValue","newValue"]}
package/package.json CHANGED
@@ -1,11 +1,25 @@
1
1
  {
2
2
  "name": "react-compiler-runtime",
3
- "version": "0.0.0",
4
- "description": "",
5
- "main": "index.js",
3
+ "version": "1.0.0",
4
+ "description": "Runtime for React Compiler",
5
+ "license": "MIT",
6
+ "main": "dist/index.js",
7
+ "typings": "dist/index.d.ts",
8
+ "files": [
9
+ "dist",
10
+ "src"
11
+ ],
12
+ "peerDependencies": {
13
+ "react": "^17.0.0 || ^18.0.0 || ^19.0.0 || ^0.0.0-experimental"
14
+ },
6
15
  "scripts": {
7
- "test": "echo \"Error: no test specified\" && exit 1"
16
+ "build": "rimraf dist && tsup",
17
+ "test": "echo 'no tests'",
18
+ "watch": "yarn build --watch"
8
19
  },
9
- "author": "",
10
- "license": "MIT"
20
+ "repository": {
21
+ "type": "git",
22
+ "url": "git+https://github.com/facebook/react.git",
23
+ "directory": "compiler/packages/react-compiler-runtime"
24
+ }
11
25
  }
package/src/index.ts ADDED
@@ -0,0 +1,417 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+
8
+ import * as React from 'react';
9
+
10
+ const {useRef, useEffect, isValidElement} = React;
11
+ const ReactSecretInternals =
12
+ //@ts-ignore
13
+ React.__CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE ??
14
+ //@ts-ignore
15
+ React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED;
16
+
17
+ type MemoCache = Array<number | typeof $empty>;
18
+
19
+ const $empty = Symbol.for('react.memo_cache_sentinel');
20
+
21
+ // Re-export React.c if present, otherwise fallback to the userspace polyfill for versions of React
22
+ // < 19.
23
+ export const c =
24
+ // @ts-expect-error
25
+ typeof React.__COMPILER_RUNTIME?.c === 'function'
26
+ ? // @ts-expect-error
27
+ React.__COMPILER_RUNTIME.c
28
+ : function c(size: number) {
29
+ return React.useMemo<Array<unknown>>(() => {
30
+ const $ = new Array(size);
31
+ for (let ii = 0; ii < size; ii++) {
32
+ $[ii] = $empty;
33
+ }
34
+ // This symbol is added to tell the react devtools that this array is from
35
+ // useMemoCache.
36
+ // @ts-ignore
37
+ $[$empty] = true;
38
+ return $;
39
+ }, []);
40
+ };
41
+
42
+ const LazyGuardDispatcher: {[key: string]: (...args: Array<any>) => any} = {};
43
+ [
44
+ 'readContext',
45
+ 'useCallback',
46
+ 'useContext',
47
+ 'useEffect',
48
+ 'useImperativeHandle',
49
+ 'useInsertionEffect',
50
+ 'useLayoutEffect',
51
+ 'useMemo',
52
+ 'useReducer',
53
+ 'useRef',
54
+ 'useState',
55
+ 'useDebugValue',
56
+ 'useDeferredValue',
57
+ 'useTransition',
58
+ 'useMutableSource',
59
+ 'useSyncExternalStore',
60
+ 'useId',
61
+ 'unstable_isNewReconciler',
62
+ 'getCacheSignal',
63
+ 'getCacheForType',
64
+ 'useCacheRefresh',
65
+ ].forEach(name => {
66
+ LazyGuardDispatcher[name] = () => {
67
+ throw new Error(
68
+ `[React] Unexpected React hook call (${name}) from a React compiled function. ` +
69
+ "Check that all hooks are called directly and named according to convention ('use[A-Z]') ",
70
+ );
71
+ };
72
+ });
73
+
74
+ let originalDispatcher: unknown = null;
75
+
76
+ // Allow guards are not emitted for useMemoCache
77
+ LazyGuardDispatcher['useMemoCache'] = (count: number) => {
78
+ if (originalDispatcher == null) {
79
+ throw new Error(
80
+ 'React Compiler internal invariant violation: unexpected null dispatcher',
81
+ );
82
+ } else {
83
+ return (originalDispatcher as any).useMemoCache(count);
84
+ }
85
+ };
86
+
87
+ enum GuardKind {
88
+ PushGuardContext = 0,
89
+ PopGuardContext = 1,
90
+ PushExpectHook = 2,
91
+ PopExpectHook = 3,
92
+ }
93
+
94
+ function setCurrent(newDispatcher: any) {
95
+ ReactSecretInternals.ReactCurrentDispatcher.current = newDispatcher;
96
+ return ReactSecretInternals.ReactCurrentDispatcher.current;
97
+ }
98
+
99
+ const guardFrames: Array<unknown> = [];
100
+
101
+ /**
102
+ * When `enableEmitHookGuards` is set, this does runtime validation
103
+ * of the no-conditional-hook-calls rule.
104
+ * As React Compiler needs to statically understand which calls to move out of
105
+ * conditional branches (i.e. React Compiler cannot memoize the results of hook
106
+ * calls), its understanding of "the rules of React" are more restrictive.
107
+ * This validation throws on unsound inputs at runtime.
108
+ *
109
+ * Components should only be invoked through React as React Compiler could memoize
110
+ * the call to AnotherComponent, introducing conditional hook calls in its
111
+ * compiled output.
112
+ * ```js
113
+ * function Invalid(props) {
114
+ * const myJsx = AnotherComponent(props);
115
+ * return <div> { myJsx } </div>;
116
+ * }
117
+ *
118
+ * Hooks must be named as hooks.
119
+ * ```js
120
+ * const renamedHook = useState;
121
+ * function Invalid() {
122
+ * const [state, setState] = renamedHook(0);
123
+ * }
124
+ * ```
125
+ *
126
+ * Hooks must be directly called.
127
+ * ```
128
+ * function call(fn) {
129
+ * return fn();
130
+ * }
131
+ * function Invalid() {
132
+ * const result = call(useMyHook);
133
+ * }
134
+ * ```
135
+ */
136
+ export function $dispatcherGuard(kind: GuardKind) {
137
+ const curr = ReactSecretInternals.ReactCurrentDispatcher.current;
138
+ if (kind === GuardKind.PushGuardContext) {
139
+ // Push before checking invariant or errors
140
+ guardFrames.push(curr);
141
+
142
+ if (guardFrames.length === 1) {
143
+ // save if we're the first guard on the stack
144
+ originalDispatcher = curr;
145
+ }
146
+
147
+ if (curr === LazyGuardDispatcher) {
148
+ throw new Error(
149
+ `[React] Unexpected call to custom hook or component from a React compiled function. ` +
150
+ "Check that (1) all hooks are called directly and named according to convention ('use[A-Z]') " +
151
+ 'and (2) components are returned as JSX instead of being directly invoked.',
152
+ );
153
+ }
154
+ setCurrent(LazyGuardDispatcher);
155
+ } else if (kind === GuardKind.PopGuardContext) {
156
+ // Pop before checking invariant or errors
157
+ const lastFrame = guardFrames.pop();
158
+
159
+ if (lastFrame == null) {
160
+ throw new Error(
161
+ 'React Compiler internal error: unexpected null in guard stack',
162
+ );
163
+ }
164
+ if (guardFrames.length === 0) {
165
+ originalDispatcher = null;
166
+ }
167
+ setCurrent(lastFrame);
168
+ } else if (kind === GuardKind.PushExpectHook) {
169
+ // ExpectHooks could be nested, so we save the current dispatcher
170
+ // for the matching PopExpectHook to restore.
171
+ guardFrames.push(curr);
172
+ setCurrent(originalDispatcher);
173
+ } else if (kind === GuardKind.PopExpectHook) {
174
+ const lastFrame = guardFrames.pop();
175
+ if (lastFrame == null) {
176
+ throw new Error(
177
+ 'React Compiler internal error: unexpected null in guard stack',
178
+ );
179
+ }
180
+ setCurrent(lastFrame);
181
+ } else {
182
+ throw new Error('React Compiler internal error: unreachable block' + kind);
183
+ }
184
+ }
185
+
186
+ export function $reset($: MemoCache) {
187
+ for (let ii = 0; ii < $.length; ii++) {
188
+ $[ii] = $empty;
189
+ }
190
+ }
191
+
192
+ export function $makeReadOnly() {
193
+ throw new Error('TODO: implement $makeReadOnly in react-compiler-runtime');
194
+ }
195
+
196
+ /**
197
+ * Instrumentation to count rerenders in React components
198
+ */
199
+ export const renderCounterRegistry: Map<
200
+ string,
201
+ Set<{count: number}>
202
+ > = new Map();
203
+ export function clearRenderCounterRegistry() {
204
+ for (const counters of renderCounterRegistry.values()) {
205
+ counters.forEach(counter => {
206
+ counter.count = 0;
207
+ });
208
+ }
209
+ }
210
+
211
+ function registerRenderCounter(name: string, val: {count: number}) {
212
+ let counters = renderCounterRegistry.get(name);
213
+ if (counters == null) {
214
+ counters = new Set();
215
+ renderCounterRegistry.set(name, counters);
216
+ }
217
+ counters.add(val);
218
+ }
219
+
220
+ function removeRenderCounter(name: string, val: {count: number}): void {
221
+ const counters = renderCounterRegistry.get(name);
222
+ if (counters == null) {
223
+ return;
224
+ }
225
+ counters.delete(val);
226
+ }
227
+
228
+ export function useRenderCounter(name: string): void {
229
+ const val = useRef<{count: number}>(null);
230
+
231
+ if (val.current != null) {
232
+ val.current.count += 1;
233
+ }
234
+ useEffect(() => {
235
+ // Not counting initial render shouldn't be a problem
236
+ if (val.current == null) {
237
+ const counter = {count: 0};
238
+ registerRenderCounter(name, counter);
239
+ // @ts-ignore
240
+ val.current = counter;
241
+ }
242
+ return () => {
243
+ if (val.current !== null) {
244
+ removeRenderCounter(name, val.current);
245
+ }
246
+ };
247
+ });
248
+ }
249
+
250
+ const seenErrors = new Set();
251
+
252
+ export function $structuralCheck(
253
+ oldValue: any,
254
+ newValue: any,
255
+ variableName: string,
256
+ fnName: string,
257
+ kind: string,
258
+ loc: string,
259
+ ): void {
260
+ function error(l: string, r: string, path: string, depth: number) {
261
+ const str = `${fnName}:${loc} [${kind}] ${variableName}${path} changed from ${l} to ${r} at depth ${depth}`;
262
+ if (seenErrors.has(str)) {
263
+ return;
264
+ }
265
+ seenErrors.add(str);
266
+ console.error(str);
267
+ }
268
+ const depthLimit = 2;
269
+ function recur(oldValue: any, newValue: any, path: string, depth: number) {
270
+ if (depth > depthLimit) {
271
+ return;
272
+ } else if (oldValue === newValue) {
273
+ return;
274
+ } else if (typeof oldValue !== typeof newValue) {
275
+ error(`type ${typeof oldValue}`, `type ${typeof newValue}`, path, depth);
276
+ } else if (typeof oldValue === 'object') {
277
+ const oldArray = Array.isArray(oldValue);
278
+ const newArray = Array.isArray(newValue);
279
+ if (oldValue === null && newValue !== null) {
280
+ error('null', `type ${typeof newValue}`, path, depth);
281
+ } else if (newValue === null) {
282
+ error(`type ${typeof oldValue}`, 'null', path, depth);
283
+ } else if (oldValue instanceof Map) {
284
+ if (!(newValue instanceof Map)) {
285
+ error(`Map instance`, `other value`, path, depth);
286
+ } else if (oldValue.size !== newValue.size) {
287
+ error(
288
+ `Map instance with size ${oldValue.size}`,
289
+ `Map instance with size ${newValue.size}`,
290
+ path,
291
+ depth,
292
+ );
293
+ } else {
294
+ for (const [k, v] of oldValue) {
295
+ if (!newValue.has(k)) {
296
+ error(
297
+ `Map instance with key ${k}`,
298
+ `Map instance without key ${k}`,
299
+ path,
300
+ depth,
301
+ );
302
+ } else {
303
+ recur(v, newValue.get(k), `${path}.get(${k})`, depth + 1);
304
+ }
305
+ }
306
+ }
307
+ } else if (newValue instanceof Map) {
308
+ error('other value', `Map instance`, path, depth);
309
+ } else if (oldValue instanceof Set) {
310
+ if (!(newValue instanceof Set)) {
311
+ error(`Set instance`, `other value`, path, depth);
312
+ } else if (oldValue.size !== newValue.size) {
313
+ error(
314
+ `Set instance with size ${oldValue.size}`,
315
+ `Set instance with size ${newValue.size}`,
316
+ path,
317
+ depth,
318
+ );
319
+ } else {
320
+ for (const v of newValue) {
321
+ if (!oldValue.has(v)) {
322
+ error(
323
+ `Set instance without element ${v}`,
324
+ `Set instance with element ${v}`,
325
+ path,
326
+ depth,
327
+ );
328
+ }
329
+ }
330
+ }
331
+ } else if (newValue instanceof Set) {
332
+ error('other value', `Set instance`, path, depth);
333
+ } else if (oldArray || newArray) {
334
+ if (oldArray !== newArray) {
335
+ error(
336
+ `type ${oldArray ? 'array' : 'object'}`,
337
+ `type ${newArray ? 'array' : 'object'}`,
338
+ path,
339
+ depth,
340
+ );
341
+ } else if (oldValue.length !== newValue.length) {
342
+ error(
343
+ `array with length ${oldValue.length}`,
344
+ `array with length ${newValue.length}`,
345
+ path,
346
+ depth,
347
+ );
348
+ } else {
349
+ for (let ii = 0; ii < oldValue.length; ii++) {
350
+ recur(oldValue[ii], newValue[ii], `${path}[${ii}]`, depth + 1);
351
+ }
352
+ }
353
+ } else if (isValidElement(oldValue) || isValidElement(newValue)) {
354
+ if (isValidElement(oldValue) !== isValidElement(newValue)) {
355
+ error(
356
+ `type ${isValidElement(oldValue) ? 'React element' : 'object'}`,
357
+ `type ${isValidElement(newValue) ? 'React element' : 'object'}`,
358
+ path,
359
+ depth,
360
+ );
361
+ } else if (oldValue.type !== newValue.type) {
362
+ error(
363
+ `React element of type ${oldValue.type}`,
364
+ `React element of type ${newValue.type}`,
365
+ path,
366
+ depth,
367
+ );
368
+ } else {
369
+ recur(
370
+ oldValue.props,
371
+ newValue.props,
372
+ `[props of ${path}]`,
373
+ depth + 1,
374
+ );
375
+ }
376
+ } else {
377
+ for (const key in newValue) {
378
+ if (!(key in oldValue)) {
379
+ error(
380
+ `object without key ${key}`,
381
+ `object with key ${key}`,
382
+ path,
383
+ depth,
384
+ );
385
+ }
386
+ }
387
+ for (const key in oldValue) {
388
+ if (!(key in newValue)) {
389
+ error(
390
+ `object with key ${key}`,
391
+ `object without key ${key}`,
392
+ path,
393
+ depth,
394
+ );
395
+ } else {
396
+ recur(oldValue[key], newValue[key], `${path}.${key}`, depth + 1);
397
+ }
398
+ }
399
+ }
400
+ } else if (typeof oldValue === 'function') {
401
+ // Bail on functions for now
402
+ return;
403
+ } else if (isNaN(oldValue) || isNaN(newValue)) {
404
+ if (isNaN(oldValue) !== isNaN(newValue)) {
405
+ error(
406
+ `${isNaN(oldValue) ? 'NaN' : 'non-NaN value'}`,
407
+ `${isNaN(newValue) ? 'NaN' : 'non-NaN value'}`,
408
+ path,
409
+ depth,
410
+ );
411
+ }
412
+ } else if (oldValue !== newValue) {
413
+ error(oldValue, newValue, path, depth);
414
+ }
415
+ }
416
+ recur(oldValue, newValue, '', 0);
417
+ }