diff --git a/js/bundle.go b/js/bundle.go index 9a9f2104099..c14a69dcc03 100644 --- a/js/bundle.go +++ b/js/bundle.go @@ -79,19 +79,20 @@ func newBundle( } // Make a bundle, instantiate it into a throwaway VM to populate caches. - bundle := Bundle{ + bundle := &Bundle{ Filename: src.URL, Source: src.Data, Options: options, CompatibilityMode: compatMode, callableExports: make(map[string]struct{}), - moduleResolver: newModuleResolution(getJSModules()), filesystems: filesystems, pwd: loader.Dir(src.URL), logger: piState.Logger, preInitState: piState, } c := bundle.newCompiler(piState.Logger) + bundle.moduleResolver = newModuleResolution(getJSModules(), generateCJSLoad(bundle, c)) + if err = bundle.moduleResolver.setMain(src, c); err != nil { return nil, err } @@ -110,7 +111,7 @@ func newBundle( return nil, err } - return &bundle, nil + return bundle, nil } // NewBundleFromArchive creates a new bundle from an lib.Archive. @@ -273,8 +274,7 @@ func (b *Bundle) instantiate(vuImpl *moduleVUImpl, vuID uint64, c *compiler.Comp LookupEnv: b.preInitState.LookupEnv, } - cjsLoad := generateCJSLoad(b, c) - modSys := newModuleSystem(b.moduleResolver, vuImpl, cjsLoad) + modSys := newModuleSystem(b.moduleResolver, vuImpl) unbindInit := b.setInitGlobals(rt, modSys) vuImpl.initEnv = initenv defer func() { @@ -298,7 +298,7 @@ func (b *Bundle) instantiate(vuImpl *moduleVUImpl, vuID uint64, c *compiler.Comp err = common.RunWithPanicCatching(b.logger, rt, func() error { return vuImpl.eventLoop.Start(func() error { //nolint:shadow,govet // here we shadow err on purpose - mod, err := b.moduleResolver.resolve(b.pwd, b.Filename.String(), cjsLoad) + mod, err := b.moduleResolver.resolve(b.pwd, b.Filename.String()) if err != nil { return err // TODO wrap as this should never happen } diff --git a/js/modules.go b/js/modules.go index d694125ed39..7a32c3d6dcb 100644 --- a/js/modules.go +++ b/js/modules.go @@ -27,10 +27,11 @@ type moduleCacheElement struct { type moduleResolver struct { cache map[string]moduleCacheElement goModules map[string]interface{} + loadCJS cjsModuleLoader } -func newModuleResolution(goModules map[string]interface{}) *moduleResolver { - return &moduleResolver{goModules: goModules, cache: make(map[string]moduleCacheElement)} +func newModuleResolution(goModules map[string]interface{}, loadCJS cjsModuleLoader) *moduleResolver { + return &moduleResolver{goModules: goModules, cache: make(map[string]moduleCacheElement), loadCJS: loadCJS} } func (mr *moduleResolver) setMain(main *loader.SourceData, c *compiler.Compiler) error { @@ -59,7 +60,7 @@ func (mr *moduleResolver) requireModule(name string) (module, error) { return &baseGoModule{mod: mod}, nil } -func (mr *moduleResolver) resolve(basePWD *url.URL, arg string, loadCJS cjsModuleLoader) (module, error) { +func (mr *moduleResolver) resolve(basePWD *url.URL, arg string) (module, error) { if cached, ok := mr.cache[arg]; ok { return cached.mod, cached.err } @@ -80,7 +81,7 @@ func (mr *moduleResolver) resolve(basePWD *url.URL, arg string, loadCJS cjsModul return cached.mod, cached.err } // Fall back to loading from the filesystem. - mod, err := loadCJS(specifier, arg) + mod, err := mr.loadCJS(specifier, arg) mr.cache[specifier.String()] = moduleCacheElement{mod: mod, err: err} return mod, err } @@ -93,18 +94,17 @@ type moduleSystem struct { cjsLoad cjsModuleLoader } -func newModuleSystem(resolution *moduleResolver, vu modules.VU, cjsLoad cjsModuleLoader) *moduleSystem { +func newModuleSystem(resolution *moduleResolver, vu modules.VU) *moduleSystem { return &moduleSystem{ resolver: resolution, instanceCache: make(map[module]moduleInstance), vu: vu, - cjsLoad: cjsLoad, } } // Require is called when a module/file needs to be loaded by a script func (ms *moduleSystem) Require(pwd *url.URL, arg string) (*goja.Object, error) { - mod, err := ms.resolver.resolve(pwd, arg, ms.cjsLoad) + mod, err := ms.resolver.resolve(pwd, arg) if err != nil { return nil, err }