mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-06-15 22:11:23 +00:00
cargo +nightly fmt --all
Rustfmt all the things!
This commit is contained in:
@ -163,20 +163,14 @@ impl Descriptor {
|
||||
|
||||
pub fn is_wasm_native(&self) -> bool {
|
||||
match *self {
|
||||
Descriptor::I32
|
||||
| Descriptor::U32
|
||||
| Descriptor::F32
|
||||
| Descriptor::F64 => true,
|
||||
Descriptor::I32 | Descriptor::U32 | Descriptor::F32 | Descriptor::F64 => true,
|
||||
_ => return false,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_abi_as_u32(&self) -> bool {
|
||||
match *self {
|
||||
Descriptor::I8
|
||||
| Descriptor::U8
|
||||
| Descriptor::I16
|
||||
| Descriptor::U16 => true,
|
||||
Descriptor::I8 | Descriptor::U8 | Descriptor::I16 | Descriptor::U16 => true,
|
||||
_ => return false,
|
||||
}
|
||||
}
|
||||
@ -223,12 +217,10 @@ impl Descriptor {
|
||||
Descriptor::Slice(ref d) => &**d,
|
||||
_ => return None,
|
||||
},
|
||||
Descriptor::Clamped(ref d) => {
|
||||
match d.vector_kind()? {
|
||||
VectorKind::U8 => return Some(VectorKind::ClampedU8),
|
||||
_ => return None,
|
||||
}
|
||||
}
|
||||
Descriptor::Clamped(ref d) => match d.vector_kind()? {
|
||||
VectorKind::U8 => return Some(VectorKind::ClampedU8),
|
||||
_ => return None,
|
||||
},
|
||||
_ => return None,
|
||||
};
|
||||
match *inner {
|
||||
|
@ -16,8 +16,8 @@ use failure::Error;
|
||||
use parity_wasm::elements::*;
|
||||
|
||||
use descriptor::Descriptor;
|
||||
use js::Context;
|
||||
use js::js2rust::Js2Rust;
|
||||
use js::Context;
|
||||
|
||||
pub fn rewrite(input: &mut Context) -> Result<(), Error> {
|
||||
let info = ClosureDescriptors::new(input);
|
||||
@ -29,7 +29,7 @@ pub fn rewrite(input: &mut Context) -> Result<(), Error> {
|
||||
info.code_idx_to_descriptor.len(),
|
||||
);
|
||||
if info.element_removal_list.len() == 0 {
|
||||
return Ok(())
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
// Make sure the names section is available in the wasm module because we'll
|
||||
@ -39,7 +39,8 @@ pub fn rewrite(input: &mut Context) -> Result<(), Error> {
|
||||
input.parse_wasm_names();
|
||||
Remap {
|
||||
code_idx_to_descriptor: &info.code_idx_to_descriptor,
|
||||
old_num_imports: input.module
|
||||
old_num_imports: input
|
||||
.module
|
||||
.import_section()
|
||||
.map(|s| s.functions())
|
||||
.unwrap_or(0) as u32,
|
||||
@ -90,7 +91,9 @@ impl ClosureDescriptors {
|
||||
Some(i) => i,
|
||||
None => return Default::default(),
|
||||
};
|
||||
let imports = input.module.import_section()
|
||||
let imports = input
|
||||
.module
|
||||
.import_section()
|
||||
.map(|s| s.functions())
|
||||
.unwrap_or(0);
|
||||
let mut ret = ClosureDescriptors::default();
|
||||
@ -100,24 +103,18 @@ impl ClosureDescriptors {
|
||||
None => return Default::default(),
|
||||
};
|
||||
for (i, function) in code.bodies().iter().enumerate() {
|
||||
let call_pos = function.code()
|
||||
.elements()
|
||||
.iter()
|
||||
.position(|i| {
|
||||
match i {
|
||||
Instruction::Call(i) => *i == wbindgen_describe_closure,
|
||||
_ => false,
|
||||
}
|
||||
});
|
||||
let call_pos = function.code().elements().iter().position(|i| match i {
|
||||
Instruction::Call(i) => *i == wbindgen_describe_closure,
|
||||
_ => false,
|
||||
});
|
||||
let call_pos = match call_pos {
|
||||
Some(i) => i,
|
||||
None => continue,
|
||||
};
|
||||
let descriptor = input.interpreter.interpret_closure_descriptor(
|
||||
i,
|
||||
input.module,
|
||||
&mut ret.element_removal_list,
|
||||
).unwrap();
|
||||
let descriptor = input
|
||||
.interpreter
|
||||
.interpret_closure_descriptor(i, input.module, &mut ret.element_removal_list)
|
||||
.unwrap();
|
||||
// `new_idx` is the function-space index of the function that we'll
|
||||
// be injecting. Calls to the code function `i` will instead be
|
||||
// rewritten to calls to `new_idx`, which is an import that we'll
|
||||
@ -132,7 +129,7 @@ impl ClosureDescriptors {
|
||||
},
|
||||
);
|
||||
}
|
||||
return ret
|
||||
return ret;
|
||||
}
|
||||
|
||||
/// Here we remove elements from the function table. All our descriptor
|
||||
@ -165,7 +162,7 @@ impl ClosureDescriptors {
|
||||
// If we keep this entry, then keep going
|
||||
if !to_remove.contains(&j) {
|
||||
current.push(*idx);
|
||||
continue
|
||||
continue;
|
||||
}
|
||||
|
||||
// If we have members of `current` then we save off a section
|
||||
@ -173,10 +170,8 @@ impl ClosureDescriptors {
|
||||
let next_offset = offset + (current.len() as i32) + 1;
|
||||
if current.len() > 0 {
|
||||
let members = mem::replace(&mut current, Vec::new());
|
||||
let offset = InitExpr::new(vec![
|
||||
Instruction::I32Const(offset),
|
||||
Instruction::End,
|
||||
]);
|
||||
let offset =
|
||||
InitExpr::new(vec![Instruction::I32Const(offset), Instruction::End]);
|
||||
let new_entry = ElementSegment::new(0, offset, members);
|
||||
elements.entries_mut().push(new_entry);
|
||||
}
|
||||
@ -184,10 +179,7 @@ impl ClosureDescriptors {
|
||||
}
|
||||
// Any remaining function table entries get pushed at the end.
|
||||
if current.len() > 0 {
|
||||
let offset = InitExpr::new(vec![
|
||||
Instruction::I32Const(offset),
|
||||
Instruction::End,
|
||||
]);
|
||||
let offset = InitExpr::new(vec![Instruction::I32Const(offset), Instruction::End]);
|
||||
let new_entry = ElementSegment::new(0, offset, current);
|
||||
elements.entries_mut().push(new_entry);
|
||||
}
|
||||
@ -211,9 +203,8 @@ impl ClosureDescriptors {
|
||||
// signature of our `#[inline(never)]` functions. Find the type
|
||||
// signature index so we can assign it below.
|
||||
let type_idx = {
|
||||
let kind = input.module.import_section()
|
||||
.unwrap()
|
||||
.entries()[wbindgen_describe_closure as usize]
|
||||
let kind = input.module.import_section().unwrap().entries()
|
||||
[wbindgen_describe_closure as usize]
|
||||
.external();
|
||||
match kind {
|
||||
External::Function(i) => *i,
|
||||
@ -268,7 +259,9 @@ impl ClosureDescriptors {
|
||||
import_name,
|
||||
External::Function(type_idx as u32),
|
||||
);
|
||||
input.module.import_section_mut()
|
||||
input
|
||||
.module
|
||||
.import_section_mut()
|
||||
.unwrap()
|
||||
.entries_mut()
|
||||
.push(new_import);
|
||||
@ -313,7 +306,9 @@ impl<'a> Remap<'a> {
|
||||
Section::Export(e) => self.remap_export_section(e),
|
||||
Section::Element(e) => self.remap_element_section(e),
|
||||
Section::Code(e) => self.remap_code_section(e),
|
||||
Section::Start(i) => { self.remap_idx(i); }
|
||||
Section::Start(i) => {
|
||||
self.remap_idx(i);
|
||||
}
|
||||
Section::Name(n) => self.remap_name_section(n),
|
||||
_ => {}
|
||||
}
|
||||
@ -328,10 +323,11 @@ impl<'a> Remap<'a> {
|
||||
|
||||
fn remap_export_entry(&self, entry: &mut ExportEntry) {
|
||||
match entry.internal_mut() {
|
||||
Internal::Function(i) => { self.remap_idx(i); }
|
||||
Internal::Function(i) => {
|
||||
self.remap_idx(i);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fn remap_element_section(&self, section: &mut ElementSection) {
|
||||
@ -364,7 +360,9 @@ impl<'a> Remap<'a> {
|
||||
|
||||
fn remap_instruction(&self, instr: &mut Instruction) {
|
||||
match instr {
|
||||
Instruction::Call(i) => { self.remap_idx(i); }
|
||||
Instruction::Call(i) => {
|
||||
self.remap_idx(i);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
@ -403,7 +401,7 @@ impl<'a> Remap<'a> {
|
||||
// If this was an imported function we didn't reorder those, so nothing
|
||||
// to do.
|
||||
if *idx < self.old_num_imports {
|
||||
return false
|
||||
return false;
|
||||
}
|
||||
// ... otherwise we're injecting a number of new imports, so offset
|
||||
// everything.
|
||||
|
@ -197,10 +197,8 @@ impl<'a, 'b> Js2Rust<'a, 'b> {
|
||||
self.cx.expose_add_heap_object();
|
||||
if optional {
|
||||
self.cx.expose_is_like_none();
|
||||
self.rust_arguments.push(format!(
|
||||
"isLikeNone({0}) ? 0 : addHeapObject({0})",
|
||||
name,
|
||||
));
|
||||
self.rust_arguments
|
||||
.push(format!("isLikeNone({0}) ? 0 : addHeapObject({0})", name,));
|
||||
} else {
|
||||
self.rust_arguments.push(format!("addHeapObject({})", name));
|
||||
}
|
||||
@ -225,7 +223,8 @@ impl<'a, 'b> Js2Rust<'a, 'b> {
|
||||
}
|
||||
|
||||
self.rust_arguments.push(format!("!isLikeNone({0})", name));
|
||||
self.rust_arguments.push(format!("isLikeNone({0}) ? 0 : {0}", name));
|
||||
self.rust_arguments
|
||||
.push(format!("isLikeNone({0}) ? 0 : {0}", name));
|
||||
return Ok(self);
|
||||
}
|
||||
|
||||
@ -245,7 +244,8 @@ impl<'a, 'b> Js2Rust<'a, 'b> {
|
||||
));
|
||||
}
|
||||
|
||||
self.rust_arguments.push(format!("isLikeNone({0}) ? 0xFFFFFF : {0}", name));
|
||||
self.rust_arguments
|
||||
.push(format!("isLikeNone({0}) ? 0xFFFFFF : {0}", name));
|
||||
return Ok(self);
|
||||
}
|
||||
|
||||
@ -278,7 +278,8 @@ impl<'a, 'b> Js2Rust<'a, 'b> {
|
||||
match *arg {
|
||||
Descriptor::Boolean => {
|
||||
self.cx.expose_is_like_none();
|
||||
self.js_arguments.push((name.clone(), "boolean".to_string()));
|
||||
self.js_arguments
|
||||
.push((name.clone(), "boolean".to_string()));
|
||||
if self.cx.config.debug {
|
||||
self.cx.expose_assert_bool();
|
||||
self.prelude(&format!(
|
||||
@ -290,17 +291,22 @@ impl<'a, 'b> Js2Rust<'a, 'b> {
|
||||
name,
|
||||
));
|
||||
}
|
||||
self.rust_arguments.push(format!("isLikeNone({0}) ? 0xFFFFFF : {0} ? 1 : 0", name));
|
||||
self.rust_arguments
|
||||
.push(format!("isLikeNone({0}) ? 0xFFFFFF : {0} ? 1 : 0", name));
|
||||
return Ok(self);
|
||||
},
|
||||
}
|
||||
Descriptor::Char => {
|
||||
self.cx.expose_is_like_none();
|
||||
self.js_arguments.push((name.clone(), "string".to_string()));
|
||||
self.rust_arguments.push(format!("!isLikeNone({0})", name));
|
||||
self.rust_arguments.push(format!("isLikeNone({0}) ? 0 : {0}.codePointAt(0)", name));
|
||||
self.rust_arguments
|
||||
.push(format!("isLikeNone({0}) ? 0 : {0}.codePointAt(0)", name));
|
||||
return Ok(self);
|
||||
},
|
||||
_ => bail!("unsupported optional argument type for calling Rust function from JS: {:?}", arg),
|
||||
}
|
||||
_ => bail!(
|
||||
"unsupported optional argument type for calling Rust function from JS: {:?}",
|
||||
arg
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
@ -401,7 +407,10 @@ impl<'a, 'b> Js2Rust<'a, 'b> {
|
||||
self.js_arguments.push((name.clone(), "string".to_string()));
|
||||
self.rust_arguments.push(format!("{}.codePointAt(0)", name))
|
||||
}
|
||||
_ => bail!("unsupported argument type for calling Rust function from JS: {:?}", arg),
|
||||
_ => bail!(
|
||||
"unsupported argument type for calling Rust function from JS: {:?}",
|
||||
arg
|
||||
),
|
||||
}
|
||||
Ok(self)
|
||||
}
|
||||
@ -412,14 +421,15 @@ impl<'a, 'b> Js2Rust<'a, 'b> {
|
||||
Some(class) if class == name => {
|
||||
self.ret_expr = format!("this.ptr = RET;");
|
||||
if self.cx.config.weak_refs {
|
||||
self.ret_expr.push_str(&format!("\
|
||||
self.ret_expr.push_str(&format!(
|
||||
"\
|
||||
addCleanup(this, this.ptr, free{});
|
||||
", name));
|
||||
",
|
||||
name
|
||||
));
|
||||
}
|
||||
}
|
||||
Some(class) => {
|
||||
bail!("constructor for `{}` cannot return `{}`", class, name)
|
||||
}
|
||||
Some(class) => bail!("constructor for `{}` cannot return `{}`", class, name),
|
||||
None => {
|
||||
self.ret_ty = name.to_string();
|
||||
self.cx.require_class_wrap(name);
|
||||
@ -465,7 +475,11 @@ impl<'a, 'b> Js2Rust<'a, 'b> {
|
||||
",
|
||||
f,
|
||||
ty.size(),
|
||||
guard = if optional { "if (rustptr === 0) return;" } else { "" },
|
||||
guard = if optional {
|
||||
"if (rustptr === 0) return;"
|
||||
} else {
|
||||
""
|
||||
},
|
||||
);
|
||||
return Ok(self);
|
||||
}
|
||||
@ -559,7 +573,7 @@ impl<'a, 'b> Js2Rust<'a, 'b> {
|
||||
return ret === 0xFFFFFF ? undefined : ret !== 0;
|
||||
".to_string();
|
||||
return Ok(self);
|
||||
},
|
||||
}
|
||||
Descriptor::Char => {
|
||||
self.ret_ty = "string".to_string();
|
||||
self.cx.expose_global_argument_ptr()?;
|
||||
@ -573,8 +587,11 @@ impl<'a, 'b> Js2Rust<'a, 'b> {
|
||||
return present === 0 ? undefined : String.fromCodePoint(value);
|
||||
".to_string();
|
||||
return Ok(self);
|
||||
},
|
||||
_ => bail!("unsupported optional return type for calling Rust function from JS: {:?}", ty),
|
||||
}
|
||||
_ => bail!(
|
||||
"unsupported optional return type for calling Rust function from JS: {:?}",
|
||||
ty
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
@ -633,15 +650,20 @@ impl<'a, 'b> Js2Rust<'a, 'b> {
|
||||
self.ret_ty = "string".to_string();
|
||||
self.ret_expr = format!("return String.fromCodePoint(RET);")
|
||||
}
|
||||
_ => bail!("unsupported return type for calling Rust function from JS: {:?}", ty),
|
||||
_ => bail!(
|
||||
"unsupported return type for calling Rust function from JS: {:?}",
|
||||
ty
|
||||
),
|
||||
}
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
pub fn js_doc_comments(&self) -> String {
|
||||
let mut ret: String = self.js_arguments.iter().map(|a| {
|
||||
format!("@param {{{}}} {}\n", a.1, a.0)
|
||||
}).collect();
|
||||
let mut ret: String = self
|
||||
.js_arguments
|
||||
.iter()
|
||||
.map(|a| format!("@param {{{}}} {}\n", a.1, a.0))
|
||||
.collect();
|
||||
ret.push_str(&format!("@returns {{{}}}", self.ret_ty));
|
||||
ret
|
||||
}
|
||||
|
@ -390,7 +390,8 @@ impl<'a> Context<'a> {
|
||||
function() {{
|
||||
return addHeapObject({});
|
||||
}}
|
||||
", mem
|
||||
",
|
||||
mem
|
||||
))
|
||||
})?;
|
||||
|
||||
@ -549,7 +550,7 @@ impl<'a> Context<'a> {
|
||||
constructor() {
|
||||
throw new Error('cannot invoke `new` directly');
|
||||
}
|
||||
"
|
||||
",
|
||||
);
|
||||
}
|
||||
|
||||
@ -705,8 +706,7 @@ impl<'a> Context<'a> {
|
||||
.filter_map(|s| match *s {
|
||||
Section::Import(ref mut s) => Some(s),
|
||||
_ => None,
|
||||
})
|
||||
.flat_map(|s| s.entries_mut());
|
||||
}).flat_map(|s| s.entries_mut());
|
||||
|
||||
for import in imports {
|
||||
if import.module() == "__wbindgen_placeholder__" {
|
||||
@ -726,7 +726,7 @@ impl<'a> Context<'a> {
|
||||
import.module_mut().truncate(0);
|
||||
import.module_mut().push_str("./");
|
||||
import.module_mut().push_str(module_name);
|
||||
continue
|
||||
continue;
|
||||
}
|
||||
|
||||
let renamed_import = format!("__wbindgen_{}", import.field());
|
||||
@ -817,7 +817,9 @@ impl<'a> Context<'a> {
|
||||
slab_next = idx;
|
||||
}}
|
||||
",
|
||||
validate_owned, INITIAL_SLAB_VALUES.len(), dec_ref
|
||||
validate_owned,
|
||||
INITIAL_SLAB_VALUES.len(),
|
||||
dec_ref
|
||||
));
|
||||
}
|
||||
|
||||
@ -848,7 +850,8 @@ impl<'a> Context<'a> {
|
||||
if !self.exposed_globals.insert("slab") {
|
||||
return;
|
||||
}
|
||||
let initial_values = INITIAL_SLAB_VALUES.iter()
|
||||
let initial_values = INITIAL_SLAB_VALUES
|
||||
.iter()
|
||||
.map(|s| format!("{{ obj: {} }}", s))
|
||||
.collect::<Vec<_>>();
|
||||
self.global(&format!("const slab = [{}];", initial_values.join(", ")));
|
||||
@ -1013,7 +1016,8 @@ impl<'a> Context<'a> {
|
||||
self.require_internal_export("__wbindgen_malloc")?;
|
||||
self.expose_uint32_memory();
|
||||
self.expose_add_heap_object();
|
||||
self.global("
|
||||
self.global(
|
||||
"
|
||||
function passArrayJsValueToWasm(array) {
|
||||
const ptr = wasm.__wbindgen_malloc(array.length * 4);
|
||||
const mem = getUint32Memory();
|
||||
@ -1023,7 +1027,8 @@ impl<'a> Context<'a> {
|
||||
return [ptr, array.length];
|
||||
}
|
||||
|
||||
");
|
||||
",
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@ -1126,7 +1131,8 @@ impl<'a> Context<'a> {
|
||||
// non-shared mode there's no need to copy the data except for the
|
||||
// string itself.
|
||||
self.memory(); // set self.memory_init
|
||||
let is_shared = self.module
|
||||
let is_shared = self
|
||||
.module
|
||||
.memory_section()
|
||||
.map(|s| s.entries()[0].limits().shared())
|
||||
.unwrap_or(match &self.memory_init {
|
||||
@ -1135,11 +1141,14 @@ impl<'a> Context<'a> {
|
||||
});
|
||||
let method = if is_shared { "slice" } else { "subarray" };
|
||||
|
||||
self.global(&format!("
|
||||
self.global(&format!(
|
||||
"
|
||||
function getStringFromWasm(ptr, len) {{
|
||||
return cachedDecoder.decode(getUint8Memory().{}(ptr, ptr + len));
|
||||
}}
|
||||
", method));
|
||||
",
|
||||
method
|
||||
));
|
||||
}
|
||||
|
||||
fn expose_get_array_js_value_from_wasm(&mut self) {
|
||||
@ -1646,18 +1655,20 @@ impl<'a> Context<'a> {
|
||||
|
||||
fn expose_is_like_none(&mut self) {
|
||||
if !self.exposed_globals.insert("is_like_none") {
|
||||
return
|
||||
return;
|
||||
}
|
||||
self.global("
|
||||
self.global(
|
||||
"
|
||||
function isLikeNone(x) {
|
||||
return x === undefined || x === null;
|
||||
}
|
||||
");
|
||||
",
|
||||
);
|
||||
}
|
||||
|
||||
fn expose_cleanup_groups(&mut self) {
|
||||
if !self.exposed_globals.insert("cleanup_groups") {
|
||||
return
|
||||
return;
|
||||
}
|
||||
self.global(
|
||||
"
|
||||
@ -1668,7 +1679,7 @@ impl<'a> Context<'a> {
|
||||
const ref = CLEANUPS.makeRef(obj, () => free(ptr));
|
||||
CLEANUPS_MAP.set(ptr, ref);
|
||||
}
|
||||
"
|
||||
",
|
||||
);
|
||||
}
|
||||
|
||||
@ -1719,17 +1730,16 @@ impl<'a> Context<'a> {
|
||||
return "wasm.memory";
|
||||
}
|
||||
|
||||
let (entry, mem) = self.module.import_section()
|
||||
let (entry, mem) = self
|
||||
.module
|
||||
.import_section()
|
||||
.expect("must import memory")
|
||||
.entries()
|
||||
.iter()
|
||||
.filter_map(|i| {
|
||||
match i.external() {
|
||||
External::Memory(m) => Some((i, m)),
|
||||
_ => None,
|
||||
}
|
||||
})
|
||||
.next()
|
||||
.filter_map(|i| match i.external() {
|
||||
External::Memory(m) => Some((i, m)),
|
||||
_ => None,
|
||||
}).next()
|
||||
.expect("must import memory");
|
||||
assert_eq!(entry.module(), "env");
|
||||
assert_eq!(entry.field(), "memory");
|
||||
@ -1822,8 +1832,11 @@ impl<'a, 'b> SubContext<'a, 'b> {
|
||||
};
|
||||
let (js, ts, js_doc) = Js2Rust::new(function_name, self.cx)
|
||||
.method(export.method, export.consumed)
|
||||
.constructor(if export.is_constructor { Some(class_name) } else { None })
|
||||
.process(descriptor.unwrap_function())?
|
||||
.constructor(if export.is_constructor {
|
||||
Some(class_name)
|
||||
} else {
|
||||
None
|
||||
}).process(descriptor.unwrap_function())?
|
||||
.finish("", &format!("wasm.{}", wasm_name));
|
||||
|
||||
let class = self
|
||||
@ -1837,8 +1850,7 @@ impl<'a, 'b> SubContext<'a, 'b> {
|
||||
|
||||
if export.is_constructor {
|
||||
if class.has_constructor {
|
||||
bail!("found duplicate constructor `{}`",
|
||||
export.function.name);
|
||||
bail!("found duplicate constructor `{}`", export.function.name);
|
||||
}
|
||||
class.has_constructor = true;
|
||||
} else if !export.method {
|
||||
@ -1871,10 +1883,7 @@ impl<'a, 'b> SubContext<'a, 'b> {
|
||||
}
|
||||
shared::ImportKind::Type(ref ty) => {
|
||||
self.generate_import_type(import, ty).with_context(|_| {
|
||||
format!(
|
||||
"failed to generate bindings for JS import `{}`",
|
||||
ty.name,
|
||||
)
|
||||
format!("failed to generate bindings for JS import `{}`", ty.name,)
|
||||
})?;
|
||||
}
|
||||
shared::ImportKind::Enum(_) => {}
|
||||
@ -1890,7 +1899,7 @@ impl<'a, 'b> SubContext<'a, 'b> {
|
||||
// The same static can be imported in multiple locations, so only
|
||||
// generate bindings once for it.
|
||||
if !self.cx.imported_statics.insert(import.shim.clone()) {
|
||||
return Ok(())
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
// TODO: should support more types to import here
|
||||
@ -1962,7 +1971,7 @@ impl<'a, 'b> SubContext<'a, 'b> {
|
||||
format!("{}_target", import.shim)
|
||||
} else {
|
||||
name
|
||||
})
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
@ -2052,9 +2061,15 @@ impl<'a, 'b> SubContext<'a, 'b> {
|
||||
class, location, s, binding,
|
||||
)
|
||||
}
|
||||
shared::OperationKind::IndexingGetter => panic!("indexing getter should be structural"),
|
||||
shared::OperationKind::IndexingSetter => panic!("indexing setter should be structural"),
|
||||
shared::OperationKind::IndexingDeleter => panic!("indexing deleter should be structural"),
|
||||
shared::OperationKind::IndexingGetter => {
|
||||
panic!("indexing getter should be structural")
|
||||
}
|
||||
shared::OperationKind::IndexingSetter => {
|
||||
panic!("indexing setter should be structural")
|
||||
}
|
||||
shared::OperationKind::IndexingDeleter => {
|
||||
panic!("indexing deleter should be structural")
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@ -2090,7 +2105,8 @@ impl<'a, 'b> SubContext<'a, 'b> {
|
||||
}
|
||||
let name = self.import_name(info, &import.name)?;
|
||||
self.cx.expose_get_object();
|
||||
let body = format!("
|
||||
let body = format!(
|
||||
"
|
||||
function(idx) {{
|
||||
return getObject(idx) instanceof {} ? 1 : 0;
|
||||
}}
|
||||
@ -2139,10 +2155,7 @@ impl<'a, 'b> SubContext<'a, 'b> {
|
||||
|
||||
// Figure out what identifier we're importing from the module. If we've
|
||||
// got a namespace we use that, otherwise it's the name specified above.
|
||||
let name_to_import = import.js_namespace
|
||||
.as_ref()
|
||||
.map(|s| &**s)
|
||||
.unwrap_or(item);
|
||||
let name_to_import = import.js_namespace.as_ref().map(|s| &**s).unwrap_or(item);
|
||||
|
||||
// Here's where it's a bit tricky. We need to make sure that importing
|
||||
// the same identifier from two different modules works, and they're
|
||||
@ -2158,7 +2171,10 @@ impl<'a, 'b> SubContext<'a, 'b> {
|
||||
let use_node_require = self.cx.use_node_require();
|
||||
let imported_identifiers = &mut self.cx.imported_identifiers;
|
||||
let imports = &mut self.cx.imports;
|
||||
let identifier = self.cx.imported_names.entry(import.module.clone())
|
||||
let identifier = self
|
||||
.cx
|
||||
.imported_names
|
||||
.entry(import.module.clone())
|
||||
.or_insert_with(Default::default)
|
||||
.entry(name_to_import.to_string())
|
||||
.or_insert_with(|| {
|
||||
@ -2170,10 +2186,7 @@ impl<'a, 'b> SubContext<'a, 'b> {
|
||||
name, module, name_to_import
|
||||
));
|
||||
} else if name_to_import == name {
|
||||
imports.push_str(&format!(
|
||||
"import {{ {} }} from '{}';\n",
|
||||
name, module
|
||||
));
|
||||
imports.push_str(&format!("import {{ {} }} from '{}';\n", name, module));
|
||||
} else {
|
||||
imports.push_str(&format!(
|
||||
"import {{ {} as {} }} from '{}';\n",
|
||||
|
@ -109,7 +109,11 @@ impl<'a, 'b> Rust2Js<'a, 'b> {
|
||||
abi,
|
||||
abi2,
|
||||
func = f,
|
||||
prefix = if optional { format!("{} == 0 ? undefined : ", abi) } else { String::new() },
|
||||
prefix = if optional {
|
||||
format!("{} == 0 ? undefined : ", abi)
|
||||
} else {
|
||||
String::new()
|
||||
},
|
||||
));
|
||||
|
||||
if !arg.is_by_ref() && !arg.is_clamped_by_ref() {
|
||||
@ -123,9 +127,12 @@ impl<'a, 'b> Rust2Js<'a, 'b> {
|
||||
abi,
|
||||
abi2,
|
||||
size = ty.size(),
|
||||
start = if optional { format!("if ({} !== 0) {{", abi) } else { String::new() },
|
||||
start = if optional {
|
||||
format!("if ({} !== 0) {{", abi)
|
||||
} else {
|
||||
String::new()
|
||||
},
|
||||
end = if optional { "}" } else { "" },
|
||||
|
||||
));
|
||||
self.cx.require_internal_export("__wbindgen_free")?;
|
||||
}
|
||||
@ -138,11 +145,11 @@ impl<'a, 'b> Rust2Js<'a, 'b> {
|
||||
if arg.is_anyref() {
|
||||
self.cx.expose_take_object();
|
||||
self.js_arguments.push(format!("takeObject({})", abi));
|
||||
return Ok(())
|
||||
return Ok(());
|
||||
} else if arg.is_ref_anyref() {
|
||||
self.cx.expose_get_object();
|
||||
self.js_arguments.push(format!("getObject({})", abi));
|
||||
return Ok(())
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
if optional {
|
||||
@ -153,12 +160,13 @@ impl<'a, 'b> Rust2Js<'a, 'b> {
|
||||
value = value,
|
||||
present = abi,
|
||||
));
|
||||
return Ok(())
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
if arg.is_abi_as_u32() {
|
||||
self.js_arguments.push(format!("{0} === 0xFFFFFF ? undefined : {0}", abi));
|
||||
return Ok(())
|
||||
self.js_arguments
|
||||
.push(format!("{0} === 0xFFFFFF ? undefined : {0}", abi));
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
if let Some(signed) = arg.get_64() {
|
||||
@ -189,9 +197,10 @@ impl<'a, 'b> Rust2Js<'a, 'b> {
|
||||
|
||||
match *arg {
|
||||
Descriptor::Boolean => {
|
||||
self.js_arguments.push(format!("{0} === 0xFFFFFF ? undefined : {0} !== 0", abi));
|
||||
return Ok(())
|
||||
},
|
||||
self.js_arguments
|
||||
.push(format!("{0} === 0xFFFFFF ? undefined : {0} !== 0", abi));
|
||||
return Ok(());
|
||||
}
|
||||
Descriptor::Char => {
|
||||
let value = self.shim_argument();
|
||||
self.js_arguments.push(format!(
|
||||
@ -199,9 +208,12 @@ impl<'a, 'b> Rust2Js<'a, 'b> {
|
||||
value = value,
|
||||
present = abi,
|
||||
));
|
||||
return Ok(())
|
||||
},
|
||||
_ => bail!("unsupported optional argument type for calling JS function from Rust: {:?}", arg),
|
||||
return Ok(());
|
||||
}
|
||||
_ => bail!(
|
||||
"unsupported optional argument type for calling JS function from Rust: {:?}",
|
||||
arg
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
@ -280,7 +292,10 @@ impl<'a, 'b> Rust2Js<'a, 'b> {
|
||||
ref d if d.is_number() => abi,
|
||||
Descriptor::Boolean => format!("{} !== 0", abi),
|
||||
Descriptor::Char => format!("String.fromCodePoint({})", abi),
|
||||
_ => bail!("unsupported argument type for calling JS function from Rust: {:?}", arg),
|
||||
_ => bail!(
|
||||
"unsupported argument type for calling JS function from Rust: {:?}",
|
||||
arg
|
||||
),
|
||||
};
|
||||
self.js_arguments.push(invoc_arg);
|
||||
Ok(())
|
||||
@ -318,8 +333,7 @@ impl<'a, 'b> Rust2Js<'a, 'b> {
|
||||
mem[ret / 4] = retptr;
|
||||
mem[ret / 4 + 1] = retlen;
|
||||
",
|
||||
prelude,
|
||||
expr
|
||||
prelude, expr
|
||||
);
|
||||
return Ok(());
|
||||
}
|
||||
@ -334,7 +348,7 @@ impl<'a, 'b> Rust2Js<'a, 'b> {
|
||||
} else {
|
||||
self.ret_expr = "return addHeapObject(JS);".to_string()
|
||||
}
|
||||
return Ok(())
|
||||
return Ok(());
|
||||
}
|
||||
if optional {
|
||||
if ty.is_wasm_native() {
|
||||
@ -411,7 +425,7 @@ impl<'a, 'b> Rust2Js<'a, 'b> {
|
||||
return isLikeNone(val) ? 0xFFFFFF : val ? 1 : 0;
|
||||
".to_string();
|
||||
return Ok(());
|
||||
},
|
||||
}
|
||||
Descriptor::Char => {
|
||||
self.cx.expose_is_like_none();
|
||||
self.cx.expose_uint32_memory();
|
||||
@ -422,8 +436,11 @@ impl<'a, 'b> Rust2Js<'a, 'b> {
|
||||
getUint32Memory()[ret / 4 + 1] = isLikeNone(val) ? 0 : val.codePointAt(0);
|
||||
".to_string();
|
||||
return Ok(());
|
||||
},
|
||||
_ => bail!("unsupported optional return type for calling JS function from Rust: {:?}", ty),
|
||||
}
|
||||
_ => bail!(
|
||||
"unsupported optional return type for calling JS function from Rust: {:?}",
|
||||
ty
|
||||
),
|
||||
};
|
||||
}
|
||||
if ty.is_number() {
|
||||
@ -474,7 +491,10 @@ impl<'a, 'b> Rust2Js<'a, 'b> {
|
||||
self.ret_expr = match *ty {
|
||||
Descriptor::Boolean => "return JS ? 1 : 0;".to_string(),
|
||||
Descriptor::Char => "return JS.codePointAt(0);".to_string(),
|
||||
_ => bail!("unsupported return type for calling JS function from Rust: {:?}", ty),
|
||||
_ => bail!(
|
||||
"unsupported return type for calling JS function from Rust: {:?}",
|
||||
ty
|
||||
),
|
||||
};
|
||||
Ok(())
|
||||
}
|
||||
@ -494,7 +514,9 @@ impl<'a, 'b> Rust2Js<'a, 'b> {
|
||||
|
||||
let mut invoc = if self.variadic {
|
||||
if self.js_arguments.is_empty() {
|
||||
return Err(failure::err_msg("a function with no arguments cannot be variadic"));
|
||||
return Err(failure::err_msg(
|
||||
"a function with no arguments cannot be variadic",
|
||||
));
|
||||
}
|
||||
let last_arg = self.js_arguments.len() - 1; // check implies >= 0
|
||||
if self.js_arguments.len() != 1 {
|
||||
@ -505,16 +527,12 @@ impl<'a, 'b> Rust2Js<'a, 'b> {
|
||||
invoc,
|
||||
self.js_arguments[..last_arg].join(", "),
|
||||
self.js_arguments[last_arg],
|
||||
)
|
||||
),
|
||||
)
|
||||
} else {
|
||||
self.ret_expr.replace(
|
||||
"JS",
|
||||
&format!(
|
||||
"{}(...{})",
|
||||
invoc,
|
||||
self.js_arguments[last_arg],
|
||||
)
|
||||
&format!("{}(...{})", invoc, self.js_arguments[last_arg],),
|
||||
)
|
||||
}
|
||||
} else {
|
||||
|
@ -1,8 +1,8 @@
|
||||
#![doc(html_root_url = "https://docs.rs/wasm-bindgen-cli-support/0.2")]
|
||||
|
||||
extern crate parity_wasm;
|
||||
extern crate wasm_bindgen_shared as shared;
|
||||
extern crate serde_json;
|
||||
extern crate wasm_bindgen_shared as shared;
|
||||
extern crate wasm_gc;
|
||||
#[macro_use]
|
||||
extern crate failure;
|
||||
@ -90,7 +90,7 @@ impl Bindgen {
|
||||
if let Some(module) = (&mut module as &mut Any).downcast_mut::<Module>() {
|
||||
let blank = Module::new(Vec::new());
|
||||
self.input = Input::Module(mem::replace(module, blank), name);
|
||||
return self
|
||||
return self;
|
||||
}
|
||||
|
||||
self.input = Input::Bytes(into_bytes(module), name);
|
||||
@ -213,7 +213,11 @@ impl Bindgen {
|
||||
cx.finalize(stem)?
|
||||
};
|
||||
|
||||
let extension = if self.nodejs_experimental_modules { "mjs" } else { "js" };
|
||||
let extension = if self.nodejs_experimental_modules {
|
||||
"mjs"
|
||||
} else {
|
||||
"js"
|
||||
};
|
||||
let js_path = out_dir.join(stem).with_extension(extension);
|
||||
fs::write(&js_path, reset_indentation(&js))
|
||||
.with_context(|_| format!("failed to write `{}`", js_path.display()))?;
|
||||
@ -251,12 +255,12 @@ impl Bindgen {
|
||||
|
||||
if self.nodejs_experimental_modules {
|
||||
for (i, module) in imports.iter().enumerate() {
|
||||
shim.push_str(&format!("import * as import{} from '{}';\n",
|
||||
i, module));
|
||||
shim.push_str(&format!("import * as import{} from '{}';\n", i, module));
|
||||
}
|
||||
// On windows skip the leading `/` which comes out when we parse a
|
||||
// url to use `C:\...` instead of `\C:\...`
|
||||
shim.push_str(&format!("
|
||||
shim.push_str(&format!(
|
||||
"
|
||||
import * as path from 'path';
|
||||
import * as fs from 'fs';
|
||||
import * as url from 'url';
|
||||
@ -267,12 +271,17 @@ impl Bindgen {
|
||||
file = file.substring(1);
|
||||
}}
|
||||
const bytes = fs.readFileSync(path.join(file, '{}'));
|
||||
", path.file_name().unwrap().to_str().unwrap()));
|
||||
",
|
||||
path.file_name().unwrap().to_str().unwrap()
|
||||
));
|
||||
} else {
|
||||
shim.push_str(&format!("
|
||||
shim.push_str(&format!(
|
||||
"
|
||||
const path = require('path').join(__dirname, '{}');
|
||||
const bytes = require('fs').readFileSync(path);
|
||||
", path.file_name().unwrap().to_str().unwrap()));
|
||||
",
|
||||
path.file_name().unwrap().to_str().unwrap()
|
||||
));
|
||||
}
|
||||
shim.push_str("let imports = {};\n");
|
||||
for (i, module) in imports.iter().enumerate() {
|
||||
@ -387,7 +396,11 @@ fn reset_indentation(s: &str) -> String {
|
||||
if line.starts_with('}') || (line.ends_with('}') && !line.starts_with('*')) {
|
||||
indent = indent.saturating_sub(1);
|
||||
}
|
||||
let extra = if line.starts_with(':') || line.starts_with('?') { 1 } else { 0 };
|
||||
let extra = if line.starts_with(':') || line.starts_with('?') {
|
||||
1
|
||||
} else {
|
||||
0
|
||||
};
|
||||
if !line.is_empty() {
|
||||
for _ in 0..indent + extra {
|
||||
dst.push_str(" ");
|
||||
@ -399,5 +412,5 @@ fn reset_indentation(s: &str) -> String {
|
||||
indent += 1;
|
||||
}
|
||||
}
|
||||
return dst
|
||||
return dst;
|
||||
}
|
||||
|
Reference in New Issue
Block a user