about summary refs log tree commit diff stats
path: root/src/buffer/buffer.nim
diff options
context:
space:
mode:
Diffstat (limited to 'src/buffer/buffer.nim')
-rw-r--r--src/buffer/buffer.nim109
1 files changed, 60 insertions, 49 deletions
diff --git a/src/buffer/buffer.nim b/src/buffer/buffer.nim
index b0dc0696..7fda2887 100644
--- a/src/buffer/buffer.nim
+++ b/src/buffer/buffer.nim
@@ -32,6 +32,7 @@ import ips/serialize
 import ips/serversocket
 import ips/socketstream
 import js/regex
+import js/timeout
 import io/window
 import layout/box
 import render/renderdocument
@@ -70,7 +71,9 @@ type
     str*: string
 
   Buffer* = ref object
-    fd: int
+    rfd: int # file descriptor of command pipe
+    fd: int # file descriptor of buffer source
+    oldfd: int # fd after being unregistered
     alive: bool
     readbufsize: int
     contenttype: string
@@ -97,7 +100,6 @@ type
     loader: FileLoader
     config: BufferConfig
     userstyle: CSSStylesheet
-    timeouts: Table[int, (proc())]
     tasks: array[BufferCommand, int] #TODO this should have arguments
     savetask: bool
     hovertext: array[HoverType, string]
@@ -649,7 +651,7 @@ proc finishLoad(buffer: Buffer): EmptyPromise =
     buffer.sstream.setPosition(0)
     buffer.available = 0
     if buffer.window == nil:
-      buffer.window = newWindow(buffer.config.scripting)
+      buffer.window = newWindow(buffer.config.scripting, buffer.selector)
     let doc = parseHTML(buffer.sstream, charsets = buffer.charsets,
       window = buffer.window, url = buffer.url)
     buffer.document = doc
@@ -659,6 +661,7 @@ proc finishLoad(buffer: Buffer): EmptyPromise =
     p = EmptyPromise()
     p.resolve()
   buffer.selector.unregister(buffer.fd)
+  buffer.oldfd = buffer.fd
   buffer.fd = -1
   buffer.istream.close()
   return p
@@ -741,7 +744,7 @@ proc cancel*(buffer: Buffer): int {.proxy.} =
     buffer.sstream.setPosition(0)
     buffer.available = 0
     if buffer.window == nil:
-      buffer.window = newWindow(buffer.config.scripting)
+      buffer.window = newWindow(buffer.config.scripting, buffer.selector)
     buffer.document = parseHTML(buffer.sstream,
       charsets = buffer.charsets, window = buffer.window,
       url = buffer.url, canReinterpret = false)
@@ -1168,51 +1171,58 @@ proc readCommand(buffer: Buffer) =
   buffer.pstream.sread(packetid)
   bufferDispatcher(ProxyFunctions, buffer, cmd, packetid)
 
+proc handleRead(buffer: Buffer, fd: int) =
+  if fd == buffer.rfd:
+    try:
+      buffer.readCommand()
+    except EOFError:
+      #eprint "EOF error", $buffer.url & "\nMESSAGE:",
+      #       getCurrentExceptionMsg() & "\n",
+      #       getStackTrace(getCurrentException())
+      buffer.alive = false
+  elif fd == buffer.fd:
+    buffer.onload()
+  elif fd in buffer.loader.connecting:
+    buffer.loader.onConnected(fd)
+  elif fd in buffer.loader.ongoing:
+    #TODO something with readablestream?
+    discard
+  elif buffer.fd == -1 and buffer.oldfd == fd:
+    discard #TODO hack
+  else: assert false
+
+proc handleError(buffer: Buffer, fd: int) =
+  if fd == buffer.rfd:
+    # Connection reset by peer, probably. Close the buffer.
+    buffer.alive = false
+  elif fd == buffer.fd:
+    buffer.onload()
+  elif fd in buffer.loader.connecting:
+    # probably shouldn't happen. TODO
+    assert false
+  elif fd in buffer.loader.ongoing:
+    #TODO something with readablestream?
+    discard
+  elif buffer.fd == -1 and fd == buffer.oldfd:
+    discard #TODO hack
+  else:
+    assert false
+
 proc runBuffer(buffer: Buffer, rfd: int) =
