Skip to content

Commit

Permalink
fix @main method handling in scripts run via --class-based
Browse files Browse the repository at this point in the history
  • Loading branch information
lihaoyi committed Dec 19, 2019
1 parent 0aad8e0 commit b6ae3ff
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class Interpreter(val printer: Printer,
val createFrame: () => Frame,
initialClassLoader: ClassLoader = null,
replCodeWrapper: CodeWrapper,
scriptCodeWrapper: CodeWrapper,
val scriptCodeWrapper: CodeWrapper,
alreadyLoadedDependencies: Seq[Dependency],
importHooks: Map[Seq[String], ImportHook] = ImportHook.defaults,
classPathWhitelist: Set[Seq[String]] = Set.empty)
Expand Down
31 changes: 23 additions & 8 deletions amm/src/main/scala/ammonite/main/Scripts.scala
Original file line number Diff line number Diff line change
Expand Up @@ -74,20 +74,35 @@ object Scripts {
.evalClassloader
.loadClass(processed.blockInfo.last.id.wrapperPath + "$")

routesCls =
interp
.evalClassloader
.loadClass(routeClsName + "$$routes$")

scriptMains =
routesCls
scriptMains = interp.scriptCodeWrapper match{
case ammonite.interp.CodeWrapper =>
interp
.evalClassloader
.loadClass(routeClsName + "$$routes$")
.getField("MODULE$")
.get(null)
.asInstanceOf[() => Seq[Router.EntryPoint[Any]]]
.apply()

case ammonite.interp.CodeClassWrapper =>
val outer = interp
.evalClassloader
.loadClass(routeClsName)
.getMethod("instance")
.invoke(null)

outer.getClass.getMethod("$routes").invoke(outer)
.asInstanceOf[() => Seq[Router.EntryPoint[Any]]]
.apply()
case _ => Nil
}

mainObj = mainCls.getField("MODULE$").get(null)
mainObj = interp.scriptCodeWrapper match {
case ammonite.interp.CodeWrapper => mainCls.getField("MODULE$").get(null)
case ammonite.interp.CodeClassWrapper =>
mainCls.getMethod("instance").invoke(mainCls.getField("MODULE$").get(null))
case _ => ()
}

res <- Util.withContextClassloader(interp.evalClassloader){
scriptMains match {
Expand Down
4 changes: 4 additions & 0 deletions readme/Footer.scalatex
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@


@sect{Changelog}
@sect{1.9.1}
@ul
@li
Fix @code{@@main} method handling in scripts run via @code{--class-based}
@sect{1.9.0}
@ul
@li
Expand Down

0 comments on commit b6ae3ff

Please sign in to comment.