Skip to content

Commit

Permalink
Merge pull request #6 from szkiba/feature/addons
Browse files Browse the repository at this point in the history
Feature/addons
  • Loading branch information
szkiba authored Jun 12, 2023
2 parents a5fdaa8 + fbefe24 commit 3da3e20
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 90 deletions.
33 changes: 0 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -375,8 +375,6 @@ func init() {

The first parameter of the `Default` function is optionally a [context.Context](https://pkg.go.dev/context). This can be used to perform context aware operations and to access various context variables.

#### k6 variables

The usual k6 variables (eg `__VU`, `__ENV`, `__ITER`) and the variables of the `k6/execution` module can be accessed using the `Value` function of the context parameter.

```go
Expand All @@ -400,37 +398,6 @@ func Default(ctx context.Context) {
}
```

#### JavaScript interop

JavaScript variables and functions can be accessed using the `Value` function of the context parameter. In addition, using the `JS` string variable/constant, a JavaScript code fragment can be specified, which is evaluated before the go code is executed. In this way, practically the entire k6 JavaScript API and external modules are made available to the golang script. This feature is useful until all necessary k6 features are available in xk6-g0.


```go
package main

import (
"context"

"github.com/sirupsen/logrus"
)

func Default(ctx context.Context) {
add := ctx.Value("add").(func(...any) any)

logrus.Info(add(2, 3))
logrus.Info(ctx.Value("welcome"))
}

const JS = `
global.add = function (a, b) {
return a + b
}
global.welcome = 'Hello, World!'
`
```

## Download

You can download pre-built k6 binaries from [Releases](https://github.com/szkiba/xk6-g0/releases/) page. Check [Packages](https://github.com/szkiba/xk6-g0/pkgs/container/xk6-g0) page for pre-built k6 Docker images.
Expand Down
23 changes: 11 additions & 12 deletions g0/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,23 @@ import (
)

func registerBuiltins() {
registerExports(stdlib.Exports)
registerExports(logrus.Exports)
registerExports(resty.Exports)
registerExports(testify.Exports)
registerExports(goquery.Exports)
registerExports(gjson.Exports)
registerExports(jsonpath.Exports)
registerExports(jsonschema.Exports)
registerExports(gofakeit.Exports)
RegisterExports(stdlib.Exports)
RegisterExports(logrus.Exports)
RegisterExports(resty.Exports)
RegisterExports(testify.Exports)
RegisterExports(goquery.Exports)
RegisterExports(gjson.Exports)
RegisterExports(jsonpath.Exports)
RegisterExports(jsonschema.Exports)
RegisterExports(gofakeit.Exports)
}

func registerExtension() {
modules.Register("k6/x/g0", New())
}

func Bootstrap(addons ...ExportsFunc) {
registerBuiltins()
registerExports(addons...)
func Bootstrap() {
redirectStdin()
registerBuiltins()
registerExtension()
}
21 changes: 1 addition & 20 deletions g0/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func redirectStdin() {

os.Stdin = reader

_, err = writer.Write(getJs())
_, err = writer.Write([]byte(jsScript))
if err != nil {
writer.Close()

Expand All @@ -73,25 +73,6 @@ func redirectStdin() {
}
}

func getJs() []byte {
mod := new(RootModule).NewModuleInstance(nil).(*Module) //nolint:forcetypeassert

if val, err := mod.yaegi.Eval("JS"); err == nil {
return concat([]byte(jsScript), []byte(val.String()))
}

return []byte(jsScript)
}

func concat(a, b []byte) []byte {
tmp := make([]byte, len(a)+len(b))

copy(tmp, a)
copy(tmp[len(a):], b)

return tmp
}

const jsScript = `//js
export * from 'k6/x/g0'
export { default } from 'k6/x/g0'
Expand Down
2 changes: 1 addition & 1 deletion g0/exports.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

type ExportsFunc func(modules.VU) interp.Exports

func registerExports(fn ...ExportsFunc) {
func RegisterExports(fn ...ExportsFunc) {
registry.register(fn...)
}

Expand Down
24 changes: 0 additions & 24 deletions scripts/js/script.go

This file was deleted.

0 comments on commit 3da3e20

Please sign in to comment.