LibWeb/WebGL: Make getExtension spec-compliant

Namely:
- Perform case-insensitive matching
- Return the same extension objects every time
- Only enable the extension if it's supported by the current context
This commit is contained in:
Luke Wilde 2025-01-10 13:39:53 +00:00 committed by Andreas Kling
parent 442f0b9a13
commit cfbd125e57
Notes: github-actions[bot] 2025-01-21 20:37:52 +00:00
3 changed files with 39 additions and 4 deletions

View file

@ -841,6 +841,8 @@ struct OscillatorOptions;
}
namespace Web::WebGL {
class ANGLEInstancedArrays;
class OESVertexArrayObject;
class OpenGLContext;
class WebGL2RenderingContext;
class WebGLActiveInfo;
@ -858,6 +860,7 @@ class WebGLSync;
class WebGLTexture;
class WebGLUniformLocation;
class WebGLVertexArrayObject;
class WebGLVertexArrayObjectOES;
}
namespace Web::WebIDL {

View file

@ -11,6 +11,7 @@
#include <LibWeb/Bindings/WebGLRenderingContextPrototype.h>
#include <LibWeb/HTML/HTMLCanvasElement.h>
#include <LibWeb/HTML/TraversableNavigable.h>
#include <LibWeb/Infra/Strings.h>
#include <LibWeb/Painting/Paintable.h>
#include <LibWeb/WebGL/ANGLEInstancedArrays.h>
#include <LibWeb/WebGL/EventNames.h>
@ -89,6 +90,8 @@ void WebGLRenderingContext::visit_edges(Cell::Visitor& visitor)
Base::visit_edges(visitor);
WebGLRenderingContextImpl::visit_edges(visitor);
visitor.visit(m_canvas_element);
visitor.visit(m_angle_instanced_arrays_extension);
visitor.visit(m_oes_vertex_array_object_extension);
}
void WebGLRenderingContext::present()
@ -174,12 +177,36 @@ Optional<Vector<String>> WebGLRenderingContext::get_supported_extensions()
JS::Object* WebGLRenderingContext::get_extension(String const& name)
{
if (name == "ANGLE_instanced_arrays"sv) {
return MUST(ANGLEInstancedArrays::create(realm()));
// Returns an object if, and only if, name is an ASCII case-insensitive match [HTML] for one of the names returned
// from getSupportedExtensions; otherwise, returns null. The object returned from getExtension contains any constants
// or functions provided by the extension. A returned object may have no constants or functions if the extension does
// not define any, but a unique object must still be returned. That object is used to indicate that the extension has
// been enabled.
auto supported_extensions = get_supported_extensions();
auto supported_extension_iterator = supported_extensions->find_if([&name](String const& supported_extension) {
return Infra::is_ascii_case_insensitive_match(supported_extension, name);
});
if (supported_extension_iterator == supported_extensions->end())
return nullptr;
if (Infra::is_ascii_case_insensitive_match(name, "ANGLE_instanced_arrays"sv)) {
if (!m_angle_instanced_arrays_extension) {
m_angle_instanced_arrays_extension = MUST(ANGLEInstancedArrays::create(realm(), *this));
}
VERIFY(m_angle_instanced_arrays_extension);
return m_angle_instanced_arrays_extension;
}
if (name == "OES_vertex_array_object"sv) {
return MUST(OESVertexArrayObject::create(realm(), *this));
if (Infra::is_ascii_case_insensitive_match(name, "OES_vertex_array_object"sv)) {
if (!m_oes_vertex_array_object_extension) {
m_oes_vertex_array_object_extension = MUST(OESVertexArrayObject::create(realm(), *this));
}
VERIFY(m_oes_vertex_array_object_extension);
return m_oes_vertex_array_object_extension;
}
return nullptr;
}

View file

@ -79,6 +79,11 @@ private:
GLenum m_error { 0 };
// Extensions
// "Multiple calls to getExtension with the same extension string, taking into account case-insensitive comparison, must return the same object as long as the extension is enabled."
GC::Ptr<ANGLEInstancedArrays> m_angle_instanced_arrays_extension;
GC::Ptr<OESVertexArrayObject> m_oes_vertex_array_object_extension;
virtual void set_error(GLenum error) override;
};