Export Functions
Guide to export functions from your plugin to be used by other language modules within Plugify.
In the Plugify ecosystem, Go plugins can export functions to make them accessible to other plugins. This guide explains how to define and export functions in Go 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++ Type | Go Type | Plugify Alias | Ref Support ? |
---|---|---|---|
void | void (not used in Go) | void | ❌ |
bool | bool | bool | ✅ |
char | byte | char8 | ✅ |
char16_t | rune | char16 | ✅ |
int8_t | int8 | int8 | ✅ |
int16_t | int16 | int16 | ✅ |
int32_t | int32 | int32 | ✅ |
int64_t | int64 | int64 | ✅ |
uint8_t | uint8 | uint8 | ✅ |
uint16_t | uint16 | uint16 | ✅ |
uint32_t | uint32 | uint32 | ✅ |
uint64_t | uint64 | uint64 | ✅ |
uintptr_t | uintptr | ptr64 | ✅ |
uintptr_t | uintptr | ptr32 | ✅ |
float | float32 | float | ✅ |
double | float64 | double | ✅ |
void* | unsafe.Pointer | function | ❌ |
plg::string | string | string | ✅ |
plg::any | any | any | ✅ |
plg::vector<bool> | bool | bool | ✅ |
plg::vector<char> | byte | char8 | ✅ |
plg::vector<char16_t> | rune | char16 | ✅ |
plg::vector<int8_t> | int8 | int8 | ✅ |
plg::vector<int16_t> | int16 | int16 | ✅ |
plg::vector<int32_t> | int32 | int32 | ✅ |
plg::vector<int64_t> | int64 | int64 | ✅ |
plg::vector<uint8_t> | uint8 | uint8 | ✅ |
plg::vector<uint16_t> | uint16 | uint16 | ✅ |
plg::vector<uint32_t> | uint32 | uint32 | ✅ |
plg::vector<uint64_t> | uint64 | uint64 | ✅ |
plg::vector<uintptr_t> | uintptr | ptr64 | ✅ |
plg::vector<uintptr_t> | uintptr | ptr32 | ✅ |
plg::vector<float> | float32 | float | ✅ |
plg::vector<double> | float64 | double | ✅ |
plg::vector<plg::string> | string | string | ✅ |
plg::vector<plg::any> | any | 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 Go
Exporting functions in Go requires marking the functions for export using the //export
directive. These functions can then be called by other plugins. Plugify's Go Language Module handles the rest.
Using Generator to Simplify Function Export
The generator.go
tool simplifies the process of exporting Go functions by:
- Scanning the Plugin Folder: It scans your plugin's root folder to identify functions to export.
- Reading the Manifest: It reads the
.pplugin
manifest file to understand the function signatures and types. - Generating Files: It generates
autoexport.go
andautoexport.h
files with the necessary code to export functions.
This tool eliminates the need for manual marshalling, making it easier for developers to integrate their Go plugins into the Plugify ecosystem.
Basic Example
Here’s a simple example of exporting a function in a Go plugin:
Function Definition
Plugin Manifest
To export the function, describe it in the plugin manifest under the exportedMethods
section:
Generated Code
Run the generator.go
tool to generate the autoexport.go
and autoexport.h
files. These files will handle the marshalling of Plugify types to Go types.
This generated code handles the conversion of Plugify types to Go types and ensures the function can be called from other plugins.
Advanced Example: Exporting Complex Functions
Here’s an example of exporting a function with complex parameter and return types:
Function Definition
Plugin Manifest
Generated Code
Run the generator.go
tool to generate the autoexport.go
and autoexport.h
files. These files will handle the marshalling of Plugify types to Go types.
This generated code handles the conversion of Plugify types to Go types and ensures the function can be called from other plugins.
Handling Callbacks
Plugify allows you to export functions that accept callbacks as parameters. Here’s an example:
Function Definition
Plugin Manifest
Generated Code
Run the generator.go
tool to generate the autoexport.go
and autoexport.h
files. These files will handle the marshalling of Plugify types to Go types.
This generated code handles the conversion of Plugify types to Go types and ensures the function can be called from other plugins.
Best Practices
- Use
//export
: Always use the//export
directive to mark 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 Go plugins is straightforward when you follow Plugify's conventions and best practices. By using the //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.