Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ language: scala
scala:
- 2.10.7
- 2.11.12
- 2.12.6
- 2.12.8
jdk:
- oraclejdk8
- openjdk8
env:
- JSDOM_VERSION=9.12.0
- JSDOM_VERSION=10.0.0
Expand Down
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ inThisBuild(Seq(
version := "1.0.0-SNAPSHOT",
organization := "org.scala-js",

crossScalaVersions := Seq("2.12.6", "2.10.7", "2.11.12"),
crossScalaVersions := Seq("2.12.8", "2.10.7", "2.11.12"),
scalaVersion := crossScalaVersions.value.head,
scalacOptions ++= Seq("-deprecation", "-feature", "-Xfatal-warnings"),

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ import scala.collection.immutable
import scala.util.control.NonFatal

import java.io._
import java.nio.file.{Files, StandardCopyOption}
import java.nio.charset.StandardCharsets
import java.nio.file.{Files, Path, StandardCopyOption}
import java.net.URI

import org.scalajs.io._
import org.scalajs.io.JSUtils.escapeJS
import com.google.common.jimfs.Jimfs

import org.scalajs.jsenv._
import org.scalajs.jsenv.nodejs._
import org.scalajs.jsenv.JSUtils.escapeJS

class JSDOMNodeJSEnv(config: JSDOMNodeJSEnv.Config) extends JSEnv {

Expand Down Expand Up @@ -49,7 +50,7 @@ class JSDOMNodeJSEnv(config: JSDOMNodeJSEnv.Config) extends JSEnv {
}
}

private def validateInput(input: Input): List[VirtualBinaryFile] = {
private def validateInput(input: Input): List[Path] = {
input match {
case Input.ScriptsToLoad(scripts) =>
scripts
Expand All @@ -58,8 +59,7 @@ class JSDOMNodeJSEnv(config: JSDOMNodeJSEnv.Config) extends JSEnv {
}
}

private def internalStart(files: List[VirtualBinaryFile],
runConfig: RunConfig): JSRun = {
private def internalStart(files: List[Path], runConfig: RunConfig): JSRun = {
val command = config.executable :: config.args
val externalConfig = ExternalJSRun.Config()
.withEnv(env)
Expand All @@ -70,9 +70,7 @@ class JSDOMNodeJSEnv(config: JSDOMNodeJSEnv.Config) extends JSEnv {
private def env: Map[String, String] =
Map("NODE_MODULE_CONTEXTS" -> "0") ++ config.env

private def codeWithJSDOMContext(
scripts: List[VirtualBinaryFile]): List[VirtualBinaryFile] = {

private def codeWithJSDOMContext(scripts: List[Path]): List[Path] = {
val scriptsURIs = scripts.map(JSDOMNodeJSEnv.materialize(_))
val scriptsURIsAsJSStrings =
scriptsURIs.map(uri => '"' + escapeJS(uri.toASCIIString) + '"')
Expand Down Expand Up @@ -116,42 +114,45 @@ class JSDOMNodeJSEnv(config: JSDOMNodeJSEnv.Config) extends JSEnv {
|})();
|""".stripMargin
}
List(MemVirtualBinaryFile.fromStringUTF8("codeWithJSDOMContext.js", jsDOMCode))
List(Files.write(
Jimfs.newFileSystem().getPath("codeWithJSDOMContext.js"),
jsDOMCode.getBytes(StandardCharsets.UTF_8)))
}
}

object JSDOMNodeJSEnv {
private lazy val validator = ExternalJSRun.supports(RunConfig.Validator())

// Copied from NodeJSEnv.scala upstream
private def write(files: List[VirtualBinaryFile])(out: OutputStream): Unit = {
private def write(files: List[Path])(out: OutputStream): Unit = {
val p = new PrintStream(out, false, "UTF8")
try {
files.foreach {
case file: FileVirtualBinaryFile =>
val fname = file.file.getAbsolutePath
p.println(s"""require("${escapeJS(fname)}");""")
case f =>
val in = f.inputStream
try {
val buf = new Array[Byte](4096)

@tailrec
def loop(): Unit = {
val read = in.read(buf)
if (read != -1) {
p.write(buf, 0, read)
loop()
}
}

loop()
} finally {
in.close()
}

p.println()
def writeRunScript(path: Path): Unit = {
try {
val f = path.toFile
val pathJS = "\"" + escapeJS(f.getAbsolutePath) + "\""
p.println(s"""
require('vm').runInThisContext(
require('fs').readFileSync($pathJS, { encoding: "utf-8" }),
{ filename: $pathJS, displayErrors: true }
);
""")
} catch {
case _: UnsupportedOperationException =>
val code = new String(Files.readAllBytes(path), StandardCharsets.UTF_8)
val codeJS = "\"" + escapeJS(code) + "\""
val pathJS = "\"" + escapeJS(path.toString) + "\""
p.println(s"""
require('vm').runInThisContext(
$codeJS,
{ filename: $pathJS, displayErrors: true }
);
""")
}
}

for (file <- files)
writeRunScript(file)
} finally {
p.close()
}
Expand All @@ -178,12 +179,14 @@ object JSDOMNodeJSEnv {
}
}

private def materialize(file: VirtualBinaryFile): URI = {
file match {
case file: FileVirtualFile => file.file.toURI
case file => tmpFile(file.path, file.inputStream)
private def materialize(path: Path): URI = {
try {
path.toFile.toURI
} catch {
case _: UnsupportedOperationException =>
tmpFile(path.toString, Files.newInputStream(path))
}
}
}

final class Config private (
val executable: String,
Expand Down
2 changes: 1 addition & 1 deletion project/build.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version=0.13.17
sbt.version=1.2.8
4 changes: 2 additions & 2 deletions project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.0.0-M7")
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.0.0-M8")

libraryDependencies += "org.scala-js" %% "scalajs-env-nodejs" % "1.0.0-M7"
libraryDependencies += "org.scala-js" %% "scalajs-env-nodejs" % "1.0.0-M8"

unmanagedSourceDirectories in Compile +=
baseDirectory.value.getParentFile / "jsdom-nodejs-env/src/main/scala"