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

Nested []any should be addressable #620

Closed
dop251 opened this issue Oct 9, 2024 Discussed in #618 · 2 comments
Closed

Nested []any should be addressable #620

dop251 opened this issue Oct 9, 2024 Discussed in #618 · 2 comments
Labels

Comments

@dop251
Copy link
Owner

dop251 commented Oct 9, 2024

Discussed in #618

Originally posted by pgundlach October 8, 2024
The following code prints []. How can I add the word "Hello" to the slice? The slice is a []any slice.

I can of course add a "addToSlice" method, but I'expect a .push() to work.

package main

import (
	"fmt"
	"os"

	"github.com/dop251/goja"
)

type document struct {
	Items []any
}

func (d *document) Check() {
	fmt.Println(d.Items)
}

func main() {
	runtime := goja.New()
	runtime.Set("newdoc", func() *document { return &document{} })

	_, err := runtime.RunString(`
var d = newdoc();
d.Items.push("Hello");
d.Check();
`)
	if err != nil {
		fmt.Println(err)
		os.Exit(-1)
	}

}
@dop251
Copy link
Owner Author

dop251 commented Oct 9, 2024

This is actually a bug. The field should be addressable, but it doesn't work for 'simple' slices (i.e. []any). If you change it to []string it will work, so this needs to be fixed.

@dop251 dop251 added the bug label Oct 9, 2024
@dop251 dop251 closed this as completed in 21e3355 Oct 9, 2024
@pgundlach
Copy link

Thank you very much, it works nicely in my real-world use case.

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

No branches or pull requests

2 participants