Skip to content

Commit bbc71eb

Browse files
committed
More options for asc; asm.js output
1 parent 64516ee commit bbc71eb

File tree

5 files changed

+79
-14
lines changed

5 files changed

+79
-14
lines changed

README.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ Side effects:
2929
How does it work?
3030
-----------------
3131

32-
AssemblyScript NEXT compiles a subset (or variant) of TypeScript to Binaryen IR. The resulting module can then be optimized, emitted in text or binary format, or even be converted to asm.js as a polyfill.
32+
AssemblyScript NEXT compiles a subset (or variant) of TypeScript to [Binaryen](https://github.com/WebAssembly/binaryen) IR. The resulting module can then be optimized, emitted in text or binary format or converted to [asm.js](http://asmjs.org) as a polyfill.
3333

3434
Getting started
3535
---------------
@@ -59,3 +59,24 @@ Development status
5959
------------------
6060

6161
For now, see the [compiler tests](https://github.com/AssemblyScript/next/tree/master/tests/compiler) for an overview of what's supposed to be working already.
62+
63+
Using the CLI
64+
-------------
65+
66+
```
67+
Syntax: asc [options] [file ...]
68+
69+
Examples: asc hello.ts
70+
71+
Options:
72+
-v, --version Prints the compiler's version.
73+
-h, --help Prints this message.
74+
-O, --optimize Optimizes the module.
75+
-c, --validate Validates the module.
76+
-o, --outFile Specifies the output file.
77+
-b, --binaryFile Specifies the binary format output file.
78+
-t, --textFile Specifies the text format output file.
79+
-a, --asmjsFile Specifies the asm.js format output file.
80+
--noTreeShaking Disables tree-shaking.
81+
--noDebug Disables assertions.
82+
```

bin/asc.js

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@ if (args.help || args._.length < 1) {
4545
Object.keys(conf).forEach(name => {
4646
var option = conf[name];
4747
var text = " ";
48-
if (option.aliases)
48+
if (option.aliases && option.aliases[0].length === 1)
4949
text += "-" + option.aliases[0] + ", ";
5050
text += "--" + name;
51-
while (text.length < 20)
51+
while (text.length < 24)
5252
text += " ";
5353
options.push(text + option.desc);
5454
});
@@ -141,14 +141,32 @@ if (args.optimize)
141141
var hasOutput = false;
142142

143143
if (args.outFile != null) {
144-
fs.writeFileSync(args.outFile, module.toBinary());
144+
if (/\.wast$/.test(args.outFile) && args.textFile == null)
145+
args.textFile = args.outFile;
146+
else if (/\.js$/.test(args.outFile) && args.asmjsFile == null)
147+
args.asmjsFile = args.outFile;
148+
else if (args.binaryFile == null)
149+
args.binaryFile = args.outFile;
150+
}
151+
if (args.binaryFile != null && args.binaryFile.length) {
152+
fs.writeFileSync(args.binaryFile, module.toBinary());
145153
hasOutput = true;
146154
}
147-
if (args.textFile != null) {
155+
if (args.textFile != null && args.textFile.length) {
148156
fs.writeFileSync(args.textFile, module.toText(), { encoding: "utf8" });
149157
hasOutput = true;
150158
}
151-
if (!hasOutput)
152-
module.print();
159+
if (args.asmjsFile != null && args.asmjsFile.length) {
160+
fs.writeFileSync(args.asmjsFile, module.toAsmjs(), { encoding: "utf8" });
161+
hasOutput = true;
162+
}
163+
if (!hasOutput) {
164+
if (args.binaryFile === "")
165+
process.stdout.write(Buffer.from(module.toBinary()));
166+
else if (args.asmjsFile === "")
167+
module.printAsmjs();
168+
else
169+
module.print();
170+
}
153171

154172
module.dispose();

bin/asc.json

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,35 @@
1616
},
1717
"validate": {
1818
"desc": "Validates the module.",
19-
"type": "boolean"
19+
"type": "boolean",
20+
"aliases": [ "c", "check" ]
2021
},
2122
"outFile": {
22-
"desc": "Specifies the output file (binary format).",
23-
"type": "string"
23+
"desc": "Specifies the output file.",
24+
"type": "string",
25+
"aliases": [ "o" ]
26+
},
27+
"binaryFile": {
28+
"desc": "Specifies the binary format output file.",
29+
"type": "string",
30+
"aliases": [ "b" ]
2431
},
2532
"textFile": {
26-
"desc": "Specifies the output file (text format).",
27-
"type": "string"
33+
"desc": "Specifies the text format output file.",
34+
"type": "string",
35+
"aliases": [ "t" ]
36+
},
37+
"asmjsFile": {
38+
"desc": "Specifies the asm.js format output file.",
39+
"type": "string",
40+
"aliases": [ "a" ]
2841
},
2942
"noTreeShaking": {
30-
"desc": "Disables built-in tree-shaking.",
43+
"desc": "Disables tree-shaking.",
3144
"type": "boolean"
3245
},
3346
"noDebug": {
34-
"desc": "Replaces assertions with nops.",
47+
"desc": "Disables assertions.",
3548
"type": "boolean"
3649
}
3750
}

src/glue/js.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,12 @@ Module.prototype.toText = function toText() {
4040
binaryen.print = previousPrint;
4141
return ret;
4242
}
43+
44+
Module.prototype.toAsmjs = function toAsmjs() {
45+
var previousPrint = binaryen.print;
46+
var ret = "";
47+
binaryen.print = function print(x) { ret += x + "\n" };
48+
this.printAsmjs();
49+
binaryen.print = previousPrint;
50+
return ret;
51+
}

src/module.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -743,6 +743,10 @@ export class Module {
743743
return _BinaryenModulePrint(this.ref);
744744
}
745745

746+
printAsmjs(): void {
747+
return _BinaryenModulePrintAsmjs(this.ref);
748+
}
749+
746750
toBinary(bufferSize: usize = 1048576): Uint8Array {
747751
// FIXME: target specific / JS glue overrides this
748752
throw new Error("not implemented");

0 commit comments

Comments
 (0)