From baab42bd845249ee3cf052c074ecb86727332c14 Mon Sep 17 00:00:00 2001 From: Bigjango13 Date: Wed, 17 Jan 2024 03:20:40 -0500 Subject: [PATCH] Fix backwards compat for methods, static properties, vtable base addresses, and virtual methods --- src/method.ts | 9 +++++++-- src/property.ts | 9 +++++---- src/struct.ts | 16 ++-------------- src/vtable.ts | 12 ++++-------- 4 files changed, 18 insertions(+), 28 deletions(-) diff --git a/src/method.ts b/src/method.ts index a668df2..5b37bcf 100644 --- a/src/method.ts +++ b/src/method.ts @@ -1,4 +1,4 @@ -import { formatType } from './common'; +import { formatType, toHex } from './common'; export class Method { readonly self: string; @@ -30,8 +30,13 @@ export class Method { return `typedef ${returnType}(*${this.getType()})${this.args};\n`; } - // Generate Variable Definition + // Generate Method Definition generateDefinition(nameSuffix?: string) { return `${this.getType()} ${this.getName()}${nameSuffix !== undefined ? nameSuffix : ''};\n`; } + + // Generate Method Declaration + generateDeclaration() { + return `static ${this.getType()} ${this.getName()} = (${this.getType()}) ${toHex(this.address)};\n`; + } } \ No newline at end of file diff --git a/src/property.ts b/src/property.ts index 6c1014a..e6556c2 100644 --- a/src/property.ts +++ b/src/property.ts @@ -1,4 +1,4 @@ -import { POINTER_SIZE } from './common'; +import { POINTER_SIZE, toHex } from './common'; import { getStructure } from './map'; export interface Property { @@ -124,9 +124,10 @@ export class StaticProperty { return type; } - // Generate Variable Definition - generateDefinition() { - return `${this.getPointerType()}${this.getName()}_pointer;\n`; + // Generate Variable Declaration + generateDeclaration() { + const type = this.getPointerType(); + return `${type}${this.getName()}_pointer = (${type}) ${toHex(this.address)};\n`; } // Generate Macro diff --git a/src/struct.ts b/src/struct.ts index 0abf1df..fc41bd6 100644 --- a/src/struct.ts +++ b/src/struct.ts @@ -194,14 +194,14 @@ export class Struct { // Static Properties for (const property of this.#staticProperties) { - out += `extern ${property.generateDefinition()}`; + out += `static ${property.generateDeclaration()}`; out += property.generateMacro(); } // Methods for (const method of this.#methods) { out += method.generateTypedef(); - out += `extern ${method.generateDefinition()}`; + out += method.generateDeclaration(); } // Early Exit For Undefined Structures @@ -272,18 +272,6 @@ export class Struct { // Check this.#check(); - // Static Properties - for (const property of this.#staticProperties) { - init += `${INDENT}${property.getName()}_pointer = (${property.getPointerType()}) ${toHex(property.address)};\n`; - declarations += property.generateDefinition(); - } - - // Methods - for (const method of this.#methods) { - init += `${INDENT}${method.getName()} = (${method.getType()}) ${toHex(method.address)};\n`; - declarations += method.generateDefinition(); - } - // VTable if (this.#vtable !== null) { const vtable = this.#vtable.generateCode(); diff --git a/src/vtable.ts b/src/vtable.ts index 5a93f70..de50f8e 100644 --- a/src/vtable.ts +++ b/src/vtable.ts @@ -115,13 +115,15 @@ export class VTable implements Property { // Pointers if (this.#address !== null) { // Base - out += `extern ${this.#getName()} *${this.#getName()}_base;\n`; + const name = this.#getName(); + out += `static ${name} *${name}_base = (${name} *) ${toHex(this.#address)};\n`; // Methods for (let i = 0; i < this.#methods.length; i++) { const info = this.#methods[i]; if (info) { const type = `${info.getType()} *`; - out += `extern ${type}${info.getName()}_vtable_addr;\n`; + const vtableAddress = this.#address + (i * POINTER_SIZE); + out += `static ${type}${info.getName()}_vtable_addr = (${type}) ${toHex(vtableAddress)};\n`; out += `extern ${info.generateDefinition('_non_virtual')}`; } } @@ -147,16 +149,10 @@ export class VTable implements Property { // Pointers if (this.#address !== null) { // Base - init += `${INDENT}${this.#getName()}_base = (${this.#getName()} *) ${toHex(this.#address)};\n`; - declarations += `${this.#getName()} *${this.#getName()}_base;\n`; // Methods for (let i = 0; i < this.#methods.length; i++) { const info = this.#methods[i]; if (info) { - const vtableAddress = this.#address + (i * POINTER_SIZE); - const type = `${info.getType()} *`; - init += `${INDENT}${info.getName()}_vtable_addr = (${type}) ${toHex(vtableAddress)};\n`; - declarations += `${type}${info.getName()}_vtable_addr;\n`; init += `${INDENT}${info.getName()}_non_virtual = *${info.getName()}_vtable_addr;\n`; declarations += info.generateDefinition('_non_virtual'); } -- 2.45.2