Module["teDisposeRuntime"] = function () {
  const canvas = Module["canvas"];
  try {
    if (Module["_TE_Destroy"]) Module["_TE_Destroy"]();
  } catch (_) {}
  try {
    if (typeof JSEvents !== "undefined") JSEvents.removeAllEventListeners();
  } catch (_) {}
  try {
    const sdl = Module["SDL2"];
    if (sdl && sdl.audioContext && sdl.audioContext.state !== "closed") {
      sdl.audioContext.close();
    }
    Module["SDL2"] = null;
  } catch (_) {}
  try {
    if (typeof GL !== "undefined" && GL.contexts) {
      for (let handle = 1; handle < GL.contexts.length; handle += 1) {
        const context = GL.contexts[handle];
        if (!context) continue;
        try {
          const extension = context.GLctx && context.GLctx.getExtension("WEBGL_lose_context");
          if (extension) extension.loseContext();
        } catch (_) {}
        GL.deleteContext(handle);
      }
    }
    Module["ctx"] = null;
  } catch (_) {}
  try {
    if (canvas && canvas.getContextSafariWebGL2Fixed) {
      canvas.getContext = canvas.getContextSafariWebGL2Fixed;
      delete canvas.getContextSafariWebGL2Fixed;
    }
    if (canvas) delete canvas.GLctxObject;
  } catch (_) {}
  Module["canvas"] = null;
  Module["wasmBinary"] = null;
  Module["onSaveComplete"] = null;
  Module["onLevelComplete"] = null;
  Module["onResumeMetadata"] = null;
  Module["onRuntimeFailure"] = null;
  Module["FS"] = null;
  Module["callMain"] = null;
  Module["ccall"] = null;
  for (const key of Object.keys(Module)) {
    if (key.startsWith("_")) Module[key] = null;
  }
  Module["teDisposeRuntime"] = null;
};
