Skip to content

Commit 0c908bb

Browse files
committed
Merge remote-tracking branch 'origin/master' into webidl_partial_mixins
2 parents d5fee8d + 696678b commit 0c908bb

File tree

24 files changed

+733
-41
lines changed

24 files changed

+733
-41
lines changed

crates/backend/src/ast.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ pub enum ImportKind {
4242
Static(ImportStatic),
4343
Type(ImportType),
4444
Enum(ImportEnum),
45+
Const(Const),
4546
}
4647

4748
#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq, Eq))]
@@ -174,6 +175,24 @@ pub struct TypeAlias {
174175
pub src: syn::Type,
175176
}
176177

178+
#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq, Eq))]
179+
pub struct Const {
180+
pub vis: syn::Visibility,
181+
pub name: Ident,
182+
pub interface_name: Ident,
183+
pub ty: syn::Type,
184+
pub value: ConstValue,
185+
}
186+
187+
#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq, Eq))]
188+
/// same as webidl::ast::ConstValue
189+
pub enum ConstValue {
190+
BooleanLiteral(bool),
191+
FloatLiteral(f64),
192+
IntegerLiteral(i64),
193+
Null,
194+
}
195+
177196
impl Program {
178197
pub(crate) fn shared(&self) -> shared::Program {
179198
shared::Program {
@@ -293,6 +312,7 @@ impl ImportKind {
293312
ImportKind::Static(_) => false,
294313
ImportKind::Type(_) => false,
295314
ImportKind::Enum(_) => false,
315+
ImportKind::Const(_) => false,
296316
}
297317
}
298318

@@ -302,6 +322,7 @@ impl ImportKind {
302322
ImportKind::Static(ref f) => shared::ImportKind::Static(f.shared()),
303323
ImportKind::Type(ref f) => shared::ImportKind::Type(f.shared()),
304324
ImportKind::Enum(ref f) => shared::ImportKind::Enum(f.shared()),
325+
ImportKind::Const(ref f) => shared::ImportKind::Const(f.shared()),
305326
}
306327
}
307328
}
@@ -404,3 +425,9 @@ impl StructField {
404425
}
405426
}
406427
}
428+
429+
impl Const {
430+
fn shared(&self) -> shared::Const {
431+
shared::Const {}
432+
}
433+
}

crates/backend/src/codegen.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,7 @@ impl ToTokens for ast::ImportKind {
501501
ast::ImportKind::Static(ref s) => s.to_tokens(tokens),
502502
ast::ImportKind::Type(ref t) => t.to_tokens(tokens),
503503
ast::ImportKind::Enum(ref e) => e.to_tokens(tokens),
504+
ast::ImportKind::Const(ref c) => c.to_tokens(tokens),
504505
}
505506
}
506507
}
@@ -842,6 +843,7 @@ impl<'a> ToTokens for DescribeImport<'a> {
842843
ast::ImportKind::Static(_) => return,
843844
ast::ImportKind::Type(_) => return,
844845
ast::ImportKind::Enum(_) => return,
846+
ast::ImportKind::Const(_) => return,
845847
};
846848
let describe_name = format!("__wbindgen_describe_{}", f.shim);
847849
let describe_name = Ident::new(&describe_name, Span::call_site());
@@ -958,3 +960,41 @@ impl ToTokens for ast::TypeAlias {
958960
}).to_tokens(into);
959961
}
960962
}
963+
964+
impl ToTokens for ast::Const {
965+
fn to_tokens(&self, tokens: &mut TokenStream) {
966+
use ast::ConstValue::*;
967+
968+
let vis = &self.vis;
969+
let name = &self.name;
970+
let interface_name = &self.interface_name;
971+
let ty = &self.ty;
972+
973+
let value: TokenStream = match self.value {
974+
BooleanLiteral(false) => quote!(false),
975+
BooleanLiteral(true) => quote!(true),
976+
// the actual type is unknown because of typedefs
977+
// so we cannot use std::fxx::INFINITY
978+
// but we can use type inference
979+
FloatLiteral(f) if f.is_infinite() && f.is_sign_positive() => quote!(1.0 / 0.0),
980+
FloatLiteral(f) if f.is_infinite() && f.is_sign_negative() => quote!(-1.0 / 0.0),
981+
FloatLiteral(f) if f.is_nan() => quote!(0.0 / 0.0),
982+
// again no suffix
983+
// panics on +-inf, nan
984+
FloatLiteral(f) => {
985+
let f = Literal::f64_unsuffixed(f);
986+
quote!(#f)
987+
},
988+
IntegerLiteral(i) => {
989+
let i = Literal::i64_unsuffixed(i);
990+
quote!(#i)
991+
},
992+
Null => unimplemented!(),
993+
};
994+
(quote! {
995+
impl #interface_name {
996+
#vis const #name: #ty = #value;
997+
}
998+
}).to_tokens(tokens);
999+
}
1000+
}

crates/backend/src/defined.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ impl ImportedTypes for ast::ImportKind {
106106
ast::ImportKind::Function(fun) => fun.imported_types(f),
107107
ast::ImportKind::Type(ty) => ty.imported_types(f),
108108
ast::ImportKind::Enum(enm) => enm.imported_types(f),
109+
ast::ImportKind::Const(c) => c.imported_types(f),
109110
}
110111
}
111112
}
@@ -229,6 +230,15 @@ impl ImportedTypes for ast::TypeAlias {
229230
}
230231
}
231232

