proposal-compartments icon indicating copy to clipboard operation
proposal-compartments copied to clipboard

Receive the module instance in importMetaHook?

Open Jack-Works opened this issue 5 years ago • 7 comments

Example:

const x = new Compartment({
    // ...
    importMetaHook(fullSpec, meta, moduleInsance) {
        meta.url = fullSpec
        meta.self = moduleInstance
    }
})
await x.import("/index")
// {url: "/index", self: Module }

Jack-Works avatar May 24 '20 02:05 Jack-Works

See https://github.com/tc39/proposal-realms/issues/244#issuecomment-633180485

The Compartment proposal has an importMetaHook. This hook is expected to stay in the Compartment proposal, as it relates (loosely) to importing.

erights avatar May 24 '20 05:05 erights

I have updated the issue to reflect the latest compartment API.

Jack-Works avatar Jun 09 '22 04:06 Jack-Works

Do you mean ModuleExportsNamespace? What’s a motivating use-case? That is, how would this feature be used?

kriskowal avatar Jun 10 '22 13:06 kriskowal

Example:

const x = new Compartment({
    // ...
    importMetaHook(fullSpec, meta, moduleInsance) {
        meta.url = fullSpec
        meta.self = moduleInstance
    }
})
await x.import("/index")
// {url: "/index", self: Module }

This is my motivating use case although it is not common.

Jack-Works avatar Jun 10 '22 15:06 Jack-Works

Do you mean to build a Map of all the compartment’s instances by their full specifier? Do you need a reified instance or do you need the module exports namespace?

kriskowal avatar Jun 10 '22 15:06 kriskowal

The case seems incomplete, since the object resolved by import is the module exports namespace (so presumably the module in question exports url and self, like export const url = import.meta.url). So, I’ll infer that you do in fact need a reified module instance object. To my knowledge, the only use you’d have for that would be to get the namespace or maybe the environment record.

What would you go on to do with these objects?

kriskowal avatar Jun 10 '22 15:06 kriskowal

const x = new Compartment({
    // ...
    importMetaHook(fullSpec, meta, moduleInsance) {
        meta.url = fullSpec
        meta.self = moduleInstance
    }
})
await x.import("/index")

/index

export const x = 1
import.meta.self.x = 1;
(await import('/index')) === import.meta.self

Yes, I agree this use case does not look so compelling so maybe we can ignore it.

Jack-Works avatar Jun 10 '22 16:06 Jack-Works