Skip to content

Commit

Permalink
Remove semantics Compliant for asInstaceOf
Browse files Browse the repository at this point in the history
  • Loading branch information
MaciejG604 committed Dec 7, 2023
1 parent d98be3b commit 398b3ad
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 53 deletions.
Expand Up @@ -690,19 +690,20 @@ object Package extends ScalaCommand[PackageOptions] with BuildCommandHelpers {
destPath: os.Path,
mainClass: Option[String],
logger: Logger
): Either[BuildException, os.Path] = {
val linkerConfig = build.options.scalaJsOptions.linkerConfig(logger)
linkJs(
): Either[BuildException, os.Path] = for {
isFullOpt <- build.options.scalaJsOptions.fullOpt
linkerConfig = build.options.scalaJsOptions.linkerConfig(logger)
linkResult <- linkJs(
build,
destPath,
mainClass,
addTestInitializer = false,
linkerConfig,
build.options.scalaJsOptions.fullOpt,
isFullOpt,
build.options.scalaJsOptions.noOpt.getOrElse(false),
logger
)
}
} yield linkResult

private def bootstrap(
build: Build.Successful,
Expand Down
Expand Up @@ -447,7 +447,7 @@ object Run extends ScalaCommand[RunOptions] with BuildCommandHelpers {
Some(mainClass),
addTestInitializer = false,
linkerConfig,
build.options.scalaJsOptions.fullOpt,
value(build.options.scalaJsOptions.fullOpt),
build.options.scalaJsOptions.noOpt.getOrElse(false),
logger,
scratchDirOpt
Expand Down
Expand Up @@ -21,8 +21,9 @@ final case class ScalaJsOptions(

@Group(HelpGroup.ScalaJs.toString)
@Tag(tags.should)
@HelpMessage("The Scala.js mode, either `dev` or `release`")
@HelpMessage("The Scala.js mode, for `fastLinkJS` use one of [`dev`, `fastLinkJS` or `fast`], for `fullLinkJS` use one of [`release`, `fullLinkJS`, `full`]")
jsMode: Option[String] = None,

@HelpMessage("The Scala.js module kind: commonjs/common, esmodule/es, nomodule/none")
@Group(HelpGroup.ScalaJs.toString)
@Tag(tags.should)
Expand Down
Expand Up @@ -205,7 +205,7 @@ object Test extends ScalaCommand[TestOptions] {
None,
addTestInitializer = true,
linkerConfig,
build.options.scalaJsOptions.fullOpt,
value(build.options.scalaJsOptions.fullOpt),
build.options.scalaJsOptions.noOpt.getOrElse(false),
logger,
esModule
Expand Down
@@ -0,0 +1,12 @@
package scala.build.errors

final class UnrecognizedJsOptModeError(
mode: String,
aliasesForFullLink: Seq[String],
aliasesForFastLink: Seq[String]
) extends BuildException(
s"""Unrecognized JS optimization mode: $mode.
|Available options:
|- for fastLinkJs: ${aliasesForFastLink.mkString(", ")}
|- for fullLinkJs: ${aliasesForFullLink.mkString(", ")}""".stripMargin
)
Expand Up @@ -5,7 +5,7 @@ import com.eed3si9n.expecty.Expecty.expect
import scala.cli.integration.TestUtil.removeAnsiColors

trait RunScalaJsTestDefinitions { _: RunTestDefinitions =>
def simpleJsTest(extraArgs: String*): Unit = {
def simpleJsTestOutput(extraArgs: String*): String = {
val fileName = "simple.sc"
val message = "Hello"
val inputs = TestInputs(
Expand All @@ -17,18 +17,31 @@ trait RunScalaJsTestDefinitions { _: RunTestDefinitions =>
|""".stripMargin
)
inputs.fromRoot { root =>
val output = os.proc(TestUtil.cli, extraOptions, fileName, "--js", extraArgs).call(cwd =
root
val output = os.proc(TestUtil.cli, extraOptions, fileName, "--js", extraArgs).call(
cwd = root,
mergeErrIntoOut = true
).out.trim()
expect(output.linesIterator.toSeq.last == message)
output
}
}

test("simple script JS") {
simpleJsTest()
simpleJsTestOutput()
}

test(s"simple script JS in fullLinkJs mode") {
val output = simpleJsTestOutput("--js-mode", "fullLinkJs", "-v", "-v", "-v")
expect(output.contains("--fullOpt"))
expect(!output.contains("--fastOpt"))
expect(!output.contains("--noOpt"))
}
test("simple script JS in release mode") {
simpleJsTest("--js-mode", "release")

test(s"simple script JS in fastLinkJs mode") {
val output = simpleJsTestOutput("--js-mode", "fastLinkJs", "-v", "-v", "-v")
expect(output.contains("--fastOpt"))
expect(!output.contains("--fullOpt"))
expect(!output.contains("--noOpt"))
}

test("without node on the PATH") {
Expand Down
Expand Up @@ -10,19 +10,9 @@ final case class ScalaJsLinkerConfig(
esFeatures: ScalaJsLinkerConfig.ESFeatures = ScalaJsLinkerConfig.ESFeatures(),
jsHeader: Option[String] = None,
prettyPrint: Boolean = false,
relativizeSourceMapBase: Option[String] = None,
semantics: ScalaJsLinkerConfig.Semantics = ScalaJsLinkerConfig.Semantics()
relativizeSourceMapBase: Option[String] = None
) {
def linkerCliArgs: Seq[String] = {

// FIXME Fatal asInstanceOfs should be the default, but it seems we can't
// pass Unchecked via the CLI here
// It seems we can't pass the other semantics fields either.
val semanticsArgs =
if (semantics.asInstanceOfs == ScalaJsLinkerConfig.CheckedBehavior.Compliant)
Seq("--compliantAsInstanceOfs")
else
Nil
val moduleKindArgs = Seq("--moduleKind", moduleKind)
val moduleSplitStyleArgs = Seq("--moduleSplitStyle", moduleSplitStyle)
val smallModuleForPackageArgs =
Expand All @@ -41,7 +31,6 @@ final case class ScalaJsLinkerConfig(
else Nil
val jsHeaderArg = if (jsHeader.nonEmpty) Seq("--jsHeader", jsHeader.getOrElse("")) else Nil
val configArgs = Seq[os.Shellable](
semanticsArgs,
moduleKindArgs,
moduleSplitStyleArgs,
smallModuleForPackageArgs,
Expand Down Expand Up @@ -89,12 +78,4 @@ object ScalaJsLinkerConfig {

def default = ES2015
}

final case class Semantics(
asInstanceOfs: String = CheckedBehavior.Compliant
)

object CheckedBehavior {
val Compliant = "Compliant"
}
}
Expand Up @@ -6,6 +6,7 @@ import dependency._
import java.util.Locale

import scala.build.Logger
import scala.build.errors.UnrecognizedJsOptModeError
import scala.build.internal.{Constants, ScalaJsLinkerConfig}

final case class ScalaJsOptions(
Expand All @@ -25,7 +26,27 @@ final case class ScalaJsOptions(
esVersionStr: Option[String] = None,
noOpt: Option[Boolean] = None
) {
def fullOpt: Boolean = mode.contains("release")
private val validFullLinkAliases = Set(
"release",
"fullLinkJs",
"full"
)
private val validFastLinkAliases = Set(
"dev",
"fastLinkJs",
"fast"
)

def fullOpt: Either[UnrecognizedJsOptModeError, Boolean] =
if (mode.isEmpty || mode.exists(validFullLinkAliases.union(validFastLinkAliases).contains))
Right(mode.exists(validFullLinkAliases.contains))
else
Left(UnrecognizedJsOptModeError(
mode.get,
validFullLinkAliases.toSeq,
validFastLinkAliases.toSeq
))

def platformSuffix: String =
"sjs" + ScalaVersion.jsBinary(finalVersion).getOrElse(finalVersion)
def jsDependencies(scalaVersion: String): Seq[AnyDependency] =
Expand Down Expand Up @@ -128,13 +149,13 @@ final case class ScalaJsOptions(
)

ScalaJsLinkerConfig(
moduleKind(logger),
checkIr.getOrElse(false), // meh
emitSourceMaps,
moduleSplitStyle(logger),
smallModuleForPackage,
esFeatures,
header
moduleKind = moduleKind(logger),
checkIR = checkIr.getOrElse(false), // meh
sourceMap = emitSourceMaps,
moduleSplitStyle = moduleSplitStyle(logger),
smallModuleForPackage = smallModuleForPackage,
esFeatures = esFeatures,
jsHeader = header
)
}
}
Expand Down
2 changes: 1 addition & 1 deletion website/docs/reference/cli-options.md
Expand Up @@ -1255,7 +1255,7 @@ The Scala.js version (1.14.0 by default).

### `--js-mode`

The Scala.js mode, either `dev` or `release`
The Scala.js mode, for `fastLinkJS` use one of [`dev`, `fastLinkJS` or `fast`], for `fullLinkJS` use one of [`release`, `fullLinkJS`, `full`]

### `--js-module-kind`

Expand Down
2 changes: 1 addition & 1 deletion website/docs/reference/scala-command/cli-options.md
Expand Up @@ -712,7 +712,7 @@ The Scala.js version (1.14.0 by default).

`SHOULD have` per Scala Runner specification

The Scala.js mode, either `dev` or `release`
The Scala.js mode, for `fastLinkJS` use one of [`dev`, `fastLinkJS` or `fast`], for `fullLinkJS` use one of [`release`, `fullLinkJS`, `full`]

### `--js-module-kind`

Expand Down
18 changes: 9 additions & 9 deletions website/docs/reference/scala-command/runner-specification.md
Expand Up @@ -132,7 +132,7 @@ The Scala.js version (1.14.0 by default).

**--js-mode**

The Scala.js mode, either `dev` or `release`
The Scala.js mode, for `fastLinkJS` use one of [`dev`, `fastLinkJS` or `fast`], for `fullLinkJS` use one of [`release`, `fullLinkJS`, `full`]

**--js-module-kind**

Expand Down Expand Up @@ -875,7 +875,7 @@ The Scala.js version (1.14.0 by default).

**--js-mode**

The Scala.js mode, either `dev` or `release`
The Scala.js mode, for `fastLinkJS` use one of [`dev`, `fastLinkJS` or `fast`], for `fullLinkJS` use one of [`release`, `fullLinkJS`, `full`]

**--js-module-kind**

Expand Down Expand Up @@ -1422,7 +1422,7 @@ The Scala.js version (1.14.0 by default).

**--js-mode**

The Scala.js mode, either `dev` or `release`
The Scala.js mode, for `fastLinkJS` use one of [`dev`, `fastLinkJS` or `fast`], for `fullLinkJS` use one of [`release`, `fullLinkJS`, `full`]

**--js-module-kind**

Expand Down Expand Up @@ -1995,7 +1995,7 @@ The Scala.js version (1.14.0 by default).

**--js-mode**

The Scala.js mode, either `dev` or `release`
The Scala.js mode, for `fastLinkJS` use one of [`dev`, `fastLinkJS` or `fast`], for `fullLinkJS` use one of [`release`, `fullLinkJS`, `full`]

**--js-module-kind**

Expand Down Expand Up @@ -2587,7 +2587,7 @@ The Scala.js version (1.14.0 by default).

**--js-mode**

The Scala.js mode, either `dev` or `release`
The Scala.js mode, for `fastLinkJS` use one of [`dev`, `fastLinkJS` or `fast`], for `fullLinkJS` use one of [`release`, `fullLinkJS`, `full`]

**--js-module-kind**

Expand Down Expand Up @@ -3155,7 +3155,7 @@ The Scala.js version (1.14.0 by default).

**--js-mode**

The Scala.js mode, either `dev` or `release`
The Scala.js mode, for `fastLinkJS` use one of [`dev`, `fastLinkJS` or `fast`], for `fullLinkJS` use one of [`release`, `fullLinkJS`, `full`]

**--js-module-kind**

Expand Down Expand Up @@ -3760,7 +3760,7 @@ The Scala.js version (1.14.0 by default).

**--js-mode**

The Scala.js mode, either `dev` or `release`
The Scala.js mode, for `fastLinkJS` use one of [`dev`, `fastLinkJS` or `fast`], for `fullLinkJS` use one of [`release`, `fullLinkJS`, `full`]

**--js-module-kind**

Expand Down Expand Up @@ -4416,7 +4416,7 @@ The Scala.js version (1.14.0 by default).

**--js-mode**

The Scala.js mode, either `dev` or `release`
The Scala.js mode, for `fastLinkJS` use one of [`dev`, `fastLinkJS` or `fast`], for `fullLinkJS` use one of [`release`, `fullLinkJS`, `full`]

**--js-module-kind**

Expand Down Expand Up @@ -5303,7 +5303,7 @@ The Scala.js version (1.14.0 by default).

**--js-mode**

The Scala.js mode, either `dev` or `release`
The Scala.js mode, for `fastLinkJS` use one of [`dev`, `fastLinkJS` or `fast`], for `fullLinkJS` use one of [`release`, `fullLinkJS`, `full`]

**--js-module-kind**

Expand Down

0 comments on commit 398b3ad

Please sign in to comment.