Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to unset / delete runtime bindings #404

Open
raidancampbell opened this issue May 8, 2021 · 1 comment
Open

Unable to unset / delete runtime bindings #404

raidancampbell opened this issue May 8, 2021 · 1 comment

Comments

@raidancampbell
Copy link
Contributor

The Otto runtime exposes a Set function for setting a variable, but no equivalent deletion is available. The closest I could come to was another Set to otto.UndefinedValue(), but that doesn't yield the same behavior. The below code snippet describes what I'm trying to achieve.

func TestOtto(t *testing.T) {
	runtime := otto.New()

	_, err := runtime.Run("console.log(foo)")
	assert.NotNil(t, err)
	assert.Contains(t, err.Error(), "ReferenceError: 'foo' is not defined")

	err = runtime.Set("foo", "bar")
	assert.Nil(t, err)
	_, err = runtime.Run("console.log(foo)")
	assert.Nil(t, err)

	err = runtime.Set("foo", otto.UndefinedValue())
	assert.Nil(t, err)
	_, err = runtime.Run("console.log(foo)")
	assert.NotNil(t, err) // fails, err is nil
	assert.Equal(t, err, "ReferenceError: 'foo' is not defined") // fails, err is nil
}
@stevenh
Copy link
Collaborator

stevenh commented Nov 25, 2022

Happy to accept a PR to add this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants