summaryrefslogtreecommitdiff
path: root/examples/scala-js/sbt-plugin/src/main/scala/scala/scalajs/sbtplugin/env/rhino/package.scala
blob: 926fbb22a0269c9255b5439c95284324013e9330 (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
/*                     __                                               *\
**     ________ ___   / /  ___      __ ____  Scala.js sbt plugin        **
**    / __/ __// _ | / /  / _ | __ / // __/  (c) 2013, LAMP/EPFL        **
**  __\ \/ /__/ __ |/ /__/ __ |/_// /_\ \    http://scala-js.org/       **
** /____/\___/_/ |_/____/_/ | |__/ /____/                               **
**                          |/____/                                     **
\*                                                                      */


package scala.scalajs.sbtplugin.env

import org.mozilla.javascript._

import scala.scalajs.tools.io._

package object rhino {

  implicit class ContextOps(val self: Context) extends AnyVal {
    def evaluateFile(scope: Scriptable, file: VirtualJSFile,
        securityDomain: AnyRef = null): Any = {
      self.evaluateString(scope, file.content, file.path, 1, securityDomain)
    }
  }

  implicit class ScriptableObjectOps(val self: Scriptable) {
    def addFunction(name: String, function: Array[AnyRef] => Any) = {
      val rhinoFunction =
        new BaseFunction {
          ScriptRuntime.setFunctionProtoAndParent(this, self)
          override def call(context: Context, scope: Scriptable,
              thisObj: Scriptable, args: Array[AnyRef]): AnyRef = {
            function(args) match {
              case () => Undefined.instance
              case r => r.asInstanceOf[AnyRef]
            }
          }
        }

      ScriptableObject.putProperty(self, name, rhinoFunction)
    }
  }
}