Skip to content

Commit

Permalink
It works! (Probably with dragons)
Browse files Browse the repository at this point in the history
  • Loading branch information
drew-y committed Aug 31, 2024
1 parent 3286c92 commit ddee4d2
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
3 changes: 3 additions & 0 deletions src/assembler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,9 @@ const buildObjectType = (
type: mapBinaryenType(mod, field.type!),
name: field.name,
})),
supertype: obj.parentObj
? binaryenTypeToHeapType(mapBinaryenType(mod, obj.parentObj))
: undefined,
});
obj.binaryenType = binaryenType;
return binaryenType;
Expand Down
20 changes: 11 additions & 9 deletions src/semantics/check-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ const checkCallTypes = (call: Call): Call | ObjectLiteral => {

const type = getIdentifierType(call.fnName);
if (type?.isObjectType()) {
return checkObjectLiteralInit(call, type);
return checkObjectInit(call, type);
}

call.fn = resolveCallFn(call);
Expand All @@ -68,16 +68,13 @@ const checkCallTypes = (call: Call): Call | ObjectLiteral => {
return call;
};

const checkObjectLiteralInit = (
call: Call,
type: ObjectType
): ObjectLiteral => {
const checkObjectInit = (call: Call, type: ObjectType): ObjectLiteral => {
const literal = call.argAt(0);
if (!literal?.isObjectLiteral()) {
throw new Error(`Expected object literal, got ${literal}`);
}

if (!typesAreEquivalent(literal.type, type)) {
if (!typesAreEquivalent(literal.type, type, true)) {
throw new Error(`Object literal type does not match expected type`);
}

Expand Down Expand Up @@ -512,7 +509,8 @@ const findBestFnMatch = (candidates: Fn[], call: Call): Fn => {
throw new Error(`Could not determine type. I'm helpful >.<`);
}

return (score += argType.extensionDistance(param.type));
const distance = argType.extensionDistance(param.type);
return score + distance;
}, 0);

if (lowestScore === undefined) {
Expand Down Expand Up @@ -549,7 +547,11 @@ const getExprLabel = (expr?: Expr): string | undefined => {
return id.value;
};

const typesAreEquivalent = (a?: Type, b?: Type): boolean => {
const typesAreEquivalent = (
a?: Type,
b?: Type,
ignoreExtension?: boolean // Hacky
): boolean => {
if (!a || !b) return false;

if (a.isPrimitiveType() && b.isPrimitiveType()) {
Expand All @@ -558,7 +560,7 @@ const typesAreEquivalent = (a?: Type, b?: Type): boolean => {

if (a.isObjectType() && b.isObjectType()) {
return (
a.extends(b) &&
(ignoreExtension || a.extends(b)) &&
a.fields.every((field) => {
const match = b.fields.find((f) => f.name === field.name);
return match && typesAreEquivalent(field.type, match.type);
Expand Down
4 changes: 2 additions & 2 deletions src/syntax-objects/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ export class ObjectType extends BaseType {
}

extends(ancestor: ObjectType): boolean {
if (this.constructor === ancestor.constructor) {
if (this === ancestor) {
return true;
}

Expand All @@ -216,7 +216,7 @@ export class ObjectType extends BaseType {
* 0 = same type, 1 = ancestor is parent, 2 = ancestor is grandparent, etc
*/
extensionDistance(ancestor: ObjectType, start = 0): number {
if (this.constructor === ancestor.constructor) {
if (this === ancestor) {
return start;
}

Expand Down

0 comments on commit ddee4d2

Please sign in to comment.