Skip to content

Commit 4ac15e3

Browse files
committed
change for_in impl for arrays
1 parent 9c1a499 commit 4ac15e3

File tree

3 files changed

+39
-17
lines changed

3 files changed

+39
-17
lines changed

jscomp/runtime/caml_obj.ml

Lines changed: 13 additions & 7 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,12 @@ 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){
142+
let caml_update_dummy : _ -> _ -> unit= [%raw{|function (x, y) {
143+
var set = function (k) {
138144
x[k] = y[k]
139145
}
140-
}
141-
|}]
146+
for_in(y, set)
147+
}|}]
142148

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

lib/es6/caml_obj.js

Lines changed: 13 additions & 5 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,12 @@ function caml_lazy_make(fn) {
5461
return block;
5562
}
5663

57-
var caml_update_dummy = (function(x,y){
58-
for (var k in y){
64+
var caml_update_dummy = (function (x, y) {
65+
var set = function (k) {
5966
x[k] = y[k]
6067
}
61-
});
68+
for_in(y, set)
69+
});
6270

6371
function caml_compare(_a, _b) {
6472
while(true) {

lib/js/caml_obj.js

Lines changed: 13 additions & 5 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,12 @@ function caml_lazy_make(fn) {
5461
return block;
5562
}
5663

57-
var caml_update_dummy = (function(x,y){
58-
for (var k in y){
64+
var caml_update_dummy = (function (x, y) {
65+
var set = function (k) {
5966
x[k] = y[k]
6067
}
61-
});
68+
for_in(y, set)
69+
});
6270

6371
function caml_compare(_a, _b) {
6472
while(true) {

0 commit comments

Comments
 (0)