Skip to content

Commit

Permalink
feat: Support lazy global and function exports (#1371)
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodeIO authored Jul 2, 2020
1 parent 2a29cab commit af3b489
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 11 deletions.
20 changes: 10 additions & 10 deletions src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,8 +358,8 @@ export class Compiler extends DiagnosticEmitter {
skippedAutoreleases: Set<ExpressionRef> = new Set();
/** Current inline functions stack. */
inlineStack: Function[] = [];
/** Lazily compiled library functions. */
lazyLibraryFunctions: Set<Function> = new Set();
/** Lazily compiled functions. */
lazyFunctions: Set<Function> = new Set();
/** Pending class-specific instanceof helpers. */
pendingClassInstanceOf: Set<ClassPrototype> = new Set();
/** Functions potentially involving a virtual call. */
Expand Down Expand Up @@ -490,20 +490,20 @@ export class Compiler extends DiagnosticEmitter {
program.registerConstantInteger("__GC_ALL_ACYCLIC", Type.bool, i64_new(1, 0));
}

// compile lazy library functions
var lazyLibraryFunctions = this.lazyLibraryFunctions;
// compile lazy functions
var lazyFunctions = this.lazyFunctions;
do {
let functionsToCompile = new Array<Function>();
// TODO: for (let instance of lazyLibraryFunctions) {
for (let _values = Set_values(lazyLibraryFunctions), i = 0, k = _values.length; i < k; ++i) {
for (let _values = Set_values(lazyFunctions), i = 0, k = _values.length; i < k; ++i) {
let instance = unchecked(_values[i]);
functionsToCompile.push(instance);
}
lazyLibraryFunctions.clear();
lazyFunctions.clear();
for (let i = 0, k = functionsToCompile.length; i < k; ++i) {
this.compileFunction(unchecked(functionsToCompile[i]), true);
}
} while (lazyLibraryFunctions.size);
} while (lazyFunctions.size);

// compile pending class-specific instanceof helpers
// TODO: for (let prototype of this.pendingClassInstanceOf.values()) {
Expand Down Expand Up @@ -759,7 +759,7 @@ export class Compiler extends DiagnosticEmitter {
global.identifierNode.range
);
} else {
this.module.addGlobalExport(element.internalName, prefix + name);
if (element.is(CommonFlags.COMPILED)) this.module.addGlobalExport(element.internalName, prefix + name);
}
break;
}
Expand Down Expand Up @@ -916,7 +916,7 @@ export class Compiler extends DiagnosticEmitter {
// TODO: for (let element of exports.values()) {
for (let _values = Map_values(exports), i = 0, k = _values.length; i < k; ++i) {
let element = unchecked(_values[i]);
this.compileElement(element);
if (!element.hasDecorator(DecoratorFlags.LAZY)) this.compileElement(element);
}
}
var exportsStar = file.exportsStar;
Expand Down Expand Up @@ -1349,7 +1349,7 @@ export class Compiler extends DiagnosticEmitter {
if (!forceStdAlternative) {
if (instance.hasDecorator(DecoratorFlags.BUILTIN)) return true;
if (instance.hasDecorator(DecoratorFlags.LAZY)) {
this.lazyLibraryFunctions.add(instance);
this.lazyFunctions.add(instance);
return true;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2239,7 +2239,7 @@ export class Program extends DiagnosticEmitter {
validDecorators |= DecoratorFlags.EXTERNAL;
} else {
validDecorators |= DecoratorFlags.INLINE;
if (declaration.range.source.isLibrary) {
if (declaration.range.source.isLibrary || declaration.is(CommonFlags.EXPORT)) {
validDecorators |= DecoratorFlags.LAZY;
}
}
Expand Down
5 changes: 5 additions & 0 deletions tests/compiler/exports-lazy.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"asc_flags": [
"--runtime none"
]
}
13 changes: 13 additions & 0 deletions tests/compiler/exports-lazy.optimized.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
(module
(type $none_=>_none (func))
(memory $0 1)
(data (i32.const 1024) "\0c\00\00\00\01\00\00\00\00\00\00\00\0c\00\00\00\01\00\00\00\02\00\00\00\03")
(data (i32.const 1056) "\10\00\00\00\01\00\00\00\03\00\00\00\10\00\00\00\10\04\00\00\10\04\00\00\0c\00\00\00\03")
(global $exports-lazy/lazyGlobalUsed i32 (i32.const 1072))
(export "memory" (memory $0))
(export "lazyGlobalUsed" (global $exports-lazy/lazyGlobalUsed))
(export "lazyFuncUsed" (func $~start))
(func $~start
nop
)
)
9 changes: 9 additions & 0 deletions tests/compiler/exports-lazy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
@lazy export const lazyGlobalUnused: i32[] = [1,2,3];

@lazy export const lazyGlobalUsed: i32[] = [1,2,3];
lazyGlobalUsed;

@lazy export function lazyFuncUnused(): void {}

@lazy export function lazyFuncUsed(): void {}
lazyFuncUsed();
25 changes: 25 additions & 0 deletions tests/compiler/exports-lazy.untouched.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
(module
(type $none_=>_none (func))
(memory $0 1)
(data (i32.const 16) "\0c\00\00\00\01\00\00\00\00\00\00\00\0c\00\00\00\01\00\00\00\02\00\00\00\03\00\00\00")
(data (i32.const 48) "\10\00\00\00\01\00\00\00\03\00\00\00\10\00\00\00 \00\00\00 \00\00\00\0c\00\00\00\03\00\00\00")
(table $0 1 funcref)
(global $exports-lazy/lazyGlobalUsed i32 (i32.const 64))
(export "memory" (memory $0))
(export "lazyGlobalUsed" (global $exports-lazy/lazyGlobalUsed))
(export "lazyFuncUsed" (func $exports-lazy/lazyFuncUsed))
(start $~start)
(func $start:exports-lazy
(local $0 i32)
(local $1 i32)
global.get $exports-lazy/lazyGlobalUsed
drop
call $exports-lazy/lazyFuncUsed
)
(func $~start
call $start:exports-lazy
)
(func $exports-lazy/lazyFuncUsed
nop
)
)

0 comments on commit af3b489

Please sign in to comment.