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++ TypeD TypePlugify AliasRef Support ?
voidvoidvoid
boolboolbool
charcharchar8
char16_twcharchar16
int8_tbyteint8
int16_tshortint16
int32_tintint32
int64_tlongint64
uint8_tubyteuint8
uint16_tushortuint16
uint32_tuintuint32
uint64_tulonguint64
uintptr_tsize_tptr64
uintptr_tsize_tptr32
floatfloatfloat
doubledoubledouble
void*void*function
plg::stringstringstring
plg::anyVariantany
plg::vector<bool>boolbool
plg::vector<char>charchar8
plg::vector<char16_t>wcharchar16
plg::vector<int8_t>byteint8
plg::vector<int16_t>shortint16
plg::vector<int32_t>intint32
plg::vector<int64_t>longint64
plg::vector<uint8_t>ubyteuint8
plg::vector<uint16_t>ushortuint16
plg::vector<uint32_t>uintuint32
plg::vector<uint64_t>ulonguint64
plg::vector<uintptr_t>size_tptr64
plg::vector<uintptr_t>size_tptr32
plg::vector<float>floatfloat
plg::vector<double>doubledouble
plg::vector<plg::string>stringstring
plg::vector<plg::any>Variantany
plg::vector<plg::vec2>Vector2vec2
plg::vector<plg::vec3>Vector3vec3
plg::vector<plg::vec4>Vector4vec4
plg::vector<plg::mat4x4>Matrix4x4mat4x4
plg::vec2Vector2vec2
plg::vec3Vector3vec3
plg::vec4Vector4vec4
plg::mat4x4Matrix4x4mat4x4

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.d
module example_plugin;

import plugify.export;

// Export the function using mixin Export
mixin Export!(
    "AddNumbers", // Function name
    int,          // Return type
    [int, int]    // Parameter types
);

int AddNumbers(int a, int b) {
    /**
     * Adds two integers.
     *
     * @param a: First integer.
     * @param b: Second integer.
     * @return: Sum of a and b.
     */
    return a + b;
}

Plugin Manifest

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

plugin_name.pplugin
{
  "name": "ExampleDPlugin",
  "version": "1.0.0",
  "exportedMethods": [
    {
      "name": "AddNumbers",
      "funcName": "AddNumbers_Exported",
      "paramTypes": [
        {
          "type": "int32",
          "name": "a"
        },
        {
          "type": "int32",
          "name": "b"
        }
      ],
      "retType": {
        "type": "int32"
      }
    }
  ]
}

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.d
module example_plugin;

import plugify.export;

// Export the function using mixin Export
mixin Export!(
    "ProcessData", // Function name
    string[],      // Return type
    [double[], string] // Parameter types
);

string[] ProcessData(double[] data, string prefix) {
    /**
     * Processes an array of doubles and returns an array of strings.
     *
     * @param data: Array of double values.
     * @param prefix: Prefix to add to each value.
     * @return: Array of formatted strings.
     */
    return data.map!(value => prefix ~ value.to!string).array;
}

Plugin Manifest

plugin_name.pplugin
{
  "name": "ExampleDPlugin",
  "version": "1.0.0",
  "exportedMethods": [
    {
      "name": "ProcessData",
      "funcName": "ProcessData_Exported",
      "paramTypes": [
        {
          "type": "double[]",
          "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.d
module example_plugin;

import plugify.export;

// Export the function using mixin Export
mixin Export!(
    "ExecuteWithCallback", // Function name
    void,                  // Return type
    [int, string, void delegate(int, string)] // Parameter types
);

void ExecuteWithCallback(int value, string inputStr, void delegate(int, string) callback) {
    /**
     * Executes a callback function with the provided parameters.
     *
     * @param value: Integer value.
     * @param inputStr: Input string.
     * @param callback: Callback function to execute.
     */
    string result = callback(value, inputStr);
    import std.stdio;
    writeln("Callback result: ", result);
}

Plugin Manifest

plugin_name.pplugin
{
  "name": "ExampleDPlugin",
  "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. Use mixin Export: Always use the mixin Export directive to create extern(C) functions for export.
  2. Follow Type Conventions: Adhere to Plugify's type conventions for parameters and return values.
  3. Document Your Functions: Clearly document the purpose, parameters, and return values of exported functions.
  4. Test Thoroughly: Test your exported functions to ensure they work as expected when called by other plugins.
  5. 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.