script codegen: Avoid modifying in-place a generated file.

This commit is contained in:
Simon Sapin 2017-01-19 17:13:35 +01:00
parent 71fb02953c
commit 0b9ff576e0
5 changed files with 29 additions and 34 deletions

1
Cargo.lock generated
View file

@ -2299,6 +2299,7 @@ dependencies = [
"script_traits 0.0.1", "script_traits 0.0.1",
"selectors 0.15.0 (registry+https://github.com/rust-lang/crates.io-index)", "selectors 0.15.0 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 0.8.20 (registry+https://github.com/rust-lang/crates.io-index)", "serde 0.8.20 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_json 0.8.4 (registry+https://github.com/rust-lang/crates.io-index)",
"servo_atoms 0.0.1", "servo_atoms 0.0.1",
"servo_config 0.0.1", "servo_config 0.0.1",
"servo_geometry 0.0.1", "servo_geometry 0.0.1",

View file

@ -18,7 +18,7 @@ debugmozjs = ['js/debugmozjs']
cmake = "0.1" cmake = "0.1"
phf_codegen = "0.7.18" phf_codegen = "0.7.18"
phf_shared = "0.7.18" phf_shared = "0.7.18"
regex = "0.2" serde_json = "0.8"
[target.'cfg(any(target_os = "macos", target_os = "linux", target_os = "windows"))'.dependencies] [target.'cfg(any(target_os = "macos", target_os = "linux", target_os = "windows"))'.dependencies]
tinyfiledialogs = "2.5.9" tinyfiledialogs = "2.5.9"

View file

@ -5,13 +5,13 @@
extern crate cmake; extern crate cmake;
extern crate phf_codegen; extern crate phf_codegen;
extern crate phf_shared; extern crate phf_shared;
extern crate regex; extern crate serde_json;
use regex::Regex; use serde_json::Value;
use std::env; use std::env;
use std::fmt; use std::fmt;
use std::fs::File; use std::fs::File;
use std::io::{Read, Write}; use std::io::Write;
use std::path::PathBuf; use std::path::PathBuf;
use std::time::Instant; use std::time::Instant;
@ -44,27 +44,17 @@ fn main() {
println!("Binding generation completed in {}s", start.elapsed().as_secs()); println!("Binding generation completed in {}s", start.elapsed().as_secs());
convert_phf(); let json = PathBuf::from(env::var("OUT_DIR").unwrap()).join("build").join("InterfaceObjectMapData.json");
} let json: Value = serde_json::from_reader(File::open(&json).unwrap()).unwrap();
fn convert_phf() {
let filename = PathBuf::from(env::var("OUT_DIR").unwrap()).join("InterfaceObjectMap.rs");
let mut source = String::new();
File::open(&filename).unwrap().read_to_string(&mut source).unwrap();
let map_macro = Regex::new("phf_map! \\{([^}]+)\\}").unwrap().captures(&source).unwrap();
let entries_re = Regex::new("b\"([^\"]+)\" => ([^\n]+),\n").unwrap();
let entries = entries_re.captures_iter(&map_macro[1]);
let mut map = phf_codegen::Map::new(); let mut map = phf_codegen::Map::new();
for entry in entries { for (key, value) in json.as_object().unwrap() {
map.entry(Bytes(entry.get(1).unwrap().as_str()), entry.get(2).unwrap().as_str()); map.entry(Bytes(key), value.as_str().unwrap());
} }
let phf = PathBuf::from(env::var("OUT_DIR").unwrap()).join("InterfaceObjectMapPhf.rs");
let mut file = File::create(&filename).unwrap(); let mut phf = File::create(&phf).unwrap();
let map_macro = map_macro.get(0).unwrap(); write!(&mut phf, "pub static MAP: phf::Map<&'static [u8], unsafe fn(*mut JSContext, HandleObject)> = ").unwrap();
file.write_all(source[..map_macro.start()].as_bytes()).unwrap(); map.build(&mut phf).unwrap();
map.build(&mut file).unwrap(); write!(&mut phf, ";\n").unwrap();
file.write_all(source[map_macro.end()..].as_bytes()).unwrap();
} }
#[derive(Eq, PartialEq, Hash)] #[derive(Eq, PartialEq, Hash)]

View file

@ -6815,6 +6815,15 @@ class GlobalGenRoots():
], "\n")), pre="pub flags Globals: u8 {\n", post="\n}") ], "\n")), pre="pub flags Globals: u8 {\n", post="\n}")
globals_ = CGWrapper(CGIndenter(global_flags), pre="bitflags! {\n", post="\n}") globals_ = CGWrapper(CGIndenter(global_flags), pre="bitflags! {\n", post="\n}")
phf = CGGeneric("include!(concat!(env!(\"OUT_DIR\"), \"/InterfaceObjectMapPhf.rs\"));")
return CGList([
CGGeneric(AUTOGENERATED_WARNING_COMMENT),
CGList([imports, globals_, phf], "\n\n")
])
@staticmethod
def InterfaceObjectMapData(config):
pairs = [] pairs = []
for d in config.getDescriptors(hasInterfaceObject=True, isInline=False): for d in config.getDescriptors(hasInterfaceObject=True, isInline=False):
binding = toBindingNamespace(d.name) binding = toBindingNamespace(d.name)
@ -6823,19 +6832,13 @@ class GlobalGenRoots():
pairs.append((ctor.identifier.name, binding, binding)) pairs.append((ctor.identifier.name, binding, binding))
pairs.sort(key=operator.itemgetter(0)) pairs.sort(key=operator.itemgetter(0))
mappings = [ mappings = [
CGGeneric('b"%s" => codegen::Bindings::%s::%s::DefineDOMInterface as unsafe fn(_, _),' % pair) CGGeneric('"%s": "codegen::Bindings::%s::%s::DefineDOMInterface as unsafe fn(_, _)"' % pair)
for pair in pairs for pair in pairs
] ]
mapType = "phf::Map<&'static [u8], unsafe fn(*mut JSContext, HandleObject)>" return CGWrapper(
phf = CGWrapper( CGList(mappings, ",\n"),
CGIndenter(CGList(mappings, "\n")), pre="{\n",
pre="pub static MAP: %s = phf_map! {\n" % mapType, post="\n}\n")
post="\n};\n")
return CGList([
CGGeneric(AUTOGENERATED_WARNING_COMMENT),
CGList([imports, globals_, phf], "\n\n")
])
@staticmethod @staticmethod
def PrototypeList(config): def PrototypeList(config):

View file

@ -76,6 +76,7 @@ def main():
('PrototypeList', 'PrototypeList.rs'), ('PrototypeList', 'PrototypeList.rs'),
('RegisterBindings', 'RegisterBindings.rs'), ('RegisterBindings', 'RegisterBindings.rs'),
('InterfaceObjectMap', 'InterfaceObjectMap.rs'), ('InterfaceObjectMap', 'InterfaceObjectMap.rs'),
('InterfaceObjectMapData', 'InterfaceObjectMapData.json'),
('InterfaceTypes', 'InterfaceTypes.rs'), ('InterfaceTypes', 'InterfaceTypes.rs'),
('InheritTypes', 'InheritTypes.rs'), ('InheritTypes', 'InheritTypes.rs'),
('Bindings', os.path.join('Bindings', 'mod.rs')), ('Bindings', os.path.join('Bindings', 'mod.rs')),