Skip to content

Commit 1acc1ed

Browse files
committedJun 10, 2020
Merge remote-tracking branch 'origin/master' into release
2 parents 673f04f + 42f6c85 commit 1acc1ed

18 files changed

+480
-260
lines changed
 

‎NOTICE

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ under the licensing terms detailed in LICENSE:
2222
* Duncan Uszkay <duncan.uszkay@shopify.com>
2323
* Surma <surma@surma.dev>
2424
* Julien Letellier <letellier.julien@gmail.com>
25+
* Guido Zuidhof <me@guido.io>
2526

2627
Portions of this software are derived from third-party works licensed under
2728
the following terms:

‎README.md

+23-47
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,34 @@
1-
![](https://avatars1.githubusercontent.com/u/28916798?s=64) AssemblyScript
2-
=================
3-
4-
[![Actions Status](https://github.com/AssemblyScript/assemblyscript/workflows/Test/badge.svg?branch=master&event=push)](https://github.com/AssemblyScript/assemblyscript/actions) [![npm](https://img.shields.io/npm/v/assemblyscript.svg?color=0074C1)](https://www.npmjs.com/package/assemblyscript) [![npm@nightly](https://img.shields.io/npm/v/assemblyscript/nightly.svg?color=0074C1)](https://www.npmjs.com/package/assemblyscript)
5-
6-
**AssemblyScript** compiles a strict subset of [TypeScript](http://www.typescriptlang.org) (basically JavaScript with types) to [WebAssembly](http://webassembly.org) using [Binaryen](https://github.com/WebAssembly/binaryen). It generates lean and mean WebAssembly modules while being just an `npm install` away.
7-
8-
Check out the [documentation](https://docs.assemblyscript.org) or try it out in [WebAssembly Studio](https://webassembly.studio)!
9-
10-
---
11-
12-
<h3 align="center">Our Sponsors</h3>
131
<p align="center">
14-
<a href="https://opencollective.com/assemblyscript/tiers/sponsor/0/website" target="_blank"><img src="https://opencollective.com/assemblyscript/tiers/sponsor/0/avatar.svg"></a>
15-
<a href="https://opencollective.com/assemblyscript/tiers/sponsor/1/website" target="_blank"><img src="https://opencollective.com/assemblyscript/tiers/sponsor/1/avatar.svg"></a>
16-
<a href="https://opencollective.com/assemblyscript/tiers/sponsor/2/website" target="_blank"><img src="https://opencollective.com/assemblyscript/tiers/sponsor/2/avatar.svg"></a>
17-
<a href="https://opencollective.com/assemblyscript/tiers/sponsor/3/website" target="_blank"><img src="https://opencollective.com/assemblyscript/tiers/sponsor/3/avatar.svg"></a>
18-
<a href="https://opencollective.com/assemblyscript/tiers/sponsor/4/website" target="_blank"><img src="https://opencollective.com/assemblyscript/tiers/sponsor/4/avatar.svg"></a>
19-
<a href="https://opencollective.com/assemblyscript/tiers/sponsor/5/website" target="_blank"><img src="https://opencollective.com/assemblyscript/tiers/sponsor/5/avatar.svg"></a>
20-
<a href="https://opencollective.com/assemblyscript/tiers/sponsor/6/website" target="_blank"><img src="https://opencollective.com/assemblyscript/tiers/sponsor/6/avatar.svg"></a>
21-
<a href="https://opencollective.com/assemblyscript/tiers/sponsor/7/website" target="_blank"><img src="https://opencollective.com/assemblyscript/tiers/sponsor/7/avatar.svg"></a>
22-
<a href="https://opencollective.com/assemblyscript/tiers/sponsor/8/website" target="_blank"><img src="https://opencollective.com/assemblyscript/tiers/sponsor/8/avatar.svg"></a>
23-
<a href="https://opencollective.com/assemblyscript/tiers/sponsor/9/website" target="_blank"><img src="https://opencollective.com/assemblyscript/tiers/sponsor/9/avatar.svg"></a>
2+
<a href="https://assemblyscript.org" target="_blank" rel="noopener"><img width="100" src="https://avatars1.githubusercontent.com/u/28916798?s=200&v=4" alt="AssemblyScript logo"></a>
243
</p>
25-
<br />
26-
<h3 align="center">Our Backers</h3>
27-
<p align="center">
28-
<a href="https://opencollective.com/assemblyscript#backers" target="_blank"><img src="https://opencollective.com/assemblyscript/backer.svg?avatarHeight=44" /></a>
29-
</p>
30-
31-
The core team members and most contributors do this open source work in their free time. If you use AssemblyScript for a serious task or plan to do so, and you'd like us to invest more time on it, [please donate to our OpenCollective](https://opencollective.com/assemblyscript). By sponsoring this project, your logo will show up above. Thank you so much for your support!
32-
33-
---
344

35-
Motivation
36-
----------
5+
<h1 align="center">AssemblyScript Compiler</h1>
376

38-
> You are now able to write WebAssembly, without learning a new language, and harness all these benefits WebAssembly might offer you. I think that is kind of powerful. [...] It [AssemblyScript] is absolutely usable, and very enjoyable! - Surma, [WebAssembly for Web Developers (Google I/O ’19)](https://youtu.be/njt-Qzw0mVY) (May 8th, 2019)
39-
40-
> AssemblyScript was frictionless. Not only does it allow you to use TypeScript to write WebAssembly, [...] it also produces glue-free WebAssembly modules that are very small with decent performance. – Surma, [Replacing a hot path in your app's JavaScript with WebAssembly](https://developers.google.com/web/updates/2019/02/hotpath-with-wasm) (Feb 16, 2019)
7+
<p align="center">
8+
<a href="https://github.com/AssemblyScript/assemblyscript/actions"><img src="https://github.com/AssemblyScript/assemblyscript/workflows/Test/badge.svg?branch=master&event=push" alt="Actions Status" /></a>
9+
<a href="https://www.npmjs.com/package/assemblyscript"><img src="https://img.shields.io/npm/v/assemblyscript.svg?color=0074C1" alt="npm package" /></a>
10+
<a href="https://www.npmjs.com/package/assemblyscript"><img src="https://img.shields.io/npm/v/assemblyscript/nightly.svg?color=0074C1" alt="npm package (nightly)" /></a>
11+
</p>
4112

42-
> Perhaps the fundamental issue [to get a small .wasm file] is that JavaScript is the only language for which the Web runtime is a perfect fit. Close relatives that were designed to compile to it, like TypeScript, can be very efficient as well. But languages like C, C++, Rust, and so forth were not originally designed for that purpose. – Alon Zakai, [Small WebAssembly Binaries with Rust + Emscripten](https://kripken.github.io/blog/binaryen/2018/04/18/rust-emscripten.html) (Apr 18, 2018)
13+
<p align="justify">AssemblyScript compiles a strict variant of <a href="http://www.typescriptlang.org">TypeScript</a> (basically JavaScript with types) to <a href="http://webassembly.org">WebAssembly</a> using <a href="https://github.com/WebAssembly/binaryen">Binaryen</a>. It generates lean and mean WebAssembly modules while being just an <code>npm install</code> away.</p>
4314

44-
> JavaScript's heyday as the only browser language is over, but most web developers are used to writing JavaScript, and learning a new syntax just to get access to WebAssembly is not (always) ideal. If only there was something in to bridge the gap… – Jani Tarvainen, [TypeScript is the bridge between JavaScript and WebAssembly](https://malloc.fi/typescript-bridge-javascript-webassembly) (Feb 20, 2018)
15+
<p align="center">
16+
<a href="https://assemblyscript.org">About</a> ·
17+
<a href="https://assemblyscript.org/introduction.html">Introduction</a> ·
18+
<a href="https://assemblyscript.org/quick-start.html">Quick start</a> ·
19+
<a href="https://assemblyscript.org/development.html">Development instructions</a>
20+
</p>
4521

46-
> I do think [compiling TypeScript into WASM] is tremendously useful. It allows JavaScript developers to create WASM modules without having to learn C. – Colin Eberhardt, [Exploring different approaches to building WebAssembly modules](http://blog.scottlogic.com/2017/10/17/wasm-mandelbrot.html) (Oct 17, 2017)
22+
<h2 align="center">Contributors</h2>
4723

48-
Further resources
49-
-----------------
24+
<p align="center">
25+
<a href="https://assemblyscript.org/#contributors"><img src="https://assemblyscript.org/contributors.svg" alt="Contributor logos" /></a>
26+
</p>
5027

51-
* [Documentation](https://docs.assemblyscript.org)<br />
52-
Introduction, quick start, examples and general usage instructions.
28+
<h2 align="center">Thanks to our sponsors!</h2>
5329

54-
* [Development instructions](https://docs.assemblyscript.org/details/development)<br />
55-
How to set up a development environment (to submit a pull request).
30+
<p align="justify">Most of the core team members and most contributors do this open source work in their free time. If you use AssemblyScript for a serious task or plan to do so, and you'd like us to invest more time on it, <a href="https://opencollective.com/assemblyscript/donate" target="_blank" rel="noopener">please donate</a> to our <a href="https://opencollective.com/assemblyscript" target="_blank" rel="noopener">OpenCollective</a>. By sponsoring this project, your logo will show up below. Thank you so much for your support!</p>
5631

57-
* [Project governance](https://github.com/AssemblyScript/working-group)<br />
58-
Discussions, goals, roadmaps, assets, etc. related to development and project organization.
32+
<p align="center">
33+
<a href="https://assemblyscript.org/#sponsors"><img src="https://assemblyscript.org/sponsors.svg" alt="Sponsor logos" /></a>
34+
</p>

‎examples/README.md

-1
This file was deleted.

‎lib/loader/README.md

+5-188
Original file line numberDiff line numberDiff line change
@@ -1,191 +1,8 @@
1-
![AS](https://avatars1.githubusercontent.com/u/28916798?s=48) AssemblyScript Loader
2-
======================
1+
# AssemblyScript Loader
32

4-
A convenient loader for AssemblyScript modules. Demangles module exports to a friendly object structure compatible with WebIDL and TypeScript definitions and provides some useful utility to read/write data from/to memory.
3+
<a href="https://www.npmjs.com/package/@assemblyscript/loader"><img src="https://img.shields.io/npm/v/@assemblyscript/loader.svg?color=0074C1" alt="npm package" /></a>
4+
<a href="https://www.npmjs.com/package/@assemblyscript/loader"><img src="https://img.shields.io/npm/v/@assemblyscript/loader/nightly.svg?color=0074C1" alt="npm package (nightly)" /></a>
55

6-
Usage
7-
-----
6+
A convenient loader for [AssemblyScript](https://assemblyscript.org) modules. Demangles module exports to a friendly object structure compatible with TypeScript definitions and provides useful utility to read/write data from/to memory.
87

9-
```js
10-
const loader = require("@assemblyscript/loader");
11-
...
12-
```
13-
14-
API
15-
---
16-
17-
* **instantiate**<`T`>(moduleOrBuffer: `WebAssembly.Module | BufferSource | Response | PromiseLike<WebAssembly.Module | BufferSource | Response>`, imports?: `WasmImports`): `Promise<ASUtil & T>`<br />
18-
Asynchronously instantiates an AssemblyScript module from anything that can be instantiated.
19-
20-
* **instantiateSync**<`T`>(moduleOrBuffer: `WebAssembly.Module | BufferSource`, imports?: `WasmImports`): `ASUtil & T`<br />
21-
Synchronously instantiates an AssemblyScript module from a WebAssembly.Module or binary buffer.
22-
23-
* **instantiateStreaming**<`T`>(response: `Response | PromiseLike<Response>`, imports?: `WasmImports`): `Promise<ASUtil & T>`<br />
24-
Asynchronously instantiates an AssemblyScript module from a response, i.e. as obtained by `fetch`.
25-
26-
* **demangle**<`T`>(exports: `WasmExports`, baseModule?: `Object`): `T`<br />
27-
Demangles an AssemblyScript module's exports to a friendly object structure. You usually don't have to call this manually as instantiation does this implicitly.
28-
29-
**Note** that `T` above can either be omitted if the structure of the module is unknown, or can reference a `.d.ts` (i.e. `typeof MyModule`) as produced by the compiler with the `-d` option.
30-
31-
Besides demangling classes exported from your entry file to a handy object structure one can use like JS objects, instances are automatically populated with useful utility:
32-
33-
* **_start**(): `void`<br />
34-
Explicit start function if the `--explicitStart` option is used. Must be called before any other exports if present.
35-
36-
* **__allocString**(str: `string`): `number`<br />
37-
Allocates a new string in the module's memory and returns a reference (pointer) to it.
38-
39-
```ts
40-
var ptr = module.__retain(module.__allocString("hello world"));
41-
...
42-
module.__release(ptr);
43-
```
44-
45-
* **__getString**(ptr: `number`): `string`<br />
46-
Copies a string's value from the module's memory. `ptr` must not be zero.
47-
48-
```ts
49-
var str = module.__getString(ptr);
50-
...
51-
```
52-
53-
* **__getArrayBuffer**(ptr: `number`): `ArrayBuffer`<br />
54-
Copies an ArrayBuffer's value from the module's memory. `ptr` must not be zero.
55-
56-
* **__getArray**(ptr: `number`): `number[]`<br />
57-
Copies an array's values from the module's memory. Infers the array type from RTTI. `ptr` must not be zero.
58-
59-
```ts
60-
var arr = module.__getArray(ptr);
61-
...
62-
```
63-
64-
If the type of the array is known beforehand, the following slightly faster helpers that don't infer the type can be used:
65-
66-
**__getInt8Array**(ptr: `number`): `Int8Array`<br />
67-
**__getUint8Array**(ptr: `number`): `Uint8Array`<br />
68-
**__getUint8ClampedArray**(ptr: `number`): `Uint8ClampedArray`<br />
69-
**__getInt16Array**(ptr: `number`): `Int16Array`<br />
70-
**__getUint16Array**(ptr: `number`): `Uint16Array`<br />
71-
**__getInt32Array**(ptr: `number`): `Int32Array`<br />
72-
**__getUint32Array**(ptr: `number`): `Uint32Array`<br />
73-
**__getInt64Array**(ptr: `number`): `BigInt64Array`<br />
74-
**__getUint64Array**(ptr: `number`): `BigUint64Array`<br />
75-
**__getFloat32Array**(ptr: `number`): `Float32Array`<br />
76-
**__getFloat64Array**(ptr: `number`): `Float64Array`
77-
78-
* **__getArrayView**(ptr: `number`): `TypedArray`<br />
79-
Gets a live view on the values of an array in the module's memory. Infers the array type from RTTI. `ptr` must not be zero.
80-
81-
This differs from `__getArray` in that the data isn't copied but remains *live* in both directions. That's faster but also unsafe because if the array grows or becomes released, the view will no longer represent the correct memory region and modifying its values in this state will most likely corrupt memory. Use, but use with care.
82-
83-
If the type of the array is known beforehand, the following slightly faster helpers that don't infer the type can be used:
84-
85-
**__getInt8ArrayView**(ptr: `number`): `Int8Array`<br />
86-
**__getUint8ArrayView**(ptr: `number`): `Uint8Array`<br />
87-
**__getUint8ClampedArrayView**(ptr: `number`): `Uint8ClampedArray`<br />
88-
**__getInt16ArrayView**(ptr: `number`): `Int16Array`<br />
89-
**__getUint16ArrayView**(ptr: `number`): `Uint16Array`<br />
90-
**__getInt32ArrayView**(ptr: `number`): `Int32Array`<br />
91-
**__getUint32ArrayView**(ptr: `number`): `Uint32Array`<br />
92-
**__getInt64ArrayView**(ptr: `number`): `BigInt64Array`<br />
93-
**__getUint64ArrayView**(ptr: `number`): `BigUint64Array`<br />
94-
**__getFloat32ArrayView**(ptr: `number`): `Float32Array`<br />
95-
**__getFloat64ArrayView**(ptr: `number`): `Float64Array`
96-
97-
* **__retain**(ptr: `number`): `number`<br />
98-
Retains a reference to a managed object externally, making sure that it doesn't become collected prematurely. Returns the pointer.
99-
100-
* **__release**(ptr: `number`): `void`<br />
101-
Releases a previously retained reference to a managed object, allowing the runtime to collect it once its reference count reaches zero.
102-
103-
* **__alloc**(size: `number`, id: `number`): `number`<br />
104-
Allocates an instance of the class represented by the specified id. If you are using `MyClass` for example, the best way to know the id and the necessary size is an `export const MYCLASS_ID = idof<MyClass>()` and an `export const MYCLASS_SIZE = offsetof<MyClass>()`. Afterwards, use the respective views to assign values to the class's memory while making sure to retain interior references to other managed objects once. When done with the class, make sure to release it, which will automatically release any interior references once the class becomes collected.
105-
106-
```ts
107-
var ptr = module.__retain(module.__alloc(module.MYCLASS_SIZE, module.MYCLASS_ID));
108-
const F32 = new Float32Array(module.memory.buffer);
109-
F32[ptr + MYCLASS_BASICFIELD1_OFFSET >>> 2] = field1_value_f32;
110-
const U32 = new Uint32Array(module.memory.buffer);
111-
U32[ptr + MYCLASS_MANAGEDFIELD2_OFFSET >>> 2] = module.__retain(field2_value_ptr);
112-
...
113-
module.__release(ptr);
114-
```
115-
116-
* **__allocArray**(id: `number`, values: `number[]`): `number`<br />
117-
Allocates a new array in the module's memory and returns a reference (pointer) to it.
118-
Automatically retains interior pointers. The `id` is the unique runtime id of the respective array class. If you are using `Int32Array` for example, the best way to know the id is an `export const INT32ARRAY_ID = idof<Int32Array>()`. When done with the array, make sure to release it.
119-
120-
```ts
121-
var ptr = module.__retain(module.__allocArray(module.INT32ARRAY, [1, 2, 3]));
122-
...
123-
module.__release(ptr);
124-
```
125-
126-
* **__instanceof**(ptr: `number`, baseId: `number`): `boolean`<br />
127-
Tests whether an object is an instance of the class represented by the specified base id.
128-
129-
```ts
130-
if (module.__instanceof(ptr, module.MYCLASS_ID)) {
131-
...
132-
}
133-
```
134-
135-
* **__collect**(): `void`<br />
136-
Forces a cycle collection. Only relevant if objects potentially forming reference cycles are used.
137-
138-
**Note** that the views like `I32` above will automatically be updated when the module's memory grows. Don't cache these if this can happen.
139-
140-
**Note** that the allocation and ownership features above require the `full` (this is the default) or the `stub` runtime to be present in your module. Other runtime variations do not export this functionality without further ado (so the compiler can eliminate what's dead code).
141-
142-
**Note** that references returned from exported functions have already been retained for you and the runtime expects that you release them once not needed anymore. This is also true for constructors and getters.
143-
144-
**Beware that your module is likely going to explode with seemingly random errors when using the allocation and ownership features incorrectly!**
145-
146-
* Use the correct ids, sizes and layouts (C-style non-packed, export `offsetof<MyClass>("myField")` in case of doubt)
147-
* Clear unused memory to zero
148-
* Retain what you allocate
149-
* Retain interior pointers (except in `__allocArray`)
150-
* Don't retain what's returned (is already retained for you)
151-
* Release when you're done with something and don't ever use it again
152-
153-
Examples
154-
--------
155-
156-
### Instantiating a module
157-
158-
The **asynchronous API** is analogous to [WebAssembly.instantiate](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/instantiate) and [WebAssembly.instantiateStreaming](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/instantiateStreaming)
159-
160-
```js
161-
const myModule = await loader.instantiate(myModuleBuffer, myImports);
162-
const myModule = await loader.instantiateStreaming(fetch("myModule.wasm"), myImports);
163-
```
164-
165-
with `loader.instantiate` actually accepting anything that can be instantiated for convenience:
166-
167-
```js
168-
const myModule = await loader.instantiate(fs.promises.readFile("myModule.wasm"), myImports);
169-
const myModule = await loader.instantiate(fetch("myModule.wasm"), myImports);
170-
...
171-
```
172-
173-
If `WebAssembly.instantiateStreaming` is not supported by the environment a fallback is applied.
174-
175-
The **synchronous API** utilizes [new WebAssembly.Instance](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/Instance#Constructor_Syntax) and [new WebAssembly.Module](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/Module#Constructor_Syntax), which is useful if the goal is to immediately re-export as a node module for example:
176-
177-
```js
178-
module.exports = loader.instantiateSync(fs.readFileSync("myModule.wasm"), myImports);
179-
```
180-
181-
Note, though, that browsers have relatively tight limits for synchronous compilation and instantiation because these block the main thread, hence it is recommended to use the asynchronous API in browsers.
182-
183-
### Usage with TypeScript definitions produced by the compiler
184-
185-
The compiler is able to emit definitions using the `-d` command line option that are compatible with modules demangled by the loader, and these can be used for proper typings in development:
186-
187-
```ts
188-
import MyModule from "myModule"; // pointing at the d.ts
189-
190-
const myModule = await loader.instatiate<typeof MyModule>(fs.promises.readFile("myModule.wasm"), myImports);
191-
```
8+
[Documentation](https://assemblyscript.org/loader.html)

‎lib/parse/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# ![AS](https://avatars1.githubusercontent.com/u/28916798?s=48) parse
1+
# WebAssembly Parser
22

33
A WebAssembly binary parser in WebAssembly. Super small, super fast, with TypeScript support.
44

0 commit comments

Comments
 (0)
Please sign in to comment.