@@ -514,6 +514,44 @@ let should_make_global x st = Var.Set.mem x st.context.globalized_variables, st
514514
515515let value_type st = st.context.value_type, st
516516
517+ let get_constant x st = Var.Hashtbl. find_opt st.context.constants x, st
518+
519+ let placeholder_value typ f =
520+ let * c = get_constant typ in
521+ match c with
522+ | None ->
523+ let x = Var. fresh () in
524+ let * () = register_constant typ (W. GlobalGet x) in
525+ let * () =
526+ register_global
527+ ~constant: true
528+ x
529+ { mut = false ; typ = Ref { nullable = false ; typ = Type typ } }
530+ (f typ)
531+ in
532+ return (W. GlobalGet x)
533+ | Some c -> return c
534+
535+ let array_placeholder typ = placeholder_value typ (fun typ -> ArrayNewFixed (typ, [] ))
536+
537+ let default_value val_typ st =
538+ match val_typ with
539+ | W. Ref { typ = I31 | Eq | Any ; _ } -> (W. RefI31 (Const (I32 0l )), val_typ, None ), st
540+ | W. Ref { typ = Type typ ; nullable = false } -> (
541+ match (Var.Hashtbl. find st.context.types typ).typ with
542+ | Array _ ->
543+ (let * placeholder = array_placeholder typ in
544+ return (placeholder, val_typ, None ))
545+ st
546+ | Struct _ | Func _ ->
547+ ( ( W. RefNull (Type typ)
548+ , W. Ref { typ = Type typ; nullable = true }
549+ , Some { W. typ = Type typ; nullable = false } )
550+ , st ))
551+ | W. Ref { nullable = true ; _ }
552+ | W. Ref { typ = Func | Extern | Struct | Array | None_ ; _ }
553+ | I32 | I64 | F32 | F64 -> assert false
554+
517555let rec store ?(always = false ) ?typ x e =
518556 let * e = e in
519557 match e with
0 commit comments