From 842f05a288de6be14fa92f97755d2e18acb00873 Mon Sep 17 00:00:00 2001 From: TheBrokenRail Date: Tue, 2 Apr 2024 15:21:05 -0400 Subject: [PATCH] Copy RTTI Info --- src/common.ts | 1 + src/vtable.ts | 14 +++++++++----- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/common.ts b/src/common.ts index cb63d1e..eb34b30 100644 --- a/src/common.ts +++ b/src/common.ts @@ -2,6 +2,7 @@ import * as fs from 'node:fs'; export const INDENT = ' '; export const POINTER_SIZE = 4; +export const RTTI_SIZE = POINTER_SIZE; export const EXTENSION = '.def'; export const STRUCTURE_FILES: {[id: string]: string} = {}; export function readDefinition(name: string) { diff --git a/src/vtable.ts b/src/vtable.ts index 2018a5e..39f0f99 100644 --- a/src/vtable.ts +++ b/src/vtable.ts @@ -1,4 +1,4 @@ -import { INDENT, POINTER_SIZE, Size, assertSize, getSelfArg, toHex } from './common'; +import { INDENT, POINTER_SIZE, RTTI_SIZE, Size, assertSize, getSelfArg, toHex } from './common'; import { Method } from './method'; import { Property } from './property'; @@ -170,12 +170,16 @@ export class VTable implements Property { // Duplication Method if (this.#size.isExact()) { declarations += `${this.#getName()} *dup_${this.#getName()}(${this.#getName()} *vtable) {\n`; - declarations += `${INDENT}${this.#getName()} *obj = new ${this.#getName()};\n`; - declarations += `${INDENT}if (obj == NULL) {\n`; + declarations += `${INDENT}uchar *real_vtable = (uchar *) vtable;\n`; + declarations += `${INDENT}real_vtable -= ${RTTI_SIZE};\n`; + declarations += `${INDENT}size_t real_vtable_size = sizeof(${this.#getName()}) + ${RTTI_SIZE};\n`; + declarations += `${INDENT}uchar *new_vtable = (uchar *) ::operator new(real_vtable_size);\n`; + declarations += `${INDENT}if (new_vtable == NULL) {\n`; declarations += `${INDENT}${INDENT}return NULL;\n`; declarations += `${INDENT}}\n`; - declarations += `${INDENT}memcpy((void *) obj, (void *) vtable, sizeof(${this.#getName()}));\n`; - declarations += `${INDENT}return obj;\n`; + declarations += `${INDENT}memcpy((void *) new_vtable, (void *) real_vtable, real_vtable_size);\n`; + declarations += `${INDENT}new_vtable += ${RTTI_SIZE};\n`; + declarations += `${INDENT}return (${this.#getName()} *) new_vtable;\n`; declarations += '}\n'; }