Skip to content

Commit

Permalink
Refactor parts of js module
Browse files Browse the repository at this point in the history
  • Loading branch information
mstoykov committed Nov 16, 2022
1 parent e7a70ae commit 5fa71b7
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 122 deletions.
8 changes: 8 additions & 0 deletions js/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ type BundleInstance struct {
pgm programWithSource
}

func (bi *BundleInstance) getCallableExport(name string) goja.Callable {
return bi.exports[name]
}

func (bi *BundleInstance) getExported(name string) goja.Value {
return bi.pgm.exports.Get(name)
}

// NewBundle creates a new bundle from a source file and a filesystem.
func NewBundle(
piState *lib.TestPreInitState, src *loader.SourceData, filesystems map[string]afero.Fs,
Expand Down
16 changes: 8 additions & 8 deletions js/bundle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ func TestNewBundleFromArchive(t *testing.T) {
require.Equal(t, lib.Options{VUs: null.IntFrom(12345)}, b.Options)
bi, err := b.Instantiate(logger, 0)
require.NoError(t, err)
val, err := bi.exports[consts.DefaultFn](goja.Undefined())
val, err := bi.getCallableExport(consts.DefaultFn)(goja.Undefined())
require.NoError(t, err)
require.Equal(t, "hi!", val.Export())
}
Expand Down Expand Up @@ -571,7 +571,7 @@ func TestNewBundleFromArchive(t *testing.T) {
require.NoError(t, err)
bi, err := b.Instantiate(logger, 0)
require.NoError(t, err)
val, err := bi.exports[consts.DefaultFn](goja.Undefined())
val, err := bi.getCallableExport(consts.DefaultFn)(goja.Undefined())
require.NoError(t, err)
require.Equal(t, int64(999), val.Export())
})
Expand Down Expand Up @@ -715,7 +715,7 @@ func TestOpen(t *testing.T) {
t.Run(source, func(t *testing.T) {
bi, err := b.Instantiate(logger, 0)
require.NoError(t, err)
v, err := bi.exports[consts.DefaultFn](goja.Undefined())
v, err := bi.getCallableExport(consts.DefaultFn)(goja.Undefined())
require.NoError(t, err)
require.Equal(t, "hi", v.Export())
})
Expand Down Expand Up @@ -751,7 +751,7 @@ func TestBundleInstantiate(t *testing.T) {

bi, err := b.Instantiate(logger, 0)
require.NoError(t, err)
v, err := bi.exports[consts.DefaultFn](goja.Undefined())
v, err := bi.getCallableExport(consts.DefaultFn)(goja.Undefined())
require.NoError(t, err)
require.Equal(t, true, v.Export())
})
Expand All @@ -772,7 +772,7 @@ func TestBundleInstantiate(t *testing.T) {
bi, err := b.Instantiate(logger, 0)
require.NoError(t, err)
// Ensure `options` properties are correctly marshalled
jsOptions := bi.pgm.exports.Get("options").ToObject(bi.Runtime)
jsOptions := bi.getExported("options").ToObject(bi.Runtime)
vus := jsOptions.Get("vus").Export()
require.Equal(t, int64(5), vus)
tdt := jsOptions.Get("teardownTimeout").Export()
Expand All @@ -783,7 +783,7 @@ func TestBundleInstantiate(t *testing.T) {
b.Options.VUs = null.IntFrom(10)
bi2, err := b.Instantiate(logger, 0)
require.NoError(t, err)
jsOptions = bi2.pgm.exports.Get("options").ToObject(bi2.Runtime)
jsOptions = bi2.getExported("options").ToObject(bi2.Runtime)
vus = jsOptions.Get("vus").Export()
require.Equal(t, int64(10), vus)
b.Options.VUs = optOrig
Expand Down Expand Up @@ -819,7 +819,7 @@ func TestBundleEnv(t *testing.T) {

bi, err := b.Instantiate(logger, 0)
require.NoError(t, err)
_, err = bi.exports[consts.DefaultFn](goja.Undefined())
_, err = bi.getCallableExport(consts.DefaultFn)(goja.Undefined())
require.NoError(t, err)
})
}
Expand Down Expand Up @@ -857,7 +857,7 @@ func TestBundleNotSharable(t *testing.T) {
require.NoError(t, err)
for j := 0; j < iters; j++ {
bi.Runtime.Set("__ITER", j)
_, err := bi.exports[consts.DefaultFn](goja.Undefined())
_, err := bi.getCallableExport(consts.DefaultFn)(goja.Undefined())
require.NoError(t, err)
}
}
Expand Down
94 changes: 0 additions & 94 deletions js/initcontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,10 @@ import (

"go.k6.io/k6/js/common"
"go.k6.io/k6/js/compiler"
"go.k6.io/k6/js/eventloop"
"go.k6.io/k6/js/modules"
"go.k6.io/k6/js/modules/k6"
"go.k6.io/k6/js/modules/k6/crypto"
"go.k6.io/k6/js/modules/k6/crypto/x509"
"go.k6.io/k6/js/modules/k6/data"
"go.k6.io/k6/js/modules/k6/encoding"
"go.k6.io/k6/js/modules/k6/execution"
"go.k6.io/k6/js/modules/k6/grpc"
"go.k6.io/k6/js/modules/k6/html"
"go.k6.io/k6/js/modules/k6/http"
"go.k6.io/k6/js/modules/k6/metrics"
"go.k6.io/k6/js/modules/k6/ws"
"go.k6.io/k6/lib"
"go.k6.io/k6/lib/fsext"
"go.k6.io/k6/loader"

"github.com/grafana/xk6-redis/redis"
"github.com/grafana/xk6-timers/timers"
expws "github.com/grafana/xk6-websockets/websockets"
)

