servo/ports/geckolib/string_cache/regen_atom_macro.py
author Bobby Holley <bobbyholley@gmail.com>
Wed, 18 May 2016 11:35:28 -0700
changeset 338838 ad573a1c9815333d750d231e8223ddd8f72c9ca8
child 339109 48b6d1049f449393c16e8627cf623faba317c822
permissions -rwxr-xr-x
servo: Merge #11242 - Add basic support for Gecko atoms (from bholley:gecko_atoms); r=emilio This is a rough initial implementation of gecko atoms. This allows us to get rid of the custom rust-selectors branch we use to build stylo. The gecko changes are at https://bugzilla.mozilla.org/show_bug.cgi?id=1273771 Source-Repo: https://github.com/servo/servo Source-Revision: e3be7184fb79d9d0b7b2a491769a58c59a990460

# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

import re
import sys

if len(sys.argv) != 2:
    print "usage: ./%s PATH/TO/OBJDIR" % sys.argv[0]
objdir_path = sys.argv[1]


def line_to_atom(line):
    result = re.match('^GK_ATOM\((.+),\s*"(.*)"\)', line)
    return (result.group(1), result.group(2))


def symbolify(ident):
    return "_ZN9nsGkAtoms" + str(len(ident)) + ident + "E"


with open(objdir_path + "/dist/include/nsGkAtomList.h") as f:
    lines = [line for line in f.readlines() if line.startswith("GK_ATOM")]
    atoms = [line_to_atom(line) for line in lines]

with open("atom_macro.rs", "w") as f:
    f.write("use gecko_bindings::bindings::nsIAtom;\n\n")
    f.write("use Atom;\n\n")
    f.write("pub fn unsafe_atom_from_static(ptr: *mut nsIAtom) -> Atom { unsafe { Atom::from_static(ptr) } }\n\n")
    for atom in atoms:
        f.write('extern { pub static %s: *mut nsIAtom; }\n' % symbolify(atom[0]))
    f.write("#[macro_export]\n")
    f.write("macro_rules! atom {\n")
    f.writelines(['("%s") => { $crate::atom_macro::unsafe_atom_from_static($crate::atom_macro::%s) };\n'
                 % (atom[1], symbolify(atom[0])) for atom in atoms])
    f.write("}\n")