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