@@ -27,6 +27,8 @@ let include_closure_arity = false
27
27
module Type = struct
28
28
let value = W. Ref { nullable = false ; typ = Eq }
29
29
30
+ let closure = W. Ref { nullable = false ; typ = Struct }
31
+
30
32
let block_type =
31
33
register_type " block" (fun () ->
32
34
return
@@ -205,7 +207,8 @@ module Type = struct
205
207
let primitive_type n =
206
208
{ W. params = List. init ~len: n ~f: (fun _ -> value); result = [ value ] }
207
209
208
- let func_type n = primitive_type (n + 1 )
210
+ let func_type n =
211
+ { W. params = List. init ~len: n ~f: (fun _ -> value) @ [ closure ]; result = [ value ] }
209
212
210
213
let function_type ~cps n =
211
214
let n = if cps then n + 1 else n in
@@ -430,6 +433,8 @@ module Value = struct
430
433
let * t = Type. block_type in
431
434
array_placeholder t
432
435
436
+ let dummy_closure = empty_struct
437
+
433
438
let as_block e =
434
439
let * t = Type. block_type in
435
440
let * e = e in
@@ -811,6 +816,11 @@ module Memory = struct
811
816
then 1
812
817
else (if include_closure_arity then 1 else 0 ) + if arity = 1 then 1 else 2
813
818
819
+ let cast_closure ~cps ~arity closure =
820
+ let arity = if cps then arity - 1 else arity in
821
+ let * ty = Type. closure_type ~usage: `Access ~cps arity in
822
+ wasm_cast ty closure
823
+
814
824
let load_function_pointer ~cps ~arity ?(skip_cast = false ) closure =
815
825
let arity = if cps then arity - 1 else arity in
816
826
let * ty = Type. closure_type ~usage: `Access ~cps arity in
@@ -1189,7 +1199,7 @@ module Closure = struct
1189
1199
if free_variable_count = 0
1190
1200
then
1191
1201
(* The closures are all constants and the environment is empty. *)
1192
- let * _ = add_var (Code.Var. fresh () ) in
1202
+ let * _ = add_var ~typ: Type. closure (Code.Var. fresh () ) in
1193
1203
return ()
1194
1204
else
1195
1205
let env_type_id = Option. value ~default: (- 1 ) info.id in
@@ -1199,7 +1209,7 @@ module Closure = struct
1199
1209
match info.Closure_conversion. functions with
1200
1210
| [ _ ] ->
1201
1211
let * typ = Type. env_type ~cps ~arity ~env_type_id ~env_type: [] in
1202
- let * _ = add_var f in
1212
+ let * _ = add_var ~typ: Type. closure f in
1203
1213
let env = Code.Var. fresh_n " env" in
1204
1214
let * () =
1205
1215
store
@@ -1220,7 +1230,7 @@ module Closure = struct
1220
1230
let * typ =
1221
1231
Type. rec_closure_type ~cps ~arity ~function_count ~env_type_id ~env_type: []
1222
1232
in
1223
- let * _ = add_var f in
1233
+ let * _ = add_var ~typ: Type. closure f in
1224
1234
let env = Code.Var. fresh_n " env" in
1225
1235
let * env_typ = Type. rec_env_type ~function_count ~env_type_id ~env_type: [] in
1226
1236
let * () =
0 commit comments