type programWithSource struct {
Expand Down Expand Up @@ -136,53 +120,6 @@ func (i *InitContext) Require(arg string) goja.Value {
}
}

type moduleVUImpl struct {
ctx context.Context
initEnv *common.InitEnvironment
state *lib.State
runtime *goja.Runtime
eventLoop *eventloop.EventLoop
}

func (m *moduleVUImpl) Context() context.Context {
return m.ctx
}

func (m *moduleVUImpl) InitEnv() *common.InitEnvironment {
return m.initEnv
}

func (m *moduleVUImpl) State() *lib.State {
return m.state
}

func (m *moduleVUImpl) Runtime() *goja.Runtime {
return m.runtime
}

func (m *moduleVUImpl) RegisterCallback() func(func() error) {
return m.eventLoop.RegisterCallback()
}

/* This is here to illustrate how to use RegisterCallback to get a promise to work with the event loop
// TODO move this to a common function or remove before merging
// MakeHandledPromise will create and promise and return it's resolve, reject methods as well wrapped in such a way that
// it will block the eventloop from exiting before they are called even if the promise isn't resolved by the time the
// current script ends executing
func (m *moduleVUImpl) MakeHandledPromise() (*goja.Promise, func(interface{}), func(interface{})) {
callback := m.eventLoop.registerCallback()
p, resolve, reject := m.runtime.NewPromise()
return p, func(i interface{}) {
// more stuff
callback(func() { resolve(i) })
}, func(i interface{}) {
// more stuff
callback(func() { reject(i) })
}
}
*/

