@@ -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
@@ -433,6 +436,8 @@ module Value = struct
433
436
let * t = Type. block_type in
434
437
return (W. ArrayNewFixed (t, [] ))
435
438
439
+ let dummy_closure = empty_struct
440
+
436
441
let as_block e =
437
442
let * t = Type. block_type in
438
443
let * e = e in
@@ -816,6 +821,11 @@ module Memory = struct
816
821
then 1
817
822
else (if include_closure_arity then 1 else 0 ) + if arity = 1 then 1 else 2
818
823
824
+ let cast_closure ~cps ~arity closure =
825
+ let arity = if cps then arity - 1 else arity in
826
+ let * ty = Type. closure_type ~usage: `Access ~cps arity in
827
+ wasm_cast ty closure
828
+
819
829
let load_function_pointer ~cps ~arity ?(skip_cast = false ) closure =
820
830
let arity = if cps then arity - 1 else arity in
821
831
let * ty = Type. closure_type ~usage: `Access ~cps arity in
@@ -1192,7 +1202,7 @@ module Closure = struct
1192
1202
if free_variable_count = 0
1193
1203
then
1194
1204
(* The closures are all constants and the environment is empty. *)
1195
- let * _ = add_var (Code.Var. fresh () ) in
1205
+ let * _ = add_var ~typ: Type. closure (Code.Var. fresh () ) in
1196
1206
return ()
1197
1207
else
1198
1208
let arity = List. assoc f info.functions in
@@ -1201,7 +1211,7 @@ module Closure = struct
1201
1211
match info.Closure_conversion. functions with
1202
1212
| [ _ ] ->
1203
1213
let * typ = Type. env_type ~cps ~arity free_variable_count in
1204
- let * _ = add_var f in
1214
+ let * _ = add_var ~typ: Type. closure f in
1205
1215
let env = Code.Var. fresh_n " env" in
1206
1216
let * () =
1207
1217
store
@@ -1222,7 +1232,7 @@ module Closure = struct
1222
1232
let * typ =
1223
1233
Type. rec_closure_type ~cps ~arity ~function_count ~free_variable_count
1224
1234
in
1225
- let * _ = add_var f in
1235
+ let * _ = add_var ~typ: Type. closure f in
1226
1236
let env = Code.Var. fresh_n " env" in
1227
1237
let * env_typ = Type. rec_env_type ~function_count ~free_variable_count in
1228
1238
let * () =
0 commit comments