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
syscall/js: any way of accessing javascript list members? #29212
Comments
Works for me. index.html <!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Go wasm</title>
</head>
<body>
<button id="close">Shutdown app</button>
<script src="wasm_loader.js"></script>
<script>
const go = new Go();
let mod, inst;
console.log("Initializing wasm...");
WebAssembly.instantiateStreaming(
fetch("main.wasm", {cache: 'no-cache'}), go.importObject).then((result) => {
mod = result.module;
inst = result.instance;
console.log("Initialization complete.");
run();
test([1, 2, 3]);
});
async function run() {
await go.run(inst);
}
</script>
</body>
</html> main.go // +build js,wasm
package main
import (
"fmt"
"syscall/js"
)
var console = js.Global().Get("console")
func main() {
// Setup callbacks
cb := js.NewCallback(func(i []js.Value) {
fmt.Println(i[0])
fmt.Println(i[0].Length())
fmt.Println(i[0].Type())
arr := i[0]
fmt.Println(arr.Index(0))
})
js.Global().Set("test", cb)
done := make(chan struct{})
shutdownCb := js.NewEventCallback(js.PreventDefault, func(ev js.Value) {
done <- struct{}{}
})
js.Global().Get("document").
Call("getElementById", "close").
Call("addEventListener", "click", shutdownCb)
<-done
console.Call("log", "Shutting down app")
cb.Release()
shutdownCb.Release()
} Output -
Attached the wasm_loader.js too. I will close the issue now as I think the question has been adequately answered. We mainly track bugs here in the issue tracker. If you still face issues, I would request you to take a look at the Questions wiki page; it has a list of good places for asking questions. |
hi @agnivade you explicitly called test([1, 2, 3]); :- here [1,2,3] will be array, the code works with an array , try with a List. And creating the list like: immutable.List([1, 2, 3]) |
I see. Yes, it will not work in that case because that list is not a browser native type, rather an object with its own set of methods. Effectively, to the browser, that is just a normal object and it has no idea that it is internally a list. Since that object does not behave like an array, so you will not get that behavior in Go.
So, essentially you are getting the stringified representation of it which is |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
For our use case, I am creating a wasm file from the golang project, the callback function is passed a list of integers.
JS code: test(immutable_1.List([1, 2, 3]));
GOLANG code:
func test(params []js.Value){
param0:= params[0]
len1 := param0.Length()
fmt.Println("param0: ", param0)
fmt.Println("len: ", len1)
}
the println prints:
param0: List [ 1, 2, 3 ]
len: 0
Is there a way to access the list length and value at the indexes?
What did you expect to see?
I expected the list to behave like an array, the Length, and Index functions to work on it too
What did you see instead?
The array function not able to work on List
The text was updated successfully, but these errors were encountered: