@@ -27,6 +27,8 @@ let include_closure_arity = false
2727module Type = struct
2828 let value = W. Ref { nullable = false ; typ = Eq }
2929
30+ let closure = W. Ref { nullable = false ; typ = Struct }
31+
3032 let block_type =
3133 register_type " block" (fun () ->
3234 return
@@ -205,7 +207,8 @@ module Type = struct
205207 let primitive_type n =
206208 { W. params = List. init ~len: n ~f: (fun _ -> value); result = [ value ] }
207209
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 ] }
209212
210213 let function_type ~cps n =
211214 let n = if cps then n + 1 else n in
@@ -433,6 +436,8 @@ module Value = struct
433436 let * t = Type. block_type in
434437 return (W. ArrayNewFixed (t, [] ))
435438
439+ let dummy_closure = empty_struct
440+
436441 let as_block e =
437442 let * t = Type. block_type in
438443 let * e = e in
@@ -817,6 +822,11 @@ module Memory = struct
817822 then 1
818823 else (if include_closure_arity then 1 else 0 ) + if arity = 1 then 1 else 2
819824
825+ let cast_closure ~cps ~arity closure =
826+ let arity = if cps then arity - 1 else arity in
827+ let * ty = Type. closure_type ~usage: `Access ~cps arity in
828+ wasm_cast ty closure
829+
820830 let load_function_pointer ~cps ~arity ?(skip_cast = false ) closure =
821831 let arity = if cps then arity - 1 else arity in
822832 let * ty = Type. closure_type ~usage: `Access ~cps arity in
@@ -1196,7 +1206,7 @@ module Closure = struct
11961206 if free_variable_count = 0
11971207 then
11981208 (* The closures are all constants and the environment is empty. *)
1199- let * _ = add_var (Code.Var. fresh () ) in
1209+ let * _ = add_var ~typ: Type. closure (Code.Var. fresh () ) in
12001210 return ()
12011211 else
12021212 let arity = List. assoc f info.functions in
@@ -1205,7 +1215,7 @@ module Closure = struct
12051215 match info.Closure_conversion. functions with
12061216 | [ _ ] ->
12071217 let * typ = Type. env_type ~cps ~arity free_variable_count in
1208- let * _ = add_var f in
1218+ let * _ = add_var ~typ: Type. closure f in
12091219 let env = Code.Var. fresh_n " env" in
12101220 let * () =
12111221 store
@@ -1226,7 +1236,7 @@ module Closure = struct
12261236 let * typ =
12271237 Type. rec_closure_type ~cps ~arity ~function_count ~free_variable_count
12281238 in
1229- let * _ = add_var f in
1239+ let * _ = add_var ~typ: Type. closure f in
12301240 let env = Code.Var. fresh_n " env" in
12311241 let * env_typ = Type. rec_env_type ~function_count ~free_variable_count in
12321242 let * () =
0 commit comments