Skip to content

Commit b39e3e8

Browse files
committed
Add :javap to the REPL
Provides bytecode disassembly using the `javap` tool/interface supplied by the user's JDK. Adapted from the Scala 2 implementation, which was written by Paul Phillips and Som Snytt / A. P. Marki
1 parent 8587414 commit b39e3e8

File tree

8 files changed

+487
-0
lines changed

8 files changed

+487
-0
lines changed

compiler/src/dotty/tools/dotc/config/PathResolver.scala

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,47 @@ object PathResolver {
129129
)
130130
}
131131

132+
/** Locations discovered by supplemental heuristics.
133+
*/
134+
object SupplementalLocations {
135+
136+
/** The platform-specific support jar.
137+
*
138+
* Usually this is `tools.jar` in the jdk/lib directory of the platform distribution.
139+
*
140+
* The file location is determined by probing the lib directory under JDK_HOME or JAVA_HOME,
141+
* if one of those environment variables is set, then the lib directory under java.home,
142+
* and finally the lib directory under the parent of java.home. Or, as a last resort,
143+
* search deeply under those locations (except for the parent of java.home, on the notion
144+
* that if this is not a canonical installation, then that search would have little
145+
* chance of succeeding).
146+
*/
147+
def platformTools: Option[File] = {
148+
val jarName = "tools.jar"
149+
def jarPath(path: Path) = (path / "lib" / jarName).toFile
150+
def jarAt(path: Path) = {
151+
val f = jarPath(path)
152+
if (f.isFile) Some(f) else None
153+
}
154+
val jdkDir = {
155+
val d = Directory(jdkHome)
156+
if (d.isDirectory) Some(d) else None
157+
}
158+
def deeply(dir: Directory) = dir.deepFiles find (_.name == jarName)
159+
160+
val home = envOrSome("JDK_HOME", envOrNone("JAVA_HOME")) map (p => Path(p))
161+
val install = Some(Path(javaHome))
162+
163+
(home flatMap jarAt) orElse (install flatMap jarAt) orElse (install map (_.parent) flatMap jarAt) orElse
164+
(jdkDir flatMap deeply)
165+
}
166+
167+
override def toString = s"""
168+
|object SupplementalLocations {
169+
| platformTools = $platformTools
170+
|}""".trim.stripMargin
171+
}
172+
132173
def fromPathString(path: String)(using Context): ClassPath = {
133174
val settings = ctx.settings.classpath.update(path)
134175
inContext(ctx.fresh.setSettings(settings)) {

compiler/src/dotty/tools/dotc/config/Properties.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ trait PropertiesTrait {
5555

5656
def envOrElse(name: String, alt: String): String = Option(System getenv name) getOrElse alt
5757
def envOrNone(name: String): Option[String] = Option(System getenv name)
58+
def envOrSome(name: String, alt: => Option[String]) = envOrNone(name) orElse alt
5859

5960
// for values based on propFilename
6061
def scalaPropOrElse(name: String, alt: String): String = scalaProps.getProperty(name, alt)

0 commit comments

Comments
 (0)