@@ -640,6 +640,14 @@ module Value = struct
640
640
let int_asr = Arith. ( asr )
641
641
end
642
642
643
+ let store_in_global ?(name = " const" ) c =
644
+ let name = Code.Var. fresh_n name in
645
+ let * typ = expression_type c in
646
+ let * () =
647
+ register_global name { mut = false ; typ = Option. value ~default: Type. value typ } c
648
+ in
649
+ return (W. GlobalGet name)
650
+
643
651
module Memory = struct
644
652
let wasm_cast ty e =
645
653
let * e = e in
@@ -888,7 +896,9 @@ module Memory = struct
888
896
in
889
897
let * ty = Type. int32_type in
890
898
let * e = e in
891
- return (W. StructNew (ty, [ GlobalGet int32_ops; e ]))
899
+ let e' = W. StructNew (ty, [ GlobalGet int32_ops; e ]) in
900
+ let * b = is_small_constant e in
901
+ if b then store_in_global e' else return e'
892
902
893
903
let box_int32 e = make_int32 ~kind: `Int32 e
894
904
@@ -906,7 +916,9 @@ module Memory = struct
906
916
in
907
917
let * ty = Type. int64_type in
908
918
let * e = e in
909
- return (W. StructNew (ty, [ GlobalGet int64_ops; e ]))
919
+ let e' = W. StructNew (ty, [ GlobalGet int64_ops; e ]) in
920
+ let * b = is_small_constant e in
921
+ if b then store_in_global e' else return e'
910
922
911
923
let box_int64 e = make_int64 e
912
924
@@ -926,11 +938,6 @@ module Constant = struct
926
938
strings are encoded as a sequence of bytes in the wasm module. *)
927
939
let string_length_threshold = 64
928
940
929
- let store_in_global ?(name = " const" ) c =
930
- let name = Code.Var. fresh_n name in
931
- let * () = register_global name { mut = false ; typ = Type. value } c in
932
- return (W. GlobalGet name)
933
-
934
941
let byte_string s =
935
942
let b = Buffer. create (String. length s) in
936
943
String. iter s ~f: (function
@@ -1066,13 +1073,15 @@ module Constant = struct
1066
1073
if b then return c else store_in_global c
1067
1074
| Const_named name -> store_in_global ~name c
1068
1075
| Mutated ->
1076
+ let * typ = Type. string_type in
1069
1077
let name = Code.Var. fresh_n " const" in
1078
+ let * placeholder = array_placeholder typ in
1070
1079
let * () =
1071
1080
register_global
1072
1081
~constant: true
1073
1082
name
1074
- { mut = true ; typ = Type. value }
1075
- ( W. RefI31 ( Const ( I32 0l )))
1083
+ { mut = true ; typ = Ref { nullable = false ; typ = Type typ } }
1084
+ placeholder
1076
1085
in
1077
1086
let * () = register_init_code (instr (W. GlobalSet (name, c))) in
1078
1087
return (W. GlobalGet name))
0 commit comments