Merge pull request #678 from derekdreery/webidl_namespace_support

Add support webidl namespaces.
This commit is contained in:
R. Andrew Ohana
2018-08-12 17:41:54 -07:00
committed by GitHub
14 changed files with 509 additions and 55 deletions

View File

@ -1,3 +1,4 @@
use std::collections::BTreeMap;
use proc_macro2::{Ident, Span};
use shared;
use syn;
@ -19,6 +20,8 @@ pub struct Program {
pub structs: Vec<Struct>,
/// rust consts
pub consts: Vec<Const>,
/// rust submodules
pub modules: BTreeMap<Ident, Module>,
}
/// A rust to js interface. Allows interaction with rust objects/functions
@ -220,6 +223,16 @@ pub enum ConstValue {
Null,
}
/// A rust module
///
/// This exists to give the ability to namespace js imports.
#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq, Eq))]
pub struct Module {
pub vis: syn::Visibility,
/// js -> rust interfaces
pub imports: Vec<Import>,
}
impl Program {
pub(crate) fn shared(&self) -> Result<shared::Program, Diagnostic> {
Ok(shared::Program {
@ -227,6 +240,8 @@ impl Program {
structs: self.structs.iter().map(|a| a.shared()).collect(),
enums: self.enums.iter().map(|a| a.shared()).collect(),
imports: self.imports.iter()
// add in imports from inside modules
.chain(self.modules.values().flat_map(|m| m.imports.iter()))
.map(|a| a.shared())
.collect::<Result<_, Diagnostic>>()?,
version: shared::version(),

View File

@ -43,6 +43,8 @@ impl TryToTokens for ast::Program {
for i in self.imports.iter() {
DescribeImport(&i.kind).to_tokens(tokens);
// If there is a js namespace, check that name isn't a type. If it is,
// this import might be a method on that type.
if let Some(ns) = &i.js_namespace {
if types.contains(ns) && i.kind.fits_on_impl() {
let kind = match i.kind.try_to_token_stream() {
@ -61,6 +63,11 @@ impl TryToTokens for ast::Program {
errors.push(e);
}
}
for m in self.modules.iter() {
if let Err(e) = ModuleInIter::from(m).try_to_tokens(tokens) {
errors.push(e);
}
}
for e in self.enums.iter() {
e.to_tokens(tokens);
}
@ -87,6 +94,7 @@ impl TryToTokens for ast::Program {
// Each JSON blob is prepended with the length of the JSON blob so when
// all these sections are concatenated in the final wasm file we know
// how to extract all the JSON pieces, so insert the byte length here.
// The value is little-endian.
let generated_static_length = description.len() + 4;
let mut bytes = vec![
(description.len() >> 0) as u8,
@ -1103,6 +1111,43 @@ impl ToTokens for ast::Const {
}
}
/// Struct to help implementing TryToTokens over the key/value pairs from the hashmap.
struct ModuleInIter<'a> {
name: &'a Ident,
module: &'a ast::Module
}
impl<'a> From<(&'a Ident, &'a ast::Module)> for ModuleInIter<'a> {
fn from((name, module): (&'a Ident, &'a ast::Module)) -> ModuleInIter<'a> {
ModuleInIter { name, module }
}
}
impl<'a> TryToTokens for ModuleInIter<'a> {
fn try_to_tokens(&self, tokens: &mut TokenStream) -> Result<(), Diagnostic> {
let name = &self.name;
let imports = &self.module.imports;
let mut errors = Vec::new();
for i in imports.iter() {
DescribeImport(&i.kind).to_tokens(tokens);
}
let vis = &self.module.vis;
let mut body = TokenStream::new();
for i in imports.iter() {
if let Err(e) = i.kind.try_to_tokens(&mut body) {
errors.push(e);
}
}
Diagnostic::from_vec(errors)?;
(quote!{
#vis mod #name {
#body
}
}).to_tokens(tokens);
Ok(())
}
}
/// Emits the necessary glue tokens for "descriptor", generating an appropriate
/// symbol name as well as attributes around the descriptor function itself.
struct Descriptor<'a, T>(&'a Ident, T);