Skip to content

Commit

Permalink
Change to using sobek instead of goja
Browse files Browse the repository at this point in the history
We are moving to a fork of goja under grafana org called sobek.

More info in:
- grafana/k6#3772
- grafana/k6#3773
  • Loading branch information
mstoykov committed Jun 10, 2024
1 parent 9553fc5 commit a649ce4
Show file tree
Hide file tree
Showing 6 changed files with 199 additions and 214 deletions.
60 changes: 30 additions & 30 deletions faker/faker.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
// Package faker contains Faker class implementation for goja.
// Package faker contains Faker class implementation for sobek.
package faker

import (
"math/rand"

"github.com/brianvoe/gofakeit/v6"
"github.com/dop251/goja"
"github.com/grafana/sobek"
"lukechampine.com/frand"
)

// Constructor is a Faker class constructor.
func Constructor(call goja.ConstructorCall, runtime *goja.Runtime) *goja.Object {
func Constructor(call sobek.ConstructorCall, runtime *sobek.Runtime) *sobek.Object {
seed := call.Argument(0).ToInteger()

return runtime.NewDynamicObject(newFaker(seed, runtime))
}

// New calls Faker constructor and returns new Faker object.
func New(seed int64, runtime *goja.Runtime) *goja.Object {
func New(seed int64, runtime *sobek.Runtime) *sobek.Object {
return Constructor(
goja.ConstructorCall{
sobek.ConstructorCall{
This: runtime.NewObject(),
Arguments: []goja.Value{runtime.ToValue(seed)},
Arguments: []sobek.Value{runtime.ToValue(seed)},
},
runtime,
)
Expand All @@ -30,11 +30,11 @@ func New(seed int64, runtime *goja.Runtime) *goja.Object {
// faker represents JavaScript Faker class.
type faker struct {
rand *rand.Rand
runtime *goja.Runtime
runtime *sobek.Runtime
}

// newFaker creates new Faker instance.
func newFaker(seed int64, runtime *goja.Runtime) *faker {
func newFaker(seed int64, runtime *sobek.Runtime) *faker {
src := frand.NewSource()

if seed != 0 {
Expand All @@ -44,46 +44,46 @@ func newFaker(seed int64, runtime *goja.Runtime) *faker {
return &faker{rand: rand.New(src), runtime: runtime} //nolint:gosec
}

// Delete implements goja.DynamicObject.
// Delete implements sobek.DynamicObject.
func (f *faker) Delete(_ string) bool {
return false
}

// Get implements goja.DynamicObject.
func (f *faker) Get(key string) goja.Value {
// Get implements sobek.DynamicObject.
func (f *faker) Get(key string) sobek.Value {
if key == "call" {
return f.runtime.ToValue(f.call)
}

category := newCategory(f, key)
if category == nil {
return goja.Undefined()
return sobek.Undefined()
}

return f.runtime.NewDynamicObject(category)
}

// Has implements goja.DynamicObject.
// Has implements sobek.DynamicObject.
func (f *faker) Has(_ string) bool {
return false
}

// Keys implements goja.DynamicObject.
// Keys implements sobek.DynamicObject.
func (f *faker) Keys() []string {
return getCategoryNames()
}

// Set implements goja.DynamicObject.
func (f *faker) Set(_ string, _ goja.Value) bool {
// Set implements sobek.DynamicObject.
func (f *faker) Set(_ string, _ sobek.Value) bool {
return false
}

// call invokes faker function by name.
// The faker function name is the first parameter, the rest of parameters passed to function.
func (f *faker) call(call goja.FunctionCall) goja.Value {
func (f *faker) call(call sobek.FunctionCall) sobek.Value {
function := call.Argument(0)

if goja.IsUndefined(function) {
if sobek.IsUndefined(function) {
panic(f.runtime.NewTypeError(function))
}

Expand All @@ -97,7 +97,7 @@ func (f *faker) call(call goja.FunctionCall) goja.Value {
return f.invoke(info, call)
}

func (f *faker) toMapParams(info *gofakeit.Info, call goja.FunctionCall) *gofakeit.MapParams {
func (f *faker) toMapParams(info *gofakeit.Info, call sobek.FunctionCall) *gofakeit.MapParams {
if len(info.Params) == 0 {
return nil
}
Expand All @@ -106,7 +106,7 @@ func (f *faker) toMapParams(info *gofakeit.Info, call goja.FunctionCall) *gofake

for idx, param := range info.Params {
val := call.Argument(idx)
if goja.IsUndefined(val) {
if sobek.IsUndefined(val) {
if len(param.Default) != 0 {
params.Add(param.Field, param.Default)
continue
Expand All @@ -131,7 +131,7 @@ func (f *faker) toMapParams(info *gofakeit.Info, call goja.FunctionCall) *gofake
return params
}

func (f *faker) invoke(info *gofakeit.Info, call goja.FunctionCall) goja.Value {
func (f *faker) invoke(info *gofakeit.Info, call sobek.FunctionCall) sobek.Value {
params := f.toMapParams(info, call)

val, err := info.Generate(f.rand, params, info)
Expand All @@ -156,34 +156,34 @@ func newCategory(faker *faker, name string) *category {
return &category{faker: faker, funcs: funcs}
}

// Delete implements goja.DynamicObject.
// Delete implements sobek.DynamicObject.
func (c *category) Delete(_ string) bool {
return false
}

// Get implements goja.DynamicObject.
func (c *category) Get(key string) goja.Value {
// Get implements sobek.DynamicObject.
func (c *category) Get(key string) sobek.Value {
info, ok := c.funcs[key]
if !ok {
return goja.Undefined()
return sobek.Undefined()
}

return c.faker.runtime.ToValue(func(call goja.FunctionCall) goja.Value {
return c.faker.runtime.ToValue(func(call sobek.FunctionCall) sobek.Value {
return c.faker.invoke(info, call)
})
}

// Has implements goja.DynamicObject.
// Has implements sobek.DynamicObject.
func (c *category) Has(_ string) bool {
return false
}

// Keys implements goja.DynamicObject.
// Keys implements sobek.DynamicObject.
func (c *category) Keys() []string {
return []string{}
}

// Set implements goja.DynamicObject.
func (c *category) Set(_ string, _ goja.Value) bool {
// Set implements sobek.DynamicObject.
func (c *category) Set(_ string, _ sobek.Value) bool {
return false
}
32 changes: 16 additions & 16 deletions faker/faker_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@ import (
"testing"

"github.com/brianvoe/gofakeit/v6"
"github.com/dop251/goja"
"github.com/grafana/sobek"
"github.com/stretchr/testify/require"
)

func Test_faker_dynamic(t *testing.T) {
t.Parallel()

faker := newFaker(11, goja.New())
faker := newFaker(11, sobek.New())

// Delete
require.False(t, faker.Delete("foo"))

// Get
require.False(t, goja.IsUndefined(faker.Get("call")))
require.True(t, goja.IsUndefined(faker.Get("no such category")))
require.False(t, goja.IsUndefined(faker.Get("zen")))
require.False(t, sobek.IsUndefined(faker.Get("call")))
require.True(t, sobek.IsUndefined(faker.Get("no such category")))
require.False(t, sobek.IsUndefined(faker.Get("zen")))

// Has
require.False(t, faker.Has("zen"))
Expand All @@ -36,15 +36,15 @@ func Test_faker_dynamic(t *testing.T) {
func Test_faker_invoke(t *testing.T) {
t.Parallel()

faker := newFaker(11, goja.New())
faker := newFaker(11, sobek.New())

info, ok := lookupFunc("username")

require.True(t, ok)

val := faker.invoke(info, goja.FunctionCall{This: goja.Undefined()})
val := faker.invoke(info, sobek.FunctionCall{This: sobek.Undefined()})

require.False(t, goja.IsUndefined(val))
require.False(t, sobek.IsUndefined(val))

clone := *info
info = &clone
Expand All @@ -54,14 +54,14 @@ func Test_faker_invoke(t *testing.T) {
}

require.Panics(t, func() {
faker.invoke(info, goja.FunctionCall{This: goja.Undefined()})
faker.invoke(info, sobek.FunctionCall{This: sobek.Undefined()})
})
}

func Test_newCategory(t *testing.T) {
t.Parallel()

faker := newFaker(11, goja.New())
faker := newFaker(11, sobek.New())

require.Nil(t, newCategory(faker, "no such category"))
require.NotNil(t, newCategory(faker, "zen"))
Expand All @@ -70,14 +70,14 @@ func Test_newCategory(t *testing.T) {
func Test_category_dynamic(t *testing.T) {
t.Parallel()

category := newCategory(newFaker(11, goja.New()), "zen")
category := newCategory(newFaker(11, sobek.New()), "zen")

// Delete
require.False(t, category.Delete("foo"))

// Get
require.False(t, goja.IsUndefined(category.Get("username")))
require.True(t, goja.IsUndefined(category.Get("no such function")))
require.False(t, sobek.IsUndefined(category.Get("username")))
require.True(t, sobek.IsUndefined(category.Get("no such function")))

// Has
require.False(t, category.Has("username"))
Expand All @@ -92,14 +92,14 @@ func Test_category_dynamic(t *testing.T) {
func Test_faker_toMapParams(t *testing.T) {
t.Parallel()

runtime := goja.New()
runtime := sobek.New()
faker := newFaker(11, runtime)

info, ok := lookupFunc("intRange")

require.True(t, ok)

var call goja.FunctionCall
var call sobek.FunctionCall

require.Panics(t, func() {
faker.toMapParams(info, call)
Expand All @@ -119,7 +119,7 @@ func Test_faker_toMapParams(t *testing.T) {

info.Params[1].Default = "24"

call.Arguments = []goja.Value{runtime.ToValue(1)}
call.Arguments = []sobek.Value{runtime.ToValue(1)}

mparams = faker.toMapParams(info, call)

Expand Down
24 changes: 12 additions & 12 deletions faker/faker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,55 +3,55 @@ package faker_test
import (
"testing"

"github.com/dop251/goja"
"github.com/grafana/sobek"
"github.com/stretchr/testify/require"
"github.com/szkiba/xk6-faker/faker"
)

func Test_Constructor(t *testing.T) {
t.Parallel()

vm := goja.New()
vm := sobek.New()

require.NoError(t, vm.Set("Faker", faker.Constructor))

val, err := vm.RunString("new Faker()")

require.NoError(t, err)

_, ok := goja.AssertFunction(val.ToObject(vm).Get("call"))
_, ok := sobek.AssertFunction(val.ToObject(vm).Get("call"))

require.True(t, ok, "has call() method")

val, err = vm.RunString("new Faker(11)")

require.NoError(t, err)

_, ok = goja.AssertFunction(val.ToObject(vm).Get("call"))
_, ok = sobek.AssertFunction(val.ToObject(vm).Get("call"))

require.True(t, ok, "has call() method")
}

func Test_New(t *testing.T) {
t.Parallel()

vm := goja.New()
vm := sobek.New()

var val *goja.Object
var val *sobek.Object

require.NotPanics(t, func() {
val = faker.New(0, vm)
})

_, ok := goja.AssertFunction(val.ToObject(vm).Get("call"))
_, ok := sobek.AssertFunction(val.ToObject(vm).Get("call"))

require.True(t, ok, "has call() method")
}

func Test_Faker_call(t *testing.T) {
t.Parallel()

vm := goja.New()
vm := sobek.New()

require.NoError(t, vm.Set("Faker", faker.Constructor))

Expand All @@ -72,7 +72,7 @@ func Test_Faker_call(t *testing.T) {
func Test_Faker_no_parameter(t *testing.T) {
t.Parallel()

vm := goja.New()
vm := sobek.New()

require.NoError(t, vm.Set("Faker", faker.Constructor))

Expand All @@ -85,7 +85,7 @@ func Test_Faker_no_parameter(t *testing.T) {
func Test_Faker_int_parameters(t *testing.T) {
t.Parallel()

vm := goja.New()
vm := sobek.New()

require.NoError(t, vm.Set("Faker", faker.Constructor))

Expand All @@ -98,7 +98,7 @@ func Test_Faker_int_parameters(t *testing.T) {
func Test_Faker_string_array_parameter(t *testing.T) {
t.Parallel()

vm := goja.New()
vm := sobek.New()

require.NoError(t, vm.Set("Faker", faker.Constructor))

Expand All @@ -111,7 +111,7 @@ func Test_Faker_string_array_parameter(t *testing.T) {
func Test_Faker_int_array_parameter(t *testing.T) {
t.Parallel()

vm := goja.New()
vm := sobek.New()

require.NoError(t, vm.Set("Faker", faker.Constructor))

Expand Down
Loading

0 comments on commit a649ce4

Please sign in to comment.