webui-dev / webui

Use any web browser or WebView as GUI, with your preferred language in the backend and modern web technologies in the frontend, all in a lightweight portable library.
https://webui.me
MIT License
2.37k stars 146 forks source link

RangeError: Maximum call stack size exceeded (webui.js) #270

Closed 7flash closed 7 months ago

7flash commented 7 months ago

This error appear in browser when trying to send heavy response to backend in result of script callback

Particularly it happens in this.#ws.onmessage handler

Here TextEncoder().encode(FunReturn) fails because of some in browser CPU limits

          const Return8 = Uint8Array.of(
            this.#WEBUI_SIGNATURE,
            0,
            0,
            0,
            0,
            // Token (4 Bytes)
            0,
            0,
            // ID (2 Bytes)
            this.#CMD_JS,
            FunError ? 1 : 0,
            ...new TextEncoder().encode(FunReturn)
          );

Which can be possible solutions to this issue? Notice, it works when response is small but fails when its around 250kb

AlbertShown commented 7 months ago

I suppose you are using C as the backend. Try using webui_send_raw() in the backend, and this has no TextEncoder(), which may solve your issue.

Backend:

webui_send_raw(myWindow, "myJavascriptFunction", myBuffer, 1024); // 1024 is myBuffer len

Frontend:

myJavascriptFunction(myData) {
    // myData is Uint8Arra...
}
AlbertShown commented 7 months ago

Sorry. I just noticed that you want it the other way... so to send big data to the backend and avoid TextEncoder, make your data as Uint8Array instead of a string object. More details here:

https://github.com/webui-dev/webui/blob/main/examples/C/call_c_from_js/main.c Look at const big_arr = new Uint8Array(arr_size);.

AlbertShown commented 7 months ago

Wait, you are right... const arr_size = 250 * 1000; gives:

image

hassandraga commented 7 months ago

Thank you @7flash @AlbertShown for reporting. This will be fixed very soon in v2.4.1.