Skip to content

Commit f9ef1b7

Browse files
Simplify buffer interface
1 parent c843e00 commit f9ef1b7

File tree

9 files changed

+426
-356
lines changed

9 files changed

+426
-356
lines changed

js/acutis_js.ml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,10 @@ module Concurrent = struct
9999

100100
let buffer_create () = new%js Js.array_empty
101101

102-
let buffer_add_string (b : buffer) s =
103-
b##push (promise (Js.string s)) |> ignore
104-
105-
let buffer_add_promise (b : buffer) p =
102+
let buffer_append (b : buffer) p =
106103
b##push (Promise.then_ p @@ fun s -> promise (Js.string s)) |> ignore
107104

108-
let buffer_to_promise (b : buffer) =
105+
let buffer_contents (b : buffer) =
109106
Promise.then_ (Promise.all b) @@ fun a ->
110107
promise (a##join (Js.string "") |> Js.to_string)
111108
end

lib/instruct.ml

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -140,16 +140,14 @@ module type SEM = sig
140140
(** {1 Buffers.} *)
141141

142142
type buffer
143-
(** This type is not a typical "buffer" since it must work with promises. The
144-
language may implement it however is most suitable. *)
143+
(** A buffer of concurrent string promises. *)
145144

146145
val buffer_create : unit -> buffer exp
147-
val buffer_add_string : buffer exp -> string exp -> unit stmt
148-
val buffer_add_promise : buffer exp -> string promise exp -> unit stmt
146+
val buffer_append : buffer exp -> string promise exp -> unit stmt
149147

150148
(** These are lambdas to minimize generated code. *)
151149

152-
val buffer_to_promise : (buffer -> string promise) exp
150+
val buffer_contents : (buffer -> string promise) exp
153151
val escape : (string -> string) exp
154152

155153
(** {1 Mutable stacks.} *)
@@ -278,7 +276,7 @@ end = struct
278276

279277
type runtime = {
280278
comps : (data hashtbl -> string promise) hashtbl exp;
281-
buffer_to_promise : (buffer -> string promise) exp;
279+
buffer_contents : (buffer -> string promise) exp;
282280
escape : (string -> string) exp;
283281
}
284282

@@ -289,8 +287,8 @@ end = struct
289287

290288
let parse_escape runtime buf esc x =
291289
match esc with
292-
| C.No_escape -> buffer_add_string buf x
293-
| C.Escape -> buffer_add_string buf (runtime.escape @@ x)
290+
| C.No_escape -> buffer_append buf (promise x)
291+
| C.Escape -> buffer_append buf (promise (runtime.escape @@ x))
294292

295293
let fmt runtime buf esc x = function
296294
| C.Fmt_string -> parse_escape runtime buf esc (Data.to_string x)
@@ -482,7 +480,7 @@ end = struct
482480
aux hd tl
483481

484482
let rec node runtime buffer props = function
485-
| C.Text s -> buffer_add_string buffer (string s)
483+
| C.Text s -> buffer_append buffer (promise (string s))
486484
| C.Echo (echs, fmt, default, esc) ->
487485
echoes runtime buffer props esc default fmt echs
488486
| C.Match (blocks, data, { tree; exits }) ->
@@ -539,7 +537,7 @@ end = struct
539537
s1 |: s2))
540538
| Component (name, _, blocks, dict) ->
541539
construct_blocks runtime buffer blocks props (fun blocks buffer ->
542-
buffer_add_promise buffer
540+
buffer_append buffer
543541
(runtime.comps.%{string name}
544542
@@ construct_data_hashtbl blocks props dict))
545543

