#pragma once #include #include #include #include #include #include #include // ---- 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(void*) // Overloads convert the slot to the target C++ type T. template inline T from_slot(void* p) { // Default: value type stored directly in the slot's memory. return *reinterpret_cast*>(p); } // Specialize for references if you want by-ref semantics later. // For object wrapper types we still copy by value (cheap handle). template inline T& from_slot_ref(void* p) { return *reinterpret_cast(p); } // Build tuple of arguments from slots [base .. base+N) template Tuple build_tuple(void** slots, std::index_sequence) { return Tuple{ from_slot>(slots[I])... }; } // Apply callable to (Self?, Args..., CallbackInfo &/Returnable&) template auto call_with(F&& f, Self* self, void** argSlots, CI& ci) { if constexpr (std::is_same_v>) { if constexpr (std::is_same_v) { return std::invoke(std::forward(f), ci); } else { return std::invoke(std::forward(f), *self, ci); } } else { constexpr auto N = std::tuple_size_v; auto args = build_tuple(argSlots, std::make_index_sequence{}); if constexpr (std::is_same_v) { return std::apply([&](auto&&... unpacked){ return std::invoke(std::forward(f), std::forward(unpacked)..., ci); }, std::move(args)); } else { return std::apply([&](auto&&... unpacked){ return std::invoke(std::forward(f), *self, std::forward(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 vl::mixin::ErasedCallback make_typed_inject(F&& f) { using Sig = ::vl::reflect::method_signature; using SelfT = std::conditional_t; using ArgsT = typename Sig::Args; return [fn = std::forward(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(*cursor++); } // Consume parameters from remaining slots (void)argc; // not needed with typed tuple auto& ci = *reinterpret_cast(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 or void (set via ci) template vl::mixin::ErasedCallback make_typed_redirect(F&& f) { using Sig = ::vl::reflect::method_signature; static_assert(!std::is_same_v, "REDIRECT requires method with non-void return type"); using RetT = typename Sig::Ret; using SelfT = std::conditional_t; using ArgsT = typename Sig::Args; return [fn = std::forward(f)](void** args, int argc, void* cbinfo) -> void* { void** cursor = args; SelfT* selfPtr = nullptr; if constexpr (!Sig::is_static) selfPtr = reinterpret_cast(*cursor++); auto& ci = *reinterpret_cast*>(cbinfo); if constexpr (std::is_void_v, vl::mixin::CallbackInfoReturnable&>>) { // callable returns void, must set ci.setReturnValue(...) vl::mixin::detail::call_with>(fn, selfPtr, cursor, ci); if (ci.hasReturnValue()) { // Core expects pointer to value storage return const_cast(&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>(fn, selfPtr, cursor, ci); ci.setReturnValue(storage); // also mark cancelled return &storage; } }; } // --------------------------- FIELD_GET ------------------------------------ // lambda signature: (Self[, CallbackInfoField&]) -> void // if cancelled and ci.hasReturnValue(), value replaces field read template vl::mixin::ErasedCallback make_typed_field_get(F&& fn) { return [f = std::forward(fn)](void** args, int argc, void* cbinfo) -> void* { (void)argc; SelfT* self = reinterpret_cast(args[0]); auto& ci = *reinterpret_cast*>(cbinfo); std::invoke(f, *self, ci); if (ci.hasReturnValue()) return const_cast(&ci.getReturnValue()); return nullptr; }; } // --------------------------- FIELD_PUT ------------------------------------ // lambda signature: (Self, FieldT&, CallbackInfoField&) template vl::mixin::ErasedCallback make_typed_field_put(F&& fn) { return [f = std::forward(fn)](void** args, int argc, void* cbinfo) -> void* { (void)argc; SelfT* self = reinterpret_cast(args[0]); FieldT* incoming = reinterpret_cast(args[1]); auto& ci = *reinterpret_cast*>(cbinfo); std::invoke(f, *self, *incoming, ci); return nullptr; }; } // ------------------- INVOKE BEFORE ------------------------------------- // lambda signature: (Self[, Args...], CallbackInfoReturnable&) // may cancel original call and set replacement return template ErasedCallback make_typed_invoke_before(F&& f) { using Sig = ::vl::reflect::method_signature; using RetT = typename Sig::Ret; using SelfT = std::conditional_t; using ArgsT = typename Sig::Args; return [fn = std::forward(f)](void** args, int argc, void* cbinfo) -> void* { void** cursor = args; SelfT* selfPtr = nullptr; if constexpr (!Sig::is_static) selfPtr = reinterpret_cast(*cursor++); auto& ci = *reinterpret_cast*>(cbinfo); detail::call_with(fn, selfPtr, cursor, ci); if (ci.isCancelled() && ci.hasReturnValue()) { return const_cast(&ci.getReturnValue()); } return nullptr; // continue to original invoke }; } // ------------------- INVOKE AFTER -------------------------------------- // lambda signature: (Self[, Args...], Ret&, CallbackInfoInvokeAfter&) template ErasedCallback make_typed_invoke_after(F&& f) { using Sig = ::vl::reflect::method_signature; using RetT = typename Sig::Ret; using SelfT = std::conditional_t; using ArgsT = typename Sig::Args; return [fn = std::forward(f)](void** args, int argc, void* cbinfo) -> void* { void** cursor = args; SelfT* selfPtr = nullptr; if constexpr (!Sig::is_static) selfPtr = reinterpret_cast(*cursor++); auto& ci = *reinterpret_cast*>(cbinfo); // Last slot in args[] is original return value (Core places it) RetT* retSlot = reinterpret_cast(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(&ci.getReturnValue()); return nullptr; }; } }