Finishing Touches!

This commit is contained in:
TheBrokenRail 2024-07-17 06:40:04 -04:00
parent b0814f257a
commit 10026e9a04
5 changed files with 30 additions and 58 deletions

View File

@ -29,12 +29,4 @@ __Function<Ret(Args...)>::__Function(const char *const name_, __Function<Ret(Arg
thunk(thunk_) {} thunk(thunk_) {}
// Thunks // Thunks
template <typename Ret, typename... Args> thunk_enabler_t thunk_enabler;
void __Function<Ret(Args...)>::enable_thunk(const thunk_enabler_t &thunk_enabler) {
if (enabled) {
ptr_type real_thunk = (ptr_type) thunk_enabler((void *) get(), (void *) thunk);
if (!is_virtual) {
func = real_thunk;
}
}
}

View File

@ -18,7 +18,7 @@ class __VirtualFunctionInfo {
// Thunks // Thunks
typedef void *(*thunk_enabler_t)(void *target, void *thunk); typedef void *(*thunk_enabler_t)(void *target, void *thunk);
void enable_all_thunks(const thunk_enabler_t &thunk_enabler); extern thunk_enabler_t thunk_enabler;
// Function Information // Function Information
template <typename Ret, typename... Args> template <typename Ret, typename... Args>
@ -40,25 +40,29 @@ public:
if (!enabled) { if (!enabled) {
return false; return false;
} }
// Enable Thunk
enable_thunk();
// Overwrite // Overwrite
type original = get_thunk_target(); type original = get_thunk_target();
thunk_target = [original, target](Args... args) { thunk_target = [original, target](Args... args) {
return target(original, args...); return target(original, std::forward<Args>(args)...);
}; };
return true; return true;
} }
// Getters // Getters
[[nodiscard]] ptr_type get_backup() const { [[nodiscard]] ptr_type get(bool result_will_be_stored) {
return backup;
}
[[nodiscard]] ptr_type get() const {
if (!enabled) { if (!enabled) {
return nullptr; return nullptr;
} else if (is_virtual) {
return *get_vtable_addr();
} else { } else {
return std::get<ptr_type>(func); if (result_will_be_stored) {
enable_thunk();
}
if (is_virtual) {
return *get_vtable_addr();
} else {
return std::get<ptr_type>(func);
}
} }
} }
[[nodiscard]] ptr_type *get_vtable_addr() const { [[nodiscard]] ptr_type *get_vtable_addr() const {
@ -68,14 +72,11 @@ public:
return nullptr; return nullptr;
} }
} }
[[nodiscard]] const char *get_name() const {
return name;
}
[[nodiscard]] type get_thunk_target() const { [[nodiscard]] type get_thunk_target() const {
if (thunk_target) { if (thunk_target) {
return thunk_target; return thunk_target;
} else { } else {
return get_backup(); return backup;
} }
} }
@ -84,6 +85,7 @@ private:
const bool is_virtual; const bool is_virtual;
std::variant<ptr_type, __VirtualFunctionInfo<ptr_type>> func; std::variant<ptr_type, __VirtualFunctionInfo<ptr_type>> func;
public:
// State // State
const bool enabled; const bool enabled;
const char *const name; const char *const name;
@ -91,9 +93,18 @@ private:
// Backup Of Original Function Pointer // Backup Of Original Function Pointer
const ptr_type backup; const ptr_type backup;
private:
// Thunk // Thunk
const ptr_type thunk; const ptr_type thunk;
type thunk_target; type thunk_target;
void enable_thunk(const thunk_enabler_t &thunk_enabler); bool thunk_enabled = false;
friend void enable_all_thunks(const thunk_enabler_t &); void enable_thunk() {
if (!thunk_enabled && enabled) {
ptr_type real_thunk = (ptr_type) thunk_enabler((void *) backup, (void *) thunk);
if (!is_virtual) {
func = real_thunk;
}
thunk_enabled = true;
}
}
}; };

View File

@ -12,9 +12,4 @@ struct __Thunk<Ret(Args...)> {
} }
}; };
{{ main }} {{ main }}
// Enable All Thunks
void enable_all_thunks(const thunk_enabler_t &thunk_enabler) {
{{ enableThunks }}
}

View File

@ -157,25 +157,22 @@ function makeCompiledCode(output: string) {
// Generate // Generate
let declarations = ''; let declarations = '';
let enableThunks = '';
for (const structure of structureObjects) { for (const structure of structureObjects) {
const name = structure.getName(); const name = structure.getName();
declarations += `// ${name}\n`; declarations += `// ${name}\n`;
try { try {
declarations += structure.generateCode(); declarations += structure.generateCode();
enableThunks += structure.generateEnableThunks();
} catch (e) { } catch (e) {
console.log(`Error Generating Code: ${name}: ${e instanceof Error ? e.stack : e}`); console.log(`Error Generating Code: ${name}: ${e instanceof Error ? e.stack : e}`);
process.exit(1); process.exit(1);
} }
declarations += '\n'; declarations += '\n';
} }
enableThunks = enableThunks.slice(0, -1); // Remove Last Newline
// Write // Write
const headerPath = fs.realpathSync(headerOutput); const headerPath = fs.realpathSync(headerOutput);
const main = declarations.trim(); const main = declarations.trim();
const result = formatFile('out.cpp', {headerPath, main, enableThunks, data: getDataDir()}); const result = formatFile('out.cpp', {headerPath, main, data: getDataDir()});
fs.writeFileSync(output, result); fs.writeFileSync(output, result);
} }
makeCompiledCode(sourceOutput); makeCompiledCode(sourceOutput);

View File

@ -144,7 +144,7 @@ export class Struct {
if (isVirtual) { if (isVirtual) {
call += `this->vtable->${method.shortName}`; call += `this->vtable->${method.shortName}`;
} else { } else {
call += `${method.getName()}->get()`; call += `${method.getName()}->get(false)`;
} }
call += '('; call += '(';
if (!method.isStatic) { if (!method.isStatic) {
@ -269,27 +269,4 @@ export class Struct {
setDirectParent(directParent: string) { setDirectParent(directParent: string) {
this.#directParent = directParent; this.#directParent = directParent;
} }
// Generate Part Of enable_all_thunks()
generateEnableThunks() {
// Get All Methods
const allMethods: Method[] = [];
for (const method of this.#methods) {
allMethods.push(method);
}
if (this.#vtable !== null && this.#vtable.canGenerateWrappers()) {
const virtualMethods = this.#vtable.getMethods();
for (const method of virtualMethods) {
if (method) {
allMethods.push(method);
}
}
}
// Generate
let out = '';
for (const method of allMethods) {
out += `${INDENT}${method.getName()}->enable_thunk(thunk_enabler);\n`;
}
return out;
}
} }