diff --git a/src/common.ts b/src/common.ts index a79439a..5f70dbd 100644 --- a/src/common.ts +++ b/src/common.ts @@ -134,7 +134,13 @@ export class Size { this.#pointerAligned = pointerAligned; } + has() { + return this.#size !== null; + } get() { + if (this.#size === null) { + throw new Error('No Size Specified'); + } return this.#size; } set(size: number, isExact: boolean) { diff --git a/src/struct.ts b/src/struct.ts index b58db90..12d8b12 100644 --- a/src/struct.ts +++ b/src/struct.ts @@ -63,13 +63,15 @@ export class Struct { let size; if (this.#size.isExact()) { // Exact Size Is Specified - size = this.#size.get()!; + size = this.#size.get(); } else { // Specified Size Is A Lower Bound size = this.#getRealSize(); - const specifiedSize = this.#size.get(); - if (specifiedSize !== null && specifiedSize > size) { - size = specifiedSize; + if (this.#size.has()) { + const specifiedSize = this.#size.get(); + if (specifiedSize > size) { + size = specifiedSize; + } } } if (round) { diff --git a/src/vtable.ts b/src/vtable.ts index 002b2a6..1c51458 100644 --- a/src/vtable.ts +++ b/src/vtable.ts @@ -69,8 +69,8 @@ export class VTable implements Property { // Check #check() { // Check Size - const size = this.#size.get(); - if (size !== null) { + if (this.#size.has()) { + const size = this.#size.get(); const maxMethodCount = size / POINTER_SIZE; if (this.#size.isExact() && maxMethodCount < this.#methods.length) { throw new Error(`VTable Size Too Small: ${toHex(size)}`); @@ -112,9 +112,8 @@ export class VTable implements Property { out += `};\n`; // Sanity Check Size - const rawSize = this.#size.get(); const isSizeDefined = this.#size.isExact(); - const size = isSizeDefined ? rawSize! : (this.#methods.length * POINTER_SIZE); + const size = isSizeDefined ? this.#size.get() : (this.#methods.length * POINTER_SIZE); out += assertSize(this.#getName(), size, isSizeDefined); // Pointers