Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[compiler][ez] Add types for Math.min, recursive global #30641

Merged
merged 2 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion compiler/packages/babel-plugin-react-compiler/src/HIR/Globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,17 @@ const TYPED_GLOBALS: Array<[string, BuiltInType]> = [
returnValueKind: ValueKind.Primitive,
}),
],
[
'min',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whole bunch more Math.* stuff missing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, added min as Math.min and Math.max differences surprised me a few times. Will go ahead and add a few others

// Math.min(value0, ..., valueN)
addFunction(DEFAULT_SHAPES, [], {
positionalParams: [],
restParam: Effect.Read,
returnType: {kind: 'Primitive'},
calleeEffect: Effect.Read,
returnValueKind: ValueKind.Primitive,
}),
],
]),
],
['Infinity', {kind: 'Primitive'}],
Expand Down Expand Up @@ -455,11 +466,15 @@ for (const [name, type_] of TYPED_GLOBALS) {
DEFAULT_GLOBALS.set(name, type_);
}

// Recursive global type
// Recursive global types
DEFAULT_GLOBALS.set(
'globalThis',
addObject(DEFAULT_SHAPES, 'globalThis', TYPED_GLOBALS),
);
DEFAULT_GLOBALS.set(
'global',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

window too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I debated on adding window but figured we should leave it out for now. I wonder if we want to handle window and document separately as people often use these for DOM-manipulation (global mutable state)

addObject(DEFAULT_SHAPES, 'global', TYPED_GLOBALS),
);

export function installReAnimatedTypes(
globals: GlobalRegistry,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ function Component(props) {
console.error(x);
console.trace(x);
console.table(x);
global.console.log(x);
return x;
}

Expand Down Expand Up @@ -48,6 +49,7 @@ function Component(props) {
console.error(x);
console.trace(x);
console.table(x);
global.console.log(x);
return x;
}

Expand All @@ -61,4 +63,4 @@ export const FIXTURE_ENTRYPOINT = {

### Eval output
(kind: ok) {"a":1,"b":2}
logs: [{ a: 1, b: 2 },{ a: 1, b: 2 },{ a: 1, b: 2 },{ a: 1, b: 2 },{ a: 1, b: 2 }]
logs: [{ a: 1, b: 2 },{ a: 1, b: 2 },{ a: 1, b: 2 },{ a: 1, b: 2 },{ a: 1, b: 2 },{ a: 1, b: 2 }]
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ function Component(props) {
console.error(x);
console.trace(x);
console.table(x);
global.console.log(x);
return x;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,15 @@ export const FIXTURE_ENTRYPOINT = {
## Code

```javascript
import { c as _c } from "react/compiler-runtime"; // @validatePreserveExistingMemoizationGuarantees
// @validatePreserveExistingMemoizationGuarantees

import { useMemo } from "react";

// It's correct to infer a useMemo value is non-allocating
// and not provide it with a reactive scope
function useFoo(num1, num2) {
const $ = _c(3);
let t0;
let t1;
if ($[0] !== num1 || $[1] !== num2) {
t1 = Math.min(num1, num2);
$[0] = num1;
$[1] = num2;
$[2] = t1;
} else {
t1 = $[2];
}
t0 = t1;
t0 = Math.min(num1, num2);
return t0;
}

Expand Down