diff --git a/src/commonMain/kotlin/jvm.kt b/src/commonMain/kotlin/jvm.kt index 8283895..0357b51 100644 --- a/src/commonMain/kotlin/jvm.kt +++ b/src/commonMain/kotlin/jvm.kt @@ -118,14 +118,11 @@ class JvmRuntimeConfig(recognizedArgs: Array) : } } - // If not already declared, calculate and declare the max heap size. + // If not manually declared, add a max heap flag from the config. val mxIndex = runtimeArgs.indexOfFirst { it.startsWith("-Xmx") } if (mxIndex < 0) { - val maxHeap = calculateMaxHeap(config.jvmMaxHeap) - if (maxHeap != null) { - runtimeArgs += "-Xmx$maxHeap" - debug("Added maxHeap arg: ${runtimeArgs.last()}") - } + runtimeArgs += "-Xmx${config.jvmMaxHeap}" + debug("Added maxHeap arg: ${runtimeArgs.last()}") } // Calculate main class. @@ -165,6 +162,22 @@ class JvmRuntimeConfig(recognizedArgs: Array) : } } + override fun processArgs(args: MutableList) { + val prefix = "-Xmx" + val memIndices = args.withIndex() + .filter{ (_, value) -> value.startsWith("-Xmx" ) } + .map{it.index} + + for (memIdx in memIndices) { + if (args[memIdx].contains('%')) { + val memPercent = args[memIdx].substring(prefix.length) + val maxHeap = calculateMaxHeap(memPercent) + args[memIdx] = "$prefix$maxHeap"; + debug("Expanded % in jvm runtime arg: ${args[memIdx]}") + } + } + } + // -- Directive handlers -- fun classpath(divider: String = "\n- "): String? { diff --git a/src/commonMain/kotlin/main.kt b/src/commonMain/kotlin/main.kt index eb0116b..bba5c70 100644 --- a/src/commonMain/kotlin/main.kt +++ b/src/commonMain/kotlin/main.kt @@ -108,6 +108,15 @@ fun main(args: Array) { vars.interpolateInto(programArgs.main) } + // Now that our program arguments have been fully interpolated, we offer the + // runtimes an additional opportunity to perform any runtime-specific + // custom logic (for example, resolving %'s in -Xmx notation). + for (programArgs in argsInContext.values) { + for (runtime in runtimes) { + runtime.processArgs(programArgs.runtime) + } + } + // Finally, execute all the directives! \^_^/ executeDirectives(nonGlobalDirectives, launchDirectives, runtimes, userArgs, argsInContext) diff --git a/src/commonMain/kotlin/python.kt b/src/commonMain/kotlin/python.kt index e14bc53..aae3e99 100644 --- a/src/commonMain/kotlin/python.kt +++ b/src/commonMain/kotlin/python.kt @@ -119,6 +119,10 @@ class PythonRuntimeConfig(recognizedArgs: Array) : } } + override fun processArgs(args: MutableList) { + // No-op + } + // -- Directive handlers -- fun dryRun(args: ProgramArgs): String { diff --git a/src/commonMain/kotlin/runtime.kt b/src/commonMain/kotlin/runtime.kt index b238bc6..1adea28 100644 --- a/src/commonMain/kotlin/runtime.kt +++ b/src/commonMain/kotlin/runtime.kt @@ -43,6 +43,11 @@ abstract class RuntimeConfig( /** Get the launch directive block for this runtime configuration. */ abstract fun launch(args: ProgramArgs): List + /** + * Perform any runtime-specific argument processing here. + */ + abstract fun processArgs(args: MutableList) + /** * Check whether the given argument matches one of the [recognizedArgs]. *