Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't "unset" open and require after the init context #3645

Merged
merged 1 commit into from Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 2 additions & 8 deletions js/bundle.go
Expand Up @@ -276,10 +276,9 @@ func (b *Bundle) instantiate(vuImpl *moduleVUImpl, vuID uint64) (*goja.Object, e
}

modSys := modules.NewModuleSystem(b.ModuleResolver, vuImpl)
unbindInit := b.setInitGlobals(rt, vuImpl, modSys)
b.setInitGlobals(rt, vuImpl, modSys)
vuImpl.initEnv = initenv
defer func() {
unbindInit()
vuImpl.initEnv = nil
}()

Expand Down Expand Up @@ -377,7 +376,7 @@ func (r *requireImpl) require(specifier string) (*goja.Object, error) {
return r.internal.Require(specifier)
}

func (b *Bundle) setInitGlobals(rt *goja.Runtime, vu *moduleVUImpl, modSys *modules.ModuleSystem) (unset func()) {
func (b *Bundle) setInitGlobals(rt *goja.Runtime, vu *moduleVUImpl, modSys *modules.ModuleSystem) {
mustSet := func(k string, v interface{}) {
if err := rt.Set(k, v); err != nil {
panic(fmt.Errorf("failed to set '%s' global object: %w", k, err))
Expand All @@ -404,11 +403,6 @@ func (b *Bundle) setInitGlobals(rt *goja.Runtime, vu *moduleVUImpl, modSys *modu
pwd := impl.internal.CurrentlyRequiredModule()
return openImpl(rt, b.filesystems["file"], &pwd, filename, args...)
})

return func() {
mustSet("require", goja.Undefined())
mustSet("open", goja.Undefined())
}
}

func generateFileLoad(b *Bundle) modules.FileLoader {
Expand Down
7 changes: 0 additions & 7 deletions js/runner.go
Expand Up @@ -255,13 +255,6 @@ func (r *Runner) newVU(
vu.moduleVUImpl.state = vu.state
_ = vu.Runtime.Set("console", vu.Console)

// This is here mostly so if someone tries they get a nice message
// instead of "Value is not an object: undefined ..."
_ = vu.Runtime.GlobalObject().Set("open",
func() {
common.Throw(vu.Runtime, fmt.Errorf(cantBeUsedOutsideInitContextMsg, "open"))
})

return vu, nil
}

Expand Down
17 changes: 17 additions & 0 deletions js/runner_test.go
Expand Up @@ -1444,6 +1444,23 @@ func TestVUIntegrationTLSConfig(t *testing.T) {
}
}

func TestVUIntegrationRequireFunctionError(t *testing.T) {
t.Parallel()
r, err := getSimpleRunner(t, "/script.js", `
exports.default = function() { require("k6/http") }
`)
require.NoError(t, err)

ctx, cancel := context.WithCancel(context.Background())
defer cancel()
initVU, err := r.NewVU(ctx, 1, 1, make(chan metrics.SampleContainer, 100))
require.NoError(t, err)
vu := initVU.Activate(&lib.VUActivationParams{RunContext: ctx})
err = vu.RunOnce()
require.Error(t, err)
assert.Contains(t, err.Error(), "only available in the init stage")
}

func TestVUIntegrationOpenFunctionError(t *testing.T) {
t.Parallel()
r, err := getSimpleRunner(t, "/script.js", `
Expand Down