Skip to content

Commit

Permalink
Add global as in nodejs for backwards compatibility (#1845)
Browse files Browse the repository at this point in the history
this is only being added to the "extented" compatibility-mode as it was
being provided by core-js before it was dropped.
  • Loading branch information
mstoykov authored Feb 8, 2021
1 parent c6dec36 commit a379c37
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
4 changes: 4 additions & 0 deletions js/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,10 @@ func (b *Bundle) instantiate(logger logrus.FieldLogger, rt *goja.Runtime, init *
rt.Set("__VU", vuID)
rt.Set("console", common.Bind(rt, newConsole(logger), init.ctxPtr))

if init.compatibilityMode == lib.CompatibilityModeExtended {
rt.Set("global", rt.GlobalObject())
}

// TODO: get rid of the unused ctxPtr, use a real external context (so we
// can interrupt), build the common.InitEnvironment earlier and reuse it
initenv := &common.InitEnvironment{
Expand Down
12 changes: 12 additions & 0 deletions js/bundle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,18 @@ func TestNewBundle(t *testing.T) {
}
})
t.Run("CompatibilityMode", func(t *testing.T) {
t.Run("Extended/ok/global", func(t *testing.T) {
rtOpts := lib.RuntimeOptions{
CompatibilityMode: null.StringFrom(lib.CompatibilityModeExtended.String()),
}
_, err := getSimpleBundle(t, "/script.js",
`module.exports.default = function() {}
if (global.Math != Math) {
throw new Error("global is not defined");
}`, rtOpts)

assert.NoError(t, err)
})
t.Run("Base/ok/Minimal", func(t *testing.T) {
rtOpts := lib.RuntimeOptions{
CompatibilityMode: null.StringFrom(lib.CompatibilityModeBase.String()),
Expand Down

0 comments on commit a379c37

Please sign in to comment.