Skip to content

Commit 8bb7043

Browse files
committed
clean up dependency between caml_hash_primitive and caml_hash
1 parent 72add8c commit 8bb7043

16 files changed

+217
-154
lines changed

jscomp/core/js_runtime_modules.ml

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ let format = "Caml_format"
3434
let string = "Caml_string"
3535
let bytes = "Caml_bytes"
3636
let float = "Caml_float"
37+
let hash_primitive = "Caml_hash_primitive"
3738
let hash = "Caml_hash"
3839
let oo = "Caml_oo"
3940
let curry = "Curry"

jscomp/core/lam_dispatch_primitive.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -796,7 +796,7 @@ let translate loc (prim_name : string)
796796
| "caml_hash_mix_string"
797797
| "caml_hash_mix_int"
798798
| "caml_hash_final_mix"
799-
799+
-> call Js_runtime_modules.hash_primitive
800800
| "caml_hash"
801801
-> call Js_runtime_modules.hash
802802
| "caml_weak_set"

jscomp/runtime/.depend

+4-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ caml_format.cmj : js_nativeint.cmj js_int64.cmj js_float.cmj caml_utils.cmj \
1515
bs_string.cmj caml_format.cmi
1616
caml_md5.cmj : bs_string.cmj caml_md5.cmi
1717
caml_queue.cmj : caml_queue.cmi
18-
caml_hash.cmj : js.cmj caml_queue.cmj bs_string.cmj bs_obj.cmj caml_hash.cmi
18+
caml_hash_primitive.cmj : bs_string.cmj caml_hash_primitive.cmi
19+
caml_hash.cmj : js.cmj caml_queue.cmj caml_hash_primitive.cmj bs_obj.cmj \
20+
caml_hash.cmi
1921
caml_weak.cmj : js_undefined.cmj js.cmj caml_array.cmj caml_weak.cmi
2022
caml_backtrace.cmj : caml_backtrace.cmi
2123
caml_int32.cmj : caml_int32.cmi
@@ -50,6 +52,7 @@ caml_parser.cmi :
5052
caml_format.cmi :
5153
caml_md5.cmi :
5254
caml_queue.cmi :
55+
caml_hash_primitive.cmi :
5356
caml_hash.cmi :
5457
caml_weak.cmi :
5558
caml_backtrace.cmi :

jscomp/runtime/Makefile

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ OTHERS= caml_array caml_string caml_bytes\
66
caml_obj caml_int64 \
77
caml_exceptions caml_utils caml_sys caml_io\
88
caml_float caml_lexer caml_parser \
9-
caml_format caml_md5 caml_queue caml_hash caml_weak\
9+
caml_format caml_md5 caml_queue \
10+
caml_hash_primitive\
11+
caml_hash caml_weak\
1012
caml_backtrace caml_int32 caml_gc js_typed_array \
1113
js_primitive caml_basic caml_oo curry caml_oo_curry caml_module \
1214
caml_missing_polyfill\

jscomp/runtime/caml_hash.ml

+3-66
Original file line numberDiff line numberDiff line change
@@ -24,73 +24,10 @@
2424

2525

2626

27+
external ( +~ ) : nativeint -> nativeint -> nativeint =
28+
"caml_int32_add"
2729

