Trying webworker sample, no console output...

Entered the examples, tried running locally & on remote web server, web page loads, javascript loads, no console output...
14 Replies
apignotti
apignotti5mo ago
Can you clarify which product and what example are you following?
DakerShaffer
DakerShafferOP5mo ago
Leaning Technologies Developer Hub
Threading with Web Workers - Cheerp Documentation
Cheerp is an enterprise-grade compiler toolchain that can compile C/C++ into efficient WebAssembly and JavaScript. It is open source, liberally licensed, and actively developed by Leaning Technologies.
apignotti
apignotti5mo ago
Please share what you have in the console log And network tab as well,to make sure the code can be loaded
DakerShaffer
DakerShafferOP5mo ago
% /Applications/cheerp/bin/clang++ --version
Cheerp 3.0 clang version 16.0.0 (https://github.com/leaningtech/cheerp-compiler 29e4600563ed2153faf0221362ce4f0a5b93e1ce) Target: cheerp-leaningtech-webbrowser-wasm Thread model: posix InstalledDir: /Applications/cheerp/bin
apignotti
apignotti5mo ago
I meant the devtools console
DakerShaffer
DakerShafferOP5mo ago
Console is totally empty Got the other examples working
apignotti
apignotti5mo ago
Check the network tab Can you confirm both javascript files are loaded?
DakerShaffer
DakerShafferOP5mo ago
No description
DakerShaffer
DakerShafferOP5mo ago
In debugging it, breakpoints added at the addEventListener calls are reached Ah... interesting, IMO: console in the 'worker.js' file shows the "Hello World" output, not visible from loading the index.html Wait, apparently not not working again... had it call the event listener once while debugging though!? Hmm; if I set a breakpoint right on the 'console.log' call, it is reaching there, and if I single step over, it shows up in the console, but otherwise mostly not (?). Yes, for some reason single stepping from the addEventListener() calls causes the console.log() call to be eventually reached, otherwise generally not (!?). WTF?
apignotti
apignotti5mo ago
The docs example is incorrect, we need to fix this The underlying problem is that the worker can lose messages since compiling wasm code is asynchronous This is a general web worker issue, not connected to Cheerp, but the example needs to be modified to take that into account Please try with the following modified sources: cheerpWorkerHost.cpp
c++
// cheerpWorkerHost.cpp: Code to include in the HTML page
#include <cheerp/client.h>
#include <cheerp/clientlib.h>

using namespace client;

[[cheerp::genericjs]]
void webMain()
{
Worker* w = new Worker("cheerpWorker.js");
w->addEventListener("message", cheerp::Callback([w](MessageEvent* e) {
if(e->get_data() == nullptr)
w->postMessage(String("Hello World"));
else
console.log((String*)(e->get_data()));
}));
}
c++
// cheerpWorkerHost.cpp: Code to include in the HTML page
#include <cheerp/client.h>
#include <cheerp/clientlib.h>

using namespace client;

[[cheerp::genericjs]]
void webMain()
{
Worker* w = new Worker("cheerpWorker.js");
w->addEventListener("message", cheerp::Callback([w](MessageEvent* e) {
if(e->get_data() == nullptr)
w->postMessage(String("Hello World"));
else
console.log((String*)(e->get_data()));
}));
}
cheerpWorker.cpp
c++
// cheerpWorker.cpp: Code that runs inside the worker
#include <cheerp/clientlib.h>
#include <cheerp/client.h>

using namespace client;

[[cheerp::genericjs]]
void webMain()
{
addEventListener("message", cheerp::Callback([](MessageEvent* e) {
postMessage(e->get_data());
postMessage(e->get_data());
}));
postMessage(nullptr);
}
c++
// cheerpWorker.cpp: Code that runs inside the worker
#include <cheerp/clientlib.h>
#include <cheerp/client.h>

using namespace client;

[[cheerp::genericjs]]
void webMain()
{
addEventListener("message", cheerp::Callback([](MessageEvent* e) {
postMessage(e->get_data());
postMessage(e->get_data());
}));
postMessage(nullptr);
}
The idea for the new approach is to wait for the worker to be ready before attempting to send an actual message. The first null message is interpreted as "ready" message. @Chen Please devise a better example for the worker docs and update labs accordingly
DakerShaffer
DakerShafferOP5mo ago
Reported on github issues also, with reply showing compiler ~"error: variable 'w' cannot be implicitly captured in a lambda with no capture-default specified w->postMessage(String("Hello World"));"...
apignotti
apignotti5mo ago
Simply add w to the capture list, apologies for the mistake Hmmm.. Actually the code above includes the [w] as expected I think you might have updated the lambda body, but not the whole code
DakerShaffer
DakerShafferOP5mo ago
Ah, yes, you are correct (Ops, sorry about my part in that!) : I just pasted in the apparent body change, missing the [w]... The altered code does appear to be producing the dual "Hello World" console result in my Safari and FireFox browsers, but now gives an error in Chrome saying ~"worker.js:1 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'buffer') at worker.js:1:1143", hiliting the " heap = asm.W.buffer;" line. Hmm... although I had reloaded & hit the option-reload button, now I explicitly selected the menu for a forced 'empty cache and hard reload', and Chrome appears to be 'working' also!?
apignotti
apignotti5mo ago
Glad to hear the test now works for you. We will take care of fixing the docs, thanks for reporting this.
Want results from more Discord servers?
Add your server