Skip to content

Commit

Permalink
Add sys::Closure type
Browse files Browse the repository at this point in the history
  • Loading branch information
brson committed Aug 29, 2012
1 parent ec9c68c commit 6e20ffe
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/libcore/sys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ enum TypeDesc = {
// Remaining fields not listed
};

/// The representation of a Rust closure
struct Closure {
code: *();
env: *();
}

#[abi = "cdecl"]
extern mod rustrt {
pure fn shape_log_str(t: *sys::TypeDesc, data: *()) -> ~str;
Expand Down Expand Up @@ -138,6 +144,27 @@ mod tests {
assert pref_align_of::<uint>() == 8u;
assert pref_align_of::<*uint>() == 8u;
}

#[test]
fn synthesize_closure() unsafe {
let x = 10;
let f: fn(int) -> int = |y| x + y;

assert f(20) == 30;

let original_closure: Closure = unsafe::transmute(f);

let actual_function_pointer = original_closure.code;
let environment = original_closure.env;

let new_closure = Closure {
code: actual_function_pointer,
env: environment
};

let new_f: fn(int) -> int = unsafe::transmute(new_closure);
assert new_f(20) == 30;
}
}

// Local Variables:
Expand Down

0 comments on commit 6e20ffe

Please sign in to comment.