@@ -14,14 +14,15 @@ import scala.collection.immutable
1414import scala .util .control .NonFatal
1515
1616import java .io ._
17- import java .nio .file .{Files , StandardCopyOption }
17+ import java .nio .charset .StandardCharsets
18+ import java .nio .file .{Files , Path , StandardCopyOption }
1819import java .net .URI
1920
20- import org .scalajs .io ._
21- import org .scalajs .io .JSUtils .escapeJS
21+ import com .google .common .jimfs .Jimfs
2222
2323import org .scalajs .jsenv ._
2424import org .scalajs .jsenv .nodejs ._
25+ import org .scalajs .jsenv .JSUtils .escapeJS
2526
2627class JSDOMNodeJSEnv (config : JSDOMNodeJSEnv .Config ) extends JSEnv {
2728
@@ -49,7 +50,7 @@ class JSDOMNodeJSEnv(config: JSDOMNodeJSEnv.Config) extends JSEnv {
4950 }
5051 }
5152
52- private def validateInput (input : Input ): List [VirtualBinaryFile ] = {
53+ private def validateInput (input : Input ): List [Path ] = {
5354 input match {
5455 case Input .ScriptsToLoad (scripts) =>
5556 scripts
@@ -58,8 +59,7 @@ class JSDOMNodeJSEnv(config: JSDOMNodeJSEnv.Config) extends JSEnv {
5859 }
5960 }
6061
61- private def internalStart (files : List [VirtualBinaryFile ],
62- runConfig : RunConfig ): JSRun = {
62+ private def internalStart (files : List [Path ], runConfig : RunConfig ): JSRun = {
6363 val command = config.executable :: config.args
6464 val externalConfig = ExternalJSRun .Config ()
6565 .withEnv(env)
@@ -70,9 +70,7 @@ class JSDOMNodeJSEnv(config: JSDOMNodeJSEnv.Config) extends JSEnv {
7070 private def env : Map [String , String ] =
7171 Map (" NODE_MODULE_CONTEXTS" -> " 0" ) ++ config.env
7272
73- private def codeWithJSDOMContext (
74- scripts : List [VirtualBinaryFile ]): List [VirtualBinaryFile ] = {
75-
73+ private def codeWithJSDOMContext (scripts : List [Path ]): List [Path ] = {
7674 val scriptsURIs = scripts.map(JSDOMNodeJSEnv .materialize(_))
7775 val scriptsURIsAsJSStrings =
7876 scriptsURIs.map(uri => '"' + escapeJS(uri.toASCIIString) + '"' )
@@ -116,42 +114,45 @@ class JSDOMNodeJSEnv(config: JSDOMNodeJSEnv.Config) extends JSEnv {
116114 |})();
117115 | """ .stripMargin
118116 }
119- List (MemVirtualBinaryFile .fromStringUTF8(" codeWithJSDOMContext.js" , jsDOMCode))
117+ List (Files .write(
118+ Jimfs .newFileSystem().getPath(" codeWithJSDOMContext.js" ),
119+ jsDOMCode.getBytes(StandardCharsets .UTF_8 )))
120120 }
121121}
122122
123123object JSDOMNodeJSEnv {
124124 private lazy val validator = ExternalJSRun .supports(RunConfig .Validator ())
125125
126126 // Copied from NodeJSEnv.scala upstream
127- private def write (files : List [VirtualBinaryFile ])(out : OutputStream ): Unit = {
127+ private def write (files : List [Path ])(out : OutputStream ): Unit = {
128128 val p = new PrintStream (out, false , " UTF8" )
129129 try {
130- files.foreach {
131- case file : FileVirtualBinaryFile =>
132- val fname = file.file.getAbsolutePath
133- p.println(s """ require(" ${escapeJS(fname)}"); """ )
134- case f =>
135- val in = f.inputStream
136- try {
137- val buf = new Array [Byte ](4096 )
138-
139- @ tailrec
140- def loop (): Unit = {
141- val read = in.read(buf)
142- if (read != - 1 ) {
143- p.write(buf, 0 , read)
144- loop()
145- }
146- }
147-
148- loop()
149- } finally {
150- in.close()
151- }
152-
153- p.println()
130+ def writeRunScript (path : Path ): Unit = {
131+ try {
132+ val f = path.toFile
133+ val pathJS = " \" " + escapeJS(f.getAbsolutePath) + " \" "
134+ p.println(s """
135+ require('vm').runInThisContext(
136+ require('fs').readFileSync( $pathJS, { encoding: "utf-8" }),
137+ { filename: $pathJS, displayErrors: true }
138+ );
139+ """ )
140+ } catch {
141+ case _ : UnsupportedOperationException =>
142+ val code = new String (Files .readAllBytes(path), StandardCharsets .UTF_8 )
143+ val codeJS = " \" " + escapeJS(code) + " \" "
144+ val pathJS = " \" " + escapeJS(path.toString) + " \" "
145+ p.println(s """
146+ require('vm').runInThisContext(
147+ $codeJS,
148+ { filename: $pathJS, displayErrors: true }
149+ );
150+ """ )
151+ }
154152 }
153+
154+ for (file <- files)
155+ writeRunScript(file)
155156 } finally {
156157 p.close()
157158 }
@@ -178,12 +179,14 @@ object JSDOMNodeJSEnv {
178179 }
179180 }
180181
181- private def materialize (file : VirtualBinaryFile ): URI = {
182- file match {
183- case file : FileVirtualFile => file.file.toURI
184- case file => tmpFile(file.path, file.inputStream)
182+ private def materialize (path : Path ): URI = {
183+ try {
184+ path.toFile.toURI
185+ } catch {
186+ case _ : UnsupportedOperationException =>
187+ tmpFile(path.toString, Files .newInputStream(path))
185188 }
186- }
189+ }
187190
188191 final class Config private (
189192 val executable : String ,
0 commit comments