From bbc71ebe811257b90cad2cedc19424553e51df21 Mon Sep 17 00:00:00 2001 From: dcodeIO Date: Sat, 9 Dec 2017 00:45:12 +0100 Subject: [PATCH] More options for asc; asm.js output --- README.md | 23 ++++++++++++++++++++++- bin/asc.js | 30 ++++++++++++++++++++++++------ bin/asc.json | 27 ++++++++++++++++++++------- src/glue/js.js | 9 +++++++++ src/module.ts | 4 ++++ 5 files changed, 79 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 989911d7..556d22ee 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ Side effects: How does it work? ----------------- -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. +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. Getting started --------------- @@ -59,3 +59,24 @@ Development status ------------------ 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. + +Using the CLI +------------- + +``` +Syntax: asc [options] [file ...] + +Examples: asc hello.ts + +Options: + -v, --version Prints the compiler's version. + -h, --help Prints this message. + -O, --optimize Optimizes the module. + -c, --validate Validates the module. + -o, --outFile Specifies the output file. + -b, --binaryFile Specifies the binary format output file. + -t, --textFile Specifies the text format output file. + -a, --asmjsFile Specifies the asm.js format output file. + --noTreeShaking Disables tree-shaking. + --noDebug Disables assertions. +``` diff --git a/bin/asc.js b/bin/asc.js index 5610d165..65330c41 100644 --- a/bin/asc.js +++ b/bin/asc.js @@ -45,10 +45,10 @@ if (args.help || args._.length < 1) { Object.keys(conf).forEach(name => { var option = conf[name]; var text = " "; - if (option.aliases) + if (option.aliases && option.aliases[0].length === 1) text += "-" + option.aliases[0] + ", "; text += "--" + name; - while (text.length < 20) + while (text.length < 24) text += " "; options.push(text + option.desc); }); @@ -141,14 +141,32 @@ if (args.optimize) var hasOutput = false; if (args.outFile != null) { - fs.writeFileSync(args.outFile, module.toBinary()); + if (/\.wast$/.test(args.outFile) && args.textFile == null) + args.textFile = args.outFile; + else if (/\.js$/.test(args.outFile) && args.asmjsFile == null) + args.asmjsFile = args.outFile; + else if (args.binaryFile == null) + args.binaryFile = args.outFile; +} +if (args.binaryFile != null && args.binaryFile.length) { + fs.writeFileSync(args.binaryFile, module.toBinary()); hasOutput = true; } -if (args.textFile != null) { +if (args.textFile != null && args.textFile.length) { fs.writeFileSync(args.textFile, module.toText(), { encoding: "utf8" }); hasOutput = true; } -if (!hasOutput) - module.print(); +if (args.asmjsFile != null && args.asmjsFile.length) { + fs.writeFileSync(args.asmjsFile, module.toAsmjs(), { encoding: "utf8" }); + hasOutput = true; +} +if (!hasOutput) { + if (args.binaryFile === "") + process.stdout.write(Buffer.from(module.toBinary())); + else if (args.asmjsFile === "") + module.printAsmjs(); + else + module.print(); +} module.dispose(); diff --git a/bin/asc.json b/bin/asc.json index 29471888..88a00ab6 100644 --- a/bin/asc.json +++ b/bin/asc.json @@ -16,22 +16,35 @@ }, "validate": { "desc": "Validates the module.", - "type": "boolean" + "type": "boolean", + "aliases": [ "c", "check" ] }, "outFile": { - "desc": "Specifies the output file (binary format).", - "type": "string" + "desc": "Specifies the output file.", + "type": "string", + "aliases": [ "o" ] + }, + "binaryFile": { + "desc": "Specifies the binary format output file.", + "type": "string", + "aliases": [ "b" ] }, "textFile": { - "desc": "Specifies the output file (text format).", - "type": "string" + "desc": "Specifies the text format output file.", + "type": "string", + "aliases": [ "t" ] + }, + "asmjsFile": { + "desc": "Specifies the asm.js format output file.", + "type": "string", + "aliases": [ "a" ] }, "noTreeShaking": { - "desc": "Disables built-in tree-shaking.", + "desc": "Disables tree-shaking.", "type": "boolean" }, "noDebug": { - "desc": "Replaces assertions with nops.", + "desc": "Disables assertions.", "type": "boolean" } } diff --git a/src/glue/js.js b/src/glue/js.js index 78803912..01f77644 100644 --- a/src/glue/js.js +++ b/src/glue/js.js @@ -40,3 +40,12 @@ Module.prototype.toText = function toText() { binaryen.print = previousPrint; return ret; } + +Module.prototype.toAsmjs = function toAsmjs() { + var previousPrint = binaryen.print; + var ret = ""; + binaryen.print = function print(x) { ret += x + "\n" }; + this.printAsmjs(); + binaryen.print = previousPrint; + return ret; +} diff --git a/src/module.ts b/src/module.ts index 15fa61b3..1fafff3e 100644 --- a/src/module.ts +++ b/src/module.ts @@ -743,6 +743,10 @@ export class Module { return _BinaryenModulePrint(this.ref); } + printAsmjs(): void { + return _BinaryenModulePrintAsmjs(this.ref); + } + toBinary(bufferSize: usize = 1048576): Uint8Array { // FIXME: target specific / JS glue overrides this throw new Error("not implemented");