Export Functions
Guide to export functions from your plugin to be used by other language modules within Plugify.
In the Plugify ecosystem, Lua plugins can export functions to make them accessible to other plugins. This guide explains how to define and export functions in Lua and provides examples to help you integrate your plugins seamlessly.
Basic Type Mapping
The following table lists how types are exposed to the Lua API:
| C++ Type | Lua Type | Plugify Alias | Ref Support ? |
|---|---|---|---|
| void | nil | void | ❌ |
| bool | boolean | bool | ✅ |
| char | string | char8 | ✅ |
| char16_t | string | char16 | ✅ |
| int8_t | integer | int8 | ✅ |
| int16_t | integer | int16 | ✅ |
| int32_t | integer | int32 | ✅ |
| int64_t | integer | int64 | ✅ |
| uint8_t | integer | uint8 | ✅ |
| uint16_t | integer | uint16 | ✅ |
| uint32_t | integer | uint32 | ✅ |
| uint64_t | integer | uint64 | ✅ |
| uintptr_t | integer | ptr64 | ✅ |
| uintptr_t | integer | ptr32 | ✅ |
| float | number | float | ✅ |
| double | number | double | ✅ |
| void* | function | function | ❌ |
| plg::string | string | string | ✅ |
| plg::any | any | any | ✅ |
| plg::vector<bool> | table | bool[] | ✅ |
| plg::vector<char> | table | char8[] | ✅ |
| plg::vector<char16_t> | table | char16[] | ✅ |
| plg::vector<int8_t> | table | int8[] | ✅ |
| plg::vector<int16_t> | table | int16[] | ✅ |
| plg::vector<int32_t> | table | int32[] | ✅ |
| plg::vector<int64_t> | table | int64[] | ✅ |
| plg::vector<uint8_t> | table | uint8[] | ✅ |
| plg::vector<uint16_t> | table | uint16[] | ✅ |
| plg::vector<uint32_t> | table | uint32[] | ✅ |
| plg::vector<uint64_t> | table | uint64[] | ✅ |
| plg::vector<uintptr_t> | table | ptr64[] | ✅ |
| plg::vector<uintptr_t> | table | ptr32[] | ✅ |
| plg::vector<float> | table | float[] | ✅ |
| plg::vector<double> | table | double[] | ✅ |
| plg::vector<plg::string> | table | string[] | ✅ |
| plg::vector<plg::any> | table | any[] | ✅ |
| plg::vector<plg::vec2> | table | vec2[] | ✅ |
| plg::vector<plg::vec3> | table | vec3[] | ✅ |
| plg::vector<plg::vec4> | table | vec4[] | ✅ |
| plg::vector<plg::mat4x4> | table | mat4x4[] | ✅ |
| plg::vec2 | Vector2 | vec2 | ✅ |
| plg::vec3 | Vector3 | vec3 | ✅ |
| plg::vec4 | Vector4 | vec4 | ✅ |
| plg::mat4x4 | Matrix4x4 | mat4x4 | ✅ |
Exporting Functions in Lua
Lua functions can be exported by defining them in your plugin and referencing them in the .pplugin manifest. Plugify’s Lua Language Module will handle the necessary type conversions.
Basic Example
Here’s a simple example of exporting a function in a Lua plugin:
Function Definition
Plugin Manifest
To export the function, describe it in the plugin manifest under the methods section:
Advanced Example: Exporting Complex Functions
Here’s an example of exporting a function with complex parameter and return types:
Function Definition
Plugin Manifest
Exporting Functions with References
Plugify supports reference parameters in dynamically-typed languages like Lua, but they work differently than in statically-typed languages. In Lua, you cannot directly modify parameters passed by reference. Instead, you must return a tuple where:
- First element: The function's return value (use
nilif the function returnsvoid) - Remaining elements: Modified values for all reference parameters, in order
Function Definition with Reference Parameters
Plugin Manifest with Reference Parameters
In the manifest, mark parameters that should be treated as references using "ref": true:
How It Works
- Return Order: The first return value is the function's actual return value. Subsequent values correspond to reference parameters in order.
- Void Functions: If the function returns
void, the first element of the tuple must benil. - Mixed Returns: If a function has both a return value and reference parameters, the return value comes first, followed by all reference parameters.
Reference Parameter Support
Reference parameters work with most Plugify types as shown in the "Ref Support" column of the type mapping table. The following types do not support references:
void/nil(cannot be passed by reference)function(callback types)
All other types including primitives, strings, and tables support reference parameters via the tuple return pattern.
Handling Callbacks
Plugify allows you to export functions that accept callbacks as parameters. Here’s an example:
Function Definition
Plugin Manifest
Best Practices
- Define Functions Clearly: Ensure your functions are well-documented and easy to understand.
- Follow Type Conventions: Adhere to Plugify's type conventions for parameters and return values.
- 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
methodssection.
Conclusion
Exporting functions in Lua plugins is clean and flexible. Just define your functions, register them in your manifest, and they become accessible to other modules. For more advanced scenarios, including passing callbacks, refer to the examples in this guide.