TypeScript-DOM-lib-generator
TypeScript-DOM-lib-generator copied to clipboard
WebGLRenderingContext function types incorrectly reject null resources
WebGL has a bit of a quirk in that the browser is able to destroy the rendering context at any time. It's not even something which can happen between event handlers; you can call any WebGL function, and the rendering context could be taken away from you before it returns.
When that happens, functions which create resources will return null. This is correctly modelled in TypeScript's types for functions such as createShader, createBuffer, etc.
However, Khronos' guidance specifically states that an application should NOT attempt to check whether created resources are null. In the event of context loss, all existing resources are effectively void and all methods on the WebGL context become no-ops until the browser decides to reinstate the rendering context (at which point an event is raised and the application is expected to recreate all of its resources).
TypeScript's types for a WebGLRenderingContext, however, seem to disallow null in place of a resource in all functions I've come across. For example, gl.shaderSource(null, "some source") is considered a type error.
Given that the only way to avoid a type error is to contradict the guidance of the standards body who invented the standard, this seems like a bug to me.
This was previously raised in https://github.com/microsoft/TypeScript-DOM-lib-generator/pull/399, but only a small handful of functions were fixed. I could raise a PR to do some more if wanted?
I'm not sure how much of the webgl types are generated from upstream WIDLs which might not note the nulls, but I think we'd be open to adding more nulls like #399
#399 has been reverted because upstream IDL has added null to those methods. Now most WebGL APIs are directly generated from IDL (including shaderSource).
https://github.com/w3c/webref/blob/12358e007a8f4d60a302f84dde67a83407df47b7/ed/idl/webgl1.idl#L712-L715
undefined uniform1fv(WebGLUniformLocation? location, Float32List v);
undefined uniform2fv(WebGLUniformLocation? location, Float32List v);
undefined uniform3fv(WebGLUniformLocation? location, Float32List v);
undefined uniform4fv(WebGLUniformLocation? location, Float32List v);
https://github.com/w3c/webref/blob/12358e007a8f4d60a302f84dde67a83407df47b7/ed/idl/webgl1.idl#L642
undefined shaderSource(WebGLShader shader, DOMString source);
gl.shaderSource(null, "some source") is actually a runtime error:

while gl.uniform1f(null,1) is fine.

Maybe you need to file an issue at https://github.com/KhronosGroup/WebGL/issues first.