Skip to content

Commit

Permalink
Replace sobek with goja in comments and identifiers
Browse files Browse the repository at this point in the history
Some places weren't applicable as they link to issues or are
acknowledging contributions.
  • Loading branch information
mstoykov committed Jun 26, 2024
1 parent 1655011 commit d3497ed
Show file tree
Hide file tree
Showing 27 changed files with 76 additions and 76 deletions.
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
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) }
6 changes: 3 additions & 3 deletions js/common/interrupt_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (

// 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
46 changes: 23 additions & 23 deletions js/compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 @@ -396,7 +396,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/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
24 changes: 12 additions & 12 deletions js/modules/k6/html/html.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ func (s Selection) emptySelection() Selection {
return s.Eq(s.Size())
}

func (s Selection) buildMatcher(v sobek.Value, gojaFn sobek.Callable) func(int, *goquery.Selection) bool {
func (s Selection) buildMatcher(v sobek.Value, sobekFn sobek.Callable) func(int, *goquery.Selection) bool {
return func(idx int, sel *goquery.Selection) bool {
fnRes, fnErr := gojaFn(v, s.rt.ToValue(idx), s.rt.ToValue(sel))
fnRes, fnErr := sobekFn(v, s.rt.ToValue(idx), s.rt.ToValue(sel))
if fnErr != nil {
common.Throw(s.rt, fnErr)
}
Expand Down Expand Up @@ -181,12 +181,12 @@ func (s Selection) Has(arg interface{}) Selection {
}

func (s Selection) Not(v sobek.Value) Selection {
gojaFn, isFn := sobek.AssertFunction(v)
sobekFn, isFn := sobek.AssertFunction(v)
if !isFn {
return s.varargFnCall(v, s.sel.Not, s.sel.NotSelection, s.sel.NotNodes)
}

return Selection{s.rt, s.sel.NotFunction(s.buildMatcher(v, gojaFn)), s.URL}
return Selection{s.rt, s.sel.NotFunction(s.buildMatcher(v, sobekFn)), s.URL}
}

func (s Selection) Next(def ...string) Selection {
Expand Down Expand Up @@ -357,13 +357,13 @@ func (s Selection) Children(def ...string) Selection {
}

func (s Selection) Each(v sobek.Value) Selection {
gojaFn, isFn := sobek.AssertFunction(v)
sobekFn, isFn := sobek.AssertFunction(v)
if !isFn {
common.Throw(s.rt, errors.New("the argument to each() must be a function"))
}

fn := func(idx int, _ *goquery.Selection) {
if _, err := gojaFn(v, s.rt.ToValue(idx), selToElement(Selection{s.rt, s.sel.Eq(idx), s.URL})); err != nil {
if _, err := sobekFn(v, s.rt.ToValue(idx), selToElement(Selection{s.rt, s.sel.Eq(idx), s.URL})); err != nil {
common.Throw(s.rt, fmt.Errorf("the function passed to each() failed: %w", err))
}
}
Expand All @@ -380,12 +380,12 @@ func (s Selection) Filter(v sobek.Value) Selection {
return Selection{s.rt, s.sel.FilterSelection(val.sel), s.URL}
}

gojaFn, isFn := sobek.AssertFunction(v)
sobekFn, isFn := sobek.AssertFunction(v)
if !isFn {
common.Throw(s.rt, errors.New("the argument to filter() must be a function, a selector or a selection"))
}

return Selection{s.rt, s.sel.FilterFunction(s.buildMatcher(v, gojaFn)), s.URL}
return Selection{s.rt, s.sel.FilterFunction(s.buildMatcher(v, sobekFn)), s.URL}
}

func (s Selection) Is(v sobek.Value) bool {
Expand All @@ -397,18 +397,18 @@ func (s Selection) Is(v sobek.Value) bool {
return s.sel.IsSelection(val.sel)

default:
gojaFn, isFn := sobek.AssertFunction(v)
sobekFn, isFn := sobek.AssertFunction(v)
if !isFn {
common.Throw(s.rt, errors.New("the argument to is() must be a function, a selector or a selection"))
}

return s.sel.IsFunction(s.buildMatcher(v, gojaFn))
return s.sel.IsFunction(s.buildMatcher(v, sobekFn))
}
}

// Map implements ES5 Array.prototype.map
func (s Selection) Map(v sobek.Value) []sobek.Value {
gojaFn, isFn := sobek.AssertFunction(v)
sobekFn, isFn := sobek.AssertFunction(v)
if !isFn {
common.Throw(s.rt, errors.New("the argument to map() must be a function"))
}
Expand All @@ -417,7 +417,7 @@ func (s Selection) Map(v sobek.Value) []sobek.Value {
s.sel.Each(func(idx int, sel *goquery.Selection) {
selection := &Selection{sel: sel, URL: s.URL, rt: s.rt}

if fnRes, fnErr := gojaFn(v, s.rt.ToValue(idx), s.rt.ToValue(selection)); fnErr == nil {
if fnRes, fnErr := sobekFn(v, s.rt.ToValue(idx), s.rt.ToValue(selection)); fnErr == nil {
values = append(values, fnRes)
}
})
Expand Down
2 changes: 1 addition & 1 deletion js/modules/k6/http/cookiejar.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var ErrJarForbiddenInInitContext = common.NewInitContextError("Making cookie jar
// CookieJar is cookiejar.Jar wrapper to be used in js scripts
type CookieJar struct {
moduleInstance *ModuleInstance
// js is to make it not be accessible from inside goja/js, the json is
// js is to make it not be accessible from inside sobek/js, the json is
// for when it is returned from setup().
Jar *cookiejar.Jar `js:"-" json:"-"`
}
Expand Down
4 changes: 2 additions & 2 deletions js/modules/k6/http/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"go.k6.io/k6/lib/netext/httpext"
)

// Response is a representation of an HTTP response to be returned to the goja VM
// Response is a representation of an HTTP response to be returned to the sobek VM
type Response struct {
*httpext.Response `js:"-"`
client *Client
Expand Down Expand Up @@ -61,7 +61,7 @@ func (res *Response) HTML(selector ...string) html.Selection {
return sel
}

// JSON parses the body of a response as JSON and returns it to the goja VM.
// JSON parses the body of a response as JSON and returns it to the sobek VM.
func (res *Response) JSON(selector ...string) sobek.Value {
rt := res.client.moduleInstance.vu.Runtime()

Expand Down
2 changes: 1 addition & 1 deletion js/modules/k6/timers/timers.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (*RootModule) NewModuleInstance(vu modules.VU) modules.Instance {
func (e *Timers) Exports() modules.Exports {
return modules.Exports{
Named: map[string]interface{}{
// TODO the usage of `ToValue` here is so that goja doesn't do it automatically later
// TODO the usage of `ToValue` here is so that sobek doesn't do it automatically later
// which will effectively create new instance each time it is accessed.
"setTimeout": e.vu.Runtime().ToValue(e.setTimeout),
"clearTimeout": e.vu.Runtime().ToValue(e.clearTimeout),
Expand Down
2 changes: 1 addition & 1 deletion js/modules/require_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (r *LegacyRequireImpl) Require(specifier string) (*sobek.Object, error) {
//
// With native ESM this won't even be possible as `require` might not be called - instead an import
// might be used in which case we won't be able to be doing this hack. In that case we either will
// need some goja specific helper or to use stack traces as goja_nodejs does.
// need some sobek specific helper or to use stack traces.
currentPWD := r.currentlyRequiredModule
if specifier != "k6" && !strings.HasPrefix(specifier, "k6/") {
defer func() {
Expand Down
2 changes: 1 addition & 1 deletion js/promises/promises.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

// New can be used to create promises that will be dispatched to k6's event loop.
//
// Calling the function will create a goja promise and return its `resolve` and `reject` callbacks, wrapped
// Calling the function will create a sobek promise and return its `resolve` and `reject` callbacks, wrapped
// in such a way that it will block the k6 JavaScript runtime's event loop from exiting before they are
// called, even if the promise isn't resolved by the time the current script ends executing.
//
Expand Down
Loading

0 comments on commit d3497ed

Please sign in to comment.