mirror of
https://github.com/rustwasm/wasm-bindgen.git
synced 2025-01-04 10:24:27 +03:00
Use a struct instead of a bool variant
Helps it be a bit more readable!
This commit is contained in:
parent
0e032955fb
commit
f80a7067a0
@ -48,7 +48,7 @@ pub enum Descriptor {
|
|||||||
F64,
|
F64,
|
||||||
Boolean,
|
Boolean,
|
||||||
Function(Box<Function>),
|
Function(Box<Function>),
|
||||||
Closure(Box<Function>, bool),
|
Closure(Box<Closure>),
|
||||||
Ref(Box<Descriptor>),
|
Ref(Box<Descriptor>),
|
||||||
RefMut(Box<Descriptor>),
|
RefMut(Box<Descriptor>),
|
||||||
Slice(Box<Descriptor>),
|
Slice(Box<Descriptor>),
|
||||||
@ -65,6 +65,12 @@ pub struct Function {
|
|||||||
pub ret: Option<Descriptor>,
|
pub ret: Option<Descriptor>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct Closure {
|
||||||
|
pub function: Function,
|
||||||
|
pub mutable: bool,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Copy, Clone)]
|
#[derive(Copy, Clone)]
|
||||||
pub enum VectorKind {
|
pub enum VectorKind {
|
||||||
I8,
|
I8,
|
||||||
@ -100,11 +106,7 @@ impl Descriptor {
|
|||||||
F64 => Descriptor::F64,
|
F64 => Descriptor::F64,
|
||||||
BOOLEAN => Descriptor::Boolean,
|
BOOLEAN => Descriptor::Boolean,
|
||||||
FUNCTION => Descriptor::Function(Box::new(Function::decode(data))),
|
FUNCTION => Descriptor::Function(Box::new(Function::decode(data))),
|
||||||
CLOSURE => {
|
CLOSURE => Descriptor::Closure(Box::new(Closure::decode(data))),
|
||||||
let mutable = get(data) == REFMUT;
|
|
||||||
assert_eq!(get(data), FUNCTION);
|
|
||||||
Descriptor::Closure(Box::new(Function::decode(data)), mutable)
|
|
||||||
}
|
|
||||||
REF => Descriptor::Ref(Box::new(Descriptor::_decode(data))),
|
REF => Descriptor::Ref(Box::new(Descriptor::_decode(data))),
|
||||||
REFMUT => Descriptor::RefMut(Box::new(Descriptor::_decode(data))),
|
REFMUT => Descriptor::RefMut(Box::new(Descriptor::_decode(data))),
|
||||||
SLICE => Descriptor::Slice(Box::new(Descriptor::_decode(data))),
|
SLICE => Descriptor::Slice(Box::new(Descriptor::_decode(data))),
|
||||||
@ -153,16 +155,16 @@ impl Descriptor {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn ref_closure(&self) -> Option<(&Function, bool)> {
|
pub fn ref_closure(&self) -> Option<&Closure> {
|
||||||
match *self {
|
match *self {
|
||||||
Descriptor::Ref(ref s) => s.closure(),
|
Descriptor::Ref(ref s) => s.closure(),
|
||||||
_ => None,
|
_ => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn closure(&self) -> Option<(&Function, bool)> {
|
pub fn closure(&self) -> Option<&Closure> {
|
||||||
match *self {
|
match *self {
|
||||||
Descriptor::Closure(ref s, mutable) => Some((s, mutable)),
|
Descriptor::Closure(ref s) => Some(s),
|
||||||
_ => None,
|
_ => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -240,6 +242,17 @@ fn get(a: &mut &[u32]) -> u32 {
|
|||||||
ret
|
ret
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Closure {
|
||||||
|
fn decode(data: &mut &[u32]) -> Closure {
|
||||||
|
let mutable = get(data) == REFMUT;
|
||||||
|
assert_eq!(get(data), FUNCTION);
|
||||||
|
Closure {
|
||||||
|
mutable,
|
||||||
|
function: Function::decode(data),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Function {
|
impl Function {
|
||||||
fn decode(data: &mut &[u32]) -> Function {
|
fn decode(data: &mut &[u32]) -> Function {
|
||||||
let arguments = (0..get(data))
|
let arguments = (0..get(data))
|
||||||
|
@ -140,10 +140,10 @@ impl<'a, 'b> Rust2Js<'a, 'b> {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some((f, mutable)) = arg.ref_closure() {
|
if let Some(closure) = arg.ref_closure() {
|
||||||
let (js, _ts) = {
|
let (js, _ts) = {
|
||||||
let mut builder = Js2Rust::new("", self.cx);
|
let mut builder = Js2Rust::new("", self.cx);
|
||||||
if mutable {
|
if closure.mutable {
|
||||||
builder.prelude("let a = this.a;\n")
|
builder.prelude("let a = this.a;\n")
|
||||||
.prelude("this.a = 0;\n")
|
.prelude("this.a = 0;\n")
|
||||||
.rust_argument("a")
|
.rust_argument("a")
|
||||||
@ -153,7 +153,7 @@ impl<'a, 'b> Rust2Js<'a, 'b> {
|
|||||||
}
|
}
|
||||||
builder
|
builder
|
||||||
.rust_argument("this.b")
|
.rust_argument("this.b")
|
||||||
.process(f)
|
.process(&closure.function)
|
||||||
.finish("function", "this.f")
|
.finish("function", "this.f")
|
||||||
};
|
};
|
||||||
self.cx.expose_get_global_argument();
|
self.cx.expose_get_global_argument();
|
||||||
|
Loading…
Reference in New Issue
Block a user