Skip to content

Commit

Permalink
Deprecated context-based utils
Browse files Browse the repository at this point in the history
  • Loading branch information
codebien committed Feb 11, 2022
1 parent 77ff732 commit 335d99c
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 1 deletion.
6 changes: 6 additions & 0 deletions api/common/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,16 @@ type ContextKey int

const ctxKeyEngine = ContextKey(1)

// WithEngine sets the k6 running Engine in the under the hood context.
//
// Deprecated: Use directly the Engine as dependency.
func WithEngine(ctx context.Context, engine *core.Engine) context.Context {
return context.WithValue(ctx, ctxKeyEngine, engine)
}

// GetEngine returns the k6 running Engine fetching it from the context.
//
// Deprecated: Use directly the Engine as dependency.
func GetEngine(ctx context.Context) *core.Engine {
return ctx.Value(ctxKeyEngine).(*core.Engine)
}
3 changes: 3 additions & 0 deletions js/common/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ func BindToGlobal(rt *goja.Runtime, data map[string]interface{}) func() {
}

// Bind the provided value v to the provided runtime
//
// Deprecated: JS modules can implement the modules.VU interface for getting the context,
// goja runtime and the VU State, so the goja.Runtime.Set method can be used for data binding.
func Bind(rt *goja.Runtime, v interface{}, ctxPtr *context.Context) map[string]interface{} {
exports := make(map[string]interface{})

Expand Down
11 changes: 11 additions & 0 deletions js/common/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ import (
"github.com/dop251/goja"
)

// TODO: https://github.com/grafana/k6/issues/2385
// Rid all the context-based utils functions

type ctxKey int

const (
Expand All @@ -34,11 +37,15 @@ const (
)

// WithRuntime attaches the given goja runtime to the context.
//
// Deprecated: Implement the modules.VU interface for sharing the Runtime.
func WithRuntime(ctx context.Context, rt *goja.Runtime) context.Context {
return context.WithValue(ctx, ctxKeyRuntime, rt)
}

// GetRuntime retrieves the attached goja runtime from the given context.
//
// Deprecated: Use modules.VU for get the Runtime.
func GetRuntime(ctx context.Context) *goja.Runtime {
v := ctx.Value(ctxKeyRuntime)
if v == nil {
Expand All @@ -48,11 +55,15 @@ func GetRuntime(ctx context.Context) *goja.Runtime {
}

// WithInitEnv attaches the given init environment to the context.
//
// Deprecated: Implement the modules.VU interface for sharing the init environment.
func WithInitEnv(ctx context.Context, initEnv *InitEnvironment) context.Context {
return context.WithValue(ctx, ctxKeyInitEnv, initEnv)
}

// GetInitEnv retrieves the attached init environment struct from the given context.
//
// Deprecated: Use modules.VU for get the init environment.
func GetInitEnv(ctx context.Context) *InitEnvironment {
v := ctx.Value(ctxKeyInitEnv)
if v == nil {
Expand Down
10 changes: 10 additions & 0 deletions js/initcontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"path/filepath"
"runtime"
"strings"
"sync"

"github.com/dop251/goja"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -201,6 +202,9 @@ func toESModuleExports(exp modules.Exports) interface{} {
return result
}

// TODO: https://github.com/grafana/k6/issues/2385
var onceBindWarning sync.Once //nolint: gochecknoglobals

func (i *InitContext) requireModule(name string) (goja.Value, error) {
mod, ok := i.modules[name]
if !ok {
Expand All @@ -214,6 +218,12 @@ func (i *InitContext) requireModule(name string) (goja.Value, error) {
mod = perInstance.NewModuleInstancePerVU()
}

onceBindWarning.Do(func() {
i.logger.Warnf(`Module '%s' is using deprecated APIs that will be removed in k6 v0.38.0,`+
` for more details on how to update it see`+
` https://k6.io/docs/extensions/guides/create-an-extension/#advanced-javascript-extension`, name)
})

return i.moduleVUImpl.runtime.ToValue(common.Bind(i.moduleVUImpl.runtime, mod, i.moduleVUImpl.ctxPtr)), nil
}

Expand Down
11 changes: 10 additions & 1 deletion lib/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@

package lib

import "context"
import (
"context"
)

type ctxKey int

Expand All @@ -30,12 +32,19 @@ const (
ctxKeyScenario
)

// TODO: https://github.com/grafana/k6/issues/2385
// Rid the State's context-based utils functions

// WithState embeds a State in ctx.
//
// Deprecated: Implement the modules.VU interface for sharing the State.
func WithState(ctx context.Context, state *State) context.Context {
return context.WithValue(ctx, ctxKeyState, state)
}

// GetState returns a State from ctx.
//
// Deprecated: Use modules.VU for get the State.
func GetState(ctx context.Context) *State {
v := ctx.Value(ctxKeyState)
if v == nil {
Expand Down

0 comments on commit 335d99c

Please sign in to comment.