Skip to content

Commit c354793

Browse files
committed
Remove the deprecated APIs in JS envs.
In the process, the implementation of `org.scalajs.jsenv.nodejs.JSDOMNodeJSEnv` is transferred to the new `org.scalajs.jsenv.jsdomnodejs.JSDOMNodeJSEnv`.
1 parent 2768e29 commit c354793

File tree

2 files changed

+117
-147
lines changed

2 files changed

+117
-147
lines changed

js-envs/src/main/scala/org/scalajs/jsenv/jsdomnodejs/JSDOMNodeJSEnv.scala

Lines changed: 117 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,126 @@
88

99
package org.scalajs.jsenv.jsdomnodejs
1010

11+
import java.io.OutputStream
12+
13+
import org.scalajs.core.tools.io._
14+
import org.scalajs.jsenv._
15+
import org.scalajs.jsenv.nodejs.AbstractNodeJSEnv
16+
17+
import org.scalajs.core.ir.Utils.escapeJS
18+
1119
class JSDOMNodeJSEnv(config: JSDOMNodeJSEnv.Config)
12-
extends org.scalajs.jsenv.nodejs.JSDOMNodeJSEnv(config.executable,
13-
config.args, config.env, internal = ()) {
20+
extends AbstractNodeJSEnv(config.executable, config.args, config.env,
21+
sourceMap = false) {
1422

1523
def this() = this(JSDOMNodeJSEnv.Config())
24+
25+
protected def vmName: String = "Node.js with JSDOM"
26+
27+
override def jsRunner(files: Seq[VirtualJSFile]): JSRunner =
28+
new DOMNodeRunner(files)
29+
30+
override def asyncRunner(files: Seq[VirtualJSFile]): AsyncJSRunner =
31+
new AsyncDOMNodeRunner(files)
32+
33+
override def comRunner(files: Seq[VirtualJSFile]): ComJSRunner =
34+
new ComDOMNodeRunner(files)
35+
36+
protected class DOMNodeRunner(files: Seq[VirtualJSFile])
37+
extends ExtRunner(files) with AbstractDOMNodeRunner
38+
39+
protected class AsyncDOMNodeRunner(files: Seq[VirtualJSFile])
40+
extends AsyncExtRunner(files) with AbstractDOMNodeRunner
41+
42+
protected class ComDOMNodeRunner(files: Seq[VirtualJSFile])
43+
extends AsyncDOMNodeRunner(files) with NodeComJSRunner
44+
45+
protected trait AbstractDOMNodeRunner extends AbstractNodeRunner {
46+
47+
protected def codeWithJSDOMContext(): Seq[VirtualJSFile] = {
48+
val scriptsPaths = getScriptsJSFiles().map {
49+
case file: FileVirtualFile => file.path
50+
case file => libCache.materialize(file).getAbsolutePath
51+
}
52+
val scriptsURIs =
53+
scriptsPaths.map(path => new java.io.File(path).toURI.toASCIIString)
54+
val scriptsURIsAsJSStrings = scriptsURIs.map('"' + escapeJS(_) + '"')
55+
val jsDOMCode = {
56+
s"""
57+
|(function () {
58+
| var jsdom;
59+
| try {
60+
| jsdom = require("jsdom/lib/old-api.js"); // jsdom >= 10.x
61+
| } catch (e) {
62+
| jsdom = require("jsdom"); // jsdom <= 9.x
63+
| }
64+
|
65+
| var virtualConsole = jsdom.createVirtualConsole()
66+
| .sendTo(console, { omitJsdomErrors: true });
67+
| virtualConsole.on("jsdomError", function (error) {
68+
| /* This inelegant if + console.error is the only way I found
69+
| * to make sure the stack trace of the original error is
70+
| * printed out.
71+
| */
72+
| if (error.detail && error.detail.stack)
73+
| console.error(error.detail.stack);
74+
|
75+
| // Throw the error anew to make sure the whole execution fails
76+
| throw error;
77+
| });
78+
|
79+
| jsdom.env({
80+
| html: "",
81+
| url: "http://localhost/",
82+
| virtualConsole: virtualConsole,
83+
| created: function (error, window) {
84+
| if (error == null) {
85+
| window["__ScalaJSEnv"] = __ScalaJSEnv;
86+
| window["scalajsCom"] = global.scalajsCom;
87+
| } else {
88+
| throw error;
89+
| }
90+
| },
91+
| scripts: [${scriptsURIsAsJSStrings.mkString(", ")}]
92+
| });
93+
|})();
94+
|""".stripMargin
95+
}
96+
Seq(new MemVirtualJSFile("codeWithJSDOMContext.js").withContent(jsDOMCode))
97+
}
98+
99+
/** All the JS files that are passed to the VM.
100+
*
101+
* This method can overridden to provide custom behavior in subclasses.
102+
*
103+
* This method is overridden in `JSDOMNodeJSEnv` so that user-provided
104+
* JS files (excluding "init" files) are executed as *scripts* within the
105+
* jsdom environment, rather than being directly executed by the VM.
106+
*
107+
* The value returned by this method in `JSDOMNodeJSEnv` is
108+
* `initFiles() ++ customInitFiles() ++ codeWithJSDOMContext()`.
109+
*/
110+
override protected def getJSFiles(): Seq[VirtualJSFile] =
111+
initFiles() ++ customInitFiles() ++ codeWithJSDOMContext()
112+
113+
/** JS files to be loaded via scripts in the jsdom environment.
114+
*
115+
* This method can be overridden to provide a different list of scripts.
116+
*
117+
* The default value in `JSDOMNodeJSEnv` is `files`.
118+
*/
119+
protected def getScriptsJSFiles(): Seq[VirtualJSFile] =
120+
files
121+
122+
// Send code to Stdin
123+
override protected def sendVMStdin(out: OutputStream): Unit = {
124+
/* Do not factor this method out into AbstractNodeRunner or when mixin in
125+
* the traits it would use AbstractExtRunner.sendVMStdin due to
126+
* linearization order.
127+
*/
128+
sendJS(getJSFiles(), out)
129+
}
130+
}
16131
}
17132

18133
object JSDOMNodeJSEnv {

js-envs/src/main/scala/org/scalajs/jsenv/nodejs/JSDOMNodeJSEnv.scala

Lines changed: 0 additions & 145 deletions
This file was deleted.

0 commit comments

Comments
 (0)