Repository: brendanzab/gl-rs (language: rust)

Issue:
webgl_generator fails to generate WebGL 1 bindings
using `Api::WebGl` instead of `Api::WebGl2` leads to compilation error:

    thread 'main' panicked at 'WebGLQuery'


Discussion / hints:

Fix patch:
diff --git a/webgl_generator/webgl_generators/stdweb_gen.rs b/webgl_generator/webgl_generators/stdweb_gen.rs
index d89006c4..773f21ea 100644
--- a/webgl_generator/webgl_generators/stdweb_gen.rs
+++ b/webgl_generator/webgl_generators/stdweb_gen.rs
@@ -586,7 +586,7 @@ where
         attrs += &format!("#[reference(instance_of = {:?})]\n", name);
         None
     } else {
-        Some(format!("@{{reference}}.constructor.name == {:?}", name))
+        Some("true".to_owned())
     };
 
     write!(
diff --git a/webgl_generator/webgl_registry/mod.rs b/webgl_generator/webgl_registry/mod.rs
index cc388fe6..9884394f 100644
--- a/webgl_generator/webgl_registry/mod.rs
+++ b/webgl_generator/webgl_registry/mod.rs
@@ -33,7 +33,7 @@ const RENDERING_CONTEXTS: &'static [(&'static str, &'static str)] = &[
     ("webgl2", "WebGL2RenderingContext"),
 ];
 
-#[derive(Copy, Clone, Debug, PartialEq, Eq)]
+#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
 pub enum Api {
     WebGl,
     WebGl2,
@@ -45,6 +45,7 @@ struct ExtensionIDL {
     pub idl: String,
     pub overview: String,
     pub new_funs: BTreeMap<String, String>,
+    pub min_api: Api,
 }
 
 pub enum Exts<'a> {
@@ -80,6 +81,9 @@ impl<'a> Exts<'a> {
                 }
             }
 
+            let dependencies = elem.get_child("depends", None).unwrap();
+            let api_dep = dependencies.get_child("api", None).unwrap();
+
             let mut ext = ExtensionIDL {
                 name: elem.get_child("name", None).unwrap().content_str(),
                 idl: elem.get_child("idl", None).unwrap().content_str(),
@@ -88,6 +92,11 @@ impl<'a> Exts<'a> {
                     convert_html_to_doc_comment(&overview_html)
                 ),
                 new_funs,
+                min_api: match api_dep.get_attribute("version", None).unwrap() {
+                    "1.0" => Api::WebGl,
+                    "2.0" => Api::WebGl2,
+                    other => panic!("Unknown API version: {}", other)
+                }
             };
 
             if match self {
diff --git a/webgl_generator/webgl_registry/registry.rs b/webgl_generator/webgl_registry/registry.rs
index 37089430..5fc94c06 100644
--- a/webgl_generator/webgl_registry/registry.rs
+++ b/webgl_generator/webgl_registry/registry.rs
@@ -67,6 +67,10 @@ impl Registry {
 
         // Next find all requested extensions
         for ext in exts.enumerate() {
+            if api < ext.min_api {
+                continue;
+            }
+
             // Make a note that we included this extension
             result.extensions.insert(ext.name.clone());
 


Test patch:
diff --git a/tests/test_webgl_stdweb/build.rs b/tests/test_webgl_stdweb/build.rs
index c6ae9e0a..714561f0 100644
--- a/tests/test_webgl_stdweb/build.rs
+++ b/tests/test_webgl_stdweb/build.rs
@@ -21,9 +21,14 @@ use std::path::*;
 
 fn main() {
     let dest = env::var("OUT_DIR").unwrap();
-    let mut file = File::create(&Path::new(&dest).join("test_webgl_stdweb.rs")).unwrap();
+    let mut file1 = File::create(&Path::new(&dest).join("test_webgl_stdweb.rs")).unwrap();
+    let mut file2 = File::create(&Path::new(&dest).join("test_webgl2_stdweb.rs")).unwrap();
+
+    Registry::new(Api::WebGl, Exts::ALL)
+        .write_bindings(StdwebGenerator, &mut file1)
+        .unwrap();
 
     Registry::new(Api::WebGl2, Exts::ALL)
-        .write_bindings(StdwebGenerator, &mut file)
+        .write_bindings(StdwebGenerator, &mut file2)
         .unwrap();
 }
diff --git a/tests/test_webgl_stdweb/lib.rs b/tests/test_webgl_stdweb/lib.rs
index aee9e308..77f53a0a 100644
--- a/tests/test_webgl_stdweb/lib.rs
+++ b/tests/test_webgl_stdweb/lib.rs
@@ -15,10 +15,16 @@
 #![allow(unused_parens, non_camel_case_types)]
 
 #[macro_use]
-extern crate serde_derive as _serde_derive;
+extern crate serde_derive;
 #[macro_use]
-extern crate stdweb as _stdweb;
+extern crate stdweb;
 #[macro_use]
-extern crate stdweb_derive as _stdweb_derive;
+extern crate stdweb_derive;
 
-include!(concat!(env!("OUT_DIR"), "/test_webgl_stdweb.rs"));
+mod webgl {
+    include!(concat!(env!("OUT_DIR"), "/test_webgl_stdweb.rs"));
+}
+
+mod webgl2 {
+    include!(concat!(env!("OUT_DIR"), "/test_webgl2_stdweb.rs"));
+}