Skip to content
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

Keep resolving queued imports while there is progress #1415

Merged
merged 2 commits into from
Jul 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/glue/js/node.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@

declare const global: Record<string,unknown>;
declare function require(name: string): unknown;
declare namespace console {
function log(...args: unknown[]): void;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, thanks for adding this for console log debuggers like me 😂

}
99 changes: 59 additions & 40 deletions src/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1020,50 +1020,69 @@ export class Program extends DiagnosticEmitter {
}
}

// queued imports should be resolvable now through traversing exports and queued exports
for (let i = 0, k = queuedImports.length; i < k; ++i) {
let queuedImport = queuedImports[i];
let localIdentifier = queuedImport.localIdentifier;
let foreignIdentifier = queuedImport.foreignIdentifier;
if (foreignIdentifier) { // i.e. import { foo [as bar] } from "./baz"
let element = this.lookupForeign(
foreignIdentifier.text,
queuedImport.foreignPath,
queuedImport.foreignPathAlt,
queuedExports
);
if (element) {
queuedImport.localFile.add(
localIdentifier.text,
element,
localIdentifier // isImport
);
} else {
// FIXME: file not found is not reported if this happens?
this.error(
DiagnosticCode.Module_0_has_no_exported_member_1,
foreignIdentifier.range, queuedImport.foreignPath, foreignIdentifier.text
// 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.
do {
let i = 0, madeProgress = false;
while (i < queuedImports.length) {
let queuedImport = queuedImports[i];
let localIdentifier = queuedImport.localIdentifier;
let foreignIdentifier = queuedImport.foreignIdentifier;
if (foreignIdentifier) { // i.e. import { foo [as bar] } from "./baz"
let element = this.lookupForeign(
foreignIdentifier.text,
queuedImport.foreignPath,
queuedImport.foreignPathAlt,
queuedExports
);
}
} else { // i.e. import * as bar from "./bar"
let foreignFile = this.lookupForeignFile(queuedImport.foreignPath, queuedImport.foreignPathAlt);
if (foreignFile) {
let localFile = queuedImport.localFile;
let localName = localIdentifier.text;
localFile.add(
localName,
foreignFile.asImportedNamespace(
if (element) {
queuedImport.localFile.add(
localIdentifier.text,
element,
localIdentifier // isImport
);
queuedImports.splice(i, 1);
madeProgress = true;
} else {
++i;
}
} else { // i.e. import * as bar from "./bar"
let foreignFile = this.lookupForeignFile(queuedImport.foreignPath, queuedImport.foreignPathAlt);
if (foreignFile) {
let localFile = queuedImport.localFile;
let localName = localIdentifier.text;
localFile.add(
localName,
localFile,
localIdentifier
),
localIdentifier // isImport
);
} else {
assert(false); // already reported by the parser not finding the file
foreignFile.asImportedNamespace(
localName,
localFile,
localIdentifier
),
localIdentifier // isImport
);
queuedImports.splice(i, 1);
madeProgress = true;
} else {
++i;
assert(false); // already reported by the parser not finding the file
}
}
}
}
if (!madeProgress) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahhhhh! I had a feeling it may have been an ordering issue where one import had to resolve before others 😄 👍

// report queued imports we were unable to resolve
for (let j = 0, l = queuedImports.length; j < l; ++j) {
let queuedImport = queuedImports[j];
let foreignIdentifier = queuedImport.foreignIdentifier;
if (foreignIdentifier) {
this.error(
DiagnosticCode.Module_0_has_no_exported_member_1,
foreignIdentifier.range, queuedImport.foreignPath, foreignIdentifier.text
);
}
}
break;
}
} while (true);

// queued exports should be resolvable now that imports are finalized
// TODO: for (let [file, exports] of queuedExports) {
Expand Down
41 changes: 41 additions & 0 deletions tests/compiler/exportstar-rereexport.optimized.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
(module
(type $i32_i32_=>_i32 (func (param i32 i32) (result i32)))
(type $none_=>_none (func))
(memory $0 0)
(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-default/theDefault))
(export "exportstar.default.two" (func $export-default/theDefault))
(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 $export-default/theDefault
nop
)
(func $export/sub (param $0 i32) (param $1 i32) (result i32)
local.get $0
local.get $1
i32.sub
)
)
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@
(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
Expand All @@ -37,13 +45,24 @@
(func $start:rereexport
call $start:reexport
)
(func $start:wildcard-export
(func $start:exportstar-rereexport
call $start:rereexport
)
(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:wildcard-export
call $start:exportstar-rereexport
)
)
25 changes: 25 additions & 0 deletions tests/compiler/rereexport.optimized.wat
Original file line number Diff line number Diff line change
@@ -1,16 +1,41 @@
(module
(type $i32_i32_=>_i32 (func (param i32 i32) (result i32)))
(type $none_=>_none (func))
(memory $0 0)
(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-default/theDefault))
(export "exportstar.default.two" (func $export-default/theDefault))
(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 $export-default/theDefault
nop
)
(func $export/sub (param $0 i32) (param $1 i32) (result i32)
local.get $0
local.get $1
i32.sub
)
)
3 changes: 3 additions & 0 deletions tests/compiler/rereexport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ export {
default,
default as renamed_default
} from "./reexport";

import { exportstar } from "./reexport";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding a test as well! 👍

export { exportstar };
19 changes: 19 additions & 0 deletions tests/compiler/rereexport.untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@
(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
Expand Down Expand Up @@ -40,6 +48,17 @@
(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
)
Expand Down
16 changes: 0 additions & 16 deletions tests/compiler/wildcard-export.optimized.wat

This file was deleted.