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

Cannot push into array that was set with vm.Set() #357

Closed
mgregory-carecloud opened this issue Sep 12, 2019 · 0 comments · Fixed by #460
Closed

Cannot push into array that was set with vm.Set() #357

mgregory-carecloud opened this issue Sep 12, 2019 · 0 comments · Fixed by #460

Comments

@mgregory-carecloud
Copy link

mgregory-carecloud commented Sep 12, 2019

This example prints a TypeError with no error message.

func main() {
	vm := otto.New();
	arr := []string{"wow", "hey"};
	vm.Set("arr", arr);

	_, err := vm.Run("console.log(arr); arr.push('huh?'); console.log(arr);");
	if err != nil {
		log.Print(err.Error());
	}
}

Otto could fix this by cloning inputs automatically, or at least returning a helpful error message.

For now, cloning your inputs will resolve the issue. (Note the 'arr = arr.slice()')

func main() {
	vm := otto.New();
	arr := []string{"wow", "hey"};
	vm.Set("arr", arr);

	_, err := vm.Run("arr = arr.slice(); console.log(arr); arr.push('huh?'); console.log(arr);");
	if err != nil {
		log.Print(err.Error());
	}
}
stevenh added a commit that referenced this issue Nov 27, 2022
Add support for direct use of push to go slices without converting to
array using .slice().

Fixes #357
stevenh added a commit that referenced this issue Nov 27, 2022
Add support for direct use of push to go slices without converting to
array using .slice().

Fixes #357

Also:
* Add classGoSlice and use it, to make it clear the type when debugging.
stevenh added a commit that referenced this issue Nov 27, 2022
Add support for direct use of push to go slices without converting to
array using .slice().

Fixes #357

Also:
* Add classGoSlice and use it, to make it clear the type when debugging.
stevenh added a commit that referenced this issue Nov 27, 2022
Add support for direct use of push to go slices without converting to
array using .slice().

Fixes #357

Also:
* Add classGoSlice and use it, to make it clear the type when debugging.
sg3des pushed a commit to sg3des/otto that referenced this issue Jul 17, 2023
Add support for direct use of push to go slices without converting to
array using .slice().

Fixes robertkrimen#357

Also:
* Add classGoSlice and use it, to make it clear the type when debugging.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant