@@ -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
@@ -816,6 +821,11 @@ module Memory = struct
816821 then 1
817822 else (if include_closure_arity then 1 else 0 ) + if arity = 1 then 1 else 2
818823
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+
819829 let load_function_pointer ~cps ~arity ?(skip_cast = false ) closure =
820830 let arity = if cps then arity - 1 else arity in
821831 let * ty = Type. closure_type ~usage: `Access ~cps arity in
@@ -1192,7 +1202,7 @@ module Closure = struct
11921202 if free_variable_count = 0
11931203 then
11941204 (* 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
11961206 return ()
11971207 else
11981208 let arity = List. assoc f info.functions in
@@ -1201,7 +1211,7 @@ module Closure = struct
12011211 match info.Closure_conversion. functions with
12021212 | [ _ ] ->
12031213 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
12051215 let env = Code.Var. fresh_n " env" in
12061216 let * () =
12071217 store
@@ -1222,7 +1232,7 @@ module Closure = struct
12221232 let * typ =
12231233 Type. rec_closure_type ~cps ~arity ~function_count ~free_variable_count
12241234 in
1225- let * _ = add_var f in
1235+ let * _ = add_var ~typ: Type. closure f in
12261236 let env = Code.Var. fresh_n " env" in
12271237 let * env_typ = Type. rec_env_type ~function_count ~free_variable_count in
12281238 let * () =
0 commit comments