felixhao28 / JSCPP

A simple C++ interpreter written in JavaScript
https://felixhao28.github.io/JSCPP/
MIT License
861 stars 72 forks source link

WebWorker compatibility (for browser multi-threading) #121

Open BruceKnowsHow opened 3 years ago

BruceKnowsHow commented 3 years ago

The JSCPP distribution has exactly one error when being loaded into a WebWorker via importScripts(). That error has to do with the way window.JSCPP is assigned in index.js. WebWorkers don't have access to the window object. I was able to fix it in the context of my project by doing the following replacement.

Before: window.JSCPP = require('./launcher').default; After: var JSCPP = require('./launcher').default;

This completely fixes the issue and I'm able to run C++ via a worker. However, I am not familiar enough with NodeJS to determine whether this is a reasonable solution. Just wanted to bring this to your attention.

Thank you for this project, it has been extremely helpful so far.

felixhao28 commented 3 years ago

Thanks for pointing it out. I just added two helper classes to make running JSCPP in WebWorkers easier:

https://github.com/felixhao28/JSCPP/tree/master#running-in-webworker

It should be helpful for you. Debugging in WebWorker is supported too. You can find example code in https://github.com/felixhao28/JSCPP/blob/master/dist/index.html.