Files
2025-10-22 00:17:58 +03:00

207 lines
9.4 KiB
C++

#pragma once
#include <tuple>
#include <utility>
#include <type_traits>
#include <Mixin/Types.hpp>
#include <Mixin/CallbackInfo.hpp>
#include <Mixin/CallbackInfoExtended.hpp>
#include <Reflect/Signature.hpp>
// ---- Slot decoding helpers: these are pure API conventions that Core obeys ----
namespace vl::mixin::detail {
// Core passes args in this order:
// - instance methods: slot0 = pointer to Self (wrapper); then parameters 0..N-1
// - static methods: parameters 0..N-1
// Each slot is a pointer to the exact wrapper or to a trivially copyable primitive storage.
//
// from_slot<T>(void*)
// Overloads convert the slot to the target C++ type T.
template<class T>
inline T from_slot(void* p) {
// Default: value type stored directly in the slot's memory.
return *reinterpret_cast<std::remove_reference_t<T>*>(p);
}
// Specialize for references if you want by-ref semantics later.
// For object wrapper types we still copy by value (cheap handle).
template<class T>
inline T& from_slot_ref(void* p) {
return *reinterpret_cast<T*>(p);
}
// Build tuple of arguments from slots [base .. base+N)
template<class Tuple, std::size_t...I>
Tuple build_tuple(void** slots, std::index_sequence<I...>) {
return Tuple{ from_slot<std::tuple_element_t<I, Tuple>>(slots[I])... };
}
// Apply callable to (Self?, Args..., CallbackInfo &/Returnable&)
template<class F, class Self, class ArgsTuple, class CI>
auto call_with(F&& f, Self* self, void** argSlots, CI& ci) {
if constexpr (std::is_same_v<ArgsTuple, std::tuple<>>) {
if constexpr (std::is_same_v<Self, void>) {
return std::invoke(std::forward<F>(f), ci);
} else {
return std::invoke(std::forward<F>(f), *self, ci);
}
} else {
constexpr auto N = std::tuple_size_v<ArgsTuple>;
auto args = build_tuple<ArgsTuple>(argSlots, std::make_index_sequence<N>{});
if constexpr (std::is_same_v<Self, void>) {
return std::apply([&](auto&&... unpacked){
return std::invoke(std::forward<F>(f), std::forward<decltype(unpacked)>(unpacked)..., ci);
}, std::move(args));
} else {
return std::apply([&](auto&&... unpacked){
return std::invoke(std::forward<F>(f), *self, std::forward<decltype(unpacked)>(unpacked)..., ci);
}, std::move(args));
}
}
}
} // namespace vl::mixin::detail
namespace vl::mixin {
// ----------------------- INJECT typed adapter ----------------------------------
// Accepts F with signature: (Self[, Args...], CallbackInfo&)
// Returns ErasedCallback that Core can call.
template<auto MethodPtr, class F>
vl::mixin::ErasedCallback make_typed_inject(F&& f) {
using Sig = ::vl::reflect::method_signature<MethodPtr>;
using SelfT = std::conditional_t<Sig::is_static, void, typename Sig::Self>;
using ArgsT = typename Sig::Args;
return [fn = std::forward<F>(f)](void** args, int argc, void* cbinfo) -> void* {
// Layout: [self?], [arg0..argN-1]
void** cursor = args;
SelfT* selfPtr = nullptr;
if constexpr (!Sig::is_static) {
selfPtr = reinterpret_cast<SelfT*>(*cursor++);
}
// Consume parameters from remaining slots
(void)argc; // not needed with typed tuple
auto& ci = *reinterpret_cast<vl::mixin::CallbackInfo*>(cbinfo);
vl::mixin::detail::call_with(fn, selfPtr, cursor, ci);
return nullptr; // inject doesn't produce a direct return value
};
}
// ----------------------- REDIRECT typed adapter --------------------------------
// Accepts F with signature: (Self[, Args...], CallbackInfoReturnable<Ret>&) -> Ret or void (set via ci)
template<auto MethodPtr, class F>
vl::mixin::ErasedCallback make_typed_redirect(F&& f) {
using Sig = ::vl::reflect::method_signature<MethodPtr>;
static_assert(!std::is_same_v<typename Sig::Ret, void>,
"REDIRECT requires method with non-void return type");
using RetT = typename Sig::Ret;
using SelfT = std::conditional_t<Sig::is_static, void, typename Sig::Self>;
using ArgsT = typename Sig::Args;
return [fn = std::forward<F>(f)](void** args, int argc, void* cbinfo) -> void* {
void** cursor = args;
SelfT* selfPtr = nullptr;
if constexpr (!Sig::is_static) selfPtr = reinterpret_cast<SelfT*>(*cursor++);
auto& ci = *reinterpret_cast<vl::mixin::CallbackInfoReturnable<RetT>*>(cbinfo);
if constexpr (std::is_void_v<std::invoke_result_t<F, SelfT&, /*Args...*/ std::tuple_element_t<0, ArgsT>, vl::mixin::CallbackInfoReturnable<RetT>&>>) {
// callable returns void, must set ci.setReturnValue(...)
vl::mixin::detail::call_with<F, SelfT, ArgsT, vl::mixin::CallbackInfoReturnable<RetT>>(fn, selfPtr, cursor, ci);
if (ci.hasReturnValue()) {
// Core expects pointer to value storage
return const_cast<RetT*>(&ci.getReturnValue());
}
return nullptr; // Core will fall back to original if not cancelled
} else {
// callable returns Ret directly
static RetT storage{};
storage = vl::mixin::detail::call_with<F, SelfT, ArgsT, vl::mixin::CallbackInfoReturnable<RetT>>(fn, selfPtr, cursor, ci);
ci.setReturnValue(storage); // also mark cancelled
return &storage;
}
};
}
// --------------------------- FIELD_GET ------------------------------------
// lambda signature: (Self[, CallbackInfoField<FieldT>&]) -> void
// if cancelled and ci.hasReturnValue(), value replaces field read
template<auto FieldPtr, class F, class FieldT, class SelfT>
vl::mixin::ErasedCallback make_typed_field_get(F&& fn) {
return [f = std::forward<F>(fn)](void** args, int argc, void* cbinfo) -> void* {
(void)argc;
SelfT* self = reinterpret_cast<SelfT*>(args[0]);
auto& ci = *reinterpret_cast<vl::mixin::CallbackInfoField<FieldT>*>(cbinfo);
std::invoke(f, *self, ci);
if (ci.hasReturnValue()) return const_cast<FieldT*>(&ci.getReturnValue());
return nullptr;
};
}
// --------------------------- FIELD_PUT ------------------------------------
// lambda signature: (Self, FieldT&, CallbackInfoField<FieldT>&)
template<auto FieldPtr, class F, class FieldT, class SelfT>
vl::mixin::ErasedCallback make_typed_field_put(F&& fn) {
return [f = std::forward<F>(fn)](void** args, int argc, void* cbinfo) -> void* {
(void)argc;
SelfT* self = reinterpret_cast<SelfT*>(args[0]);
FieldT* incoming = reinterpret_cast<FieldT*>(args[1]);
auto& ci = *reinterpret_cast<vl::mixin::CallbackInfoField<FieldT>*>(cbinfo);
std::invoke(f, *self, *incoming, ci);
return nullptr;
};
}
// ------------------- INVOKE BEFORE -------------------------------------
// lambda signature: (Self[, Args...], CallbackInfoReturnable<Ret>&)
// may cancel original call and set replacement return
template<auto TargetMethodPtr, class F>
ErasedCallback make_typed_invoke_before(F&& f) {
using Sig = ::vl::reflect::method_signature<TargetMethodPtr>;
using RetT = typename Sig::Ret;
using SelfT = std::conditional_t<Sig::is_static, void, typename Sig::Self>;
using ArgsT = typename Sig::Args;
return [fn = std::forward<F>(f)](void** args, int argc, void* cbinfo) -> void* {
void** cursor = args;
SelfT* selfPtr = nullptr;
if constexpr (!Sig::is_static) selfPtr = reinterpret_cast<SelfT*>(*cursor++);
auto& ci = *reinterpret_cast<CallbackInfoReturnable<RetT>*>(cbinfo);
detail::call_with(fn, selfPtr, cursor, ci);
if (ci.isCancelled() && ci.hasReturnValue()) {
return const_cast<RetT*>(&ci.getReturnValue());
}
return nullptr; // continue to original invoke
};
}
// ------------------- INVOKE AFTER --------------------------------------
// lambda signature: (Self[, Args...], Ret&, CallbackInfoInvokeAfter<Ret>&)
template<auto TargetMethodPtr, class F>
ErasedCallback make_typed_invoke_after(F&& f) {
using Sig = ::vl::reflect::method_signature<TargetMethodPtr>;
using RetT = typename Sig::Ret;
using SelfT = std::conditional_t<Sig::is_static, void, typename Sig::Self>;
using ArgsT = typename Sig::Args;
return [fn = std::forward<F>(f)](void** args, int argc, void* cbinfo) -> void* {
void** cursor = args;
SelfT* selfPtr = nullptr;
if constexpr (!Sig::is_static) selfPtr = reinterpret_cast<SelfT*>(*cursor++);
auto& ci = *reinterpret_cast<CallbackInfoInvokeAfter<RetT>*>(cbinfo);
// Last slot in args[] is original return value (Core places it)
RetT* retSlot = reinterpret_cast<RetT*>(cursor[argc - 1]);
ci.setOriginalReturn(*retSlot);
// Call the mod callback
std::invoke(fn, *selfPtr, *retSlot, ci);
// If cancelled and ci.hasReturnValue(), Core replaces return with it
if (ci.isCancelled() && ci.hasReturnValue())
return const_cast<RetT*>(&ci.getReturnValue());
return nullptr;
};
}
}