vugu / vugu

Vugu: A modern UI library for Go+WebAssembly (experimental)
https://www.vugu.org
MIT License
4.8k stars 175 forks source link

js Set can not set Function #266

Open CJBigsharp opened 5 months ago

CJBigsharp commented 5 months ago

Describe the bug js Set can not set a Function ,it will be throw panic: ValueOf: invalid value

Software Versions Vugu version: Go version: Browser and version:

To Reproduce Please describe how to reproduce the bug you encountered.

If possible, please include a concise code snippet which reproduces the issue (and excludes other unnecessary functionality, for clarity).

Expected behavior check the js.Set, and add a judge for funtion

Additional Notes Anything else...

CJBigsharp commented 5 months ago

func (v Value) Set(p string, x interface{}) { // judge x is FuncOf if f, ok := x.(Func); ok { sjs.Value(v).Set(p, f.f) } else { sjs.Value(v).Set(p, x) }

}

owenwaller commented 5 months ago

Hi @CJBigsharp

Thanks for this, but can you please provide a complete working example of what you are trying to do, and why? Can you also tell us what version of Go and vugu you have been using, and how you compiled the code? We also need to see the full panic trace.

I suspect, this may be noting to do with vugu, if and only if sjs is a reference to the syscall/js package. In this case you would expect Set to panic when v is not a valid javascript object. This is the documented behaviour of Set.

If this is the case then my guess is that sjs.Value(v) is not returning you a javascript object, which causes Set to panic.

But without more context that is just a guess.

CJBigsharp commented 3 months ago

Hi @CJBigsharp

Thanks for this, but can you please provide a complete working example of what you are trying to do, and why? Can you also tell us what version of Go and vugu you have been using, and how you compiled the code? We also need to see the full panic trace.

I suspect, this may be noting to do with vugu, if and only if sjs is a reference to the syscall/js package. In this case you would expect Set to panic when v is not a valid javascript object. This is the documented behaviour of Set.

If this is the case then my guess is that sjs.Value(v) is not returning you a javascript object, which causes Set to panic.

But without more context that is just a guess.

sjs: func (v Value) Set(p string, x interface{}) { if vType := v.Type(); !vType.isObject() { panic(&ValueError{"Value.Set", vType}) } xv := ValueOf(x) // Only sjs type can be recognized, the kind of x is Func, it will be panic valueSet(v.ref, p, xv.ref) runtime.KeepAlive(v) runtime.KeepAlive(xv) }

CJBigsharp commented 3 months ago

Hi @CJBigsharp

Thanks for this, but can you please provide a complete working example of what you are trying to do, and why? Can you also tell us what version of Go and vugu you have been using, and how you compiled the code? We also need to see the full panic trace.

I suspect, this may be noting to do with vugu, if and only if sjs is a reference to the syscall/js package. In this case you would expect Set to panic when v is not a valid javascript object. This is the documented behaviour of Set.

If this is the case then my guess is that sjs.Value(v) is not returning you a javascript object, which causes Set to panic.

But without more context that is just a guess.

    change := js.FuncOf(c.PlayingRadioChanged)
playingRadio.Call("change",change)  // **it will panic**
owenwaller commented 3 months ago

Hi @CJBigsharp

Sorry, but I need a complete working example that shows that you are trying to do.

What is c in the above?

What is the code of PlayingRadioChanged and playingRadio?

What is the panic message.

I need to know all of this before I can help you. Can you please create a minimal, complete example in the vugu playgound?

https://play.vugu.org/

@bradleypeabody and I can then try and help you. To me this still looks like the syscall/js package (I have no idea what sjs is) is being used incorrectly. But without a reproducible example that remains a guess on my part.

Thanks

CJBigsharp commented 3 months ago

PlayingRadioChanged

PlayingRadioChanged is a go func playingRadio is a js Value

CJBigsharp commented 3 months ago

Hi @CJBigsharp

Sorry, but I need a complete working example that shows that you are trying to do.

What is c in the above?

What is the code of PlayingRadioChanged and playingRadio?

What is the panic message.

I need to know all of this before I can help you. Can you please create a minimal, complete example in the vugu playgound?

https://play.vugu.org/

@bradleypeabody and I can then try and help you. To me this still looks like the syscall/js package (I have no idea what sjs is) is being used incorrectly. But without a reproducible example that remains a guess on my part.

Thanks playingRadio := js.Global().Call("$",input:radio[name="playingSubject"]) func (c *Playing)PlayingRadioChanged(this js.Value,args []js.Value) interface{}{ js.Global().Get("console").Call("log","PlayingRadioChanged") js.Global().Get("console").Call("log",this.Get("id")) playingRadioId := this.Get("id").String() c.SelectSubjectKey = playingRadioId c.SetPlayingSubjectKey(playingRadioId) return nil }

owenwaller commented 3 months ago

@CJBigsharp

I really don't mean to labour the point but can you please come up with a full working example that demonstrates the panic you are seeing in either the vugu playground or the standard Go playground.

https://play.vugu.org/ https://go.dev/play/

In the above code I have no idea what Playing is, and as far as I can tell from that example, you are not using any vugu functionality. You are calling syscall/js directly.

I need a complete example I can run, before I can help you.

Thanks

owenwaller commented 3 months ago

If in this code code snippet "$" is some sort of JQuery call (???)

playingRadio := js.Global().Call("$",input:radio[name="playingSubject"])

then this isn't going to work IIRC. JQuery is going to walk or manipulate the DOM, which vugu changes dynamically. JQuery and vugu are known not to play nicely. See Issue #153

I'm unclear how any of this relates to the original question of "Set" (which "Set", in which package?) not working correctly.

As I said without a complete minimal example and an explanation of what you are trying to achieve it's difficult to offer much help, at this stage.