Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
57 changes: 56 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,57 @@ 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,
}),
],
[
'trunc',
addFunction(DEFAULT_SHAPES, [], {
positionalParams: [],
restParam: Effect.Read,
returnType: {kind: 'Primitive'},
calleeEffect: Effect.Read,
returnValueKind: ValueKind.Primitive,
}),
],
[
'ceil',
addFunction(DEFAULT_SHAPES, [], {
positionalParams: [],
restParam: Effect.Read,
returnType: {kind: 'Primitive'},
calleeEffect: Effect.Read,
returnValueKind: ValueKind.Primitive,
}),
],
[
'floor',
addFunction(DEFAULT_SHAPES, [], {
positionalParams: [],
restParam: Effect.Read,
returnType: {kind: 'Primitive'},
calleeEffect: Effect.Read,
returnValueKind: ValueKind.Primitive,
}),
],
[
'pow',
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 +506,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