Marshalling
In Plugify, marshalling types is an essential process that allows different programming languages to interoperate within the framework. This is achieved using an additional library, plugify-jit, which provides functionality for creating wrappers for calls and managing dynamic function generation.
In Plugify, marshalling types is an essential process that allows different programming languages to interoperate within the framework. This is achieved using an additional library, plugify-jit
, which provides functionality for creating wrappers for calls and managing dynamic function generation.
Using the plugify-jit
Library
The plugify-jit
library contains the JitCallback
and JitCall
classes, which is crucial for runtime function generation and dynamic function call.
JitCallback
That class can be used to create callback objects, that can be passed to functions as callback function pointers. In other words, a pointer to the callback object can be "called", directly. A generic callback handler invoked by this object then allows iterating dynamically over the arguments once called back.
The CallbackHandler type is defined as follows:
Here is a step-by-step guide on how to use the JitCallback
class:
- Initialize the Object: Create an instance of the
JitCallback
class. It requireasmjit
runtime object for executable memory allocation.
The function will be deallocated when it goes out of scope. Ensure that the JitCallback
object remains in scope as long as the generated function is needed, or use appropriate smart pointers to manage its lifetime.
- Generate the Function: Use the GetJitFunc method to get the address of the dynamically generated function.
Before calling GetJitFunc
, ensure that the method object passed as an argument is valid and will be valid. Also, correctly represents the method for which you want to generate a callback function.
- Implementation of Callback Function: This function should be responsible for converting types and calling the original function.
Detailed Example
Below is a more detailed example demonstrating the entire process:
JitCall
That class encapsulates architecture-, OS- and compiler-specific function call semantics in a virtual "bind argument parameters from left to right and then call" interface allowing programmers to call C functions in a completely dynamic manner. In other words, instead of calling a function directly, class provides a mechanism to push the function parameters manually and to issue the call afterwards.
Here is a step-by-step guide on how to use the JitCall
class:
- Initialize the Object: Create an instance of the
JitCall
class. It requireasmjit
runtime object for executable memory allocation.
The function will be deallocated when it goes out of scope. Ensure that the JitCall
object remains in scope as long as the generated function is needed, or use appropriate smart pointers to manage its lifetime.
- Generate the Function: Use the GetJitFunc method to get the address of the dynamically generated function.
Before calling GetJitFunc
, ensure that the method object passed as an argument is valid and will be valid. Also, correctly represents the method for which you want to generate a callback function.
Detailed Example
Below is a more detailed example demonstrating the entire process:
Benefits of Using
The plugify-jit library provides several benefits:
Dynamic Function Generation
: Allows the creation and calling of functions at runtime, making it flexible and adaptable to various use cases.Interoperability
: Facilitates communication between different programming languages within the Plugify framework.Simplified Integration
: Provides a straightforward API for generating and using dynamic functions, reducing the complexity of marshalling types.
By leveraging the plugify-jit library, language modules can efficiently manage function calls and ensure seamless integration within the Plugify ecosystem.
Linking Library
To link the plugify-jit
library with your language module, you simply need to add plugify::plugify-jit
to your CMake target. This tells CMake to link your language module with the plugify-jit
library during the build process.
Here's an example of how to add the link in your CMakeLists.txt file:
By adding this line to your CMakeLists.txt file, CMake will automatically handle linking the plugify-jit library with your language module, making the functionality of plugify-jit available to your code.