Export Functions

Guide to export functions from your plugin to be used by other language modules within Plugify.

In the Plugify ecosystem, JavaScript plugins can export functions to make them accessible to other plugins. This guide explains how to define and export functions in JavaScript and provides examples to help you integrate your plugins seamlessly.

Basic Type Mapping

The following table lists how types are exposed to the JavaScript API:

C++ TypeJavaScript TypePlugify AliasRef Support ?
voidundefinedvoid
boolbooleanbool
charstringchar8
char16_tstringchar16
int8_tnumberint8
int16_tnumberint16
int32_tnumberint32
int64_tBigIntint64
uint8_tnumberuint8
uint16_tnumberuint16
uint32_tnumberuint32
uint64_tBigIntuint64
uintptr_tnumberptr64
uintptr_tnumberptr32
floatnumberfloat
doublenumberdouble
void*Functionfunction
plg::stringstringstring
plg::anyanyany
plg::vector<bool>booleanbool
plg::vector<char>stringchar8
plg::vector<char16_t>stringchar16
plg::vector<int8_t>numberint8
plg::vector<int16_t>numberint16
plg::vector<int32_t>numberint32
plg::vector<int64_t>BigIntint64
plg::vector<uint8_t>numberuint8
plg::vector<uint16_t>numberuint16
plg::vector<uint32_t>numberuint32
plg::vector<uint64_t>BigIntuint64
plg::vector<uintptr_t>numberptr64
plg::vector<uintptr_t>numberptr32
plg::vector<float>numberfloat
plg::vector<double>numberdouble
plg::vector<plg::string>stringstring
plg::vector<plg::any>anyany
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::mat4x4Mattor4x4mat4x4

Exporting Functions in JavaScript

Exporting functions in JavaScript is straightforward because JavaScript is a dynamically-typed language. You only need to define the function and specify it in the plugin manifest. Plugify's JavaScript Language Module handles the rest.

Basic Example

Here’s a simple example of exporting a function in a JavaScript plugin:

Function Definition

plugin.mjs
export function addNumbers_exported(a, b) {
    /**
     * Adds two integers.
     *
     * @param {number} a - First integer.
     * @param {number} b - Second integer.
     * @returns {number} 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": "ExampleJavaScriptPlugin",
  "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 JavaScript:

1. Primitive Types

  • Parameter: Pass primitive types (e.g., number, boolean) directly.
  • Return: Return primitive types directly.

2. Strings

  • Parameter: Pass strings as string.
  • Return: Return strings as string.

3. Arrays

  • Parameter: Pass arrays as Array.
  • Return: Return arrays as Array.

4. Objects

  • Parameter: Pass objects as Object.
  • Return: Return objects as Object.

Advanced Example: Exporting Complex Functions

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

Function Definition

plugin.mjs
export function processData_exported(data, prefix) {
    /**
     * Processes an array of numbers and returns an array of strings.
     *
     * @param {Array<number>} data - Array of numbers.
     * @param {string} prefix - Prefix to add to each value.
     * @returns {Array<string>} Array of formatted strings.
     */
    return data.map(value => `${prefix}${value}`);
}

Plugin Manifest

plugin_name.pplugin
{
  "name": "ExampleJavaScriptPlugin",
  "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.mjs
export function executeWithCallback_exported(value, inputStr, callback) {
    /**
     * Executes a callback function with the provided parameters.
     *
     * @param {number} value - Integer value.
     * @param {string} inputStr - Input string.
     * @param {Function} callback - Callback function to execute.
     */
    const result = callback(value, inputStr);
    console.log(`Callback result: ${result}`);
}

Plugin Manifest

plugin_name.pplugin
{
  "name": "ExampleJavaScriptPlugin",
  "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 JavaScript plugins is simple and straightforward. By defining your functions and describing them 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.