plugify  1.0.0.0
callback.hpp
1 #pragma once
2 
3 #include <asmjit/asmjit.h>
4 #include <plugify/mem_addr.hpp>
5 #include <plugify/method.hpp>
6 #include <string_view>
7 #include <utility>
8 #include <memory>
9 
10 namespace plugify {
19  class JitCallback {
20  public:
25  explicit JitCallback(std::weak_ptr<asmjit::JitRuntime> rt);
26 
31  JitCallback(JitCallback&& other) noexcept;
32 
37 
42  struct Parameters {
50  template<typename T>
51  void SetArgument(uint8_t idx, T val) const noexcept {
52  *(T*) GetArgumentPtr(idx) = val;
53  }
54 
63  template<typename T>
64  void SetArgumentAt(uint8_t idx, T val, uint8_t i = 0) const noexcept {
65  (*(T**) GetArgumentPtr(idx))[i] = val;
66  }
67 
74  template<typename T>
75  T GetArgument(uint8_t idx) const noexcept {
76  return *(T*) GetArgumentPtr(idx);
77  }
78 
84  int8_t* GetArgumentPtr(uint8_t idx) const noexcept {
85  return ((int8_t*) &arguments) + sizeof(uint64_t) * idx;
86  }
87 
88  private:
89  volatile uint64_t arguments;
90  };
91 
96  struct Return {
101  template<typename T, typename...Args>
102  void ConstructAt(Args&&... args) const noexcept {
103  std::construct_at((T*) GetReturnPtr(), std::forward<Args>(args)...);
104  }
105 
112  template<typename T>
113  void SetReturn(T val) const noexcept {
114  *(T*) GetReturnPtr() = val;
115  }
116 
122  template<typename T>
123  T GetReturn() const noexcept {
124  return *(T*) GetReturnPtr();
125  }
126 
131  int8_t* GetReturnPtr() const noexcept {
132  return (int8_t*) &ret;
133  }
134 
135  private:
136  volatile uint64_t ret;
137  };
138 
139  using CallbackHandler = void(*)(MethodRef method, MemAddr data, const Parameters* params, uint8_t count, const Return* ret);
140  using HiddenParam = bool(*)(ValueType);
141 
151  MemAddr GetJitFunc(const asmjit::FuncSignature& sig, MethodRef method, CallbackHandler callback, MemAddr data, bool hidden);
152 
169  MemAddr GetJitFunc(MethodRef method, CallbackHandler callback, MemAddr data = nullptr, HiddenParam hidden = &ValueUtils::IsHiddenParam);
170 
176  MemAddr GetFunction() const noexcept { return _function; }
177 
184  MemAddr GetUserData() const noexcept { return _userData; }
185 
190  std::string_view GetError() noexcept { return !_function && _errorCode ? _errorCode : ""; }
191 
192  private:
193  std::weak_ptr<asmjit::JitRuntime> _rt;
194  MemAddr _function;
195  union {
196  MemAddr _userData;
197  const char* _errorCode{};
198  };
199  };
200 } // namespace plugify
Class to create callback objects, that can be passed to functions as callback function pointers....
Definition: callback.hpp:19
MemAddr GetFunction() const noexcept
Get a dynamically created function.
Definition: callback.hpp:176
JitCallback(std::weak_ptr< asmjit::JitRuntime > rt)
Constructor.
std::string_view GetError() noexcept
Get the error message, if any.
Definition: callback.hpp:190
MemAddr GetUserData() const noexcept
Get the user data associated with the object.
Definition: callback.hpp:184
~JitCallback()
Destructor.
JitCallback(JitCallback &&other) noexcept
Move constructor.
MemAddr GetJitFunc(MethodRef method, CallbackHandler callback, MemAddr data=nullptr, HiddenParam hidden=&ValueUtils::IsHiddenParam)
Get a dynamically created function based on the method reference.
MemAddr GetJitFunc(const asmjit::FuncSignature &sig, MethodRef method, CallbackHandler callback, MemAddr data, bool hidden)
Get a dynamically created callback function based on the raw signature.
A wrapper class for memory addresses, providing utility functions for pointer manipulation.
Definition: mem_addr.hpp:11
A reference class for the Method structure.
Definition: method.hpp:118
constexpr bool IsHiddenParam(ValueType type) noexcept
Checks if a given ValueType is considered a hidden object parameter.
Definition: value_type.hpp:379
Structure to represent function parameters.
Definition: callback.hpp:42
int8_t * GetArgumentPtr(uint8_t idx) const noexcept
Get a pointer to the argument at the specified index.
Definition: callback.hpp:84
void SetArgumentAt(uint8_t idx, T val, uint8_t i=0) const noexcept
Set the value of the argument at the specified index and position within a multi-dimensional array.
Definition: callback.hpp:64
T GetArgument(uint8_t idx) const noexcept
Get the value of the argument at the specified index.
Definition: callback.hpp:75
void SetArgument(uint8_t idx, T val) const noexcept
Set the value of the argument at the specified index.
Definition: callback.hpp:51
void ConstructAt(Args &&... args) const noexcept
Constructs an object of type T at the memory location.
Definition: callback.hpp:102
T GetReturn() const noexcept
Get the return value.
Definition: callback.hpp:123
int8_t * GetReturnPtr() const noexcept
Get a pointer to the return value.
Definition: callback.hpp:131
void SetReturn(T val) const noexcept
Set the return value.
Definition: callback.hpp:113