Skip to content

Commit

Permalink
fixup! js: refactor how modules are loaded
Browse files Browse the repository at this point in the history
  • Loading branch information
mstoykov committed Feb 28, 2023
1 parent d8fc49b commit f020530
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
12 changes: 6 additions & 6 deletions js/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -110,7 +111,7 @@ func newBundle(
return nil, err
}

return &bundle, nil
return bundle, nil
}

// NewBundleFromArchive creates a new bundle from an lib.Archive.
Expand Down Expand Up @@ -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() {
Expand All @@ -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
}
Expand Down
14 changes: 7 additions & 7 deletions js/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down

0 comments on commit f020530

Please sign in to comment.