-  block loop:
-    while buffer.alive:
-      let events = buffer.selector.select(-1)
-      for event in events:
-        if event.fd == rfd:
-          if Error in event.events:
-            # Connection reset by peer, probably. Close the buffer.
-            break loop
-          elif Read in event.events:
-            try:
-              buffer.readCommand()
-            except EOFError:
-              #eprint "EOF error", $buffer.url & "\nMESSAGE:",
-              #       getCurrentExceptionMsg() & "\n",
-              #       getStackTrace(getCurrentException())
-              break loop
-          else:
-            assert false
-        elif event.fd == buffer.fd:
-          if Read in event.events or Error in event.events:
-            buffer.onload()
-          else:
-            assert false
-        elif event.fd in buffer.loader.connecting:
-          if Event.Read in event.events:
-            buffer.loader.onConnected(event.fd)
-          else:
-            # probably shouldn't happen. TODO: maybe with Error?
-            assert false
-        elif event.fd in buffer.loader.ongoing:
-          #TODO something with readablestream?
-          discard
-        elif event.fd in buffer.timeouts:
-          if Event.Timer in event.events:
-            buffer.selector.unregister(event.fd)
-            var timeout: proc()
-            if buffer.timeouts.pop(event.fd, timeout):
-              timeout()
-            else:
-              assert false
-          else:
-            assert false
-        else:
-          assert false
+  buffer.rfd = rfd
+  while buffer.alive:
+    let events = buffer.selector.select(-1)
+    for event in events:
+      if Error in event.events:
+        buffer.handleError(event.fd)
+      if not buffer.alive:
+        break
+      if Read in event.events:
+        buffer.handleRead(event.fd)
+      if Event.Timer in event.events:
+        assert buffer.window != nil
+        assert buffer.window.timeouts.runTimeoutFd(event.fd)
+        buffer.window.runJSJobs()
   buffer.pstream.close()
   buffer.loader.quit()
   quit(0)
