Skip to content

Commit ece12b1

Browse files
committed
remove unsafe code in Belt.Stack
1 parent dd78784 commit ece12b1

File tree

3 files changed

+29
-23
lines changed

3 files changed

+29
-23
lines changed

jscomp/others/belt_MutableStack.ml

+11-9
Original file line numberDiff line numberDiff line change
@@ -89,15 +89,17 @@ let forEachU s f =
8989
iterAux s.root f
9090

9191
let forEach s f = forEachU s (fun [@bs] x -> f x)
92-
93-
let dynamicPopIterU s f =
94-
let cursor = ref s.root in
95-
while cursor.contents != None do
96-
let v = Belt_Option.getUnsafe cursor.contents in
97-
s.root <- v.tail;
98-
f v.head [@bs];
99-
cursor .contents<- s.root (* using root, [f] may change it*)
100-
done
92+
93+
94+
let rec dynamicPopIterU s f =
95+
match s.root with
96+
| Some {tail; head }->
97+
s.root <- tail;
98+
f head [@bs] ;
99+
dynamicPopIterU s f (* using root, [f] may change it*)
100+
| None -> ()
101+
102+
101103

102104
let dynamicPopIter s f = dynamicPopIterU s (fun [@bs] x -> f x)
103105

lib/es6/belt_MutableStack.js

+9-7
Original file line numberDiff line numberDiff line change
@@ -108,14 +108,16 @@ function forEach(s, f) {
108108
}
109109

110110
function dynamicPopIterU(s, f) {
111-
var cursor = s.root;
112-
while(cursor !== undefined) {
113-
var v = cursor;
114-
s.root = v.tail;
115-
f(v.head);
116-
cursor = s.root;
111+
while(true) {
112+
var match = s.root;
113+
if (match === undefined) {
114+
return ;
115+
}
116+
var match$1 = match;
117+
s.root = match$1.tail;
118+
f(match$1.head);
119+
continue ;
117120
};
118-
119121
}
120122

121123
function dynamicPopIter(s, f) {

lib/js/belt_MutableStack.js

+9-7
Original file line numberDiff line numberDiff line change
@@ -108,14 +108,16 @@ function forEach(s, f) {
108108
}
109109

110110
function dynamicPopIterU(s, f) {
111-
var cursor = s.root;
112-
while(cursor !== undefined) {
113-
var v = cursor;
114-
s.root = v.tail;
115-
f(v.head);
116-
cursor = s.root;
111+
while(true) {
112+
var match = s.root;
113+
if (match === undefined) {
114+
return ;
115+
}
116+
var match$1 = match;
117+
s.root = match$1.tail;
118+
f(match$1.head);
119+
continue ;
117120
};
118-
119121
}
120122

121123
function dynamicPopIter(s, f) {

0 commit comments

Comments
 (0)