From c0a8f561c88cc0c2c1fc817a157b0f7e044a7774 Mon Sep 17 00:00:00 2001 From: Ivan <2103732+codebien@users.noreply.github.com> Date: Thu, 17 Mar 2022 14:58:13 +0100 Subject: [PATCH] js: Drop common.BindToGlobal --- js/bundle.go | 19 +++++++++++++++---- js/common/bridge.go | 38 -------------------------------------- js/common/bridge_test.go | 9 --------- 3 files changed, 15 insertions(+), 51 deletions(-) diff --git a/js/bundle.go b/js/bundle.go index 9c90df07562..65dcc919f1f 100644 --- a/js/bundle.go +++ b/js/bundle.go @@ -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) @@ -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) { diff --git a/js/common/bridge.go b/js/common/bridge.go index 431437b3341..ce5a6ad16d5 100644 --- a/js/common/bridge.go +++ b/js/common/bridge.go @@ -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 . - * - */ - package common import ( @@ -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()) - } - } -} diff --git a/js/common/bridge_test.go b/js/common/bridge_test.go index 3fb4640a089..e6ad1232fb9 100644 --- a/js/common/bridge_test.go +++ b/js/common/bridge_test.go @@ -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 @@ -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) })