@@ -629,6 +629,14 @@ module Value = struct
629
629
let int_asr = Arith. ( asr )
630
630
end
631
631
632
+ let store_in_global ?(name = " const" ) c =
633
+ let name = Code.Var. fresh_n name in
634
+ let * typ = expression_type c in
635
+ let * () =
636
+ register_global name { mut = false ; typ = Option. value ~default: Type. value typ } c
637
+ in
638
+ return (W. GlobalGet name)
639
+
632
640
module Memory = struct
633
641
let wasm_cast ty e =
634
642
let * e = e in
@@ -885,7 +893,9 @@ module Memory = struct
885
893
in
886
894
let * ty = Type. int32_type in
887
895
let * e = e in
888
- return (W. StructNew (ty, [ GlobalGet int32_ops; e ]))
896
+ let e' = W. StructNew (ty, [ GlobalGet int32_ops; e ]) in
897
+ let * b = is_small_constant e in
898
+ if b then store_in_global e' else return e'
889
899
890
900
let box_int32 e = make_int32 ~kind: `Int32 e
891
901
@@ -903,7 +913,9 @@ module Memory = struct
903
913
in
904
914
let * ty = Type. int64_type in
905
915
let * e = e in
906
- return (W. StructNew (ty, [ GlobalGet int64_ops; e ]))
916
+ let e' = W. StructNew (ty, [ GlobalGet int64_ops; e ]) in
917
+ let * b = is_small_constant e in
918
+ if b then store_in_global e' else return e'
907
919
908
920
let box_int64 e = make_int64 e
909
921
@@ -923,11 +935,6 @@ module Constant = struct
923
935
strings are encoded as a sequence of bytes in the wasm module. *)
924
936
let string_length_threshold = 64
925
937
926
- let store_in_global ?(name = " const" ) c =
927
- let name = Code.Var. fresh_n name in
928
- let * () = register_global name { mut = false ; typ = Type. value } c in
929
- return (W. GlobalGet name)
930
-
931
938
let byte_string s =
932
939
let b = Buffer. create (String. length s) in
933
940
String. iter s ~f: (function
@@ -1060,13 +1067,15 @@ module Constant = struct
1060
1067
if b then return c else store_in_global c
1061
1068
| Const_named name -> store_in_global ~name c
1062
1069
| Mutated ->
1070
+ let * typ = Type. string_type in
1063
1071
let name = Code.Var. fresh_n " const" in
1072
+ let * placeholder = array_placeholder typ in
1064
1073
let * () =
1065
1074
register_global
1066
1075
~constant: true
1067
1076
name
1068
- { mut = true ; typ = Type. value }
1069
- ( W. RefI31 ( Const ( I32 0l )))
1077
+ { mut = true ; typ = Ref { nullable = false ; typ = Type typ } }
1078
+ placeholder
1070
1079
in
1071
1080
let * () = register_init_code (instr (W. GlobalSet (name, c))) in
1072
1081
return (W. GlobalGet name))
0 commit comments