|
| 1 | +--- |
| 2 | +layout: blog |
| 3 | +post-type: blog |
| 4 | +by: Martin Duhem, Guillaume Massé and Denys Shabalin |
| 5 | +title: "What's new with Scala Native?" |
| 6 | +--- |
| 7 | + |
| 8 | +It has been a little bit more than two months since Scala Native 0.1 has been |
| 9 | +released. What's new in Scala Native? What should you expect for future |
| 10 | +releases? |
| 11 | + |
| 12 | +## Scala Native 0.2 |
| 13 | + |
| 14 | +In April, [Scala Native 0.2 was released][scala-native-0.2-release]. The main |
| 15 | +focus of this release was to increase the coverage of classes from |
| 16 | +the JDK, such that more programs can be ported to Scala Native without any |
| 17 | +further effort. What parts of Java do we now support in Scala Native? Lots! |
| 18 | +We've added support for IO and regular expressions, among others: |
| 19 | + |
| 20 | +### Improvements to the standard library |
| 21 | + |
| 22 | +* Support for file I/O APIs from `java.io` was added by [@Duhemm][@Duhemm] |
| 23 | + from the Scala Center with help from [@cedricviaccoz][@cedricviaccoz] and |
| 24 | + [@Korf74][@Korf74] in [#574][#574]. Scala Native now supports enough to read |
| 25 | + and write files. Doing I/O with Scala Native feels just the same as in normal |
| 26 | + Scala or Java: |
| 27 | + |
| 28 | + ```scala |
| 29 | + import java.io.{DataInputStream, File, FileInputStream} |
| 30 | + val fis = new FileInputStream(new File("hello.txt")) |
| 31 | + val dis = new DataInputStream(fis) |
| 32 | + println(dis.readLine()) |
| 33 | + ``` |
| 34 | + |
| 35 | +* In [#588][#588], [@MasseGuillaume] from the Scala Center added support for |
| 36 | + regular expressions. This implementation relies on Google's RE2 engine and |
| 37 | + [uses a syntax slightly different from the JDK][scala-native-doc-regular-expressions]. |
| 38 | + Using regular expressions with Scala Native works similarly as it does on the |
| 39 | + JVM: |
| 40 | + |
| 41 | + ```scala |
| 42 | + import java.util.regex._ |
| 43 | + val m = Pattern.compile("a+(b+)(a+)").matcher("aaabbba") |
| 44 | + assert(m.find()) |
| 45 | + println(m.group(1)) // prints "bbb" |
| 46 | + println(m.group(2)) // prints "a" |
| 47 | + ``` |
| 48 | + |
| 49 | +* [@densh][@densh] added initial support for Scala's `Future`s in [#618][#618], |
| 50 | + using an implementation similar to that of Scala.js, where `Future`s will be |
| 51 | + completed after the `main` method is executed. Here's an example using Scala's |
| 52 | + `Future`s with Scala Native. Of course, the same code works as well on the |
| 53 | + JVM: |
| 54 | + |
| 55 | + ```scala |
| 56 | + import java.util.concurrent.Future |
| 57 | + import scala.concurrent.ExecutionContext.Implicits.global |
| 58 | + |
| 59 | + object Test { |
| 60 | + def main(args: Array[String]): Unit = { |
| 61 | + println("Start") |
| 62 | + Future { |
| 63 | + println("Hello from the Future") |
| 64 | + }.foreach(_ => ()) |
| 65 | + println("End") |
| 66 | + } |
| 67 | + } |
| 68 | + ``` |
| 69 | + |
| 70 | +* Scala Native now supports pointer subtraction. This work has been |
| 71 | + contributed by [@jonas][@jonas] in [#624][#624]. Pointer subtraction is |
| 72 | + useful, for instance, to determine how many elements there are between two |
| 73 | + elements of the same array: |
| 74 | + |
| 75 | + ```scala |
| 76 | + val carr: Ptr[CChar] = toCString("abcdefg") |
| 77 | + val cptr: Ptr[CChar] = string.strchr(carr, 'd') |
| 78 | + println(cptr - carr) // prints '3' |
| 79 | + ``` |
| 80 | + |
| 81 | +* [@ekrich][@ekrich] extended Scala Native's implementation of `String` to |
| 82 | + support `toUpperCase` and `toLowerCase` in [#573][#573]. |
| 83 | +* The implementation of `java.lang.Character` was extended to support |
| 84 | + `Character.Subset` and `Character.UnicodeBlock` by [@densh][@densh] in |
| 85 | + [#651][#651]. |
| 86 | +* [@asoltysik][@asoltysik] implemented support for system properties in |
| 87 | + [#591][#591]. |
| 88 | + |
| 89 | + ```scala |
| 90 | + println(System.getProperty("java.version")) // prints '1.8' |
| 91 | + println(System.getProperty("file.separator")) // '\' on Windows, |
| 92 | + // '/' elsewhere |
| 93 | + ``` |
| 94 | +* Scala Native can now read environment variables using `System.getEnv`, |
| 95 | + thanks to [@jonas][@jonas]' efforts in [#606][#606]. |
| 96 | +* `stdin` and `stdout` can reliably be read from and written to, thanks to |
| 97 | + [@fduraffourg][@fduraffourg] and [@densh][@densh] in [#622][#622] and |
| 98 | + [#659][#659]. |
| 99 | + |
| 100 | +### Bugfixes |
| 101 | + |
| 102 | +* A bugfix has been contributed by [@jonas][@jonas] to `String.replace`, |
| 103 | + fixing one broken benchmark at the same time. His work can be found in |
| 104 | + [#616][#616]. |
| 105 | +* `System.nanoTime` was fixed by [@brad-rathke][@brad-rathke] in [#549][#549]. |
| 106 | +* [@xuwei-k][@xuwei-k] fixed `nativeAvailableDependencies` which didn't work |
| 107 | + in the `test` configuration in [#565][#565]. |
| 108 | + |
| 109 | +### Improvements to the tooling and integration |
| 110 | + |
| 111 | +* [@MasseGuillaume][@MasseGuillaume] and [@densh][@densh] |
| 112 | + worked on refactoring Scala Native's sbt plugin to make it more idiomatic in |
| 113 | + [#568][#568] and [#630][#630]. |
| 114 | +* Follow up fixes were contributed by [@jonas][@jonas] in [#639][#639] and |
| 115 | + [#653][#653]. They improve how sbt determines the target architecture to |
| 116 | + compile to. |
| 117 | + |
| 118 | +### Preparing for a better garbage collector |
| 119 | + |
| 120 | +In this release, [@LukasKellenberger][@LukasKellenberger] introduced in |
| 121 | +[#539][#539] a new setting in the sbt plugin that lets users select what |
| 122 | +implementation of the garbage collector should be used. Currently, it lets you |
| 123 | +select Boehm GC, or disable the garbage collector altogether. |
| 124 | + |
| 125 | +This work was done in preparation for [the improved GC that will be shipped |
| 126 | +with Scala Native 0.3][#726]! |
| 127 | + |
| 128 | + |
| 129 | +## A community effort |
| 130 | + |
| 131 | +As shown, many of the improvements that were brought by Scala Native 0.2 have |
| 132 | +been contributed by members of the vibrant community that is developing itself |
| 133 | +around Scala Native. In total, to get to Scala Native 0.2, there have been 61 |
| 134 | +commits merged, 11,344 lines added and 1,954 lines deleted by 17 people: |
| 135 | +Denys Shabalin, Jonas Fonseca, Guillaume Massé, Martin Duhem, |
| 136 | +Lukas Kellenberger, Andrzej Sołtysik, Eric K Richardson, Remi Coudert, |
| 137 | +Florian Duraffour, Brad Rathke, Richard Whaling, Ruben Berenguel M, |
| 138 | +Sam Halliday, Shunsuke Otani, Cedric Viaccoz, Kenji Yoshida, Ignat Loskutov. |
| 139 | + |
| 140 | +The combination of these improvements was enough to get a prototype of |
| 141 | +`scalafmt` running on Scala Native by [@olafurpg][@olafurpg], showing a blazing |
| 142 | +fast startup time! |
| 143 | + |
| 144 | +<blockquote class="twitter-tweet" data-conversation="none" data-lang="en"> |
| 145 | + <p lang="en" dir="ltr"> |
| 146 | + <a href="https://twitter.com/adriaanm">@adriaanm</a> |
| 147 | + <a href="https://twitter.com/den_sh">@den_sh</a> |
| 148 | + <a href="https://twitter.com/scala_native">@scala_native</a> OMG IT WORKS :D 30x faster! |
| 149 | + <a href="https://t.co/M7V9udU5bT">pic.twitter.com/M7V9udU5bT</a> |
| 150 | + </p> |
| 151 | + — Ólafur Páll Geirsson (@olafurpg) |
| 152 | + <a href="https://twitter.com/olafurpg/status/857559907876433920">April 27, 2017</a> |
| 153 | +</blockquote> |
| 154 | +<script async src="//platform.twitter.com/widgets.js" charset="utf-8"></script> |
| 155 | + |
| 156 | +## What to expect for Scala Native 0.3? |
| 157 | + |
| 158 | +The plans for the next release of Scala Native include a new garbage collector, |
| 159 | +a better integration with sbt and more additions to the standard library. |
| 160 | + |
| 161 | +### Improved garbage collector |
| 162 | + |
| 163 | +The first releases of Scala Native use Boehm GC. A new and improved garbage |
| 164 | +collector has been under development by [@LukasKellenberger][@LukasKellenberger] |
| 165 | +and will be presented at [Scala Days during Denys' talk][denys-talk]. Stay |
| 166 | +tuned for more more details to come soon! |
| 167 | + |
| 168 | +The pull request introducing the new garbage collector can be found in |
| 169 | +[#726][#726]. |
| 170 | + |
| 171 | +### Running tests from sbt |
| 172 | + |
| 173 | +Currently, testing frameworks such as utest or scalacheck cannot be used with |
| 174 | +Scala Native. An effort to enable support for sbt-compatible testing |
| 175 | +frameworks has been undertaken by [@Duhemm][@Duhemm] from the Scala Center and |
| 176 | +is expected to land in Scala Native's third release. |
| 177 | + |
| 178 | +### Support for `java.nio`, `java.util.{jar, zip}` |
| 179 | + |
| 180 | +The existing I/O capabilities of Scala Native have been extended in this |
| 181 | +release by adding support for the classes defined in the packages `java.nio`, |
| 182 | +`java.util.jar` and `java.util.zip`. |
| 183 | + |
| 184 | +### Smaller binaries |
| 185 | + |
| 186 | +In [#686][#686], [@densh][@densh] started work to reduce the size of the |
| 187 | +binaries compiled by Scala Native, using a technique called selector-based |
| 188 | +row displacement. |
| 189 | + |
| 190 | +These improvements make the dispatch table up to 10 times smaller, on some |
| 191 | +codebases in the wild. |
| 192 | + |
| 193 | +## 0.4 and beyond |
| 194 | + |
| 195 | +Some features are already in the works for Scala Native 0.4. |
| 196 | + |
| 197 | +### Windows support |
| 198 | + |
| 199 | +[@muxanick][@muxanick] has been working on a port of Scala Native to Windows. |
| 200 | +The advancement of his work can be consulted in [#691][#691]. |
| 201 | + |
| 202 | +### Automatic binding generation |
| 203 | + |
| 204 | +A prototype of [automatic binding generation][#642] is in development by |
| 205 | +[@jonas][@jonas]. The goal is to be able to generate automatically bindings |
| 206 | +for C libraries. For instance, given the following C header `test.h`: |
| 207 | + |
| 208 | +```C |
| 209 | +enum color { |
| 210 | + RED = 1, |
| 211 | + GREEN, |
| 212 | + BLUE = 100 |
| 213 | +}; |
| 214 | +typedef int SomeInt; |
| 215 | +char *strchr(const char *s, int c); |
| 216 | +``` |
| 217 | +
|
| 218 | +We want to generate the following definitions: |
| 219 | +
|
| 220 | +```scala |
| 221 | +@extern |
| 222 | +object Test { |
| 223 | + object color { |
| 224 | + val RED = 1 |
| 225 | + val GREEN = 2 |
| 226 | + val BLUE = 100 |
| 227 | + } |
| 228 | + type SomeInt = CInt |
| 229 | + def strchr(s: CString, c: CInt): CString = extern |
| 230 | +} |
| 231 | +``` |
| 232 | + |
| 233 | +Ultimately, this tool will make it much easier to providing bindings for C's |
| 234 | +stdlib and external libraries. |
| 235 | + |
| 236 | +[scala-native-0.2-release]: https://github.com/scala-native/scala-native/releases/tag/v0.2.0 |
| 237 | +[@asoltysik]: https://github.com/asoltysik |
| 238 | +[@brad-rathke]: https://github.com/brad-rathke |
| 239 | +[@cedricviaccoz]: https://github.com/cedricviaccoz |
| 240 | +[@densh]: https://github.com/densh |
| 241 | +[@Duhemm]: https://github.com/Duhemm |
| 242 | +[@ekrich]: https://github.com/ekrich |
| 243 | +[@fduraffourg]: https://github.com/fduraffourg |
| 244 | +[@jonas]: https://github.com/jonas |
| 245 | +[@Korf74]: https://github.com/Korf74 |
| 246 | +[@LukasKellenberger]: https://github.com/LukasKellenberger |
| 247 | +[@MasseGuillaume]: https://github.com/MasseGuillaume |
| 248 | +[@muxanick]: https://github.com/muxanick |
| 249 | +[@olafurpg]: https://github.com/olafurpg |
| 250 | +[@xuwei-k]: https://github.com/xuwei-k |
| 251 | + |
| 252 | +[scala-native-doc-regular-expressions]: http://scala-native.readthedocs.io/en/latest/lib/javalib.html?highlight=regex#regular-expressions-java-util-regex |
| 253 | + |
| 254 | +[#539]: https://github.com/scala-native/scala-native/pull/539 |
| 255 | +[#549]: https://github.com/scala-native/scala-native/pull/549 |
| 256 | +[#562]: https://github.com/scala-native/scala-native/pull/562 |
| 257 | +[#565]: https://github.com/scala-native/scala-native/pull/565 |
| 258 | +[#568]: https://github.com/scala-native/scala-native/pull/568 |
| 259 | +[#573]: https://github.com/scala-native/scala-native/pull/573 |
| 260 | +[#574]: https://github.com/scala-native/scala-native/pull/574 |
| 261 | +[#588]: https://github.com/scala-native/scala-native/pull/588 |
| 262 | +[#591]: https://github.com/scala-native/scala-native/pull/591 |
| 263 | +[#606]: https://github.com/scala-native/scala-native/pull/606 |
| 264 | +[#616]: https://github.com/scala-native/scala-native/pull/616 |
| 265 | +[#618]: https://github.com/scala-native/scala-native/pull/618 |
| 266 | +[#621]: https://github.com/scala-native/scala-native/pull/621 |
| 267 | +[#622]: https://github.com/scala-native/scala-native/pull/621 |
| 268 | +[#624]: https://github.com/scala-native/scala-native/pull/624 |
| 269 | +[#630]: https://github.com/scala-native/scala-native/pull/630 |
| 270 | +[#639]: https://github.com/scala-native/scala-native/pull/639 |
| 271 | +[#642]: https://github.com/scala-native/scala-native/pull/642 |
| 272 | +[#651]: https://github.com/scala-native/scala-native/pull/651 |
| 273 | +[#653]: https://github.com/scala-native/scala-native/pull/653 |
| 274 | +[#659]: https://github.com/scala-native/scala-native/pull/659 |
| 275 | +[#686]: https://github.com/scala-native/scala-native/pull/686 |
| 276 | +[#691]: https://github.com/scala-native/scala-native/pull/691 |
| 277 | +[#726]: https://github.com/scala-native/scal-anative/pull/726 |
| 278 | +[denys-talk]: http://event.scaladays.org/scaladays-cph-2017#!#schedulePopupExtras-8135 |
0 commit comments