@@ -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 
@@ -430,6 +433,8 @@ module Value = struct
430433    let *  t =  Type. block_type in 
431434    array_placeholder t
432435
436+   let  dummy_closure =  empty_struct
437+ 
433438  let  as_block  e  = 
434439    let *  t =  Type. block_type in 
435440    let *  e =  e in 
@@ -811,6 +816,11 @@ module Memory = struct
811816    then  1 
812817    else  (if  include_closure_arity then  1  else  0 ) +  if  arity =  1  then  1  else  2 
813818
819+   let  cast_closure  ~cps   ~arity   closure  = 
820+     let  arity =  if  cps then  arity -  1  else  arity in 
821+     let *  ty =  Type. closure_type ~usage: `Access  ~cps  arity in 
822+     wasm_cast ty closure
823+ 
814824  let  load_function_pointer  ~cps   ~arity   ?(skip_cast  = false )  closure  = 
815825    let  arity =  if  cps then  arity -  1  else  arity in 
816826    let *  ty =  Type. closure_type ~usage: `Access  ~cps  arity in 
@@ -1189,7 +1199,7 @@ module Closure = struct
11891199    if  free_variable_count =  0 
11901200    then 
11911201      (*  The closures are all constants and the environment is empty. *) 
1192-       let *  _ =  add_var (Code.Var. fresh () ) in 
1202+       let *  _ =  add_var ~typ: Type. closure  (Code.Var. fresh () ) in 
11931203      return () 
11941204    else 
11951205      let  env_type_id =  Option. value ~default: (- 1 ) info.id in 
@@ -1199,7 +1209,7 @@ module Closure = struct
11991209      match  info.Closure_conversion. functions with 
12001210      |  [ _ ] ->
12011211          let *  typ =  Type. env_type ~cps  ~arity  ~env_type_id  ~env_type: []  in 
1202-           let *  _ =  add_var f in 
1212+           let *  _ =  add_var ~typ: Type. closure  f in 
12031213          let  env =  Code.Var. fresh_n " env" in 
12041214          let *  ()  = 
12051215            store
@@ -1220,7 +1230,7 @@ module Closure = struct
12201230          let *  typ = 
12211231            Type. rec_closure_type ~cps  ~arity  ~function_count  ~env_type_id  ~env_type: [] 
12221232          in 
1223-           let *  _ =  add_var f in 
1233+           let *  _ =  add_var ~typ: Type. closure  f in 
12241234          let  env =  Code.Var. fresh_n " env" in 
12251235          let *  env_typ =  Type. rec_env_type ~function_count  ~env_type_id  ~env_type: []  in 
12261236          let *  ()  = 
0 commit comments