Skip to content

Commit

Permalink
js: Drop common.BindToGlobal
Browse files Browse the repository at this point in the history
  • Loading branch information
codebien committed Mar 18, 2022
1 parent 6836146 commit c0a8f56
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 51 deletions.
19 changes: 15 additions & 4 deletions js/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,10 +330,7 @@ func (b *Bundle) instantiate(logger logrus.FieldLogger, rt *goja.Runtime, init *
}
init.moduleVUImpl.initEnv = initenv
init.moduleVUImpl.ctx = context.Background()
unbindInit := common.BindToGlobal(rt, map[string]interface{}{
"require": init.Require,
"open": init.Open,
})
unbindInit := b.setInitGlobals(rt, init)
init.moduleVUImpl.eventLoop = newEventLoop(init.moduleVUImpl)
err := init.moduleVUImpl.eventLoop.start(func() error {
_, err := rt.RunProgram(b.Program)
Expand Down Expand Up @@ -361,6 +358,20 @@ func (b *Bundle) instantiate(logger logrus.FieldLogger, rt *goja.Runtime, init *
return nil
}

func (b *Bundle) setInitGlobals(rt *goja.Runtime, init *InitContext) (unset func()) {
mustSet := func(k string, v interface{}) {
if err := rt.Set(k, v); err != nil {
panic(fmt.Errorf("failed to set '%s' global object: %w", k, err))
}
}
mustSet("require", init.Require)
mustSet("open", init.Open)
return func() {
mustSet("require", goja.Undefined())
mustSet("open", goja.Undefined())
}
}

func generateSourceMapLoader(logger logrus.FieldLogger, filesystems map[string]afero.Fs,
) func(path string) ([]byte, error) {
return func(path string) ([]byte, error) {
Expand Down
38 changes: 0 additions & 38 deletions js/common/bridge.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,3 @@
/*
*
* k6 - a next-generation load testing tool
* Copyright (C) 2016 Load Impact
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

package common

import (
Expand Down Expand Up @@ -113,21 +93,3 @@ func (FieldNameMapper) FieldName(t reflect.Type, f reflect.StructField) string {
// MethodName is part of the goja.FieldNameMapper interface
// https://godoc.org/github.com/dop251/goja#FieldNameMapper
func (FieldNameMapper) MethodName(t reflect.Type, m reflect.Method) string { return MethodName(t, m) }

// BindToGlobal Binds an object's members to the global scope. Returns a function that un-binds them.
// Note that this will panic if passed something that isn't a struct; please don't do that.
func BindToGlobal(rt *goja.Runtime, data map[string]interface{}) func() {
keys := make([]string, len(data))
i := 0
for k, v := range data {
rt.Set(k, v)
keys[i] = k
i++
}

return func() {
for _, k := range keys {
rt.Set(k, goja.Undefined())
}
}
}
9 changes: 0 additions & 9 deletions js/common/bridge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,14 +277,6 @@ func TestFieldNameMapper(t *testing.T) {
}
}

func TestBindToGlobal(t *testing.T) {
rt := goja.New()
unbind := BindToGlobal(rt, map[string]interface{}{"a": 1})
assert.Equal(t, int64(1), rt.Get("a").Export())
unbind()
assert.Nil(t, rt.Get("a").Export())
}

func BenchmarkProxy(b *testing.B) {
types := []struct {
Name, FnName string
Expand Down Expand Up @@ -411,7 +403,6 @@ func BenchmarkProxy(b *testing.B) {
b.Run("Call", func(b *testing.B) {
rt := goja.New()
rt.SetFieldNameMapper(FieldNameMapper{})
// ctx := context.Background()
fn := func() {}
typ.Fn(b, fn)
})
Expand Down

0 comments on commit c0a8f56

Please sign in to comment.