Skip to content

Commit a196f4d

Browse files
committed
Merge remote-tracking branch 'origin/master' into release
2 parents 3b2d41f + ccb1296 commit a196f4d

File tree

114 files changed

+10757
-6591
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

114 files changed

+10757
-6591
lines changed

.github/workflows/ci.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,3 +141,20 @@ jobs:
141141
cd lib/loader
142142
npm run asbuild
143143
npm run test
144+
test-bootstrap:
145+
name: "Test self-compilation on node: node"
146+
runs-on: ubuntu-latest
147+
needs: check
148+
steps:
149+
- uses: actions/[email protected]
150+
- uses: dcodeIO/[email protected]
151+
with:
152+
node-version: node
153+
- name: Install dependencies
154+
run: npm ci --no-audit
155+
- name: Clean distribution files
156+
run: npm run clean
157+
- name: Test self-compilation
158+
run: |
159+
npm run asbuild
160+
npm run astest

cli/asc.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/**
2+
* @fileoverview Definitions for asc.
3+
* @license Apache-2.0
4+
*/
5+
16
import { OptionDescription } from "./util/options";
27
export { OptionDescription };
38

cli/asc.js

Lines changed: 47 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,31 @@
1-
"use strict";
21
/**
3-
* Compiler frontend for node.js
2+
* @license
3+
* Copyright 2020 Daniel Wirtz / The AssemblyScript Authors.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
* SPDX-License-Identifier: Apache-2.0
18+
*/
19+
20+
/**
21+
* @fileoverview Compiler frontend for node.js
422
*
523
* Uses the low-level API exported from src/index.ts so it works with the compiler compiled to
624
* JavaScript as well as the compiler compiled to WebAssembly (eventually). Runs the sources
725
* directly through ts-node if distribution files are not present (indicated by a `-dev` version).
826
*
927
* Can also be packaged as a bundle suitable for in-browser use with the standard library injected
1028
* in the build step. See dist/asc.js for the bundle and webpack.config.js for building details.
11-
*
12-
* @module cli/asc
1329
*/
1430

