Skip to content

Add postInitialize transform step #1145

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 4 commits into from
Mar 3, 2020
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
14 changes: 14 additions & 0 deletions cli/asc.js
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,20 @@ exports.main = function main(argv, options, callback) {
optimizeLevel = Math.min(Math.max(optimizeLevel, 0), 3);
shrinkLevel = Math.min(Math.max(shrinkLevel, 0), 2);

try {
stats.compileTime += measure(() => {
assemblyscript.initializeProgram(program, compilerOptions);
});
} catch(e) {
return callback(e);
}

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

var module;
stats.compileCount++;
try {
Expand Down
3 changes: 3 additions & 0 deletions cli/transform.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ export abstract class Transform {
/** Called when parsing is complete, before a program is instantiated from the AST. */
afterParse?(parser: Parser): void;

/** Called after the program is instantiated. */
afterInitialize?(program: Program): void;

/** Called when compilation is complete, before the module is being validated. */
afterCompile?(module: Module): void;
}
9 changes: 7 additions & 2 deletions src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -376,14 +376,19 @@ export class Compiler extends DiagnosticEmitter {
module.setFeatures(featureFlags);
}

initializeProgram(): void {
// initialize lookup maps, built-ins, imports, exports, etc.
this.program.initialize(this.options);
}

/** Performs compilation of the underlying {@link Program} to a {@link Module}. */
compile(): Module {
var options = this.options;
var module = this.module;
var program = this.program;

// initialize lookup maps, built-ins, imports, exports, etc.
program.initialize(options);
// check and perform this program initialization if it hasn't been done
this.initializeProgram();

// set up the main start function
var startFunctionInstance = program.makeNativeFunction(BuiltinNames.start, new Signature(program, [], Type.void));
Expand Down
5 changes: 5 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,11 @@ export function getDependee(program: Program, file: string): string | null {

// Compiler

/** Initializes the program pre-emptively for transform hooks. */
export function initializeProgram(program: Program, options: Options): void {
program.initialize(options);
}

/** Compiles the parsed sources to a module. */
export function compile(program: Program): Module {
program.parser.finish();
Expand Down
6 changes: 6 additions & 0 deletions src/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,8 @@ export class Program extends DiagnosticEmitter {
nextClassId: u32 = 0;
/** Next signature id. */
nextSignatureId: i32 = 0;
/** An indicator if the program has been initialized. */
initialized: bool = false;
/** Constructs a new program, optionally inheriting parser diagnostics. */
constructor(
/** Compiler options. */
Expand Down Expand Up @@ -633,6 +635,10 @@ export class Program extends DiagnosticEmitter {

/** Initializes the program and its elements prior to compilation. */
initialize(options: Options): void {
// Initialize only once
if (this.initialized) return;

this.initialized = true;
this.options = options;

// register native types
Expand Down