28-
29-
(** *)
30-
31-
let (<< ) = Nativeint.shift_left
32-
let (>>>) = Nativeint.shift_right_logical
33-
let (|~) = Nativeint.logor
34-
let (^) = Nativeint.logxor
35-
36-
external ( *~ ) : nativeint -> nativeint -> nativeint = "caml_int32_mul"
37-
external ( +~ ) : nativeint -> nativeint -> nativeint = "caml_int32_add"
38-
39-
40-
41-
let rotl32 (x : nativeint) n =
42-
(x << n) |~ (x >>> (32 - n))
43-
44-
45-
let caml_hash_mix_int h d =
46-
let d = ref d in
47-
d := !d *~ 0xcc9e2d51n ;
48-
d := rotl32 !d 15 ;
49-
d := !d *~ 0x1b873593n ;
50-
let h = ref (h ^ !d) in
51-
h := rotl32 !h 13 ;
52-
!h +~ (!h << 2) +~ 0xe6546b64n
53-
54-
let caml_hash_final_mix h =
55-
let h = ref (h ^ (h >>> 16)) in
56-
h := !h *~ 0x85ebca6bn ;
57-
h := !h ^ (!h >>> 13);
58-
h := !h *~ 0xc2b2ae35n ;
59-
!h ^ (!h >>> 16)
60-
(* Nativeint.logand (!h ^ (!h >>> 16)) 0x3FFFFFFFn *)
61-
62-
let caml_hash_mix_string h s =
63-
let len =Bs_string.length s in
64-
let block = len / 4 - 1 in
65-
let hash = ref h in
66-
for i = 0 to block do
67-
let j = 4 * i in
68-
let w =
69-
Char.code s.[j] lor
70-
(Char.code s.[j+1] lsl 8) lor
71-
(Char.code s.[j+2] lsl 16) lor
72-
(Char.code s.[j+3] lsl 24)
73-
in
74-
hash := caml_hash_mix_int !hash (Nativeint.of_int w)
75-
done ;
76-
let modulo = len land 0b11 in
77-
if modulo <> 0 then
78-
begin
79-
let w =
80-
if modulo = 3 then
81-
(Char.code s.[len - 1] lsl 16) lor
82-
(Char.code s.[len - 2] lsl 8) lor
83-
(Char.code s.[len - 3])
84-
else if modulo = 2 then
85-
(Char.code s.[len -1] lsl 8) lor
86-
Char.code s.[len -2]
87-
else Char.code s.[len - 1]
88-
in
89-
hash := caml_hash_mix_int !hash (Nativeint.of_int w)
90-
end;
91-
hash := !hash ^ (Nativeint.of_int len) ;
92-
!hash
93-
30+
open Caml_hash_primitive
9431

9532
let caml_hash count _limit seed obj =
9633
let hash = ref seed in

jscomp/runtime/caml_hash.mli

-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,4 @@
2626

2727

2828
(** *)
29-
val caml_hash_mix_int : nativeint -> nativeint -> nativeint
30-
val caml_hash_mix_string : nativeint -> string -> nativeint
31-
val caml_hash_final_mix : nativeint -> nativeint
3229
val caml_hash : int -> 'a -> nativeint -> Obj.t -> nativeint

