Skip to content

Remove undocumented Module['ctx'] property - #28079

Open
sbc100 wants to merge 1 commit into
emscripten-core:mainfrom
sbc100:remove_module_ctx
Open

sbc100 wants to merge 1 commit into
emscripten-core:mainfrom
sbc100:remove_module_ctx

Conversation

@sbc100

@sbc100 sbc100 commented Oct 26, 2026

Copy link
Copy Markdown
Collaborator

Module['ctx'] was an undocumented legacy property populated by WebGL context activation (libwebgl.js, libhtml5_webgl.js) and main-canvas context creation (Browser.createContext in libbrowser.js). Because GL.makeContextCurrent unconditionally assigned to the quoted Module['ctx'] property, Closure Compiler could not dead-strip it, adding a few bytes to every WebGL build.

  • Stop assigning Module['ctx'] in libwebgl.js and libhtml5_webgl.js.
  • Store the main-canvas context on internal Browser.ctx instead of Module['ctx'] across libbrowser.js, libglfw.js, and libglut.js.
  • Update the ASSERTIONS-mode EM_TIMING_SETTIMEOUT warning in libeventloop.js to check GLctx and Browser.ctx.
  • Drop the dead Module['ctx'] fallback in src/cpuprofiler.js (left over from --proxy-to-worker, removed in Remove --proxy-to-worker/-sPROXY_TO_WORKER #25645).

`Module['ctx']` was an undocumented legacy property populated by WebGL
context activation (`libwebgl.js`, `libhtml5_webgl.js`) and main-canvas
context creation (`Browser.createContext` in `libbrowser.js`). Because
`GL.makeContextCurrent` unconditionally assigned to the quoted
`Module['ctx']` property, Closure Compiler could not dead-strip it,
adding a few bytes to every WebGL build.

- Stop assigning `Module['ctx']` in `libwebgl.js` and
  `libhtml5_webgl.js`.
- Store the main-canvas context on internal `Browser.ctx` instead of
  `Module['ctx']` across `libbrowser.js`, `libglfw.js`, and
  `libglut.js`.
- Update the `ASSERTIONS`-mode `EM_TIMING_SETTIMEOUT` warning in
  `libeventloop.js` to check `GLctx` and `Browser.ctx`.
- Drop the dead `Module['ctx']` fallback in `src/cpuprofiler.js` (left
  over from `--proxy-to-worker`, removed in emscripten-core#25645).

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request removes the undocumented Module['ctx'] property, replacing its usage with Browser.ctx or GLctx across various library files, and updates the changelog and test code sizes. A review comment points out a potential issue in src/lib/libbrowser.js where a cached 2D context could be incorrectly returned instead of creating a WebGL context, and suggests checking Browser.useWebGL to prevent this.

Comment thread src/lib/libbrowser.js

createContext(/** @type {HTMLCanvasElement} */ canvas, useWebGL, setInModule, webGLContextAttributes) {
if (useWebGL && Module['ctx'] && canvas == Browser.getCanvas()) return Module['ctx']; // no need to recreate GL context if it's already been created for this canvas.
if (useWebGL && Browser.ctx && canvas == Browser.getCanvas()) return Browser.ctx; // no need to recreate GL context if it's already been created for this canvas.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

If a 2D context was previously created on the canvas, Browser.ctx will be populated, but Browser.useWebGL will be false. If createContext is subsequently called with useWebGL = true on the same canvas, the current check will incorrectly return the 2D context instead of attempting to create a WebGL context. Checking Browser.useWebGL ensures that we only return the cached context if it matches the requested WebGL state.

      if (useWebGL && Browser.useWebGL && Browser.ctx && canvas == Browser.getCanvas()) return Browser.ctx; // no need to recreate GL context if it's already been created for this canvas.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant