From 2aa547064182d0a3ec789339962aa868c8db887c Mon Sep 17 00:00:00 2001 From: Aaron Turner Date: Tue, 28 Jul 2020 18:31:16 -0700 Subject: [PATCH 01/10] Proved a new import * bug --- tests/compiler/exports.ts | 2 ++ tests/compiler/reexport.ts | 4 ++++ 2 files changed, 6 insertions(+) diff --git a/tests/compiler/exports.ts b/tests/compiler/exports.ts index 5953536e4a..68deb91cac 100644 --- a/tests/compiler/exports.ts +++ b/tests/compiler/exports.ts @@ -56,3 +56,5 @@ export namespace outer { export const a = 42; } } + +export {mul} from "./export"; diff --git a/tests/compiler/reexport.ts b/tests/compiler/reexport.ts index f8b3fdb1cf..5e87b5bc7b 100644 --- a/tests/compiler/reexport.ts +++ b/tests/compiler/reexport.ts @@ -28,5 +28,9 @@ export { ns as renamed_ns } from "./export"; import * as exportstar from "./exportstar"; export { exportstar }; +import * as BasicExports from "./exports"; +assert(BasicExports.add(2, 2) == 4); +assert(BasicExports.mul(2, 2) == 4); + export { default } from "./export-default"; export { default as renamed_default } from "./export-default"; From d5261e00efdbd188f9b2d92e2a09e9089f21e078 Mon Sep 17 00:00:00 2001 From: Aaron Turner Date: Wed, 29 Jul 2020 17:36:39 -0700 Subject: [PATCH 02/10] Fixed the test, and still doesn't work --- tests/compiler/exports.ts | 2 +- tests/compiler/reexport.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/compiler/exports.ts b/tests/compiler/exports.ts index 68deb91cac..c18fc6a1fa 100644 --- a/tests/compiler/exports.ts +++ b/tests/compiler/exports.ts @@ -57,4 +57,4 @@ export namespace outer { } } -export {mul} from "./export"; +export {renamed_mul} from "./export"; diff --git a/tests/compiler/reexport.ts b/tests/compiler/reexport.ts index 5e87b5bc7b..c1c6ff3c74 100644 --- a/tests/compiler/reexport.ts +++ b/tests/compiler/reexport.ts @@ -30,7 +30,7 @@ export { exportstar }; import * as BasicExports from "./exports"; assert(BasicExports.add(2, 2) == 4); -assert(BasicExports.mul(2, 2) == 4); +assert(BasicExports.renamed_mul(2, 2) == 4); export { default } from "./export-default"; export { default as renamed_default } from "./export-default"; From 686c720a8429074cfd3505ecf3f6652f3bb6243a Mon Sep 17 00:00:00 2001 From: Aaron Turner Date: Thu, 30 Jul 2020 18:42:07 -0700 Subject: [PATCH 03/10] Removing the queued export did not work for fatal fix --- src/compiler.ts | 1 + src/program.ts | 45 ++++- tests/compiler/reexport.optimized.wat | 131 ++++++++++++- tests/compiler/reexport.ts | 13 +- tests/compiler/reexport.untouched.wat | 246 +++++++++++++++++++++++- tests/compiler/rereexport.ts | 14 ++ tests/compiler/rereexport.untouched.wat | 65 ------- 7 files changed, 441 insertions(+), 74 deletions(-) diff --git a/src/compiler.ts b/src/compiler.ts index 3d7228765c..dc5cedfd55 100644 --- a/src/compiler.ts +++ b/src/compiler.ts @@ -9194,6 +9194,7 @@ export class Compiler extends DiagnosticEmitter { let memberName = names[i].text; let member: DeclaredElement; if (!members || !members.has(memberName) || (member = assert(members.get(memberName))).kind != ElementKind.FIELD) { + console.log('Yoooooo this the error'); this.error( DiagnosticCode.Property_0_does_not_exist_on_type_1, names[i].range, memberName, classType.toString() diff --git a/src/program.ts b/src/program.ts index 8cc6ba4347..c8ea65ba63 100644 --- a/src/program.ts +++ b/src/program.ts @@ -1056,7 +1056,9 @@ export class Program extends DiagnosticEmitter { foreignFile.asImportedNamespace( localName, localFile, - localIdentifier + localIdentifier, + this, + queuedExports ), localIdentifier // isImport ); @@ -1609,7 +1611,7 @@ export class Program extends DiagnosticEmitter { } /** Tries to locate a foreign element by traversing exports and queued exports. */ - private lookupForeign( + lookupForeign( /** Identifier within the other file. */ foreignName: string, /** Normalized path to the other file. */ @@ -2987,12 +2989,49 @@ export class File extends Element { } /** Creates an imported namespace from this file. */ - asImportedNamespace(name: string, parent: Element, localIdentifier: IdentifierExpression): Namespace { + asImportedNamespace( + name: string, + parent: Element, + localIdentifier: IdentifierExpression, + program: Program, + queuedExports: Map> + ): Namespace { var declaration = this.program.makeNativeNamespaceDeclaration(name); declaration.name = localIdentifier; var ns = new Namespace(name, parent, declaration); ns.set(CommonFlags.SCOPED); this.copyExportsToNamespace(ns); + + // Add any queued exports to the namespace as well + if (queuedExports.has(this)) { + let fileQueuedExports = assert(queuedExports.get(this)); + for (let _keys = Map_keys(fileQueuedExports), i = 0, k = _keys.length; i < k; ++i) { + let exportName = unchecked(_keys[i]); + let queuedExport = assert(fileQueuedExports.get(exportName)); + let localName = queuedExport.localIdentifier.text; + let foreignPath = queuedExport.foreignPath; + if (foreignPath) { + let element = program.lookupForeign( + localName, + foreignPath, + assert(queuedExport.foreignPathAlt), // must be set if foreignPath is + queuedExports + ); + if (element) { + ns.add(exportName, element); + } else { + program.error( + DiagnosticCode.Module_0_has_no_exported_member_1, + queuedExport.localIdentifier.range, + foreignPath, localName + ); + } + } else { + // Should have already been added + } + } + } + return ns; } diff --git a/tests/compiler/reexport.optimized.wat b/tests/compiler/reexport.optimized.wat index ff82192864..98fe6dc9a6 100644 --- a/tests/compiler/reexport.optimized.wat +++ b/tests/compiler/reexport.optimized.wat @@ -1,10 +1,19 @@ (module (type $i32_i32_=>_i32 (func (param i32 i32) (result i32))) (type $none_=>_none (func)) - (memory $0 0) + (type $i32_=>_none (func (param i32))) + (type $i32_i32_=>_none (func (param i32 i32))) + (type $none_=>_i32 (func (result i32))) + (type $i32_=>_i32 (func (param i32) (result 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") (global $export/a i32 (i32.const 1)) (global $export/b i32 (i32.const 2)) (global $export/c i32 (i32.const 3)) + (global $exports/Car.TIRES i32 (i32.const 4)) + (global $~lib/rt/stub/offset (mut i32) (i32.const 0)) + (global $exports/Car i32 (i32.const 3)) + (global $~argumentsLength (mut i32) (i32.const 0)) (export "memory" (memory $0)) (export "add" (func $export/add)) (export "renamed_sub" (func $export/sub)) @@ -14,6 +23,15 @@ (export "renamed_b" (global $export/b)) (export "renamed_c" (global $export/c)) (export "rerenamed_c" (global $export/c)) + (export "Car" (global $exports/Car)) + (export "Car#get:doors" (func $exports/Car#get:doors)) + (export "Car#set:doors" (func $exports/Car#set:doors)) + (export "Car#constructor" (func $exports/Car#constructor@varargs)) + (export "Car#get:numDoors" (func $exports/Car#get:doors)) + (export "Car#set:numDoors" (func $exports/Car#set:doors)) + (export "Car#openDoors" (func $exports/Car#openDoors)) + (export "Car.TIRES" (global $exports/Car.TIRES)) + (export "Car.getNumTires" (func $exports/Car.getNumTires)) (export "renamed_add" (func $export/add)) (export "rerenamed_sub" (func $export/mul)) (export "renamed_ns.two" (func $export/ns.one)) @@ -27,6 +45,8 @@ (export "exportstar.default.two" (func $export/ns.one)) (export "default" (func $export/ns.one)) (export "renamed_default" (func $export/ns.one)) + (export "__setArgumentsLength" (func $~setArgumentsLength)) + (start $~start) (func $export/add (param $0 i32) (param $1 i32) (result i32) local.get $0 local.get $1 @@ -42,7 +62,116 @@ local.get $1 i32.sub ) + (func $exports/Car.getNumTires (result i32) + i32.const 4 + ) + (func $exports/Car#get:doors (param $0 i32) (result i32) + local.get $0 + i32.load + ) + (func $exports/Car#set:doors (param $0 i32) (param $1 i32) + local.get $0 + local.get $1 + i32.store + ) + (func $exports/Car#openDoors (param $0 i32) + nop + ) (func $export/ns.one nop ) + (func $~start + i32.const 1072 + global.set $~lib/rt/stub/offset + ) + (func $exports/Car#constructor@varargs (param $0 i32) (param $1 i32) (result i32) + (local $2 i32) + (local $3 i32) + (local $4 i32) + block $1of1 + block $0of1 + block $outOfRange + global.get $~argumentsLength + br_table $0of1 $1of1 $outOfRange + end + unreachable + end + i32.const 2 + local.set $1 + end + local.get $0 + i32.eqz + if + global.get $~lib/rt/stub/offset + i32.const 16 + i32.add + local.tee $0 + i32.const 16 + i32.add + local.tee $2 + memory.size + local.tee $4 + i32.const 16 + i32.shl + local.tee $3 + i32.gt_u + if + local.get $4 + local.get $2 + local.get $3 + i32.sub + i32.const 65535 + i32.add + i32.const -65536 + i32.and + i32.const 16 + i32.shr_u + local.tee $3 + local.get $4 + local.get $3 + i32.gt_s + select + memory.grow + i32.const 0 + i32.lt_s + if + local.get $3 + memory.grow + i32.const 0 + i32.lt_s + if + unreachable + end + end + end + local.get $2 + global.set $~lib/rt/stub/offset + local.get $0 + i32.const 16 + i32.sub + local.tee $2 + i32.const 16 + i32.store + local.get $2 + i32.const 1 + i32.store offset=4 + local.get $2 + i32.const 3 + i32.store offset=8 + local.get $2 + i32.const 4 + i32.store offset=12 + end + local.get $0 + local.get $1 + i32.store + local.get $0 + local.get $1 + i32.store + local.get $0 + ) + (func $~setArgumentsLength (param $0 i32) + local.get $0 + global.set $~argumentsLength + ) ) diff --git a/tests/compiler/reexport.ts b/tests/compiler/reexport.ts index c1c6ff3c74..35d2addbf9 100644 --- a/tests/compiler/reexport.ts +++ b/tests/compiler/reexport.ts @@ -10,6 +10,10 @@ export { renamed_c as rerenamed_c } from "./export"; +export { + Car +} from "./exports"; + import { add as imported_add, renamed_mul as imported_sub, @@ -28,9 +32,12 @@ export { ns as renamed_ns } from "./export"; import * as exportstar from "./exportstar"; export { exportstar }; -import * as BasicExports from "./exports"; -assert(BasicExports.add(2, 2) == 4); -assert(BasicExports.renamed_mul(2, 2) == 4); +import * as ExportsNamespace from "./exports"; +export { ExportsNamespace }; +assert(ExportsNamespace.add(2, 2) == 4); +assert(ExportsNamespace.renamed_mul(2, 2) == 4); +let car: ExportsNamespace.Car = new ExportsNamespace.Car(); +assert(car.numDoors == 2); export { default } from "./export-default"; export { default as renamed_default } from "./export-default"; diff --git a/tests/compiler/reexport.untouched.wat b/tests/compiler/reexport.untouched.wat index f1fc2f503b..106656a22e 100644 --- a/tests/compiler/reexport.untouched.wat +++ b/tests/compiler/reexport.untouched.wat @@ -1,11 +1,26 @@ (module - (type $none_=>_none (func)) (type $i32_i32_=>_i32 (func (param i32 i32) (result i32))) - (memory $0 0) + (type $none_=>_none (func)) + (type $i32_=>_none (func (param i32))) + (type $i32_=>_i32 (func (param i32) (result i32))) + (type $i32_i32_=>_none (func (param i32 i32))) + (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 16) "\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\00") (table $0 1 funcref) (global $export/a i32 (i32.const 1)) (global $export/b i32 (i32.const 2)) (global $export/c i32 (i32.const 3)) + (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 $~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 56)) + (global $exports/Car i32 (i32.const 3)) + (global $~argumentsLength (mut i32) (i32.const 0)) (export "memory" (memory $0)) (export "add" (func $export/add)) (export "renamed_sub" (func $export/sub)) @@ -15,6 +30,15 @@ (export "renamed_b" (global $export/b)) (export "renamed_c" (global $export/c)) (export "rerenamed_c" (global $export/c)) + (export "Car" (global $exports/Car)) + (export "Car#get:doors" (func $exports/Car#get:doors)) + (export "Car#set:doors" (func $exports/Car#set:doors)) + (export "Car#constructor" (func $exports/Car#constructor@varargs)) + (export "Car#get:numDoors" (func $exports/Car#get:numDoors)) + (export "Car#set:numDoors" (func $exports/Car#set:numDoors)) + (export "Car#openDoors" (func $exports/Car#openDoors)) + (export "Car.TIRES" (global $exports/Car.TIRES)) + (export "Car.getNumTires" (func $exports/Car.getNumTires)) (export "renamed_add" (func $export/add)) (export "rerenamed_sub" (func $export/mul)) (export "renamed_ns.two" (func $export/ns.two)) @@ -28,6 +52,7 @@ (export "exportstar.default.two" (func $export/ns.two)) (export "default" (func $export-default/theDefault)) (export "renamed_default" (func $export-default/theDefault)) + (export "__setArgumentsLength" (func $~setArgumentsLength)) (start $~start) (func $export/add (param $0 i32) (param $1 i32) (result i32) local.get $0 @@ -39,6 +64,11 @@ local.get $1 i32.mul ) + (func $exports/add (param $0 i32) (param $1 i32) (result i32) + local.get $0 + local.get $1 + i32.add + ) (func $start:reexport i32.const 1 i32.const 2 @@ -48,12 +78,194 @@ call $export/mul i32.add drop + i32.const 2 + i32.const 2 + call $exports/add + i32.const 4 + i32.eq + i32.eqz + if + i32.const 0 + i32.const 32 + i32.const 36 + i32.const 1 + call $~lib/builtins/abort + unreachable + end + i32.const 2 + i32.const 2 + call $export/mul + i32.const 4 + i32.eq + i32.eqz + if + i32.const 0 + i32.const 32 + i32.const 37 + i32.const 1 + call $~lib/builtins/abort + unreachable + end ) (func $export/sub (param $0 i32) (param $1 i32) (result i32) local.get $0 local.get $1 i32.sub ) + (func $exports/Car.getNumTires (result i32) + global.get $exports/Car.TIRES + ) + (func $~lib/rt/stub/maybeGrowMemory (param $0 i32) + (local $1 i32) + (local $2 i32) + (local $3 i32) + (local $4 i32) + (local $5 i32) + memory.size + local.set $1 + local.get $1 + i32.const 16 + i32.shl + local.set $2 + local.get $0 + local.get $2 + i32.gt_u + if + local.get $0 + local.get $2 + i32.sub + i32.const 65535 + i32.add + i32.const 65535 + i32.const -1 + i32.xor + i32.and + i32.const 16 + i32.shr_u + local.set $3 + local.get $1 + local.tee $4 + local.get $3 + local.tee $5 + local.get $4 + local.get $5 + i32.gt_s + select + local.set $4 + local.get $4 + memory.grow + i32.const 0 + i32.lt_s + if + local.get $3 + memory.grow + i32.const 0 + i32.lt_s + if + unreachable + end + end + end + local.get $0 + global.set $~lib/rt/stub/offset + ) + (func $~lib/rt/stub/__alloc (param $0 i32) (param $1 i32) (result i32) + (local $2 i32) + (local $3 i32) + (local $4 i32) + (local $5 i32) + (local $6 i32) + local.get $0 + i32.const 1073741808 + i32.gt_u + if + unreachable + end + global.get $~lib/rt/stub/offset + i32.const 16 + i32.add + local.set $2 + local.get $0 + i32.const 15 + i32.add + i32.const 15 + i32.const -1 + i32.xor + i32.and + local.tee $3 + i32.const 16 + local.tee $4 + local.get $3 + local.get $4 + i32.gt_u + select + local.set $5 + local.get $2 + local.get $5 + i32.add + call $~lib/rt/stub/maybeGrowMemory + local.get $2 + i32.const 16 + i32.sub + local.set $6 + local.get $6 + local.get $5 + i32.store + i32.const 1 + drop + local.get $6 + i32.const 1 + i32.store offset=4 + local.get $6 + local.get $1 + i32.store offset=8 + local.get $6 + local.get $0 + i32.store offset=12 + local.get $2 + ) + (func $~lib/rt/stub/__retain (param $0 i32) (result i32) + local.get $0 + ) + (func $exports/Car#constructor (param $0 i32) (param $1 i32) (result i32) + local.get $0 + i32.eqz + if + i32.const 4 + i32.const 3 + call $~lib/rt/stub/__alloc + call $~lib/rt/stub/__retain + local.set $0 + end + local.get $0 + local.get $1 + i32.store + local.get $0 + local.get $1 + i32.store + local.get $0 + ) + (func $exports/Car#get:doors (param $0 i32) (result i32) + local.get $0 + i32.load + ) + (func $exports/Car#set:doors (param $0 i32) (param $1 i32) + local.get $0 + local.get $1 + i32.store + ) + (func $exports/Car#get:numDoors (param $0 i32) (result i32) + local.get $0 + i32.load + ) + (func $exports/Car#set:numDoors (param $0 i32) (param $1 i32) + local.get $0 + local.get $1 + i32.store + ) + (func $exports/Car#openDoors (param $0 i32) + nop + ) (func $export/ns.one nop ) @@ -65,5 +277,35 @@ ) (func $~start call $start:reexport + global.get $~lib/heap/__heap_base + i32.const 15 + i32.add + i32.const 15 + i32.const -1 + i32.xor + i32.and + global.set $~lib/rt/stub/startOffset + global.get $~lib/rt/stub/startOffset + global.set $~lib/rt/stub/offset + ) + (func $exports/Car#constructor@varargs (param $0 i32) (param $1 i32) (result i32) + block $1of1 + block $0of1 + block $outOfRange + global.get $~argumentsLength + br_table $0of1 $1of1 $outOfRange + end + unreachable + end + i32.const 2 + local.set $1 + end + local.get $0 + local.get $1 + call $exports/Car#constructor + ) + (func $~setArgumentsLength (param $0 i32) + local.get $0 + global.set $~argumentsLength ) ) diff --git a/tests/compiler/rereexport.ts b/tests/compiler/rereexport.ts index 29aeb41abe..b0b1e55837 100644 --- a/tests/compiler/rereexport.ts +++ b/tests/compiler/rereexport.ts @@ -9,3 +9,17 @@ export { import { exportstar } from "./reexport"; export { exportstar }; + +import * as ReexportsNamespace from "./reexport"; +// Test our import * as namespace works with different types +assert(ReexportsNamespace.add(2, 2) == 4); +assert(ReexportsNamespace.rerenamed_mul(2, 2) == 4); +let car: ReexportsNamespace.Car = new ReexportsNamespace.Car(); +assert(car.numDoors == 2); + +//Test our imported namespace with the exported import * as namespace +assert(ReexportsNamespace.ExportsNamespace.add(2, 2) == 4); +assert(ReexportsNamespace.ExportsNamespace.renamed_mul(2, 2) == 4); +let exportsNamespaceCar: ReexportsNamespace.Car = new ReexportsNamespace.ExportsNamespace.Car(); +assert(exportsNamespaceCar.numDoors == 2); + diff --git a/tests/compiler/rereexport.untouched.wat b/tests/compiler/rereexport.untouched.wat index dd449374cc..e69de29bb2 100644 --- a/tests/compiler/rereexport.untouched.wat +++ b/tests/compiler/rereexport.untouched.wat @@ -1,65 +0,0 @@ -(module - (type $none_=>_none (func)) - (type $i32_i32_=>_i32 (func (param i32 i32) (result i32))) - (memory $0 0) - (table $0 1 funcref) - (global $export/a i32 (i32.const 1)) - (global $export/b i32 (i32.const 2)) - (global $export/c i32 (i32.const 3)) - (export "memory" (memory $0)) - (export "a" (global $export/a)) - (export "renamed_a" (global $export/a)) - (export "renamed_b" (global $export/b)) - (export "renamed_renamed_b" (global $export/b)) - (export "default" (func $export-default/theDefault)) - (export "renamed_default" (func $export-default/theDefault)) - (export "exportstar.add" (func $export/add)) - (export "exportstar.sub" (func $export/sub)) - (export "exportstar.renamed_mul" (func $export/mul)) - (export "exportstar.a" (global $export/a)) - (export "exportstar.b" (global $export/b)) - (export "exportstar.renamed_c" (global $export/c)) - (export "exportstar.ns.two" (func $export/ns.two)) - (export "exportstar.default.two" (func $export/ns.two)) - (start $~start) - (func $export/add (param $0 i32) (param $1 i32) (result i32) - local.get $0 - local.get $1 - i32.add - ) - (func $export/mul (param $0 i32) (param $1 i32) (result i32) - local.get $0 - local.get $1 - i32.mul - ) - (func $start:reexport - i32.const 1 - i32.const 2 - call $export/add - i32.const 3 - i32.const 4 - call $export/mul - i32.add - drop - ) - (func $start:rereexport - call $start:reexport - ) - (func $export-default/theDefault - nop - ) - (func $export/sub (param $0 i32) (param $1 i32) (result i32) - local.get $0 - local.get $1 - i32.sub - ) - (func $export/ns.one - nop - ) - (func $export/ns.two - nop - ) - (func $~start - call $start:rereexport - ) -) From 4ae91d67c647888e92bca6a47de1832a5310c55b Mon Sep 17 00:00:00 2001 From: Aaron Turner Date: Fri, 31 Jul 2020 17:04:09 -0700 Subject: [PATCH 04/10] Fixed the wild import * exports bug --- src/compiler.ts | 8 +- src/program.ts | 101 ++++--- tests/compiler/reexport.optimized.wat | 248 +++++++++++----- tests/compiler/reexport.untouched.wat | 262 +++++++++++++---- tests/compiler/rereexport.optimized.wat | 130 ++++++++- tests/compiler/rereexport.ts | 3 +- tests/compiler/rereexport.untouched.wat | 364 ++++++++++++++++++++++++ 7 files changed, 944 insertions(+), 172 deletions(-) diff --git a/src/compiler.ts b/src/compiler.ts index dc5cedfd55..9d9fbc2518 100644 --- a/src/compiler.ts +++ b/src/compiler.ts @@ -815,6 +815,13 @@ export class Compiler extends DiagnosticEmitter { if (!classInstance.type.isUnmanaged) { let module = this.module; let internalName = classInstance.internalName; + + // We remove the global before adding it, as this ensures + // we are not adding the same global twice. This is needed, + // As we can export a class from a file, and in that sample file, + // Export a namspace (import * as), that exports the same class. + // And there is no module.hasGlobal(), and this works fine :) + module.removeGlobal(internalName); module.addGlobal(internalName, NativeType.I32, false, module.i32(classInstance.id)); module.addGlobalExport(internalName, prefix + name); } @@ -9194,7 +9201,6 @@ export class Compiler extends DiagnosticEmitter { let memberName = names[i].text; let member: DeclaredElement; if (!members || !members.has(memberName) || (member = assert(members.get(memberName))).kind != ElementKind.FIELD) { - console.log('Yoooooo this the error'); this.error( DiagnosticCode.Property_0_does_not_exist_on_type_1, names[i].range, memberName, classType.toString() diff --git a/src/program.ts b/src/program.ts index c8ea65ba63..132379f1f0 100644 --- a/src/program.ts +++ b/src/program.ts @@ -1022,6 +1022,11 @@ export class Program extends DiagnosticEmitter { // queued imports should be resolvable now through traversing exports and queued exports. // note that imports may depend upon imports, so repeat until there's no more progress. + // + // Also, we are creating a map for imported namespaces to have their exports resolved + // while we are resolving exports + // Our importNameSpaces traverses like foreignFile -> localFile -> namespaceNames + let importNamespacesMap = new Map>>(); do { let i = 0, madeProgress = false; while (i < queuedImports.length) { @@ -1056,14 +1061,25 @@ export class Program extends DiagnosticEmitter { foreignFile.asImportedNamespace( localName, localFile, - localIdentifier, - this, - queuedExports + localIdentifier ), localIdentifier // isImport ); queuedImports.splice(i, 1); madeProgress = true; + + // Also, make sure to record the namespace in our importNamespaces for later + if (importNamespacesMap.has(foreignFile)) { + let importNamespaceLocalFileMap = importNamespacesMap.get(foreignFile) as Map>; + let importNamespaceNames = importNamespaceLocalFileMap.get(localFile) as Array; + importNamespaceNames.push(localName); + } else { + let importNamespaceLocalFileMap = new Map>(); + let importNamespaceNames = new Array(); + importNamespaceNames.push(localName); + importNamespaceLocalFileMap.set(localFile, importNamespaceNames); + importNamespacesMap.set(foreignFile, importNamespaceLocalFileMap); + } } else { ++i; assert(false); // already reported by the parser not finding the file @@ -1106,6 +1122,27 @@ export class Program extends DiagnosticEmitter { ); if (element) { file.ensureExport(exportName, element); + + // Check if there is a namespace that also needs this + if (importNamespacesMap.has(file)) { + + let importNamespaceLocalFileMap = importNamespacesMap.get(file) as Map>; + let importNameSpaceLocalFileKeys = Map_keys(importNamespaceLocalFileMap); + let importNameSpaceLocalFileKeysLength = importNameSpaceLocalFileKeys.length; + for (let ii = 0; ii < importNameSpaceLocalFileKeysLength; ++ii) { + let localFile = importNameSpaceLocalFileKeys[ii]; + + // Get our namespaces + let importNamespacesNames = importNamespaceLocalFileMap.get(localFile) as Array; + for (let jj = 0; jj < importNamespacesNames.length; jj++) { + let namespace = localFile.lookup(importNamespacesNames[jj]); + if (namespace && exportName !== importNamespacesNames[jj]) { + namespace.add(exportName, element); + } + } + } + } + } else { this.error( DiagnosticCode.Module_0_has_no_exported_member_1, @@ -1117,6 +1154,27 @@ export class Program extends DiagnosticEmitter { let element = file.lookupInSelf(localName); if (element) { file.ensureExport(exportName, element); + + // Check if there is a namespace that also needs this + if (importNamespacesMap.has(file)) { + + let importNamespaceLocalFileMap = importNamespacesMap.get(file) as Map>; + let importNameSpaceLocalFileKeys = Map_keys(importNamespaceLocalFileMap); + let importNameSpaceLocalFileKeysLength = importNameSpaceLocalFileKeys.length; + for (let ii = 0; ii < importNameSpaceLocalFileKeysLength; ++ii) { + let localFile = importNameSpaceLocalFileKeys[ii]; + + // Get our namespaces + let importNamespacesNames = importNamespaceLocalFileMap.get(localFile) as Array; + for (let jj = 0; jj < importNamespacesNames.length; jj++) { + let namespace = localFile.lookup(importNamespacesNames[jj]); + if (namespace && exportName !== importNamespacesNames[jj]) { + namespace.add(exportName, element); + } + } + } + } + } else { let globalElement = this.lookupGlobal(localName); if (globalElement !== null && isDeclaredElement(globalElement.kind)) { // export { memory } @@ -2992,46 +3050,15 @@ export class File extends Element { asImportedNamespace( name: string, parent: Element, - localIdentifier: IdentifierExpression, - program: Program, - queuedExports: Map> + 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); - - // Add any queued exports to the namespace as well - if (queuedExports.has(this)) { - let fileQueuedExports = assert(queuedExports.get(this)); - for (let _keys = Map_keys(fileQueuedExports), i = 0, k = _keys.length; i < k; ++i) { - let exportName = unchecked(_keys[i]); - let queuedExport = assert(fileQueuedExports.get(exportName)); - let localName = queuedExport.localIdentifier.text; - let foreignPath = queuedExport.foreignPath; - if (foreignPath) { - let element = program.lookupForeign( - localName, - foreignPath, - assert(queuedExport.foreignPathAlt), // must be set if foreignPath is - queuedExports - ); - if (element) { - ns.add(exportName, element); - } else { - program.error( - DiagnosticCode.Module_0_has_no_exported_member_1, - queuedExport.localIdentifier.range, - foreignPath, localName - ); - } - } else { - // Should have already been added - } - } - } - + // NOTE: Some exports are still queued, and are not added here. + // But will be added to the namespace when the exports are being resolved. return ns; } diff --git a/tests/compiler/reexport.optimized.wat b/tests/compiler/reexport.optimized.wat index 98fe6dc9a6..16aa1f3533 100644 --- a/tests/compiler/reexport.optimized.wat +++ b/tests/compiler/reexport.optimized.wat @@ -2,18 +2,28 @@ (type $i32_i32_=>_i32 (func (param i32 i32) (result i32))) (type $none_=>_none (func)) (type $i32_=>_none (func (param i32))) + (type $i32_=>_i32 (func (param i32) (result i32))) (type $i32_i32_=>_none (func (param i32 i32))) + (type $i32_i32_i32_i32_=>_none (func (param i32 i32 i32 i32))) (type $none_=>_i32 (func (result i32))) - (type $i32_=>_i32 (func (param i32) (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") (global $export/a i32 (i32.const 1)) (global $export/b i32 (i32.const 2)) (global $export/c i32 (i32.const 3)) + (global $exports/Animal.CAT i32 (i32.const 0)) + (global $exports/Animal.DOG i32 (i32.const 1)) + (global $exports/animals.Animal.CAT i32 (i32.const 0)) + (global $exports/animals.Animal.DOG i32 (i32.const 1)) (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 $~lib/rt/stub/offset (mut i32) (i32.const 0)) - (global $exports/Car i32 (i32.const 3)) + (global $reexport/car (mut i32) (i32.const 0)) (global $~argumentsLength (mut i32) (i32.const 0)) + (global $exports/Car i32 (i32.const 3)) + (global $exports/vehicles.Car i32 (i32.const 4)) (export "memory" (memory $0)) (export "add" (func $export/add)) (export "renamed_sub" (func $export/sub)) @@ -24,10 +34,10 @@ (export "renamed_c" (global $export/c)) (export "rerenamed_c" (global $export/c)) (export "Car" (global $exports/Car)) - (export "Car#get:doors" (func $exports/Car#get:doors)) + (export "Car#get:doors" (func $exports/Car#get:numDoors)) (export "Car#set:doors" (func $exports/Car#set:doors)) (export "Car#constructor" (func $exports/Car#constructor@varargs)) - (export "Car#get:numDoors" (func $exports/Car#get:doors)) + (export "Car#get:numDoors" (func $exports/Car#get:numDoors)) (export "Car#set:numDoors" (func $exports/Car#set:doors)) (export "Car#openDoors" (func $exports/Car#openDoors)) (export "Car.TIRES" (global $exports/Car.TIRES)) @@ -43,6 +53,33 @@ (export "exportstar.renamed_c" (global $export/c)) (export "exportstar.ns.two" (func $export/ns.one)) (export "exportstar.default.two" (func $export/ns.one)) + (export "ExportsNamespace.add" (func $export/add)) + (export "ExportsNamespace.subOpt" (func $exports/subOpt@varargs)) + (export "ExportsNamespace.math.sub" (func $export/sub)) + (export "ExportsNamespace.Animal.CAT" (global $exports/Animal.CAT)) + (export "ExportsNamespace.Animal.DOG" (global $exports/Animal.DOG)) + (export "ExportsNamespace.animals.Animal.CAT" (global $exports/animals.Animal.CAT)) + (export "ExportsNamespace.animals.Animal.DOG" (global $exports/animals.Animal.DOG)) + (export "ExportsNamespace.Car" (global $exports/Car)) + (export "ExportsNamespace.Car#get:doors" (func $exports/Car#get:numDoors)) + (export "ExportsNamespace.Car#set:doors" (func $exports/Car#set:doors)) + (export "ExportsNamespace.Car#constructor" (func $exports/Car#constructor@varargs)) + (export "ExportsNamespace.Car#get:numDoors" (func $exports/Car#get:numDoors)) + (export "ExportsNamespace.Car#set:numDoors" (func $exports/Car#set:doors)) + (export "ExportsNamespace.Car#openDoors" (func $exports/Car#openDoors)) + (export "ExportsNamespace.Car.TIRES" (global $exports/Car.TIRES)) + (export "ExportsNamespace.Car.getNumTires" (func $exports/Car.getNumTires)) + (export "ExportsNamespace.vehicles.Car" (global $exports/vehicles.Car)) + (export "ExportsNamespace.vehicles.Car#get:doors" (func $exports/Car#get:numDoors)) + (export "ExportsNamespace.vehicles.Car#set:doors" (func $exports/Car#set:doors)) + (export "ExportsNamespace.vehicles.Car#constructor" (func $exports/vehicles.Car#constructor@varargs)) + (export "ExportsNamespace.vehicles.Car#get:numDoors" (func $exports/Car#get:numDoors)) + (export "ExportsNamespace.vehicles.Car#set:numDoors" (func $exports/Car#set:doors)) + (export "ExportsNamespace.vehicles.Car#openDoors" (func $exports/Car#openDoors)) + (export "ExportsNamespace.vehicles.Car.TIRES" (global $exports/vehicles.Car.TIRES)) + (export "ExportsNamespace.vehicles.Car.getNumTires" (func $exports/Car.getNumTires)) + (export "ExportsNamespace.outer.inner.a" (global $exports/outer.inner.a)) + (export "ExportsNamespace.renamed_mul" (func $export/mul)) (export "default" (func $export/ns.one)) (export "renamed_default" (func $export/ns.one)) (export "__setArgumentsLength" (func $~setArgumentsLength)) @@ -57,6 +94,92 @@ local.get $1 i32.mul ) + (func $~lib/rt/stub/__alloc (param $0 i32) (result i32) + (local $1 i32) + (local $2 i32) + (local $3 i32) + (local $4 i32) + global.get $~lib/rt/stub/offset + i32.const 16 + i32.add + local.tee $3 + i32.const 16 + i32.add + local.tee $1 + memory.size + local.tee $4 + i32.const 16 + i32.shl + local.tee $2 + i32.gt_u + if + local.get $4 + local.get $1 + 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 $4 + 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 $1 + global.set $~lib/rt/stub/offset + local.get $3 + i32.const 16 + i32.sub + local.tee $1 + i32.const 16 + i32.store + local.get $1 + i32.const 1 + i32.store offset=4 + local.get $1 + local.get $0 + i32.store offset=8 + local.get $1 + i32.const 4 + i32.store offset=12 + local.get $3 + ) + (func $exports/Car#constructor (param $0 i32) (param $1 i32) (result i32) + local.get $0 + i32.eqz + if + i32.const 3 + call $~lib/rt/stub/__alloc + local.set $0 + end + local.get $0 + local.get $1 + i32.store + local.get $0 + local.get $1 + i32.store + local.get $0 + ) + (func $exports/Car#get:numDoors (param $0 i32) (result i32) + local.get $0 + i32.load + ) (func $export/sub (param $0 i32) (param $1 i32) (result i32) local.get $0 local.get $1 @@ -65,10 +188,6 @@ (func $exports/Car.getNumTires (result i32) i32.const 4 ) - (func $exports/Car#get:doors (param $0 i32) (result i32) - local.get $0 - i32.load - ) (func $exports/Car#set:doors (param $0 i32) (param $1 i32) local.get $0 local.get $1 @@ -83,11 +202,24 @@ (func $~start i32.const 1072 global.set $~lib/rt/stub/offset + i32.const 0 + i32.const 2 + 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 ) (func $exports/Car#constructor@varargs (param $0 i32) (param $1 i32) (result i32) - (local $2 i32) - (local $3 i32) - (local $4 i32) block $1of1 block $0of1 block $outOfRange @@ -100,67 +232,45 @@ local.set $1 end local.get $0 - i32.eqz - if - global.get $~lib/rt/stub/offset - i32.const 16 - i32.add - local.tee $0 - i32.const 16 - i32.add - local.tee $2 - memory.size - local.tee $4 - i32.const 16 - i32.shl - local.tee $3 - i32.gt_u - if - local.get $4 - local.get $2 - local.get $3 - i32.sub - i32.const 65535 - i32.add - i32.const -65536 - i32.and - i32.const 16 - i32.shr_u - local.tee $3 - local.get $4 - local.get $3 - i32.gt_s - select - memory.grow - i32.const 0 - i32.lt_s - if - local.get $3 - memory.grow - i32.const 0 - i32.lt_s - if - unreachable - end + local.get $1 + call $exports/Car#constructor + ) + (func $exports/subOpt@varargs (param $0 i32) (param $1 i32) (result i32) + block $1of1 + block $0of1 + block $outOfRange + global.get $~argumentsLength + i32.const 1 + i32.sub + br_table $0of1 $1of1 $outOfRange end + unreachable end - local.get $2 - global.set $~lib/rt/stub/offset - local.get $0 - i32.const 16 - i32.sub - local.tee $2 - i32.const 16 - i32.store - local.get $2 - i32.const 1 - i32.store offset=4 - local.get $2 - i32.const 3 - i32.store offset=8 - local.get $2 + i32.const 0 + local.set $1 + end + local.get $0 + local.get $1 + i32.sub + ) + (func $exports/vehicles.Car#constructor@varargs (param $0 i32) (param $1 i32) (result i32) + block $1of1 + block $0of1 + block $outOfRange + global.get $~argumentsLength + br_table $0of1 $1of1 $outOfRange + end + unreachable + end + i32.const 2 + local.set $1 + end + local.get $0 + i32.eqz + if i32.const 4 - i32.store offset=12 + call $~lib/rt/stub/__alloc + local.set $0 end local.get $0 local.get $1 diff --git a/tests/compiler/reexport.untouched.wat b/tests/compiler/reexport.untouched.wat index 106656a22e..a768b73c2a 100644 --- a/tests/compiler/reexport.untouched.wat +++ b/tests/compiler/reexport.untouched.wat @@ -1,11 +1,11 @@ (module (type $i32_i32_=>_i32 (func (param i32 i32) (result i32))) (type $none_=>_none (func)) - (type $i32_=>_none (func (param i32))) (type $i32_=>_i32 (func (param i32) (result i32))) + (type $i32_=>_none (func (param i32))) (type $i32_i32_=>_none (func (param i32 i32))) - (type $i32_i32_i32_i32_=>_none (func (param i32 i32 i32 i32))) (type $none_=>_i32 (func (result i32))) + (type $i32_i32_i32_i32_=>_none (func (param i32 i32 i32 i32))) (import "env" "abort" (func $~lib/builtins/abort (param i32 i32 i32 i32))) (memory $0 1) (data (i32.const 16) "\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\00") @@ -13,14 +13,20 @@ (global $export/a i32 (i32.const 1)) (global $export/b i32 (i32.const 2)) (global $export/c i32 (i32.const 3)) + (global $exports/Animal.CAT i32 (i32.const 0)) + (global $exports/Animal.DOG i32 (i32.const 1)) + (global $exports/animals.Animal.CAT i32 (i32.const 0)) + (global $exports/animals.Animal.DOG i32 (i32.const 1)) (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 $~lib/rt/stub/startOffset (mut i32) (i32.const 0)) (global $~lib/rt/stub/offset (mut i32) (i32.const 0)) + (global $reexport/car (mut i32) (i32.const 0)) (global $~lib/heap/__heap_base i32 (i32.const 56)) - (global $exports/Car i32 (i32.const 3)) (global $~argumentsLength (mut i32) (i32.const 0)) + (global $exports/Car i32 (i32.const 3)) + (global $exports/vehicles.Car i32 (i32.const 4)) (export "memory" (memory $0)) (export "add" (func $export/add)) (export "renamed_sub" (func $export/sub)) @@ -50,6 +56,33 @@ (export "exportstar.renamed_c" (global $export/c)) (export "exportstar.ns.two" (func $export/ns.two)) (export "exportstar.default.two" (func $export/ns.two)) + (export "ExportsNamespace.add" (func $exports/add)) + (export "ExportsNamespace.subOpt" (func $exports/subOpt@varargs)) + (export "ExportsNamespace.math.sub" (func $exports/math.sub)) + (export "ExportsNamespace.Animal.CAT" (global $exports/Animal.CAT)) + (export "ExportsNamespace.Animal.DOG" (global $exports/Animal.DOG)) + (export "ExportsNamespace.animals.Animal.CAT" (global $exports/animals.Animal.CAT)) + (export "ExportsNamespace.animals.Animal.DOG" (global $exports/animals.Animal.DOG)) + (export "ExportsNamespace.Car" (global $exports/Car)) + (export "ExportsNamespace.Car#get:doors" (func $exports/Car#get:doors)) + (export "ExportsNamespace.Car#set:doors" (func $exports/Car#set:doors)) + (export "ExportsNamespace.Car#constructor" (func $exports/Car#constructor@varargs)) + (export "ExportsNamespace.Car#get:numDoors" (func $exports/Car#get:numDoors)) + (export "ExportsNamespace.Car#set:numDoors" (func $exports/Car#set:numDoors)) + (export "ExportsNamespace.Car#openDoors" (func $exports/Car#openDoors)) + (export "ExportsNamespace.Car.TIRES" (global $exports/Car.TIRES)) + (export "ExportsNamespace.Car.getNumTires" (func $exports/Car.getNumTires)) + (export "ExportsNamespace.vehicles.Car" (global $exports/vehicles.Car)) + (export "ExportsNamespace.vehicles.Car#get:doors" (func $exports/vehicles.Car#get:doors)) + (export "ExportsNamespace.vehicles.Car#set:doors" (func $exports/vehicles.Car#set:doors)) + (export "ExportsNamespace.vehicles.Car#constructor" (func $exports/vehicles.Car#constructor@varargs)) + (export "ExportsNamespace.vehicles.Car#get:numDoors" (func $exports/vehicles.Car#get:numDoors)) + (export "ExportsNamespace.vehicles.Car#set:numDoors" (func $exports/vehicles.Car#set:numDoors)) + (export "ExportsNamespace.vehicles.Car#openDoors" (func $exports/vehicles.Car#openDoors)) + (export "ExportsNamespace.vehicles.Car.TIRES" (global $exports/vehicles.Car.TIRES)) + (export "ExportsNamespace.vehicles.Car.getNumTires" (func $exports/vehicles.Car.getNumTires)) + (export "ExportsNamespace.outer.inner.a" (global $exports/outer.inner.a)) + (export "ExportsNamespace.renamed_mul" (func $export/mul)) (export "default" (func $export-default/theDefault)) (export "renamed_default" (func $export-default/theDefault)) (export "__setArgumentsLength" (func $~setArgumentsLength)) @@ -69,52 +102,6 @@ local.get $1 i32.add ) - (func $start:reexport - i32.const 1 - i32.const 2 - call $export/add - i32.const 3 - i32.const 4 - call $export/mul - i32.add - drop - i32.const 2 - i32.const 2 - call $exports/add - i32.const 4 - i32.eq - i32.eqz - if - i32.const 0 - i32.const 32 - i32.const 36 - i32.const 1 - call $~lib/builtins/abort - unreachable - end - i32.const 2 - i32.const 2 - call $export/mul - i32.const 4 - i32.eq - i32.eqz - if - i32.const 0 - i32.const 32 - i32.const 37 - i32.const 1 - call $~lib/builtins/abort - unreachable - end - ) - (func $export/sub (param $0 i32) (param $1 i32) (result i32) - local.get $0 - local.get $1 - i32.sub - ) - (func $exports/Car.getNumTires (result i32) - global.get $exports/Car.TIRES - ) (func $~lib/rt/stub/maybeGrowMemory (param $0 i32) (local $1 i32) (local $2 i32) @@ -245,19 +232,92 @@ i32.store local.get $0 ) - (func $exports/Car#get:doors (param $0 i32) (result i32) + (func $exports/Car#get:numDoors (param $0 i32) (result i32) local.get $0 i32.load ) - (func $exports/Car#set:doors (param $0 i32) (param $1 i32) + (func $start:reexport + i32.const 1 + i32.const 2 + call $export/add + i32.const 3 + i32.const 4 + call $export/mul + i32.add + drop + i32.const 2 + i32.const 2 + call $exports/add + i32.const 4 + i32.eq + i32.eqz + if + i32.const 0 + i32.const 32 + i32.const 37 + i32.const 1 + call $~lib/builtins/abort + unreachable + end + i32.const 2 + i32.const 2 + call $export/mul + i32.const 4 + i32.eq + i32.eqz + if + i32.const 0 + i32.const 32 + i32.const 38 + i32.const 1 + call $~lib/builtins/abort + unreachable + end + global.get $~lib/heap/__heap_base + i32.const 15 + i32.add + i32.const 15 + i32.const -1 + i32.xor + i32.and + global.set $~lib/rt/stub/startOffset + global.get $~lib/rt/stub/startOffset + global.set $~lib/rt/stub/offset + i32.const 0 + i32.const 2 + call $exports/Car#constructor + global.set $reexport/car + global.get $reexport/car + call $exports/Car#get:numDoors + i32.const 2 + i32.eq + i32.eqz + if + i32.const 0 + i32.const 32 + i32.const 40 + i32.const 1 + call $~lib/builtins/abort + unreachable + end + ) + (func $export/sub (param $0 i32) (param $1 i32) (result i32) local.get $0 local.get $1 - i32.store + i32.sub ) - (func $exports/Car#get:numDoors (param $0 i32) (result i32) + (func $exports/Car.getNumTires (result i32) + global.get $exports/Car.TIRES + ) + (func $exports/Car#get:doors (param $0 i32) (result i32) local.get $0 i32.load ) + (func $exports/Car#set:doors (param $0 i32) (param $1 i32) + local.get $0 + local.get $1 + i32.store + ) (func $exports/Car#set:numDoors (param $0 i32) (param $1 i32) local.get $0 local.get $1 @@ -272,21 +332,63 @@ (func $export/ns.two nop ) + (func $exports/subOpt (param $0 i32) (param $1 i32) (result i32) + local.get $0 + local.get $1 + i32.sub + ) + (func $exports/math.sub (param $0 i32) (param $1 i32) (result i32) + local.get $0 + local.get $1 + i32.sub + ) + (func $exports/vehicles.Car.getNumTires (result i32) + global.get $exports/vehicles.Car.TIRES + ) + (func $exports/vehicles.Car#constructor (param $0 i32) (param $1 i32) (result i32) + local.get $0 + i32.eqz + if + i32.const 4 + i32.const 4 + call $~lib/rt/stub/__alloc + call $~lib/rt/stub/__retain + local.set $0 + end + local.get $0 + local.get $1 + i32.store + local.get $0 + local.get $1 + i32.store + local.get $0 + ) + (func $exports/vehicles.Car#get:doors (param $0 i32) (result i32) + local.get $0 + i32.load + ) + (func $exports/vehicles.Car#set:doors (param $0 i32) (param $1 i32) + local.get $0 + local.get $1 + i32.store + ) + (func $exports/vehicles.Car#get:numDoors (param $0 i32) (result i32) + local.get $0 + i32.load + ) + (func $exports/vehicles.Car#set:numDoors (param $0 i32) (param $1 i32) + local.get $0 + local.get $1 + i32.store + ) + (func $exports/vehicles.Car#openDoors (param $0 i32) + nop + ) (func $export-default/theDefault nop ) (func $~start call $start:reexport - global.get $~lib/heap/__heap_base - i32.const 15 - i32.add - i32.const 15 - i32.const -1 - i32.xor - i32.and - global.set $~lib/rt/stub/startOffset - global.get $~lib/rt/stub/startOffset - global.set $~lib/rt/stub/offset ) (func $exports/Car#constructor@varargs (param $0 i32) (param $1 i32) (result i32) block $1of1 @@ -304,6 +406,40 @@ local.get $1 call $exports/Car#constructor ) + (func $exports/subOpt@varargs (param $0 i32) (param $1 i32) (result i32) + block $1of1 + block $0of1 + block $outOfRange + global.get $~argumentsLength + i32.const 1 + i32.sub + br_table $0of1 $1of1 $outOfRange + end + unreachable + end + i32.const 0 + local.set $1 + end + local.get $0 + local.get $1 + call $exports/subOpt + ) + (func $exports/vehicles.Car#constructor@varargs (param $0 i32) (param $1 i32) (result i32) + block $1of1 + block $0of1 + block $outOfRange + global.get $~argumentsLength + br_table $0of1 $1of1 $outOfRange + end + unreachable + end + i32.const 2 + local.set $1 + end + local.get $0 + local.get $1 + call $exports/vehicles.Car#constructor + ) (func $~setArgumentsLength (param $0 i32) local.get $0 global.set $~argumentsLength diff --git a/tests/compiler/rereexport.optimized.wat b/tests/compiler/rereexport.optimized.wat index b66c5fd158..606338749d 100644 --- a/tests/compiler/rereexport.optimized.wat +++ b/tests/compiler/rereexport.optimized.wat @@ -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)) @@ -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 @@ -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 ) @@ -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 + ) ) diff --git a/tests/compiler/rereexport.ts b/tests/compiler/rereexport.ts index b0b1e55837..b8a00c451c 100644 --- a/tests/compiler/rereexport.ts +++ b/tests/compiler/rereexport.ts @@ -17,9 +17,10 @@ assert(ReexportsNamespace.rerenamed_mul(2, 2) == 4); let car: ReexportsNamespace.Car = new ReexportsNamespace.Car(); assert(car.numDoors == 2); -//Test our imported namespace with the exported import * as namespace +// Test our imported namespace with the exported import * as namespace assert(ReexportsNamespace.ExportsNamespace.add(2, 2) == 4); assert(ReexportsNamespace.ExportsNamespace.renamed_mul(2, 2) == 4); let exportsNamespaceCar: ReexportsNamespace.Car = new ReexportsNamespace.ExportsNamespace.Car(); assert(exportsNamespaceCar.numDoors == 2); + diff --git a/tests/compiler/rereexport.untouched.wat b/tests/compiler/rereexport.untouched.wat index e69de29bb2..97762fb368 100644 --- a/tests/compiler/rereexport.untouched.wat +++ b/tests/compiler/rereexport.untouched.wat @@ -0,0 +1,364 @@ +(module + (type $none_=>_none (func)) + (type $i32_i32_=>_i32 (func (param i32 i32) (result i32))) + (type $i32_=>_i32 (func (param i32) (result i32))) + (type $i32_=>_none (func (param i32))) + (type $i32_i32_i32_i32_=>_none (func (param i32 i32 i32 i32))) + (import "env" "abort" (func $~lib/builtins/abort (param i32 i32 i32 i32))) + (memory $0 1) + (data (i32.const 16) "\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\00") + (data (i32.const 64) "\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\00") + (table $0 1 funcref) + (global $export/a i32 (i32.const 1)) + (global $export/b i32 (i32.const 2)) + (global $export/c i32 (i32.const 3)) + (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 $~lib/rt/stub/startOffset (mut i32) (i32.const 0)) + (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)) + (global $~lib/heap/__heap_base i32 (i32.const 108)) + (export "memory" (memory $0)) + (export "a" (global $export/a)) + (export "renamed_a" (global $export/a)) + (export "renamed_b" (global $export/b)) + (export "renamed_renamed_b" (global $export/b)) + (export "default" (func $export-default/theDefault)) + (export "renamed_default" (func $export-default/theDefault)) + (export "exportstar.add" (func $export/add)) + (export "exportstar.sub" (func $export/sub)) + (export "exportstar.renamed_mul" (func $export/mul)) + (export "exportstar.a" (global $export/a)) + (export "exportstar.b" (global $export/b)) + (export "exportstar.renamed_c" (global $export/c)) + (export "exportstar.ns.two" (func $export/ns.two)) + (export "exportstar.default.two" (func $export/ns.two)) + (start $~start) + (func $export/add (param $0 i32) (param $1 i32) (result i32) + local.get $0 + local.get $1 + i32.add + ) + (func $export/mul (param $0 i32) (param $1 i32) (result i32) + local.get $0 + local.get $1 + i32.mul + ) + (func $exports/add (param $0 i32) (param $1 i32) (result i32) + local.get $0 + local.get $1 + i32.add + ) + (func $~lib/rt/stub/maybeGrowMemory (param $0 i32) + (local $1 i32) + (local $2 i32) + (local $3 i32) + (local $4 i32) + (local $5 i32) + memory.size + local.set $1 + local.get $1 + i32.const 16 + i32.shl + local.set $2 + local.get $0 + local.get $2 + i32.gt_u + if + local.get $0 + local.get $2 + i32.sub + i32.const 65535 + i32.add + i32.const 65535 + i32.const -1 + i32.xor + i32.and + i32.const 16 + i32.shr_u + local.set $3 + local.get $1 + local.tee $4 + local.get $3 + local.tee $5 + local.get $4 + local.get $5 + i32.gt_s + select + local.set $4 + local.get $4 + memory.grow + i32.const 0 + i32.lt_s + if + local.get $3 + memory.grow + i32.const 0 + i32.lt_s + if + unreachable + end + end + end + local.get $0 + global.set $~lib/rt/stub/offset + ) + (func $~lib/rt/stub/__alloc (param $0 i32) (param $1 i32) (result i32) + (local $2 i32) + (local $3 i32) + (local $4 i32) + (local $5 i32) + (local $6 i32) + local.get $0 + i32.const 1073741808 + i32.gt_u + if + unreachable + end + global.get $~lib/rt/stub/offset + i32.const 16 + i32.add + local.set $2 + local.get $0 + i32.const 15 + i32.add + i32.const 15 + i32.const -1 + i32.xor + i32.and + local.tee $3 + i32.const 16 + local.tee $4 + local.get $3 + local.get $4 + i32.gt_u + select + local.set $5 + local.get $2 + local.get $5 + i32.add + call $~lib/rt/stub/maybeGrowMemory + local.get $2 + i32.const 16 + i32.sub + local.set $6 + local.get $6 + local.get $5 + i32.store + i32.const 1 + drop + local.get $6 + i32.const 1 + i32.store offset=4 + local.get $6 + local.get $1 + i32.store offset=8 + local.get $6 + local.get $0 + i32.store offset=12 + local.get $2 + ) + (func $~lib/rt/stub/__retain (param $0 i32) (result i32) + local.get $0 + ) + (func $exports/Car#constructor (param $0 i32) (param $1 i32) (result i32) + local.get $0 + i32.eqz + if + i32.const 4 + i32.const 3 + call $~lib/rt/stub/__alloc + call $~lib/rt/stub/__retain + local.set $0 + end + local.get $0 + local.get $1 + i32.store + local.get $0 + local.get $1 + i32.store + local.get $0 + ) + (func $exports/Car#get:numDoors (param $0 i32) (result i32) + local.get $0 + i32.load + ) + (func $start:reexport + i32.const 1 + i32.const 2 + call $export/add + i32.const 3 + i32.const 4 + call $export/mul + i32.add + drop + i32.const 2 + i32.const 2 + call $exports/add + i32.const 4 + i32.eq + i32.eqz + if + i32.const 0 + i32.const 32 + i32.const 37 + i32.const 1 + call $~lib/builtins/abort + unreachable + end + i32.const 2 + i32.const 2 + call $export/mul + i32.const 4 + i32.eq + i32.eqz + if + i32.const 0 + i32.const 32 + i32.const 38 + i32.const 1 + call $~lib/builtins/abort + unreachable + end + global.get $~lib/heap/__heap_base + i32.const 15 + i32.add + i32.const 15 + i32.const -1 + i32.xor + i32.and + global.set $~lib/rt/stub/startOffset + global.get $~lib/rt/stub/startOffset + global.set $~lib/rt/stub/offset + i32.const 0 + i32.const 2 + call $exports/Car#constructor + global.set $reexport/car + global.get $reexport/car + call $exports/Car#get:numDoors + i32.const 2 + i32.eq + i32.eqz + if + i32.const 0 + i32.const 32 + i32.const 40 + i32.const 1 + call $~lib/builtins/abort + unreachable + end + ) + (func $start:rereexport + call $start:reexport + i32.const 2 + i32.const 2 + call $export/add + i32.const 4 + i32.eq + i32.eqz + if + i32.const 0 + i32.const 80 + i32.const 15 + i32.const 1 + call $~lib/builtins/abort + unreachable + end + i32.const 2 + i32.const 2 + call $export/mul + i32.const 4 + i32.eq + i32.eqz + if + i32.const 0 + i32.const 80 + i32.const 16 + i32.const 1 + call $~lib/builtins/abort + unreachable + end + i32.const 0 + i32.const 2 + call $exports/Car#constructor + global.set $rereexport/car + global.get $rereexport/car + call $exports/Car#get:numDoors + i32.const 2 + i32.eq + i32.eqz + if + i32.const 0 + i32.const 80 + i32.const 18 + i32.const 1 + call $~lib/builtins/abort + unreachable + end + i32.const 2 + i32.const 2 + call $exports/add + i32.const 4 + i32.eq + i32.eqz + if + i32.const 0 + i32.const 80 + i32.const 21 + i32.const 1 + call $~lib/builtins/abort + unreachable + end + i32.const 2 + i32.const 2 + call $export/mul + i32.const 4 + i32.eq + i32.eqz + if + i32.const 0 + i32.const 80 + i32.const 22 + i32.const 1 + call $~lib/builtins/abort + unreachable + end + i32.const 0 + i32.const 2 + call $exports/Car#constructor + global.set $rereexport/exportsNamespaceCar + global.get $rereexport/exportsNamespaceCar + call $exports/Car#get:numDoors + i32.const 2 + i32.eq + i32.eqz + if + i32.const 0 + i32.const 80 + i32.const 24 + i32.const 1 + call $~lib/builtins/abort + unreachable + end + ) + (func $export-default/theDefault + nop + ) + (func $export/sub (param $0 i32) (param $1 i32) (result i32) + local.get $0 + local.get $1 + i32.sub + ) + (func $export/ns.one + nop + ) + (func $export/ns.two + nop + ) + (func $~start + call $start:rereexport + ) +) From f198b300c11218044ff30ffd4e6209dd4d2dbe36 Mon Sep 17 00:00:00 2001 From: Aaron Turner Date: Fri, 31 Jul 2020 17:08:54 -0700 Subject: [PATCH 05/10] Fixed the tests of exporting namespaces so they now actually work --- tests/compiler/exports.optimized.wat | 6 + tests/compiler/exports.untouched.wat | 9 + .../exportstar-rereexport.optimized.wat | 130 +++++++- .../exportstar-rereexport.untouched.wat | 301 +++++++++++++++++- 4 files changed, 444 insertions(+), 2 deletions(-) diff --git a/tests/compiler/exports.optimized.wat b/tests/compiler/exports.optimized.wat index 9e2849876f..d935ff06e9 100644 --- a/tests/compiler/exports.optimized.wat +++ b/tests/compiler/exports.optimized.wat @@ -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) @@ -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 diff --git a/tests/compiler/exports.untouched.wat b/tests/compiler/exports.untouched.wat index 43014ed255..642deb6a97 100644 --- a/tests/compiler/exports.untouched.wat +++ b/tests/compiler/exports.untouched.wat @@ -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)) @@ -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) @@ -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 diff --git a/tests/compiler/exportstar-rereexport.optimized.wat b/tests/compiler/exportstar-rereexport.optimized.wat index b66c5fd158..606338749d 100644 --- a/tests/compiler/exportstar-rereexport.optimized.wat +++ b/tests/compiler/exportstar-rereexport.optimized.wat @@ -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)) @@ -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 @@ -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 ) @@ -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 + ) ) diff --git a/tests/compiler/exportstar-rereexport.untouched.wat b/tests/compiler/exportstar-rereexport.untouched.wat index 477f4788a2..57c3341551 100644 --- a/tests/compiler/exportstar-rereexport.untouched.wat +++ b/tests/compiler/exportstar-rereexport.untouched.wat @@ -1,11 +1,26 @@ (module (type $none_=>_none (func)) (type $i32_i32_=>_i32 (func (param i32 i32) (result i32))) - (memory $0 0) + (type $i32_=>_i32 (func (param i32) (result i32))) + (type $i32_=>_none (func (param i32))) + (type $i32_i32_i32_i32_=>_none (func (param i32 i32 i32 i32))) + (import "env" "abort" (func $~lib/builtins/abort (param i32 i32 i32 i32))) + (memory $0 1) + (data (i32.const 16) "\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\00") + (data (i32.const 64) "\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\00") (table $0 1 funcref) (global $export/a i32 (i32.const 1)) (global $export/b i32 (i32.const 2)) (global $export/c i32 (i32.const 3)) + (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 $~lib/rt/stub/startOffset (mut i32) (i32.const 0)) + (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)) + (global $~lib/heap/__heap_base i32 (i32.const 108)) (export "memory" (memory $0)) (export "a" (global $export/a)) (export "renamed_a" (global $export/a)) @@ -32,6 +47,145 @@ local.get $1 i32.mul ) + (func $exports/add (param $0 i32) (param $1 i32) (result i32) + local.get $0 + local.get $1 + i32.add + ) + (func $~lib/rt/stub/maybeGrowMemory (param $0 i32) + (local $1 i32) + (local $2 i32) + (local $3 i32) + (local $4 i32) + (local $5 i32) + memory.size + local.set $1 + local.get $1 + i32.const 16 + i32.shl + local.set $2 + local.get $0 + local.get $2 + i32.gt_u + if + local.get $0 + local.get $2 + i32.sub + i32.const 65535 + i32.add + i32.const 65535 + i32.const -1 + i32.xor + i32.and + i32.const 16 + i32.shr_u + local.set $3 + local.get $1 + local.tee $4 + local.get $3 + local.tee $5 + local.get $4 + local.get $5 + i32.gt_s + select + local.set $4 + local.get $4 + memory.grow + i32.const 0 + i32.lt_s + if + local.get $3 + memory.grow + i32.const 0 + i32.lt_s + if + unreachable + end + end + end + local.get $0 + global.set $~lib/rt/stub/offset + ) + (func $~lib/rt/stub/__alloc (param $0 i32) (param $1 i32) (result i32) + (local $2 i32) + (local $3 i32) + (local $4 i32) + (local $5 i32) + (local $6 i32) + local.get $0 + i32.const 1073741808 + i32.gt_u + if + unreachable + end + global.get $~lib/rt/stub/offset + i32.const 16 + i32.add + local.set $2 + local.get $0 + i32.const 15 + i32.add + i32.const 15 + i32.const -1 + i32.xor + i32.and + local.tee $3 + i32.const 16 + local.tee $4 + local.get $3 + local.get $4 + i32.gt_u + select + local.set $5 + local.get $2 + local.get $5 + i32.add + call $~lib/rt/stub/maybeGrowMemory + local.get $2 + i32.const 16 + i32.sub + local.set $6 + local.get $6 + local.get $5 + i32.store + i32.const 1 + drop + local.get $6 + i32.const 1 + i32.store offset=4 + local.get $6 + local.get $1 + i32.store offset=8 + local.get $6 + local.get $0 + i32.store offset=12 + local.get $2 + ) + (func $~lib/rt/stub/__retain (param $0 i32) (result i32) + local.get $0 + ) + (func $exports/Car#constructor (param $0 i32) (param $1 i32) (result i32) + local.get $0 + i32.eqz + if + i32.const 4 + i32.const 3 + call $~lib/rt/stub/__alloc + call $~lib/rt/stub/__retain + local.set $0 + end + local.get $0 + local.get $1 + i32.store + local.get $0 + local.get $1 + i32.store + local.get $0 + ) + (func $exports/Car#get:numDoors (param $0 i32) (result i32) + local.get $0 + i32.load + ) (func $start:reexport i32.const 1 i32.const 2 @@ -41,9 +195,154 @@ call $export/mul i32.add drop + i32.const 2 + i32.const 2 + call $exports/add + i32.const 4 + i32.eq + i32.eqz + if + i32.const 0 + i32.const 32 + i32.const 37 + i32.const 1 + call $~lib/builtins/abort + unreachable + end + i32.const 2 + i32.const 2 + call $export/mul + i32.const 4 + i32.eq + i32.eqz + if + i32.const 0 + i32.const 32 + i32.const 38 + i32.const 1 + call $~lib/builtins/abort + unreachable + end + global.get $~lib/heap/__heap_base + i32.const 15 + i32.add + i32.const 15 + i32.const -1 + i32.xor + i32.and + global.set $~lib/rt/stub/startOffset + global.get $~lib/rt/stub/startOffset + global.set $~lib/rt/stub/offset + i32.const 0 + i32.const 2 + call $exports/Car#constructor + global.set $reexport/car + global.get $reexport/car + call $exports/Car#get:numDoors + i32.const 2 + i32.eq + i32.eqz + if + i32.const 0 + i32.const 32 + i32.const 40 + i32.const 1 + call $~lib/builtins/abort + unreachable + end ) (func $start:rereexport call $start:reexport + i32.const 2 + i32.const 2 + call $export/add + i32.const 4 + i32.eq + i32.eqz + if + i32.const 0 + i32.const 80 + i32.const 15 + i32.const 1 + call $~lib/builtins/abort + unreachable + end + i32.const 2 + i32.const 2 + call $export/mul + i32.const 4 + i32.eq + i32.eqz + if + i32.const 0 + i32.const 80 + i32.const 16 + i32.const 1 + call $~lib/builtins/abort + unreachable + end + i32.const 0 + i32.const 2 + call $exports/Car#constructor + global.set $rereexport/car + global.get $rereexport/car + call $exports/Car#get:numDoors + i32.const 2 + i32.eq + i32.eqz + if + i32.const 0 + i32.const 80 + i32.const 18 + i32.const 1 + call $~lib/builtins/abort + unreachable + end + i32.const 2 + i32.const 2 + call $exports/add + i32.const 4 + i32.eq + i32.eqz + if + i32.const 0 + i32.const 80 + i32.const 21 + i32.const 1 + call $~lib/builtins/abort + unreachable + end + i32.const 2 + i32.const 2 + call $export/mul + i32.const 4 + i32.eq + i32.eqz + if + i32.const 0 + i32.const 80 + i32.const 22 + i32.const 1 + call $~lib/builtins/abort + unreachable + end + i32.const 0 + i32.const 2 + call $exports/Car#constructor + global.set $rereexport/exportsNamespaceCar + global.get $rereexport/exportsNamespaceCar + call $exports/Car#get:numDoors + i32.const 2 + i32.eq + i32.eqz + if + i32.const 0 + i32.const 80 + i32.const 24 + i32.const 1 + call $~lib/builtins/abort + unreachable + end ) (func $start:exportstar-rereexport call $start:rereexport From 9e11d7f0a1bd9535959007f8815b0fedf8f9a3a1 Mon Sep 17 00:00:00 2001 From: Aaron Turner Date: Fri, 31 Jul 2020 17:17:53 -0700 Subject: [PATCH 06/10] Cleaned up the code a little bit --- src/program.ts | 69 +++++++++++++++++++++++++------------------------- 1 file changed, 35 insertions(+), 34 deletions(-) diff --git a/src/program.ts b/src/program.ts index 132379f1f0..26468aae87 100644 --- a/src/program.ts +++ b/src/program.ts @@ -1125,24 +1125,13 @@ export class Program extends DiagnosticEmitter { // Check if there is a namespace that also needs this if (importNamespacesMap.has(file)) { - - let importNamespaceLocalFileMap = importNamespacesMap.get(file) as Map>; - let importNameSpaceLocalFileKeys = Map_keys(importNamespaceLocalFileMap); - let importNameSpaceLocalFileKeysLength = importNameSpaceLocalFileKeys.length; - for (let ii = 0; ii < importNameSpaceLocalFileKeysLength; ++ii) { - let localFile = importNameSpaceLocalFileKeys[ii]; - - // Get our namespaces - let importNamespacesNames = importNamespaceLocalFileMap.get(localFile) as Array; - for (let jj = 0; jj < importNamespacesNames.length; jj++) { - let namespace = localFile.lookup(importNamespacesNames[jj]); - if (namespace && exportName !== importNamespacesNames[jj]) { - namespace.add(exportName, element); - } - } - } + this.addExportToImportNameSpace( + importNamespacesMap, + file, + exportName, + element + ); } - } else { this.error( DiagnosticCode.Module_0_has_no_exported_member_1, @@ -1157,24 +1146,13 @@ export class Program extends DiagnosticEmitter { // Check if there is a namespace that also needs this if (importNamespacesMap.has(file)) { - - let importNamespaceLocalFileMap = importNamespacesMap.get(file) as Map>; - let importNameSpaceLocalFileKeys = Map_keys(importNamespaceLocalFileMap); - let importNameSpaceLocalFileKeysLength = importNameSpaceLocalFileKeys.length; - for (let ii = 0; ii < importNameSpaceLocalFileKeysLength; ++ii) { - let localFile = importNameSpaceLocalFileKeys[ii]; - - // Get our namespaces - let importNamespacesNames = importNamespaceLocalFileMap.get(localFile) as Array; - for (let jj = 0; jj < importNamespacesNames.length; jj++) { - let namespace = localFile.lookup(importNamespacesNames[jj]); - if (namespace && exportName !== importNamespacesNames[jj]) { - namespace.add(exportName, element); - } - } - } + this.addExportToImportNameSpace( + importNamespacesMap, + file, + exportName, + element + ); } - } else { let globalElement = this.lookupGlobal(localName); if (globalElement !== null && isDeclaredElement(globalElement.kind)) { // export { memory } @@ -1366,6 +1344,29 @@ export class Program extends DiagnosticEmitter { } } + private addExportToImportNameSpace( + importNamespacesMap: Map>>, + file: File, + exportName: string, + element: DeclaredElement + ): void { + let importNamespaceLocalFileMap = importNamespacesMap.get(file) as Map>; + let importNameSpaceLocalFileKeys = Map_keys(importNamespaceLocalFileMap); + let importNameSpaceLocalFileKeysLength = importNameSpaceLocalFileKeys.length; + for (let i = 0; i < importNameSpaceLocalFileKeysLength; ++i) { + let localFile = importNameSpaceLocalFileKeys[i]; + + // Get our namespaces + let importNamespacesNames = importNamespaceLocalFileMap.get(localFile) as Array; + for (let j = 0; j < importNamespacesNames.length; j++) { + let namespace = localFile.lookup(importNamespacesNames[j]); + if (namespace && exportName !== importNamespacesNames[j]) { + namespace.add(exportName, element); + } + } + } + } + /** Marks virtual members in a base class overloaded in this class. */ private markVirtuals(thisPrototype: ClassPrototype, basePrototype: ClassPrototype): void { // TODO: make this work with interfaaces as well From 1822c4e4ea68a68d2048175518f0aff7d8dbf356 Mon Sep 17 00:00:00 2001 From: Aaron Turner Date: Tue, 4 Aug 2020 11:58:01 -0700 Subject: [PATCH 07/10] Made requested changes --- src/compiler.ts | 6 +- src/program.ts | 79 ++++++--------------------- tests/compiler/reexport.optimized.wat | 3 +- tests/compiler/reexport.untouched.wat | 3 +- 4 files changed, 23 insertions(+), 68 deletions(-) diff --git a/src/compiler.ts b/src/compiler.ts index 9d9fbc2518..8bc89edafe 100644 --- a/src/compiler.ts +++ b/src/compiler.ts @@ -367,6 +367,8 @@ export class Compiler extends DiagnosticEmitter { virtualCalls: Set = new Set(); /** Elements currently undergoing compilation. */ pendingElements: Set = new Set(); + /** Classes that are already added as globals */ + globalClasses: Set = new Set(); /** Compiles a {@link Program} to a {@link Module} using the specified options. */ static compile(program: Program): Module { @@ -812,7 +814,7 @@ export class Compiler extends DiagnosticEmitter { case ElementKind.CLASS: { let classInstance = element; // make the class name itself represent its runtime id - if (!classInstance.type.isUnmanaged) { + if (!classInstance.type.isUnmanaged && !this.globalClasses.has(classInstance)) { let module = this.module; let internalName = classInstance.internalName; @@ -821,9 +823,9 @@ export class Compiler extends DiagnosticEmitter { // As we can export a class from a file, and in that sample file, // Export a namspace (import * as), that exports the same class. // And there is no module.hasGlobal(), and this works fine :) - module.removeGlobal(internalName); module.addGlobal(internalName, NativeType.I32, false, module.i32(classInstance.id)); module.addGlobalExport(internalName, prefix + name); + this.globalClasses.add(classInstance); } break; } diff --git a/src/program.ts b/src/program.ts index 26468aae87..b9d38ffb9c 100644 --- a/src/program.ts +++ b/src/program.ts @@ -1022,11 +1022,6 @@ export class Program extends DiagnosticEmitter { // queued imports should be resolvable now through traversing exports and queued exports. // note that imports may depend upon imports, so repeat until there's no more progress. - // - // Also, we are creating a map for imported namespaces to have their exports resolved - // while we are resolving exports - // Our importNameSpaces traverses like foreignFile -> localFile -> namespaceNames - let importNamespacesMap = new Map>>(); do { let i = 0, madeProgress = false; while (i < queuedImports.length) { @@ -1067,19 +1062,6 @@ export class Program extends DiagnosticEmitter { ); queuedImports.splice(i, 1); madeProgress = true; - - // Also, make sure to record the namespace in our importNamespaces for later - if (importNamespacesMap.has(foreignFile)) { - let importNamespaceLocalFileMap = importNamespacesMap.get(foreignFile) as Map>; - let importNamespaceNames = importNamespaceLocalFileMap.get(localFile) as Array; - importNamespaceNames.push(localName); - } else { - let importNamespaceLocalFileMap = new Map>(); - let importNamespaceNames = new Array(); - importNamespaceNames.push(localName); - importNamespaceLocalFileMap.set(localFile, importNamespaceNames); - importNamespacesMap.set(foreignFile, importNamespaceLocalFileMap); - } } else { ++i; assert(false); // already reported by the parser not finding the file @@ -1122,16 +1104,6 @@ export class Program extends DiagnosticEmitter { ); if (element) { file.ensureExport(exportName, element); - - // Check if there is a namespace that also needs this - if (importNamespacesMap.has(file)) { - this.addExportToImportNameSpace( - importNamespacesMap, - file, - exportName, - element - ); - } } else { this.error( DiagnosticCode.Module_0_has_no_exported_member_1, @@ -1143,16 +1115,6 @@ export class Program extends DiagnosticEmitter { let element = file.lookupInSelf(localName); if (element) { file.ensureExport(exportName, element); - - // Check if there is a namespace that also needs this - if (importNamespacesMap.has(file)) { - this.addExportToImportNameSpace( - importNamespacesMap, - file, - exportName, - element - ); - } } else { let globalElement = this.lookupGlobal(localName); if (globalElement !== null && isDeclaredElement(globalElement.kind)) { // export { memory } @@ -1344,29 +1306,6 @@ export class Program extends DiagnosticEmitter { } } - private addExportToImportNameSpace( - importNamespacesMap: Map>>, - file: File, - exportName: string, - element: DeclaredElement - ): void { - let importNamespaceLocalFileMap = importNamespacesMap.get(file) as Map>; - let importNameSpaceLocalFileKeys = Map_keys(importNamespaceLocalFileMap); - let importNameSpaceLocalFileKeysLength = importNameSpaceLocalFileKeys.length; - for (let i = 0; i < importNameSpaceLocalFileKeysLength; ++i) { - let localFile = importNameSpaceLocalFileKeys[i]; - - // Get our namespaces - let importNamespacesNames = importNamespaceLocalFileMap.get(localFile) as Array; - for (let j = 0; j < importNamespacesNames.length; j++) { - let namespace = localFile.lookup(importNamespacesNames[j]); - if (namespace && exportName !== importNamespacesNames[j]) { - namespace.add(exportName, element); - } - } - } - } - /** Marks virtual members in a base class overloaded in this class. */ private markVirtuals(thisPrototype: ClassPrototype, basePrototype: ClassPrototype): void { // TODO: make this work with interfaaces as well @@ -1670,7 +1609,7 @@ export class Program extends DiagnosticEmitter { } /** Tries to locate a foreign element by traversing exports and queued exports. */ - lookupForeign( + private lookupForeign( /** Identifier within the other file. */ foreignName: string, /** Normalized path to the other file. */ @@ -2954,6 +2893,11 @@ export class File extends Element { exportsStar: File[] | null = null; /** Top-level start function of this file. */ startFunction!: Function; + /** + * Array of all the names space made from this file + * E.g import * as MyNamespace from "./this-file" + */ + importedNamespacesFromThisFile: Array = new Array(); /** Constructs a new file. */ constructor( @@ -3023,6 +2967,14 @@ 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 caputure our exports + for(let i = 0; i < this.importedNamespacesFromThisFile.length; i++) { + let ns = this.importedNamespacesFromThisFile[i]; + if (!ns.lookup(name)) { + ns.add(name, element); + } + } } /** Ensures that another file is a re-export of this file. */ @@ -3060,6 +3012,9 @@ export class File extends Element { this.copyExportsToNamespace(ns); // NOTE: Some exports are still queued, and are not added here. // But will be added to the namespace when the exports are being resolved. + // We are adding this namespaces to our this files imported namespaces so + // we can resolve when we add our exports + this.importedNamespacesFromThisFile.push(ns); return ns; } diff --git a/tests/compiler/reexport.optimized.wat b/tests/compiler/reexport.optimized.wat index 16aa1f3533..0a020fc3b2 100644 --- a/tests/compiler/reexport.optimized.wat +++ b/tests/compiler/reexport.optimized.wat @@ -21,8 +21,8 @@ (global $exports/outer.inner.a i32 (i32.const 42)) (global $~lib/rt/stub/offset (mut i32) (i32.const 0)) (global $reexport/car (mut i32) (i32.const 0)) - (global $~argumentsLength (mut i32) (i32.const 0)) (global $exports/Car i32 (i32.const 3)) + (global $~argumentsLength (mut i32) (i32.const 0)) (global $exports/vehicles.Car i32 (i32.const 4)) (export "memory" (memory $0)) (export "add" (func $export/add)) @@ -60,7 +60,6 @@ (export "ExportsNamespace.Animal.DOG" (global $exports/Animal.DOG)) (export "ExportsNamespace.animals.Animal.CAT" (global $exports/animals.Animal.CAT)) (export "ExportsNamespace.animals.Animal.DOG" (global $exports/animals.Animal.DOG)) - (export "ExportsNamespace.Car" (global $exports/Car)) (export "ExportsNamespace.Car#get:doors" (func $exports/Car#get:numDoors)) (export "ExportsNamespace.Car#set:doors" (func $exports/Car#set:doors)) (export "ExportsNamespace.Car#constructor" (func $exports/Car#constructor@varargs)) diff --git a/tests/compiler/reexport.untouched.wat b/tests/compiler/reexport.untouched.wat index a768b73c2a..abfcff3db7 100644 --- a/tests/compiler/reexport.untouched.wat +++ b/tests/compiler/reexport.untouched.wat @@ -24,8 +24,8 @@ (global $~lib/rt/stub/offset (mut i32) (i32.const 0)) (global $reexport/car (mut i32) (i32.const 0)) (global $~lib/heap/__heap_base i32 (i32.const 56)) - (global $~argumentsLength (mut i32) (i32.const 0)) (global $exports/Car i32 (i32.const 3)) + (global $~argumentsLength (mut i32) (i32.const 0)) (global $exports/vehicles.Car i32 (i32.const 4)) (export "memory" (memory $0)) (export "add" (func $export/add)) @@ -63,7 +63,6 @@ (export "ExportsNamespace.Animal.DOG" (global $exports/Animal.DOG)) (export "ExportsNamespace.animals.Animal.CAT" (global $exports/animals.Animal.CAT)) (export "ExportsNamespace.animals.Animal.DOG" (global $exports/animals.Animal.DOG)) - (export "ExportsNamespace.Car" (global $exports/Car)) (export "ExportsNamespace.Car#get:doors" (func $exports/Car#get:doors)) (export "ExportsNamespace.Car#set:doors" (func $exports/Car#set:doors)) (export "ExportsNamespace.Car#constructor" (func $exports/Car#constructor@varargs)) From bb3e0b57e6aa512b645c6410d281c24614abaf74 Mon Sep 17 00:00:00 2001 From: Aaron Turner Date: Tue, 4 Aug 2020 18:28:32 -0700 Subject: [PATCH 08/10] Made requested changes --- src/compiler.ts | 17 +++++++---------- src/program.ts | 24 ++++++++++-------------- tests/compiler/reexport.optimized.wat | 1 + tests/compiler/reexport.untouched.wat | 1 + 4 files changed, 19 insertions(+), 24 deletions(-) diff --git a/src/compiler.ts b/src/compiler.ts index 8bc89edafe..99de950cd2 100644 --- a/src/compiler.ts +++ b/src/compiler.ts @@ -367,8 +367,8 @@ export class Compiler extends DiagnosticEmitter { virtualCalls: Set = new Set(); /** Elements currently undergoing compilation. */ pendingElements: Set = new Set(); - /** Classes that are already added as globals */ - globalClasses: Set = new Set(); + /** Elements, that are module exports, already processed */ + doneModuleExports: Set = new Set(); /** Compiles a {@link Program} to a {@link Module} using the specified options. */ static compile(program: Program): Module { @@ -814,18 +814,15 @@ export class Compiler extends DiagnosticEmitter { case ElementKind.CLASS: { let classInstance = element; // make the class name itself represent its runtime id - if (!classInstance.type.isUnmanaged && !this.globalClasses.has(classInstance)) { + if (!classInstance.type.isUnmanaged) { let module = this.module; let internalName = classInstance.internalName; - // We remove the global before adding it, as this ensures - // we are not adding the same global twice. This is needed, - // As we can export a class from a file, and in that sample file, - // Export a namspace (import * as), that exports the same class. - // And there is no module.hasGlobal(), and this works fine :) - 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); - this.globalClasses.add(classInstance); } break; } diff --git a/src/program.ts b/src/program.ts index b9d38ffb9c..e49771b078 100644 --- a/src/program.ts +++ b/src/program.ts @@ -1053,7 +1053,7 @@ export class Program extends DiagnosticEmitter { let localName = localIdentifier.text; localFile.add( localName, - foreignFile.asImportedNamespace( + foreignFile.asAliasNamespace( localName, localFile, localIdentifier @@ -2893,11 +2893,8 @@ export class File extends Element { exportsStar: File[] | null = null; /** Top-level start function of this file. */ startFunction!: Function; - /** - * Array of all the names space made from this file - * E.g import * as MyNamespace from "./this-file" - */ - importedNamespacesFromThisFile: Array = new Array(); + /** Array of `import * as X` alias namespaces of this file. */ + aliasNamespaces: Array = new Array(); /** Constructs a new file. */ constructor( @@ -2969,8 +2966,8 @@ export class File extends Element { if (this.source.sourceKind == SourceKind.LIBRARY_ENTRY) this.program.ensureGlobal(name, element); // Also, add to the namespaces that caputure our exports - for(let i = 0; i < this.importedNamespacesFromThisFile.length; i++) { - let ns = this.importedNamespacesFromThisFile[i]; + for(let i = 0; i < this.aliasNamespaces.length; i++) { + let ns = this.aliasNamespaces[i]; if (!ns.lookup(name)) { ns.add(name, element); } @@ -3000,7 +2997,7 @@ export class File extends Element { } /** Creates an imported namespace from this file. */ - asImportedNamespace( + asAliasNamespace( name: string, parent: Element, localIdentifier: IdentifierExpression @@ -3010,11 +3007,10 @@ export class File extends Element { var ns = new Namespace(name, parent, declaration); ns.set(CommonFlags.SCOPED); this.copyExportsToNamespace(ns); - // NOTE: Some exports are still queued, and are not added here. - // But will be added to the namespace when the exports are being resolved. - // We are adding this namespaces to our this files imported namespaces so - // we can resolve when we add our exports - this.importedNamespacesFromThisFile.push(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; } diff --git a/tests/compiler/reexport.optimized.wat b/tests/compiler/reexport.optimized.wat index 0a020fc3b2..502c3cd9f8 100644 --- a/tests/compiler/reexport.optimized.wat +++ b/tests/compiler/reexport.optimized.wat @@ -60,6 +60,7 @@ (export "ExportsNamespace.Animal.DOG" (global $exports/Animal.DOG)) (export "ExportsNamespace.animals.Animal.CAT" (global $exports/animals.Animal.CAT)) (export "ExportsNamespace.animals.Animal.DOG" (global $exports/animals.Animal.DOG)) + (export "ExportsNamespace.Car" (global $exports/Car)) (export "ExportsNamespace.Car#get:doors" (func $exports/Car#get:numDoors)) (export "ExportsNamespace.Car#set:doors" (func $exports/Car#set:doors)) (export "ExportsNamespace.Car#constructor" (func $exports/Car#constructor@varargs)) diff --git a/tests/compiler/reexport.untouched.wat b/tests/compiler/reexport.untouched.wat index abfcff3db7..5c277543ef 100644 --- a/tests/compiler/reexport.untouched.wat +++ b/tests/compiler/reexport.untouched.wat @@ -63,6 +63,7 @@ (export "ExportsNamespace.Animal.DOG" (global $exports/Animal.DOG)) (export "ExportsNamespace.animals.Animal.CAT" (global $exports/animals.Animal.CAT)) (export "ExportsNamespace.animals.Animal.DOG" (global $exports/animals.Animal.DOG)) + (export "ExportsNamespace.Car" (global $exports/Car)) (export "ExportsNamespace.Car#get:doors" (func $exports/Car#get:doors)) (export "ExportsNamespace.Car#set:doors" (func $exports/Car#set:doors)) (export "ExportsNamespace.Car#constructor" (func $exports/Car#constructor@varargs)) From 8b8d0a1e038a2000684ac7433b97cd74510ab859 Mon Sep 17 00:00:00 2001 From: Aaron Turner Date: Wed, 5 Aug 2020 09:18:18 -0700 Subject: [PATCH 09/10] Update src/program.ts Co-authored-by: Daniel Wirtz --- src/program.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/program.ts b/src/program.ts index e49771b078..9aa6c1f203 100644 --- a/src/program.ts +++ b/src/program.ts @@ -2965,7 +2965,7 @@ export class File extends Element { exports.set(name, element); if (this.source.sourceKind == SourceKind.LIBRARY_ENTRY) this.program.ensureGlobal(name, element); - // Also, add to the namespaces that caputure our exports + // Also, add to the namespaces that capture our exports for(let i = 0; i < this.aliasNamespaces.length; i++) { let ns = this.aliasNamespaces[i]; if (!ns.lookup(name)) { From b7d020195fe67c0de7784eb47846c2d23fdcee79 Mon Sep 17 00:00:00 2001 From: Aaron Turner Date: Wed, 5 Aug 2020 09:22:21 -0700 Subject: [PATCH 10/10] Made requested changes --- src/program.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/program.ts b/src/program.ts index 9aa6c1f203..63fdf560da 100644 --- a/src/program.ts +++ b/src/program.ts @@ -2968,9 +2968,7 @@ export class File extends Element { // Also, add to the namespaces that capture our exports for(let i = 0; i < this.aliasNamespaces.length; i++) { let ns = this.aliasNamespaces[i]; - if (!ns.lookup(name)) { - ns.add(name, element); - } + ns.add(name, element); } }