@@ -1238,7 +1248,8 @@ proc launchBuffer*(config: BufferConfig, source: BufferSource,
   loader.unregisterFun = proc(fd: int) = buffer.selector.unregister(fd)
   buffer.srenderer = newStreamRenderer(buffer.sstream, buffer.charsets)
   if buffer.config.scripting:
-    buffer.window = newWindow(buffer.config.scripting, some(buffer.loader))
+    buffer.window = newWindow(buffer.config.scripting, buffer.selector,
+      some(buffer.loader))
   let socks = connectSocketStream(mainproc, false)
   socks.swrite(getpid())
   buffer.pstream = socks
ision' href='/akkartik/mu/blame/100trace_browser.cc?h=main&id=797edfd041c2a8025c6273cea92a1a1ac481d5cc'>^
8f249677 ^
4243d710 ^


795f5244 ^
166e3c0d ^



4243d710 ^





8f249677 ^







4243d710 ^
b24eb476 ^

ab53600b ^
b24eb476 ^

9aa2fd34 ^
cd723793 ^



4243d710 ^



bf458036 ^
b24eb476 ^
4886e4e9 ^
3d8e5101 ^
3d8e5101 ^

bf458036 ^
3d8e5101 ^
4886e4e9 ^
293dbd7e ^
3d8e5101 ^

4243d710 ^
537ad74c ^
4243d710 ^
341239c9 ^

4243d710 ^

29f1b21e ^
1d2d6cf5 ^
b1ce29dd ^
1725a858 ^
22d1a275 ^

ab146dd2 ^
1725a858 ^
22d1a275 ^

ab146dd2 ^
1725a858 ^
9463d417 ^

ab146dd2 ^
1725a858 ^
9463d417 ^
053ffdc6 ^

9463d417 ^
ab146dd2 ^
1725a858 ^
9463d417 ^
053ffdc6 ^

9463d417 ^
ab146dd2 ^

b0ef6d58 ^



ab146dd2 ^
b734ab6b ^

0668b1b2 ^
ab146dd2 ^
b734ab6b ^
0668b1b2 ^

ab146dd2 ^
8bec2907 ^
ab53600b ^


ab146dd2 ^
8bec2907 ^
ab53600b ^

ab146dd2 ^
1725a858 ^
9463d417 ^
f22250a1 ^
9463d417 ^


ab146dd2 ^
1725a858 ^
4886e4e9 ^
9463d417 ^

f3760b0f ^
9463d417 ^
9463d417 ^
4d03f69c ^
9463d417 ^

ab146dd2 ^
1d2d6cf5 ^




ab146dd2 ^
63030ea7 ^

4886e4e9 ^
63030ea7 ^

f3760b0f ^
63030ea7 ^






ab146dd2 ^
1725a858 ^
f3760b0f ^
b24eb476 ^

293dbd7e ^

4886e4e9 ^
f3760b0f ^
293dbd7e ^
293dbd7e ^


b24eb476 ^
293dbd7e ^
4886e4e9 ^
293dbd7e ^

293dbd7e ^


293dbd7e ^

ab146dd2 ^
1725a858 ^
f3760b0f ^
b24eb476 ^

ff1f5976 ^

4886e4e9 ^
f3760b0f ^
12f93e4a ^
ff1f5976 ^
12f93e4a ^
b24eb476 ^
12f93e4a ^
4886e4e9 ^
ff1f5976 ^
12f93e4a ^


9aa2fd34 ^
cd723793 ^





9aa2fd34 ^

4b487ef5 ^
cd723793 ^
9aa2fd34 ^

4b487ef5 ^
cd723793 ^
9aa2fd34 ^
4243d710 ^



cd723793 ^
9aa2fd34 ^


fcffe1d7 ^
9aa2fd34 ^
537ad74c ^

9aa2fd34 ^



cd723793 ^
4b487ef5 ^
cd723793 ^

4b487ef5 ^




fcffe1d7 ^



fcffe1d7 ^





fcffe1d7 ^




fcffe1d7 ^



fcffe1d7 ^
4b487ef5 ^
fcffe1d7 ^
537ad74c ^
4b487ef5 ^
1485ee1b ^


537ad74c ^



4b487ef5 ^
4b487ef5 ^
9aa2fd34 ^
890f9b61 ^


537ad74c ^
890f9b61 ^
537ad74c ^
890f9b61 ^
890f9b61 ^



537ad74c ^

890f9b61 ^
537ad74c ^
890f9b61 ^
537ad74c ^

890f9b61 ^
4b487ef5 ^
fcffe1d7 ^
9aa2fd34 ^
537ad74c ^

fcffe1d7 ^


537ad74c ^
9aa2fd34 ^

9aa2fd34 ^



cd723793 ^









9aa2fd34 ^
























537ad74c ^
9aa2fd34 ^
537ad74c ^

9aa2fd34 ^

341239c9 ^

b24eb476 ^
341239c9 ^
4886e4e9 ^
4243d710 ^
f3760b0f ^
4243d710 ^



f22250a1 ^
341239c9 ^




b24eb476 ^
4886e4e9 ^
f3760b0f ^
f22250a1 ^
4243d710 ^

1164aefc ^
b24eb476 ^
1164aefc ^
9be4a67f ^




1164aefc ^
8ccf992d ^
4243d710 ^
4243d710 ^
22d1a275 ^
c01f82b0 ^
77f88e8c ^
4243d710 ^

4243d710 ^

b24eb476 ^
f3760b0f ^

f22250a1 ^
1164aefc ^
f22250a1 ^
1164aefc ^

77f88e8c ^
b24eb476 ^
1164aefc ^
77f88e8c ^

537ad74c ^
4886e4e9 ^
ab53600b ^
4243d710 ^
1164aefc ^
4886e4e9 ^

77f88e8c ^
537ad74c ^
77f88e8c ^
537ad74c ^
4243d710 ^
4886e4e9 ^
537ad74c ^
77f88e8c ^





















4243d710 ^
8f249677 ^








bf458036 ^







8f249677 ^








bf458036 ^
8f249677 ^
29f1b21e ^







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528