@@ -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
@@ -906,7 +914,9 @@ module Memory = struct
906
914
in
907
915
let * ty = Type. int32_type in
908
916
let * e = e in
909
- return (W. StructNew (ty, [ GlobalGet int32_ops; e ]))
917
+ let e' = W. StructNew (ty, [ GlobalGet int32_ops; e ]) in
918
+ let * b = is_small_constant e in
919
+ if b then store_in_global e' else return e'
910
920
911
921
let box_int32 e = make_int32 ~kind: `Int32 e
912
922
@@ -924,7 +934,9 @@ module Memory = struct
924
934
in
925
935
let * ty = Type. int64_type in
926
936
let * e = e in
927
- return (W. StructNew (ty, [ GlobalGet int64_ops; e ]))
937
+ let e' = W. StructNew (ty, [ GlobalGet int64_ops; e ]) in
938
+ let * b = is_small_constant e in
939
+ if b then store_in_global e' else return e'
928
940
929
941
let box_int64 e = make_int64 e
930
942
@@ -944,11 +956,6 @@ module Constant = struct
944
956
strings are encoded as a sequence of bytes in the wasm module. *)
945
957
let string_length_threshold = 64
946
958
947
- let store_in_global ?(name = " const" ) c =
948
- let name = Code.Var. fresh_n name in
949
- let * () = register_global name { mut = false ; typ = Type. value } c in
950
- return (W. GlobalGet name)
951
-
952
959
let byte_string s =
953
960
let b = Buffer. create (String. length s) in
954
961
String. iter s ~f: (function
@@ -1081,13 +1088,15 @@ module Constant = struct
1081
1088
if b then return c else store_in_global c
1082
1089
| Const_named name -> store_in_global ~name c
1083
1090
| Mutated ->
1091
+ let * typ = Type. string_type in
1084
1092
let name = Code.Var. fresh_n " const" in
1093
+ let * placeholder = array_placeholder typ in
1085
1094
let * () =
1086
1095
register_global
1087
1096
~constant: true
1088
1097
name
1089
- { mut = true ; typ = Type. value }
1090
- ( W. RefI31 ( Const ( I32 0l )))
1098
+ { mut = true ; typ = Ref { nullable = false ; typ = Type typ } }
1099
+ placeholder
1091
1100
in
1092
1101
let * () = register_init_code (instr (W. GlobalSet (name, c))) in
1093
1102
return (W. GlobalGet name))
0 commit comments