@@ -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
@@ -818,6 +823,11 @@ module Memory = struct
818
823
then 1
819
824
else (if include_closure_arity then 1 else 0 ) + if arity = 1 then 1 else 2
820
825
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
+
821
831
let load_function_pointer ~cps ~arity ?(skip_cast = false ) closure =
822
832
let arity = if cps then arity - 1 else arity in
823
833
let * ty = Type. closure_type ~usage: `Access ~cps arity in
@@ -1197,7 +1207,7 @@ module Closure = struct
1197
1207
if free_variable_count = 0
1198
1208
then
1199
1209
(* 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
1201
1211
return ()
1202
1212
else
1203
1213
let _, arity = List. find ~f: (fun (f' , _ ) -> Code.Var. equal f f') info.functions in
@@ -1206,7 +1216,7 @@ module Closure = struct
1206
1216
match info.Closure_conversion. functions with
1207
1217
| [ _ ] ->
1208
1218
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
1210
1220
let env = Code.Var. fresh_n " env" in
1211
1221
let * () =
1212
1222
store
@@ -1227,7 +1237,7 @@ module Closure = struct
1227
1237
let * typ =
1228
1238
Type. rec_closure_type ~cps ~arity ~function_count ~free_variable_count
1229
1239
in
1230
- let * _ = add_var f in
1240
+ let * _ = add_var ~typ: Type. closure f in
1231
1241
let env = Code.Var. fresh_n " env" in
1232
1242
let * env_typ = Type. rec_env_type ~function_count ~free_variable_count in
1233
1243
let * () =
0 commit comments