aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/scala/async/internal/AsyncBase.scala
diff options
context:
space:
mode:
authorJason Zaugg <jzaugg@gmail.com>2013-07-07 10:48:11 +1000
committerJason Zaugg <jzaugg@gmail.com>2013-07-07 10:48:11 +1000
commit2d8506a64392cd7192b6831c38798cc9a7c8bfed (patch)
tree84eafcf1a9a179eeaa97dd1e3595c18351b2b814 /src/main/scala/scala/async/internal/AsyncBase.scala
parentc60c38ca6098402f7a9cc6d6746b664bb2b1306c (diff)
downloadscala-async-2d8506a64392cd7192b6831c38798cc9a7c8bfed.tar.gz
scala-async-2d8506a64392cd7192b6831c38798cc9a7c8bfed.tar.bz2
scala-async-2d8506a64392cd7192b6831c38798cc9a7c8bfed.zip
Move implementation details to scala.async.internal._.
If we intend to keep CPS fallback around for any length of time it should probably move there too.
Diffstat (limited to 'src/main/scala/scala/async/internal/AsyncBase.scala')
-rw-r--r--src/main/scala/scala/async/internal/AsyncBase.scala58
1 files changed, 58 insertions, 0 deletions
diff --git a/src/main/scala/scala/async/internal/AsyncBase.scala b/src/main/scala/scala/async/internal/AsyncBase.scala
new file mode 100644
index 0000000..2f7e38d
--- /dev/null
+++ b/src/main/scala/scala/async/internal/AsyncBase.scala
@@ -0,0 +1,58 @@
+/*
+ * Copyright (C) 2012 Typesafe Inc. <http://www.typesafe.com>
+ */
+
+package scala.async.internal
+
+import scala.reflect.internal.annotations.compileTimeOnly
+import scala.reflect.macros.Context
+
+/**
+ * A base class for the `async` macro. Subclasses must provide:
+ *
+ * - Concrete types for a given future system
+ * - Tree manipulations to create and complete the equivalent of Future and Promise
+ * in that system.
+ * - The `async` macro declaration itself, and a forwarder for the macro implementation.
+ * (The latter is temporarily needed to workaround bug SI-6650 in the macro system)
+ *
+ * The default implementation, [[scala.async.Async]], binds the macro to `scala.concurrent._`.
+ */
+abstract class AsyncBase {
+ self =>
+
+ type FS <: FutureSystem
+ val futureSystem: FS
+
+ /**
+ * A call to `await` must be nested in an enclosing `async` block.
+ *
+ * A call to `await` does not block the current thread, rather it is a delimiter
+ * used by the enclosing `async` macro. Code following the `await`
+ * call is executed asynchronously, when the argument of `await` has been completed.
+ *
+ * @param awaitable the future from which a value is awaited.
+ * @tparam T the type of that value.
+ * @return the value.
+ */
+ @compileTimeOnly("`await` must be enclosed in an `async` block")
+ def await[T](awaitable: futureSystem.Fut[T]): T = ???
+
+ protected[async] def fallbackEnabled = false
+
+ def asyncImpl[T: c.WeakTypeTag](c: Context)
+ (body: c.Expr[T])
+ (execContext: c.Expr[futureSystem.ExecContext]): c.Expr[futureSystem.Fut[T]] = {
+ import c.universe._
+
+ val asyncMacro = AsyncMacro(c, futureSystem)
+
+ val code = asyncMacro.asyncTransform[T](
+ body.tree.asInstanceOf[asyncMacro.global.Tree],
+ execContext.tree.asInstanceOf[asyncMacro.global.Tree],
+ fallbackEnabled)(implicitly[c.WeakTypeTag[T]].asInstanceOf[asyncMacro.global.WeakTypeTag[T]])
+
+ AsyncUtils.vprintln(s"async state machine transform expands to:\n ${code}")
+ c.Expr[futureSystem.Fut[T]](code.asInstanceOf[Tree])
+ }
+}