mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-06-13 04:51:23 +00:00
Implement static
imports
This allows importing static objects like `document`, `window`, or an arbitrary JS object from a module
This commit is contained in:
@ -11,6 +11,7 @@ pub struct Program {
|
||||
pub enums: Vec<Enum>,
|
||||
pub imported_types: Vec<ImportedType>,
|
||||
pub structs: Vec<Struct>,
|
||||
pub imported_fields: Vec<ImportField>,
|
||||
}
|
||||
|
||||
pub struct Export {
|
||||
@ -62,6 +63,13 @@ pub struct ImportedType {
|
||||
pub name: syn::Ident,
|
||||
}
|
||||
|
||||
pub struct ImportField {
|
||||
pub vis: syn::Visibility,
|
||||
pub ty: syn::Type,
|
||||
pub module: Option<String>,
|
||||
pub name: syn::Ident,
|
||||
}
|
||||
|
||||
pub enum Type {
|
||||
// special
|
||||
Vector(VectorType, bool),
|
||||
@ -208,6 +216,7 @@ impl Program {
|
||||
match item {
|
||||
syn::ForeignItem::Fn(f) => self.push_foreign_fn(f, &opts),
|
||||
syn::ForeignItem::Type(t) => self.push_foreign_ty(t, &opts),
|
||||
syn::ForeignItem::Static(s) => self.push_foreign_static(s, &opts),
|
||||
_ => panic!("only foreign functions/types allowed for now"),
|
||||
}
|
||||
}
|
||||
@ -341,6 +350,20 @@ impl Program {
|
||||
});
|
||||
}
|
||||
|
||||
pub fn push_foreign_static(&mut self,
|
||||
f: syn::ForeignItemStatic,
|
||||
module_opts: &BindgenAttrs) {
|
||||
if f.mutability.is_some() {
|
||||
panic!("cannot import mutable globals yet")
|
||||
}
|
||||
self.imported_fields.push(ImportField {
|
||||
module: module_opts.module().map(|s| s.to_string()),
|
||||
ty: *f.ty,
|
||||
vis: f.vis,
|
||||
name: f.ident
|
||||
});
|
||||
}
|
||||
|
||||
pub fn literal(&self, dst: &mut Tokens) -> usize {
|
||||
let mut tmp = Tokens::new();
|
||||
let cnt = {
|
||||
@ -792,3 +815,12 @@ impl ToTokens for VectorType {
|
||||
me.to_tokens(tokens);
|
||||
}
|
||||
}
|
||||
|
||||
impl ImportField {
|
||||
pub fn shared(&self) -> shared::ImportField {
|
||||
shared::ImportField {
|
||||
module: self.module.clone(),
|
||||
name: self.name.to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -78,6 +78,9 @@ impl ToTokens for ast::Program {
|
||||
for it in self.imported_types.iter() {
|
||||
it.to_tokens(tokens);
|
||||
}
|
||||
for it in self.imported_fields.iter() {
|
||||
it.to_tokens(tokens);
|
||||
}
|
||||
|
||||
// Generate a static which will eventually be what lives in a custom section
|
||||
// of the wasm executable. For now it's just a plain old static, but we'll
|
||||
@ -591,3 +594,28 @@ impl ToTokens for ast::Enum {
|
||||
}).to_tokens(into);
|
||||
}
|
||||
}
|
||||
|
||||
impl ToTokens for ast::ImportField {
|
||||
fn to_tokens(&self, into: &mut Tokens) {
|
||||
let name = self.name;
|
||||
let ty = &self.ty;
|
||||
let shim_name = syn::Ident::from(self.shared().shim_name());
|
||||
let vis = &self.vis;
|
||||
(my_quote! {
|
||||
#vis static #name: ::wasm_bindgen::JsStatic<#ty> = {
|
||||
fn init() -> #ty {
|
||||
extern {
|
||||
fn #shim_name() -> u32;
|
||||
}
|
||||
unsafe {
|
||||
::wasm_bindgen::convert::WasmBoundary::from_js(#shim_name())
|
||||
}
|
||||
}
|
||||
::wasm_bindgen::JsStatic {
|
||||
__inner: ::std::cell::UnsafeCell::new(None),
|
||||
__init: init,
|
||||
}
|
||||
};
|
||||
}).to_tokens(into);
|
||||
}
|
||||
}
|
||||
|
@ -107,7 +107,8 @@ impl Literal for ast::Program {
|
||||
fn literal(&self, a: &mut LiteralBuilder) {
|
||||
a.fields(&[
|
||||
("exports", &|a| a.list_of(&self.exports)),
|
||||
("imports", &|a| a.list_of(&self.imports)),
|
||||
("imported_functions", &|a| a.list_of(&self.imports)),
|
||||
("imported_fields", &|a| a.list_of(&self.imported_fields)),
|
||||
("enums", &|a| a.list_of(&self.enums)),
|
||||
("custom_type_names", &|a| {
|
||||
let names = self.exports
|
||||
@ -271,3 +272,15 @@ impl Literal for ast::Variant {
|
||||
])
|
||||
}
|
||||
}
|
||||
|
||||
impl Literal for ast::ImportField {
|
||||
fn literal(&self, a: &mut LiteralBuilder) {
|
||||
a.fields(&[
|
||||
("name", &|a| a.str(self.name.as_ref())),
|
||||
("module", &|a| match self.module {
|
||||
Some(ref s) => a.str(s),
|
||||
None => a.append("null"),
|
||||
}),
|
||||
])
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user