jscomp/runtime/caml_hash_primitive.ml

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
(* Copyright (C) 2018 Authors of BuckleScript
2+
*
3+
* This program is free software: you can redistribute it and/or modify
4+
* it under the terms of the GNU Lesser General Public License as published by
5+
* the Free Software Foundation, either version 3 of the License, or
6+
* (at your option) any later version.
7+
*
8+
* In addition to the permissions granted to you by the LGPL, you may combine
9+
* or link a "work that uses the Library" with a publicly distributed version
10+
* of this file to produce a combined library or application, then distribute
11+
* that combined work under the terms of your choosing, with no requirement
12+
* to comply with the obligations normally placed on you by section 4 of the
13+
* LGPL version 3 (or the corresponding section of a later version of the LGPL
14+
* should you choose to use a later version).
15+
*
16+
* This program is distributed in the hope that it will be useful,
17+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
* GNU Lesser General Public License for more details.
20+
*
21+
* You should have received a copy of the GNU Lesser General Public License
22+
* along with this program; if not, write to the Free Software
23+
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
24+
25+
26+
27+
let (<< ) = Nativeint.shift_left
28+
let (>>>) = Nativeint.shift_right_logical
29+
let (|~) = Nativeint.logor
30+
let (^) = Nativeint.logxor
31+
32+
external ( *~ ) : nativeint -> nativeint -> nativeint = "caml_int32_mul"
33+
external ( +~ ) : nativeint -> nativeint -> nativeint = "caml_int32_add"
34+
35+
36+
37+
let rotl32 (x : nativeint) n =
38+
(x << n) |~ (x >>> (32 - n))
39+
40+
41+
let caml_hash_mix_int h d =
42+
let d = ref d in
43+
d := !d *~ 0xcc9e2d51n ;
44+
d := rotl32 !d 15 ;
45+
d := !d *~ 0x1b873593n ;
46+
let h = ref (h ^ !d) in
47+
h := rotl32 !h 13 ;
48+
!h +~ (!h << 2) +~ 0xe6546b64n
49+
50+
let caml_hash_final_mix h =
51+
let h = ref (h ^ (h >>> 16)) in
52+
h := !h *~ 0x85ebca6bn ;
53+
h := !h ^ (!h >>> 13);
54+
h := !h *~ 0xc2b2ae35n ;
55+
!h ^ (!h >>> 16)
56+
(* Nativeint.logand (!h ^ (!h >>> 16)) 0x3FFFFFFFn *)
57+
58+
let caml_hash_mix_string h s =
59+
let len =Bs_string.length s in
60+
let block = len / 4 - 1 in
61+
let hash = ref h in
62+
for i = 0 to block do
63+
let j = 4 * i in
64+
let w =
65+
Char.code s.[j] lor
66+
(Char.code s.[j+1] lsl 8) lor
67+
(Char.code s.[j+2] lsl 16) lor
68+
(Char.code s.[j+3] lsl 24)
69+
in
70+
hash := caml_hash_mix_int !hash (Nativeint.of_int w)
71+
done ;
72+
let modulo = len land 0b11 in
73+
if modulo <> 0 then
74+
begin
75+
let w =
76+
if modulo = 3 then
77+
(Char.code s.[len - 1] lsl 16) lor
78+
(Char.code s.[len - 2] lsl 8) lor
79+
(Char.code s.[len - 3])
80+
else if modulo = 2 then
81+
(Char.code s.[len -1] lsl 8) lor
82+
Char.code s.[len -2]
83+
else Char.code s.[len - 1]
84+
in
85+
hash := caml_hash_mix_int !hash (Nativeint.of_int w)
86+
end;
87+
hash := !hash ^ (Nativeint.of_int len) ;
88+
!hash
89+
90+
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
(* Copyright (C) 2018 Authors of BuckleScript
2+
*
3+
* This program is free software: you can redistribute it and/or modify
4+
* it under the terms of the GNU Lesser General Public License as published by
5+
* the Free Software Foundation, either version 3 of the License, or
6+
* (at your option) any later version.
7+
*
8+
* In addition to the permissions granted to you by the LGPL, you may combine
9+
* or link a "work that uses the Library" with a publicly distributed version
10+
* of this file to produce a combined library or application, then distribute
11+
* that combined work under the terms of your choosing, with no requirement
12+
* to comply with the obligations normally placed on you by section 4 of the
13+
* LGPL version 3 (or the corresponding section of a later version of the LGPL
14+
* should you choose to use a later version).
15+
*
16+
* This program is distributed in the hope that it will be useful,
17+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
* GNU Lesser General Public License for more details.
20+
*
21+
* You should have received a copy of the GNU Lesser General Public License
22+
* along with this program; if not, write to the Free Software
23+
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
24+
25+
val caml_hash_mix_int : nativeint -> nativeint -> nativeint
26+
val caml_hash_mix_string : nativeint -> string -> nativeint
27+
val caml_hash_final_mix : nativeint -> nativeint

jscomp/test/bs_hashtbl_string_test.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@
22

33
var Belt_Id = require("../../lib/js/belt_Id.js");
44
var Hashtbl = require("../../lib/js/hashtbl.js");
5-
var Caml_hash = require("../../lib/js/caml_hash.js");
65
var Belt_HashMap = require("../../lib/js/belt_HashMap.js");
76
var Belt_MapDict = require("../../lib/js/belt_MapDict.js");
87
var Caml_primitive = require("../../lib/js/caml_primitive.js");
98
var Belt_HashMapInt = require("../../lib/js/belt_HashMapInt.js");
109
var Belt_HashSetInt = require("../../lib/js/belt_HashSetInt.js");
1110
var Belt_HashMapString = require("../../lib/js/belt_HashMapString.js");
11+
var Caml_hash_primitive = require("../../lib/js/caml_hash_primitive.js");
1212
var Caml_builtin_exceptions = require("../../lib/js/caml_builtin_exceptions.js");
1313
var Belt_internalBucketsType = require("../../lib/js/belt_internalBucketsType.js");
1414

1515
function hash_string(s) {
16-
return Caml_hash.caml_hash_final_mix(Caml_hash.caml_hash_mix_string(0, s));
16+
return Caml_hash_primitive.caml_hash_final_mix(Caml_hash_primitive.caml_hash_mix_string(0, s));
1717
}
1818

1919
var hashString = (function (str) {
@@ -36,7 +36,7 @@ var String1 = Belt_Id.hashable(hashString, (function (x, y) {
3636
}));
3737

3838
var String2 = Belt_Id.hashable((function (x) {
39-
return Caml_hash.caml_hash_final_mix(Caml_hash.caml_hash_mix_string(0, x));
39+
return Caml_hash_primitive.caml_hash_final_mix(Caml_hash_primitive.caml_hash_mix_string(0, x));
4040
}), (function (x, y) {
4141
return x === y;
4242
}));

lib/js/belt_HashMapInt.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
var Caml_hash = require("./caml_hash.js");
3+
var Caml_hash_primitive = require("./caml_hash_primitive.js");
44
var Belt_internalBuckets = require("./belt_internalBuckets.js");
55
var Belt_internalBucketsType = require("./belt_internalBucketsType.js");
66

@@ -9,7 +9,7 @@ function copyBucketReHash(h_buckets, ndata_tail, _old_bucket) {
99
var old_bucket = _old_bucket;
1010
if (old_bucket !== undefined) {
1111
var s = old_bucket.key;
12-
var nidx = Caml_hash.caml_hash_final_mix(Caml_hash.caml_hash_mix_int(0, s)) & (h_buckets.length - 1 | 0);
12+
var nidx = Caml_hash_primitive.caml_hash_final_mix(Caml_hash_primitive.caml_hash_mix_int(0, s)) & (h_buckets.length - 1 | 0);
1313
var match = ndata_tail[nidx];
1414
if (match !== undefined) {
1515
match.next = old_bucket;
@@ -46,7 +46,7 @@ function replaceInBucket(key, info, _cell) {
4646
function set(h, key, value) {
4747
var h_buckets = h.buckets;
4848
var buckets_len = h_buckets.length;
49-
var i = Caml_hash.caml_hash_final_mix(Caml_hash.caml_hash_mix_int(0, key)) & (buckets_len - 1 | 0);
49+
var i = Caml_hash_primitive.caml_hash_final_mix(Caml_hash_primitive.caml_hash_mix_int(0, key)) & (buckets_len - 1 | 0);
5050
var l = h_buckets[i];
5151
if (l !== undefined) {
5252
if (replaceInBucket(key, value, l)) {
@@ -96,7 +96,7 @@ function set(h, key, value) {
9696

9797
function remove(h, key) {
9898
var h_buckets = h.buckets;
99-
var i = Caml_hash.caml_hash_final_mix(Caml_hash.caml_hash_mix_int(0, key)) & (h_buckets.length - 1 | 0);
99+
var i = Caml_hash_primitive.caml_hash_final_mix(Caml_hash_primitive.caml_hash_mix_int(0, key)) & (h_buckets.length - 1 | 0);
100100
var bucket = h_buckets[i];
101101
if (bucket !== undefined) {
102102
if (bucket.key === key) {
@@ -134,7 +134,7 @@ function remove(h, key) {
134134

135135
function get(h, key) {
136136
var h_buckets = h.buckets;
137-
var nid = Caml_hash.caml_hash_final_mix(Caml_hash.caml_hash_mix_int(0, key)) & (h_buckets.length - 1 | 0);
137+
var nid = Caml_hash_primitive.caml_hash_final_mix(Caml_hash_primitive.caml_hash_mix_int(0, key)) & (h_buckets.length - 1 | 0);
138138
var match = h_buckets[nid];
139139
if (match !== undefined) {
140140
if (key === match.key) {
@@ -181,7 +181,7 @@ function get(h, key) {
181181

182182
function has(h, key) {
183183
var h_buckets = h.buckets;
184-
var nid = Caml_hash.caml_hash_final_mix(Caml_hash.caml_hash_mix_int(0, key)) & (h_buckets.length - 1 | 0);
184+
var nid = Caml_hash_primitive.caml_hash_final_mix(Caml_hash_primitive.caml_hash_mix_int(0, key)) & (h_buckets.length - 1 | 0);
185185
var bucket = h_buckets[nid];
186186
if (bucket !== undefined) {
187187
var key$1 = key;

lib/js/belt_HashMapString.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
var Caml_hash = require("./caml_hash.js");
3+
var Caml_hash_primitive = require("./caml_hash_primitive.js");
44
var Belt_internalBuckets = require("./belt_internalBuckets.js");
55
var Belt_internalBucketsType = require("./belt_internalBucketsType.js");
66

@@ -9,7 +9,7 @@ function copyBucketReHash(h_buckets, ndata_tail, _old_bucket) {
99
var old_bucket = _old_bucket;
1010
if (old_bucket !== undefined) {
1111
var s = old_bucket.key;
12-
var nidx = Caml_hash.caml_hash_final_mix(Caml_hash.caml_hash_mix_string(0, s)) & (h_buckets.length - 1 | 0);
12+
var nidx = Caml_hash_primitive.caml_hash_final_mix(Caml_hash_primitive.caml_hash_mix_string(0, s)) & (h_buckets.length - 1 | 0);
1313
var match = ndata_tail[nidx];
1414
if (match !== undefined) {
1515
match.next = old_bucket;
@@ -46,7 +46,7 @@ function replaceInBucket(key, info, _cell) {
4646
function set(h, key, value) {
4747
var h_buckets = h.buckets;
4848
var buckets_len = h_buckets.length;
49-
var i = Caml_hash.caml_hash_final_mix(Caml_hash.caml_hash_mix_string(0, key)) & (buckets_len - 1 | 0);
49+
var i = Caml_hash_primitive.caml_hash_final_mix(Caml_hash_primitive.caml_hash_mix_string(0, key)) & (buckets_len - 1 | 0);
5050
var l = h_buckets[i];
5151
if (l !== undefined) {
5252
if (replaceInBucket(key, value, l)) {
@@ -96,7 +96,7 @@ function set(h, key, value) {
9696

9797
function remove(h, key) {
9898
var h_buckets = h.buckets;
99-
var i = Caml_hash.caml_hash_final_mix(Caml_hash.caml_hash_mix_string(0, key)) & (h_buckets.length - 1 | 0);
99+
var i = Caml_hash_primitive.caml_hash_final_mix(Caml_hash_primitive.caml_hash_mix_string(0, key)) & (h_buckets.length - 1 | 0);
100100
var bucket = h_buckets[i];
101101
if (bucket !== undefined) {
102102
if (bucket.key === key) {
@@ -134,7 +134,7 @@ function remove(h, key) {
134134

135135
function get(h, key) {
136136
var h_buckets = h.buckets;
137-
var nid = Caml_hash.caml_hash_final_mix(Caml_hash.caml_hash_mix_string(0, key)) & (h_buckets.length - 1 | 0);
137+
var nid = Caml_hash_primitive.caml_hash_final_mix(Caml_hash_primitive.caml_hash_mix_string(0, key)) & (h_buckets.length - 1 | 0);
138138
var match = h_buckets[nid];
139139
if (match !== undefined) {
140140
if (key === match.key) {
@@ -181,7 +181,7 @@ function get(h, key) {
181181

182182
function has(h, key) {
183183
var h_buckets = h.buckets;
184-
var nid = Caml_hash.caml_hash_final_mix(Caml_hash.caml_hash_mix_string(0, key)) & (h_buckets.length - 1 | 0);
184+
var nid = Caml_hash_primitive.caml_hash_final_mix(Caml_hash_primitive.caml_hash_mix_string(0, key)) & (h_buckets.length - 1 | 0);
185185
var bucket = h_buckets[nid];
186186
if (bucket !== undefined) {
187187
var key$1 = key;

0 commit comments

Comments
 (0)