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

Modifying a pointer to a slice from Go doesn't seem to update properly in goja #521

Closed
ganigeorgiev opened this issue Jun 21, 2023 · 1 comment
Labels

Comments

@ganigeorgiev
Copy link
Contributor

ganigeorgiev commented Jun 21, 2023

Modifying a slice value or pointer to a slice from Go doesn't seem to update properly in goja. Consider the following code sample:

package main

import (
    "fmt"

    "github.com/dop251/goja"
)

func main() {
    data := &[]int{}

    vm := goja.New()
    vm.Set("data", data)
    vm.Set("print", fmt.Println)
    vm.Set("append", func(a *[]int, v int) {
        *a = append(*a, v)
    })

    _, err := vm.RunString(`
        // modify with js
        data.push(1);
        print("push:", data.length); // 1

        // modify with go
        append(data, 2)
        print("append:", data.length); // still 1

        // but both elements are there
        print("data.0", data[0]) // 1
        print("data.1", data[1]) // 2
    `)
    if err != nil {
        panic(err)
    }
}

I haven't explored the goja internals yet to understand what actually is happening, but:

  1. Is this some version of the implementation caveats mentioned in the readme?

  2. If it is a caveat, is there a workaround, even if that means sacrificing the performance, eg. overwriting the default array behavior?

@dop251
Copy link
Owner

dop251 commented Jun 21, 2023

This is a case of an overzealous optimisation around the length property, and not maintaining the original value type for non-reflect slices (does not directly apply to your case, but still needs fixing).

@dop251 dop251 added the bug label Jun 21, 2023
@dop251 dop251 closed this as completed in 7749907 Jun 21, 2023
Gabri3l pushed a commit to Gabri3l/goja that referenced this issue Sep 20, 2023
…rink. Maintain pointer type of a wrapped *[]any. Fixes dop251#521.

(cherry picked from commit 7749907)
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