webui-dev / nim-webui

Use any web browser as GUI, with Nim in the backend and HTML5 in the frontend.
https://webui.me
MIT License
130 stars 9 forks source link

SIGSEGV : Illegal storage access when calling window.bind("CheckPassword") do (e: Event) function from examples #17

Closed demetera closed 1 year ago

demetera commented 1 year ago

Trying to run examples to get familiar with the webui hello_world.nim generates this EM:

SIGSEGV: Illegal storage access. (Attempt to read from nil?)
fish: Job 1, './test' terminated by signal SIGSEGV (Address boundary error)

Running Nim version 1.6.12 and webui 0.5.0 Failing with nimble install (stable and git versions), nimble develop and git recursive too

neroist commented 1 year ago

I can't reproduce this, can you figure out which line in bind or maybe bindHandler this error is coming from?

demetera commented 1 year ago

Thanks for your prompt reply. This code:

window.bind("MyButton1") do (e: Event): # Check the password function
    echo "Clicked"

doesn't fail, but after I add next line:

var js = e.window.script("return document.getElementById(\"MyInput\").value;")

generates this error. I'm using Vivaldi, as a main browser, but Firefox also does the same.

Traceback showing this output (I've shrinked your code without comments, that's why line numbering is different). First line in the Traceback is the main() function call in the end, second line is wait()

/home/demetera/Programming/Nim/webuitest/test.nim(68) test
/home/demetera/Programming/Nim/webuitest/test.nim(66) main
/home/demetera/.nimble/pkgs/webui-0.5.0/webui.nim(36) wait
/home/demetera/.nimble/pkgs/webui-0.5.0/webui.nim(230) bindHandler
/home/demetera/Programming/Nim/webuitest/test.nim(45) :anonymous
/home/demetera/.nimble/pkgs/webui-0.5.0/webui.nim(202) script
/home/demetera/.choosenim/toolchains/nim-1.6.12/lib/pure/strutils.nim(1799) join
/home/demetera/.choosenim/toolchains/nim-1.6.12/lib/system/strmantle.nim(221) nimCharToStr
/home/demetera/.choosenim/toolchains/nim-1.6.12/lib/system/gc.nim(486) newObj
SIGSEGV: Illegal storage access. (Attempt to read from nil?)
Error: execution of an external program failed: '/home/demetera/Programming/Nim/webuitest/test '
AlbertShown commented 1 year ago

Probably the same as https://github.com/webui-dev/nim-webui/issues/15

@demetera Please change webui.nim:261 from

data = buffer.join().strip(leading = false, chars = {'\x00'})

to

data = $(cast[cstring](addr buffer[0]))

If this fixes the issue, then we should merge https://github.com/webui-dev/nim-webui/pull/16

demetera commented 1 year ago

@AlbertShown

Yes. It worked! Thanks!

In the example I've used this modified main proc:

proc main =
  # Create a window
  let window = newWindow()

  window.bind("MyButton1") do (e: Event): # Check the password function
    var js = e.window.script("return document.querySelector(\"#MyInput\").value;")
    echo js

  window.bind("MyButton2") do (_: Event):
    webui.exit()

  window.show(html)
  wait()

main()

It produces back (data: "123", error: true) when password is 123, but doesn't SIGSEGV me after I've replaced the line in webui.nim as you've suggested I've used querySelector instead of getElementById, but with get elements is the same behaviour. js.data is true, but it's a different problem.

Thanks!

AlbertShown commented 1 year ago

@demetera You are welcome! About the JavaScript error, you can hit F12 on the window, then type in the console your script to make sure first is working.

image

demetera commented 1 year ago

Thanks for the hint! I will figure that one out indeed. I have used example which wasn't compiled on my machine, That I was wondered.

But main point was that the problem solved. I guess #16 was the case.

At the meantime, I'm using Vivaldi browser and webui on Arch, where Vivaldi executed by vivaldi-stable instead of vivaldi like in C code backend used by wrapper. Epic coincidence. Left the issue / proposal on C repository cause I don't want to make a PR for this without knowing what is expected (change the code or push the note in docs that Arch users have to use an alias)

clzls commented 1 year ago

@AlbertShown

Yes. It worked! Thanks!

In the example I've used this modified main proc:

proc main =
  # Create a window
  let window = newWindow()

  window.bind("MyButton1") do (e: Event): # Check the password function
    var js = e.window.script("return document.querySelector(\"#MyInput\").value;")
    echo js

  window.bind("MyButton2") do (_: Event):
    webui.exit()

  window.show(html)
  wait()

main()

It produces back (data: "123", error: true) when password is 123, but doesn't SIGSEGV me after I've replaced the line in webui.nim as you've suggested I've used querySelector instead of getElementById, but with get elements is the same behaviour. js.data is true, but it's a different problem.

Thanks!

error here was probably a mistake in release version and seemed to be fixed in the main branch (a not operator was added in https://github.com/webui-dev/nim-webui/commit/f3b7f8061de9d6da8442fc06d89676f931f09374).

(Personally I think these 2 changes together deserve a patch release 0.5.1)

neroist commented 1 year ago

error here was probably a mistake in release version and seemed to be fixed in the main branch (a not operator was added in f3b7f80).

(Personally I think these 2 changes together deserve a patch release 0.5.1)

It was no mistake, but I thought it didn't make sense for error to be false if there was an error, so I changed it.

Also, yeah, it does deserve a new patch, I'll make one soon.

AlbertShown commented 1 year ago

Let's not forgot https://github.com/webui-dev/nim-webui/issues/13