Skip to content

Commit f0477ba

Browse files
committed
Merge branch 'master' into parser
2 parents c823422 + d5f72e3 commit f0477ba

Some content is hidden

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

76 files changed

+13728
-4809
lines changed

NOTICE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ under the licensing terms detailed in LICENSE:
1010
* Alan Pierce <[email protected]>
1111
1212
* Linus Unnebäck <[email protected]>
13+
* Joshua Tenner <[email protected]>
1314

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

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,10 @@ $> npm link
5757
Examples
5858
--------
5959

60-
* **[Conway's Game of Life](./examples/game-of-life)** [ [demo](https://rawgit.com/AssemblyScript/assemblyscript/master/examples/game-of-life/index.html) | [fiddle](https://webassembly.studio/?f=gvuw4enb3qk) ]<br />
60+
* **[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 />
6161
Continuously updates the cellular automaton and visualizes its state on a canvas.
6262

63-
* **[Mandelbrot Set](./examples/mandelbrot)** [ [demo](https://rawgit.com/AssemblyScript/assemblyscript/master/examples/mandelbrot/index.html) | [fiddle](https://webassembly.studio/?f=m6hbiw9wyq) ]<br />
63+
* **[Mandelbrot Set](./examples/mandelbrot)** [ [demo](https://assemblyscript.github.io/assemblyscript/examples/mandelbrot) | [fiddle](https://webassembly.studio/?f=m6hbiw9wyq) ]<br />
6464
Renders the Mandelbrot set to a canvas.
6565

6666
* **[i64 polyfill](./examples/i64-polyfill)**<br />
@@ -72,7 +72,7 @@ Examples
7272
* **[WASM parser](./lib/parse)**<br />
7373
A WebAssembly binary parser in WebAssembly.
7474

75-
* **[N-body system](./examples/n-body)** [ [demo](https://rawgit.com/AssemblyScript/assemblyscript/master/examples/n-body/index.html) ]<br />
75+
* **[N-body system](./examples/n-body)** [ [demo](https://assemblyscript.github.io/assemblyscript/examples/n-body) ]<br />
7676
An implementation of the N-body system from the [Computer Language Benchmarks Game](https://benchmarksgame-team.pages.debian.net/benchmarksgame/).
7777

7878
Building

cli/asc.d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,11 @@ interface CompilerOptions {
5555
/** Standard error stream to use. */
5656
stderr?: OutputStream;
5757
/** Reads a file from disk (or memory). */
58-
readFile?: (name: string) => string | null;
58+
readFile?: (filename: string, baseDir: string) => string | null;
5959
/** Writes a file to disk (or memory). */
60-
writeFile?: (name: string, contents: Uint8Array) => void;
60+
writeFile?: (filename: string, contents: Uint8Array, baseDir: string) => void;
6161
/** Lists all files within a directory. */
62-
listFiles?: (dir: string) => string[] | null;
62+
listFiles?: (dirname: string, baseDir: string) => string[] | null;
6363
}
6464

6565
/** Convenience function that parses and compiles source strings directly. */

cli/asc.js

Lines changed: 59 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,7 @@ exports.compileString = async (sources, options) => {
9696
if (typeof sources === "string") sources = { "input.ts": sources };
9797
const output = Object.create({
9898
stdout: createMemoryStream(),
99-
stderr: createMemoryStream(),
100-
binary: null,
101-
text: null
99+
stderr: createMemoryStream()
102100
});
103101
debugger;
104102
var argv = [];
@@ -278,7 +276,7 @@ exports.main = async function main(argv, options, callback) {
278276
}
279277
for (let j = 0, l = libFiles.length; j < l; ++j) {
280278
let libPath = libFiles[j];
281-
let libText = await readFile(path.join(libDir, libPath));
279+
let libText = readFile(libPath, libDir);
282280
if (libText === null) return callback(Error("Library file '" + libPath + "' not found."));
283281
stats.parseCount++;
284282
stats.parseTime += measure(() => {
@@ -293,35 +291,10 @@ exports.main = async function main(argv, options, callback) {
293291
}
294292
}
295293

296-
// Include entry files
297-
for (let i = 0, k = argv.length; i < k; ++i) {
298-
const filename = argv[i];
299-
if (filename == "undefined") continue;
300-
301-
let sourcePath = String(filename).replace(/\\/g, "/").replace(/(\.ts|\/)$/, "");
302-
303-
// Try entryPath.ts, then entryPath/index.ts
304-
let sourceText = await readFile(path.join(baseDir, sourcePath) + ".ts");
305-
if (sourceText === null) {
306-
sourceText = await readFile(path.join(baseDir, sourcePath, "index.ts"));
307-
if (sourceText === null) {
308-
return callback(Error("Entry file '" + sourcePath + ".ts' not found."));
309-
} else {
310-
sourcePath += "/index.ts";
311-
}
312-
} else {
313-
sourcePath += ".ts";
314-
}
315-
debugger;
316-
317-
stats.parseCount++;
318-
stats.parseTime += measure(() => {
319-
parser = assemblyscript.parseFile(sourceText, sourcePath, true, parser);
320-
});
321-
322-
// Process backlog
294+
// Parses the backlog of imported files after including entry files
295+
function parseBacklog() {
296+
var sourcePath, sourceText;
323297
while ((sourcePath = parser.nextFile()) != null) {
324-
let found = false;
325298

326299
// Load library file if explicitly requested
327300
if (sourcePath.startsWith(exports.libraryPrefix)) {
@@ -335,13 +308,12 @@ exports.main = async function main(argv, options, callback) {
335308
sourcePath = exports.libraryPrefix + indexName + ".ts";
336309
} else {
337310
for (let i = 0, k = customLibDirs.length; i < k; ++i) {
338-
const dir = customLibDirs[i];
339-
sourceText = await readFile(path.join(dir, plainName + ".ts"));
311+
sourceText = readFile(plainName + ".ts", customLibDirs[i]);
340312
if (sourceText !== null) {
341313
sourcePath = exports.libraryPrefix + plainName + ".ts";
342314
break;
343315
} else {
344-
sourceText = await readFile(path.join(dir, indexName + ".ts"));
316+
sourceText = readFile(indexName + ".ts", customLibDirs[i]);
345317
if (sourceText !== null) {
346318
sourcePath = exports.libraryPrefix + indexName + ".ts";
347319
break;
@@ -354,11 +326,11 @@ exports.main = async function main(argv, options, callback) {
354326
} else {
355327
const plainName = sourcePath;
356328
const indexName = sourcePath + "/index";
357-
sourceText = await readFile(path.join(baseDir, plainName + ".ts"));
329+
sourceText = readFile(plainName + ".ts", baseDir);
358330
if (sourceText !== null) {
359331
sourcePath = plainName + ".ts";
360332
} else {
361-
sourceText = await readFile(path.join(baseDir, indexName + ".ts"));
333+
sourceText = readFile(indexName + ".ts", baseDir);
362334
if (sourceText !== null) {
363335
sourcePath = indexName + ".ts";
364336
} else if (!plainName.startsWith(".")) {
@@ -371,12 +343,12 @@ exports.main = async function main(argv, options, callback) {
371343
} else {
372344
for (let i = 0, k = customLibDirs.length; i < k; ++i) {
373345
const dir = customLibDirs[i];
374-
sourceText = await readFile(path.join(dir, plainName + ".ts"));
346+
sourceText = readFile(plainName + ".ts", customLibDirs[i]);
375347
if (sourceText !== null) {
376348
sourcePath = exports.libraryPrefix + plainName + ".ts";
377349
break;
378350
} else {
379-
sourceText = await readFile(path.join(dir, indexName + ".ts"));
351+
sourceText = readFile(indexName + ".ts", customLibDirs[i]);
380352
if (sourceText !== null) {
381353
sourcePath = exports.libraryPrefix + indexName + ".ts";
382354
break;
@@ -400,7 +372,38 @@ exports.main = async function main(argv, options, callback) {
400372
}
401373
}
402374

375+
// Include entry files
376+
for (let i = 0, k = argv.length; i < k; ++i) {
377+
const filename = argv[i];
378+
379+
let sourcePath = String(filename).replace(/\\/g, "/").replace(/(\.ts|\/)$/, "");
380+
381+
// Try entryPath.ts, then entryPath/index.ts
382+
let sourceText = readFile(sourcePath + ".ts", baseDir);
383+
if (sourceText === null) {
384+
sourceText = readFile(sourcePath + "/index.ts", baseDir);
385+
if (sourceText === null) {
386+
return callback(Error("Entry file '" + sourcePath + ".ts' not found."));
387+
} else {
388+
sourcePath += "/index.ts";
389+
}
390+
} else {
391+
sourcePath += ".ts";
392+
}
393+
394+
stats.parseCount++;
395+
stats.parseTime += measure(() => {
396+
parser = assemblyscript.parseFile(sourceText, sourcePath, true, parser);
397+
});
398+
let code = parseBacklog();
399+
if (code) return code;
400+
}
401+
403402
applyTransform("afterParse", parser);
403+
{
404+
let code = parseBacklog();
405+
if (code) return code;
406+
}
404407

405408
// Finish parsing
406409
const program = assemblyscript.finishParsing(parser);
@@ -578,7 +581,7 @@ exports.main = async function main(argv, options, callback) {
578581
});
579582

580583
if (args.binaryFile.length) {
581-
await writeFile(path.join(baseDir, args.binaryFile), wasm.output);
584+
writeFile(args.binaryFile, wasm.output, baseDir);
582585
} else {
583586
writeStdout(wasm.output);
584587
hasStdout = true;
@@ -598,27 +601,23 @@ exports.main = async function main(argv, options, callback) {
598601
text = exports.libraryFiles[stdName];
599602
} else {
600603
for (let i = 0, k = customLibDirs.length; i < k; ++i) {
601-
text = await readFile(path.join(
602-
customLibDirs[i],
603-
name.substring(exports.libraryPrefix.length))
604-
);
604+
text = readFile(name.substring(exports.libraryPrefix.length), customLibDirs[i]);
605605
if (text !== null) break;
606606
}
607607
}
608608
} else {
609-
text = await readFile(path.join(baseDir, name));
609+
text = readFile(name, baseDir);
610610
}
611611
if (text === null) {
612612
return callback(Error("Source file '" + name + "' not found."));
613613
}
614614
if (!sourceMap.sourceContents) sourceMap.sourceContents = [];
615615
sourceMap.sourceContents[index] = text;
616616
});
617-
await writeFile(path.join(
618-
baseDir,
617+
writeFile(path.join(
619618
path.dirname(args.binaryFile),
620619
path.basename(sourceMapURL)
621-
), JSON.stringify(sourceMap));
620+
).replace(/^\.\//, ""), JSON.stringify(sourceMap), baseDir);
622621
} else {
623622
stderr.write("Skipped source map (stdout already occupied)" + EOL);
624623
}
@@ -633,7 +632,7 @@ exports.main = async function main(argv, options, callback) {
633632
stats.emitTime += measure(() => {
634633
asm = module.toAsmjs();
635634
});
636-
await writeFile(path.join(baseDir, args.asmjsFile), asm);
635+
writeFile(args.asmjsFile, asm, baseDir);
637636
} else if (!hasStdout) {
638637
stats.emitCount++;
639638
stats.emitTime += measure(() => {
@@ -653,7 +652,7 @@ exports.main = async function main(argv, options, callback) {
653652
stats.emitTime += measure(() => {
654653
idl = assemblyscript.buildIDL(program);
655654
});
656-
await writeFile(path.join(baseDir, args.idlFile), idl);
655+
writeFile(args.idlFile, idl, baseDir);
657656
} else if (!hasStdout) {
658657
stats.emitCount++;
659658
stats.emitTime += measure(() => {
@@ -673,7 +672,7 @@ exports.main = async function main(argv, options, callback) {
673672
stats.emitTime += measure(() => {
674673
tsd = assemblyscript.buildTSD(program);
675674
});
676-
await writeFile(path.join(baseDir, args.tsdFile), tsd);
675+
writeFile(args.tsdFile, tsd, baseDir);
677676
} else if (!hasStdout) {
678677
stats.emitCount++;
679678
stats.emitTime += measure(() => {
@@ -693,7 +692,7 @@ exports.main = async function main(argv, options, callback) {
693692
stats.emitTime += measure(() => {
694693
wat = module.toText();
695694
});
696-
await writeFile(path.join(baseDir, args.textFile), wat);
695+
writeFile(args.textFile, wat, baseDir);
697696
} else if (!hasStdout) {
698697
stats.emitCount++;
699698
stats.emitTime += measure(() => {
@@ -710,28 +709,28 @@ exports.main = async function main(argv, options, callback) {
710709
}
711710
return callback(null);
712711

713-
function readFileNode(filename) {
712+
function readFileNode(filename, baseDir) {
714713
try {
715714
let text;
716715
stats.readCount++;
717716
stats.readTime += measure(() => {
718-
text = fs.readFileSync(filename, { encoding: "utf8" });
717+
text = fs.readFileSync(path.join(baseDir, filename), { encoding: "utf8" });
719718
});
720719
return text;
721720
} catch (e) {
722721
return null;
723722
}
724723
}
725724

726-
function writeFileNode(filename, contents) {
725+
function writeFileNode(filename, contents, baseDir) {
727726
try {
728727
stats.writeCount++;
729728
stats.writeTime += measure(() => {
730-
mkdirp(path.dirname(filename));
729+
mkdirp(path.join(baseDir, path.dirname(filename)));
731730
if (typeof contents === "string") {
732-
fs.writeFileSync(filename, contents, { encoding: "utf8" } );
731+
fs.writeFileSync(path.join(baseDir, filename), contents, { encoding: "utf8" } );
733732
} else {
734-
fs.writeFileSync(filename, contents);
733+
fs.writeFileSync(path.join(baseDir, filename), contents);
735734
}
736735
});
737736
return true;
@@ -740,11 +739,11 @@ exports.main = async function main(argv, options, callback) {
740739
}
741740
}
742741

743-
function listFilesNode(dirname) {
742+
function listFilesNode(dirname, baseDir) {
744743
var files;
745744
try {
746745
stats.readTime += measure(() => {
747-
files = fs.readdirSync(dirname).filter(file => /^(?!.*\.d\.ts$).*\.ts$/.test(file));
746+
files = fs.readdirSync(path.join(baseDir, dirname)).filter(file => /^(?!.*\.d\.ts$).*\.ts$/.test(file));
748747
});
749748
return files;
750749
} catch (e) {

dist/asc.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/asc.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/assemblyscript.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/assemblyscript.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/loader/index.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"use strict";
22

33
const hasBigInt64 = typeof BigUint64Array !== "undefined";
4+
const thisPtr = Symbol();
45

56
/** Gets a string from an U32 and an U16 view on a memory. */
67
function getStringImpl(U32, U16, ptr) {
@@ -265,11 +266,11 @@ function demangle(exports, baseModule) {
265266
let classElem = curr[className];
266267
if (typeof classElem === "undefined" || !classElem.prototype) {
267268
let ctor = function(...args) {
268-
return ctor.wrap(ctor.prototype.constructor(...args));
269+
return ctor.wrap(ctor.prototype.constructor(0, ...args));
269270
};
270271
ctor.prototype = {};
271272
ctor.wrap = function(thisValue) {
272-
return Object.create(ctor.prototype, { "this": { value: thisValue, writable: false } });
273+
return Object.create(ctor.prototype, { [thisPtr]: { value: thisValue, writable: false } });
273274
};
274275
if (classElem) Object.getOwnPropertyNames(classElem).forEach(name =>
275276
Object.defineProperty(ctor, name, Object.getOwnPropertyDescriptor(classElem, name))
@@ -283,13 +284,22 @@ function demangle(exports, baseModule) {
283284
let getter = exports[internalName.replace("set:", "get:")];
284285
let setter = exports[internalName.replace("get:", "set:")];
285286
Object.defineProperty(curr, name, {
286-
get: function() { return getter(this.this); },
287-
set: function(value) { setter(this.this, value); },
287+
get: function() { return getter(this[thisPtr]); },
288+
set: function(value) { setter(this[thisPtr], value); },
288289
enumerable: true
289290
});
290291
}
291292
} else {
292-
curr[name] = wrapFunction(elem, setargc);
293+
if (name === 'constructor') {
294+
curr[name] = wrapFunction(elem, setargc);
295+
} else { // for methods
296+
Object.defineProperty(curr, name, {
297+
value: function (...args) {
298+
setargc(args.length);
299+
return elem(this[thisPtr], ...args);
300+
}
301+
});
302+
}
293303
}
294304
} else {
295305
if (/^(get|set):/.test(name)) {

lib/loader/tests/index.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,14 @@ assert.strictEqual(fn(2), 4);
6060
ptr = module.newFunction(module.varadd);
6161
assert.strictEqual(module.calladd(ptr, 2, 3), 5);
6262

63+
// should be able to use a class
64+
var car = new module.Car(5);
65+
assert.strictEqual(car.numDoors, 5);
66+
assert.strictEqual(car.isDoorsOpen, 0);
67+
car.openDoors();
68+
assert.strictEqual(car.isDoorsOpen, 1);
69+
car.closeDoors();
70+
assert.strictEqual(car.isDoorsOpen, 0);
71+
6372
// should be able to use trace
64-
module.dotrace(42);
73+
module.dotrace(42);

src/README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,4 @@ Portable compiler sources that compile to both JavaScript using `tsc` and, event
22

33
Architecture
44
------------
5-
6-
![](https://rawgit.com/AssemblyScript/assemblyscript/master/media/architecture.svg)
5+
![](https://assemblyscript.github.io/assemblyscript/media/architecture.svg)

0 commit comments

Comments
 (0)