Skip to content

Commit

Permalink
Enable 2-stage runtime parameter processing
Browse files Browse the repository at this point in the history
Previously we were performing special-case parameter processing (e.g.
expanding %'s in jvmMaxHeap specifications) at the time of initial
runtime configuration. However, this would happen a) before variable
interpolation and b) only if the user hadn't manually specified a value.
As a result, %'s would only work in a very limited number of cases (when
set in the .toml)

Instead, we now do a second "processArgs" pass that happens after
variable interpolation. It is intended to operate on all runtime args at
that point, whether they came from a toml configuration, user, etc...

This allows the particular case of % calculation in jvmMaxHeap to
trigger in all cases.

Closes #64

Note that because we process every item in the arg list we are working
around #63, without actually
fixing it.
  • Loading branch information
hinerm committed Dec 11, 2024
1 parent c6daa49 commit b8a8bca
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 6 deletions.
25 changes: 19 additions & 6 deletions src/commonMain/kotlin/jvm.kt
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,11 @@ class JvmRuntimeConfig(recognizedArgs: Array<String>) :
}
}

// 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.
Expand Down Expand Up @@ -165,6 +162,22 @@ class JvmRuntimeConfig(recognizedArgs: Array<String>) :
}
}

override fun processArgs(args: MutableList<String>) {
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? {
Expand Down
9 changes: 9 additions & 0 deletions src/commonMain/kotlin/main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,15 @@ fun main(args: Array<String>) {
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)
Expand Down
4 changes: 4 additions & 0 deletions src/commonMain/kotlin/python.kt
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ class PythonRuntimeConfig(recognizedArgs: Array<String>) :
}
}

override fun processArgs(args: MutableList<String>) {
// No-op
}

// -- Directive handlers --

fun dryRun(args: ProgramArgs): String {
Expand Down
5 changes: 5 additions & 0 deletions src/commonMain/kotlin/runtime.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ abstract class RuntimeConfig(
/** Get the launch directive block for this runtime configuration. */
abstract fun launch(args: ProgramArgs): List<String>

/**
* Perform any runtime-specific argument processing here.
*/
abstract fun processArgs(args: MutableList<String>)

/**
* Check whether the given argument matches one of the [recognizedArgs].
*
Expand Down

0 comments on commit b8a8bca

Please sign in to comment.