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

Move to the forked Sobek code #3792

Merged
merged 2 commits into from
Jun 26, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Dependencies.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Through the rest of the development cycle dependencies shouldn't be updated unle


The last part predominantly goes for `golang.org/x/*` and particularly `golang.org/x/net` which usually have updates through the development of Go itself.
[Goja](https://github.com/dop251/goja) has special considerations as it's heavily used and bug fixes there or new features usually have high impact on k6. Which means that we usually try to update it whenever something new lands there.
[Sobek](https://github.com/grafana/sobek) has special considerations as it's heavily used and bug fixes there or new features usually have high impact on k6. Which means that we usually try to update it whenever something new lands there.

As the stability of any k6 release is pretty essential, this should be done only when adequate testing can be done, and in general, the changelog for each dependency should be consulted on what has changed.

Expand Down Expand Up @@ -55,10 +55,10 @@ You can use the command `modtools check --direct-only` provided you, to update i
Commit dependencies one by one with a message like `Update <dependency> from vX.Y.Z to vX.Y.Z` and a relevant changelog for k6. Sometimes that means "nothing of relevance for k6", sometimes it means a list of bug fixes or new features.

It's preferable to make multiple PRs - in most cases you can split them in three:
- update for goja - which usually needs to happen.
- update for Sobek - which usually needs to happen.
- update for `golang.org/x/*` - also again happen literally every release
- everything else - this in general doesn't include more than 5-6 small updates.

Further splitting is recommended if PRs become too big.

When updating goja it's recommended to run the tc39 tests in `js/tc39`. And if needed, update the breaking ones as explained in an [Introduction to a k6's TC39 testing](./js/tc39/README.md).
When updating Sobek it's recommended to run the tc39 tests in `js/tc39`. And if needed, update the breaking ones as explained in an [Introduction to a k6's TC39 testing](./js/tc39/README.md).
2 changes: 1 addition & 1 deletion cmd/runtime_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func runtimeOptionFlagSet(includeSysEnv bool) *pflag.FlagSet {
flags.Bool("include-system-env-vars", includeSysEnv, "pass the real system environment variables to the runtime")
flags.String("compatibility-mode", "extended",
`JavaScript compiler compatibility mode, "extended" or "base" or "experimental_enhanced"
base: pure goja - Golang JS VM supporting ES5.1+
base: pure Sobek - Golang JS VM supporting ES5.1+
extended: base + Babel with parts of ES2015 preset
slower to compile in case the script uses syntax unsupported by base
experimental_enhanced: esbuild-based transpiling for TypeScript and ES6+ support
Expand Down
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@ require (
github.com/PuerkitoBio/goquery v1.9.1
github.com/Soontao/goHttpDigestClient v0.0.0-20170320082612-6d28bb1415c5
github.com/andybalholm/brotli v1.1.0
github.com/dop251/goja v0.0.0-20240610225006-393f6d42497b
github.com/evanw/esbuild v0.21.2
github.com/fatih/color v1.16.0
github.com/go-sourcemap/sourcemap v2.1.4+incompatible
github.com/golang/protobuf v1.5.4
github.com/gorilla/websocket v1.5.1
github.com/grafana/sobek v0.0.0-20240613124309-cb36746e8fee
github.com/grafana/sobek v0.0.0-20240611084759-1a77bdaf1a4a
github.com/grafana/xk6-browser v1.6.0
github.com/grafana/xk6-dashboard v0.7.4
github.com/grafana/xk6-output-prometheus-remote v0.4.0
Expand Down
7 changes: 3 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/r
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
github.com/dlclark/regexp2 v1.9.0 h1:pTK/l/3qYIKaRXuHnEnIf7Y5NxfRPfpb7dis6/gdlVI=
github.com/dlclark/regexp2 v1.9.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=
github.com/dop251/goja v0.0.0-20240610225006-393f6d42497b h1:fMKDnOAKCGXSZBphY/ilLtu7cmwMnjqE+xJxUkfkpCY=
github.com/dop251/goja v0.0.0-20240610225006-393f6d42497b/go.mod h1:o31y53rb/qiIAONF7w3FHJZRqqP3fzHUr1HqanthByw=
github.com/dop251/goja v0.0.0-20240516125602-ccbae20bcec2 h1:OFTHt+yJDo/uaIKMGjEKzc3DGhrpQZoqvMUIloZv6ZY=
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
Expand Down Expand Up @@ -83,8 +82,8 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/QY=
github.com/gorilla/websocket v1.5.1/go.mod h1:x3kM2JMyaluk02fnUJpQuwD2dCS5NDG2ZHL0uE0tcaY=
github.com/grafana/sobek v0.0.0-20240613124309-cb36746e8fee h1:KjQPRiLoSZMz+oMdEyh8fE/rurMpx3lpRH8PIR66nzo=
github.com/grafana/sobek v0.0.0-20240613124309-cb36746e8fee/go.mod h1:4uA93vCOP7fFfkAEByuRieuU72pAc5DH05b71yCIVsQ=
github.com/grafana/sobek v0.0.0-20240611084759-1a77bdaf1a4a h1:RE9WtCdu6yQ5kfHA5ophYdf2FSchssXt85USln9eks0=
github.com/grafana/sobek v0.0.0-20240611084759-1a77bdaf1a4a/go.mod h1:tUEHKWaMrxFGrMgjeAH85OEceCGQiSl6a/6Wckj/Vf4=
github.com/grafana/xk6-browser v1.6.0 h1:x8ZfBwiUJRRKNEw+Asr5ae9o2gFvYU1Ll/4dDMNIPZ8=
github.com/grafana/xk6-browser v1.6.0/go.mod h1:xLaGGhTMHIRsMvkVWFYh9RPy87kG2n4L4Or6DeI8U+o=
github.com/grafana/xk6-dashboard v0.7.4 h1:0ZRPTAXW+6A3Xqq/a/OaIZhxUt1SOMwUFff0IPwBHrs=
Expand Down
2 changes: 1 addition & 1 deletion js/bundle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ BigInt(1231412444)`,
Expr, Error string
}{
"Array": {`[]`, "json: cannot unmarshal array into Go value of type lib.Options"},
"Function": {`function(){}`, "error parsing script options: json: unsupported type: func(goja.FunctionCall) goja.Value"},
"Function": {`function(){}`, "error parsing script options: json: unsupported type: func(sobek.FunctionCall) sobek.Value"},
}
for name, data := range invalidOptions {
t.Run(name, func(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions js/common/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ func MethodName(_ reflect.Type, m reflect.Method) string {
type FieldNameMapper struct{}

// FieldName is part of the sobek.FieldNameMapper interface
// https://godoc.org/github.com/dop251/goja#FieldNameMapper
// https://godoc.org/github.com/grafana/sobek#FieldNameMapper
func (FieldNameMapper) FieldName(t reflect.Type, f reflect.StructField) string {
return FieldName(t, f)
}

// MethodName is part of the sobek.FieldNameMapper interface
// https://godoc.org/github.com/dop251/goja#FieldNameMapper
// https://godoc.org/github.com/grafana/sobek#FieldNameMapper
func (FieldNameMapper) MethodName(t reflect.Type, m reflect.Method) string { return MethodName(t, m) }
8 changes: 4 additions & 4 deletions js/common/interrupt_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import (
"github.com/grafana/sobek"
)

// UnwrapGojaInterruptedError returns the internal error handled by sobek.
// UnwrapGojaInterruptedError returns the internal error handled by Sobek.
func UnwrapGojaInterruptedError(err error) error {
var gojaErr *sobek.InterruptedError
if errors.As(err, &gojaErr) {
if e, ok := gojaErr.Value().(error); ok {
var sobekErr *sobek.InterruptedError
if errors.As(err, &sobekErr) {
if e, ok := sobekErr.Value().(error); ok {
return e
}
}
Expand Down
4 changes: 2 additions & 2 deletions js/common/randsource.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"github.com/grafana/sobek"
)

// NewRandSource is copied from goja's source code:
// https://github.com/dop251/goja/blob/master/goja/main.go#L44
// NewRandSource is copied from Sobek's source code:
// https://github.com/grafana/sobek/blob/master/sobek/main.go#L44
// The returned RandSource is NOT safe for concurrent use:
// https://golang.org/pkg/math/rand/#NewSource
func NewRandSource() sobek.RandSource {
Expand Down
2 changes: 1 addition & 1 deletion js/common/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func Throw(rt *sobek.Runtime, err error) {
panic(rt.NewGoError(err)) // this catches the stack unlike rt.ToValue
}

// GetReader tries to return an io.Reader value from an exported goja value.
// GetReader tries to return an io.Reader value from an exported Sobek value.
func GetReader(data interface{}) (io.Reader, error) {
switch r := data.(type) {
case string:
Expand Down
48 changes: 24 additions & 24 deletions js/compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import (
"sync"
"time"

"github.com/dop251/goja/parser"
"github.com/go-sourcemap/sourcemap"
"github.com/grafana/sobek"
"github.com/grafana/sobek/parser"
"github.com/sirupsen/logrus"

"go.k6.io/k6/lib"
Expand All @@ -30,26 +30,26 @@ var (
// "presets": []string{"latest"},
"plugins": []interface{}{
// es2015 https://github.com/babel/babel/blob/v6.26.0/packages/babel-preset-es2015/src/index.js
// in goja
// in Sobek
// []interface{}{"transform-es2015-template-literals", map[string]interface{}{"loose": false, "spec": false}},
// "transform-es2015-literals", // in goja
// "transform-es2015-function-name", // in goja
// []interface{}{"transform-es2015-arrow-functions", map[string]interface{}{"spec": false}}, // in goja
// "transform-es2015-block-scoped-functions", // in goja
// []interface{}{"transform-es2015-classes", map[string]interface{}{"loose": false}}, // in goja
// "transform-es2015-object-super", // in goja
// "transform-es2015-shorthand-properties", // in goja
// "transform-es2015-duplicate-keys", // in goja
// []interface{}{"transform-es2015-computed-properties", map[string]interface{}{"loose": false}}, // in goja
// "transform-es2015-for-of", // in goja
// "transform-es2015-sticky-regex", // in goja
// "transform-es2015-unicode-regex", // in goja
// "check-es2015-constants", // in goja
// []interface{}{"transform-es2015-spread", map[string]interface{}{"loose": false}}, // in goja
// "transform-es2015-parameters", // in goja
// []interface{}{"transform-es2015-destructuring", map[string]interface{}{"loose": false}}, // in goja
// "transform-es2015-block-scoping", // in goja
// "transform-es2015-typeof-symbol", // in goja
// "transform-es2015-literals", // in Sobek
// "transform-es2015-function-name", // in Sobek
// []interface{}{"transform-es2015-arrow-functions", map[string]interface{}{"spec": false}}, // in Sobek
// "transform-es2015-block-scoped-functions", // in Sobek
// []interface{}{"transform-es2015-classes", map[string]interface{}{"loose": false}}, // in Sobek
// "transform-es2015-object-super", // in Sobek
// "transform-es2015-shorthand-properties", // in Sobek
// "transform-es2015-duplicate-keys", // in Sobek
// []interface{}{"transform-es2015-computed-properties", map[string]interface{}{"loose": false}}, // in Sobek
// "transform-es2015-for-of", // in Sobek
// "transform-es2015-sticky-regex", // in Sobek
// "transform-es2015-unicode-regex", // in Sobek
// "check-es2015-constants", // in Sobek
// []interface{}{"transform-es2015-spread", map[string]interface{}{"loose": false}}, // in Sobek
// "transform-es2015-parameters", // in Sobek
// []interface{}{"transform-es2015-destructuring", map[string]interface{}{"loose": false}}, // in Sobek
// "transform-es2015-block-scoping", // in Sobek
// "transform-es2015-typeof-symbol", // in Sobek
// all the other module plugins are just dropped
[]interface{}{"transform-es2015-modules-commonjs", map[string]interface{}{"loose": false}},
// "transform-regenerator", // Doesn't really work unless regeneratorRuntime is also added
Expand All @@ -58,7 +58,7 @@ var (
// "transform-exponentiation-operator",

// es2017 https://github.com/babel/babel/blob/v6.26.0/packages/babel-preset-es2017/src/index.js
// "syntax-trailing-function-commas", // in goja
// "syntax-trailing-function-commas", // in Sobek
// "transform-async-to-generator", // Doesn't really work unless regeneratorRuntime is also added
},
"ast": false,
Expand Down Expand Up @@ -172,12 +172,12 @@ type compilationState struct {
}

// Compile the program in the given CompatibilityMode, wrapping it between pre and post code
// TODO isESM will be used once goja support ESM modules natively
// TODO isESM will be used once Sobek support ESM modules natively
func (c *Compiler) Compile(src, filename string, isESM bool) (*sobek.Program, string, error) {
return c.compileImpl(src, filename, !isESM, c.Options.CompatibilityMode, nil)
}

// sourceMapLoader is to be used with goja's WithSourceMapLoader
// sourceMapLoader is to be used with Sobek's WithSourceMapLoader
// it not only gets the file from disk in the simple case, but also returns it if the map was generated from babel
// additioanlly it fixes off by one error in commonjs dependencies due to having to wrap them in a function.
func (c *compilationState) sourceMapLoader(path string) ([]byte, error) {
Expand Down Expand Up @@ -406,7 +406,7 @@ func (b *babel) transformImpl(
return code, nil, nil
}

// this is to make goja try to load a sourcemap.
// this is to make Sobek try to load a sourcemap.
// it is a special url as it should never leak outside of this code
// additionally the alternative support from babel is to embed *the whole* sourcemap at the end
code += "\n//# sourceMappingURL=" + sourceMapURLFromBabel
Expand Down
4 changes: 2 additions & 2 deletions js/compiler/enhanced.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package compiler
import (
"path/filepath"

"github.com/dop251/goja/file"
"github.com/dop251/goja/parser"
"github.com/evanw/esbuild/pkg/api"
"github.com/grafana/sobek/file"
"github.com/grafana/sobek/parser"
)

func esbuildTransform(src, filename string) (code string, srcMap []byte, err error) {
Expand Down
2 changes: 1 addition & 1 deletion js/compiler/enhanced_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"errors"
"testing"

"github.com/dop251/goja/parser"
"github.com/grafana/sobek"
"github.com/grafana/sobek/parser"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.k6.io/k6/lib"
Expand Down
4 changes: 2 additions & 2 deletions js/eventloop/eventloop.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func (e *EventLoop) wakeup() {
// This ensures that the actual work happens asynchronously, while the Promise
// is immediately returned and the main thread resumes execution. It also
// ensures that the Promise resolution happens safely back on the main thread
// once the async work is done, as required by goja and all other JS runtimes.
// once the async work is done, as required by Sobek and all other JS runtimes.
//
// TODO: rename to ReservePendingCallback or something more appropriate?
func (e *EventLoop) RegisterCallback() (enqueueCallback func(func() error)) {
Expand All @@ -132,7 +132,7 @@ func (e *EventLoop) RegisterCallback() (enqueueCallback func(func() error)) {
}

func (e *EventLoop) promiseRejectionTracker(p *sobek.Promise, op sobek.PromiseRejectionOperation) {
// No locking necessary here as the goja runtime will call this synchronously
// No locking necessary here as the Sobek runtime will call this synchronously
// Read Notes on https://tc39.es/ecma262/#sec-host-promise-rejection-tracker
if op == sobek.PromiseRejectionReject {
e.pendingPromiseRejections[p] = struct{}{}
Expand Down
4 changes: 2 additions & 2 deletions js/modules/k6/data/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,11 @@ func (s *sharedArrays) get(rt *sobek.Runtime, name string, call sobek.Callable)
}

func getShareArrayFromCall(rt *sobek.Runtime, call sobek.Callable) sharedArray {
gojaValue, err := call(sobek.Undefined())
sobekValue, err := call(sobek.Undefined())
if err != nil {
common.Throw(rt, err)
}
obj := gojaValue.ToObject(rt)
obj := sobekValue.ToObject(rt)
if obj.ClassName() != "Array" {
common.Throw(rt, errors.New("only arrays can be made into SharedArray")) // TODO better error
}
Expand Down
4 changes: 2 additions & 2 deletions js/modules/k6/execution/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ func (o *tagsDynamicObject) Get(key string) sobek.Value {
}

// Set a property value for the key. It returns true if succeed. String, Boolean
// and Number types are implicitly converted to the goja's relative string
// and Number types are implicitly converted to the Sobek's relative string
// representation. An exception is raised in case a denied type is provided.
func (o *tagsDynamicObject) Set(key string, val sobek.Value) bool {
o.state.Tags.Modify(func(tagsAndMeta *metrics.TagsAndMeta) {
Expand Down Expand Up @@ -410,7 +410,7 @@ func (o *metadataDynamicObject) Get(key string) sobek.Value {
}

// Set a property value for the key. It returns true if successful. String, Boolean
// and Number types are implicitly converted to the goja's relative string
// and Number types are implicitly converted to the Sobek's relative string
// representation. An exception is raised in case a denied type is provided.
func (o *metadataDynamicObject) Set(key string, val sobek.Value) bool {
o.state.Tags.Modify(func(tagsAndMeta *metrics.TagsAndMeta) {
Expand Down
2 changes: 1 addition & 1 deletion js/modules/k6/experimental/streams/goja.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func promiseThen(
return newPromise, nil
}

// isNumber returns true if the given goja value holds a number
// isNumber returns true if the given sobek.Value holds a number
func isNumber(value sobek.Value) bool {
_, isFloat := value.Export().(float64)
_, isInt := value.Export().(int64)
Expand Down
2 changes: 1 addition & 1 deletion js/modules/k6/experimental/streams/tests/checkout.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ git sparse-checkout set resources streams
git fetch origin --depth=1 "${sha}"
git checkout ${sha}

# Apply custom patches needed to run the tests in k6/goja
# Apply custom patches needed to run the tests in k6/Sobek
for patch in ../*.patch
do
git apply "$patch"
Expand Down
2 changes: 1 addition & 1 deletion js/modules/k6/grpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ func (c *Client) Close() error {
return err
}

// MethodInfo holds information on any parsed method descriptors that can be used by the goja VM
// MethodInfo holds information on any parsed method descriptors that can be used by the Sobek VM
type MethodInfo struct {
Package string
Service string
Expand Down
2 changes: 1 addition & 1 deletion js/modules/k6/grpc/listeners.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type eventListener struct {
eventType string

// this return sobek.value *and* error in order to return error on exception instead of panic
// https://pkg.go.dev/github.com/dop251/goja#hdr-Functions
// https://pkg.go.dev/github.com/grafana/sobek#hdr-Functions
list []func(sobek.Value) (sobek.Value, error)
}

Expand Down
2 changes: 1 addition & 1 deletion js/modules/k6/grpc/params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func TestCallParamsTimeOutParse(t *testing.T) {
// newParamsTestRuntime creates a new test runtime
// that could be used to test the params
// it also moves to the VU context and creates the params
// goja value that could be used in the tests
// Sobek value that could be used in the tests
func newParamsTestRuntime(t *testing.T, paramsJSON string) (*modulestest.Runtime, sobek.Value) {
t.Helper()

Expand Down
Loading