-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15543 from dotty-staging/sjs-vulpix-run-tests
- Loading branch information
Showing
339 changed files
with
920 additions
and
224 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
sjs-compiler-tests/test/scala/dotty/tools/dotc/JSRun.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package dotty.tools.dotc | ||
|
||
import scala.concurrent.duration.Duration | ||
import scala.concurrent.{Await, ExecutionContext} | ||
|
||
import java.io.InputStream | ||
import java.nio.charset.StandardCharsets | ||
import java.nio.file.Path | ||
|
||
import dotty.tools.vulpix.* | ||
|
||
import org.scalajs.jsenv.* | ||
import org.scalajs.jsenv.nodejs.NodeJSEnv | ||
import org.scalajs.logging.* | ||
|
||
object JSRun: | ||
def runJSCode(sjsCode: Path)(using ExecutionContext): Status = | ||
val logger = new ScalaConsoleLogger(Level.Warn) | ||
|
||
var stdoutStream: Option[InputStream] = None | ||
var stderrStream: Option[InputStream] = None | ||
|
||
val input = Input.Script(sjsCode) :: Nil | ||
val config = RunConfig() | ||
.withLogger(logger) | ||
.withInheritOut(false) | ||
.withInheritErr(false) | ||
.withOnOutputStream { (out, err) => | ||
stdoutStream = out | ||
stderrStream = err | ||
} | ||
|
||
val run = new NodeJSEnv().start(input, config) | ||
try | ||
val success = try { | ||
Await.result(run.future, Duration.Inf) | ||
true | ||
} catch { | ||
case _: Exception => | ||
false | ||
} | ||
val output = readStreamFully(stderrStream) + readStreamFully(stdoutStream) | ||
if success then | ||
Success(output) | ||
else | ||
Failure(output) | ||
finally | ||
run.close() | ||
end runJSCode | ||
|
||
private def readStreamFully(optStream: Option[InputStream]): String = | ||
optStream match | ||
case None => | ||
"" | ||
case Some(stream) => | ||
try | ||
val result = new java.io.ByteArrayOutputStream() | ||
val buffer = new Array[Byte](1024) | ||
while ({ | ||
val len = stream.read(buffer) | ||
len >= 0 && { | ||
result.write(buffer, 0, len) | ||
true | ||
} | ||
}) () | ||
new String(result.toByteArray(), StandardCharsets.UTF_8) | ||
finally | ||
stream.close() | ||
end readStreamFully | ||
end JSRun |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
sjs-compiler-tests/test/scala/dotty/tools/dotc/ScalaJSLink.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package dotty.tools.dotc | ||
|
||
import scala.concurrent.duration.Duration | ||
import scala.concurrent.{Await, ExecutionContext} | ||
|
||
import java.io.File | ||
import java.nio.file.{Files, Path} | ||
|
||
import com.google.common.jimfs.Jimfs | ||
|
||
import org.scalajs.linker.* | ||
import org.scalajs.linker.interface.* | ||
import org.scalajs.logging.* | ||
|
||
object ScalaJSLink: | ||
private val compliantSemantics: Semantics = | ||
Semantics.Defaults | ||
.withAsInstanceOfs(CheckedBehavior.Compliant) | ||
.withArrayIndexOutOfBounds(CheckedBehavior.Compliant) | ||
.withModuleInit(CheckedBehavior.Compliant) | ||
end compliantSemantics | ||
|
||
def link(classPath: String, useCompliantSemantics: Boolean)(using ExecutionContext): Path = | ||
val cpEntries = classPath.split(File.pathSeparatorChar) | ||
|
||
val logger = new ScalaConsoleLogger(Level.Warn) | ||
|
||
val moduleInitializers = Seq(ModuleInitializer.mainMethodWithArgs( | ||
"Test", "main", Nil)) | ||
|
||
val semantics = if useCompliantSemantics then compliantSemantics else Semantics.Defaults | ||
|
||
val linkerConfig = StandardConfig() | ||
.withCheckIR(true) | ||
.withSourceMap(false) | ||
.withBatchMode(true) | ||
.withSemantics(semantics) | ||
|
||
val linker = StandardImpl.linker(linkerConfig) | ||
|
||
val dir = Jimfs.newFileSystem().getPath("tmp") | ||
Files.createDirectory(dir) | ||
|
||
val cache = StandardImpl.irFileCache().newCache | ||
val result = PathIRContainer | ||
.fromClasspath(cpEntries.toSeq.map(entry => new File(entry).toPath())) | ||
.map(_._1) | ||
.flatMap(cache.cached _) | ||
.flatMap(linker.link(_, moduleInitializers, PathOutputDirectory(dir), logger)) | ||
|
||
val report = Await.result(result, Duration.Inf) | ||
|
||
if (report.publicModules.size != 1) | ||
throw new AssertionError(s"got other than 1 module: $report") | ||
|
||
dir.resolve(report.publicModules.head.jsFileName) | ||
end link | ||
end ScalaJSLink |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
// scalajs: --skip | ||
|
||
object Test { | ||
def main(args: Array[String]): Unit = { | ||
val a = new A | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.