233+
impl ImportedTypes for ast::Const {
234+
fn imported_types<F>(&self, f: &mut F)
235+
where
236+
F: FnMut(&Ident, ImportedTypeKind),
237+
{
238+
self.ty.imported_types(f);
239+
}
240+
}
241+
232242
/// Remove any methods, statics, &c, that reference types that are *not*
233243
/// defined.
234244
pub trait RemoveUndefinedImports {

crates/cli-support/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@ serde_derive = "1.0"
1919
serde_json = "1.0"
2020
tempfile = "3.0"
2121
wasm-bindgen-shared = { path = "../shared", version = '=0.2.11' }
22-
wasm-gc-api = "0.1"
22+
wasm-gc-api = "0.1.8"
2323
wasmi = "0.3"

crates/cli-support/src/js/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1580,6 +1580,7 @@ impl<'a> Context<'a> {
15801580
let wasm_bytes = parity_wasm::serialize(module)?;
15811581
let bytes = wasm_gc::Config::new()
15821582
.demangle(self.config.demangle)
1583+
.keep_debug(self.config.keep_debug || self.config.debug)
15831584
.gc(&wasm_bytes)?;
15841585
*self.module = deserialize_buffer(&bytes)?;
15851586
Ok(())
@@ -1757,6 +1758,7 @@ impl<'a, 'b> SubContext<'a, 'b> {
17571758
}
17581759
shared::ImportKind::Type(_) => {}
17591760
shared::ImportKind::Enum(_) => {}
1761+
shared::ImportKind::Const(_) => {}
17601762
}
17611763
Ok(())
17621764
}

crates/cli-support/src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ pub struct Bindgen {
3131
debug: bool,
3232
typescript: bool,
3333
demangle: bool,
34+
keep_debug: bool,
3435
}
3536

3637
impl Bindgen {
@@ -45,6 +46,7 @@ impl Bindgen {
4546
debug: false,
4647
typescript: false,
4748
demangle: true,
49+
keep_debug: false,
4850
}
4951
}
5052

@@ -93,6 +95,11 @@ impl Bindgen {
9395
self
9496
}
9597

98+
pub fn keep_debug(&mut self, keep_debug: bool) -> &mut Bindgen {
99+
self.keep_debug = keep_debug;
100+
self
101+
}
102+
96103
pub fn generate<P: AsRef<Path>>(&mut self, path: P) -> Result<(), Error> {
97104
self._generate(path.as_ref())
98105
}

crates/cli/src/bin/wasm-bindgen.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ Options:
3232
--no-typescript Don't emit a *.d.ts file
3333
--debug Include otherwise-extraneous debug checks in output
3434
--no-demangle Don't demangle Rust symbol names
35+
--keep-debug Keep debug sections in wasm files
3536
-V --version Print the version number of wasm-bindgen
3637
";
3738

@@ -47,6 +48,7 @@ struct Args {
4748
flag_version: bool,
4849
flag_no_demangle: bool,
4950
flag_no_modules_global: Option<String>,
51+
flag_keep_debug: bool,
5052
arg_input: Option<PathBuf>,
5153
}
5254

