Skip to content

Commit b7d186d

Browse files
authored
Merge pull request #4276 from rickyvetter/forin
change for_in impl for arrays
2 parents 531822a + f9ea998 commit b7d186d

File tree

3 files changed

+51
-20
lines changed

3 files changed

+51
-20
lines changed

jscomp/runtime/caml_obj.ml

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,15 @@ module O = struct
3434
external isArray : 'a -> bool = "Array.isArray" [@@bs.val]
3535
type key = string
3636
let for_in : (Caml_obj_extern.t -> (key -> unit) -> unit) =
37-
[%raw{|function(o,foo){
38-
for (var x in o) { foo(x) }}
39-
|}]
37+
[%raw{|function (o, foo) {
38+
if (Array.isArray(o)) {
39+
for (var x = 0; x < o.length; x++) {
40+
foo(x)
41+
}
42+
} else {
43+
for (var x in o) { foo(x) }
44+
}
45+
}|}]
4046
external hasOwnProperty :
4147
t -> key -> bool = "hasOwnProperty" [@@bs.send]
4248
external get_value : Caml_obj_extern.t -> key -> Caml_obj_extern.t = ""[@@bs.get_index]
@@ -133,12 +139,15 @@ let caml_lazy_make (fn : _ -> _) =
133139
In most cases, rec value comes from record/modules,
134140
whose tag is 0, we optimize that case
135141
*)
136-
let caml_update_dummy : _ -> _ -> unit= [%raw{|function(x,y){
137-
for (var k in y){
138-
x[k] = y[k]
142+
let caml_update_dummy : _ -> _ -> unit= [%raw{|function (x, y) {
143+
if (Array.isArray(y)) {
144+
for (var k = 0; k < y.length; k++) {
145+
x[k] = y[k]
146+
}
147+
} else {
148+
for (var k in y) { x[k] = y[k] }
139149
}
140-
}
141-
|}]
150+
}|}]
142151

143152
(* Caml_obj_extern.set_length x (Caml_obj_extern.length y) *)
144153
(* [set_length] seems redundant here given that it is initialized as an array

lib/es6/caml_obj.js

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,15 @@ import * as Block from "./block.js";
44
import * as Caml_primitive from "./caml_primitive.js";
55
import * as Caml_builtin_exceptions from "./caml_builtin_exceptions.js";
66

7-
var for_in = (function(o,foo){
8-
for (var x in o) { foo(x) }});
7+
var for_in = (function (o, foo) {
8+
if (Array.isArray(o)) {
9+
for (var x = 0; x < o.length; x++) {
10+
foo(x)
11+
}
12+
} else {
13+
for (var x in o) { foo(x) }
14+
}
15+
});
916

1017
function caml_obj_block(tag, size) {
1118
var v = new Array(size);
@@ -54,11 +61,15 @@ function caml_lazy_make(fn) {
5461
return block;
5562
}
5663

57-
var caml_update_dummy = (function(x,y){
58-
for (var k in y){
59-
x[k] = y[k]
64+
var caml_update_dummy = (function (x, y) {
65+
if (Array.isArray(y)) {
66+
for (var k = 0; k < y.length; k++) {
67+
x[k] = y[k]
68+
}
69+
} else {
70+
for (var k in y) { x[k] = y[k] }
6071
}
61-
});
72+
});
6273

6374
function caml_compare(_a, _b) {
6475
while(true) {

lib/js/caml_obj.js

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,15 @@ var Block = require("./block.js");
44
var Caml_primitive = require("./caml_primitive.js");
55
var Caml_builtin_exceptions = require("./caml_builtin_exceptions.js");
66

7-
var for_in = (function(o,foo){
8-
for (var x in o) { foo(x) }});
7+
var for_in = (function (o, foo) {
8+
if (Array.isArray(o)) {
9+
for (var x = 0; x < o.length; x++) {
10+
foo(x)
11+
}
12+
} else {
13+
for (var x in o) { foo(x) }
14+
}
15+
});
916

1017
function caml_obj_block(tag, size) {
1118
var v = new Array(size);
@@ -54,11 +61,15 @@ function caml_lazy_make(fn) {
5461
return block;
5562
}
5663

57-
var caml_update_dummy = (function(x,y){
58-
for (var k in y){
59-
x[k] = y[k]
64+
var caml_update_dummy = (function (x, y) {
65+
if (Array.isArray(y)) {
66+
for (var k = 0; k < y.length; k++) {
67+
x[k] = y[k]
68+
}
69+
} else {
70+
for (var k in y) { x[k] = y[k] }
6071
}
61-
});
72+
});
6273

6374
function caml_compare(_a, _b) {
6475
while(true) {

0 commit comments

Comments
 (0)