summaryrefslogtreecommitdiff
path: root/sbt-plugin/src/main/scala/scala/scalajs/sbtplugin/env/rhino/RhinoJSEnv.scala
blob: cd35ff62399ac736f8cc7d6994d0ac036be2a638 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/*                     __                                               *\
**     ________ ___   / /  ___      __ ____  Scala.js sbt plugin        **
**    / __/ __// _ | / /  / _ | __ / // __/  (c) 2013, LAMP/EPFL        **
**  __\ \/ /__/ __ |/ /__/ __ |/_// /_\ \    http://scala-js.org/       **
** /____/\___/_/ |_/____/_/ | |__/ /____/                               **
**                          |/____/                                     **
\*                                                                      */


package scala.scalajs.sbtplugin.env.rhino

import scala.scalajs.tools.sem.Semantics
import scala.scalajs.tools.io._
import scala.scalajs.tools.classpath._
import scala.scalajs.tools.env._
import scala.scalajs.tools.logging._

import scala.io.Source

import scala.collection.mutable

import scala.concurrent.{Future, Promise, Await}
import scala.concurrent.duration.Duration

import org.mozilla.javascript._

class RhinoJSEnv(semantics: Semantics,
    withDOM: Boolean = false) extends ComJSEnv {

  import RhinoJSEnv._

  /** Executes code in an environment where the Scala.js library is set up to
   *  load its classes lazily.
   *
   *  Other .js scripts in the inputs are executed eagerly before the provided
   *  `code` is called.
   */
  override def jsRunner(classpath: CompleteClasspath, code: VirtualJSFile,
      logger: Logger, console: JSConsole): JSRunner = {
    new Runner(classpath, code, logger, console)
  }

  private class Runner(classpath: CompleteClasspath, code: VirtualJSFile,
      logger: Logger, console: JSConsole) extends JSRunner {
    def run(): Unit = internalRunJS(classpath, code, logger, console, None)
  }

  override def asyncRunner(classpath: CompleteClasspath, code: VirtualJSFile,
      logger: Logger, console: JSConsole): AsyncJSRunner = {
    new AsyncRunner(classpath, code, logger, console)
  }

  private class AsyncRunner(classpath: CompleteClasspath, code: VirtualJSFile,
      logger: Logger, console: JSConsole) extends AsyncJSRunner {

    private[this] val promise = Promise[Unit]

    private[this] val thread = new Thread {
      override def run(): Unit = {
        try {
          internalRunJS(classpath, code, logger, console, optChannel)
          promise.success(())
        } catch {
          case t: Throwable =>
            promise.failure(t)
        }
      }
    }

    def start(): Future[Unit] = {
      thread.start()
      promise.future
    }

    def stop(): Unit = thread.interrupt()

    def isRunning(): Boolean = !promise.isCompleted

    def await(): Unit = Await.result(promise.future, Duration.Inf)

    protected def optChannel(): Option[Channel] = None
  }

  override def comRunner(classpath: CompleteClasspath, code: VirtualJSFile,
      logger: Logger, console: JSConsole): ComJSRunner = {
    new ComRunner(classpath, code, logger, console)
  }

  private class ComRunner(classpath: CompleteClasspath, code: VirtualJSFile,
      logger: Logger, console: JSConsole)
      extends AsyncRunner(classpath, code, logger, console) with ComJSRunner {

    private[this] val channel = new Channel

    override protected def optChannel(): Option[Channel] = Some(channel)

    def send(msg: String): Unit = {
      try {
        channel.sendToJS(msg)
      } catch {
        case _: ChannelClosedException =>
          throw new ComJSEnv.ComClosedException
      }
    }

    def receive(): String = {
      try {
        channel.recvJVM()
      } catch {
        case _: ChannelClosedException =>
          throw new ComJSEnv.ComClosedException
      }
    }

    def close(): Unit = channel.close()

    override def stop(): Unit = {
      close()
      super.stop()
    }

  }

  private def internalRunJS(classpath: CompleteClasspath, code: VirtualJSFile,
      logger: Logger, console: JSConsole, optChannel: Option[Channel]): Unit = {

    val context = Context.enter()
    try {
      val scope = context.initStandardObjects()

      if (withDOM) {
        // Fetch env.rhino.js from webjar
        val name = "env.rhino.js"
        val path = "/META-INF/resources/webjars/envjs/1.2/" + name
        val resource = getClass.getResource(path)
        assert(resource != null, s"need $name as resource")

        // Rhino can't optimize envjs
        context.setOptimizationLevel(-1)

        // Don't print envjs header
        scope.addFunction("print", args => ())

        // Pipe file to Rhino
        val reader = Source.fromURL(resource).bufferedReader
        context.evaluateReader(scope, reader, name, 1, null);

        // No need to actually define print here: It is captured by envjs to
        // implement console.log, which we'll override in the next statement
      }

      // Make sure Rhino does not do its magic for JVM top-level packages (#364)
      val PackagesObject =
        ScriptableObject.getProperty(scope, "Packages").asInstanceOf[Scriptable]
      val topLevelPackageIds = ScriptableObject.getPropertyIds(PackagesObject)
      for (id <- topLevelPackageIds) (id: Any) match {
        case name: String => ScriptableObject.deleteProperty(scope, name)
        case index: Int   => ScriptableObject.deleteProperty(scope, index)
        case _            => // should not happen, I think, but with Rhino you never know
      }

      // Setup console.log
      val jsconsole = context.newObject(scope)
      jsconsole.addFunction("log", _.foreach(console.log _))
      ScriptableObject.putProperty(scope, "console", jsconsole)

      // Optionally setup scalaJSCom
      var recvCallback: Option[String => Unit] = None
      for (channel <- optChannel) {
        val comObj = context.newObject(scope)

        comObj.addFunction("send", s =>
          channel.sendToJVM(Context.toString(s(0))))

        comObj.addFunction("init", s => s(0) match {
          case f: Function =>
            val cb: String => Unit =
              msg => f.call(context, scope, scope, Array(msg))
            recvCallback = Some(cb)
          case _ =>
            sys.error("First argument to init must be a function")
        })

        comObj.addFunction("close", _ => {
          // Tell JVM side we won't send anything
          channel.close()
          // Internally register that we're done
          recvCallback = None
        })

        ScriptableObject.putProperty(scope, "scalajsCom", comObj)
      }

      try {
        // Make the classpath available. Either through lazy loading or by
        // simply inserting
        classpath match {
          case cp: IRClasspath =>
            // Setup lazy loading classpath and source mapper
            val optLoader = if (cp.scalaJSIR.nonEmpty) {
              val loader = new ScalaJSCoreLib(semantics, cp)

              // Setup sourceMapper
              val scalaJSenv = context.newObject(scope)

              scalaJSenv.addFunction("sourceMapper", args => {
                val trace = Context.toObject(args(0), scope)
                loader.mapStackTrace(trace, context, scope)
              })

              ScriptableObject.putProperty(scope, "__ScalaJSEnv", scalaJSenv)

              Some(loader)
            } else {
              None
            }

            // Load JS libraries
            cp.jsLibs.foreach(dep => context.evaluateFile(scope, dep.lib))

            optLoader.foreach(_.insertInto(context, scope))
          case cp =>
            cp.allCode.foreach(context.evaluateFile(scope, _))
        }

        context.evaluateFile(scope, code)

        // Callback the com channel if necessary (if comCallback = None, channel
        // wasn't initialized on the client)
        for ((channel, callback) <- optChannel zip recvCallback) {
          try {
            while (recvCallback.isDefined)
              callback(channel.recvJS())
          } catch {
            case _: ChannelClosedException =>
              // the JVM side closed the connection
          }
        }

        // Enusre the channel is closed to release JVM side
        optChannel.foreach(_.close)

      } catch {
        case e: RhinoException =>
          // Trace here, since we want to be in the context to trace.
          logger.trace(e)
          sys.error(s"Exception while running JS code: ${e.getMessage}")
      }
    } finally {
      Context.exit()
    }
  }

}

object RhinoJSEnv {

  /** Communication channel between the Rhino thread and the rest of the JVM */
  private class Channel {
    private[this] var _closed = false
    private[this] val js2jvm = mutable.Queue.empty[String]
    private[this] val jvm2js = mutable.Queue.empty[String]

    def sendToJS(msg: String): Unit = synchronized {
      jvm2js.enqueue(msg)
      notify()
    }

    def sendToJVM(msg: String): Unit = synchronized {
      js2jvm.enqueue(msg)
      notify()
    }

    def recvJVM(): String = synchronized {
      while (js2jvm.isEmpty && ensureOpen())
        wait()

      js2jvm.dequeue()
    }

    def recvJS(): String = synchronized {
      while (jvm2js.isEmpty && ensureOpen())
        wait()

      jvm2js.dequeue()
    }

    def close(): Unit = synchronized {
      _closed = true
      notify()
    }

    /** Throws if the channel is closed and returns true */
    private def ensureOpen(): Boolean = {
      if (_closed)
        throw new ChannelClosedException
      true
    }
  }

  private class ChannelClosedException extends Exception

}