Skip to content

Commit c29ad69

Browse files
committed
detect array like records
1 parent cd2cec4 commit c29ad69

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

jscomp/core/js_dump.ml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -816,7 +816,10 @@ and expression_desc cxt ~(level:int) f x : cxt =
816816
fields el
817817
(fun x -> Js_op.Lit (Ext_ident.convert x) ))))
818818
(*name convention of Record is slight different from modules*)
819-
| Caml_block(el,_, _, Blk_record fields) ->
819+
| Caml_block(el,mutable_flag, _, Blk_record fields) ->
820+
if Ext_array.for_alli fields (fun i v -> string_of_int i = v) then
821+
expression_desc cxt ~level f (Array (el, mutable_flag))
822+
else
820823
expression_desc cxt ~level f (Object
821824
((Ext_list.combine_array fields el (fun i -> Js_op.Lit i))))
822825

jscomp/ext/ext_array.ml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,14 @@ let rec unsafe_loop index len p xs ys =
234234
(Array.unsafe_get ys index) &&
235235
unsafe_loop (succ index) len p xs ys
236236

237+
let for_alli a p =
238+
let n = Array.length a in
239+
let rec loop i =
240+
if i = n then true
241+
else if p i (Array.unsafe_get a i) then loop (succ i)
242+
else false in
243+
loop 0
244+
237245
let for_all2_no_exn xs ys p =
238246
let len_xs = Array.length xs in
239247
let len_ys = Array.length ys in

jscomp/ext/ext_array.mli

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@ val for_all2_no_exn :
8787
('a -> 'b -> bool) ->
8888
bool
8989

90+
val for_alli :
91+
'a array ->
92+
(int -> 'a -> bool) ->
93+
bool
94+
9095
val map :
9196
'a array ->
9297
('a -> 'b) ->

0 commit comments

Comments
 (0)