Skip to content

Filtered namespace support for static imports&re-rexports#29

Open
caiolima wants to merge 3 commits intotc39:mainfrom
caiolima:filtered-namespace-obj
Open

Filtered namespace support for static imports&re-rexports#29
caiolima wants to merge 3 commits intotc39:mainfrom
caiolima:filtered-namespace-obj

Conversation

@caiolima
Copy link
Copy Markdown
Collaborator

@caiolima caiolima commented Mar 25, 2026

This PR is a joint work from me and @nicolo-ribaudo.

It adds support for syntax and semantics of import {prop1, prop2} as ns from "mod", that will generate a namespace object with only exports selected in the braces. We are calling them filtered namespace, and there's support for static imports import [defer] {} as ns from "mod", and support for re-exports export [defer] {...} as ns from "mod".

For import {prop1} as ns from "mod", ns is a new object declared locally in the module that uses the { … } as ns syntax, rather than being shared across different importers that use the same keys. It avoids a complex cache keyed on the sorted list of names, and it also satisfies some requirements we've got from TG3's feedbacks.

Aliasing (import { foo as bar } as obj) is not supported because the import names do not introduce bindings, so there is no conflict risk.

TODO:

  • Validate names in import {valid_export, non_valid_export} as ns from "mod"
    • Validate names in export defer {valid_export, non_valid_export} as ns from "mod", only if ns is imported
  • Add the dynamic import option for this

@caiolima caiolima force-pushed the filtered-namespace-obj branch from ec444e3 to 06afee9 Compare March 25, 2026 14:19
Comment thread spec.emu Outdated
Comment thread spec.emu Outdated
Comment thread spec.emu Outdated
Comment thread spec.emu Outdated
Comment thread spec.emu Outdated
@nicolo-ribaudo
Copy link
Copy Markdown
Member

I noticed that step 1 of InitializeEnvironment calls ResolveExport on all of its re-exports, thus initializing the export { ... } as ns binding before that it's created.

I'll change the initialization lazyness to only apply to export defer { ... } as ns and not export { ... } as ns.

@nicolo-ribaudo
Copy link
Copy Markdown
Member

Uhmm there is actually a larger problem. When we have a cycle like this:

// entrypoint
export defer { x } as ns from "./dep";
import "./dep";

// dep
import { ns } from "./entrypoint"
export let x = 1;

We first run InitializeEnvironment on dep. As part of its initialization it resolves ns from entrypoint, and at this point it would need to initialize it inside entrypoint. However entrypoint.[[Environment]] does not exist yet, so there is nothing to initialize it into.

@nicolo-ribaudo
Copy link
Copy Markdown
Member

nicolo-ribaudo commented Apr 2, 2026

I pushed a commit that solves the first problem but not the second. Fixed

@nicolo-ribaudo nicolo-ribaudo force-pushed the filtered-namespace-obj branch 2 times, most recently from fb10845 to e57c0a5 Compare April 2, 2026 16:11
@caiolima
Copy link
Copy Markdown
Collaborator Author

caiolima commented Apr 9, 2026

Those last 2 changes LGTM.

@caiolima
Copy link
Copy Markdown
Collaborator Author

caiolima commented Apr 9, 2026

Regarding this TODO: Figure out where to call [EnsureResolvableBindings](https://github.com/tc39/proposal-deferred-reexports/pull/29#sec-ensureresolvablebindings). Not here, as this is meant to be free from side effects..
What if we place the Resolve biding on the InitializeEnvironment of the module that it is importing optional bindings? We could do it while iterating on module.[[ImportEntries]]. There we can get all [[OptionalIndirectExportEntries]] that are being imported and apply EnsureResolvableBindings.

@nicolo-ribaudo nicolo-ribaudo force-pushed the filtered-namespace-obj branch from 7088549 to 21f7599 Compare April 30, 2026 11:48
These changes adds support for syntax and semantics of
`import {prop1, prop2} as ns from "mod"`, that will generate a namespace
object with only exports selected in the braces. We are calling
them filtered namespace, and there's support for static imports
`import [defer] {} as ns from "mod"`, and support for re-exports
`export [defer] {...} as ns from "mod"`.
---------

Co-authored-by: Caio Lima <caiolima@igalia.com>
@nicolo-ribaudo nicolo-ribaudo force-pushed the filtered-namespace-obj branch 2 times, most recently from 605a641 to 6c0dd87 Compare April 30, 2026 11:57
@nicolo-ribaudo nicolo-ribaudo force-pushed the filtered-namespace-obj branch from 6c0dd87 to 7b1e863 Compare April 30, 2026 13:22
@nicolo-ribaudo
Copy link
Copy Markdown
Member

@caiolima Fix validation of names of deferred filtered re-exports integrates this PR with #34

@caiolima
Copy link
Copy Markdown
Collaborator Author

caiolima commented May 4, 2026

@caiolima Fix validation of names of deferred filtered re-exports integrates this PR with #34

Thanks for updating it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants