Running Emscripten under Electron in the 'node' environment

38 views
Skip to first unread message

Eric Mandel

unread,
May 6, 2019, 2:17:46 PM5/6/19
to emscripten-discuss
The desktop version of JS9 astronomical image display uses Electron to load an Emscripten-enabled web page. This works great ... I'd now like to use NODEFS to mount the host file system root into MEMFS, so that we can access our large local data files without first having to retrieve and store them in the MEMFS heap. I can easily enable nodeIntegration on the JS9 window, but a call to mount NODEFS fails mount's assertion that ENVIRONMENT_IS_NODE, Emscripten having set the environment to "web" (since the window object exists in in this hybrid scenario.)

Is there a way to tell Emscripten at run-time that the environment should be set to "node"? Or that node functionality exists, even though it's a "web" environment (hybrid, actually)? I see a compile-time option to set the environment, but I would much prefer to keep using one set of compiled files for all versions.


Eric Mandel

unread,
May 7, 2019, 4:38:07 PM5/7/19
to emscripten-discuss
In case I was not clear above ... in emscripten/src/shell.js, the following test sets the environment to web or node:

ENVIRONMENT_IS_WEB = typeof window === 'object';
ENVIRONMENT_IS_WORKER = typeof importScripts === 'function';
ENVIRONMENT_IS_NODE = typeof process === 'object' && typeof require === 'function' && !ENVIRONMENT_IS_WEB && !ENVIRONMENT_IS_WORKER;

So once web is detected, node cannot be set. In particular, an Electron web page contains "window" of type "object" and therefore the environment is set to "web", even if node has been enabled within that web page. 

As a result, it is not possible to mount the NODEFS file system, even though node is present to do so. Failure takes place in mount() at the assertion in src/library_nodefs.js:

    mount: function (mount) {
      assert(ENVIRONMENT_IS_NODE);
      return NODEFS.createNode(null, '/', NODEFS.getMode(mount.opts.root), 0);
    },

Is there a reason why once the web environment is detected, it cannot be reset to node, if node features are present?

Eric Mandel

unread,
May 8, 2019, 8:32:32 PM5/8/19
to emscripten-discuss
It seems to me that we have to distinguish between having node in the environment and being in a node environment. The following changes to Emscripten will facilitate this:

in shell.js:

  ENVIRONMENT_HAS_NODE = typeof process === 'object' && typeof require === 'function';
  ENVIRONMENT_IS_NODE = ENVIRONMENT_HAS_NODE && !ENVIRONMENT_IS_WEB && !ENVIRONMENT_IS_WORKER;

and in library_nodefs.js:

    mount: function (mount) {
      assert(ENVIRONMENT_HAS_NODE);
      return NODEFS.createNode(null, '/', NODEFS.getMode(mount.opts.root), 0);
    },

Once I verify that this does exactly what I need, I'll submit a PR.

Reply all
Reply to author
Forward
0 new messages