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 9, 2023
1 parent de6e170 commit d8fc49b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
10 changes: 5 additions & 5 deletions js/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type Bundle struct {
pwd *url.URL

callableExports map[string]struct{}
modResolution *modulesResolution
moduleResolver *moduleResolver
}

// A BundleInstance is a self-contained instance of a Bundle.
Expand Down Expand Up @@ -85,14 +85,14 @@ func newBundle(
Options: options,
CompatibilityMode: compatMode,
callableExports: make(map[string]struct{}),
modResolution: newModuleResolution(getJSModules()),
moduleResolver: newModuleResolution(getJSModules()),
filesystems: filesystems,
pwd: loader.Dir(src.URL),
logger: piState.Logger,
preInitState: piState,
}
c := bundle.newCompiler(piState.Logger)
if err = bundle.modResolution.setMain(src, c); err != nil {
if err = bundle.moduleResolver.setMain(src, c); err != nil {
return nil, err
}
// Instantiate the bundle into a new VM using a bound init context. This uses a context with a
Expand Down Expand Up @@ -274,7 +274,7 @@ func (b *Bundle) instantiate(vuImpl *moduleVUImpl, vuID uint64, c *compiler.Comp
}

cjsLoad := generateCJSLoad(b, c)
modSys := newModuleSystem(b.modResolution, vuImpl, cjsLoad)
modSys := newModuleSystem(b.moduleResolver, vuImpl, cjsLoad)
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.modResolution.resolve(b.pwd, b.Filename.String(), cjsLoad)
mod, err := b.moduleResolver.resolve(b.pwd, b.Filename.String(), cjsLoad)
if err != nil {
return err // TODO wrap as this should never happen
}
Expand Down
22 changes: 11 additions & 11 deletions js/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,30 +24,30 @@ type moduleCacheElement struct {
err error
}

type modulesResolution struct {
type moduleResolver struct {
cache map[string]moduleCacheElement
goModules map[string]interface{}
}

func newModuleResolution(goModules map[string]interface{}) *modulesResolution {
return &modulesResolution{goModules: goModules, cache: make(map[string]moduleCacheElement)}
func newModuleResolution(goModules map[string]interface{}) *moduleResolver {
return &moduleResolver{goModules: goModules, cache: make(map[string]moduleCacheElement)}
}

func (mr *modulesResolution) setMain(main *loader.SourceData, c *compiler.Compiler) error {
func (mr *moduleResolver) setMain(main *loader.SourceData, c *compiler.Compiler) error {
mod, err := cjsmoduleFromString(main.URL, main.Data, c)
mr.cache[main.URL.String()] = moduleCacheElement{mod: mod, err: err}
return err
}

func (mr *modulesResolution) resolveSpecifier(basePWD *url.URL, arg string) (*url.URL, error) {
func (mr *moduleResolver) resolveSpecifier(basePWD *url.URL, arg string) (*url.URL, error) {
specifier, err := loader.Resolve(basePWD, arg)
if err != nil {
return nil, err
}
return specifier, nil
}

func (mr *modulesResolution) requireModule(name string) (module, error) {
func (mr *moduleResolver) requireModule(name string) (module, error) {
mod, ok := mr.goModules[name]
if !ok {
return nil, fmt.Errorf("unknown module: %s", name)
Expand All @@ -59,7 +59,7 @@ func (mr *modulesResolution) requireModule(name string) (module, error) {
return &baseGoModule{mod: mod}, nil
}

func (mr *modulesResolution) resolve(basePWD *url.URL, arg string, loadCJS cjsModuleLoader) (module, error) {
func (mr *moduleResolver) resolve(basePWD *url.URL, arg string, loadCJS cjsModuleLoader) (module, error) {
if cached, ok := mr.cache[arg]; ok {
return cached.mod, cached.err
}
Expand Down Expand Up @@ -89,13 +89,13 @@ func (mr *modulesResolution) resolve(basePWD *url.URL, arg string, loadCJS cjsMo
type moduleSystem struct {
vu modules.VU
instanceCache map[module]moduleInstance
resolution *modulesResolution
resolver *moduleResolver
cjsLoad cjsModuleLoader
}

func newModuleSystem(resolution *modulesResolution, vu modules.VU, cjsLoad cjsModuleLoader) *moduleSystem {
func newModuleSystem(resolution *moduleResolver, vu modules.VU, cjsLoad cjsModuleLoader) *moduleSystem {
return &moduleSystem{
resolution: resolution,
resolver: resolution,
instanceCache: make(map[module]moduleInstance),
vu: vu,
cjsLoad: cjsLoad,
Expand All @@ -104,7 +104,7 @@ func newModuleSystem(resolution *modulesResolution, vu modules.VU, cjsLoad cjsMo

// 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.resolution.resolve(pwd, arg, ms.cjsLoad)
mod, err := ms.resolver.resolve(pwd, arg, ms.cjsLoad)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit d8fc49b

Please sign in to comment.