Skip to content

fix #2789 #2792

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 10, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion jscomp/stdlib/weak.ml
Original file line number Diff line number Diff line change
@@ -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";;
1 change: 1 addition & 0 deletions jscomp/test/.depend
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions jscomp/test/Makefile
Original file line number Diff line number Diff line change
@@ -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
23 changes: 23 additions & 0 deletions jscomp/test/gpr_2789_test.js
Original file line number Diff line number Diff line change
@@ -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 */
11 changes: 11 additions & 0 deletions jscomp/test/gpr_2789_test.ml
Original file line number Diff line number Diff line change
@@ -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
34 changes: 18 additions & 16 deletions lib/js/weak.js
Original file line number Diff line number Diff line change
@@ -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;