@@ -556,19 +554,17 @@ end = struct
556554
|> Seq.map (fun (i, block) ->
557555
let$ buffer = ("buffer", buffer_create ()) in
558556
let s1 = nodes runtime buffer props block in
559-
let s2 =
560-
blocks.%(int i) <- runtime.buffer_to_promise @@ buffer
561-
in
557+
let s2 = blocks.%(int i) <- runtime.buffer_contents @@ buffer in
562558
s1 |: s2)
563559
|> join_stmts
564560
in
565561
let s2 =
566-
buffer_add_promise buffer
562+
buffer_append buffer
567563
(bind (promise_array blocks)
568564
(lambda (fun blocks_resolved ->
569565
let$ buffer = ("buffer", buffer_create ()) in
570566
let s1 = f blocks_resolved buffer in
571-
let s2 = return (runtime.buffer_to_promise @@ buffer) in
567+
let s2 = return (runtime.buffer_contents @@ buffer) in
572568
s1 |: s2)))
573569
in
574570
s1 |: s2
@@ -1067,9 +1063,9 @@ end = struct
10671063
compiled.C.components ([], [])
10681064
in
10691065
let$ escape = ("acutis_escape", escape) in
1070-
let$ buffer_to_promise = ("buffer_to_promise", buffer_to_promise) in
1066+
let$ buffer_contents = ("buffer_contents", buffer_contents) in
10711067
let$ comps = ("components", hashtbl_create ()) in
1072-
let runtime = { escape; comps; buffer_to_promise } in
1068+
let runtime = { escape; comps; buffer_contents } in
10731069
let s1 =
10741070
List.to_seq externals
10751071
|> Seq.map (fun (k, tys, v) ->
@@ -1091,7 +1087,7 @@ end = struct
10911087
lambda (fun props ->
10921088
let$ buffer = ("buffer", buffer_create ()) in
10931089
let s1 = nodes runtime buffer props v in
1094-
let s2 = return (buffer_to_promise @@ buffer) in
1090+
let s2 = return (buffer_contents @@ buffer) in
10951091
s1 |: s2))
10961092
|> join_stmts
10971093
in
@@ -1150,7 +1146,7 @@ end = struct
11501146
let s3 =
11511147
let$ buffer = ("buffer", buffer_create ()) in
11521148
let s1 = nodes runtime buffer props compiled.nodes in
1153-
let s2 = return (buffer_to_promise @@ buffer) in
1149+
let s2 = return (buffer_contents @@ buffer) in
11541150
s1 |: s2
11551151
in
11561152
s1 |: s2 |: s3))
@@ -1270,9 +1266,8 @@ module MakeTrans
12701266
let bind a f = fwde (F.bind (bwde a) (bwde f))
12711267
let promise_array a = fwde (F.promise_array (bwde a))
12721268
let buffer_create () = fwde (F.buffer_create ())
1273-
let buffer_add_string b s = fwds (F.buffer_add_string (bwde b) (bwde s))
1274-
let buffer_add_promise b p = fwds (F.buffer_add_promise (bwde b) (bwde p))
1275-
let buffer_to_promise = fwde F.buffer_to_promise
1269+
let buffer_append b s = fwds (F.buffer_append (bwde b) (bwde s))
1270+
let buffer_contents = fwde F.buffer_contents
12761271
let escape = fwde F.escape
12771272
let stack_create () = fwde (F.stack_create ())
12781273
let stack_is_empty s = fwde (F.stack_is_empty (bwde s))
@@ -1459,9 +1454,8 @@ let pp (type a) pp_import ppf c =
14591454
type buffer
14601455

14611456
let buffer_create () = F.dprintf "(buffer_create)"
1462-
let buffer_add_string = F.dprintf "(@[buffer_add_string@ %t@ %t@])"
1463-
let buffer_add_promise = F.dprintf "(@[buffer_add_promise@ %t@ %t@])"
1464-
let buffer_to_promise = F.dprintf "(buffer_to_promise)"
1457+
let buffer_append = F.dprintf "(@[buffer_append@ %t@ %t@])"
1458+
let buffer_contents = F.dprintf "(buffer_contents)"
14651459
let escape = F.dprintf "(escape)"
14661460

14671461
type 'a stack

lib/printJs.ml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -263,10 +263,9 @@ module MakeJavaScript (M : JSMODULE) :
263263
type buffer
264264

265265
let buffer_create () = array [||]
266-
let buffer_add_string b s = stmt (b.!("push") @@ s)
267-
let buffer_add_promise = buffer_add_string
266+
let buffer_append b s = stmt (b.!("push") @@ s)
268267

269-
let buffer_to_promise =
268+
let buffer_contents =
270269
lambda (fun a ->
271270
return
272271
(bind (promise_array a)

lib/render.ml

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,8 @@ module type CONCURRENT = sig
1616
val bind : 'a promise -> ('a -> 'b promise) -> 'b promise
1717
val promise_array : 'a promise array -> 'a array promise
1818
val buffer_create : unit -> buffer
19-
val buffer_add_string : buffer -> string -> unit
20-
val buffer_add_promise : buffer -> string promise -> unit
21-
val buffer_to_promise : buffer -> string promise
19+
val buffer_append : buffer -> string promise -> unit
20+
val buffer_contents : buffer -> string promise
2221
end
2322

2423
module type DECODABLE = sig
@@ -315,7 +314,6 @@ module MakeString = Make (struct
315314
let bind = ( |> )
316315
let promise_array = Fun.id
317316
let buffer_create () = Buffer.create 1024
318-
let buffer_add_string = Buffer.add_string
319-
let buffer_add_promise = Buffer.add_string
320-
let buffer_to_promise = Buffer.contents
317+
let buffer_append = Buffer.add_string
318+
let buffer_contents = Buffer.contents
321319
end)

lib/render.mli

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,8 @@ module type CONCURRENT = sig
2020
val bind : 'a promise -> ('a -> 'b promise) -> 'b promise
2121
val promise_array : 'a promise array -> 'a array promise
2222
val buffer_create : unit -> buffer
23-
val buffer_add_string : buffer -> string -> unit
24-
val buffer_add_promise : buffer -> string promise -> unit
25-
val buffer_to_promise : buffer -> string promise
23+
val buffer_append : buffer -> string promise -> unit
24+
val buffer_contents : buffer -> string promise
2625
end
2726

2827
module type DECODABLE = sig

0 commit comments

Comments
 (0)