Use a struct instead of a bool variant

Helps it be a bit more readable!
This commit is contained in:
Alex Crichton 2018-04-17 11:29:03 -07:00
parent 0e032955fb
commit f80a7067a0
2 changed files with 25 additions and 12 deletions

View File

@ -48,7 +48,7 @@ pub enum Descriptor {
F64,
Boolean,
Function(Box<Function>),
Closure(Box<Function>, bool),
Closure(Box<Closure>),
Ref(Box<Descriptor>),
RefMut(Box<Descriptor>),
Slice(Box<Descriptor>),
@ -65,6 +65,12 @@ pub struct Function {
pub ret: Option<Descriptor>,
}
#[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))

View File

@ -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();