Skip to content

Commit af3b489

Browse files
authored
feat: Support lazy global and function exports (#1371)
1 parent 2a29cab commit af3b489

File tree

6 files changed

+63
-11
lines changed

6 files changed

+63
-11
lines changed

src/compiler.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -358,8 +358,8 @@ export class Compiler extends DiagnosticEmitter {
358358
skippedAutoreleases: Set<ExpressionRef> = new Set();
359359
/** Current inline functions stack. */
360360
inlineStack: Function[] = [];
361-
/** Lazily compiled library functions. */
362-
lazyLibraryFunctions: Set<Function> = new Set();
361+
/** Lazily compiled functions. */
362+
lazyFunctions: Set<Function> = new Set();
363363
/** Pending class-specific instanceof helpers. */
364364
pendingClassInstanceOf: Set<ClassPrototype> = new Set();
365365
/** Functions potentially involving a virtual call. */
@@ -490,20 +490,20 @@ export class Compiler extends DiagnosticEmitter {
490490
program.registerConstantInteger("__GC_ALL_ACYCLIC", Type.bool, i64_new(1, 0));
491491
}
492492

493-
// compile lazy library functions
494-
var lazyLibraryFunctions = this.lazyLibraryFunctions;
493+
// compile lazy functions
494+
var lazyFunctions = this.lazyFunctions;
495495
do {
496496
let functionsToCompile = new Array<Function>();
497497
// TODO: for (let instance of lazyLibraryFunctions) {
498-
for (let _values = Set_values(lazyLibraryFunctions), i = 0, k = _values.length; i < k; ++i) {
498+
for (let _values = Set_values(lazyFunctions), i = 0, k = _values.length; i < k; ++i) {
499499
let instance = unchecked(_values[i]);
500500
functionsToCompile.push(instance);
501501
}
502-
lazyLibraryFunctions.clear();
502+
lazyFunctions.clear();
503503
for (let i = 0, k = functionsToCompile.length; i < k; ++i) {
504504
this.compileFunction(unchecked(functionsToCompile[i]), true);
505505
}
506-
} while (lazyLibraryFunctions.size);
506+
} while (lazyFunctions.size);
507507

508508
// compile pending class-specific instanceof helpers
509509
// TODO: for (let prototype of this.pendingClassInstanceOf.values()) {
@@ -759,7 +759,7 @@ export class Compiler extends DiagnosticEmitter {
759759
global.identifierNode.range
760760
);
761761
} else {
762-
this.module.addGlobalExport(element.internalName, prefix + name);
762+
if (element.is(CommonFlags.COMPILED)) this.module.addGlobalExport(element.internalName, prefix + name);
763763
}
764764
break;
765765
}
@@ -916,7 +916,7 @@ export class Compiler extends DiagnosticEmitter {
916916
// TODO: for (let element of exports.values()) {
917917
for (let _values = Map_values(exports), i = 0, k = _values.length; i < k; ++i) {
918918
let element = unchecked(_values[i]);
919-
this.compileElement(element);
919+
if (!element.hasDecorator(DecoratorFlags.LAZY)) this.compileElement(element);
920920
}
921921
}
922922
var exportsStar = file.exportsStar;
@@ -1349,7 +1349,7 @@ export class Compiler extends DiagnosticEmitter {
13491349
if (!forceStdAlternative) {
13501350
if (instance.hasDecorator(DecoratorFlags.BUILTIN)) return true;
13511351
if (instance.hasDecorator(DecoratorFlags.LAZY)) {
1352-
this.lazyLibraryFunctions.add(instance);
1352+
this.lazyFunctions.add(instance);
13531353
return true;
13541354
}
13551355
}

src/program.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2239,7 +2239,7 @@ export class Program extends DiagnosticEmitter {
22392239
validDecorators |= DecoratorFlags.EXTERNAL;
22402240
} else {
22412241
validDecorators |= DecoratorFlags.INLINE;
2242-
if (declaration.range.source.isLibrary) {
2242+
if (declaration.range.source.isLibrary || declaration.is(CommonFlags.EXPORT)) {
22432243
validDecorators |= DecoratorFlags.LAZY;
22442244
}
22452245
}

tests/compiler/exports-lazy.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"asc_flags": [
3+
"--runtime none"
4+
]
5+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
(module
2+
(type $none_=>_none (func))
3+
(memory $0 1)
4+
(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")
5+
(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")
6+
(global $exports-lazy/lazyGlobalUsed i32 (i32.const 1072))
7+
(export "memory" (memory $0))
8+
(export "lazyGlobalUsed" (global $exports-lazy/lazyGlobalUsed))
9+
(export "lazyFuncUsed" (func $~start))
10+
(func $~start
11+
nop
12+
)
13+
)

tests/compiler/exports-lazy.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
@lazy export const lazyGlobalUnused: i32[] = [1,2,3];
2+
3+
@lazy export const lazyGlobalUsed: i32[] = [1,2,3];
4+
lazyGlobalUsed;
5+
6+
@lazy export function lazyFuncUnused(): void {}
7+
8+
@lazy export function lazyFuncUsed(): void {}
9+
lazyFuncUsed();
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
(module
2+
(type $none_=>_none (func))
3+
(memory $0 1)
4+
(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")
5+
(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")
6+
(table $0 1 funcref)
7+
(global $exports-lazy/lazyGlobalUsed i32 (i32.const 64))
8+
(export "memory" (memory $0))
9+
(export "lazyGlobalUsed" (global $exports-lazy/lazyGlobalUsed))
10+
(export "lazyFuncUsed" (func $exports-lazy/lazyFuncUsed))
11+
(start $~start)
12+
(func $start:exports-lazy
13+
(local $0 i32)
14+
(local $1 i32)
15+
global.get $exports-lazy/lazyGlobalUsed
16+
drop
17+
call $exports-lazy/lazyFuncUsed
18+
)
19+
(func $~start
20+
call $start:exports-lazy
21+
)
22+
(func $exports-lazy/lazyFuncUsed
23+
nop
24+
)
25+
)

0 commit comments

Comments
 (0)