Skip to content

Commit 215cec0

Browse files
committed
remove #setfield1 primitive
1 parent c4cd44a commit 215cec0

File tree

4 files changed

+99
-86
lines changed

4 files changed

+99
-86
lines changed

jscomp/core/lam_convert.ml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -561,8 +561,6 @@ let convert (exports : Set_ident.t) (lam : Lambda.lambda) : Lam.t * Lam_module_i
561561
| "#apply8" -> Pjs_apply
562562
| "#makemutablelist" ->
563563
Pmakeblock(0, Blk_constructor{name = "::"; num_nonconst = 1},Mutable)
564-
| "#setfield1" ->
565-
Psetfield(1, Fld_set_na)
566564
| "#undefined_to_opt" -> Pundefined_to_opt
567565
| "#nullable_to_opt" -> Pnull_undefined_to_opt
568566
| "#null_to_opt" -> Pnull_to_opt

jscomp/others/belt_List.ml

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,23 @@ module A = Belt_Array
6767

6868
external mutableCell :
6969
'a -> 'a t -> 'a t = "#makemutablelist"
70+
71+
72+
type poly = { unsafeMutateTail : 'a . 'a t -> 'a t -> unit } [@@unboxed]
73+
74+
7075
(*
7176
[mutableCell x []] == [x]
7277
but tell the compiler that is a mutable cell, so it wont
7378
be mis-inlined in the future
7479
dont inline a binding to mutable cell, it is mutable
7580
*)
76-
external unsafeMutateTail :
77-
'a t -> 'a t -> unit = "#setfield1"
81+
(* relies on list internal representation *)
82+
let m : poly = {unsafeMutateTail = [%raw{|function(xs,ys){
83+
xs[1] = ys
84+
}|}]}
85+
86+
7887

7988
(*
8089
- the cell is not empty
@@ -128,12 +137,12 @@ let rec partitionAux p cell precX precY =
128137
let next = mutableCell h [] in
129138
if p h [@bs] then
130139
begin
131-
unsafeMutateTail precX next ;
140+
m.unsafeMutateTail precX next ;
132141
partitionAux p t next precY
133142
end
134143
else
135144
begin
136-
unsafeMutateTail precY next ;
145+
m.unsafeMutateTail precY next ;
137146
partitionAux p t precX next
138147
end
139148

@@ -143,8 +152,8 @@ let rec splitAux cell precX precY =
143152
| (a,b)::t ->
144153
let nextA = mutableCell a [] in
145154
let nextB = mutableCell b [] in
146-
unsafeMutateTail precX nextA;
147-
unsafeMutateTail precY nextB;
155+
m.unsafeMutateTail precX nextA;
156+
m.unsafeMutateTail precY nextB;
148157
splitAux t nextA nextB
149158

150159
(* return the tail pointer so it can continue copy other
@@ -155,7 +164,7 @@ let rec copyAuxCont cellX prec =
155164
| [] -> prec
156165
| h::t ->
157166
let next = mutableCell h [] in
158-
unsafeMutateTail prec next ;
167+
m.unsafeMutateTail prec next ;
159168
copyAuxCont t next
160169

161170
let rec copyAuxWitFilter f cellX prec =
@@ -166,7 +175,7 @@ let rec copyAuxWitFilter f cellX prec =
166175
if f h [@bs] then
167176
begin
168177
let next = mutableCell h [] in
169-
unsafeMutateTail prec next ;
178+
m.unsafeMutateTail prec next ;
170179
copyAuxWitFilter f t next
171180
end
172181
else copyAuxWitFilter f t prec
@@ -179,7 +188,7 @@ let rec copyAuxWithFilterIndex f cellX prec i =
179188
if f h i [@bs] then
180189
begin
181190
let next = mutableCell h [] in
182-
unsafeMutateTail prec next ;
191+
m.unsafeMutateTail prec next ;
183192
copyAuxWithFilterIndex f t next (i + 1)
184193
end
185194
else copyAuxWithFilterIndex f t prec (i + 1)
@@ -193,7 +202,7 @@ let rec copyAuxWitFilterMap f cellX prec =
193202
| Some h ->
194203
begin
195204
let next = mutableCell h [] in
196-
unsafeMutateTail prec next ;
205+
m.unsafeMutateTail prec next ;
197206
copyAuxWitFilterMap f t next
198207
end
199208
| None -> copyAuxWitFilterMap f t prec
@@ -203,21 +212,21 @@ let rec removeAssocAuxWithMap cellX x prec f =
203212
| [] -> false
204213
| ((a,_) as h):: t ->
205214
if f a x [@bs] then
206-
(unsafeMutateTail prec t ; true)
215+
(m.unsafeMutateTail prec t ; true)
207216
else
208217
let next = mutableCell h [] in
209-
unsafeMutateTail prec next ;
218+
m.unsafeMutateTail prec next ;
210219
removeAssocAuxWithMap t x next f
211220

212221
let rec setAssocAuxWithMap cellX x k prec eq =
213222
match cellX with
214223
| [] -> false
215224
| ((a,_) as h) :: t ->
216225
if eq a x [@bs] then
217-
(unsafeMutateTail prec ( (x,k)::t); true)
226+
(m.unsafeMutateTail prec ( (x,k)::t); true)
218227
else
219228
let next = mutableCell h [] in
220-
unsafeMutateTail prec next ;
229+
m.unsafeMutateTail prec next ;
221230
setAssocAuxWithMap t x k next eq
222231

223232

@@ -227,15 +236,15 @@ let rec copyAuxWithMap cellX prec f =
227236
()
228237
| h::t ->
229238
let next = mutableCell (f h [@bs]) [] in
230-
unsafeMutateTail prec next ;
239+
m.unsafeMutateTail prec next ;
231240
copyAuxWithMap t next f
232241

233242

234243
let rec zipAux cellX cellY prec =
235244
match cellX, cellY with
236245
| h1::t1, h2::t2 ->
237246
let next = mutableCell ( h1, h2) [] in
238-
unsafeMutateTail prec next ;
247+
m.unsafeMutateTail prec next ;
239248
zipAux t1 t2 next
240249
| [],_ | _,[] ->
241250
()
@@ -244,7 +253,7 @@ let rec copyAuxWithMap2 f cellX cellY prec =
244253
match cellX, cellY with
245254
| h1::t1, h2::t2 ->
246255
let next = mutableCell (f h1 h2 [@bs]) [] in
247-
unsafeMutateTail prec next ;
256+
m.unsafeMutateTail prec next ;
248257
copyAuxWithMap2 f t1 t2 next
249258
| [],_ | _,[] ->
250259
()
@@ -253,7 +262,7 @@ let rec copyAuxWithMapI f i cellX prec =
253262
match cellX with
254263
| h::t ->
255264
let next = mutableCell (f i h [@bs]) [] in
256-
unsafeMutateTail prec next ;
265+
m.unsafeMutateTail prec next ;
257266
copyAuxWithMapI f (i + 1) t next
258267
| [] ->
259268
()
@@ -265,7 +274,7 @@ let rec takeAux n cell prec =
265274
| [] -> false
266275
| x::xs ->
267276
let cell = mutableCell x [] in
268-
unsafeMutateTail prec cell;
277+
m.unsafeMutateTail prec cell;
269278
takeAux (n - 1) xs cell
270279

271280
let rec splitAtAux n cell prec =
@@ -275,7 +284,7 @@ let rec splitAtAux n cell prec =
275284
| [] -> None
276285
| x::xs ->
277286
let cell = mutableCell x [] in
278-
unsafeMutateTail prec cell;
287+
m.unsafeMutateTail prec cell;
279288
splitAtAux (n - 1) xs cell
280289

281290
(* invarint [n >= 0] *)
@@ -323,7 +332,7 @@ let concat xs ys =
323332
| [] -> ys
324333
| h::t ->
325334
let cell = mutableCell h [] in
326-
unsafeMutateTail (copyAuxCont t cell) ys;
335+
m.unsafeMutateTail (copyAuxCont t cell) ys;
327336
cell
328337

329338
let mapU xs f =
@@ -365,7 +374,7 @@ let makeByU n f =
365374
let i = ref 1 in
366375
while i.contents < n do
367376
let v = mutableCell (f i.contents [@bs]) [] in
368-
unsafeMutateTail cur.contents v ;
377+
m.unsafeMutateTail cur.contents v ;
369378
cur.contents<- v ;
370379
i.contents <- i.contents + 1 ;
371380
done
@@ -374,15 +383,15 @@ let makeByU n f =
374383

375384
let makeBy n f = makeByU n (fun[@bs] x -> f x)
376385

377-
let make n v =
386+
let make (type a) n (v : a) : a list =
378387
if n <= 0 then []
379388
else
380389
let headX = mutableCell v [] in
381390
let cur = ref headX in
382391
let i = ref 1 in
383392
while i.contents < n do
384393
let v = mutableCell v [] in
385-
unsafeMutateTail cur.contents v ;
394+
m.unsafeMutateTail cur.contents v ;
386395
cur.contents<- v ;
387396
i.contents <- i.contents + 1 ;
388397
done
@@ -452,7 +461,7 @@ let shuffle xs =
452461
(* let cell = ref head in *)
453462
(* for i = 1 to len - 1 do *)
454463
(* let next = mutableCell (f (A.getUnsafe arr i) [@bs]) [] in *)
455-
(* unsafeMutateTail !cell next ; *)
464+
(* unsafeMutateTail.unsafeMutateTail !cell next ; *)
456465
(* cell .contents<- next *)
457466
(* done ; *)
458467
(* head *)
@@ -468,7 +477,7 @@ let reverse l = reverseConcat l []
468477

469478
let rec flattenAux prec xs =
470479
match xs with
471-
| [] -> unsafeMutateTail prec []
480+
| [] -> m.unsafeMutateTail prec []
472481
| h::r -> flattenAux (copyAuxCont h prec) r
473482

474483

0 commit comments

Comments
 (0)