Skip to content

Commit

Permalink
feat(kotlin): use guest classpath from embedded resources.
Browse files Browse the repository at this point in the history
Signed-off-by: Dario Valdespino <[email protected]>
  • Loading branch information
darvld committed Nov 20, 2023
1 parent d9c51e7 commit d4eafcc
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ import elide.runtime.plugins.AbstractLanguageConfig
classpathEntries.addAll(paths)
}

/** Add new entries to the guest classpath used by the engine. */
public fun classpath(paths: Iterable<String>) {
classpathEntries.addAll(paths)
}

/** Apply init-time settings to a new [context]. */
internal fun applyTo(context: PolyglotContext) {
// register intrinsics
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,50 +14,41 @@
package elide.runtime.plugins.kotlin

import elide.runtime.core.DelicateElideApi
import elide.runtime.core.EngineLifecycleEvent.ContextCreated
import elide.runtime.core.EnginePlugin.InstallationScope
import elide.runtime.core.EnginePlugin.Key
import elide.runtime.core.PolyglotContext
import elide.runtime.core.PolyglotContextBuilder
import elide.runtime.core.getOrInstall
import elide.runtime.plugins.AbstractLanguagePlugin
import elide.runtime.plugins.AbstractLanguagePlugin.LanguagePluginManifest
import elide.runtime.plugins.jvm.Jvm

@Suppress("unused") @DelicateElideApi public class Kotlin private constructor(
private val config: KotlinConfig,
private val resources: LanguagePluginManifest,
) {
private fun initializeContext(context: PolyglotContext) {
// apply init-time settings
config.applyTo(context)

// run embedded initialization code
initializeEmbeddedScripts(context, resources)
}

private fun configureContext(builder: PolyglotContextBuilder) {
// nothing to do here
}

@DelicateElideApi public class Kotlin private constructor(public val config: KotlinConfig) {
public companion object Plugin : AbstractLanguagePlugin<KotlinConfig, Kotlin>() {
private const val GUEST_CLASSPATH_KEY = "classpath"

private const val KOTLIN_LANGUAGE_ID = "kt"
private const val KOTLIN_PLUGIN_ID = "Kotlin"

override val languageId: String = KOTLIN_LANGUAGE_ID
override val key: Key<Kotlin> = Key(KOTLIN_PLUGIN_ID)


private fun resolveGuestClasspathEntries(manifest: LanguagePluginManifest): List<String> {
return manifest.resources[GUEST_CLASSPATH_KEY]?.map {
Kotlin::class.java.getResource("${manifest.root}/$it")?.toString() ?: error(
"Failed to resolve embedded classpath element: $it",
)
} ?: emptyList()
}

override fun install(scope: InstallationScope, configuration: KotlinConfig.() -> Unit): Kotlin {
// apply the JVM plugin first
scope.configuration.getOrInstall(Jvm)

// apply the configuration and create the plugin instance
val config = KotlinConfig().apply(configuration)
val resources = resolveEmbeddedManifest(scope)
val instance = Kotlin(config, resources)

// subscribe to lifecycle events
scope.lifecycle.on(ContextCreated, instance::configureContext)
// apply the JVM plugin first, and register the custom classpath entries
scope.configuration.getOrInstall(Jvm).config.apply {
classpath(resolveGuestClasspathEntries(resources))
}

// apply the configuration and create the plugin instance
val config = KotlinConfig().apply(configuration)
val instance = Kotlin(config)

// register resources with the VFS
installEmbeddedBundles(scope, resources)
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
{
"engine": "v4",
"language": "Kotlin"
"language": "Kotlin",
"resources": {
"classpath": [
"classpath/elide-kotlin-runtime.jar",
"classpath/kotlin-script-runtime-1.9.20-Beta2.jar",
"classpath/kotlin-stdlib-1.9.20-Beta2.jar"
]
}
}
Binary file not shown.

0 comments on commit d4eafcc

Please sign in to comment.