summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPaul Phillips <paulp@improving.org>2010-12-04 19:33:47 +0000
committerPaul Phillips <paulp@improving.org>2010-12-04 19:33:47 +0000
commit32ca2f2be6a027e2112bc1f8e10f99571caa4a8c (patch)
tree6d0fe6cfac2815088cfe09fe9bb23089109fc337 /src
parentb1e969a11b2b24f5883198dc0b90d9ab5513498b (diff)
downloadscala-32ca2f2be6a027e2112bc1f8e10f99571caa4a8c.tar.gz
scala-32ca2f2be6a027e2112bc1f8e10f99571caa4a8c.tar.bz2
scala-32ca2f2be6a027e2112bc1f8e10f99571caa4a8c.zip
Introducing scala.system, the product of a whir...
Introducing scala.system, the product of a whirlwind couple of hours. More useful stuff than you can shake three sticks at. See the scala.system package object. It's even documented. And since it has methods error and exit, proceeded to deprecate the ones in Predef. No review.
Diffstat (limited to 'src')
-rw-r--r--src/compiler/scala/tools/nsc/ScriptRunner.scala4
-rw-r--r--src/compiler/scala/tools/nsc/util/Origins.scala2
-rw-r--r--src/compiler/scala/tools/nsc/util/package.scala24
-rw-r--r--src/library/scala/Predef.scala15
-rw-r--r--src/library/scala/system/PropertiesMap.scala31
-rw-r--r--src/library/scala/system/ShutdownHookThread.scala39
-rw-r--r--src/library/scala/system/package.scala88
7 files changed, 169 insertions, 34 deletions
diff --git a/src/compiler/scala/tools/nsc/ScriptRunner.scala b/src/compiler/scala/tools/nsc/ScriptRunner.scala
index 1a5bcccddd..cae2219adf 100644
--- a/src/compiler/scala/tools/nsc/ScriptRunner.scala
+++ b/src/compiler/scala/tools/nsc/ScriptRunner.scala
@@ -16,7 +16,7 @@ import io.{ Directory, File, Path, PlainFile }
import java.net.URL
import java.util.jar.{ JarEntry, JarOutputStream }
-import util.{ waitingForThreads, addShutdownHook }
+import util.{ waitingForThreads }
import scala.tools.util.PathResolver
import scala.tools.nsc.reporters.{Reporter,ConsoleReporter}
import util.Exceptional.unwrap
@@ -178,7 +178,7 @@ object ScriptRunner {
val compiledPath = Directory makeTemp "scalascript"
// delete the directory after the user code has finished
- addShutdownHook(compiledPath.deleteRecursively())
+ system.addShutdownHook(compiledPath.deleteRecursively())
settings.outdir.value = compiledPath.path
diff --git a/src/compiler/scala/tools/nsc/util/Origins.scala b/src/compiler/scala/tools/nsc/util/Origins.scala
index a95558272d..29ef9f3f03 100644
--- a/src/compiler/scala/tools/nsc/util/Origins.scala
+++ b/src/compiler/scala/tools/nsc/util/Origins.scala
@@ -85,7 +85,7 @@ object Origins {
{
// Console.println("\nOrigins loaded: registering shutdown hook to display results.")
- addShutdownHook(counters foreach (_.purge()))
+ system.addShutdownHook(counters foreach (_.purge()))
}
def apply[T: Manifest](tag: String): Origins = apply(tag, manifest[T].erasure)
diff --git a/src/compiler/scala/tools/nsc/util/package.scala b/src/compiler/scala/tools/nsc/util/package.scala
index 800cb75b97..b61f3751b9 100644
--- a/src/compiler/scala/tools/nsc/util/package.scala
+++ b/src/compiler/scala/tools/nsc/util/package.scala
@@ -13,33 +13,13 @@ package object util {
/** Apply a function and return the passed value */
def returning[T](x: T)(f: T => Unit): T = { f(x) ; x }
- /** Register a shutdown hook to be run when the jvm exits.
- * Marks it as daemon so it doesn't interfere with shutdown,
- * but the thread is returned so it can be modified.
- */
- def addShutdownHook(body: => Unit) = {
- returning(new Thread { override def run { body } }) { t =>
- t setDaemon true
- Runtime.getRuntime addShutdownHook t
- }
- }
-
- /** All living threads. */
- def allThreads(): List[Thread] = {
- val num = Thread.activeCount()
- val tarray = new Array[Thread](num)
- val got = Thread.enumerate(tarray)
-
- tarray take got toList
- }
-
/** Execute code and then wait for all Threads created during its
* execution to complete.
*/
def waitingForThreads[T](body: => T) = {
- val ts1 = allThreads()
+ val ts1 = system.allThreads()
val result = body
- val ts2 = allThreads()
+ val ts2 = system.allThreads()
val newThreads = ts2.toSet -- ts1 filterNot (_.isDaemon())
newThreads foreach (_.join())
diff --git a/src/library/scala/Predef.scala b/src/library/scala/Predef.scala
index 491d9e3901..7237c628d0 100644
--- a/src/library/scala/Predef.scala
+++ b/src/library/scala/Predef.scala
@@ -58,17 +58,14 @@ object Predef extends LowPriorityImplicits {
// errors and asserts -------------------------------------------------
- // @deprecated("Throw your own exceptions") // deprecation waiting for 2.9
- def error(message: String): Nothing = throw new RuntimeException(message)
+ @deprecated("Use system.error(message) instead")
+ def error(message: String): Nothing = system.error(message)
- // @deprecated("Use System.exit instead") // deprecation waiting for 2.9
- def exit(): Nothing = exit(0)
+ @deprecated("Use system.exit() instead")
+ def exit(): Nothing = system.exit()
- // @deprecated("Use System.exit(status) instead") // deprecation waiting for 2.9
- def exit(status: Int): Nothing = {
- java.lang.System.exit(status)
- throw new Throwable()
- }
+ @deprecated("Use system.exit(status) instead")
+ def exit(status: Int): Nothing = system.exit(status)
/** Tests an expression, throwing an AssertionError if false.
* Calls to this method will not be generated if -Xelide-below
diff --git a/src/library/scala/system/PropertiesMap.scala b/src/library/scala/system/PropertiesMap.scala
new file mode 100644
index 0000000000..a01eee6931
--- /dev/null
+++ b/src/library/scala/system/PropertiesMap.scala
@@ -0,0 +1,31 @@
+/* __ *\
+** ________ ___ / / ___ Scala API **
+** / __/ __// _ | / / / _ | (c) 2003-2010, LAMP/EPFL **
+** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ **
+** /____/\___/_/ |_/____/_/ | | **
+** |/ **
+\* */
+
+package scala.system
+
+import scala.collection.mutable
+import scala.collection.JavaConverters._
+
+/** A bidirectional map wrapping the java System properties.
+ * Changes to System properties will be immediately visible in the map,
+ * and modifications made to the map will be immediately applied to the
+ * System properties.
+ *
+ * @author Paul Phillips
+ * @version 2.9
+ * @since 2.9
+ */
+class PropertiesMap extends mutable.Map[String, String] {
+ override def empty = new PropertiesMap
+ override def default(key: String): String = null
+ def iterator: Iterator[(String, String)] = System.getProperties().asScala.iterator
+ def get(key: String) = Option(System.getProperty(key))
+
+ def -= (key: String): this.type = { System.clearProperty(key) ; this }
+ def += (kv: (String, String)): this.type = { System.setProperty(kv._1, kv._2) ; this }
+} \ No newline at end of file
diff --git a/src/library/scala/system/ShutdownHookThread.scala b/src/library/scala/system/ShutdownHookThread.scala
new file mode 100644
index 0000000000..17a5e398ef
--- /dev/null
+++ b/src/library/scala/system/ShutdownHookThread.scala
@@ -0,0 +1,39 @@
+/* __ *\
+** ________ ___ / / ___ Scala API **
+** / __/ __// _ | / / / _ | (c) 2003-2010, LAMP/EPFL **
+** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ **
+** /____/\___/_/ |_/____/_/ | | **
+** |/ **
+\* */
+
+package scala.system
+
+/** A minimal Thread wrapper to enhance shutdown hooks. It knows
+ * how to unregister itself.
+ *
+ * @author Paul Phillips
+ * @version 2.9
+ * @since 2.9
+ */
+class ShutdownHookThread private (name: String) extends Thread(name) {
+ def remove() = runtime removeShutdownHook this
+}
+
+object ShutdownHookThread {
+ private var hookNameCount: Int = 0
+ private def hookName(): String = synchronized {
+ hookNameCount += 1
+ "shutdownHook" + hookNameCount
+ }
+ /** Creates, names, and registers a shutdown hook to run the
+ * given code.
+ */
+ def apply(body: => Unit): ShutdownHookThread = {
+ val t = new ShutdownHookThread(hookName()) {
+ override def run() = body
+ }
+ t setDaemon true
+ runtime addShutdownHook t
+ t
+ }
+}
diff --git a/src/library/scala/system/package.scala b/src/library/scala/system/package.scala
new file mode 100644
index 0000000000..4dc754d373
--- /dev/null
+++ b/src/library/scala/system/package.scala
@@ -0,0 +1,88 @@
+/* __ *\
+** ________ ___ / / ___ Scala API **
+** / __/ __// _ | / / / _ | (c) 2003-2010, LAMP/EPFL **
+** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ **
+** /____/\___/_/ |_/____/_/ | | **
+** |/ **
+\* */
+
+package scala
+
+import scala.collection.immutable
+import collection.JavaConverters._
+
+/** The package object `scala.system` contains methods for reading
+ * and altering core aspects of the virtual machine as well as the
+ * world outside of it.
+ *
+ * @author Paul Phillips
+ * @version 2.9
+ * @since 2.9
+ */
+package object system {
+ /** Throw a new RuntimeException with the supplied message.
+ *
+ * @return Nothing.
+ */
+ def error(message: String): Nothing = throw new RuntimeException(message)
+
+ /** Exit the JVM with the default status code.
+ *
+ * @return Nothing.
+ */
+ def exit(): Nothing = exit(0)
+
+ /** Exit the JVM with the given status code.
+ *
+ * @return Nothing.
+ */
+ def exit(status: Int): Nothing = {
+ java.lang.System.exit(status)
+ throw new Throwable()
+ }
+
+ /** A convenience method to get the current Runtime instance.
+ *
+ * @return the result of `java.lang.Runtime.getRuntime()`
+ */
+ def runtime: Runtime = Runtime.getRuntime
+
+ /** A bidirectional, mutable Map representing the current system Properties.
+ *
+ * @return a PropertiesMap.
+ * @see `scala.system.PropertiesMap`
+ */
+ def props: PropertiesMap = new PropertiesMap
+
+ /** An immutable Map representing the current system environment.
+ *
+ * @return a Map containing the system environment variables.
+ */
+ def env: immutable.Map[String, String] = immutable.Map(System.getenv().asScala.toSeq: _*)
+
+ /** Register a shutdown hook to be run when the VM exits.
+ * The newly created thread is marked as a daemon so it will not
+ * interfere with VM shutdown. The hook is automatically registered:
+ * the returned value can be ignored, but is available in case the
+ * Thread requires further modification. It can also be unregistered
+ * by calling ShutdownHookThread#remove().
+ *
+ * Note that shutdown hooks are NOT guaranteed to be run.
+ *
+ * @param the body of code to run at shutdown
+ * @return the Thread which will run the shutdown hook.
+ */
+ def addShutdownHook(body: => Unit): Thread = ShutdownHookThread(body)
+
+ /** Returns all active thread in the current thread's thread group and subgroups.
+ *
+ * @return an IndexedSeq containing the threads.
+ */
+ def allThreads(): IndexedSeq[Thread] = {
+ val num = Thread.activeCount()
+ val tarray = new Array[Thread](num)
+ val got = Thread.enumerate(tarray)
+
+ tarray take got
+ }
+} \ No newline at end of file