felixhao28 / JSCPP

A simple C++ interpreter written in JavaScript

Home Page:https://felixhao28.github.io/JSCPP/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

WebWorker compatibility (for browser multi-threading)

BruceKnowsHow opened this issue · comments

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.

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.