From df518f3054ee75e5d08c162689b87d5aac5fb6ad Mon Sep 17 00:00:00 2001 From: Hongbo Zhang Date: Thu, 10 May 2018 15:14:12 +0200 Subject: [PATCH] fix #2789 --- jscomp/stdlib/weak.ml | 5 ++++- jscomp/test/.depend | 1 + jscomp/test/Makefile | 1 + jscomp/test/gpr_2789_test.js | 23 +++++++++++++++++++++++ jscomp/test/gpr_2789_test.ml | 11 +++++++++++ lib/js/weak.js | 34 ++++++++++++++++++---------------- 6 files changed, 58 insertions(+), 17 deletions(-) create mode 100644 jscomp/test/gpr_2789_test.js create mode 100644 jscomp/test/gpr_2789_test.ml diff --git a/jscomp/stdlib/weak.ml b/jscomp/stdlib/weak.ml index 6b1165228e..57ad4a794e 100644 --- a/jscomp/stdlib/weak.ml +++ b/jscomp/stdlib/weak.ml @@ -16,8 +16,11 @@ type 'a t;; external create: int -> 'a t = "caml_weak_create";; - +#if BS then +external length : 'a t -> int = "%array_length" +#else let length x = Obj.size(Obj.repr x) - 1;; +#end external set : 'a t -> int -> 'a option -> unit = "caml_weak_set";; external get: 'a t -> int -> 'a option = "caml_weak_get";; diff --git a/jscomp/test/.depend b/jscomp/test/.depend index 034ec63dbc..341a21b9ed 100644 --- a/jscomp/test/.depend +++ b/jscomp/test/.depend @@ -315,6 +315,7 @@ gpr_2652_test.cmj : ../others/node.cmj ../stdlib/buffer.cmj gpr_2682_test.cmj : ../runtime/js.cmj gpr_2700_test.cmj : gpr_2731_test.cmj : +gpr_2789_test.cmj : ../stdlib/weak.cmj mt.cmj gpr_405_test.cmj : ../stdlib/hashtbl.cmj gpr_405_test.cmi gpr_441.cmj : gpr_459_test.cmj : mt.cmj diff --git a/jscomp/test/Makefile b/jscomp/test/Makefile index 9721d22b08..16748a1633 100644 --- a/jscomp/test/Makefile +++ b/jscomp/test/Makefile @@ -254,6 +254,7 @@ OTHERS := test_literals a test_ari test_export2 test_internalOO test_obj_simple_ arity_infer\ gpr_2682_test\ record_debug_test\ + gpr_2789_test\ # bs_uncurry_test # needs Lam to get rid of Uncurry arity first # simple_derive_test diff --git a/jscomp/test/gpr_2789_test.js b/jscomp/test/gpr_2789_test.js new file mode 100644 index 0000000000..05b67aada4 --- /dev/null +++ b/jscomp/test/gpr_2789_test.js @@ -0,0 +1,23 @@ +'use strict'; + +var Mt = require("./mt.js"); +var Caml_weak = require("../../lib/js/caml_weak.js"); + +var suites = [/* [] */0]; + +var test_id = [0]; + +function eq(loc, x, y) { + return Mt.eq_suites(test_id, suites, loc, x, y); +} + +eq("File \"gpr_2789_test.ml\", line 8, characters 5-12", 0, Caml_weak.caml_weak_create(0).length); + +eq("File \"gpr_2789_test.ml\", line 9, characters 5-12", 1, Caml_weak.caml_weak_create(1).length); + +Mt.from_pair_suites("gpr_2789_test.ml", suites[0]); + +exports.suites = suites; +exports.test_id = test_id; +exports.eq = eq; +/* Not a pure module */ diff --git a/jscomp/test/gpr_2789_test.ml b/jscomp/test/gpr_2789_test.ml new file mode 100644 index 0000000000..c1c0c92820 --- /dev/null +++ b/jscomp/test/gpr_2789_test.ml @@ -0,0 +1,11 @@ +let suites : Mt.pair_suites ref = ref [] +let test_id = ref 0 +let eq loc x y = Mt.eq_suites ~test_id ~suites loc x y + + + +let () = + eq __LOC__ 0 (Weak.length (Weak.create 0)); + eq __LOC__ 1 (Weak.length (Weak.create 1)) + +;; Mt.from_pair_suites __FILE__ !suites \ No newline at end of file diff --git a/lib/js/weak.js b/lib/js/weak.js index 2cf42394a6..16ad76b881 100644 --- a/lib/js/weak.js +++ b/lib/js/weak.js @@ -10,12 +10,8 @@ var Pervasives = require("./pervasives.js"); var Caml_primitive = require("./caml_primitive.js"); var Caml_builtin_exceptions = require("./caml_builtin_exceptions.js"); -function length(x) { - return x.length - 1 | 0; -} - function fill(ar, ofs, len, x) { - if (ofs < 0 || len < 0 || (ofs + len | 0) > (ar.length - 1 | 0)) { + if (ofs < 0 || len < 0 || (ofs + len | 0) > ar.length) { throw [ Caml_builtin_exceptions.invalid_argument, "Weak.fill" @@ -61,7 +57,7 @@ function Make(H) { while(true) { var accu = _accu; var i = _i; - if (i >= (b.length - 1 | 0)) { + if (i >= b.length) { return accu; } else { var match = Caml_weak.caml_weak_get(b, i); @@ -83,7 +79,7 @@ function Make(H) { var b = param; while(true) { var i = _i; - if (i >= (b.length - 1 | 0)) { + if (i >= b.length) { return /* () */0; } else { var match = Caml_weak.caml_weak_get(b, i); @@ -106,7 +102,7 @@ function Make(H) { var b = param$1; while(true) { var i = _i; - if (i >= (b.length - 1 | 0)) { + if (i >= b.length) { return /* () */0; } else { var match = Caml_weak.caml_weak_check(b, i); @@ -126,7 +122,7 @@ function Make(H) { while(true) { var accu = _accu; var i = _i; - if (i >= (b.length - 1 | 0)) { + if (i >= b.length) { return accu; } else { _accu = accu + ( @@ -151,7 +147,7 @@ function Make(H) { var test_shrink_bucket = function (t) { var bucket = Caml_array.caml_array_get(t[/* table */0], t[/* rover */4]); var hbucket = Caml_array.caml_array_get(t[/* hashes */1], t[/* rover */4]); - var len = bucket.length - 1 | 0; + var len = bucket.length; var prev_len = prev_sz(len); var live = count_bucket(0, bucket, 0); if (live <= prev_len) { @@ -178,7 +174,7 @@ function Make(H) { } }; }; - loop(0, (bucket.length - 1 | 0) - 1 | 0); + loop(0, bucket.length - 1 | 0); if (prev_len === 0) { Caml_array.caml_array_set(t[/* table */0], t[/* rover */4], emptybucket); Caml_array.caml_array_set(t[/* hashes */1], t[/* rover */4], /* array */[]); @@ -197,7 +193,7 @@ function Make(H) { var add_aux = function (t, setter, d, h, index) { var bucket = Caml_array.caml_array_get(t[/* table */0], index); var hashes = Caml_array.caml_array_get(t[/* hashes */1], index); - var sz = bucket.length - 1 | 0; + var sz = bucket.length; var _i = 0; while(true) { var i = _i; @@ -271,7 +267,7 @@ function Make(H) { var index = get_index(t, h); var bucket = Caml_array.caml_array_get(t[/* table */0], index); var hashes = Caml_array.caml_array_get(t[/* hashes */1], index); - var sz = bucket.length - 1 | 0; + var sz = bucket.length; var _i = 0; while(true) { var i = _i; @@ -318,7 +314,7 @@ function Make(H) { var index = get_index(t, h); var bucket = Caml_array.caml_array_get(t[/* table */0], index); var hashes = Caml_array.caml_array_get(t[/* hashes */1], index); - var sz = bucket.length - 1 | 0; + var sz = bucket.length; var _i = 0; while(true) { var i = _i; @@ -358,7 +354,7 @@ function Make(H) { var index = get_index(t, h); var bucket = Caml_array.caml_array_get(t[/* table */0], index); var hashes = Caml_array.caml_array_get(t[/* hashes */1], index); - var sz = bucket.length - 1 | 0; + var sz = bucket.length; var _i = 0; var _accu = /* [] */0; while(true) { @@ -398,7 +394,9 @@ function Make(H) { }; var stats = function (t) { var len = t[/* table */0].length; - var lens = $$Array.map(length, t[/* table */0]); + var lens = $$Array.map((function (prim) { + return prim.length; + }), t[/* table */0]); $$Array.sort(Caml_obj.caml_compare, lens); var totlen = $$Array.fold_left((function (prim, prim$1) { return prim + prim$1 | 0; @@ -430,6 +428,10 @@ function Make(H) { var create = Caml_weak.caml_weak_create; +function length(prim) { + return prim.length; +} + var set = Caml_weak.caml_weak_set; var get = Caml_weak.caml_weak_get;