Skip to content

Commit

Permalink
Fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
drew-y committed Sep 9, 2024
1 parent 9ad199b commit 6500917
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ ast.json
.env
test.void
/playground.*
/playground
1 change: 1 addition & 0 deletions src/assembler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,7 @@ const buildDSArrayType = (opts: CompileExprOpts, type: DSArrayType) => {
/** TODO: Skip building types for object literals that are part of an initializer of an obj */
const buildObjectType = (opts: CompileExprOpts, obj: ObjectType): TypeRef => {
if (obj.binaryenType) return obj.binaryenType;
if (obj.typeParameters) return opts.mod.nop();
const mod = opts.mod;

const binaryenType = defineStructType(mod, {
Expand Down
5 changes: 5 additions & 0 deletions src/semantics/check-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,11 @@ const checkVarTypes = (variable: Variable): Variable => {
};

const checkObjectType = (obj: ObjectType): ObjectType => {
if (obj.genericInstances) {
obj.genericInstances.forEach(checkTypes);
return obj;
}

obj.fields.forEach((field) => {
if (!field.type) {
throw new Error(`Unable to determine type for ${field.typeExpr}`);
Expand Down
17 changes: 16 additions & 1 deletion src/semantics/init-entities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,9 +291,24 @@ const initDSArray = (type: List) => {

const initNominalObjectType = (obj: List) => {
const hasExtension = obj.optionalIdentifierAt(2)?.is("extends");
const hasGenerics = obj.at(1)?.isList();

const name = hasGenerics
? obj.listAt(1).identifierAt(0)
: obj.identifierAt(1);

const typeParameters = hasGenerics
? obj
.listAt(1)
.listAt(1)
.sliceAsArray(1)
.flatMap((p) => (p.isIdentifier() ? p : []))
: undefined;

return new ObjectType({
...obj.metadata,
name: obj.identifierAt(1),
name,
typeParameters,
parentObjExpr: hasExtension ? obj.at(3) : undefined,
value: extractObjectFields(hasExtension ? obj.listAt(4) : obj.listAt(2)),
});
Expand Down
7 changes: 4 additions & 3 deletions src/semantics/resolution/resolve-object-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const resolveGenericObjVersion = (
call: Call,
type: ObjectType
): ObjectType | undefined => {
if (!call.typeArgs) return;
const existing = type.genericInstances?.find((c) => typeArgsMatch(call, c));
if (existing) return existing;
return resolveGenericsWithTypeArgs(type, call.typeArgs!);
Expand Down Expand Up @@ -69,9 +70,9 @@ const resolveGenericsWithTypeArgs = (
newObj.registerEntity(type);
});

const resolvedFn = resolveObjectTypeTypes(newObj);
obj.registerGenericInstance(resolvedFn);
return obj;
const resolvedObj = resolveObjectTypeTypes(newObj);
obj.registerGenericInstance(resolvedObj);
return resolvedObj;
};

const typeArgsMatch = (call: Call, candidate: ObjectType): boolean =>
Expand Down
3 changes: 3 additions & 0 deletions src/syntax-objects/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ export class ObjectType extends BaseType {
value: ObjectField[];
parentObjExpr?: Expr;
parentObj?: ObjectType;
typeParameters?: Identifier[];
}
) {
super(opts);
Expand All @@ -185,6 +186,7 @@ export class ObjectType extends BaseType {
});
this.parentObj = opts.parentObj;
this.parentObjExpr = opts.parentObjExpr;
this.typeParameters = opts.typeParameters;
}

get size() {
Expand Down Expand Up @@ -212,6 +214,7 @@ export class ObjectType extends BaseType {
})),
parentObj: this.parentObj,
parentObjExpr: this.parentObj?.clone(),
typeParameters: this.typeParameters,
});
}

Expand Down

0 comments on commit 6500917

Please sign in to comment.