Export Functions
Guide to export functions from your plugin to be used by other language modules within Plugify.
In the Plugify ecosystem, D plugins can export functions to make them accessible to other plugins. This guide explains how to define and export functions in D and provides examples to help you integrate your plugins seamlessly.
Basic Type Mapping
The following table lists how types are exposed to the D API:
C++ Type | D Type | Plugify Alias | Ref Support ? |
---|---|---|---|
void | void | void | ❌ |
bool | bool | bool | ✅ |
char | char | char8 | ✅ |
char16_t | wchar | char16 | ✅ |
int8_t | byte | int8 | ✅ |
int16_t | short | int16 | ✅ |
int32_t | int | int32 | ✅ |
int64_t | long | int64 | ✅ |
uint8_t | ubyte | uint8 | ✅ |
uint16_t | ushort | uint16 | ✅ |
uint32_t | uint | uint32 | ✅ |
uint64_t | ulong | uint64 | ✅ |
uintptr_t | size_t | ptr64 | ✅ |
uintptr_t | size_t | ptr32 | ✅ |
float | float | float | ✅ |
double | double | double | ✅ |
void* | void* | function | ❌ |
plg::string | string | string | ✅ |
plg::any | Variant | any | ✅ |
plg::vector<bool> | bool | bool | ✅ |
plg::vector<char> | char | char8 | ✅ |
plg::vector<char16_t> | wchar | char16 | ✅ |
plg::vector<int8_t> | byte | int8 | ✅ |
plg::vector<int16_t> | short | int16 | ✅ |
plg::vector<int32_t> | int | int32 | ✅ |
plg::vector<int64_t> | long | int64 | ✅ |
plg::vector<uint8_t> | ubyte | uint8 | ✅ |
plg::vector<uint16_t> | ushort | uint16 | ✅ |
plg::vector<uint32_t> | uint | uint32 | ✅ |
plg::vector<uint64_t> | ulong | uint64 | ✅ |
plg::vector<uintptr_t> | size_t | ptr64 | ✅ |
plg::vector<uintptr_t> | size_t | ptr32 | ✅ |
plg::vector<float> | float | float | ✅ |
plg::vector<double> | double | double | ✅ |
plg::vector<plg::string> | string | string | ✅ |
plg::vector<plg::any> | Variant | any | ✅ |
plg::vector<plg::vec2> | Vector2 | vec2 | ✅ |
plg::vector<plg::vec3> | Vector3 | vec3 | ✅ |
plg::vector<plg::vec4> | Vector4 | vec4 | ✅ |
plg::vector<plg::mat4x4> | Matrix4x4 | mat4x4 | ✅ |
plg::vec2 | Vector2 | vec2 | ✅ |
plg::vec3 | Vector3 | vec3 | ✅ |
plg::vec4 | Vector4 | vec4 | ✅ |
plg::mat4x4 | Matrix4x4 | mat4x4 | ✅ |
Exporting Functions in D
Exporting functions in D requires wrapping them with the mixin Export
directive to create extern(C)
functions compatible with Plugify. These functions can then be called by other plugins.
Basic Example
Here’s a simple example of exporting a function in a D plugin:
Function Definition
Plugin Manifest
To export the function, describe it in the plugin manifest under the exportedMethods
section:
Parameter and Return Type Conventions
Plugify uses specific conventions for parameter and return types to ensure compatibility across plugins. Below are the guidelines for D:
1. Primitive Types
- Parameter: Pass primitive types (e.g.,
int
,double
) directly. - Return: Return primitive types directly.
2. Strings
- Parameter: Pass strings as
string
. - Return: Return strings as
string
.
3. Arrays
- Parameter: Pass arrays as
T[]
. - Return: Return arrays as
T[]
.
4. Structures
- Parameter: Pass structures by reference (e.g.,
ref MyStruct
). - Return: Return structures by value.
Advanced Example: Exporting Complex Functions
Here’s an example of exporting a function with complex parameter and return types:
Function Definition
Plugin Manifest
Handling Callbacks
Plugify allows you to export functions that accept callbacks as parameters. Here’s an example:
Function Definition
Plugin Manifest
Best Practices
- Use
mixin Export
: Always use themixin Export
directive to createextern(C)
functions for export. - Follow Type Conventions: Adhere to Plugify's type conventions for parameters and return values.
- Document Your Functions: Clearly document the purpose, parameters, and return values of exported functions.
- Test Thoroughly: Test your exported functions to ensure they work as expected when called by other plugins.
- Update the Manifest: Always describe exported functions in the plugin manifest under the
exportedMethods
section.
Conclusion
Exporting functions in D plugins is straightforward when you follow Plugify's conventions and best practices. By using the mixin Export
directive, adhering to type conventions, and describing functions in the plugin manifest, you can create robust and interoperable plugins. For more advanced use cases, such as handling callbacks, use the techniques outlined in this guide.