func toESModuleExports(exp modules.Exports) interface{} {
if exp.Named == nil {
return exp.Default
Expand Down Expand Up @@ -351,34 +288,3 @@ func (i *InitContext) allowOnlyOpenedFiles() {

alreadyOpenedFS.AllowOnlyCached()
}

func getInternalJSModules() map[string]interface{} {
return map[string]interface{}{
"k6": k6.New(),
"k6/crypto": crypto.New(),
"k6/crypto/x509": x509.New(),
"k6/data": data.New(),
"k6/encoding": encoding.New(),
"k6/execution": execution.New(),
"k6/experimental/redis": redis.New(),
"k6/experimental/websockets": &expws.RootModule{},
"k6/experimental/timers": timers.New(),
"k6/net/grpc": grpc.New(),
"k6/html": html.New(),
"k6/http": http.New(),
"k6/metrics": metrics.New(),
"k6/ws": ws.New(),
}
}

func getJSModules() map[string]interface{} {
result := getInternalJSModules()
external := modules.GetJSModules()

// external is always prefixed with `k6/x`
for k, v := range external {
result[k] = v
}

return result
}
20 changes: 10 additions & 10 deletions js/initcontext_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ func TestInitContextRequire(t *testing.T) {

bi, err := b.Instantiate(logger, 0)
require.NoError(t, err)
_, err = bi.exports[consts.DefaultFn](goja.Undefined())
_, err = bi.getCallableExport(consts.DefaultFn)(goja.Undefined())
assert.NoError(t, err)
})
})
Expand Down Expand Up @@ -262,7 +262,7 @@ func TestInitContextOpen(t *testing.T) {
t.Parallel()
bi, err := createAndReadFile(t, tc.file, tc.content, tc.length, "")
require.NoError(t, err)
assert.Equal(t, string(tc.content), bi.pgm.exports.Get("data").Export())
assert.Equal(t, string(tc.content), bi.getExported("data").Export())
})
}

Expand All @@ -271,7 +271,7 @@ func TestInitContextOpen(t *testing.T) {
bi, err := createAndReadFile(t, "/path/to/file.bin", []byte("hi!\x0f\xff\x01"), 6, "b")
require.NoError(t, err)
buf := bi.Runtime.NewArrayBuffer([]byte{104, 105, 33, 15, 255, 1})
assert.Equal(t, buf, bi.pgm.exports.Get("data").Export())
assert.Equal(t, buf, bi.getExported("data").Export())
})

