Skip to content

Commit 894b132

Browse files
committed
support caml_weak (#272)
1 parent cc536ed commit 894b132

File tree

6 files changed

+43
-35
lines changed

6 files changed

+43
-35
lines changed

jscomp/js_config.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,4 +171,4 @@ let bigarray = "Caml_bigarray"
171171
let unix = "Caml_unix"
172172
let int64 = "Caml_int64"
173173
let md5 = "Caml_md5"
174-
174+
let weak = "Caml_weak"

jscomp/js_config.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,4 @@ val unix : string
6565
val int64 : string
6666
val md5 : string
6767
val hash : string
68+
val weak : string

jscomp/lam_dispatch_primitive.ml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -793,7 +793,13 @@ let query (prim : Lam_compile_env.primitive_description)
793793
-> E.runtime_call Js_config.md5 prim.prim_name args
794794
| "caml_hash"
795795
-> E.runtime_call Js_config.hash prim.prim_name args
796-
796+
| "caml_weak_set"
797+
| "caml_weak_create"
798+
| "caml_weak_get"
799+
| "caml_weak_check"
800+
| "caml_weak_blit"
801+
| "caml_weak_get_copy"
802+
-> E.runtime_call Js_config.weak prim.prim_name args
797803
| "caml_output_value_to_buffer"
798804
| "caml_marshal_data_size"
799805
| "caml_input_value_from_string"
@@ -804,12 +810,6 @@ let query (prim : Lam_compile_env.primitive_description)
804810
| "caml_md5_chan"
805811

806812
| "caml_hash_univ_param"
807-
| "caml_weak_set"
808-
| "caml_weak_create"
809-
| "caml_weak_get"
810-
| "caml_weak_check"
811-
| "caml_weak_blit"
812-
| "caml_weak_get_copy"
813813
| "caml_sys_close"
814814

815815
| "caml_sys_open"

jscomp/runtime/.depend

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ caml_queue.cmi :
1515
caml_string.cmi :
1616
caml_sys.cmi :
1717
caml_utils.cmi :
18+
caml_weak.cmi :
1819
caml_array.cmo : js.cmo caml_array.cmi
1920
caml_array.cmx : js.cmx caml_array.cmi
2021
caml_bigarray.cmo : caml_bigarray.cmi
@@ -53,6 +54,8 @@ caml_sys.cmo : js.cmo caml_sys.cmi
5354
caml_sys.cmx : js.cmx caml_sys.cmi
5455
caml_utils.cmo : caml_utils.cmi
5556
caml_utils.cmx : caml_utils.cmi
57+
caml_weak.cmo : caml_array.cmi caml_weak.cmi
58+
caml_weak.cmx : caml_array.cmx caml_weak.cmi
5659
fn.cmo :
5760
fn.cmx :
5861
js.cmo :
@@ -99,6 +102,8 @@ caml_sys.cmo : js.cmo caml_sys.cmi
99102
caml_sys.cmj : js.cmj caml_sys.cmi
100103
caml_utils.cmo : caml_utils.cmi
101104
caml_utils.cmj : caml_utils.cmi
105+
caml_weak.cmo : caml_array.cmi caml_weak.cmi
106+
caml_weak.cmj : caml_array.cmj caml_weak.cmi
102107
fn.cmo :
103108
fn.cmj :
104109
js.cmo :

jscomp/runtime/runtime.mllib

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ caml_bigarray
1717
caml_md5
1818
caml_queue
1919
caml_hash
20+
caml_weak
2021
typed_array
2122
js
2223
fn

jscomp/stdlib/weak.js

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ var Sys = require("./sys");
88
var Caml_primitive = require("../runtime/caml_primitive");
99
var Caml_array = require("../runtime/caml_array");
1010
var $$Array = require("./array");
11+
var Caml_weak = require("../runtime/caml_weak");
1112
var Caml_curry = require("../runtime/caml_curry");
1213

1314
function length(x) {
@@ -23,14 +24,14 @@ function fill(ar, ofs, len, x) {
2324
}
2425
else {
2526
for(var i = ofs ,i_finish = (ofs + len | 0) - 1 | 0; i<= i_finish; ++i){
26-
Caml_primitive.caml_weak_set(ar, i, x);
27+
Caml_weak.caml_weak_set(ar, i, x);
2728
}
2829
return /* () */0;
2930
}
3031
}
3132

3233
function Make(H) {
33-
var emptybucket = Caml_primitive.caml_weak_create(0);
34+
var emptybucket = Caml_weak.caml_weak_create(0);
3435
var get_index = function (t, h) {
3536
return (h & Pervasives.max_int) % t[/* table */0].length;
3637
};
@@ -66,7 +67,7 @@ function Make(H) {
6667
return accu;
6768
}
6869
else {
69-
var match = Caml_primitive.caml_weak_get(b, i);
70+
var match = Caml_weak.caml_weak_get(b, i);
7071
if (match) {
7172
_accu = Caml_curry.app2(f, match[0], accu);
7273
_i = i + 1 | 0;
@@ -92,7 +93,7 @@ function Make(H) {
9293
return /* () */0;
9394
}
9495
else {
95-
var match = Caml_primitive.caml_weak_get(b, i);
96+
var match = Caml_weak.caml_weak_get(b, i);
9697
if (match) {
9798
Caml_curry.app1(f, match[0]);
9899
_i = i + 1 | 0;
@@ -119,7 +120,7 @@ function Make(H) {
119120
return /* () */0;
120121
}
121122
else {
122-
var match = Caml_primitive.caml_weak_check(b, i);
123+
var match = Caml_weak.caml_weak_check(b, i);
123124
if (match !== 0) {
124125
Caml_curry.app3(f, b, t[/* hashes */1][j], i);
125126
_i = i + 1 | 0;
@@ -144,7 +145,7 @@ function Make(H) {
144145
}
145146
else {
146147
_accu = accu + (
147-
Caml_primitive.caml_weak_check(b, i) ? 1 : 0
148+
Caml_weak.caml_weak_check(b, i) ? 1 : 0
148149
) | 0;
149150
_i = i + 1 | 0;
150151
continue ;
@@ -175,13 +176,13 @@ function Make(H) {
175176
var j = _j;
176177
var i = _i;
177178
if (j >= prev_len) {
178-
if (Caml_primitive.caml_weak_check(bucket, i)) {
179+
if (Caml_weak.caml_weak_check(bucket, i)) {
179180
_i = i + 1 | 0;
180181
continue ;
181182

182183
}
183-
else if (Caml_primitive.caml_weak_check(bucket, j)) {
184-
Caml_primitive.caml_weak_blit(bucket, j, bucket, i, 1);
184+
else if (Caml_weak.caml_weak_check(bucket, j)) {
185+
Caml_weak.caml_weak_blit(bucket, j, bucket, i, 1);
185186
hbucket[i] = hbucket[j];
186187
_j = j - 1 | 0;
187188
_i = i + 1 | 0;
@@ -231,9 +232,9 @@ function Make(H) {
231232
"Weak.Make: hash bucket cannot grow more"
232233
];
233234
}
234-
var newbucket = Caml_primitive.caml_weak_create(newsz);
235+
var newbucket = Caml_weak.caml_weak_create(newsz);
235236
var newhashes = Caml_array.caml_make_vect(newsz, 0);
236-
Caml_primitive.caml_weak_blit(bucket, 0, newbucket, 0, sz);
237+
Caml_weak.caml_weak_blit(bucket, 0, newbucket, 0, sz);
237238
$$Array.blit(hashes, 0, newhashes, 0, sz);
238239
Caml_curry.app3(setter, newbucket, sz, d);
239240
newhashes[sz] = h;
@@ -254,7 +255,7 @@ function Make(H) {
254255
var add_weak = (function(newt){
255256
return function (ob, oh, oi) {
256257
var setter = function (nb, ni, _) {
257-
return Caml_primitive.caml_weak_blit(ob, oi, nb, ni, 1);
258+
return Caml_weak.caml_weak_blit(ob, oi, nb, ni, 1);
258259
};
259260
var h = oh[oi];
260261
return add_aux(newt, setter, /* None */0, h, get_index(newt, h));
@@ -278,7 +279,7 @@ function Make(H) {
278279
return 0;
279280
}
280281
}
281-
else if (Caml_primitive.caml_weak_check(bucket, i)) {
282+
else if (Caml_weak.caml_weak_check(bucket, i)) {
282283
_i = i + 1 | 0;
283284
continue ;
284285

@@ -292,7 +293,7 @@ function Make(H) {
292293
};
293294
var add = function (t, d) {
294295
var h = Caml_curry.app1(H[1], d);
295-
return add_aux(t, Caml_primitive.caml_weak_set, /* Some */[d], h, get_index(t, h));
296+
return add_aux(t, Caml_weak.caml_weak_set, /* Some */[d], h, get_index(t, h));
296297
};
297298
var find_or = function (t, d, ifnotfound) {
298299
var h = Caml_curry.app1(H[1], d);
@@ -307,10 +308,10 @@ function Make(H) {
307308
return Caml_curry.app2(ifnotfound, h, index);
308309
}
309310
else if (h === hashes[i]) {
310-
var match = Caml_primitive.caml_weak_get_copy(bucket, i);
311+
var match = Caml_weak.caml_weak_get_copy(bucket, i);
311312
if (match) {
312313
if (Caml_curry.app2(H[0], match[0], d)) {
313-
var match$1 = Caml_primitive.caml_weak_get(bucket, i);
314+
var match$1 = Caml_weak.caml_weak_get(bucket, i);
314315
if (match$1) {
315316
return match$1[0];
316317
}
@@ -341,7 +342,7 @@ function Make(H) {
341342
};
342343
var merge = function (t, d) {
343344
return find_or(t, d, function (h, index) {
344-
add_aux(t, Caml_primitive.caml_weak_set, /* Some */[d], h, index);
345+
add_aux(t, Caml_weak.caml_weak_set, /* Some */[d], h, index);
345346
return d;
346347
});
347348
};
@@ -363,7 +364,7 @@ function Make(H) {
363364
return ifnotfound;
364365
}
365366
else if (h === hashes[i]) {
366-
var match = Caml_primitive.caml_weak_get_copy(bucket, i);
367+
var match = Caml_weak.caml_weak_get_copy(bucket, i);
367368
if (match) {
368369
if (Caml_curry.app2(H[0], match[0], d)) {
369370
return Caml_curry.app2(iffound, bucket, i);
@@ -389,7 +390,7 @@ function Make(H) {
389390
};
390391
var remove = function (t, d) {
391392
return find_shadow(t, d, function (w, i) {
392-
return Caml_primitive.caml_weak_set(w, i, /* None */0);
393+
return Caml_weak.caml_weak_set(w, i, /* None */0);
393394
}, /* () */0);
394395
};
395396
var mem = function (t, d) {
@@ -412,10 +413,10 @@ function Make(H) {
412413
return accu;
413414
}
414415
else if (h === hashes[i]) {
415-
var match = Caml_primitive.caml_weak_get_copy(bucket, i);
416+
var match = Caml_weak.caml_weak_get_copy(bucket, i);
416417
if (match) {
417418
if (Caml_curry.app2(H[0], match[0], d)) {
418-
var match$1 = Caml_primitive.caml_weak_get(bucket, i);
419+
var match$1 = Caml_weak.caml_weak_get(bucket, i);
419420
if (match$1) {
420421
_accu = /* :: */[
421422
match$1[0],
@@ -482,17 +483,17 @@ function Make(H) {
482483
];
483484
}
484485

485-
var create = Caml_primitive.caml_weak_create
486+
var create = Caml_weak.caml_weak_create
486487

487-
var set = Caml_primitive.caml_weak_set
488+
var set = Caml_weak.caml_weak_set
488489

489-
var get = Caml_primitive.caml_weak_get
490+
var get = Caml_weak.caml_weak_get
490491

491-
var get_copy = Caml_primitive.caml_weak_get_copy
492+
var get_copy = Caml_weak.caml_weak_get_copy
492493

493-
var check = Caml_primitive.caml_weak_check
494+
var check = Caml_weak.caml_weak_check
494495

495-
var blit = Caml_primitive.caml_weak_blit
496+
var blit = Caml_weak.caml_weak_blit
496497

497498
exports.create = create;
498499
exports.length = length;

0 commit comments

Comments
 (0)