| 1 | @[has_globals] |
| 2 | module main |
| 3 | |
| 4 | $if windows { |
| 5 | #include <synchapi.h> |
| 6 | |
| 7 | struct C.builtin__closure__ClosureMutex { |
| 8 | closure_mtx C.SRWLOCK |
| 9 | } |
| 10 | } $else { |
| 11 | #include <pthread.h> |
| 12 | |
| 13 | @[typedef] |
| 14 | pub struct C.pthread_mutex_t {} |
| 15 | |
| 16 | struct C.builtin__closure__ClosureMutex { |
| 17 | closure_mtx C.pthread_mutex_t |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | struct C.builtin__closure__Closure { |
| 22 | ClosureMutex C.builtin__closure__ClosureMutex |
| 23 | closure_ptr voidptr |
| 24 | closure_get_data voidptr |
| 25 | closure_cap int |
| 26 | v_page_size int |
| 27 | free_closure_ptr voidptr |
| 28 | } |
| 29 | |
| 30 | __global C.g_closure C.builtin__closure__Closure |
| 31 | |
| 32 | fn foo(i int) fn () int { |
| 33 | return fn [i] () int { |
| 34 | return i |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | fn test_direct_call_of_returned_closure_reuses_closure_slot() { |
| 39 | assert foo(-1)() == -1 |
| 40 | start_closure_cap := C.g_closure.closure_cap |
| 41 | mut sum := 0 |
| 42 | for i in 0 .. 128 { |
| 43 | sum += foo(i)() |
| 44 | } |
| 45 | assert sum == 8128 |
| 46 | assert C.g_closure.closure_cap == start_closure_cap |
| 47 | } |
| 48 | |