这个答案的 Scala 2 版本在这里:如何在脚本运行时运行生成的代码? https://stackoverflow.com/questions/73911801/how-can-i-run-generated-code-during-script-runtime
在 Scala 3 中:
- 例如你可以使用Li Haoyi https://stackoverflow.com/users/871202/li-haoyi's Ammonite http://ammonite.io/
ammonite.Main(verboseOutput = false).runCode("""println("Hello, World!")""")
// Hello, World!
构建.sbt
scalaVersion := "3.1.3"
libraryDependencies += "com.lihaoyi" % "ammonite" % "2.5.4-22-4a9e6989" cross CrossVersion.full
excludeDependencies ++= Seq(
ExclusionRule("com.lihaoyi", "sourcecode_2.13"),
ExclusionRule("com.lihaoyi", "fansi_2.13"),
)
- 或者你可以尝试尤金·横田 https://stackoverflow.com/users/3827/eugene-yokota's Eval https://eed3si9n.com/eval/
com.eed3si9n.eval.Eval()
.evalInfer("""println("Hello, World!")""")
.getValue(this.getClass.getClassLoader)
// Hello, World!
构建.sbt
scalaVersion := "3.2.0"
libraryDependencies += "com.eed3si9n.eval" % "eval" % "0.1.0" cross CrossVersion.full
- 或者你可以试试我的执行 https://github.com/DmytroMitin/dotty-patched
com.github.dmytromitin.eval.Eval[Unit]("""println("Hello, World!")""")
// Hello, World!
scalaVersion := "3.2.1"
libraryDependencies += "com.github.dmytromitin" %% "eval" % "0.1"
- 您也可以使用标准 Scala 3 REPL口译员 https://javadoc.io/static/org.scala-lang/scala3-compiler_3/3.2.0/dotty/tools/repl/ScriptEngine.html
dotty.tools.repl.ScriptEngine().eval("""println("Hello, World!")""")
// Hello, World!
构建.sbt
scalaVersion := "3.1.3"
libraryDependencies += scalaOrganization.value %% "scala3-compiler" % scalaVersion.value
- 如果你有一个
scala.quoted.Expr
'{...}
(抽象语法树上的静态类型包装器scala.quoted.Quotes#Tree
)而不是纯字符串那么你可以使用运行时多阶段 https://docs.scala-lang.org/scala3/reference/metaprogramming/staging.html
import scala.quoted.*
given staging.Compiler = staging.Compiler.make(getClass.getClassLoader)
staging.run('{ println("Hello, World!") })
// Hello, World!
构建.sbt
scalaVersion := "3.1.3"
libraryDependencies += scalaOrganization.value %% "scala3-staging" % scalaVersion.value
- 以上都是在Scala 3中运行Scala 3代码。如果我们想在Scala 3中运行Scala 2代码那么我们仍然可以使用Scala 2反射Toolbox https://docs.scala-lang.org/overviews/reflection/symbols-trees-types.html#tree-creation-via-parse-on-toolboxes。 Scala 2 宏不起作用,所以我们不能这样做
runtime.currentMirror
or q"..."
但可以做universe.runtimeMirror
or tb.parse
import scala.tools.reflect.ToolBox // implicit
val tb = scala.reflect.runtime.universe
.runtimeMirror(getClass.getClassLoader)
.mkToolBox()
tb.eval(tb.parse("""println("Hello, World!")"""))
// Hello, World!
构建.sbt
scalaVersion := "3.1.3"
libraryDependencies ++= scalaOrganization.value % "scala-compiler" % "2.13.8"
- 另外,要在 Scala 3 中运行 Scala 2 代码,您可以使用标准 Scala 2 REPL口译员 https://www.scala-lang.org/api/2.13.9/scala-compiler/scala/tools/nsc/interpreter/shell/Scripted.html
scala.tools.nsc.interpreter.shell.Scripted()
.eval("""System.out.println("Hello, World!")""")
// Hello, World!
构建.sbt
scalaVersion := "3.1.3"
libraryDependencies ++= scalaOrganization.value % "scala-compiler" % "2.13.8"
- 您也可以使用JSR223 https://jcp.org/aboutJava/communityprocess/final/jsr223/index.html 脚本编写 https://github.com/lampepfl/dotty/blob/master/tests/run-with-compiler/scripting.scala。取决于你是否有
scala3-compiler
or scala-compiler
在类路径中,您将运行 Scala 3 或 Scala 2(上述两个脚本引擎之一:Scala 3dotty.tools.repl.ScriptEngine
或斯卡拉2scala.tools.nsc.interpreter.shell.Scripted
)。如果您首先添加了两个依赖项,则获胜。
new javax.script.ScriptEngineManager(getClass.getClassLoader)
.getEngineByName("scala")
.eval("""println("Hello, World!")""")
// Hello, World!
如果您想更好地控制使用什么依赖项(无需重新导入项目),您可以使用 Coursier 并指定类加载器
import coursier.* // libraryDependencies += "io.get-coursier" %% "coursier" % "2.1.0-M6-53-gb4f448130" cross CrossVersion.for3Use2_13
val files = Fetch()
.addDependencies(
Dependency(Module(Organization("org.scala-lang"), ModuleName("scala3-compiler_3")), "3.2.0"),
// Dependency(Module(Organization("org.scala-lang"), ModuleName("scala-compiler")), "2.13.9")
)
.run()
val classLoader = new java.net.URLClassLoader(
files.map(_.toURI.toURL).toArray,
/*getClass.getClassLoader*/null // ignoring current classpath
)
new javax.script.ScriptEngineManager(classLoader)
.getEngineByName("scala")
.eval("""
type T = [A] =>> [B] =>> (A, B) // Scala 3
//type T = List[Option[A]] forSome {type A} // Scala 2
System.out.println("Hello, World!")
""")
// Hello, World!
- 您可以使用实际的编译器自己在 Scala 3 中实现 Eval
import dotty.tools.io.AbstractFile
import dotty.tools.dotc.core.Contexts.Context
import dotty.tools.dotc.Driver
import dotty.tools.dotc.util.SourceFile
import dotty.tools.io.{VirtualDirectory, VirtualFile}
import java.net.URLClassLoader
import java.nio.charset.StandardCharsets
import dotty.tools.repl.AbstractFileClassLoader
import scala.io.Codec
import coursier.{Dependency, Module, Organization, ModuleName, Fetch}
// we apply usejavacp=true instead
// val files = Fetch()
// .addDependencies(
// Dependency(Module(Organization("org.scala-lang"), ModuleName("scala3-compiler_3")), "3.1.3"),
// )
// .run()
//
// val depClassLoader = new URLClassLoader(
// files.map(_.toURI.toURL).toArray,
// /*getClass.getClassLoader*/ null // ignoring current classpath
// )
val code =
s"""
|package mypackage
|
|object Main {
| def main(args: Array[String]): Unit = {
| println("Hello, World!")
| }
|}""".stripMargin
val outputDirectory = VirtualDirectory("(memory)")
compileCode(code, List()/*files.map(f => AbstractFile.getFile(f.toURI.toURL.getPath)).toList*/, outputDirectory)
val classLoader = AbstractFileClassLoader(outputDirectory, this.getClass.getClassLoader/*depClassLoader*/)
runObjectMethod("mypackage.Main", classLoader, "main", Seq(classOf[Array[String]]), Array.empty[String])
// Hello, World!
def compileCode(
code: String,
classpathDirectories: List[AbstractFile],
outputDirectory: AbstractFile
): Unit = {
class DriverImpl extends Driver {
private val compileCtx0 = initCtx.fresh
given Context = compileCtx0.fresh
.setSetting(
compileCtx0.settings.classpath,
classpathDirectories.map(_.path).mkString(":")
).setSetting(
compileCtx0.settings.usejavacp,
true
).setSetting(
compileCtx0.settings.outputDir,
outputDirectory
)
val compiler = newCompiler
}
val driver = new DriverImpl
import driver.given Context
val sourceFile = SourceFile(VirtualFile("(inline)", code.getBytes(StandardCharsets.UTF_8)), Codec.UTF8)
val run = driver.compiler.newRun
run.compileSources(List(sourceFile))
// val unit = run.units.head
// println("untyped tree=" + unit.untpdTree)
// println("typed tree=" + unit.tpdTree)
}
def runObjectMethod(
objectName: String,
classLoader: ClassLoader,
methodName: String,
paramClasses: Seq[Class[?]],
arguments: Any*
): Any = {
val clazz = Class.forName(s"$objectName$$", true, classLoader)
val module = clazz.getField("MODULE$").get(null)
val method = module.getClass.getMethod(methodName, paramClasses*)
method.invoke(module, arguments*)
}
(以前的版本 https://gist.github.com/DmytroMitin/2e02055bc061e5b7c9421ba76e384013)
构建.sbt
scalaVersion := "3.1.3"
libraryDependencies += scalaOrganization.value %% "scala3-compiler" % scalaVersion.value
也可以看看:从 scala 3 宏中的类获取注释 https://stackoverflow.com/questions/71390113/get-annotations-from-class-in-scala-3-macros(在 Scala 3 中破解多阶段编程并实现我们自己的eval
而不是 Scala 2context.eval
or staging.run
Scala 3 宏中禁止)。
Scala 演示编译器简介 https://www.chris-kipp.io/blog/an-intro-to-the-scala-presentation-compiler
在运行时将 scala 3 代码从字符串解析为 Scala 3 AST https://stackoverflow.com/questions/68194436/parsing-scala-3-code-from-a-string-into-scala-3-ast-at-runtime
Scala 3 反射 https://www.reddit.com/r/scala/comments/siatmj/scala_3_reflection/
在运行时帮助 dotty 编译器和类加载 https://github.com/lampepfl/dotty/discussions/16080