Files
wasm-bindgen/crates/shared/src/lib.rs
Alex Crichton eee71de0ce Support asynchronous tests (#600)
* Tweak the implementation of heap closures

This commit updates the implementation of the `Closure` type to internally store
an `Rc` and be suitable for dropping a `Closure` during the execution of the
closure. This is currently needed for promises but may be generally useful as
well!

* Support asynchronous tests

This commit adds support for executing tests asynchronously. This is modeled
by tests returning a `Future` instead of simply executing inline, and is
signified with `#[wasm_bindgen_test(async)]`.

Support for this is added through a new `wasm-bindgen-futures` crate which is a
binding between the `futures` crate and JS `Promise` objects.

Lots more details can be found in the details of the commit, but one of the end
results is that the `web-sys` tests are now entirely contained in the same test
suite and don't need `npm install` to be run to execute them!

* Review tweaks

* Add some bindings for `Function.call` to `js_sys`

Name them `call0`, `call1`, `call2`, ... for the number of arguments being
passed.

* Use oneshots channels with `JsFuture`

It did indeed clean up the implementation!
2018-08-01 15:52:24 -05:00

182 lines
4.0 KiB
Rust

#![doc(html_root_url = "https://docs.rs/wasm-bindgen-shared/0.2")]
#[macro_use]
extern crate serde_derive;
pub const SCHEMA_VERSION: &str = "7";
#[derive(Deserialize)]
pub struct ProgramOnlySchema {
pub schema_version: String,
pub version: String,
}
#[derive(Deserialize, Serialize)]
pub struct Program {
pub exports: Vec<Export>,
pub enums: Vec<Enum>,
pub imports: Vec<Import>,
pub structs: Vec<Struct>,
pub version: String,
pub schema_version: String,
}
#[derive(Deserialize, Serialize)]
pub struct Import {
pub module: Option<String>,
pub version: Option<String>,
pub js_namespace: Option<String>,
pub kind: ImportKind,
}
#[derive(Deserialize, Serialize)]
#[serde(tag = "kind", rename_all = "lowercase")]
pub enum ImportKind {
Function(ImportFunction),
Static(ImportStatic),
Type(ImportType),
Enum(ImportEnum),
}
#[derive(Deserialize, Serialize)]
pub struct ImportFunction {
pub shim: String,
pub catch: bool,
pub method: Option<MethodData>,
pub structural: bool,
pub function: Function,
}
#[derive(Deserialize, Serialize)]
pub struct MethodData {
pub class: String,
pub kind: MethodKind,
}
#[derive(Deserialize, Serialize)]
pub enum MethodKind {
Constructor,
Operation(Operation),
}
#[derive(Deserialize, Serialize)]
pub struct Operation {
pub is_static: bool,
pub kind: OperationKind,
}
#[derive(Deserialize, Serialize)]
pub enum OperationKind {
Regular,
Getter(String),
Setter(String),
}
#[derive(Deserialize, Serialize)]
pub struct ImportStatic {
pub name: String,
pub shim: String,
}
#[derive(Deserialize, Serialize)]
pub struct ImportType {}
#[derive(Deserialize, Serialize)]
pub struct ImportEnum {}
#[derive(Deserialize, Serialize)]
pub struct Export {
pub class: Option<String>,
pub method: bool,
pub consumed: bool,
pub constructor: Option<String>,
pub function: Function,
pub comments: Vec<String>,
}
#[derive(Deserialize, Serialize)]
pub struct Enum {
pub name: String,
pub variants: Vec<EnumVariant>,
pub comments: Vec<String>,
}
#[derive(Deserialize, Serialize)]
pub struct EnumVariant {
pub name: String,
pub value: u32,
}
#[derive(Deserialize, Serialize)]
pub struct Function {
pub name: String,
}
#[derive(Deserialize, Serialize)]
pub struct Struct {
pub name: String,
pub fields: Vec<StructField>,
pub comments: Vec<String>,
}
#[derive(Deserialize, Serialize)]
pub struct StructField {
pub name: String,
pub readonly: bool,
pub comments: Vec<String>,
}
pub fn new_function(struct_name: &str) -> String {
let mut name = format!("__wbg_");
name.extend(struct_name.chars().flat_map(|s| s.to_lowercase()));
name.push_str("_new");
return name;
}
pub fn free_function(struct_name: &str) -> String {
let mut name = format!("__wbg_");
name.extend(struct_name.chars().flat_map(|s| s.to_lowercase()));
name.push_str("_free");
return name;
}
pub fn free_function_export_name(function_name: &str) -> String {
function_name.to_string()
}
pub fn struct_function_export_name(struct_: &str, f: &str) -> String {
let mut name = struct_
.chars()
.flat_map(|s| s.to_lowercase())
.collect::<String>();
name.push_str("_");
name.push_str(f);
return name;
}
pub fn struct_field_get(struct_: &str, f: &str) -> String {
let mut name = String::from("__wbg_get_");
name.extend(struct_.chars().flat_map(|s| s.to_lowercase()));
name.push_str("_");
name.push_str(f);
return name;
}
pub fn struct_field_set(struct_: &str, f: &str) -> String {
let mut name = String::from("__wbg_set_");
name.extend(struct_.chars().flat_map(|s| s.to_lowercase()));
name.push_str("_");
name.push_str(f);
return name;
}
pub fn version() -> String {
let mut v = env!("CARGO_PKG_VERSION").to_string();
if let Some(s) = option_env!("WBG_VERSION") {
v.push_str(" (");
v.push_str(s);
v.push_str(")");
}
return v;
}