Skip to content

Implement transform classes #892

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 11 commits into from
Oct 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 47 additions & 13 deletions cli/asc.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,20 +212,45 @@ exports.main = function main(argv, options, callback) {
// Set up transforms
const transforms = [];
if (args.transform) {
args.transform.forEach(transform =>
transforms.push(
require(
path.isAbsolute(transform = transform.trim())
? transform
: path.join(process.cwd(), transform)
)
)
);
let transformArgs = args.transform;
for (let i = 0, k = transformArgs.length; i < k; ++i) {
let filename = transformArgs[i];
filename = path.isAbsolute(filename = filename.trim())
? filename
: path.join(process.cwd(), filename);
if (/\.ts$/.test(filename)) require("ts-node").register({ transpileOnly: true, skipProject: true });
try {
const classOrModule = require(filename);
if (typeof classOrModule === "function") {
Object.assign(classOrModule.prototype, {
baseDir,
stdout,
stderr,
log: console.error,
readFile,
writeFile,
listFiles
});
transforms.push(new classOrModule());
} else {
transforms.push(classOrModule); // legacy module
}
} catch (e) {
return callback(e);
}
}
}
function applyTransform(name, ...args) {
transforms.forEach(transform => {
if (typeof transform[name] === "function") transform[name](...args);
});
for (let i = 0, k = transforms.length; i < k; ++i) {
let transform = transforms[i];
if (typeof transform[name] === "function") {
try {
transform[name](...args);
} catch (e) {
return e;
}
}
}
}

// Begin parsing
Expand Down Expand Up @@ -426,7 +451,10 @@ exports.main = function main(argv, options, callback) {
}

// Call afterParse transform hook
applyTransform("afterParse", parser);
{
let error = applyTransform("afterParse", parser);
if (error) return callback(error);
}

// Parse additional files, if any
{
Expand Down Expand Up @@ -530,6 +558,12 @@ exports.main = function main(argv, options, callback) {
return callback(Error("Compile error"));
}

// Call afterCompile transform hook
{
let error = applyTransform("afterCompile", module);
if (error) return callback(error);
}

// Validate the module if requested
if (args.validate) {
stats.validateCount++;
Expand Down
32 changes: 29 additions & 3 deletions cli/transform.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,35 @@
* @module cli/transform
*//***/

import { Parser } from "../src/parser";
import { Parser, Module } from "..";
import { OutputStream } from "./asc";

export abstract class Transform {

/** Base directory. */
readonly baseDir: string;

/** Output stream used by the compiler. */
readonly stdout: OutputStream;

/** Error stream used by the compiler. */
readonly stderr: OutputStream;

/** Logs a message to console. */
readonly log: typeof console.log;

/** Writes a file to disk. */
writeFile(filename: string, contents: string | Uint8Array, baseDir: string): boolean;

/** Reads a file from disk. */
readFile(filename: string, baseDir: string): string | null;

/** Lists all files in a directory. */
listFiles(dirname: string, baseDir: string): string[] | null;

export interface Transform {
/** Called when parsing is complete, before a program is instantiated from the AST. */
afterParse(parser: Parser): void;
afterParse?(parser: Parser): void;

/** Called when compilation is complete, before the module is being validated. */
afterCompile?(module: Module): void;
}
2 changes: 2 additions & 0 deletions cli/transform.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// becomes replaced with the actual base by asc
exports.Transform = function Transform() {};
8 changes: 8 additions & 0 deletions examples/transform/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Compiler transform examples
===========================

Both transforms written in JS and transforms written in TS can be used, with the
latter requiring that the ts-node package is present.

* [Example JavaScript transform](./mytransform.js)
* [Example TypeScript transform](./mytransform.ts)
1 change: 1 addition & 0 deletions examples/transform/assembly/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// empty
18 changes: 18 additions & 0 deletions examples/transform/mytransform.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const { Transform } = require("../../cli/transform"); // "assemblyscript/cli/transform"
const { SourceKind } = require("../.."); // "assemblyscript"
const binaryen = require("binaryen");

class MyTransform extends Transform {
afterParse(parser) {
this.log("[mytransform.js] afterParse called, baseDir = " + this.baseDir);
var sources = parser.program.sources;
sources.forEach(source => this.log(" " + source.internalPath + " [" + SourceKind[source.sourceKind] + "]"));
}
afterCompile(asModule) {
this.log("[mytransform.js] afterCompile called");
var module = binaryen.wrapModule(asModule.ref);
this.log(module.emitBinary());
}
}

module.exports = MyTransform;
18 changes: 18 additions & 0 deletions examples/transform/mytransform.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Transform } from "../../cli/transform"; // "assemblyscript/cli/transform"
import { Parser, Module, SourceKind } from "../.."; // "assemblyscript"
import * as binaryen from "binaryen";

class MyTransform extends Transform {
afterParse(parser: Parser): void {
this.log("[mytransform.ts] afterParse called, baseDir = " + this.baseDir);
var sources = parser.program.sources;
sources.forEach(source => this.log(" " + source.internalPath + " [" + SourceKind[source.sourceKind] + "]"));
}
afterCompile(asModule: Module): void {
this.log("[mytransform.ts] afterCompile called");
var module = binaryen.wrapModule(asModule.ref);
this.log(module.emitBinary());
}
}

export = MyTransform;
8 changes: 8 additions & 0 deletions examples/transform/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"scripts": {
"test:js": "asc assembly/index.ts --runtime none --transform mytransform.js",
"test:ts": "asc assembly/index.ts --runtime none --transform mytransform.ts",
"test:multi": "asc assembly/index.ts --runtime none --transform mytransform.js --transform mytransform.ts",
"test": "npm run test:js && npm run test:ts && npm run test:multi"
}
}
88 changes: 61 additions & 27 deletions scripts/build-dts.js
Original file line number Diff line number Diff line change
Expand Up @@ -408,41 +408,40 @@
exports.default = generate;
});

