Skip to content

Imported & Exported ES6 Namespaces now contained properties that would have been queuedExports #1422

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 10 commits into from
Aug 5, 2020
8 changes: 7 additions & 1 deletion src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,8 @@ export class Compiler extends DiagnosticEmitter {
virtualCalls: Set<Function> = new Set();
/** Elements currently undergoing compilation. */
pendingElements: Set<Element> = new Set();
/** Elements, that are module exports, already processed */
doneModuleExports: Set<Element> = new Set();

/** Compiles a {@link Program} to a {@link Module} using the specified options. */
static compile(program: Program): Module {
Expand Down Expand Up @@ -815,7 +817,11 @@ export class Compiler extends DiagnosticEmitter {
if (!classInstance.type.isUnmanaged) {
let module = this.module;
let internalName = classInstance.internalName;
module.addGlobal(internalName, NativeType.I32, false, module.i32(classInstance.id));

if (!this.doneModuleExports.has(element)) {
module.addGlobal(internalName, NativeType.I32, false, module.i32(classInstance.id));
this.doneModuleExports.add(element);
}
module.addGlobalExport(internalName, prefix + name);
}
break;
Expand Down
20 changes: 18 additions & 2 deletions src/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1053,7 +1053,7 @@ export class Program extends DiagnosticEmitter {
let localName = localIdentifier.text;
localFile.add(
localName,
foreignFile.asImportedNamespace(
foreignFile.asAliasNamespace(
localName,
localFile,
localIdentifier
Expand Down Expand Up @@ -2893,6 +2893,8 @@ export class File extends Element {
exportsStar: File[] | null = null;
/** Top-level start function of this file. */
startFunction!: Function;
/** Array of `import * as X` alias namespaces of this file. */
aliasNamespaces: Array<Namespace> = new Array<Namespace>();

/** Constructs a new file. */
constructor(
Expand Down Expand Up @@ -2962,6 +2964,12 @@ export class File extends Element {
if (!exports) this.exports = exports = new Map();
exports.set(name, element);
if (this.source.sourceKind == SourceKind.LIBRARY_ENTRY) this.program.ensureGlobal(name, element);

// Also, add to the namespaces that capture our exports
for(let i = 0; i < this.aliasNamespaces.length; i++) {
let ns = this.aliasNamespaces[i];
ns.add(name, element);
}
}

/** Ensures that another file is a re-export of this file. */
Expand All @@ -2987,12 +2995,20 @@ export class File extends Element {
}

/** Creates an imported namespace from this file. */
asImportedNamespace(name: string, parent: Element, localIdentifier: IdentifierExpression): Namespace {
asAliasNamespace(
name: string,
parent: Element,
localIdentifier: IdentifierExpression
): Namespace {
var declaration = this.program.makeNativeNamespaceDeclaration(name);
declaration.name = localIdentifier;
var ns = new Namespace(name, parent, declaration);
ns.set(CommonFlags.SCOPED);
this.copyExportsToNamespace(ns);
// NOTE: Some exports are still queued, and can't yet be added here,
// so we remember all the alias namespaces and add to them as well
// when adding an element to the file.
this.aliasNamespaces.push(ns);
return ns;
}

Expand Down
6 changes: 6 additions & 0 deletions tests/compiler/exports.optimized.wat
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
(export "vehicles.Car.TIRES" (global $exports/vehicles.Car.TIRES))
(export "vehicles.Car.getNumTires" (func $exports/Car.getNumTires))
(export "outer.inner.a" (global $exports/outer.inner.a))
(export "renamed_mul" (func $export/mul))
(export "__setArgumentsLength" (func $~setArgumentsLength))
(start $~start)
(func $exports/add (param $0 i32) (param $1 i32) (result i32)
Expand Down Expand Up @@ -137,6 +138,11 @@
(func $exports/Car#openDoors (param $0 i32)
nop
)
(func $export/mul (param $0 i32) (param $1 i32) (result i32)
local.get $0
local.get $1
i32.mul
)
(func $~start
i32.const 1024
global.set $~lib/rt/stub/offset
Expand Down
2 changes: 2 additions & 0 deletions tests/compiler/exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,5 @@ export namespace outer {
export const a = 42;
}
}

export {renamed_mul} from "./export";
9 changes: 9 additions & 0 deletions tests/compiler/exports.untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
(global $exports/Car.TIRES i32 (i32.const 4))
(global $exports/vehicles.Car.TIRES i32 (i32.const 4))
(global $exports/outer.inner.a i32 (i32.const 42))
(global $export/a i32 (i32.const 1))
(global $export/b i32 (i32.const 2))
(global $export/c i32 (i32.const 3))
(global $~lib/rt/stub/startOffset (mut i32) (i32.const 0))
(global $~lib/rt/stub/offset (mut i32) (i32.const 0))
(global $~lib/heap/__heap_base i32 (i32.const 8))
Expand Down Expand Up @@ -47,6 +50,7 @@
(export "vehicles.Car.TIRES" (global $exports/vehicles.Car.TIRES))
(export "vehicles.Car.getNumTires" (func $exports/vehicles.Car.getNumTires))
(export "outer.inner.a" (global $exports/outer.inner.a))
(export "renamed_mul" (func $export/mul))
(export "__setArgumentsLength" (func $~setArgumentsLength))
(start $~start)
(func $exports/add (param $0 i32) (param $1 i32) (result i32)
Expand Down Expand Up @@ -260,6 +264,11 @@
(func $exports/vehicles.Car#openDoors (param $0 i32)
nop
)
(func $export/mul (param $0 i32) (param $1 i32) (result i32)
local.get $0
local.get $1
i32.mul
)
(func $~start
global.get $~lib/heap/__heap_base
i32.const 15
Expand Down
130 changes: 129 additions & 1 deletion tests/compiler/exportstar-rereexport.optimized.wat
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
(module
(type $i32_i32_=>_i32 (func (param i32 i32) (result i32)))
(type $none_=>_none (func))
(memory $0 0)
(type $i32_i32_i32_i32_=>_none (func (param i32 i32 i32 i32)))
(type $none_=>_i32 (func (result i32)))
(import "env" "abort" (func $~lib/builtins/abort (param i32 i32 i32 i32)))
(memory $0 1)
(data (i32.const 1024) "\16\00\00\00\01\00\00\00\01\00\00\00\16\00\00\00r\00e\00e\00x\00p\00o\00r\00t\00.\00t\00s")
(data (i32.const 1072) "\1a\00\00\00\01\00\00\00\01\00\00\00\1a\00\00\00r\00e\00r\00e\00e\00x\00p\00o\00r\00t\00.\00t\00s")
(global $export/a i32 (i32.const 1))
(global $export/b i32 (i32.const 2))
(global $export/c i32 (i32.const 3))
(global $~lib/rt/stub/offset (mut i32) (i32.const 0))
(global $reexport/car (mut i32) (i32.const 0))
(global $rereexport/car (mut i32) (i32.const 0))
(global $rereexport/exportsNamespaceCar (mut i32) (i32.const 0))
(export "memory" (memory $0))
(export "a" (global $export/a))
(export "renamed_a" (global $export/a))
Expand All @@ -20,6 +29,7 @@
(export "exportstar.renamed_c" (global $export/c))
(export "exportstar.ns.two" (func $export-default/theDefault))
(export "exportstar.default.two" (func $export-default/theDefault))
(start $~start)
(func $export/add (param $0 i32) (param $1 i32) (result i32)
local.get $0
local.get $1
Expand All @@ -30,6 +40,78 @@
local.get $1
i32.mul
)
(func $exports/Car#constructor (result i32)
(local $0 i32)
(local $1 i32)
(local $2 i32)
(local $3 i32)
global.get $~lib/rt/stub/offset
i32.const 16
i32.add
local.tee $1
i32.const 16
i32.add
local.tee $0
memory.size
local.tee $3
i32.const 16
i32.shl
local.tee $2
i32.gt_u
if
local.get $3
local.get $0
local.get $2
i32.sub
i32.const 65535
i32.add
i32.const -65536
i32.and
i32.const 16
i32.shr_u
local.tee $2
local.get $3
local.get $2
i32.gt_s
select
memory.grow
i32.const 0
i32.lt_s
if
local.get $2
memory.grow
i32.const 0
i32.lt_s
if
unreachable
end
end
end
local.get $0
global.set $~lib/rt/stub/offset
local.get $1
i32.const 16
i32.sub
local.tee $0
i32.const 16
i32.store
local.get $0
i32.const 1
i32.store offset=4
local.get $0
i32.const 3
i32.store offset=8
local.get $0
i32.const 4
i32.store offset=12
local.get $1
i32.const 2
i32.store
local.get $1
i32.const 2
i32.store
local.get $1
)
(func $export-default/theDefault
nop
)
Expand All @@ -38,4 +120,50 @@
local.get $1
i32.sub
)
(func $~start
i32.const 1120
global.set $~lib/rt/stub/offset
call $exports/Car#constructor
global.set $reexport/car
global.get $reexport/car
i32.load
i32.const 2
i32.ne
if
i32.const 0
i32.const 1040
i32.const 40
i32.const 1
call $~lib/builtins/abort
unreachable
end
call $exports/Car#constructor
global.set $rereexport/car
global.get $rereexport/car
i32.load
i32.const 2
i32.ne
if
i32.const 0
i32.const 1088
i32.const 18
i32.const 1
call $~lib/builtins/abort
unreachable
end
call $exports/Car#constructor
global.set $rereexport/exportsNamespaceCar
global.get $rereexport/exportsNamespaceCar
i32.load
i32.const 2
i32.ne
if
i32.const 0
i32.const 1088
i32.const 24
i32.const 1
call $~lib/builtins/abort
unreachable
end
)
)
Loading