Skip to content

Implement reference counting #592

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 193 commits into from
Jun 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
193 commits
Select commit Hold shift + click to select a range
80fb450
Runtime experiment
dcodeIO Mar 8, 2019
878ee3f
fix
dcodeIO Mar 8, 2019
979a0b8
simplify
dcodeIO Mar 8, 2019
dd5430a
simplify more
dcodeIO Mar 8, 2019
911a4bb
design
dcodeIO Mar 8, 2019
0ad9d56
integrate
dcodeIO Mar 8, 2019
4f1a971
make it test again
dcodeIO Mar 9, 2019
0c537c3
test a few things
dcodeIO Mar 9, 2019
661e239
refactor
dcodeIO Mar 9, 2019
5c25b0c
refactor
dcodeIO Mar 9, 2019
5a2ab3d
what std/string would look like
dcodeIO Mar 10, 2019
cb77760
unsafe, stub
dcodeIO Mar 10, 2019
85b740d
fix
dcodeIO Mar 10, 2019
f076826
update std/string
dcodeIO Mar 10, 2019
ce82e54
unshiftify padEnd
dcodeIO Mar 11, 2019
ebe92c6
untrampoline padEnd
dcodeIO Mar 11, 2019
1198e71
same for padStart
dcodeIO Mar 11, 2019
d9a5681
arraybufferview
dcodeIO Mar 11, 2019
146dfdb
it's all broken
dcodeIO Mar 11, 2019
8e95867
I'm lost
dcodeIO Mar 12, 2019
36d54d6
slowly but steadily
dcodeIO Mar 12, 2019
e78c4c7
more
dcodeIO Mar 13, 2019
37d361b
dataview
dcodeIO Mar 13, 2019
707f2da
more
dcodeIO Mar 13, 2019
e581f25
fix
dcodeIO Mar 13, 2019
6f70826
hmm
dcodeIO Mar 13, 2019
e38f627
more
dcodeIO Mar 13, 2019
6163a73
take a step back
dcodeIO Mar 14, 2019
a5e14a0
srsly
dcodeIO Mar 14, 2019
84ddd97
if that's what's necessary
dcodeIO Mar 14, 2019
3b8c233
symbols
dcodeIO Mar 14, 2019
ea43f9f
what's that btw
dcodeIO Mar 14, 2019
cdf3e2c
fixes
dcodeIO Mar 14, 2019
3a60638
simplify
dcodeIO Mar 14, 2019
d3ca06b
inline single-use implementations
dcodeIO Mar 14, 2019
139cec0
Merge branch 'master' into runtime
dcodeIO Mar 14, 2019
968b032
decisions
dcodeIO Mar 15, 2019
058dc8d
static array stuff
dcodeIO Mar 15, 2019
b8a08da
more static array
dcodeIO Mar 16, 2019
05a35f4
more
dcodeIO Mar 16, 2019
0c388ca
make std/string test ok again
dcodeIO Mar 16, 2019
2d76da9
alias locals when inlining a tail call
dcodeIO Mar 16, 2019
e63c6bd
local flow flags, see what works
dcodeIO Mar 17, 2019
f21b339
checked builtin array get, optimize abv layout
dcodeIO Mar 17, 2019
edb2299
fixes
dcodeIO Mar 17, 2019
ba4c00e
clean
dcodeIO Mar 17, 2019
c147e98
capabilities to detect half/full runtime header
dcodeIO Mar 18, 2019
0932cf1
use overloads for checked access
dcodeIO Mar 18, 2019
83566a5
eliminate unnecessary tee+drop in unchecked set
dcodeIO Mar 18, 2019
7693b54
optimize array literal init, warn on unsupported inlining
dcodeIO Mar 19, 2019
d42ef51
update n-body for comparison
dcodeIO Mar 19, 2019
74789c9
aliased makeCallInline, unmanaged rt alloc
dcodeIO Mar 19, 2019
81039c4
FixedArray experimentation
dcodeIO Mar 19, 2019
89b9d46
link on property assign
dcodeIO Mar 19, 2019
5fb3077
rt unlink
dcodeIO Mar 20, 2019
9b664dc
harden / elaborate on makeCallInline
dcodeIO Mar 20, 2019
952ac86
stdlib unlink wiring
dcodeIO Mar 20, 2019
f44dbf2
quick pure gc prototype
dcodeIO Mar 20, 2019
88a595f
some thoughts on pure gc
dcodeIO Mar 20, 2019
658a380
whoops
dcodeIO Mar 20, 2019
3fc9f55
more general gc hooks?
dcodeIO Mar 21, 2019
d4d5814
rt docs, initial itcm wiring
dcodeIO Mar 21, 2019
d9463c5
dummy gc
dcodeIO Mar 21, 2019
e57fa14
give it a shot
dcodeIO Mar 21, 2019
c2ac1a0
what if..
dcodeIO Mar 21, 2019
7c0dc66
guard, info on never null, more general array rt
dcodeIO Mar 22, 2019
3146f8f
use gc interface directly, document
dcodeIO Mar 26, 2019
bb1609c
baseline
dcodeIO Mar 27, 2019
f7ad5f8
fixes
dcodeIO Mar 27, 2019
a309547
Merge branch 'master' into runtime
dcodeIO Mar 27, 2019
e36722f
unify mem/ref interface
dcodeIO Mar 27, 2019
41abc01
readme [ci skip]
dcodeIO Mar 27, 2019
0dcfcc7
external gc interface
dcodeIO Mar 28, 2019
01fad52
fix allocator tests
dcodeIO Mar 29, 2019
97d1f6e
atomics option?
dcodeIO Mar 29, 2019
72c1dbf
maybe..
dcodeIO Mar 29, 2019
6911f9d
clear cache, revert
dcodeIO Mar 29, 2019
a3aa9a3
disable caching
dcodeIO Mar 29, 2019
6a79032
make ref_unlink optional and itcm pass a simple test again
dcodeIO Mar 29, 2019
3e3c393
finalize GC wiring
dcodeIO Mar 30, 2019
c4a5702
add binaryen fiddles for indirect to direct call optimization
dcodeIO Mar 31, 2019
27f1905
runtime integration and flag
dcodeIO Mar 31, 2019
7b99e44
update allocator tests
dcodeIO Mar 31, 2019
1ada854
some cleanup
dcodeIO Apr 1, 2019
3bcd32f
directize
dcodeIO Apr 2, 2019
6bf8276
Merge branch 'master' into runtime
dcodeIO Apr 2, 2019
e1bd005
directize more (eliminate table use)
dcodeIO Apr 2, 2019
d85a438
remove old traversal code
dcodeIO Apr 2, 2019
a639a42
initial __runtime_instanceof
dcodeIO Apr 2, 2019
b62927f
move runtime templates into stdlib for bundling with lib files
dcodeIO Apr 2, 2019
b58683a
wire __runtime_instanceof to 'instanceof' on upcasts
dcodeIO Apr 2, 2019
50c49e4
make it 'runtime.instanceof'
dcodeIO Apr 2, 2019
cc1e4cd
expose runtime id of exported class
dcodeIO Apr 3, 2019
85f3fc5
runtime api
dcodeIO Apr 3, 2019
25c5dfd
slim down runtime
dcodeIO Apr 4, 2019
ed32a4c
notes on gc.collect that I don't yet know how to work around
dcodeIO Apr 4, 2019
a9e4813
implement __runtime_flags
dcodeIO Apr 4, 2019
e1070ce
rtti & refactoring
dcodeIO Apr 6, 2019
57c8bd0
handle references externally assigned to fields
dcodeIO Apr 6, 2019
c3ff3f2
inherent acylic data structure detection
dcodeIO Apr 8, 2019
6f25642
add a note on acyclic detection
dcodeIO Apr 8, 2019
8ec5142
Merge branch 'master' into runtime
dcodeIO Apr 8, 2019
c16c19e
unify runtime header
dcodeIO Apr 8, 2019
da4a775
optimize logical and/or, initial null checking in flows
dcodeIO Apr 9, 2019
cd79376
null checking throughout logical and/or
dcodeIO Apr 9, 2019
eb6c4c0
possible-null assignment to non-null, notes
dcodeIO Apr 9, 2019
bb659bb
Runtime 'none' by default
dcodeIO Apr 9, 2019
8b5c629
reduce tlsf footprint
dcodeIO Apr 11, 2019
6b256be
asrt experiment
dcodeIO Apr 11, 2019
085e2db
asrt: derive constants
dcodeIO Apr 11, 2019
c13f4db
asrt: merge tlsf and purerc
dcodeIO Apr 15, 2019
586ca8b
backport tlsf fixes, add asrt allocator test
dcodeIO Apr 15, 2019
aee3a3e
16b alignment, cleanup
dcodeIO Apr 15, 2019
ceffc18
implications of 16b alignment
dcodeIO Apr 15, 2019
3e08e5c
asrt debugger, fixes
dcodeIO Apr 16, 2019
0ba0d8a
crunch functions
dcodeIO Apr 16, 2019
504e207
polish
dcodeIO Apr 17, 2019
ffdda4b
realloc on mm level
dcodeIO Apr 17, 2019
8216cf3
refactor into stdlib
dcodeIO Apr 18, 2019
18c3f0c
unify, stub impl
dcodeIO Apr 18, 2019
2b0a165
more
dcodeIO Apr 18, 2019
2dec529
fix export star module exports, shiftify
dcodeIO Apr 18, 2019
dd2bdd0
load/store Block
dcodeIO Apr 18, 2019
ba1a0c2
Initial new rt integration
dcodeIO May 12, 2019
cb09edf
docs
dcodeIO May 12, 2019
f73d807
rt hooks all over the place
dcodeIO May 15, 2019
3e480d9
progress
dcodeIO May 20, 2019
1cbf3fc
fix rogue releases
dcodeIO May 20, 2019
7128d19
update integration notes
dcodeIO May 21, 2019
c926438
move stuff, run both untouched and optimized
dcodeIO May 21, 2019
c92ca0d
more integration notes
dcodeIO May 21, 2019
d94b4fc
layout rtti with fixed ids for buffers and strings
dcodeIO May 21, 2019
51fdf9d
fix
dcodeIO May 23, 2019
2263dfc
update allocator tests
dcodeIO May 23, 2019
c682fdb
disable simd test for now
dcodeIO May 23, 2019
71ea61e
fix
dcodeIO May 23, 2019
81c212b
monkey-patch loader
dcodeIO May 23, 2019
fbba76e
bleeding edge binaryen
dcodeIO May 23, 2019
a49ab7a
improve rtrace and move it to lib
dcodeIO May 23, 2019
792b366
update dist files
dcodeIO May 23, 2019
c41c57e
refactor
dcodeIO May 24, 2019
af00bde
use more useful defaults for -O, fix an utoa issue
dcodeIO May 24, 2019
7cd04b6
Add lightweight paths for memcpy & memset for shrink level != 0 (#601)
MaxGraey May 24, 2019
a6896d7
more loader updates
dcodeIO May 24, 2019
a684bb1
update tests
dcodeIO May 24, 2019
9620f18
more loader work
dcodeIO May 24, 2019
c34ed66
support normal arrays
dcodeIO May 24, 2019
4b841ef
fix potential oob
dcodeIO May 24, 2019
6d6ed71
more docs
dcodeIO May 24, 2019
968192f
array is an abv, views
dcodeIO May 25, 2019
d323910
getArrayView typings
dcodeIO May 25, 2019
a564c82
use Record as proposed in #598
dcodeIO May 25, 2019
33a892d
forward some of #559
dcodeIO May 25, 2019
bd9d77e
forward #533
dcodeIO May 25, 2019
3224925
eliminate retainRelease and skippedRelease
dcodeIO May 25, 2019
f9e1f65
fix 'no-unbound-method'
dcodeIO May 25, 2019
7feb0b7
consistent naming, don't miss local.set/tee value
dcodeIO May 25, 2019
d3654e3
skip traversal if there are no temps
dcodeIO May 25, 2019
476f7d9
Minor compiler improvements (#602)
MaxGraey May 25, 2019
bc29420
fixes, dist files
dcodeIO May 25, 2019
4a308aa
remove return from unchecked operator for FixedArray (#603)
MaxGraey May 26, 2019
e278110
improve V128 declaration (#604)
MaxGraey May 26, 2019
dece239
Generate a d.ts using modified dts-generator, see #434
dcodeIO May 27, 2019
220e73f
Merge branch 'dev' of https://github.com/AssemblyScript/next into dev
dcodeIO May 27, 2019
0339d82
Update
dcodeIO May 27, 2019
9c51f13
Fix parsing numbers with padded whitespaces (#607)
MaxGraey May 27, 2019
c06d5d9
update rt/flags test
dcodeIO May 27, 2019
bc0dd3a
Tackle AS206
dcodeIO May 27, 2019
ccd1198
Merge branch 'master' into dev
dcodeIO May 27, 2019
fe70f1d
Fix some missing sources not correctly being reported
dcodeIO May 27, 2019
d9fbf8a
Add preliminary support for map keys/values, set values
dcodeIO May 28, 2019
b19005f
Use new RC utility for inlining so we can eventually inline everything
dcodeIO May 29, 2019
e65d875
llvm-like naming of __heap_base, definition fixes, update examples
dcodeIO May 31, 2019
066ea1f
consistent naming
dcodeIO Jun 1, 2019
1468923
fix rc in Array#fill, add rtti logging for debugging
dcodeIO Jun 1, 2019
5b41c1f
fix __retain typings
dcodeIO Jun 1, 2019
adc2f7e
more consistent naming, move libm
dcodeIO Jun 2, 2019
27d9ab6
Move some stuff around, update README
dcodeIO Jun 3, 2019
ba35a6d
fix
dcodeIO Jun 3, 2019
e80ca10
Fix lifetime issue with assert messages, see #623
dcodeIO Jun 4, 2019
27e54ed
Fix missing autorelease in trampolines
dcodeIO Jun 4, 2019
fa66738
Initial support for default imports/exports, see #98
dcodeIO Jun 4, 2019
bf597a0
Fix lifetime issues in field inits/ctors and refactor inlining
dcodeIO Jun 4, 2019
b835cba
Update dist files
dcodeIO Jun 4, 2019
f0b5a37
More rtrace-aware tests
dcodeIO Jun 4, 2019
b47bcb5
Merge remote-tracking branch 'origin/master' into dev
dcodeIO Jun 4, 2019
93c9a4a
Fix truncated declaration ranges
dcodeIO Jun 5, 2019
b7ca4ba
Add a TODO on Array#copyWithin
dcodeIO Jun 5, 2019
3229b71
Release on shrink/zero on grow in std/array, managed copyWithin
dcodeIO Jun 5, 2019
dfe04d0
Audit fix
dcodeIO Jun 5, 2019
eaea26b
Remove the start decorator in favor of a command line option
dcodeIO Jun 5, 2019
72e5198
Fix invalid flow switch when inlining optional arguments into a call,…
dcodeIO Jun 5, 2019
76ef9db
Update dist files
dcodeIO Jun 5, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
12 changes: 4 additions & 8 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,15 @@ notifications:
email: false
before_install: npm config set progress=false && npm i -g npm@latest --no-audit
install: npm ci --no-audit
cache:
directories:
- node_modules
jobs:
include:
- node_js: lts/*
script:
- if [ "$TRAVIS_PULL_REQUEST" != "false" ]; then ./scripts/check-pr.sh; fi
- if [ "$TRAVIS_PULL_REQUEST" != "false" ] && [ "$TRAVIS_PULL_REQUEST_BRANCH" != "dev" ]; then ./scripts/check-pr.sh; fi
- npm run all
- if [ "$TRAVIS_PULL_REQUEST" == "false" ]; then
cd $TRAVIS_BUILD_DIR/tests/allocators/arena && npm run build && cd .. && npm test arena &&
cd $TRAVIS_BUILD_DIR/tests/allocators/buddy && npm run build && cd .. && npm test buddy &&
cd $TRAVIS_BUILD_DIR/tests/allocators/tlsf && npm run build && cd .. && npm test tlsf;
cd $TRAVIS_BUILD_DIR/tests/allocators/rt-full && npm run build && cd .. && npm test rt-full &&
cd $TRAVIS_BUILD_DIR/tests/allocators/rt-stub && npm run build && cd .. && npm test rt-stub;
fi
env: Runs the tests on node.js LTS, also tests allocators
- node_js: node
Expand All @@ -24,7 +20,7 @@ jobs:
env: Runs the tests on node.js stable
- node_js: node
script:
- npm run clean && npm run test:compiler
- npm run clean && npm run test:compiler rt/flags threads
env:
- Runs experimental tests on node.js v8-canary using
- ASC_FEATURES="simd,threads"
Expand Down
54 changes: 7 additions & 47 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

[![Build Status](https://travis-ci.org/AssemblyScript/assemblyscript.svg?branch=master)](https://travis-ci.org/AssemblyScript/assemblyscript)

**AssemblyScript** compiles strictly typed [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.
**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.

Try it out in [WebAssembly Studio](https://webassembly.studio)!
Check out the [documentation](https://docs.assemblyscript.org) or try it out in [WebAssembly Studio](https://webassembly.studio)!

---

Expand Down Expand Up @@ -43,60 +43,20 @@ Motivation
> 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)
Getting started
---------------
Instructions
------------

All the details are provided in the [AssemblyScript wiki](https://github.com/AssemblyScript/assemblyscript/wiki) - make sure to pay it a visit. With that being said, the easiest way to get started with AssemblyScript is to point npm at the GitHub repository (for now)

```
$> npm install --save-dev AssemblyScript/assemblyscript
```

followed by [scaffolding](https://github.com/AssemblyScript/assemblyscript/wiki/Using-the-CLI#scaffolding-with-asinit) a new project including the necessary configuration files, for example in the current directory:

```
$> npx asinit .
```

Once the project is set up, it's just a matter of using your existing [TypeScript tooling](https://code.visualstudio.com) while coding, and [using the CLI](https://github.com/AssemblyScript/assemblyscript/wiki/Using-the-CLI) to build to WebAssembly, either manually, or using (and maybe modifying) the generated build task in the generated `package.json`:

```
$> npm run asbuild
```

The CLI API can also [be used programmatically](./cli).

If you rather prefer an installation suitable for development, pretty much the same can be achieved by cloning the GitHub repository instead:
For general usage instructions, please refer to the [documentation](https://docs.assemblyscript.org) instead. The following sets up a *development environment* of the compiler, for example if you plan to make a pull request:

```
$> git clone https://github.com/AssemblyScript/assemblyscript.git
$> cd assemblyscript
$> npm install
$> npm link
$> npm clean
```

**Note** that a fresh clone of the compiler will use the distribution files in `dist/`, but it can also run [the sources](./src) directly through ts-node after an `npm run clean`, which is useful in development. This condition can also be checked by running `asc -v` (it is running the sources if it states `-dev`).

Examples
--------

* **[Conway's Game of Life](./examples/game-of-life)** [ [demo](https://assemblyscript.github.io/assemblyscript/examples/game-of-life) | [fiddle](https://webassembly.studio/?f=gvuw4enb3qk) ]<br />
Continuously updates the cellular automaton and visualizes its state on a canvas.

* **[Mandelbrot Set](./examples/mandelbrot)** [ [demo](https://assemblyscript.github.io/assemblyscript/examples/mandelbrot) | [fiddle](https://webassembly.studio/?f=m6hbiw9wyq) ]<br />
Renders the Mandelbrot set to a canvas.

* **[i64 polyfill](./examples/i64-polyfill)**<br />
Exposes WebAssembly's i64 operations to JavaScript using 32-bit integers (low and high bits).

* **[PSON decoder](./examples/pson)**<br />
A simple decoder for the PSON binary format.

* **[WASM parser](./lib/parse)**<br />
A WebAssembly binary parser in WebAssembly.

* **[N-body system](./examples/n-body)** [ [demo](https://assemblyscript.github.io/assemblyscript/examples/n-body) ]<br />
An implementation of the N-body system from the [Computer Language Benchmarks Game](https://benchmarksgame-team.pages.debian.net/benchmarksgame/).
Note that a fresh clone of the compiler will use the distribution files in `dist/`, but after an `npm clean` it will run [the sources](./src) directly through ts-node, which is useful in development. This condition can also be checked by running `asc -v` (it is running the sources if it states `-dev`). Also please see our [contribution guidelines](./CONTRIBUTING.md) before making your first pull request.

Building
--------
Expand Down
99 changes: 63 additions & 36 deletions cli/asc.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ const optionsUtil = require("./util/options");
const mkdirp = require("./util/mkdirp");
const EOL = process.platform === "win32" ? "\r\n" : "\n";

// global.Binaryen = require("../lib/binaryen");

// Emscripten adds an `uncaughtException` listener to Binaryen that results in an additional
// useless code fragment on top of an actual error. suppress this:
if (process.removeAllListeners) process.removeAllListeners("uncaughtException");
Expand Down Expand Up @@ -68,7 +70,7 @@ exports.sourceMapRoot = "assemblyscript:///";
exports.libraryPrefix = assemblyscript.LIBRARY_PREFIX;

/** Default Binaryen optimization level. */
exports.defaultOptimizeLevel = 2;
exports.defaultOptimizeLevel = 3;

/** Default Binaryen shrink level. */
exports.defaultShrinkLevel = 1;
Expand Down Expand Up @@ -228,30 +230,18 @@ exports.main = function main(argv, options, callback) {
var parser = null;

// Include library files
if (!args.noLib) {
Object.keys(exports.libraryFiles).forEach(libPath => {
if (libPath.indexOf("/") >= 0) return; // in sub-directory: imported on demand
stats.parseCount++;
stats.parseTime += measure(() => {
parser = assemblyscript.parseFile(
exports.libraryFiles[libPath],
exports.libraryPrefix + libPath + ".ts",
false,
parser
);
});
});
} else { // always include builtins
Object.keys(exports.libraryFiles).forEach(libPath => {
if (libPath.indexOf("/") >= 0) return; // in sub-directory: imported on demand
stats.parseCount++;
stats.parseTime += measure(() => {
parser = assemblyscript.parseFile(
exports.libraryFiles["builtins"],
exports.libraryPrefix + "builtins.ts",
exports.libraryFiles[libPath],
exports.libraryPrefix + libPath + ".ts",
false,
parser
);
});
}
});
const customLibDirs = [];
if (args.lib) {
let lib = args.lib;
Expand Down Expand Up @@ -287,6 +277,7 @@ exports.main = function main(argv, options, callback) {
function parseBacklog() {
var sourcePath, sourceText;
while ((sourcePath = parser.nextFile()) != null) {
sourceText = null;

// Load library file if explicitly requested
if (sourcePath.startsWith(exports.libraryPrefix)) {
Expand Down Expand Up @@ -335,12 +326,12 @@ exports.main = function main(argv, options, callback) {
} else {
for (let i = 0, k = customLibDirs.length; i < k; ++i) {
const dir = customLibDirs[i];
sourceText = readFile(plainName + ".ts", customLibDirs[i]);
sourceText = readFile(plainName + ".ts", dir);
if (sourceText !== null) {
sourcePath = exports.libraryPrefix + plainName + ".ts";
break;
} else {
sourceText = readFile(indexName + ".ts", customLibDirs[i]);
sourceText = readFile(indexName + ".ts", dir);
if (sourceText !== null) {
sourcePath = exports.libraryPrefix + indexName + ".ts";
break;
Expand All @@ -364,6 +355,26 @@ exports.main = function main(argv, options, callback) {
}
}

// Include runtime template before entry files so its setup runs first
{
let runtimeName = String(args.runtime);
let runtimePath = "rt/index-" + runtimeName;
let runtimeText = exports.libraryFiles[runtimePath];
if (runtimeText == null) {
runtimePath = runtimeName;
runtimeText = readFile(runtimePath + ".ts", baseDir);
if (runtimeText == null) {
return callback(Error("Runtime '" + runtimeName + "' not found."));
}
} else {
runtimePath = "~lib/" + runtimePath;
}
stats.parseCount++;
stats.parseTime += measure(() => {
parser = assemblyscript.parseFile(runtimeText, runtimePath, true, parser);
});
}

// Include entry files
for (let i = 0, k = argv.length; i < k; ++i) {
const filename = argv[i];
Expand All @@ -387,11 +398,18 @@ exports.main = function main(argv, options, callback) {
stats.parseTime += measure(() => {
parser = assemblyscript.parseFile(sourceText, sourcePath, true, parser);
});
}

// Parse entry files
{
let code = parseBacklog();
if (code) return code;
}

// Call afterParse transform hook
applyTransform("afterParse", parser);

// Parse additional files, if any
{
let code = parseBacklog();
if (code) return code;
Expand Down Expand Up @@ -423,17 +441,16 @@ exports.main = function main(argv, options, callback) {
assemblyscript.setImportMemory(compilerOptions, args.importMemory);
assemblyscript.setSharedMemory(compilerOptions, args.sharedMemory);
assemblyscript.setImportTable(compilerOptions, args.importTable);
assemblyscript.setExplicitStart(compilerOptions, args.explicitStart);
assemblyscript.setMemoryBase(compilerOptions, args.memoryBase >>> 0);
assemblyscript.setSourceMap(compilerOptions, args.sourceMap != null);
assemblyscript.setOptimizeLevelHints(compilerOptions, optimizeLevel, shrinkLevel);

if (!args.noLib) {
// Initialize default aliases
assemblyscript.setGlobalAlias(compilerOptions, "Math", "NativeMath");
assemblyscript.setGlobalAlias(compilerOptions, "Mathf", "NativeMathf");
assemblyscript.setGlobalAlias(compilerOptions, "abort", "~lib/env/abort");
assemblyscript.setGlobalAlias(compilerOptions, "trace", "~lib/env/trace");
}
// Initialize default aliases
assemblyscript.setGlobalAlias(compilerOptions, "Math", "NativeMath");
assemblyscript.setGlobalAlias(compilerOptions, "Mathf", "NativeMathf");
assemblyscript.setGlobalAlias(compilerOptions, "abort", "~lib/builtins/abort");
assemblyscript.setGlobalAlias(compilerOptions, "trace", "~lib/builtins/trace");

// Add or override aliases if specified
if (args.use) {
Expand Down Expand Up @@ -463,15 +480,13 @@ exports.main = function main(argv, options, callback) {

var module;
stats.compileCount++;
(() => {
try {
stats.compileTime += measure(() => {
module = assemblyscript.compileProgram(program, compilerOptions);
});
} catch (e) {
return callback(e);
}
})();
try {
stats.compileTime += measure(() => {
module = assemblyscript.compileProgram(program, compilerOptions);
});
} catch (e) {
return callback(e);
}
if (checkDiagnostics(parser, stderr)) {
if (module) module.dispose();
return callback(Error("Compile error"));
Expand Down Expand Up @@ -697,6 +712,9 @@ exports.main = function main(argv, options, callback) {
if (args.measure) {
printStats(stats, stderr);
}
if (args.printrtti) {
printRTTI(program, stderr);
}
return callback(null);

function readFileNode(filename, baseDir) {
Expand Down Expand Up @@ -833,6 +851,15 @@ function printStats(stats, output) {

exports.printStats = printStats;

/** Prints runtime type information. */
function printRTTI(program, output) {
if (!output) output = process.stderr;
output.write("# Runtime type information (RTTI)\n");
output.write(assemblyscript.buildRTTI(program));
}

exports.printRTTI = printRTTI;

var allocBuffer = typeof global !== "undefined" && global.Buffer
? global.Buffer.allocUnsafe || function(len) { return new global.Buffer(len); }
: function(len) { return new Uint8Array(len) };
Expand Down
24 changes: 21 additions & 3 deletions cli/asc.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"description": [
"Optimizes the module. Also has the usual shorthands:",
"",
" -O Uses defaults. Equivalent to -O2s",
" -O Uses defaults. Equivalent to -O3s",
" -O0 Equivalent to --optimizeLevel 0",
" -O1 Equivalent to --optimizeLevel 1",
" -O2 Equivalent to --optimizeLevel 2",
Expand Down Expand Up @@ -81,6 +81,19 @@
],
"type": "s"
},
"runtime": {
"description": [
"Specifies the runtime implementation to include in the program.",
"",
" full Default runtime based on TLSF and reference counting.",
" half Same as 'full', but not exported to the host.",
" stub Minimal stub implementation without free/GC support.",
" none Same as 'stub', but not exported to the host.",
""
],
"type": "s",
"default": "full"
},
"debug": {
"description": "Enables debug information in emitted binaries.",
"type": "b",
Expand Down Expand Up @@ -116,8 +129,8 @@
"type": "b",
"default": false
},
"noLib": {
"description": "Does not include the shipped standard library.",
"explicitStart": {
"description": "Exports an explicit start function to be called manually.",
"type": "b",
"default": false
},
Expand Down Expand Up @@ -177,6 +190,11 @@
"type": "b",
"default": false
},
"printrtti": {
"description": "Prints the module's runtime type information to stderr.",
"type": "b",
"default": false
},
"noColors": {
"description": "Disables terminal colors.",
"type": "b",
Expand Down
2 changes: 1 addition & 1 deletion dist/asc.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/asc.js.map

Large diffs are not rendered by default.

Loading