Skip to content

Commit faac3c3

Browse files
committed
AST cleanup; Definition generators scaffolding
1 parent eef923d commit faac3c3

Some content is hidden

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

50 files changed

+3137
-2555
lines changed

bin/asc.js

Lines changed: 63 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,9 @@ exports.main = function main(argv, options, callback) {
286286
}
287287
}
288288

289+
// Finish parsing
290+
const program = assemblyscript.finishParsing(parser);
291+
289292
// Begin compilation
290293
const compilerOptions = assemblyscript.createOptions();
291294
assemblyscript.setTarget(compilerOptions, 0);
@@ -301,7 +304,7 @@ exports.main = function main(argv, options, callback) {
301304
(() => {
302305
try {
303306
stats.compileTime += measure(() => {
304-
module = assemblyscript.compile(parser, compilerOptions);
307+
module = assemblyscript.compileProgram(program, compilerOptions);
305308
});
306309
} catch (e) {
307310
return callback(e);
@@ -434,23 +437,23 @@ exports.main = function main(argv, options, callback) {
434437
: path.basename(args.binaryFile) + ".map"
435438
: null;
436439

437-
let binary;
440+
let wasm;
438441
stats.emitCount++;
439442
stats.emitTime += measure(() => {
440-
binary = module.toBinary(sourceMapURL)
443+
wasm = module.toBinary(sourceMapURL)
441444
});
442445

443446
if (args.binaryFile.length) {
444-
writeFile(path.join(baseDir, args.binaryFile), binary.output);
447+
writeFile(path.join(baseDir, args.binaryFile), wasm.output);
445448
} else {
446-
writeStdout(binary.output);
449+
writeStdout(wasm.output);
447450
hasStdout = true;
448451
}
449452

450453
// Post-process source map
451-
if (binary.sourceMap != null) {
454+
if (wasm.sourceMap != null) {
452455
if (args.binaryFile.length) {
453-
let sourceMap = JSON.parse(binary.sourceMap);
456+
let sourceMap = JSON.parse(wasm.sourceMap);
454457
sourceMap.sourceRoot = exports.sourceMapRoot;
455458
sourceMap.sources.forEach((name, index) => {
456459
let text = null;
@@ -482,48 +485,80 @@ exports.main = function main(argv, options, callback) {
482485
}
483486
}
484487

485-
// Write text
486-
if (
487-
args.textFile != null || (
488-
args.binaryFile == null &&
489-
args.asmjsFile == null
490-
)
491-
) {
492-
let text;
493-
if (args.textFile && args.textFile.length) {
488+
// Write asm.js
489+
if (args.asmjsFile != null) {
490+
let asm;
491+
if (args.asmjsFile.length) {
494492
stats.emitCount++;
495493
stats.emitTime += measure(() => {
496-
text = module.toText();
494+
asm = module.toAsmjs();
497495
});
498-
writeFile(path.join(baseDir, args.textFile), text);
496+
writeFile(path.join(baseDir, args.asmjsFile), asm);
499497
} else if (!hasStdout) {
500498
stats.emitCount++;
501499
stats.emitTime += measure(() => {
502-
text = module.toText()
500+
asm = module.toAsmjs();
503501
});
504-
writeStdout(text);
502+
writeStdout(asm);
505503
hasStdout = true;
506504
}
507505
}
508506

509-
// Write asm.js
510-
if (args.asmjsFile != null) {
511-
let asm;
512-
if (args.asmjsFile.length) {
507+
// Write WebIDL
508+
if (args.idlFile != null) {
509+
let idl;
510+
if (args.idlFile.length) {
513511
stats.emitCount++;
514512
stats.emitTime += measure(() => {
515-
asm = module.toAsmjs();
513+
idl = assemblyscript.buildIDL(program);
516514
});
517-
writeFile(path.join(baseDir, args.asmjsFile), asm);
515+
writeFile(path.join(baseDir, args.idlFile), idl);
518516
} else if (!hasStdout) {
519517
stats.emitCount++;
520518
stats.emitTime += measure(() => {
521-
asm = module.toAsmjs();
519+
idl = assemblyscript.buildIDL(program);
522520
});
523-
writeStdout(asm);
521+
writeStdout(idl);
524522
hasStdout = true;
525523
}
526524
}
525+
526+
// Write TypeScript definition
527+
if (args.tsdFile != null) {
528+
let tsd;
529+
if (args.tsdFile.length) {
530+
stats.emitCount++;
531+
stats.emitTime += measure(() => {
532+
tsd = assemblyscript.buildTSD(program);
533+
});
534+
writeFile(path.join(baseDir, args.tsdFile), tsd);
535+
} else if (!hasStdout) {
536+
stats.emitCount++;
537+
stats.emitTime += measure(() => {
538+
tsd = assemblyscript.buildTSD(program);
539+
});
540+
writeStdout(tsd);
541+
hasStdout = true;
542+
}
543+
}
544+
545+
// Write text (must be last)
546+
if (args.textFile != null) {
547+
let wat;
548+
if (args.textFile && args.textFile.length) {
549+
stats.emitCount++;
550+
stats.emitTime += measure(() => {
551+
wat = module.toText();
552+
});
553+
writeFile(path.join(baseDir, args.textFile), wat);
554+
} else if (!hasStdout) {
555+
stats.emitCount++;
556+
stats.emitTime += measure(() => {
557+
wat = module.toText()
558+
});
559+
writeStdout(wat);
560+
}
561+
}
527562
}
528563

529564
module.dispose();

bin/asc.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,16 @@
6262
"type": "string",
6363
"aliases": [ "a" ]
6464
},
65+
"idlFile": {
66+
"desc": "Specifies the WebIDL output file (.idl).",
67+
"type": "string",
68+
"aliases": [ "i" ]
69+
},
70+
"tsdFile": {
71+
"desc": "Specifies the TypeScript definition output file (.d.ts).",
72+
"type": "string",
73+
"aliases": [ "d" ]
74+
},
6575
"sourceMap": {
6676
"desc": [
6777
"Enables source map generation. Optionally takes the URL",

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: 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.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/demangle/index.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* @file AssemblyScript demangler.
3+
*/
4+
5+
module.exports = demangle;
6+
7+
/**
8+
* Demangles module exports to a friendly object structure compatible with WebIDL and TypeScript
9+
* definitions.
10+
*/
11+
function demangle(exports) {
12+
var root = {};
13+
for (let i in exports) {
14+
if (exports.hasOwnProperty(i)) {
15+
// TODO
16+
}
17+
}
18+
return root;
19+
}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@
4747
"test:parser": "node tests/parser",
4848
"test:compiler": "node tests/compiler",
4949
"test": "npm run test:config --scripts-prepend-node-path && npm run test:parser --scripts-prepend-node-path && npm run test:compiler --scripts-prepend-node-path",
50-
"test:pr": "npm run clean && npm test && npm run build && npm test && npm run clean"
50+
"test:pr": "npm run clean && npm test && npm run build && npm test && npm run clean",
51+
"all": "npm run lint && npm run clean && npm test && npm run build && npm test"
5152
},
5253
"files": [
5354
"bin/",

0 commit comments

Comments
 (0)