@@ -85,6 +87,7 @@ fn rmain(args: &Args) -> Result<(), Error> {
8587
.no_modules(args.flag_no_modules)
8688
.debug(args.flag_debug)
8789
.demangle(!args.flag_no_demangle)
90+
.keep_debug(args.flag_keep_debug)
8891
.typescript(typescript);
8992
if let Some(ref name) = args.flag_no_modules_global {
9093
b.no_modules_global(name);

crates/shared/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ pub enum ImportKind {
3434
Static(ImportStatic),
3535
Type(ImportType),
3636
Enum(ImportEnum),
37+
Const(Const)
3738
}
3839

3940
#[derive(Deserialize, Serialize)]
@@ -124,6 +125,9 @@ pub struct StructField {
124125
pub comments: Vec<String>,
125126
}
126127

128+
#[derive(Deserialize, Serialize)]
129+
pub struct Const {}
130+
127131
pub fn new_function(struct_name: &str) -> String {
128132
let mut name = format!("__wbg_");
129133
name.extend(struct_name.chars().flat_map(|s| s.to_lowercase()));

crates/web-sys/tests/all/headers.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
use super::websys_project;
2+
3+
#[test]
4+
fn headers() {
5+
websys_project()
6+
.file(
7+
"src/lib.rs",
8+
r#"
9+
#![feature(proc_macro, wasm_custom_section)]
10+
extern crate wasm_bindgen;
11+
use wasm_bindgen::prelude::*;
12+
extern crate web_sys;
13+
14+
#[wasm_bindgen]
15+
pub fn test_headers(_headers: &web_sys::Headers) {
16+
// empty for now...
17+
}
18+
"#,
19+
)
20+
.file(
21+
"test.js",
22+
r#"
23+
import * as assert from "assert";
24+
import * as wasm from "./out";
25+
26+
export function test() {
27+
let headers = new Headers({'Content-Type': 'text/plain'});
28+
wasm.test_headers(headers);
29+
}
30+
"#,
31+
)
32+
.test();
33+
}

crates/web-sys/tests/all/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ extern crate wasm_bindgen_test_project_builder as project_builder;
22
use project_builder::{project, Project};
33

44
mod event;
5+
mod headers;
56
mod response;
67

78
fn websys_project() -> Project {

crates/webidl/src/lib.rs

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ use std::path::Path;
3030
use backend::defined::{ImportedTypeDefinitions, RemoveUndefinedImports};
3131
use backend::util::{ident_ty, rust_ident, wrap_import_function};
3232
use failure::ResultExt;
33-
use heck::CamelCase;
33+
use heck::{CamelCase, ShoutySnakeCase};
3434
use quote::ToTokens;
3535

3636
use first_pass::{FirstPass, FirstPassRecord};
37-
use util::{public, TypePosition};
37+
use util::{public, webidl_const_ty_to_syn_ty, webidl_const_v_to_backend_const_v, TypePosition};
3838

3939
/// Either `Ok(t)` or `Err(failure::Error)`.
4040
pub type Result<T> = ::std::result::Result<T, failure::Error>;
@@ -250,7 +250,7 @@ impl WebidlParse<()> for webidl::ast::NonPartialInterface {
250250
js_namespace: None,
251251
kind: backend::ast::ImportKind::Type(backend::ast::ImportType {
252252
vis: public(),
253-
name: rust_ident(&self.name),
253+
name: rust_ident(self.name.to_camel_case().as_str()),
254254
attrs: Vec::new(),
255255
}),
256256
});
@@ -343,7 +343,8 @@ impl<'a> WebidlParse<&'a webidl::ast::NonPartialInterface> for webidl::ast::Exte
343343
match self {
344344
webidl::ast::ExtendedAttribute::ArgumentList(
345345
webidl::ast::ArgumentListExtendedAttribute { arguments, name },
346-
) if name == "Constructor" =>
346+
)
347+
if name == "Constructor" =>
347348
{
348349
add_constructor(arguments, &interface.name)
349350
}
@@ -358,7 +359,8 @@ impl<'a> WebidlParse<&'a webidl::ast::NonPartialInterface> for webidl::ast::Exte
358359
rhs_arguments,
359360
rhs_name,
360361
},
361-
) if lhs_name == "NamedConstructor" =>
362+
)
363+
if lhs_name == "NamedConstructor" =>
362364
{
363365
add_constructor(rhs_arguments, rhs_name)
364366
}
@@ -389,9 +391,11 @@ impl<'a> WebidlParse<&'a str> for webidl::ast::InterfaceMember {
389391
webidl::ast::InterfaceMember::Operation(op) => {
390392
op.webidl_parse(program, first_pass, self_name)
391393
}
394+
webidl::ast::InterfaceMember::Const(cnst) => {
395+
cnst.webidl_parse(program, first_pass, self_name)
396+
}
392397
// TODO
393-
webidl::ast::InterfaceMember::Const(_)
394-
| webidl::ast::InterfaceMember::Iterable(_)
398+
webidl::ast::InterfaceMember::Iterable(_)
395399
| webidl::ast::InterfaceMember::Maplike(_)
396400
| webidl::ast::InterfaceMember::Setlike(_) => {
397401
warn!("Unsupported WebIDL interface member: {:?}", self);
@@ -637,3 +641,29 @@ impl<'a> WebidlParse<()> for webidl::ast::Enum {
637641
Ok(())
638642
}
639643
}
644+
645+
impl<'a> WebidlParse<&'a str> for webidl::ast::Const {
646+
fn webidl_parse(
647+
&self,
648+
program: &mut backend::ast::Program,
649+
_: &FirstPassRecord<'_>,
650+
interface_name: &'a str,
651+
) -> Result<()> {
652+
let syn_ty = webidl_const_ty_to_syn_ty(&self.type_);
653+
program.imports.push(backend::ast::Import {
654+
module: None,
655+
version: None,
656+
js_namespace: None,
657+
kind: backend::ast::ImportKind::Const(backend::ast::Const {
658+
vis: syn::Visibility::Public(syn::VisPublic {
659+
pub_token: Default::default(),
660+
}),
661+
name: rust_ident(self.name.to_shouty_snake_case().as_str()),
662+
interface_name: rust_ident(interface_name.to_camel_case().as_str()),
663+
ty: syn_ty,
664+
value: webidl_const_v_to_backend_const_v(&self.value),
665+
}),
666+
});
667+
Ok(())
668+
}
669+
}

