diff --git a/crates/cli-support/src/descriptor.rs b/crates/cli-support/src/descriptor.rs index 0ec60db8e..bd6bc25f7 100644 --- a/crates/cli-support/src/descriptor.rs +++ b/crates/cli-support/src/descriptor.rs @@ -48,7 +48,7 @@ pub enum Descriptor { F64, Boolean, Function(Box), - Closure(Box, bool), + Closure(Box), Ref(Box), RefMut(Box), Slice(Box), @@ -65,6 +65,12 @@ pub struct Function { pub ret: Option, } +#[derive(Debug)] +pub struct Closure { + pub function: Function, + pub mutable: bool, +} + #[derive(Copy, Clone)] pub enum VectorKind { I8, @@ -100,11 +106,7 @@ impl Descriptor { F64 => Descriptor::F64, BOOLEAN => Descriptor::Boolean, FUNCTION => Descriptor::Function(Box::new(Function::decode(data))), - CLOSURE => { - let mutable = get(data) == REFMUT; - assert_eq!(get(data), FUNCTION); - Descriptor::Closure(Box::new(Function::decode(data)), mutable) - } + CLOSURE => Descriptor::Closure(Box::new(Closure::decode(data))), REF => Descriptor::Ref(Box::new(Descriptor::_decode(data))), REFMUT => Descriptor::RefMut(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 { Descriptor::Ref(ref s) => s.closure(), _ => None, } } - pub fn closure(&self) -> Option<(&Function, bool)> { + pub fn closure(&self) -> Option<&Closure> { match *self { - Descriptor::Closure(ref s, mutable) => Some((s, mutable)), + Descriptor::Closure(ref s) => Some(s), _ => None, } } @@ -240,6 +242,17 @@ fn get(a: &mut &[u32]) -> u32 { 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 { fn decode(data: &mut &[u32]) -> Function { let arguments = (0..get(data)) diff --git a/crates/cli-support/src/js/rust2js.rs b/crates/cli-support/src/js/rust2js.rs index e9412d7cb..111aca481 100644 --- a/crates/cli-support/src/js/rust2js.rs +++ b/crates/cli-support/src/js/rust2js.rs @@ -140,10 +140,10 @@ impl<'a, 'b> Rust2Js<'a, 'b> { return } - if let Some((f, mutable)) = arg.ref_closure() { + if let Some(closure) = arg.ref_closure() { let (js, _ts) = { let mut builder = Js2Rust::new("", self.cx); - if mutable { + if closure.mutable { builder.prelude("let a = this.a;\n") .prelude("this.a = 0;\n") .rust_argument("a") @@ -153,7 +153,7 @@ impl<'a, 'b> Rust2Js<'a, 'b> { } builder .rust_argument("this.b") - .process(f) + .process(&closure.function) .finish("function", "this.f") }; self.cx.expose_get_global_argument();