@@ -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
@@ -818,6 +823,11 @@ module Memory = struct
818823 then 1
819824 else (if include_closure_arity then 1 else 0 ) + if arity = 1 then 1 else 2
820825
826+ let cast_closure ~cps ~arity closure =
827+ let arity = if cps then arity - 1 else arity in
828+ let * ty = Type. closure_type ~usage: `Access ~cps arity in
829+ wasm_cast ty closure
830+
821831 let load_function_pointer ~cps ~arity ?(skip_cast = false ) closure =
822832 let arity = if cps then arity - 1 else arity in
823833 let * ty = Type. closure_type ~usage: `Access ~cps arity in
@@ -1197,7 +1207,7 @@ module Closure = struct
11971207 if free_variable_count = 0
11981208 then
11991209 (* The closures are all constants and the environment is empty. *)
1200- let * _ = add_var (Code.Var. fresh () ) in
1210+ let * _ = add_var ~typ: Type. closure (Code.Var. fresh () ) in
12011211 return ()
12021212 else
12031213 let _, arity = List. find ~f: (fun (f' , _ ) -> Code.Var. equal f f') info.functions in
@@ -1206,7 +1216,7 @@ module Closure = struct
12061216 match info.Closure_conversion. functions with
12071217 | [ _ ] ->
12081218 let * typ = Type. env_type ~cps ~arity free_variable_count in
1209- let * _ = add_var f in
1219+ let * _ = add_var ~typ: Type. closure f in
12101220 let env = Code.Var. fresh_n " env" in
12111221 let * () =
12121222 store
@@ -1227,7 +1237,7 @@ module Closure = struct
12271237 let * typ =
12281238 Type. rec_closure_type ~cps ~arity ~function_count ~free_variable_count
12291239 in
1230- let * _ = add_var f in
1240+ let * _ = add_var ~typ: Type. closure f in
12311241 let env = Code.Var. fresh_n " env" in
12321242 let * env_typ = Type. rec_env_type ~function_count ~free_variable_count in
12331243 let * () =
0 commit comments