const prelude = `declare type bool = boolean;
declare type i8 = number;
declare type i16 = number;
declare type i32 = number;
declare type isize = number;
declare type u8 = number;
declare type u16 = number;
declare type u32 = number;
declare type usize = number;
declare type f32 = number;
declare type f64 = number;
declare module 'assemblyscript' {
export * from 'assemblyscript/src/index';
const path = require("path");
const fs = require("fs");
const stream = require("stream");
const util = require("util");

function OutputStream(options) {
stream.Writable.call(this, options);
this.chunks = [];
}
`;
util.inherits(OutputStream, stream.Writable);
OutputStream.prototype._write = function(chunk, enc, cb) {
this.chunks.push(chunk);
cb();
};
OutputStream.prototype.toBuffer = function() {
return Buffer.concat(this.chunks);
};
OutputStream.prototype.toString = function() {
return this.toBuffer().toString("utf8");
};

var path = require("path");
var fs = require("fs");
var stdout = fs.createWriteStream(path.resolve(__dirname, "..", "dist", "assemblyscript.d.ts"));
stdout.write(prelude);
stdout.write = (function(_write) {
return function(...args) {
if (typeof args[0] === "string") {
args[0] = args[0].replace(/\/\/\/ <reference[^>]*>\r?\n/g, "");
}
return _write.apply(stdout, args);
};
})(stdout.write);
const stdout = new OutputStream();
stdout.write(`declare module 'assemblyscript' {
export * from 'assemblyscript/src/index';
}
`);

module.exports.default({
project: path.resolve(__dirname, "..", "src"),
prefix: "assemblyscript",
exclude: [
"glue/js/index.ts",
"glue/js/node.d.ts"
"glue/js/node.d.ts",
"glue/binaryen.d.ts"
],
verbose: true,
sendMessage: console.log,
Expand All @@ -459,3 +458,38 @@ module.exports.default({
sendMessage: console.log,
stdout: stdout
});

var source = stdout.toString().replace(/\/\/\/ <reference[^>]*>\r?\n/g, "");

const ts = require("typescript");
const sourceFile = ts.createSourceFile("assemblyscript.d.ts", source, ts.ScriptTarget.ESNext, false, ts.ScriptKind.TS);

console.log("transforming:");
var numReplaced = 0;
const result = ts.transform(sourceFile, [
function(context) {
const visit = node => {
node = ts.visitEachChild(node, visit, context);
if (ts.isTypeNode(node)) {
const name = node.getText(sourceFile);
switch (name) {
// this is wrong, but works
case "bool": ++numReplaced; return ts.createIdentifier("boolean");
default: if (!/^(?:Binaryen|Relooper)/.test(name)) break;
case "i8": case "i16": case "i32": case "isize":
case "u8": case "u16": case "u32": case "usize":
case "f32": case "f64": ++numReplaced; return ts.createIdentifier("number");
}
}
return node;
};
return node => ts.visitNode(node, visit);
}
]);
console.log(" replaced " + numReplaced + " AS types with JS types");

fs.writeFileSync(
path.resolve(__dirname, "..", "dist", "assemblyscript.d.ts"),
ts.createPrinter().printFile(result.transformed[0]),
"utf8"
);