testdata := map[string]string{
Expand Down Expand Up @@ -388,7 +388,7 @@ func TestRequestWithBinaryFile(t *testing.T) {
defer cancel()
bi.moduleVUImpl.ctx = ctx

v, err := bi.exports[consts.DefaultFn](goja.Undefined())
v, err := bi.getCallableExport(consts.DefaultFn)(goja.Undefined())
require.NoError(t, err)
require.NotNil(t, v)
assert.Equal(t, true, v.Export())
Expand Down Expand Up @@ -535,7 +535,7 @@ func TestRequestWithMultipleBinaryFiles(t *testing.T) {
defer cancel()
bi.moduleVUImpl.ctx = ctx

v, err := bi.exports[consts.DefaultFn](goja.Undefined())
v, err := bi.getCallableExport(consts.DefaultFn)(goja.Undefined())
require.NoError(t, err)
require.NotNil(t, v)
assert.Equal(t, true, v.Export())
Expand All @@ -552,7 +552,7 @@ func TestInitContextVU(t *testing.T) {
require.NoError(t, err)
bi, err := b.Instantiate(testutils.NewLogger(t), 5)
require.NoError(t, err)
v, err := bi.exports[consts.DefaultFn](goja.Undefined())
v, err := bi.getCallableExport(consts.DefaultFn)(goja.Undefined())
require.NoError(t, err)
assert.Equal(t, int64(5), v.Export())
}
Expand Down Expand Up @@ -585,7 +585,7 @@ export default function(){

bi, err := b.Instantiate(logger, 0)
require.NoError(t, err)
_, err = bi.exports[consts.DefaultFn](goja.Undefined())
_, err = bi.getCallableExport(consts.DefaultFn)(goja.Undefined())
require.Error(t, err)
exception := new(goja.Exception)
require.ErrorAs(t, err, &exception)
Expand Down Expand Up @@ -616,7 +616,7 @@ export default function () {

bi, err := b.Instantiate(logger, 0)
require.NoError(t, err)
_, err = bi.exports[consts.DefaultFn](goja.Undefined())
_, err = bi.getCallableExport(consts.DefaultFn)(goja.Undefined())
require.Error(t, err)
exception := new(goja.Exception)
require.ErrorAs(t, err, &exception)
Expand Down Expand Up @@ -648,7 +648,7 @@ export default function () {

bi, err := b.Instantiate(logger, 0)
require.NoError(t, err)
_, err = bi.exports[consts.DefaultFn](goja.Undefined())
_, err = bi.getCallableExport(consts.DefaultFn)(goja.Undefined())
require.Error(t, err)
exception := new(goja.Exception)
require.ErrorAs(t, err, &exception)
Expand Down Expand Up @@ -679,7 +679,7 @@ export default function () {

bi, err := b.Instantiate(logger, 0)
require.NoError(t, err)
_, err = bi.exports[consts.DefaultFn](goja.Undefined())
_, err = bi.getCallableExport(consts.DefaultFn)(goja.Undefined())
require.Error(t, err)
exception := new(goja.Exception)
require.ErrorAs(t, err, &exception)
Expand Down
51 changes: 51 additions & 0 deletions js/jsmodules.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package js

import (
"go.k6.io/k6/js/modules"
"go.k6.io/k6/js/modules/k6"
"go.k6.io/k6/js/modules/k6/crypto"
"go.k6.io/k6/js/modules/k6/crypto/x509"
"go.k6.io/k6/js/modules/k6/data"
"go.k6.io/k6/js/modules/k6/encoding"
"go.k6.io/k6/js/modules/k6/execution"
"go.k6.io/k6/js/modules/k6/grpc"
"go.k6.io/k6/js/modules/k6/html"
"go.k6.io/k6/js/modules/k6/http"
"go.k6.io/k6/js/modules/k6/metrics"
"go.k6.io/k6/js/modules/k6/ws"

"github.com/grafana/xk6-redis/redis"
"github.com/grafana/xk6-timers/timers"
expws "github.com/grafana/xk6-websockets/websockets"
)

func getInternalJSModules() map[string]interface{} {
return map[string]interface{}{
"k6": k6.New(),
"k6/crypto": crypto.New(),
"k6/crypto/x509": x509.New(),
"k6/data": data.New(),
"k6/encoding": encoding.New(),
"k6/execution": execution.New(),
"k6/experimental/redis": redis.New(),
"k6/experimental/websockets": &expws.RootModule{},
"k6/experimental/timers": timers.New(),
"k6/net/grpc": grpc.New(),
"k6/html": html.New(),
"k6/http": http.New(),
"k6/metrics": metrics.New(),
"k6/ws": ws.New(),
}
}

func getJSModules() map[string]interface{} {
result := getInternalJSModules()
external := modules.GetJSModules()

// external is always prefixed with `k6/x`
for k, v := range external {
result[k] = v
}

return result
}
8 changes: 3 additions & 5 deletions js/modules/k6/marshalling_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,7 @@ func TestSetupDataMarshalling(t *testing.T) {
defer cancel()
require.NoError(t, runner.Setup(ctx, samples))
initVU, err := runner.NewVU(1, 1, samples)
if assert.NoError(t, err) {
vu := initVU.Activate(&lib.VUActivationParams{RunContext: ctx})
err := vu.RunOnce()
assert.NoError(t, err)
}
require.NoError(t, err)
vu := initVU.Activate(&lib.VUActivationParams{RunContext: ctx})
assert.NoError(t, vu.RunOnce())
}
38 changes: 38 additions & 0 deletions js/modules_vu.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package js

import (
"context"

"github.com/dop251/goja"
"go.k6.io/k6/js/common"
"go.k6.io/k6/js/eventloop"
"go.k6.io/k6/lib"
)

type moduleVUImpl struct {
ctx context.Context
initEnv *common.InitEnvironment
state *lib.State
runtime *goja.Runtime
eventLoop *eventloop.EventLoop
}

func (m *moduleVUImpl) Context() context.Context {
return m.ctx
}

func (m *moduleVUImpl) InitEnv() *common.InitEnvironment {
return m.initEnv
}

func (m *moduleVUImpl) State() *lib.State {
return m.state
}

func (m *moduleVUImpl) Runtime() *goja.Runtime {
return m.runtime
}

func (m *moduleVUImpl) RegisterCallback() func(func() error) {
return m.eventLoop.RegisterCallback()
}
Loading

0 comments on commit 5fa71b7

Please sign in to comment.