'WebAssembly Problems with C++ And PThreads

I have written a chess AI and would like to port it to some Webpage. I have been following some guides and simplified my chess engine to only contain plain C++ code. To reproduce my error, I also reduced the problem to a small reproducible example.

Following this and this, I basically wrote a small C++ script:

Compiling

#include <stdio.h>
#include <emscripten/emscripten.h>

int main() {
    printf("Hello World\n");
}

#ifdef __cplusplus
extern "C" {
#endif

EMSCRIPTEN_KEEPALIVE void myFunction(int argc, char ** argv) {
    printf("MyFunction Called\n");
}

#ifdef __cplusplus
}
#endif

And compiled it like this:

emcc -o test.html test.cpp --shell-file html_template/shell_minimal.html -s NO_EXIT_RUNTIME=1 -s "EXPORTED_RUNTIME_METHODS=['ccall']" -std=c++17 -O3 -pthread -s PROXY_TO_PTHREAD

Since my AI makes use of PThreads, I enabled the flags for the simplified problem above. Disabling pthread will run without problems.


Actual Problem

I did setup a local python server to load and run the produced html file.

python3 -m http.server 8000

I went into my Browser (latest FireFox) and opened localhost:8000. After clicking test.html I ended up with the following error:

enter image description here

I think this problem has to do with the pthread-library but I am not sure how I could solve this. I am very happy for any idea on how to solve this problem. Also this is the first issue that came up. If you know more things which I need to take care of, please recommend me some ressources which I could read upon :)

EDIT 1

I did do more research and found this, this and this. which stated I would need my server to activate certain things which resulted in my server script looking like this:

#!/usr/bin/env python3
from http.server import HTTPServer, SimpleHTTPRequestHandler, test
import sys

class CORSRequestHandler (SimpleHTTPRequestHandler):
    def end_headers (self):
        self.send_header('Access-Control-Allow-Origin', 'same-origin')
        self.send_header('Cross-Origin-Embedder-Policy', 'require-corp')
        SimpleHTTPRequestHandler.end_headers(self)

if __name__ == '__main__':
    test(CORSRequestHandler, HTTPServer, port=int(sys.argv[1]) if len(sys.argv) > 1 else 8000)

Sadly I still see the same issue.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source