1531
// Use "." instead of "/" as cwd in browsers
@@ -390,7 +406,8 @@ exports.main = function main(argv, options, callback) {
390406
if ((sourceText = readFile(sourcePath = internalPath + ".ts", baseDir)) == null) {
391407
if ((sourceText = readFile(sourcePath = internalPath + "/index.ts", baseDir)) == null) {
392408
// portable d.ts: uses the .js file next to it in JS or becomes an import in Wasm
393-
sourceText = readFile(sourcePath = internalPath + ".d.ts", baseDir);
409+
sourcePath = internalPath + ".ts";
410+
sourceText = readFile(internalPath + ".d.ts", baseDir);
394411
}
395412
}
396413

@@ -478,13 +495,16 @@ exports.main = function main(argv, options, callback) {
478495
var internalPath;
479496
while ((internalPath = assemblyscript.nextFile(program)) != null) {
480497
let file = getFile(internalPath, assemblyscript.getDependee(program, internalPath));
481-
if (!file) return callback(Error("Import file '" + internalPath + ".ts' not found."))
498+
if (!file) return callback(Error("Import '" + internalPath + "' not found."))
482499
stats.parseCount++;
483500
stats.parseTime += measure(() => {
484501
assemblyscript.parse(program, file.sourceText, file.sourcePath, false);
485502
});
486503
}
487-
if (checkDiagnostics(program, stderr)) return callback(Error("Parse error"));
504+
var numErrors = checkDiagnostics(program, stderr);
505+
if (numErrors) {
506+
return callback(Error(numErrors + " parse error(s)"));
507+
}
488508
}
489509

490510
// Include runtime template before entry files so its setup runs first
@@ -570,6 +590,20 @@ exports.main = function main(argv, options, callback) {
570590
optimizeLevel = Math.min(Math.max(optimizeLevel, 0), 3);
571591
shrinkLevel = Math.min(Math.max(shrinkLevel, 0), 2);
572592

593+
try {
594+
stats.compileTime += measure(() => {
595+
assemblyscript.initializeProgram(program, compilerOptions);
596+
});
597+
} catch(e) {
598+
return callback(e);
599+
}
600+
601+
// Call afterInitialize transform hook
602+
{
603+
let error = applyTransform("afterInitialize", program);
604+
if (error) return callback(error);
605+
}
606+
573607
var module;
574608
stats.compileCount++;
575609
try {
@@ -579,9 +613,10 @@ exports.main = function main(argv, options, callback) {
579613
} catch (e) {
580614
return callback(e);
581615
}
582-
if (checkDiagnostics(program, stderr)) {
616+
var numErrors = checkDiagnostics(program, stderr);
617+
if (numErrors) {
583618
if (module) module.dispose();
584-
return callback(Error("Compile error"));
619+
return callback(Error(numErrors + " compile error(s)"));
585620
}
586621

587622
// Call afterCompile transform hook
@@ -1023,17 +1058,17 @@ exports.main = function main(argv, options, callback) {
10231058
/** Checks diagnostics emitted so far for errors. */
10241059
function checkDiagnostics(program, stderr) {
10251060
var diagnostic;
1026-
var hasErrors = false;
1061+
var numErrors = 0;
10271062
while ((diagnostic = assemblyscript.nextDiagnostic(program)) != null) {
10281063
if (stderr) {
10291064
stderr.write(
10301065
assemblyscript.formatDiagnostic(diagnostic, stderr.isTTY, true) +
10311066
EOL + EOL
10321067
);
10331068
}
1034-
if (assemblyscript.isError(diagnostic)) hasErrors = true;
1069+
if (assemblyscript.isError(diagnostic)) ++numErrors;
10351070
}
1036-
return hasErrors;
1071+
return numErrors;
10371072
}
10381073

10391074
exports.checkDiagnostics = checkDiagnostics;

cli/transform.d.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
2-
* Definitions for custom compiler transforms that can be applied with the `--transform` option.
3-
* @module cli/transform
4-
*//***/
2+
* @fileoverview Compiler transform interface definitions.
3+
* @license Apache-2.0
4+
*/
55

66
import { Program, Parser, Module } from "..";
77
import { OutputStream } from "./asc";
@@ -35,6 +35,9 @@ export abstract class Transform {
3535
/** Called when parsing is complete, before a program is instantiated from the AST. */
3636
afterParse?(parser: Parser): void;
3737

38+
/** Called after the program is instantiated. */
39+
afterInitialize?(program: Program): void;
40+
3841
/** Called when compilation is complete, before the module is being validated. */
3942
afterCompile?(module: Module): void;
4043
}

cli/transform.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
1+
/**
2+
* @fileoverview Compiler transform interface.
3+
* @license Apache-2.0
4+
*/
5+
16
// becomes replaced with the actual base by asc
27
exports.Transform = function Transform() {};

cli/util/colors.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/**
2+
* @fileoverview Terminal colors utility definitions.
3+
* @license Apache-2.0
4+
*/
5+
16
interface Colors {
27
/** Whether terminal colors are supported. */
38
supported: boolean;

cli/util/colors.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/**
2+
* @fileoverview Terminal colors utility.
3+
* @license Apache-2.0
4+
*/
5+
16
var proc = typeof process !== "undefined" && process || {};
27
var isCI = proc.env && "CI" in proc.env; // doesn't work when bundled because 'process' is a mock
38

cli/util/find.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/**
2+
* @fileoverview File finding utility definitions.
3+
* @license Apache-2.0
4+
*/
5+
16
export function files(dirname: string, filter?: ((name: string) => bool) | RegExp): string[];
27
export const TS: RegExp;
38
export const TS_EXCEPT_DTS: RegExp;

cli/util/find.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/**
2+
* @fileoverview File finding utility.
3+
* @license Apache-2.0
4+
*/
5+
16
const fs = require("fs");
27
const path = require("path");
38

cli/util/mkdirp.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/**
2+
* @fileoverview Recursive mkdir definitions.
3+
* @license Apache-2.0
4+
*/
5+
16
interface Options {
27
mode?: number;
38
}

cli/util/mkdirp.js

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,28 @@
1-
/*
2-
Copyright 2010 James Halliday ([email protected])
3-
4-
This project is free software released under the MIT/X11 license:
5-
6-
Permission is hereby granted, free of charge, to any person obtaining a copy
7-
of this software and associated documentation files (the "Software"), to deal
8-
in the Software without restriction, including without limitation the rights
9-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10-
copies of the Software, and to permit persons to whom the Software is
11-
furnished to do so, subject to the following conditions:
12-
13-
The above copyright notice and this permission notice shall be included in
14-
all copies or substantial portions of the Software.
15-
16-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22-
THE SOFTWARE.
23-
*/
1+
/**
2+
* @fileoverview Recursive mkdir.
3+
* @license
4+
* Copyright 2010 James Halliday ([email protected])
5+
*
6+
* This project is free software released under the MIT/X11 license:
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
2426

2527
var path = require("path");
2628
var fs = require("fs");

cli/util/options.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/**
2+
* @fileoverview Command line options utility definitions.
3+
* @license Apache-2.0
4+
*/
5+
16
/** Command line option description. */
27
export interface OptionDescription {
38
/** Textual description. */

cli/util/options.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/**
2+
* @fileoverview Command line options utility.
3+
* @license Apache-2.0
4+
*/
5+
16
const colorsUtil = require("./colors");
27

38
// type | meaning

cli/util/utf8.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/**
2+
* @fileoverview UTF8 utility definitions.
3+
* @license Apache-2.0
4+
*/
5+
16
/**
27
* Calculates the UTF8 byte length of a string.
38
* @param {string} string String

cli/util/utf8.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/**
2+
* @fileoverview UTF8 utility.
3+
* @license Apache-2.0
4+
*/
5+
16
// @protobufjs/utf8
27

38
/**

examples/game-of-life/assembly/index.ts

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,43 +3,49 @@
33
// Configuration imported from JS
44
import { BGR_ALIVE, BGR_DEAD, BIT_ROT } from "./config";
55

6-
var w: i32, h: i32, s: i32;
6+
var width: i32, height: i32, offset: i32;
77

88
/** Gets an input pixel in the range [0, s]. */
99
@inline
1010
function get(x: u32, y: u32): u32 {
11-
return load<u32>((y * w + x) << 2);
11+
return load<u32>((y * width + x) << 2);
1212
}
1313

1414
/** Sets an output pixel in the range [s, 2*s]. */
1515
@inline
1616
function set(x: u32, y: u32, v: u32): void {
17-
store<u32>((s + y * w + x) << 2, v);
17+
store<u32>((offset + y * width + x) << 2, v);
1818
}
1919

2020
/** Sets an output pixel in the range [s, 2*s] while fading it out. */
2121
@inline
2222
function rot(x: u32, y: u32, v: u32): void {
23-
var a = max<i32>((v >>> 24) - BIT_ROT, 0);
24-
set(x, y, (a << 24) | (v & 0x00ffffff));
23+
var alpha = max<i32>((v >> 24) - BIT_ROT, 0);
24+
set(x, y, (alpha << 24) | (v & 0x00ffffff));
2525
}
2626

2727
/** Initializes width and height. Called once from JS. */
28-
export function init(width: i32, height: i32): void {
29-
w = width;
30-
h = height;
31-
s = width * height;
28+
export function init(w: i32, h: i32): void {
29+
width = w;
30+
height = h;
31+
offset = w * h;
3232

3333
// Start by filling output with random live cells.
3434
for (let y = 0; y < h; ++y) {
3535
for (let x = 0; x < w; ++x) {
36-
set(x, y, Math.random() > 0.1 ? BGR_DEAD & 0x00ffffff : BGR_ALIVE | 0xff000000);
36+
let c = Math.random() > 0.1
37+
? BGR_DEAD & 0x00ffffff
38+
: BGR_ALIVE | 0xff000000;
39+
set(x, y, c);
3740
}
3841
}
3942
}
4043

4144
/** Performs one step. Called about 30 times a second from JS. */
4245
export function step(): void {
46+
var w = width,
47+
h = height;
48+
4349
var hm1 = h - 1, // h - 1
4450
wm1 = w - 1; // w - 1
4551

@@ -55,9 +61,9 @@ export function step(): void {
5561
// Every cell interacts with its eight neighbours, which are the cells that are horizontally,
5662
// vertically, or diagonally adjacent. Least significant bit indicates alive or dead.
5763
let aliveNeighbors = (
58-
(get(xm1, ym1) & 1) + (get(x , ym1) & 1) + (get(xp1, ym1) & 1) +
59-
(get(xm1, y ) & 1) + (get(xp1, y ) & 1) +
60-
(get(xm1, yp1) & 1) + (get(x , yp1) & 1) + (get(xp1, yp1) & 1)
64+
(get(xm1, ym1) & 1) + (get(x, ym1) & 1) + (get(xp1, ym1) & 1) +
65+
(get(xm1, y ) & 1) + (get(xp1, y ) & 1) +
66+
(get(xm1, yp1) & 1) + (get(x, yp1) & 1) + (get(xp1, yp1) & 1)
6167
);
6268

6369
let self = get(x, y);
@@ -78,10 +84,10 @@ export function step(): void {
7884

7985
/** Fills the row and column indicated by `x` and `y` with random live cells. */
8086
export function fill(x: u32, y: u32, p: f64): void {
81-
for (let ix = 0; ix < w; ++ix) {
87+
for (let ix = 0; ix < width; ++ix) {
8288
if (Math.random() < p) set(ix, y, BGR_ALIVE | 0xff000000);
8389
}
84-
for (let iy = 0; iy < h; ++iy) {
90+
for (let iy = 0; iy < height; ++iy) {
8591
if (Math.random() < p) set(x, iy, BGR_ALIVE | 0xff000000);
8692
}
8793
}

0 commit comments

Comments
 (0)