Skip to content

Commit 0881866

Browse files
committed
Support static blocks in blocks runtime
1 parent 7c1234b commit 0881866

5 files changed

Lines changed: 31 additions & 6 deletions

File tree

CHANGES.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## dev
2+
3+
* Support struct as method argument or return type
4+
* Support static blocks in blocks runtime
5+
16
## v0.3.1
27

38
* Improved libdispatch bindings

dispatch/blocks_runtime/blocks_runtime.ml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,15 @@ let make_descriptor ~size:s =
1212
let global_block_descriptor =
1313
make_descriptor ~size:(sizeof Block_layout.t) |> allocate Block_descriptor.t
1414

15-
let make_block ~invoke:f =
15+
let make_block ?(is_global = true) f =
1616
let open Block_layout in
17-
let b = make t in
18-
setf b isa (to_voidp _NSConcreteGlobalBlock);
19-
setf b flags _IS_GLOBAL;
17+
let b = make t
18+
and (btype, bflags) =
19+
if is_global then (_NSConcreteGlobalBlock, _IS_GLOBAL)
20+
else (_NSConcreteStackBlock, Int32.zero)
21+
in
22+
setf b isa (to_voidp btype);
23+
setf b flags bflags;
2024
setf b descriptor global_block_descriptor;
2125
setf b invoke f;
2226
allocate t b |> to_voidp

dispatch/blocks_runtime/function_description.ml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ module Functions (F : Ctypes.FOREIGN) = struct
1010
let _NSConcreteGlobalBlock =
1111
foreign_value "_NSConcreteGlobalBlock" (array 32 (ptr void))
1212

13+
let _NSConcreteStackBlock =
14+
foreign_value "_NSConcreteStackBlock" (array 32 (ptr void))
15+
1316
(** Create a heap based copy of a Block or simply add a reference to an
1417
existing one. This must be paired with Block_release to recover memory *)
1518
let _Block_copy = foreign "_Block_copy" (block_t @-> returning block_t)

test/test-camlkit-base/test_objc.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ let test_struct_t () =
343343
~cmd: (selector "someMethodWithStruct:")
344344
~args: Objc_type.[struc CGPoint.t]
345345
~return: Objc_type.void
346-
(fun _self _cmd rect -> x := CGPoint.x rect)
346+
(fun _self _cmd pt -> x := CGPoint.x pt)
347347
]
348348
in
349349
let obj = _new_ my_class

test/test-dispatch/test_dispatch.ml

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,19 @@ let test_block =
3535
Alcotest.(check bool "function called" true true))
3636
@@ fun f ->
3737
async' ~queue:main_queue
38-
(make_block ~invoke:(coerce Block_fun.t (ptr void) f))
38+
(make_block (coerce Block_fun.t (ptr void) f))
39+
40+
let test_static_block =
41+
"dispatch static block with async'" -: fun () ->
42+
let module Block_fun =
43+
(val Foreign.dynamic_funptr ~thread_registration:true ~runtime_lock:true
44+
(void @-> returning void))
45+
in
46+
Block_fun.with_fun (fun () ->
47+
Alcotest.(check bool "function called" true true))
48+
@@ fun f ->
49+
async' ~queue:main_queue
50+
(make_block ~is_global:false (coerce Block_fun.t (ptr void) f))
3951

4052
let test_dispatch_after =
4153
"time and after" -: fun () ->
@@ -89,6 +101,7 @@ let () =
89101
; test_queue_concurrent
90102
; test_queue_serial
91103
; test_block
104+
; test_static_block
92105
; test_dispatch_after
93106
; test_with_delay
94107
; test_async_and_wait

0 commit comments

Comments
 (0)