Skip to content

Commit

Permalink
Allow objects to be extended
Browse files Browse the repository at this point in the history
  • Loading branch information
drew-y committed Aug 31, 2024
1 parent 7f82d9a commit 09dd520
Showing 1 changed file with 26 additions and 3 deletions.
29 changes: 26 additions & 3 deletions src/syntax-objects/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,14 +163,20 @@ export type ObjectField = { name: string; typeExpr: Expr; type?: Type };
export class ObjectType extends BaseType {
readonly kindOfType = "object";
fields: ObjectField[];
parentObj?: ObjectType;
/** Type used for locals, globals, function return type */
binaryenType?: number;
/** Type used for initializing the heap type */
binaryenHeapType?: number;

constructor(opts: NamedEntityOpts & { value: ObjectField[] }) {
constructor(
opts: NamedEntityOpts & {
value: ObjectField[];
parentObj?: ObjectType | null;
}
) {
super(opts);
this.fields = opts.value;
this.parentObj =
opts.parentObj !== null ? opts.parentObj ?? voidBaseObject : undefined;
}

get size() {
Expand All @@ -191,6 +197,18 @@ export class ObjectType extends BaseType {
});
}

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

if (this.parentObj) {
return this.parentObj.extends(type);
}

return false;
}

hasField(name: Id) {
return this.fields.some((field) => field.name === getIdStr(name));
}
Expand Down Expand Up @@ -272,4 +290,9 @@ export const i64 = PrimitiveType.from("i64");
export const f64 = PrimitiveType.from("f64");
export const bool = PrimitiveType.from("bool");
export const dVoid = PrimitiveType.from("void");
export const voidBaseObject = new ObjectType({
name: "Object",
value: [],
parentObj: null,
});
export const CDT_ADDRESS_TYPE = i32;

0 comments on commit 09dd520

Please sign in to comment.