diff --git a/src/syntax-objects/types.ts b/src/syntax-objects/types.ts index 47ac933e..d7257540 100644 --- a/src/syntax-objects/types.ts +++ b/src/syntax-objects/types.ts @@ -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() { @@ -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)); } @@ -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;