1
0
mirror of https://github.com/fluencelabs/wasm-bindgen synced 2025-03-16 18:20:51 +00:00

web-sys: Add support for Global-scope methods

This commit adds further support for the `Global` attribute to not only emit
structural accessors but also emit functions that don't take `&self`. All
methods on a `[Global]` interface will not require `&self` and will call
functions and/or access properties on the global scope.

This should enable things like:

    Window::location() // returns `Location`
    Window::fetch(...) // invokes the `fetch` function

Closes 
This commit is contained in:
Alex Crichton 2018-08-28 15:19:31 -07:00
parent 447b42b7ac
commit 36b854b69c
16 changed files with 296 additions and 202 deletions
crates
backend/src
cli-support/src/js
macro-support/src
shared/src
webidl-tests
webidl/src
examples
canvas/src
fetch/src

@ -99,6 +99,10 @@ pub enum ImportFunctionKind {
ty: syn::Type,
kind: MethodKind,
},
ScopedMethod {
ty: syn::Type,
operation: Operation,
},
Normal,
}
@ -407,17 +411,9 @@ impl ImportFunction {
}
fn shared(&self) -> shared::ImportFunction {
let method = match self.kind {
ImportFunctionKind::Method {
ref class,
ref kind,
..
} => {
let kind = match kind {
MethodKind::Constructor => shared::MethodKind::Constructor,
MethodKind::Operation(Operation { is_static, kind }) => {
let is_static = *is_static;
let kind = match kind {
let shared_operation = |operation: &Operation| {
let is_static = operation.is_static;
let kind = match &operation.kind {
OperationKind::Regular => shared::OperationKind::Regular,
OperationKind::Getter(g) => {
let g = g.as_ref().map(|g| g.to_string());
@ -435,14 +431,32 @@ impl ImportFunction {
OperationKind::IndexingSetter => shared::OperationKind::IndexingSetter,
OperationKind::IndexingDeleter => shared::OperationKind::IndexingDeleter,
};
shared::MethodKind::Operation(shared::Operation { is_static, kind })
shared::Operation { is_static, kind }
};
let method = match self.kind {
ImportFunctionKind::Method {
ref class,
ref kind,
..
} => {
let kind = match kind {
MethodKind::Constructor => shared::MethodKind::Constructor,
MethodKind::Operation(op) => {
shared::MethodKind::Operation(shared_operation(op))
}
};
Some(shared::MethodData {
class: class.clone(),
class: Some(class.clone()),
kind,
})
}
ImportFunctionKind::ScopedMethod { ref operation, .. } => {
Some(shared::MethodData {
class: None,
kind: shared::MethodKind::Operation(shared_operation(operation)),
})
}
ImportFunctionKind::Normal => None,
};

@ -784,6 +784,9 @@ impl TryToTokens for ast::ImportFunction {
}
class_ty = Some(ty);
}
ast::ImportFunctionKind::ScopedMethod { ref ty, .. } => {
class_ty = Some(ty);
}
ast::ImportFunctionKind::Normal => {}
}
let vis = &self.function.rust_vis;

@ -246,6 +246,7 @@ impl ImportedTypes for ast::ImportFunctionKind {
{
match self {
ast::ImportFunctionKind::Method { ty, .. } => ty.imported_types(f),
ast::ImportFunctionKind::ScopedMethod { ty, .. } => ty.imported_types(f),
ast::ImportFunctionKind::Normal => {}
}
}

@ -1959,16 +1959,72 @@ impl<'a, 'b> SubContext<'a, 'b> {
Some(d) => d,
};
let target = match &import.method {
Some(shared::MethodData { class, kind }) => {
let class = self.import_name(info, class)?;
match kind {
shared::MethodKind::Constructor => format!("new {}", class),
shared::MethodKind::Operation(shared::Operation { is_static, kind }) => {
let target = if import.structural {
let location = if *is_static { &class } else { "this" };
let target = self.generated_import_target(info, import, &descriptor)?;
match kind {
let js = Rust2Js::new(self.cx)
.catch(import.catch)
.process(descriptor.unwrap_function())?
.finish(&target);
self.cx.export(&import.shim, &js, None);
Ok(())
}
fn generated_import_target(
&mut self,
info: &shared::Import,
import: &shared::ImportFunction,
descriptor: &Descriptor,
) -> Result<String, Error> {
let method_data = match &import.method {
Some(data) => data,
None => {
let name = self.import_name(info, &import.function.name)?;
return Ok(if name.contains(".") {
self.cx.global(&format!(
"
const {}_target = {};
",
import.shim, name
));
format!("{}_target", import.shim)
} else {
name
})
}
};
let class = match &method_data.class {
Some(class) => self.import_name(info, class)?,
None => {
let op = match &method_data.kind {
shared::MethodKind::Operation(op) => op,
shared::MethodKind::Constructor => {
bail!("\"no class\" methods cannot be constructors")
}
};
match &op.kind {
shared::OperationKind::Regular => {
return Ok(import.function.name.to_string())
}
shared::OperationKind::Getter(g) => {
return Ok(format!("(() => {})", g));
}
shared::OperationKind::Setter(g) => {
return Ok(format!("(v => {} = v)", g));
}
_ => bail!("\"no class\" methods must be regular/getter/setter"),
}
}
};
let op = match &method_data.kind {
shared::MethodKind::Constructor => return Ok(format!("new {}", class)),
shared::MethodKind::Operation(op) => op,
};
let target = if import.structural {
let location = if op.is_static { &class } else { "this" };
match &op.kind {
shared::OperationKind::Regular => {
let nargs = descriptor.unwrap_function().arguments.len();
let mut s = format!("function(");
@ -2022,13 +2078,13 @@ impl<'a, 'b> SubContext<'a, 'b> {
),
}
} else {
let (location, binding) = if *is_static {
let (location, binding) = if op.is_static {
("", format!(".bind({})", class))
} else {
(".prototype", "".into())
};
match kind {
match &op.kind {
shared::OperationKind::Regular => {
format!("{}{}.{}{}", class, location, import.function.name, binding)
}
@ -2064,41 +2120,14 @@ impl<'a, 'b> SubContext<'a, 'b> {
};
self.cx.global(&format!(
"
const {}_target = {} {} ;
",
"const {}_target = {}{};",
import.shim, target, fallback
));
format!(
Ok(format!(
"{}_target{}",
import.shim,
if *is_static { "" } else { ".call" }
)
}
}
}
None => {
let name = self.import_name(info, &import.function.name)?;
if name.contains(".") {
self.cx.global(&format!(
"
const {}_target = {};
",
import.shim, name
));
format!("{}_target", import.shim)
} else {
name
}
}
};
let js = Rust2Js::new(self.cx)
.catch(import.catch)
.process(descriptor.unwrap_function())?
.finish(&target);
self.cx.export(&import.shim, &js, None);
Ok(())
if op.is_static { "" } else { ".call" }
))
}
fn generate_import_type(

@ -514,6 +514,7 @@ impl<'a> ConvertToAst<(BindgenAttrs, &'a Option<String>)> for syn::ForeignItemFn
let shim = {
let ns = match kind {
ast::ImportFunctionKind::ScopedMethod { .. } |
ast::ImportFunctionKind::Normal => (0, "n"),
ast::ImportFunctionKind::Method { ref class, .. } => (1, &class[..]),
};

@ -48,7 +48,7 @@ pub struct ImportFunction {
#[derive(Deserialize, Serialize)]
pub struct MethodData {
pub class: String,
pub class: Option<String>,
pub kind: MethodKind,
}

@ -0,0 +1,4 @@
global.global_no_args = () => 3;
global.global_with_args = (a, b) => a + b;
global.global_attribute = 'x';

@ -0,0 +1,12 @@
use wasm_bindgen_test::*;
include!(concat!(env!("OUT_DIR"), "/global.rs"));
#[wasm_bindgen_test]
fn works() {
assert_eq!(Global::global_no_args(), 3);
assert_eq!(Global::global_with_args("a", "b"), "ab");
assert_eq!(Global::global_attribute(), "x");
Global::set_global_attribute("y");
assert_eq!(Global::global_attribute(), "y");
}

6
crates/webidl-tests/global.webidl vendored Normal file

@ -0,0 +1,6 @@
[Global=x]
interface Global {
unsigned long global_no_args();
DOMString global_with_args(DOMString a, DOMString b);
attribute DOMString global_attribute;
};

@ -10,3 +10,4 @@ pub mod namespace;
pub mod simple;
pub mod throws;
pub mod dictionary;
pub mod global;

@ -87,11 +87,7 @@ global.Unforgeable = class Unforgeable {
}
};
global.GlobalMethod = class GlobalMethod {
constructor() {
this.m = () => 123;
}
};
global.m = () => 123;
global.Indexing = function () {
return new Proxy({}, {

@ -64,8 +64,7 @@ fn nullable_method() {
#[wasm_bindgen_test]
fn global_method() {
let f = GlobalMethod::new().unwrap();
assert_eq!(f.m(), 123);
assert_eq!(GlobalMethod::m(), 123);
}
#[wasm_bindgen_test]

@ -591,6 +591,11 @@ fn member_attribute<'src>(
let is_structural = util::is_structural(attrs);
let throws = util::throws(attrs);
let global = first_pass
.interfaces
.get(self_name)
.map(|interface_data| interface_data.global)
.unwrap_or(false);
for import_function in first_pass.create_getter(
identifier,
@ -599,6 +604,7 @@ fn member_attribute<'src>(
is_static,
is_structural,
throws,
global,
) {
program.imports.push(wrap_import_function(import_function));
}
@ -611,6 +617,7 @@ fn member_attribute<'src>(
is_static,
is_structural,
throws,
global,
) {
program.imports.push(wrap_import_function(import_function));
}
@ -712,6 +719,12 @@ fn member_operation<'src>(
operation_ids.push(id);
}
let global = first_pass
.interfaces
.get(self_name)
.map(|interface_data| interface_data.global)
.unwrap_or(false);
for id in operation_ids {
let methods = first_pass
.create_basic_method(
@ -724,15 +737,10 @@ fn member_operation<'src>(
OperationId::IndexingGetter |
OperationId::IndexingSetter |
OperationId::IndexingDeleter => true,
_ => {
first_pass
.interfaces
.get(self_name)
.map(|interface_data| interface_data.global)
.unwrap_or(false)
}
_ => false,
},
util::throws(attrs),
global,
);
for method in methods {

@ -297,6 +297,7 @@ impl<'src> FirstPassRecord<'src> {
let rust_name = rust_ident(&rust_name);
let shim = {
let ns = match kind {
backend::ast::ImportFunctionKind::ScopedMethod { .. } |
backend::ast::ImportFunctionKind::Normal => "",
backend::ast::ImportFunctionKind::Method { ref class, .. } => class,
};
@ -389,6 +390,7 @@ impl<'src> FirstPassRecord<'src> {
is_static: bool,
structural: bool,
catch: bool,
global: bool,
) -> Vec<backend::ast::ImportFunction> {
let (overloaded, same_argument_names) = self.get_operation_overloading(
arguments,
@ -410,20 +412,26 @@ impl<'src> FirstPassRecord<'src> {
first_pass::OperationId::IndexingSetter => "set",
first_pass::OperationId::IndexingDeleter => "delete",
};
let kind = backend::ast::ImportFunctionKind::Method {
class: self_name.to_string(),
ty: ident_ty(rust_ident(camel_case_ident(&self_name).as_str())),
kind: backend::ast::MethodKind::Operation(backend::ast::Operation {
is_static,
kind: match &operation_id {
let operation_kind = match &operation_id {
first_pass::OperationId::Constructor => panic!("constructors are unsupported"),
first_pass::OperationId::Operation(_) => backend::ast::OperationKind::Regular,
first_pass::OperationId::IndexingGetter => backend::ast::OperationKind::IndexingGetter,
first_pass::OperationId::IndexingSetter => backend::ast::OperationKind::IndexingSetter,
first_pass::OperationId::IndexingDeleter => backend::ast::OperationKind::IndexingDeleter,
},
}),
};
let operation = backend::ast::Operation { is_static, kind: operation_kind };
let ty = ident_ty(rust_ident(camel_case_ident(&self_name).as_str()));
let kind = if global {
backend::ast::ImportFunctionKind::ScopedMethod {
ty,
operation,
}
} else {
backend::ast::ImportFunctionKind::Method {
class: self_name.to_string(),
ty,
kind: backend::ast::MethodKind::Operation(operation),
}
};
let ret = match return_type.to_idl_type(self) {
@ -591,19 +599,29 @@ impl<'src> FirstPassRecord<'src> {
is_static: bool,
is_structural: bool,
catch: bool,
global: bool,
) -> Vec<backend::ast::ImportFunction> {
let ret = match ty.to_idl_type(self) {
None => return Vec::new(),
Some(idl_type) => idl_type,
};
let kind = backend::ast::ImportFunctionKind::Method {
class: self_name.to_string(),
ty: ident_ty(rust_ident(camel_case_ident(&self_name).as_str())),
kind: backend::ast::MethodKind::Operation(backend::ast::Operation {
let operation = backend::ast::Operation {
is_static,
kind: backend::ast::OperationKind::Getter(Some(raw_ident(name))),
}),
};
let ty = ident_ty(rust_ident(camel_case_ident(&self_name).as_str()));
let kind = if global {
backend::ast::ImportFunctionKind::ScopedMethod {
ty,
operation,
}
} else {
backend::ast::ImportFunctionKind::Method {
class: self_name.to_string(),
ty,
kind: backend::ast::MethodKind::Operation(operation),
}
};
let doc_comment = Some(format!("The `{}` getter\n\n{}", name, mdn_doc(self_name, Some(name))));
@ -614,19 +632,30 @@ impl<'src> FirstPassRecord<'src> {
pub fn create_setter(
&self,
name: &str,
ty: weedle::types::Type,
field_ty: weedle::types::Type,
self_name: &str,
is_static: bool,
is_structural: bool,
catch: bool,
global: bool,
) -> Vec<backend::ast::ImportFunction> {
let kind = backend::ast::ImportFunctionKind::Method {
class: self_name.to_string(),
ty: ident_ty(rust_ident(camel_case_ident(&self_name).as_str())),
kind: backend::ast::MethodKind::Operation(backend::ast::Operation {
let operation = backend::ast::Operation {
is_static,
kind: backend::ast::OperationKind::Setter(Some(raw_ident(name))),
}),
};
let ty = ident_ty(rust_ident(camel_case_ident(&self_name).as_str()));
let kind = if global {
backend::ast::ImportFunctionKind::ScopedMethod {
ty,
operation,
}
} else {
backend::ast::ImportFunctionKind::Method {
class: self_name.to_string(),
ty,
kind: backend::ast::MethodKind::Operation(operation),
}
};
let doc_comment = Some(format!("The `{}` setter\n\n{}", name, mdn_doc(self_name, Some(name))));
@ -636,7 +665,7 @@ impl<'src> FirstPassRecord<'src> {
false,
&[(
name,
match ty.to_idl_type(self) {
match field_ty.to_idl_type(self) {
None => return Vec::new(),
Some(idl_type) => idl_type,
},

@ -6,13 +6,9 @@ use std::f64;
use wasm_bindgen::prelude::*;
use wasm_bindgen::JsCast;
#[wasm_bindgen]
extern "C" {
static document: web_sys::Document;
}
#[wasm_bindgen]
pub fn draw() {
let document = web_sys::Window::document().unwrap();
let canvas = document.get_element_by_id("canvas").unwrap();
let canvas: web_sys::HtmlCanvasElement = canvas
.dyn_into::<web_sys::HtmlCanvasElement>()

@ -40,11 +40,6 @@ pub struct Signature {
pub email: String,
}
#[wasm_bindgen]
extern "C" {
static window: Window;
}
#[wasm_bindgen]
pub fn run() -> Promise {
let mut request_options = RequestInit::new();
@ -56,7 +51,7 @@ pub fn run() -> Promise {
// the RequestInit struct will eventually support setting headers, but that's missing right now
req.headers().set("Accept", "application/vnd.github.v3+json").unwrap();
let req_promise = window.fetch_with_request(&req);
let req_promise = Window::fetch_with_request(&req);
let to_return = JsFuture::from(req_promise).and_then(|resp_value| {
// resp_value is a Response object