AssemblyScript

From HandWiki
Short description: Programming language, variant of TypeScript that compiles to WebAssembly
AssemblyScript
AssemblyScript logo 2020.svg
ParadigmMulti-paradigm: functional, generic, imperative, object-oriented
FamilyJavaScript, TypeScript
Designed byDaniel Wirtz,[1] Max Graey[2]
DeveloperThe AssemblyScript Project[1]
First appeared2017; 7 years ago (2017)[3]
Stable release
0.21.00 / August 21, 2022; 22 months ago (2022-08-21)[4]
Typing disciplineStatic
ScopeLexical
LicenseApache License 2.0[5]
Filename extensions.ts
File formatsText, Unicode (source)
.wasm binary format (object)
Websitewww.assemblyscript.org
Major implementations
asc (AssemblyScript compiler)
Influenced by
JavaScript, TypeScript, WebAssembly

AssemblyScript is a TypeScript-based programming language that is optimized for, and statically compiled to, WebAssembly (currently using asc, the reference AssemblyScript compiler). Resembling ECMAScript and JavaScript, but with static types, the language is developed by the AssemblyScript Project[6] with contributions from the AssemblyScript community.

Overview

In 2017, the availability of support for WebAssembly, a standard definition for a low-level bytecode and an associated virtual machine, became widespread among major web browsers, providing web developers a lower-level and potentially higher-performance compilation target for client-side programs and applications to execute within web browsers, in addition to the interpreted (and in practice dynamically compiled) JavaScript web scripting language.[7] WebAssembly allows programs and code to be statically compiled ahead of time in order to run at potentially native-level or “bare-metal” performance within web browsers, without the overhead of interpretation or the initial latency of dynamic compilation.[8]

With the adoption of WebAssembly in major web browsers, Alon Zakai, creator of Emscripten, an LLVM/Clang-based C and C++ compiler that targeted a subset of JavaScript called asm.js, added support for WebAssembly as a compilation target in Emscripten, allowing C and/or C++ programs and code to be compiled directly to WebAssembly.[9]

While Emscripten and similar compilers allow web developers to write new code, or port existing code, written in a high-level language such as C, C++, Go, and Rust to WebAssembly to achieve potentially higher, native-level execution performance in web browsers, this forces web developers accustomed to developing client-side web scripts and applications in ECMAScript/JavaScript (the de facto client-side programming language in web browsers) to use a different language for targeting WebAssembly than JavaScript. AssemblyScript, as a variant of TypeScript that is syntactically similar to JavaScript, allows developers accustomed to JavaScript to use a familiar language for targeting WebAssembly, potentially reducing the learning curve of a separate language that can be compiled to WebAssembly. Furthermore, because AssemblyScript was designed to be an optimal source language for WebAssembly, the language’s type system closely reflects that of WebAssembly,[10] and the language provides standard low-level functions (typically implemented as macros) that map directly to WebAssembly instructions that mirror instructions available on modern processors such as SIMD and vector instructions and more specialized instructions such as clz (count leading zero bits), ctz (count trailing zero bits), and popcnt (population count), used in applications such as encryption and cryptographic libraries.[11]

asc, the reference AssemblyScript compiler, is based on Binaryen, a back-end compiler toolchain developed by Alon Zakai that compiles to WebAssembly and is a component of Emscripten (which Zakai also developed). The asc compiler and other tooling are available via the npm package manager.

While WebAssembly was originally designed for execution within web browsers, the development of WASI (WebAssembly System Interface), a community specification for a standard API that allows WebAssembly programs access to system calls and other operating system functions,[12] has led to the development of WebAssembly runtime environments from projects such as Wasmtime[13] and Wasmer[14] that allow WebAssembly, and code written in languages such as AssemblyScript that can compile to it, to run in non-web environments as well.

Compatibility with JavaScript

AssemblyScript is compiled to WebAssembly modules, which can then be instantiated into client-side Web pages using standard JavaScript methods such as WebAssembly.compileStreaming and WebAssembly.instantiateStreaming just like standard WebAssembly binaries.[15] Data passing between JavaScript and the compiled WebAssembly modules, as well as function calls between JavaScript and WebAssembly, are then the same as for any WebAssembly module.[16]

Because the AssemblyScript language is largely a subset of TypeScript, it is theoretically possible to write an AssemblyScript program using this subset and compile it to both plain JavaScript and WebAssembly, using the TypeScript compiler and AssemblyScript compiler, respectively. This potentially allows for portable code that can be deployed in either JavaScript or WebAssembly run-time environments.

Usage

(As of July 2022) more than 12,000 projects hosted on GitHub are written, either wholly or partially, in AssemblyScript,[17] with roughly 18,000 downloads of the AssemblyScript compiler per week via npm.[18][19]

In 2021, Webpack started using AssemblyScript to speed up the calculation of hash functions such as xxhash and md4 sources. This also made it possible to get rid of native dependencies.

Reception

Lead Emscripten developer Alon Zakai has characterized AssemblyScript as being “designed with WebAssembly and code size in mind. It’s not an existing language that we are using for a new purpose, but it’s a language designed for WebAssembly. It has great wasm-opt integration—in fact, it’s built with it—and it’s very easy to get good code size.”[20]

Norwegian musician Peter Salomonsen, in a 2020 WebAssembly Summit talk titled, “WebAssembly Music,” demonstrated the use of AssemblyScript for real-time compilation to WebAssembly in live electronic music synthesis, saying, “I chose AssemblyScript because it has high-level readability and low-level control; it’s like a high-level language, but you get that low-level feeling, and you can even write direct WebAssembly intrinsics if you want to.”[21]

