|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: Announcing Scala.js 1.8.0 |
| 4 | +category: news |
| 5 | +tags: [releases] |
| 6 | +permalink: /news/2021/12/10/announcing-scalajs-1.8.0/ |
| 7 | +--- |
| 8 | + |
| 9 | + |
| 10 | +We are excited to announce the release of Scala.js 1.8.0! |
| 11 | + |
| 12 | +This release supports Node.js 17 out of the box. |
| 13 | +If you were previously using one of the workarounds described [in this issue](https://github.com/scala-js/scala-js-js-envs/issues/12), you may remove it when upgrading to Scala.js 1.8.0. |
| 14 | + |
| 15 | +It also introduces compiler warnings when using the default `ExecutionContext.global`. |
| 16 | +Read below for details about the reasons, replacements and ways to silence the warnings. |
| 17 | + |
| 18 | +Finally, it introduces a few new language features, including support for the JavaScript metaproperty `new.target`. |
| 19 | + |
| 20 | +Read on for more details. |
| 21 | + |
| 22 | +<!--more--> |
| 23 | + |
| 24 | +## Getting started |
| 25 | + |
| 26 | +If you are new to Scala.js, head over to [the tutorial]({{ BASE_PATH }}/tutorial/). |
| 27 | + |
| 28 | +If you need help with anything related to Scala.js, you may find our community [on Gitter](https://gitter.im/scala-js/scala-js) and [on Stack Overflow](https://stackoverflow.com/questions/tagged/scala.js). |
| 29 | + |
| 30 | +Bug reports can be filed [on GitHub](https://github.com/scala-js/scala-js/issues). |
| 31 | + |
| 32 | +## Release notes |
| 33 | + |
| 34 | +If upgrading from Scala.js 0.6.x, make sure to read [the release notes of Scala.js 1.0.0]({{ BASE_PATH }}/news/2020/02/25/announcing-scalajs-1.0.0/) first, as they contain a host of important information, including breaking changes. |
| 35 | + |
| 36 | +This is a **minor** release: |
| 37 | + |
| 38 | +* It is backward binary compatible with all earlier versions in the 1.x series: libraries compiled with 1.0.x through 1.7.x can be used with 1.8.0 without change. |
| 39 | +* It is *not* forward binary compatible with 1.7.x: libraries compiled with 1.8.0 cannot be used with 1.7.x or earlier. |
| 40 | +* It is *not* entirely backward source compatible: it is not guaranteed that a codebase will compile *as is* when upgrading from 1.7.x (in particular in the presence of `-Xfatal-warnings`). |
| 41 | + |
| 42 | +As a reminder, libraries compiled with 0.6.x cannot be used with Scala.js 1.x; they must be republished with 1.x first. |
| 43 | + |
| 44 | +## New compiler warnings with broad applicability |
| 45 | + |
| 46 | +### The default `ExecutionContext.global` is now deprecated |
| 47 | + |
| 48 | +The default `ExecutionContext` provided by Scala.js as `ExecutionContext.global` and `ExecutionContext.Implicits.global` uses JavaScript `Promise`s as its underlying mechanism. |
| 49 | +While it is standard in ECMAScript 2015+, it turns out that they are not *fair* with respect to other asynchronous events, like timers and network operations. |
| 50 | +Details on this issue are explained in [the readme for `scalajs-macrotask-executor`](https://github.com/scala-js/scala-js-macrotask-executor). |
| 51 | + |
| 52 | +We cannot fix the default `ExecutionContext` because the only existing solutions use features of browsers and Node.js that are outside of the ECMAScript standard. |
| 53 | +To address the issue, Scala.js 1.8.0 emits a compiler warning to nudge users towards using `scalajs-macrotask-executor`. |
| 54 | + |
| 55 | +Attempts to use either of the following imports: |
| 56 | + |
| 57 | +{% highlight scala %} |
| 58 | +import scala.concurrent.ExecutionContext.global |
| 59 | +import scala.concurrent.ExecutionContext.Implicits.global |
| 60 | +{% endhighlight %} |
| 61 | + |
| 62 | +will emit the following extensive warning: |
| 63 | + |
| 64 | +{% highlight none %} |
| 65 | +Test.scala:5: warning: The global execution context in Scala.js is based on JS Promises (microtasks). |
| 66 | +Using it may prevent macrotasks (I/O, timers, UI rendering) from running reliably. |
| 67 | + |
| 68 | +Unfortunately, there is no way with ECMAScript only to implement a performant |
| 69 | +macrotask execution context (and hence Scala.js core does not contain one). |
| 70 | + |
| 71 | +We recommend you use: https://github.com/scala-js/scala-js-macrotask-executor |
| 72 | +Please refer to the README.md of that project for more details regarding |
| 73 | +microtask vs. macrotask execution contexts. |
| 74 | + |
| 75 | +If you do not care about macrotask fairness, you can silence this warning by: |
| 76 | +- Adding @nowarn("cat=other") (Scala >= 2.13.x only) |
| 77 | +- Setting the -P:scalajs:nowarnGlobalExecutionContext compiler option |
| 78 | +- Using scala.scalajs.concurrent.JSExecutionContext.queue |
| 79 | + (the implementation of ExecutionContext.global in Scala.js) directly. |
| 80 | + |
| 81 | +If you do not care about performance, you can use |
| 82 | +scala.scalajs.concurrent.QueueExecutionContext.timeouts(). |
| 83 | +It is based on setTimeout which makes it fair but slow (due to clamping). |
| 84 | + |
| 85 | + scala.concurrent.Future { } |
| 86 | + ^ |
| 87 | +{% endhighlight %} |
| 88 | + |
| 89 | +As the warning says, the recommended fix is to use the executor provided by [`scalajs-macrotask-executor`](https://github.com/scala-js/scala-js-macrotask-executor), as an external dependency. |
| 90 | + |
| 91 | +If you prefer to keep the existing behavior and silence the warning instead, this can be done in a number of ways: |
| 92 | + |
| 93 | +* Adding `@nowarn("cat=other")` (Scala >= 2.13.x only) |
| 94 | +* Setting the `-P:scalajs:nowarnGlobalExecutionContext` compiler option (in sbt, with `scalacOptions += "-P:scalajs:..."`) |
| 95 | +* Using `scala.scalajs.concurrent.JSExecutionContext.queue` (the implementation of ExecutionContext.global in Scala.js) directly |
| 96 | + |
| 97 | +## New features |
| 98 | + |
| 99 | +### `@JSImport`'s second argument is now optional |
| 100 | + |
| 101 | +When importing native JS members from a module, we use `@JSImport` as follows: |
| 102 | + |
| 103 | +{% highlight scala %} |
| 104 | +@js.native @JSImport("module.js", "SomeClass") |
| 105 | +class SomeClass extends js.Object |
| 106 | + |
| 107 | +@js.native @JSImport("fs", "readFileSync") |
| 108 | +def readFileSync(file: String, charset: String): String = js.native |
| 109 | +{% endhighlight %} |
| 110 | + |
| 111 | +The first argument to `@JSImport` represents the module name, while the second one is the module member name to import. |
| 112 | +In many cases, like in the examples above, the member name is reused as the Scala name. |
| 113 | + |
| 114 | +Starting with Scala.js 1.8.0, the second argument becomes optional, and defaults to the name of the Scala entity that is annotated. |
| 115 | +The above example can be simplified as follows, without change of behavior: |
| 116 | + |
| 117 | +{% highlight scala %} |
| 118 | +@js.native @JSImport("module.js") |
| 119 | +class SomeClass extends js.Object |
| 120 | + |
| 121 | +@js.native @JSImport("fs") |
| 122 | +def readFileSync(file: String, charset: String): String = js.native |
| 123 | +{% endhighlight %} |
| 124 | + |
| 125 | +### `@JSGlobal`'s argument is now optional when used inside an `object` |
| 126 | + |
| 127 | +Similarly to the above change, the argument of `@JSGlobal` is now optional when the annotated entity is in a Scala `object`. |
| 128 | +It was already optional when used at the top-level. |
| 129 | +For example, the following is now allowed: |
| 130 | + |
| 131 | +{% highlight scala %} |
| 132 | +object JSTimers { |
| 133 | + @js.native @JSGlobal |
| 134 | + def setTimeout(f: js.Function0[Any], delay: Int): Unit |
| 135 | +} |
| 136 | +{% endhighlight %} |
| 137 | + |
| 138 | +### New primitive for JavaScript's metaproperty `new.target` |
| 139 | + |
| 140 | +The [JavaScript metaproperty `new.target`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/new.target) gives access to the constructor that was used in a calling `new Foo()` expression. |
| 141 | +It is mainly used in a parent class constructor to know what child class is being instantiated: |
| 142 | + |
| 143 | +{% highlight javascript %} |
| 144 | +class Parent { |
| 145 | + constructor() { |
| 146 | + console.log("Instantiating a " + new.target.name); |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +class Child extends Parent { |
| 151 | + constructor() { |
| 152 | + super(); |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +new Parent() // prints "Instantiating a Parent" |
| 157 | +new Child() // prints "Instantiating a Child" |
| 158 | +{% endhighlight %} |
| 159 | + |
| 160 | +Scala.js 1.8.0 introduces a new primitive, `js.new.target`, which can be used in the constructor of non-native JS classes, and is equivalent to JavaScript's `new.target`. |
| 161 | +With it, the above example can be translated to Scala.js as: |
| 162 | + |
| 163 | +{% highlight scala %} |
| 164 | +import scala.scalajs.js |
| 165 | + |
| 166 | +class Parent extends js.Object { |
| 167 | + println("Instantiating a " + js.`new`.target.name) |
| 168 | +} |
| 169 | + |
| 170 | +class Child extends Parent |
| 171 | + |
| 172 | +new Parent() |
| 173 | +new Child() |
| 174 | +{% endhighlight %} |
| 175 | + |
| 176 | +Attempting to use `js.new.target` anywhere but in the constructor of a non-native JS class will result in a compile error. |
| 177 | + |
| 178 | +While JavaScript also allows to use `new.target` in a `function` body, there is no equivalent in Scala.js so far. |
| 179 | +We expect the use cases for that to be very rare, and we have not found a compelling design for that feature in Scala.js yet. |
| 180 | +This is still tracked as [issue #4588](https://github.com/scala-js/scala-js/issues/4588). |
| 181 | + |
| 182 | +### Hashbang lines are accepted in .js file headers |
| 183 | + |
| 184 | +Scala.js 1.7.0 introduced a new linker configuration, `jsHeader`, to specify a comment to insert at the top of .js files: |
| 185 | +Scala.js 1.8.0 extends that mechanism to allow hashbang lines at the very beginning of the header. |
| 186 | +For example, the following header is now valid: |
| 187 | + |
| 188 | +{% highlight scala %} |
| 189 | +scalaJSLinkerConfig ~= { |
| 190 | + _.withJSHeader( |
| 191 | + """ |
| 192 | + |#!/usr/bin/env node |
| 193 | + | |
| 194 | + |/* This is the header, which source maps |
| 195 | + | * take into account. |
| 196 | + | */ |
| 197 | + """.stripMargin.trim() + "\n" |
| 198 | + ) |
| 199 | +} |
| 200 | +{% endhighlight %} |
| 201 | + |
| 202 | +## Miscellaneous |
| 203 | + |
| 204 | +### New JDK APIs |
| 205 | + |
| 206 | +The following methods of `java.lang.String` were added (thanks to [@tom91136](https://github.com/tom91136)): |
| 207 | + |
| 208 | +* `isBlank()` |
| 209 | +* `strip()` |
| 210 | +* `stripLeading()` |
| 211 | +* `stripTrailing()` |
| 212 | +* `indent(n: Int)` |
| 213 | +* `stripIndent()` |
| 214 | +* `translateEscapes()` |
| 215 | + |
| 216 | +### Upgrade to GCC v20211201 |
| 217 | + |
| 218 | +We upgraded to the Google Closure Compiler v20211201. |
| 219 | + |
| 220 | +## Bug fixes |
| 221 | + |
| 222 | +Among others, the following bugs have been fixed in 1.8.0: |
| 223 | + |
| 224 | +* [#4581](https://github.com/scala-js/scala-js/issues/4581) Scala.js v1.7.0 breaks pattern match in constructor of non-native JS class |
| 225 | +* [#4583](https://github.com/scala-js/scala-js/issues/4583) Error while emitting, head of empty list |
| 226 | +* [#4560](https://github.com/scala-js/scala-js/issues/4560) Console output (e.g., `println`) not redirected to sbt client |
| 227 | +* [#4601](https://github.com/scala-js/scala-js/issues/4601) IR checking error with `scala-java-time` |
| 228 | +* [#4604](https://github.com/scala-js/scala-js/issues/4604) `0/0` should throw an exception |
| 229 | + |
| 230 | +You can find the full list [on GitHub](https://github.com/scala-js/scala-js/issues?q=is%3Aissue+milestone%3Av1.8.0+is%3Aclosed). |
0 commit comments