Skip to content

Commit

Permalink
Cleanup and optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
drew-y committed Sep 10, 2024
1 parent fe4c077 commit b7b6a94
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/semantics/resolution/resolve-fn-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export type ResolveFnTypesOpts = {

/** Pass call to potentially resolve generics */
export const resolveFnTypes = (fn: Fn, call?: Call): Fn => {
if (fn.resolved) {
if (fn.typesResolved) {
// Already resolved
return fn;
}
Expand All @@ -35,7 +35,7 @@ export const resolveFnTypes = (fn: Fn, call?: Call): Fn => {
fn.returnType = fn.annotatedReturnType;
}

fn.resolved = true;
fn.typesResolved = true;
fn.body = resolveTypes(fn.body);
fn.inferredReturnType = getExprType(fn.body);
fn.returnType = fn.annotatedReturnType ?? fn.inferredReturnType;
Expand Down
15 changes: 9 additions & 6 deletions src/semantics/resolution/resolve-object-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ export const resolveObjectTypeTypes = (
obj: ObjectType,
call?: Call
): ObjectType => {
if (obj.typeParameters && call) {
return resolveGenericObjVersion(call, obj) ?? obj;
if (obj.typesResolved) return obj;

if (obj.typeParameters) {
return resolveGenericObjVersion(obj, call) ?? obj;
}

obj.fields.forEach((field) => {
Expand All @@ -29,17 +31,18 @@ export const resolveObjectTypeTypes = (
obj.parentObjType = voidBaseObject;
}

obj.typesResolved = true;
return obj;
};

const resolveGenericObjVersion = (
call: Call,
type: ObjectType
type: ObjectType,
call?: Call
): ObjectType | undefined => {
if (!call.typeArgs) return;
if (!call?.typeArgs) return;
const existing = type.genericInstances?.find((c) => typeArgsMatch(call, c));
if (existing) return existing;
return resolveGenericsWithTypeArgs(type, call.typeArgs!);
return resolveGenericsWithTypeArgs(type, call.typeArgs);
};

const resolveGenericsWithTypeArgs = (
Expand Down
2 changes: 1 addition & 1 deletion src/syntax-objects/fn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class Fn extends ScopedNamedEntity {
_returnTypeExpr?: Expr;
inferredReturnType?: Type;
annotatedReturnType?: Type;
resolved?: boolean;
typesResolved?: boolean;
private _body?: Expr;

constructor(
Expand Down
1 change: 1 addition & 0 deletions src/syntax-objects/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ export class ObjectType extends BaseType {
parentObjType?: ObjectType;
/** Type used for locals, globals, function return type */
binaryenType?: number;
typesResolved?: boolean;

constructor(
opts: NamedEntityOpts & {
Expand Down

0 comments on commit b7b6a94

Please sign in to comment.