EnginePlugin
Engine plugins provide a generic way to extend a PolyglotEngine and its PolyglotContext instances using an intuitive configuration DSL:
val engine = PolyglotEngine {
// install support for specific languages
install(JavaScript) {
esm = true
intrinsics {
console()
base64()
json()
}
}
}
Plugins can subscribe to events to intercept engine and context creation, and adjust options using builders.
Writing engine plugins
Plugins have three main components:
The plugin marker, which is typically a singleton (such as a companion object), that can be used as identifier in the PluginRegistry.install method.
The configuration class, used as received for the DSL provided during installation.
The plugin instance, which is attached to the engine after configuration.
A simple plugin implementation may look like this:
// This class is used for instances of the plugin
class MyPlugin(private val config: MyConfig) {
// A custom configuration class than defines this plugin's DSL
class MyConfig {
var message: String = "Hello"
}
// a simple function to be called from the engine creation event listener
fun greetEngine() {
println(config.message)
}
// a simple function to be called from the context creation event listener
fun greetContext() {
println(config.message)
}
// The companion object is used as plugin identifier
companion object : EnginePlugin<MyConfig, MyPlugin> {
// This key will identify the plugin in the registry
override val key = Key("MyPlugin")
// This event will be called by the DSL to obtain the plugin instance that
// will be added to the plugin registry
override fun install(
scope: InstallationScope,
configuration: Config.() -> Unit
): Instance {
// we receive the DSL function, so we can instantiate the configuration
// class as needed, don't forget to apply the user configuration!
val config = MyConfig().apply(configuration)
// now we create the plugin instance that will live in the engine
val instance = MyPlugin(config)
// we can subscribe to lifecycle events using the installation scope
scope.lifecycle.on(EngineCreated) { instance.greetEngine() }
scope.lifecycle.on(ContextCreated) { instance.greetContext() }
// it is also possible to detect and install other plugins in the scope
scope.configuration.plugin(OtherPlugin)?.let {
println("OtherPlugin is installed")
}
// finally, we return the instance
return instance
}
}
}
Inheritors
Types
Encapsulates the scope provided to an EnginePlugin during the install event, allowing plugins to access the lifecycle and configuration during installation.
Plugin keys identify a plugin within the registry, allowing it to be retrieved after installation, and avoiding redundant applications. Keys are type-safe, and specify the type of the plugin instance they represent.
Properties
Functions
Install this plugin into the provided scope, using the configuration DSL block to adjust settings. This method is called from PluginRegistry.install to obtain a plugin Instance that will be added to the registry.