Aaron Turner, a senior engineer at Fastly, a cloud computing services provider that uses WebAssembly for the company’s Compute@Edge serverless compute environment, in a review of AssemblyScript wrote:[22]

While AssemblyScript requires stricter typing than TypeScript does, it sticks as close as possible to TypeScript syntax and semantics—which means that most JavaScript developers will find AssemblyScript comfortable to use—and it enables great support for the modern JavaScript ecosystem. For instance, the AssemblyScript compiler is available on npm, as well as common AssemblyScript tools and libraries like as-pect. AssemblyScript files also use TypeScript’s ‘.ts’ file extension, and it includes proper typings for allowing AssemblyScript to piggy-back on TypeScript tooling, such as the TypeScript linter. With the right small tweaks, AssemblyScript can even be used with the TypeScript compiler.

This is very exciting, as AssemblyScript offers a low-overhead entry-point for JavaScript developers to pick up a language to output WebAssembly—both in terms of learning to read and write AssemblyScript, as well as using a lot of the pre-existing tooling that may already be in a JavaScript developer’s workflow. AssemblyScript is often referred to in the WebAssembly community as a great gateway to picking up WebAssembly. It offers a large group of developers who already write applications for the web a path to pick up and learn WebAssembly. Even if you are starting from scratch and are not particularly familiar with JavaScript or TypeScript, AssemblyScript is a solid choice when picking a language to start outputting WebAssembly.

However, Turner went on to cite the language’s relative newness and thus its lack of some features available in larger, more complex and established programming languages as potential but temporary shortcomings of the language.

See also

References

  1. 1.0 1.1 The AssemblyScript Project (2020-04-24). "AssemblyScript Working Group". AssemblyScript Project. https://github.com/AssemblyScript/working-group. "Daniel Wirtz (@dcodeIO) - Author of AssemblyScript" 
  2. Aaron Turner (March 28, 2019). "WebAssembly for Javascript Developers". WebAssembly SF. https://www.youtube.com/watch?v=ZlL1nduatZQ. "@dcodeIO [Daniel Wirtz] and @MaxGraey [Max Graey]—they’re the main two developers of AssemblyScript" 
  3. The AssemblyScript Project (2017). "assemblyscript 0.1.0". AssemblyScript Project. https://www.npmjs.com/package/assemblyscript/v/0.1.0. 
  4. The AssemblyScript Project (2022-08-21). "assemblyscript". The AssemblyScript Project. https://www.npmjs.com/package/assemblyscript. 
  5. The AssemblyScript Project (2017-09-29). "LICENSE". The AssemblyScript Project. https://github.com/AssemblyScript/assemblyscript/blob/master/LICENSE. "AssemblyScript/assemblyscript is licensed under the Apache License 2.0" 
  6. The AssemblyScript Project. "The AssemblyScript Project". The AssemblyScript Project. https://github.com/AssemblyScript. 
  7. WebAssembly Community Group (November 2017). "Roadmap". WebAssembly Community Group. https://webassembly.org/roadmap/. 
  8. WebAssembly Working Group. "WebAssembly". WebAssembly Working Group. https://webassembly.org/. 
  9. Alon Zakai and Emscripten Contributors. "Emscripten". Emscripten project. https://emscripten.org/. 
  10. The AssemblyScript Project. "Types". The AssemblyScript Project. https://www.assemblyscript.org/types.html. 
  11. The AssemblyScript Project. "Environment". The AssemblyScript Project. https://www.assemblyscript.org/environment.html. 
  12. The Wasmtime Project. "WASI: The WebAssembly System Interface". The Wasmtime Project. https://wasi.dev/. 
  13. The Wasmtime Project. "Wasmtime: A small and efficient runtime for WebAssembly & WASI". The Wasmtime Project. https://wasmtime.dev/. 
  14. Wasmer, Inc.. "Wasmer: The Universal WebAssembly Runtime". Wasmer, Inc.. https://wasmer.io/. 
  15. Mozilla Developer Network. "Loading and running WebAssembly code". Mozilla Developer Network. https://developer.mozilla.org/en-US/docs/WebAssembly/Loading_and_running. 
  16. Mozilla Developer Network. "Using the WebAssembly JavaScript API". Mozilla Developer Network. https://developer.mozilla.org/en-US/docs/WebAssembly/Using_the_JavaScript_API. 
  17. GitHub. "AssemblyScript/assemblyscript Dependency graph". GitHub. https://github.com/AssemblyScript/assemblyscript/network/dependents?package_id=UGFja2FnZS0xODY0NzM4NQ%3D%3D. 
  18. npmjs. "assemblyscript Weekly Downloads". npmjs.com. https://www.npmjs.com/package/assemblyscript. 
  19. "Resolve Error In Typescript". June 3, 2022. https://ittutoria.net/resolve-error-in-typescript-object-is-possibly-undefined/.  Saturday, 7 May 2022
  20. Alon Zakai (2020-02-19). "Shipping Tiny WebAssembly Builds". WebAssembly-Summit.org. https://www.youtube.com/watch?v=_lLqZR4ufSI. 
  21. Peter Salomonsen (2020-02-19). "WebAssembly Music". WebAssembly-Summit.org. https://www.youtube.com/watch?v=C8j_ieOm4vE. 
  22. Aaron Turner (2020-10-29). "Meet AssemblyScript: your next computing language". Fastly. https://www.fastly.com/blog/meet-assemblyscript-your-next-computing-language. 

External links