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 //plugify: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 find functions with
//plugify:exportattribute. - Creating the Manifest: It creates the
.ppluginmanifest file to export the function signatures to other plugins. - Generating Files: It generates
autoexport.goandautoexport.hfiles 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 methods 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.
Exporting Functions with References
Plugify supports reference parameters (also known as "out" or "inout" parameters) that allow functions to modify values and return them to the caller. In Go, reference parameters are implemented using pointers.
Function Definition with Reference Parameters
Plugin Manifest with Reference Parameters
In the manifest, mark parameters that are passed by reference using "ref": true:
Generated Code for Reference Parameters
The generator will create wrapper functions that handle pointer marshalling:
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(cannot be passed by reference)function(callback/delegate types)
All other types including primitives, strings, slices, and structs support reference parameters via pointers.
Handling Callbacks
Plugify allows you to export functions that accept callbacks as parameters. Here’s an example:
Function Definition
Plugin Manifest
Automating Manifest and Export Generation
New: You can now automate the generation of both the plugin manifest and export code using the Go generator from go-plugify!
Instead of manually writing the manifest JSON and export code, you can mark your functions with //plugify:export FuncName comments. The generator will parse your entire Go project and automatically generate:
- Plugin Manifest (
.ppluginfile) with all exported methods - Autoexport Files (
autoexport.goandautoexport.h) with marshalling code
Benefits of Automated Generation
- No Manual JSON Writing: Function signatures are automatically extracted from your code
- Complete Type Information: Parameter types and return types are automatically mapped
- Automatic Marshalling: Export wrappers with proper type conversion are generated
- Reduced Errors: Eliminates typos and type mismatches between code and manifest
- Easy Maintenance: Changes to function signatures are automatically reflected
Setup: Creating Your Generator
Since the generator is part of the go-plugify package but cannot be run directly as a dependency, you need to create your own generator.go file that calls the plugify.Generate() function.
Create a generator.go file in your project root:
Using the //plugify:export Comment
Mark your exported functions with the //plugify:export FuncName comment directive:
Running the Generator
Run the generator using the go generate command:
Or run it manually:
How It Works
- During Generation: The Go parser analyzes your entire project looking for
//plugify:exportcomments - Type Analysis: It extracts function signatures and maps Go types to Plugify types (e.g.,
int32→int32,[]string→string[]) - Manifest Generation: A
.ppluginfile is created with all exported methods - Export Code Generation:
autoexport.goandautoexport.hfiles are generated with proper marshalling code
Best Practices
- Use
//plugify:export: Mark functions for automated generation with the//plugify:export FuncNamecomment directive. - Create a Generator File: Set up your own
generator.gofile that callsplugify.Generate()for automated manifest and export generation. - Follow Type Conventions: Adhere to Plugify's type conventions for parameters and return values.
- Document Your Functions: Use Go doc comments to clearly document the purpose, parameters, and return values of exported functions.
- Use
go generate: Rungo generateto automatically regenerate manifest and export files after making changes. - Test Thoroughly: Test your exported functions to ensure they work as expected when called by other plugins.
- Keep Generated Files in Sync: Regenerate after any changes to function signatures to maintain consistency.
Conclusion
Exporting functions in Go plugins is streamlined with the new automated generator from go-plugify. By marking your functions with //plugify:export comments and running go generate, you can automatically generate both the plugin manifest and export code with proper type marshalling. This eliminates manual JSON writing and reduces errors, making it easier to create robust and interoperable plugins. The generator analyzes your entire Go project using the Go parser, extracting function signatures and generating all necessary files automatically. For more advanced use cases, such as handling callbacks, the generator handles the complex marshalling code for you.