diff --git a/compiler/forget/src/HIR/Globals.ts b/compiler/forget/src/HIR/Globals.ts index 16b39c4769..5a38510e7e 100644 --- a/compiler/forget/src/HIR/Globals.ts +++ b/compiler/forget/src/HIR/Globals.ts @@ -9,6 +9,8 @@ import { Effect, ValueKind } from "./HIR"; import { BUILTIN_SHAPES, BuiltInArrayId, + BuiltInUseRefId, + BuiltInUseStateId, ShapeRegistry, addFunction, addHook, @@ -248,7 +250,7 @@ const BUILTIN_HOOKS: Array<[string, FunctionType]> = [ addHook(DEFAULT_SHAPES, [], { positionalParams: [], restParam: Effect.Freeze, - returnType: { kind: "Poly" }, + returnType: { kind: "Object", shapeId: BuiltInUseStateId }, calleeEffect: Effect.Read, hookKind: "useState", returnValueKind: ValueKind.Frozen, @@ -259,7 +261,7 @@ const BUILTIN_HOOKS: Array<[string, FunctionType]> = [ addHook(DEFAULT_SHAPES, [], { positionalParams: [], restParam: Effect.Capture, - returnType: { kind: "Poly" }, + returnType: { kind: "Object", shapeId: BuiltInUseRefId }, calleeEffect: Effect.Read, hookKind: "useRef", returnValueKind: ValueKind.Mutable, diff --git a/compiler/forget/src/HIR/ObjectShape.ts b/compiler/forget/src/HIR/ObjectShape.ts index 0ba10a973a..a33e808c69 100644 --- a/compiler/forget/src/HIR/ObjectShape.ts +++ b/compiler/forget/src/HIR/ObjectShape.ts @@ -40,9 +40,10 @@ function createAnonId(): string { export function addFunction( registry: ShapeRegistry, properties: Iterable<[string, BuiltInType | PolyType]>, - fn: Omit + fn: Omit, + id: string | null = null ): FunctionType { - const shapeId = createAnonId(); + const shapeId = id ?? createAnonId(); addShape(registry, shapeId, properties, { ...fn, hookKind: null, @@ -157,6 +158,10 @@ export type ObjectShape = { export type ShapeRegistry = Map; export const BuiltInArrayId = "BuiltInArray"; export const BuiltInObjectId = "BuiltInObject"; +export const BuiltInUseStateId = "BuiltInUseState"; +export const BuiltInSetStateId = "BuiltInSetState"; +export const BuiltInUseRefId = "BuiltInUseRefId"; +export const BuiltInRefValueId = "BuiltInRefValue"; /** * ShapeRegistry with default definitions for built-ins. @@ -218,6 +223,31 @@ addObject(BUILTIN_SHAPES, BuiltInObjectId, [ // hasOwnProperty, isPrototypeOf, propertyIsEnumerable, toLocaleString, valueOf ]); +addObject(BUILTIN_SHAPES, BuiltInUseStateId, [ + ["0", { kind: "Poly" }], + [ + "1", + addFunction( + BUILTIN_SHAPES, + [], + { + positionalParams: [], + restParam: Effect.Freeze, + returnType: PRIMITIVE_TYPE, + calleeEffect: Effect.Read, + returnValueKind: ValueKind.Immutable, + }, + BuiltInSetStateId + ), + ], +]); + +addObject(BUILTIN_SHAPES, BuiltInUseRefId, [ + ["current", { kind: "Object", shapeId: BuiltInRefValueId }], +]); + +addObject(BUILTIN_SHAPES, BuiltInRefValueId, []); + export const DefaultMutatingHook = addHook(BUILTIN_SHAPES, [], { positionalParams: [], restParam: Effect.Mutate,