Skip to content

Commit 9906807

Browse files
committed
Fix #2874: Remove the distinction libs/code in the JSEnv API.
`JSEnv#jsRunner` and friends `asyncRunner` and `comRunner` now only take a `Seq[VirtualJSFiles]`, containing all JS files to be given to the JS environment. The JS environments therefore make no distinction between "library" files and the "code" file. The disinction was arbitrary anyway, and unifying them makes sure that all the JS files are first-class. In particular, it forced to solve shortcomings in terms of error handling in `JSDOMNodeJSEnv`.
1 parent 4640df1 commit 9906807

File tree

4 files changed

+31
-60
lines changed

4 files changed

+31
-60
lines changed

phantomjs-env/src/main/scala/org/scalajs/jsenv/phantomjs/PhantomJSEnv.scala

Lines changed: 14 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -41,32 +41,23 @@ class PhantomJSEnv(
4141

4242
protected def vmName: String = "PhantomJS"
4343

44-
override def jsRunner(libs: Seq[VirtualJSFile],
45-
code: VirtualJSFile): JSRunner = {
46-
new PhantomRunner(libs, code)
47-
}
44+
override def jsRunner(files: Seq[VirtualJSFile]): JSRunner =
45+
new PhantomRunner(files)
4846

49-
override def asyncRunner(libs: Seq[VirtualJSFile],
50-
code: VirtualJSFile): AsyncJSRunner = {
51-
new AsyncPhantomRunner(libs, code)
52-
}
47+
override def asyncRunner(files: Seq[VirtualJSFile]): AsyncJSRunner =
48+
new AsyncPhantomRunner(files)
5349

54-
override def comRunner(libs: Seq[VirtualJSFile],
55-
code: VirtualJSFile): ComJSRunner = {
56-
new ComPhantomRunner(libs, code)
57-
}
50+
override def comRunner(files: Seq[VirtualJSFile]): ComJSRunner =
51+
new ComPhantomRunner(files)
5852

59-
protected class PhantomRunner(libs: Seq[VirtualJSFile],
60-
code: VirtualJSFile) extends ExtRunner(libs, code)
61-
with AbstractPhantomRunner
53+
protected class PhantomRunner(files: Seq[VirtualJSFile])
54+
extends ExtRunner(files) with AbstractPhantomRunner
6255

63-
protected class AsyncPhantomRunner(libs: Seq[VirtualJSFile],
64-
code: VirtualJSFile) extends AsyncExtRunner(libs, code)
65-
with AbstractPhantomRunner
56+
protected class AsyncPhantomRunner(files: Seq[VirtualJSFile])
57+
extends AsyncExtRunner(files) with AbstractPhantomRunner
6658

67-
protected class ComPhantomRunner(libs: Seq[VirtualJSFile],
68-
code: VirtualJSFile) extends AsyncPhantomRunner(libs, code)
69-
with ComJSRunner {
59+
protected class ComPhantomRunner(files: Seq[VirtualJSFile])
60+
extends AsyncPhantomRunner(files) with ComJSRunner {
7061

7162
private var mgrIsRunning: Boolean = false
7263

@@ -403,9 +394,8 @@ class PhantomJSEnv(
403394
out.write(s"""<html><head>
404395
<title>Phantom.js Launcher</title>
405396
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />""")
406-
sendJS(getLibJSFiles(), out)
407-
writeCodeLauncher(code, out)
408-
out.write(s"</head>\n<body onload='$launcherName()'></body>\n</html>\n")
397+
sendJS(getJSFiles(), out)
398+
out.write(s"</head>\n<body></body>\n</html>\n")
409399
}
410400

411401
protected def createTmpLauncherFile(): File = {
@@ -486,17 +476,6 @@ class PhantomJSEnv(
486476

487477
webTmpF
488478
}
489-
490-
protected def writeCodeLauncher(code: VirtualJSFile, out: Writer): Unit = {
491-
// Create a file with the launcher function.
492-
val launcherFile = new MemVirtualJSFile("phantomjs-launcher.js")
493-
launcherFile.content = s"""
494-
// Phantom.js code launcher
495-
// Origin: ${code.path}
496-
function $launcherName() {${code.content}}
497-
"""
498-
writeJSFile(launcherFile, out)
499-
}
500479
}
501480

502481
protected def htmlEscape(str: String): String = str.flatMap {
@@ -513,6 +492,4 @@ private object PhantomJSEnv {
513492
private final val MaxByteMessageSize = 32768 // 32 KB
514493
private final val MaxCharMessageSize = MaxByteMessageSize / 2 // 2B per char
515494
private final val MaxCharPayloadSize = MaxCharMessageSize - 1 // frag flag
516-
517-
private final val launcherName = "scalaJSPhantomJSEnvLauncher"
518495
}

phantomjs-env/src/main/scala/org/scalajs/jsenv/phantomjs/RetryingComJSEnv.scala

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,26 +42,26 @@ final class RetryingComJSEnv(val baseEnv: ComJSEnv,
4242

4343
def name: String = s"Retrying ${baseEnv.name}"
4444

45-
def jsRunner(libs: Seq[VirtualJSFile], code: VirtualJSFile): JSRunner =
46-
baseEnv.jsRunner(libs, code)
45+
def jsRunner(files: Seq[VirtualJSFile]): JSRunner =
46+
baseEnv.jsRunner(files)
4747

48-
def asyncRunner(libs: Seq[VirtualJSFile], code: VirtualJSFile): AsyncJSRunner =
49-
baseEnv.asyncRunner(libs, code)
48+
def asyncRunner(files: Seq[VirtualJSFile]): AsyncJSRunner =
49+
baseEnv.asyncRunner(files)
5050

51-
def comRunner(libs: Seq[VirtualJSFile], code: VirtualJSFile): ComJSRunner =
52-
new RetryingComJSRunner(libs, code)
51+
def comRunner(files: Seq[VirtualJSFile]): ComJSRunner =
52+
new RetryingComJSRunner(files)
5353

5454
/** Hack to work around abstract override in ComJSRunner */
5555
private trait DummyJSRunner {
5656
def stop(): Unit = ()
5757
}
5858

59-
private class RetryingComJSRunner(libs: Seq[VirtualJSFile],
60-
code: VirtualJSFile) extends DummyJSRunner with ComJSRunner {
59+
private class RetryingComJSRunner(files: Seq[VirtualJSFile])
60+
extends DummyJSRunner with ComJSRunner {
6161

6262
private[this] val promise = Promise[Unit]
6363

64-
private[this] var curRunner = baseEnv.comRunner(libs, code)
64+
private[this] var curRunner = baseEnv.comRunner(files)
6565

6666
private[this] var hasReceived = false
6767
private[this] var retryCount = 0
@@ -135,7 +135,7 @@ final class RetryingComJSEnv(val baseEnv: ComJSEnv,
135135
val oldRunner = curRunner
136136

137137
curRunner = try {
138-
baseEnv.comRunner(libs, code)
138+
baseEnv.comRunner(files)
139139
} catch {
140140
case NonFatal(t) =>
141141
_logger.error("Could not retry: creating an new runner failed: " +

phantomjs-env/src/test/scala/org/scalajs/jsenv/phantomjs/RetryingComJSEnvTest.scala

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,14 @@ class RetryingComJSEnvTest extends JSEnvTest with ComTests {
2828
private[this] var fails = 0
2929
private[this] var failedReceive = false
3030

31-
def jsRunner(libs: Seq[VirtualJSFile],
32-
code: VirtualJSFile): JSRunner = {
33-
baseEnv.jsRunner(libs, code)
34-
}
31+
def jsRunner(files: Seq[VirtualJSFile]): JSRunner =
32+
baseEnv.jsRunner(files)
3533

36-
def asyncRunner(libs: Seq[VirtualJSFile],
37-
code: VirtualJSFile): AsyncJSRunner = {
38-
baseEnv.asyncRunner(libs, code)
39-
}
34+
def asyncRunner(files: Seq[VirtualJSFile]): AsyncJSRunner =
35+
baseEnv.asyncRunner(files)
4036

41-
def comRunner(libs: Seq[VirtualJSFile],
42-
code: VirtualJSFile): ComJSRunner = {
43-
new FailingComJSRunner(baseEnv.comRunner(libs, code))
44-
}
37+
def comRunner(files: Seq[VirtualJSFile]): ComJSRunner =
38+
new FailingComJSRunner(baseEnv.comRunner(files))
4539

4640
/** Hack to work around abstract override in ComJSRunner */
4741
private trait DummyJSRunner {

sbt-plugin-test/project/Jetty9Test.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ object Jetty9Test {
4242
"""
4343
)
4444

45-
val runner = jsEnv.comRunner(code)
45+
val runner = jsEnv.comRunner(code :: Nil)
4646

4747
runner.start(streams.value.log, jsConsole)
4848

0 commit comments

Comments
 (0)