crates/webidl/src/util.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,35 @@ fn shared_ref(ty: syn::Type) -> syn::Type {
1919
}.into()
2020
}
2121

22+
pub fn webidl_const_ty_to_syn_ty(ty: &webidl::ast::ConstType) -> syn::Type {
23+
use webidl::ast::ConstType::*;
24+
25+
// similar to webidl_ty_to_syn_ty
26+
match ty {
27+
Boolean => ident_ty(raw_ident("bool")),
28+
Byte => ident_ty(raw_ident("i8")),
29+
Octet => ident_ty(raw_ident("u8")),
30+
RestrictedDouble | UnrestrictedDouble => ident_ty(raw_ident("f64")),
31+
RestrictedFloat | UnrestrictedFloat => ident_ty(raw_ident("f32")),
32+
SignedLong => ident_ty(raw_ident("i32")),
33+
SignedLongLong => ident_ty(raw_ident("i64")),
34+
SignedShort => ident_ty(raw_ident("i16")),
35+
UnsignedLong => ident_ty(raw_ident("u32")),
36+
UnsignedLongLong => ident_ty(raw_ident("u64")),
37+
UnsignedShort => ident_ty(raw_ident("u16")),
38+
Identifier(ref id) => ident_ty(rust_ident(id)),
39+
}
40+
}
41+
42+
pub fn webidl_const_v_to_backend_const_v(v: &webidl::ast::ConstValue) -> backend::ast::ConstValue {
43+
match *v {
44+
webidl::ast::ConstValue::BooleanLiteral(b) => backend::ast::ConstValue::BooleanLiteral(b),
45+
webidl::ast::ConstValue::FloatLiteral(f) => backend::ast::ConstValue::FloatLiteral(f),
46+
webidl::ast::ConstValue::IntegerLiteral(i) => backend::ast::ConstValue::IntegerLiteral(i),
47+
webidl::ast::ConstValue::Null => backend::ast::ConstValue::Null,
48+
}
49+
}
50+
2251
fn simple_fn_arg(ident: Ident, ty: syn::Type) -> syn::ArgCaptured {
2352
syn::ArgCaptured {
2453
pat: syn::Pat::Ident(syn::PatIdent {

0 commit comments

Comments
 (0)