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++ TypeLua TypePlugify AliasRef Support ?
voidnilvoid
boolbooleanbool
charstringchar8
char16_tstringchar16
int8_tintegerint8
int16_tintegerint16
int32_tintegerint32
int64_tintegerint64
uint8_tintegeruint8
uint16_tintegeruint16
uint32_tintegeruint32
uint64_tintegeruint64
uintptr_tintegerptr64
uintptr_tintegerptr32
floatnumberfloat
doublenumberdouble
void*functionfunction
plg::stringstringstring
plg::anyanyany
plg::vector<bool>tablebool[]
plg::vector<char>tablechar8[]
plg::vector<char16_t>tablechar16[]
plg::vector<int8_t>tableint8[]
plg::vector<int16_t>tableint16[]
plg::vector<int32_t>tableint32[]
plg::vector<int64_t>tableint64[]
plg::vector<uint8_t>tableuint8[]
plg::vector<uint16_t>tableuint16[]
plg::vector<uint32_t>tableuint32[]
plg::vector<uint64_t>tableuint64[]
plg::vector<uintptr_t>tableptr64[]
plg::vector<uintptr_t>tableptr32[]
plg::vector<float>tablefloat[]
plg::vector<double>tabledouble[]
plg::vector<plg::string>tablestring[]
plg::vector<plg::any>tableany[]
plg::vector<plg::vec2>tablevec2[]
plg::vector<plg::vec3>tablevec3[]
plg::vector<plg::vec4>tablevec4[]
plg::vector<plg::mat4x4>tablemat4x4[]
plg::vec2Vector2vec2
plg::vec3Vector3vec3
plg::vec4Vector4vec4
plg::mat4x4Matrix4x4mat4x4

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.lua
function addNumbers_exported(a, b)
    -- Adds two integers
    return a + b
end

Plugin Manifest

To export the function, describe it in the plugin manifest under the exportedMethods section:

plugin_name.pplugin
{
  "name": "ExampleLuaPlugin",
  "version": "1.0.0",
  "exportedMethods": [
    {
      "name": "addNumbers",
      "funcName": "addNumbers_exported",
      "paramTypes": [
        {
          "type": "int32",
          "name": "a"
        },
        {
          "type": "int32",
          "name": "b"
        }
      ],
      "retType": {
        "type": "int32"
     }
   }
 ]
}

Advanced Example: Exporting Complex Functions

Here’s an example of exporting a function with complex parameter and return types:

Function Definition

plugin.lua
function processData_exported(data, prefix)
    local result = {}
    for i = 1, #data do
        result[i] = prefix .. tostring(data[i])
    end
    return result
end

Plugin Manifest

plugin_name.pplugin
{
  "name": "ExampleLuaPlugin",
  "version": "1.0.0",
  "exportedMethods": [
    {
      "name": "processData",
      "funcName": "processData_exported",
      "paramTypes": [
        {
          "type": "float[]",
          "name": "data"
        },
        {
          "type": "string",
          "name": "prefix"
        }
      ],
      "retType": {
        "type": "string[]"
      }
    }
  ]
}

Handling Callbacks

Plugify allows you to export functions that accept callbacks as parameters. Here’s an example:

Function Definition

plugin.lua
function executeWithCallback_exported(value, inputStr, callback)
    local result = callback(value, inputStr)
    print("Callback result:", result)
end

Plugin Manifest

plugin_name.pplugin
{
  "name": "ExampleLuaPlugin",
  "version": "1.0.0",
  "exportedMethods": [
    {
      "name": "executeWithCallback",
      "funcName": "executeWithCallback_exported",
      "paramTypes": [
        {
          "type": "int32",
          "name": "value"
        },
        {
          "type": "string",
          "name": "inputStr"
        },
        {
          "type": "function",
          "name": "callback",
          "prototype": {
            "name": "exampleCallback",
            "funcName": "exampleCallback_exported",
            "paramTypes": [
              {
                "type": "int32",
                "name": "value"
              },
              {
                "type": "string",
                "name": "inputStr"
              }
            ],
            "retType": {
              "type": "string"
            }
          }
        }
      ],
      "retType": {
        "type": "void"
      }
    }
  ]
}

Best Practices

  1. Define Functions Clearly: Ensure your functions are well-documented and easy to understand.
  2. Follow Type Conventions: Adhere to Plugify's type conventions for parameters and return values.
  3. Test Thoroughly: Test your exported functions to ensure they work as expected when called by other plugins.
  4. Update the Manifest: Always describe exported functions in the plugin manifest under the exportedMethods section.

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.