Skip to content

Commit ccb1296

Browse files
authored
Add postInitialize transform step (AssemblyScript#1145)
1 parent 6cee84b commit ccb1296

File tree

5 files changed

+35
-2
lines changed

5 files changed

+35
-2
lines changed

cli/asc.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,20 @@ exports.main = function main(argv, options, callback) {
590590
optimizeLevel = Math.min(Math.max(optimizeLevel, 0), 3);
591591
shrinkLevel = Math.min(Math.max(shrinkLevel, 0), 2);
592592

593+
try {
594+
stats.compileTime += measure(() => {
595+
assemblyscript.initializeProgram(program, compilerOptions);
596+
});
597+
} catch(e) {
598+
return callback(e);
599+
}
600+
601+
// Call afterInitialize transform hook
602+
{
603+
let error = applyTransform("afterInitialize", program);
604+
if (error) return callback(error);
605+
}
606+
593607
var module;
594608
stats.compileCount++;
595609
try {

cli/transform.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ export abstract class Transform {
3535
/** Called when parsing is complete, before a program is instantiated from the AST. */
3636
afterParse?(parser: Parser): void;
3737

38+
/** Called after the program is instantiated. */
39+
afterInitialize?(program: Program): void;
40+
3841
/** Called when compilation is complete, before the module is being validated. */
3942
afterCompile?(module: Module): void;
4043
}

src/compiler.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -376,14 +376,19 @@ export class Compiler extends DiagnosticEmitter {
376376
module.setFeatures(featureFlags);
377377
}
378378

379+
initializeProgram(): void {
380+
// initialize lookup maps, built-ins, imports, exports, etc.
381+
this.program.initialize(this.options);
382+
}
383+
379384
/** Performs compilation of the underlying {@link Program} to a {@link Module}. */
380385
compile(): Module {
381386
var options = this.options;
382387
var module = this.module;
383388
var program = this.program;
384389

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

388393
// set up the main start function
389394
var startFunctionInstance = program.makeNativeFunction(BuiltinNames.start, new Signature(program, [], Type.void));

src/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,11 @@ export function getDependee(program: Program, file: string): string | null {
214214

215215
// Compiler
216216

217+
/** Initializes the program pre-emptively for transform hooks. */
218+
export function initializeProgram(program: Program, options: Options): void {
219+
program.initialize(options);
220+
}
221+
217222
/** Compiles the parsed sources to a module. */
218223
export function compile(program: Program): Module {
219224
program.parser.finish();

src/program.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,8 @@ export class Program extends DiagnosticEmitter {
491491
nextClassId: u32 = 0;
492492
/** Next signature id. */
493493
nextSignatureId: i32 = 0;
494+
/** An indicator if the program has been initialized. */
495+
initialized: bool = false;
494496
/** Constructs a new program, optionally inheriting parser diagnostics. */
495497
constructor(
496498
/** Compiler options. */
@@ -646,6 +648,10 @@ export class Program extends DiagnosticEmitter {
646648

647649
/** Initializes the program and its elements prior to compilation. */
648650
initialize(options: Options): void {
651+
// Initialize only once
652+
if (this.initialized) return;
653+
654+
this.initialized = true;
649655
this.options = options;
650656

651657
// register native types

0 commit comments

Comments
 (0)