substrate-lang 0.9.4 → 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (188) hide show
  1. package/README.md +1 -1
  2. package/build/compiler/{binary.cdd2ffb408dd@0+0.wasm → binary.2d43b43d1600@0+0.wasm} +0 -0
  3. package/build/compiler/checker.4dd4556dd421@0+0.wasm +0 -0
  4. package/build/compiler/{compile.da85f9bc4478@0+0.wasm → compile.5d8f6407af2a@0+0.wasm} +0 -0
  5. package/build/compiler/{core.0410ffbdc488@0+0.wasm → core.f2defb75e00b@0+0.wasm} +0 -0
  6. package/build/compiler/expander.967ce443d379@0+0.wasm +0 -0
  7. package/build/compiler/fs.01b8aab4a9a8@0+0.wasm +0 -0
  8. package/build/compiler/{leb128.ba77430f659f@0+0.wasm → leb128.15d814b7b4f2@0+0.wasm} +0 -0
  9. package/build/compiler/{link.be6e641afa93@0+0.wasm → link.b826ea7c8f17@0+0.wasm} +0 -0
  10. package/build/compiler/{macros.b695763c0ca3@0+0.wasm → macros.b269e0c42784@0+0.wasm} +0 -0
  11. package/build/compiler/{main.cbbdf42e34c2@0+0.wasm → main.4fc52f204d54@0+0.wasm} +0 -0
  12. package/build/compiler/{ops.324f0a665fab@0+0.wasm → ops.515b5e05f646@0+0.wasm} +0 -0
  13. package/build/compiler/{path.1b5fd988b6f3@0+0.wasm → path.5a6286472cd4@0+0.wasm} +0 -0
  14. package/build/compiler/program +21 -21
  15. package/build/compiler/{prune.29dde307bdee@0+0.wasm → prune.406215c3e4f8@0+0.wasm} +0 -0
  16. package/build/compiler/reader.694b03770bf0@0+0.wasm +0 -0
  17. package/build/compiler/roots.cefd10e93684@0+0.wasm +0 -0
  18. package/build/compiler/{running.b99787c2d180@0+0.wasm → running.0d25dbaec018@0+0.wasm} +0 -0
  19. package/build/compiler/serialize.b0d82f3d63d2@0+0.wasm +0 -0
  20. package/build/compiler/{sha256.f6c742932dde@0+0.wasm → sha256.a6c32b04d799@0+0.wasm} +0 -0
  21. package/build/compiler/substrate.5ee5d0b4d3e7@0+0.wasm +0 -0
  22. package/build/compiler/{syntax.fadc832db29f@0+0.wasm → syntax.32f3e40b61c7@0+0.wasm} +0 -0
  23. package/build/compiler/{writer.550227768eb9@0+0.wasm → writer.00b7802cbe39@0+0.wasm} +0 -0
  24. package/build/sub/{base64.454b045156d2@0+0.wasm → base64.a26bdcf60a2d@0+0.wasm} +0 -0
  25. package/build/sub/build.459c5b9412bd@0+0.wasm +0 -0
  26. package/build/sub/chromium.3d5818f2707d@0+0.wasm +0 -0
  27. package/build/sub/common.1edc613607a0@0+0.wasm +0 -0
  28. package/build/sub/deflate.23f7c4679260@0+0.wasm +0 -0
  29. package/build/sub/doc.0775df5de6fe@0+0.wasm +0 -0
  30. package/build/sub/{doc.f18ad14060e9@0+0.wasm → doc.ef9965d5d402@0+0.wasm} +0 -0
  31. package/build/sub/env.e5451da6e747@0+0.wasm +0 -0
  32. package/build/sub/events.bf5473ebf220@0+0.wasm +0 -0
  33. package/build/sub/fetch.7790d4943758@0+0.wasm +0 -0
  34. package/build/sub/fetch.96b2d10f025e@0+0.wasm +0 -0
  35. package/build/sub/files.ad0469fdf6fa@0+0.wasm +0 -0
  36. package/build/sub/fs.01b8aab4a9a8@0+0.wasm +0 -0
  37. package/build/sub/git.dc4c0e757ebe@0+0.wasm +0 -0
  38. package/build/sub/grants.c54776c0d7e3@0+0.wasm +0 -0
  39. package/build/sub/gzip.16e372eda1cf@0+0.wasm +0 -0
  40. package/build/sub/hash.660682e4f491@0+0.wasm +0 -0
  41. package/build/sub/{html.0b0362932a75@0+0.wasm → html.a23126eb949a@0+0.wasm} +0 -0
  42. package/build/sub/{html.3b3c8d7d9971@0+0.wasm → html.e9ed37f34977@0+0.wasm} +0 -0
  43. package/build/sub/{inflate.3cfa17586072@0+0.wasm → inflate.59bb61f14001@0+0.wasm} +0 -0
  44. package/build/sub/json.55590d2f518a@0+0.wasm +0 -0
  45. package/build/sub/manifest.ef317480deb0@0+0.wasm +0 -0
  46. package/build/sub/{map.e582eae03ec2@0+0.wasm → map.f455cca6a42a@0+0.wasm} +0 -0
  47. package/build/sub/pack.e02836264f6a@0+0.wasm +0 -0
  48. package/build/sub/{path.1b5fd988b6f3@0+0.wasm → path.5a6286472cd4@0+0.wasm} +0 -0
  49. package/build/sub/path.6891ffea73ca@0+0.wasm +0 -0
  50. package/build/sub/process.7106571fa723@0+0.wasm +0 -0
  51. package/build/sub/program +48 -48
  52. package/build/sub/read.0b93933d62a2@0+0.wasm +0 -0
  53. package/build/sub/{reader.d256e69e09e2@0+0.wasm → reader.694b03770bf0@0+0.wasm} +0 -0
  54. package/build/sub/{root.02141d04d568@0+0.wasm → root.f10a4906f460@0+0.wasm} +0 -0
  55. package/build/sub/{roots.e366210c01d6@0+0.wasm → roots.cefd10e93684@0+0.wasm} +0 -0
  56. package/build/sub/{sha256.f6c742932dde@0+0.wasm → sha256.a6c32b04d799@0+0.wasm} +0 -0
  57. package/build/sub/{sha512.9fee24e95460@0+0.wasm → sha512.c24a792d7891@0+0.wasm} +0 -0
  58. package/build/sub/site.1732b58ec0e8@0+0.wasm +0 -0
  59. package/build/sub/site.ae3913b0b0f4@0+0.wasm +0 -0
  60. package/build/sub/store.a24d7a25daa6@0+0.wasm +0 -0
  61. package/build/sub/stream.126e3bd5d626@0+0.wasm +0 -0
  62. package/build/sub/sub.2e3092140706@0+0.wasm +0 -0
  63. package/build/sub/substrate.5ee5d0b4d3e7@0+0.wasm +0 -0
  64. package/build/sub/{syntax.fadc832db29f@0+0.wasm → syntax.32f3e40b61c7@0+0.wasm} +0 -0
  65. package/build/sub/tar.2a633861e654@0+0.wasm +0 -0
  66. package/build/sub/test.9bd59c73f65e@0+0.wasm +0 -0
  67. package/build/sub/{text.8ff142d72fd7@0+0.wasm → text.1bc90482bd43@0+0.wasm} +0 -0
  68. package/build/sub/{timer.d3454726b3bf@0+0.wasm → timer.f35eb62ae446@0+0.wasm} +0 -0
  69. package/build/sub/upgrade.39ee449b00d5@0+0.wasm +0 -0
  70. package/build/sub/{utf8.e82afeb9e953@0+0.wasm → utf8.e7216bd054e1@0+0.wasm} +0 -0
  71. package/build/sub/worker.762a9d642af0@0+0.wasm +0 -0
  72. package/build/sub/writer.00b7802cbe39@0+0.wasm +0 -0
  73. package/package.json +1 -1
  74. package/std/archive/tar.sub +30 -23
  75. package/std/browser/chromium.sub +5 -4
  76. package/std/browser/devtools.sub +24 -19
  77. package/std/compress/gzip.sub +18 -16
  78. package/std/compress/inflate.sub +7 -4
  79. package/std/crypto/password.sub +3 -3
  80. package/std/doc/doc.sub +2 -2
  81. package/std/encoding/json.sub +20 -8
  82. package/std/encoding/wire.sub +9 -7
  83. package/std/http/form.sub +2 -2
  84. package/std/http/multipart.sub +8 -10
  85. package/std/http/route.sub +9 -4
  86. package/std/http/server.sub +14 -11
  87. package/std/http/session.sub +2 -2
  88. package/std/http/static.sub +5 -5
  89. package/std/meta/derive.sub +0 -2
  90. package/std/meta/method.sub +41 -0
  91. package/std/meta/type-info.sub +30 -37
  92. package/std/package/files.sub +24 -25
  93. package/std/package/git.sub +26 -33
  94. package/std/package/hash.sub +8 -8
  95. package/std/package/manifest.sub +67 -42
  96. package/std/package/map.sub +1 -1
  97. package/std/package/pack.sub +13 -13
  98. package/std/package/read.sub +7 -6
  99. package/std/package/site.sub +7 -7
  100. package/std/package/store.sub +16 -12
  101. package/std/package.sub +6 -6
  102. package/std/test/browser.sub +9 -8
  103. package/std/test/build.sub +2 -2
  104. package/std/test/page.sub +31 -26
  105. package/std/test/run.sub +8 -7
  106. package/std/test/test.sub +12 -13
  107. package/std/vector/vector.sub +75 -0
  108. package/substrate/VERSION +1 -1
  109. package/substrate/compiler/checker.sub +40 -212
  110. package/substrate/compiler/compile.sub +3 -3
  111. package/substrate/compiler/expander.sub +32 -50
  112. package/substrate/compiler/main.sub +9 -9
  113. package/substrate/compiler/reader.sub +4 -4
  114. package/substrate/compiler/roots.sub +8 -8
  115. package/substrate/compiler/running.sub +1 -1
  116. package/substrate/compiler/serialize.sub +56 -49
  117. package/substrate/compiler/substrate.sub +53 -11
  118. package/substrate/host/errors.mjs +3 -2
  119. package/substrate/host/events.sub +3 -16
  120. package/substrate/host/fs.mjs +5 -4
  121. package/substrate/host/fs.sub +135 -102
  122. package/substrate/host/grants.mjs +1 -1
  123. package/substrate/host/grants.sub +8 -7
  124. package/substrate/host/modules/fake.mjs +1 -1
  125. package/substrate/host/modules/websocket.mjs +4 -4
  126. package/substrate/host/modules/websocket.sub +8 -6
  127. package/substrate/host/node/env.mjs +12 -6
  128. package/substrate/host/node/env.sub +21 -16
  129. package/substrate/host/node/fs.mjs +1 -1
  130. package/substrate/host/node/net.mjs +2 -2
  131. package/substrate/host/node/net.sub +13 -6
  132. package/substrate/host/node/process.mjs +4 -4
  133. package/substrate/host/node/process.sub +25 -24
  134. package/substrate/host/node/signal.mjs +3 -2
  135. package/substrate/host/node/signal.sub +9 -6
  136. package/substrate/host/node/watch.mjs +1 -1
  137. package/substrate/host/node/watch.sub +11 -9
  138. package/substrate/host/stream.sub +2 -0
  139. package/substrate/host/subhost-core.mjs +9 -1
  140. package/substrate/host/web/clipboard.mjs +2 -2
  141. package/substrate/host/web/clipboard.sub +10 -3
  142. package/substrate/host/web/files.mjs +2 -2
  143. package/substrate/host/web/files.sub +6 -2
  144. package/substrate/host/web/fs.mjs +3 -3
  145. package/substrate/host/web/location.mjs +3 -1
  146. package/substrate/host/web/location.sub +8 -4
  147. package/substrate/host/worker-side.mjs +3 -1
  148. package/substrate/host/worker.mjs +6 -5
  149. package/substrate/host/worker.sub +10 -10
  150. package/build/compiler/checker.2b0ea87ed311@0+0.wasm +0 -0
  151. package/build/compiler/expander.80707e1666d6@0+0.wasm +0 -0
  152. package/build/compiler/fs.5017b627cbd2@0+0.wasm +0 -0
  153. package/build/compiler/reader.d256e69e09e2@0+0.wasm +0 -0
  154. package/build/compiler/roots.e366210c01d6@0+0.wasm +0 -0
  155. package/build/compiler/serialize.a12536bbbe9a@0+0.wasm +0 -0
  156. package/build/compiler/substrate.728c690187f2@0+0.wasm +0 -0
  157. package/build/sub/build.115aeefc15e8@0+0.wasm +0 -0
  158. package/build/sub/chromium.fc0bfea0fbe6@0+0.wasm +0 -0
  159. package/build/sub/common.997c21b0b8cf@0+0.wasm +0 -0
  160. package/build/sub/deflate.3138a38325a1@0+0.wasm +0 -0
  161. package/build/sub/doc.e3f9a998cd91@0+0.wasm +0 -0
  162. package/build/sub/env.861d55fff974@0+0.wasm +0 -0
  163. package/build/sub/events.2c981f6f1ec7@0+0.wasm +0 -0
  164. package/build/sub/fetch.b0ce8f8b3da8@0+0.wasm +0 -0
  165. package/build/sub/fetch.d62aa5fc9623@0+0.wasm +0 -0
  166. package/build/sub/files.1706cc0941c0@0+0.wasm +0 -0
  167. package/build/sub/fs.5017b627cbd2@0+0.wasm +0 -0
  168. package/build/sub/git.1c5791236e35@0+0.wasm +0 -0
  169. package/build/sub/grants.cd99dff2854e@0+0.wasm +0 -0
  170. package/build/sub/gzip.9ebea150f218@0+0.wasm +0 -0
  171. package/build/sub/hash.ba8330dad9ff@0+0.wasm +0 -0
  172. package/build/sub/json.af2e0ae20084@0+0.wasm +0 -0
  173. package/build/sub/manifest.6e014ac19e5d@0+0.wasm +0 -0
  174. package/build/sub/pack.4295e4f5bdbd@0+0.wasm +0 -0
  175. package/build/sub/path.de22f6659534@0+0.wasm +0 -0
  176. package/build/sub/process.ca10ac0b8a62@0+0.wasm +0 -0
  177. package/build/sub/read.279fafcda508@0+0.wasm +0 -0
  178. package/build/sub/site.76aa8286c257@0+0.wasm +0 -0
  179. package/build/sub/site.e6e415bc3170@0+0.wasm +0 -0
  180. package/build/sub/store.c48db3345968@0+0.wasm +0 -0
  181. package/build/sub/stream.27fd2fe858e1@0+0.wasm +0 -0
  182. package/build/sub/sub.c0fb7310de12@0+0.wasm +0 -0
  183. package/build/sub/substrate.728c690187f2@0+0.wasm +0 -0
  184. package/build/sub/tar.692c0e8827cd@0+0.wasm +0 -0
  185. package/build/sub/test.1fd2ff8123bf@0+0.wasm +0 -0
  186. package/build/sub/upgrade.2a9219e6a45d@0+0.wasm +0 -0
  187. package/build/sub/worker.ea1e11f8f5e2@0+0.wasm +0 -0
  188. package/build/sub/writer.550227768eb9@0+0.wasm +0 -0
@@ -1,7 +1,6 @@
1
1
  ;; DEFLATE decoding (RFC 1951) of whole buffers, with lookup tables as zlib and libdeflate decode. inflate! decodes the
2
- ;; data at the start of in, adding at most most bytes to out, sets used to the bytes it took, and returns 0 or a negative
3
- ;; code whose message, zlib's, inflate-message gives.
4
- (provide inflate! inflate-message)
2
+ ;; data at the start of in, adding at most most bytes to out, and sets used to the bytes it took.
3
+ (provide inflate!)
5
4
 
6
5
  ;; Table entries hold a code's length in bits, an op and a value. Op 0 is a literal, 16 plus a count of extra bits a
7
6
  ;; base length or distance, 32 the end of a block, 64 plus a width a link to the table of that many bits at the value,
@@ -279,7 +278,11 @@
279
278
  (+= (-> out length) n)))))])))
280
279
  0))
281
280
 
282
- (define (inflate! [out (ptr buffer)] [in (slice u8)] [most u32] [used (ptr u32)]) -> i32
281
+ (define (inflate! [out (ptr buffer)] [in (slice u8)] [most u32] [used (ptr u32)] [err (ptr error)]) -> bool
282
+ (let ([code (inflate-code! out in most used)])
283
+ (or (== code 0) (error-fail! err (if (== code too-large) error-other error-invalid) (inflate-message code)))))
284
+
285
+ (define (inflate-code! [out (ptr buffer)] [in (slice u8)] [most u32] [used (ptr u32)]) -> i32
283
286
  (let ([r reader] [last #f] [result i32 0])
284
287
  (unless fixed (fixed!))
285
288
  (set! (-> r bits) 0)
@@ -62,10 +62,10 @@
62
62
  (set! last j)
63
63
  (when (== first j)
64
64
  (when (== running 0)
65
- (let ([why string])
65
+ (let ([why error] [id u32 0])
66
66
  (set! (-> listener handle) answered)
67
- (set! running (worker-start password-worker 0 0 (& listener) (& why)))
68
- (when (== running 0) (host-trap why))))
67
+ (unless (worker-start password-worker 0 0 (& listener) (& id) (& why)) (host-trap (error-text (& why))))
68
+ (set! running id)))
69
69
  (send-first!)))
70
70
 
71
71
  (define (unpadded! [b (ptr buffer)] [bytes (slice u8)])
package/std/doc/doc.sub CHANGED
@@ -45,7 +45,7 @@
45
45
  id))
46
46
 
47
47
  (define (text-of [id datum]) -> string
48
- (let ([b (slice u8)] [why string]) (if (< (fs-read-file (datum->string (host-file id)) (& b) (& why)) 0) "" (cast string b))))
48
+ (let ([b (slice u8)]) (if (fs-read-file (datum->string (host-file id)) (& b) 0) (cast string b) "")))
49
49
 
50
50
  (define (forms-of [text string] [id datum]) -> datum (program-forms (read-all text (string->symbol (datum->string id)))))
51
51
 
@@ -103,7 +103,7 @@
103
103
  (define (described [id datum]) -> datum
104
104
  (let ([known (assoc id descriptions)] [info fs-info] [forms datum 0] [d datum 0] [spec datum 0] [next datum 0])
105
105
  (unless (== known 0) (return (cdr known)))
106
- (when (and (== (fs-stat (datum->string (host-file id)) (& info) 0) 0) (== (-> info kind) fs-file))
106
+ (when (and (fs-stat (datum->string (host-file id)) (& info) 0) (== (-> info kind) fs-file))
107
107
  (set! forms (forms-of (text-of id) id))
108
108
  (set! d (doc-forms-of forms))
109
109
  (set! spec (language-of forms))
@@ -22,7 +22,7 @@
22
22
 
23
23
  (struct json-member [key string] [value json-value])
24
24
 
25
- (struct json-error [message string] [offset u32] [line u32] [column u32])
25
+ (struct json-error [error error] [offset u32] [line u32] [column u32])
26
26
 
27
27
  ;; ---- the token reader ----
28
28
 
@@ -49,12 +49,15 @@
49
49
  (set! (-> r token) "")
50
50
  (set! (-> r number) 0.0)
51
51
  (set! (-> r scratch) (make-buffer))
52
- (set! (-> r error message) "")
52
+ (set! (-> r error error code) 0)
53
+ (set! (-> r error error message) (make-buffer))
53
54
  (set! (-> r error offset) 0)
54
55
  (set! (-> r error line) 0)
55
56
  (set! (-> r error column) 0))
56
57
 
57
- (define (json-reader-free! [r (ptr json-reader)]) (buffer-free! (& (-> r scratch))))
58
+ (define (json-reader-free! [r (ptr json-reader)])
59
+ (buffer-free! (& (-> r scratch)))
60
+ (error-free! (& (-> r error error))))
58
61
 
59
62
  (define (json-next! [r (ptr json-reader)]) -> json-token
60
63
  (let ([s (-> r state)] [c (space! r)])
@@ -153,7 +156,7 @@
153
156
  (when (== (@ p i) 10)
154
157
  (+= line 1)
155
158
  (set! from (+ i 1))))
156
- (set! (-> r error message) message)
159
+ (error-fail! (& (-> r error error)) error-invalid message)
157
160
  (set! (-> r error offset) (-> r at))
158
161
  (set! (-> r error line) line)
159
162
  (set! (-> r error column) (+ (- (-> r at) from) 1))
@@ -162,6 +165,14 @@
162
165
 
163
166
  (define (reject! [r (ptr json-reader)] [message string]) -> bool (fail! r message) #f)
164
167
 
168
+ (define (failed-into! [r (ptr json-reader)] [err (ptr json-error)]) -> bool
169
+ (unless (== err 0)
170
+ (error-fail! (& (-> err error)) (-> r error error code) (error-text (& (-> r error error))))
171
+ (set! (-> err offset) (-> r error offset))
172
+ (set! (-> err line) (-> r error line))
173
+ (set! (-> err column) (-> r error column)))
174
+ #f)
175
+
165
176
  (define (string! [r (ptr json-reader)]) -> bool
166
177
  (let ([p (-> r text bytes)] [start (-> r at)] [i (-> r at)] [end (-> r text length)] [c u32 0] [k u32 0])
167
178
  (while (< i end)
@@ -306,7 +317,7 @@
306
317
 
307
318
  (define (arena-text [a (ptr arena)] [s string]) -> string (copy a s))
308
319
 
309
- (define (json-parse [a (ptr arena)] [text string] [err (ptr json-error)]) -> (ptr json-value)
320
+ (define (json-parse [a (ptr arena)] [text string] [value (ptr (ptr json-value))] [err (ptr json-error)]) -> bool
310
321
  (let ([r json-reader] [values (make-buffer)] [members (make-buffer)] [starts (array u32 1000)]
311
322
  [root (ptr json-value) 0] [t json-token] [v json-value (json-null)] [k u32 0] [m json-member] [placed #f])
312
323
  (json-reader-start! (& r) text)
@@ -345,7 +356,7 @@
345
356
  (set! (-> members length) (* (@ starts (-> r depth)) (sizeof json-member)))]
346
357
  [(== t json-token-end) (break)]
347
358
  [else
348
- (when (!= err 0) (set! (@ err) (-> r error)))
359
+ (failed-into! (& r) err)
349
360
  (set! root 0)
350
361
  (break)])
351
362
  (when placed
@@ -360,7 +371,8 @@
360
371
  (json-reader-free! (& r))
361
372
  (buffer-free! (& values))
362
373
  (buffer-free! (& members))
363
- root))
374
+ (set! (@ value) root)
375
+ (!= root 0)))
364
376
 
365
377
  ;; The k items of size bytes from index from of bytes, copied into a, or 0 for none.
366
378
  (define (block [a (ptr arena)] [bytes (ptr u8)] [from u32] [k u32] [size u32]) -> (ptr void)
@@ -722,7 +734,7 @@
722
734
  (define (end-of-text! [r (ptr json-reader)]) -> bool (== (json-next! r) json-token-end))
723
735
 
724
736
  (define (finish! [r (ptr json-reader)] [ok bool] [err (ptr json-error)]) -> bool
725
- (when (and (not ok) (!= err 0)) (set! (@ err) (-> r error)))
737
+ (unless ok (failed-into! r err))
726
738
  (json-reader-free! r)
727
739
  ok)
728
740
 
@@ -3,9 +3,9 @@
3
3
 
4
4
  (provide wire wire-error)
5
5
 
6
- (struct wire-error [message string] [offset u32])
6
+ (struct wire-error [error error] [offset u32])
7
7
 
8
- (struct wire-reader [bytes (slice u8)] [at u32] [error wire-error])
8
+ (struct wire-reader [bytes (slice u8)] [at u32] [message string] [offset u32])
9
9
 
10
10
  ;; ---- writing ----
11
11
 
@@ -28,12 +28,12 @@
28
28
  (define (start! [r (ptr wire-reader)] [bytes (slice u8)])
29
29
  (set! (-> r bytes) bytes)
30
30
  (set! (-> r at) 0)
31
- (set! (-> r error message) "")
32
- (set! (-> r error offset) 0))
31
+ (set! (-> r message) "")
32
+ (set! (-> r offset) 0))
33
33
 
34
34
  (define (fail! [r (ptr wire-reader)] [message string]) -> bool
35
- (set! (-> r error message) message)
36
- (set! (-> r error offset) (-> r at))
35
+ (set! (-> r message) message)
36
+ (set! (-> r offset) (-> r at))
37
37
  #f)
38
38
 
39
39
  (define (refuse! [r (ptr wire-reader)] [at u32] [message string]) -> bool
@@ -45,7 +45,9 @@
45
45
  (define (end! [r (ptr wire-reader)]) -> bool (or (== (left r) 0) (fail! r "bytes follow the message")))
46
46
 
47
47
  (define (read-failed [r (ptr wire-reader)] [err (ptr wire-error)]) -> bool
48
- (when (!= err 0) (set! (@ err) (-> r error)))
48
+ (unless (== err 0)
49
+ (error-fail! (& (-> err error)) error-invalid (-> r message))
50
+ (set! (-> err offset) (-> r offset)))
49
51
  #f)
50
52
 
51
53
  (define (take-byte! [r (ptr wire-reader)] [to (ptr u8)]) -> bool
package/std/http/form.sub CHANGED
@@ -10,11 +10,11 @@
10
10
 
11
11
  (struct http-upload [filename string] [type string] [bytes (slice u8)])
12
12
 
13
- (struct form-error [message string] [field string])
13
+ (struct form-error [error error] [field string])
14
14
 
15
15
  (define (refuse! [err (ptr form-error)] [message string] [field string]) -> bool
16
16
  (unless (== err 0)
17
- (set! (-> err message) message)
17
+ (error-fail! (& (-> err error)) error-invalid message)
18
18
  (set! (-> err field) field))
19
19
  #f)
20
20
 
@@ -11,9 +11,7 @@
11
11
  (define (multipart-boundary [a (ptr arena)] [content-type string]) -> string
12
12
  (if (http-media-type? content-type "multipart/form-data") (http-media-parameter a content-type "boundary") ""))
13
13
 
14
- (define (refused [why (ptr string)] [message string]) -> bool
15
- (set! (@ why) message)
16
- #f)
14
+ (define (refused [err (ptr error)] [message string]) -> bool (error-fail! err error-invalid message))
17
15
 
18
16
  (define (at? [body (slice u8)] [i u32] [s string]) -> bool
19
17
  (and (<= (+ i (-> s length)) (-> body length)) (== (cast string (slice body i (+ i (-> s length)))) s)))
@@ -66,28 +64,28 @@
66
64
  [(http-same? name "content-type") (set! (-> part type) (http-trimmed value))])))
67
65
  (set! start (+ i 1)))))))
68
66
 
69
- ;; A body's named parts, in the arena; #f with the reason in why when the body is malformed.
67
+ ;; A body's named parts, in the arena.
70
68
  (define (multipart-parse [a (ptr arena)] [body (slice u8)] [boundary string] [to (ptr (slice multipart-part))]
71
- [why (ptr string)]) -> bool
69
+ [err (ptr error)]) -> bool
72
70
  (set! (@ to) 0)
73
71
  (when (or (== (-> boundary length) 0) (> (-> boundary length) 70))
74
- (return (refused why "the boundary is missing or longer than 70 bytes")))
72
+ (return (refused err "the boundary is missing or longer than 70 bytes")))
75
73
  (let* ([dash (joined a "--" boundary)] [delimiter (joined a "\r\n" dash)] [n (-> body length)]
76
74
  [first (if (at? body 0 dash) 0 (find body delimiter 0))]
77
75
  [at u32 0] [items (ptr multipart-part) 0] [count u32 0] [room u32 0])
78
- (when (< first 0) (return (refused why "the body has no boundary")))
76
+ (when (< first 0) (return (refused err "the body has no boundary")))
79
77
  (set! at (+ (cast u32 first) (if (at? body 0 dash) 0 2) (-> dash length)))
80
78
  (while #t
81
79
  (when (at? body at "--") (break))
82
80
  (while (and (< at n) (or (== (@ body at) 32) (== (@ body at) 9))) (+= at 1))
83
- (unless (at? body at "\r\n") (return (refused why "a boundary's line goes on past it")))
81
+ (unless (at? body at "\r\n") (return (refused err "a boundary's line goes on past it")))
84
82
  (+= at 2)
85
83
  (let* ([empty (at? body at "\r\n")] [end (if empty (cast i64 at) (find body "\r\n\r\n" at))]
86
84
  [part multipart-part] [start u32 0] [next i64 0])
87
- (when (< end 0) (return (refused why "a part's head does not end")))
85
+ (when (< end 0) (return (refused err "a part's head does not end")))
88
86
  (set! start (+ (cast u32 end) (if empty 2 4)))
89
87
  (set! next (find body delimiter start))
90
- (when (< next 0) (return (refused why "the body ends inside a part")))
88
+ (when (< next 0) (return (refused err "the body ends inside a part")))
91
89
  (set! (-> part name) "")
92
90
  (set! (-> part filename) "")
93
91
  (set! (-> part type) "")
@@ -76,8 +76,9 @@
76
76
  (buffer-add-string! (& b) "field ")
77
77
  (buffer-add-string! (& b) (-> err field))
78
78
  (buffer-add-string! (& b) ": ")
79
- (buffer-add-string! (& b) (-> err message))
79
+ (buffer-add-string! (& b) (error-text (& (-> err error))))
80
80
  (http-answer-text! r 400 (buffer-string (& b)))
81
+ (error-free! (& (-> err error)))
81
82
  (buffer-free! (& b))))
82
83
 
83
84
  (define (route-json-refused! [r (ptr http-request)] [err (ptr json-error)])
@@ -87,8 +88,9 @@
87
88
  (buffer-add-byte! (& b) #\:)
88
89
  (write-digits (& b) (-> err column) 10)
89
90
  (buffer-add-string! (& b) ": ")
90
- (buffer-add-string! (& b) (-> err message))
91
+ (buffer-add-string! (& b) (error-text (& (-> err error))))
91
92
  (http-answer-text! r 400 (buffer-string (& b)))
93
+ (error-free! (& (-> err error)))
92
94
  (buffer-free! (& b))))
93
95
 
94
96
  (define (route-typed? [r (ptr http-request)] [json bool]) -> bool
@@ -103,12 +105,15 @@
103
105
 
104
106
  (define (route-form-fields [r (ptr http-request)] [body (slice u8)] [pairs (ptr (slice url-pair))]
105
107
  [parts (ptr (slice multipart-part))]) -> bool
106
- (let* ([a (& (-> r arena))] [t (http-request-header r "content-type")] [why string])
108
+ (let* ([a (& (-> r arena))] [t (http-request-header r "content-type")] [why error])
107
109
  (set! (@ pairs) 0)
108
110
  (set! (@ parts) 0)
109
111
  (if (http-media-type? t "multipart/form-data")
110
112
  (or (multipart-parse a body (multipart-boundary a t) parts (& why))
111
- (begin (http-answer-text! r 400 why) #f))
113
+ (begin
114
+ (http-answer-text! r 400 (error-text (& why)))
115
+ (error-free! (& why))
116
+ #f))
112
117
  (begin (set! (@ pairs) (url-form-parse a (cast string body))) #t))))
113
118
 
114
119
  (define (route-add-text! [b (ptr buffer)] [s string])
@@ -3,7 +3,8 @@
3
3
  ;; in ms, the default at 0 and none below 0: idle-timeout 5 s (130 s behind proxies), head-timeout 60 s,
4
4
  ;; request-timeout 300 s, send-timeout 60 s; body-limit for http-read-body!, 1 MiB at 0; compress, a gzip level for
5
5
  ;; answers of a type that compresses, of 1 KB or more, and for event streams that ask; proxies, how many proxies' hops
6
- ;; of X-Forwarded headers to believe; checks, run before handle. problem holds why listening failed.
6
+ ;; of X-Forwarded headers to believe; checks, run before handle. problem holds the failure when listening fails after
7
+ ;; http-serve! returns.
7
8
  ;; A request lasts until it is answered and its body has ended; on-done is called as it ends and must not trap. A trap
8
9
  ;; in a request's code answers 500, or ends the connection once the answer has begun, and goes to on-trap, the log, or
9
10
  ;; stderr.
@@ -65,7 +66,7 @@
65
66
 
66
67
  (struct http-server [head event-head] [listener u32] [port u32] [host string] [secure bool] [most u32]
67
68
  [idle-timeout f64] [head-timeout f64] [request-timeout f64] [send-timeout f64] [body-limit u32] [compress i32]
68
- [proxies u32] [checks (slice http-check)] [problem string] [user (ptr void)] [connections (ptr http-connection)]
69
+ [proxies u32] [checks (slice http-check)] [problem error] [user (ptr void)] [connections (ptr http-connection)]
69
70
  [hooks (ptr http-hook)] [stopping bool] [stop-timer u32] [stopped (fn ((ptr http-server)))] [log (ptr logger)]
70
71
  [handle (fn ((ptr http-request)))] [listening (fn ((ptr http-server)))] [on-trap (fn ((ptr http-request) string))]
71
72
  [on-done (fn ((ptr http-request)))])
@@ -977,19 +978,21 @@
977
978
  (when (!= (-> s listening) 0) ((-> s listening) s))]
978
979
  [(== kind net-accept) (if (-> s stopping) (stream-close (cast u32 value)) (accept! s (cast u32 value)))]
979
980
  [(== kind net-failed)
980
- (set! (-> s port) 0)
981
- (set! (-> s problem) (events-take-text))
982
- (when (!= (-> s listening) 0) ((-> s listening) s))])))
981
+ (let ([why (events-take-text)])
982
+ (set! (-> s port) 0)
983
+ (error-fail! (& (-> s problem)) value why)
984
+ (free (-> why bytes))
985
+ (when (!= (-> s listening) 0) ((-> s listening) s)))])))
983
986
 
984
- (define (http-serve! [s (ptr http-server)] [port u32] [handle (fn ((ptr http-request)))]) -> u32
987
+ (define (http-serve! [s (ptr http-server)] [port u32] [handle (fn ((ptr http-request)))] [err (ptr error)]) -> bool
985
988
  (trap-handler! http-trapped)
986
989
  (set! (-> s head handle) server-heard)
987
990
  (set! (-> s handle) handle)
988
991
  (set! (-> s connections) 0)
989
992
  (set! (-> s port) 0)
990
- (set! (-> s problem) "")
991
- (set! (-> s listener) (net-listen port (-> s host) (-> s secure) (-> s most) (cast (ptr void) s) (& (-> s problem))))
992
- (-> s listener))
993
+ (error-free! (& (-> s problem)))
994
+ (set! (-> s problem code) 0)
995
+ (net-listen port (-> s host) (-> s secure) (-> s most) (cast (ptr void) s) (& (-> s listener)) err))
993
996
 
994
997
  ;; http-server-close! stops listening and drops every connection at once, and runs the hooks forced.
995
998
  (define (http-server-close! [s (ptr http-server)])
@@ -1084,8 +1087,8 @@
1084
1087
  (set! (-> stopper server) s)
1085
1088
  (set! (-> stopper ms) ms)
1086
1089
  (set! (-> stopper done) done)
1087
- (set! (-> stopper term) (signal-listen "SIGTERM" (cast (ptr void) (& stopper)) 0))
1088
- (signal-listen "SIGINT" (cast (ptr void) (& stopper)) 0))
1090
+ (signal-listen "SIGTERM" (cast (ptr void) (& stopper)) (& (-> stopper term)) 0)
1091
+ (signal-listen "SIGINT" (cast (ptr void) (& stopper)) 0 0))
1089
1092
 
1090
1093
  ;; ---- connections without streams ----
1091
1094
 
@@ -97,9 +97,9 @@
97
97
  (raise-syntax-error 'session "this type needs a json form, which (json T) defines: write it first" t))
98
98
  `(,#`(define (#,(derive-name t '"session-read") [s (ptr http-sessions)] [r (ptr http-request)] [v (ptr #,t)])
99
99
  -> bool
100
- (let ([bytes (slice u8)] [e json-error])
100
+ (let ([bytes (slice u8)])
101
101
  (and (http-session-read s r (& bytes))
102
- (#,(derive-name t '"json-read") (& (-> r arena)) (cast string bytes) v (& e)))))
102
+ (#,(derive-name t '"json-read") (& (-> r arena)) (cast string bytes) v 0))))
103
103
  ,#`(define (#,(derive-name t '"session-write!") [s (ptr http-sessions)] [r (ptr http-request)] [v (ptr #,t)])
104
104
  (let ([b (make-buffer)])
105
105
  (#,(derive-name t '"json-write!") (& b) v)
@@ -200,15 +200,15 @@
200
200
  (http-finish! r)]
201
201
  [(<= size whole)
202
202
  (let ([f (slice u8)])
203
- (if (< (fs-read-file name (& f) 0) 0)
203
+ (if (not (fs-read-file name (& f) 0))
204
204
  (failed! r)
205
205
  (begin
206
206
  (head-of! r name partial s size)
207
207
  (http-send! r (slice f (cast u32 (-> s start)) (cast u32 (-> s end))))
208
208
  (free (-> f items)))))]
209
209
  [else
210
- (let* ([h (fs-open-file name fs-read-flag 0)] [file (if (== h 0) 0 (fs-read-stream h (-> s start) 0 0))])
211
- (if (== file 0)
210
+ (let ([h u32 0] [file u32 0])
211
+ (if (not (and (fs-open-file name fs-read-flag (& h) 0) (fs-read-stream h (-> s start) 0 (& file) 0)))
212
212
  (failed! r)
213
213
  (begin
214
214
  (head-of! r name partial s size)
@@ -262,7 +262,7 @@
262
262
  (if (and ok (> n 0) (== (refusal (slice parts n)) 0))
263
263
  (begin
264
264
  (joined! (& name) (slice parts n))
265
- (if (and (>= (fs-stat (buffer-string (& name)) (& info) 0) 0) (== (-> info kind) fs-file))
265
+ (if (and (fs-stat (buffer-string (& name)) (& info) 0) (== (-> info kind) fs-file))
266
266
  (send! r files (buffer-string (& name)) (slice (buffer-string (& name)) (+ prefix 1)) (& info))
267
267
  (not-found! r)))
268
268
  (not-found! r))))
@@ -282,7 +282,7 @@
282
282
  (joined! (& name) parts)
283
283
  (when (== (-> parts length) 0) (buffer-add-byte! (& name) #\/))
284
284
  (when slash (buffer-add-string! (& name) "index.html"))
285
- (let ([found (>= (fs-stat (buffer-string (& name)) (& info) 0) 0)])
285
+ (let ([found (fs-stat (buffer-string (& name)) (& info) 0)])
286
286
  (cond [(and found (== (-> info kind) fs-directory) (not slash)) (moved! r)]
287
287
  [(or (not found) (!= (-> info kind) fs-file)) (not-found! r)]
288
288
  [else
@@ -21,7 +21,6 @@
21
21
  (while (< i (length wanted))
22
22
  (let* ([u (list-ref wanted i)] [info (type-info u)])
23
23
  (when (or (== info 0) (== (car info) 'union)) (raise-syntax-error who "expected a struct or variant type" u))
24
- (unless (null? (cadddr info)) (raise-syntax-error who "expected a type without parameters" u))
25
24
  (set! out (append out (code u info))))
26
25
  (+= i 1))
27
26
  #`(begin #,@out))]
@@ -44,7 +43,6 @@
44
43
  (define (type-syntax [type datum]) -> datum
45
44
  (cond [(symbol? type) (datum->syntax #'here type 0)]
46
45
  [(identifier? type) type]
47
- [(identifier? (car type)) #`(#,(car type) #,@(for/list ([u (cdr type)]) (type-syntax u)))]
48
46
  [(== (car type) 'array) #`(array #,(type-syntax (cadr type)) #,(datum->syntax #'here (caddr type) 0))]
49
47
  [(== (car type) 'other) (cadr type)]
50
48
  [else #`(#,(datum->syntax #'here (car type) 0) #,(type-syntax (cadr type)))]))
@@ -0,0 +1,41 @@
1
+ ;; Operations that pick their function by the type of their first argument, as method calls do. (method form suffix x
2
+ ;; arg ...) calls the function whose name is x's type followed by the suffix, for x a pointer to a named type. With
3
+ ;; suffix -push! and x a (ptr points), it calls points-push!. form is the operation's own form, for errors.
4
+ ;; (define-methods [operation suffix] ...) defines each operation as a macro that calls method with its suffix.
5
+ ;; method-name in std/meta/type-info gives the function's name, for macros that define the functions.
6
+ (provide method define-methods)
7
+
8
+ (begin-for-syntax
9
+ (require-unqualified compiler/macros)
10
+ (require-unqualified "type-info.sub")
11
+
12
+ (define (method-form [s datum]) -> datum
13
+ (syntax-match s
14
+ [(_ form suffix x arg ...) #`(let ([the-receiver #,x]) (method-call #,form #,suffix the-receiver #,@arg))]))
15
+
16
+ (define (method-call-form [s datum]) -> datum
17
+ (syntax-match s
18
+ [(_ form suffix x arg ...)
19
+ (let ([t (variable-type x)] [op (syntax-e (car (syntax->list form)))])
20
+ (unless (and (pair? t) (== (car t) 'ptr) (identifier? (cadr t)))
21
+ (raise-syntax-error op (datum->string (format "expected a pointer to a named type, not ~s" `(,(type-datum t))))
22
+ form))
23
+ (let* ([name (method-name (cadr t) (syntax-e suffix))] [f (datum->syntax name (syntax-e name) form)])
24
+ (when (== (identifier-binding-kind f) '#f)
25
+ (raise-syntax-error op (format-message "~a has no ~a" `(,(syntax-e (cadr t)) ,(syntax-e f))) form))
26
+ #`(#,f #,x #,@arg)))]))
27
+
28
+ (define (operation-form [suffix datum] [s datum]) -> datum
29
+ (syntax-match s
30
+ [(_ x arg ...) #`(method #,s #,(datum->syntax s suffix s) #,x #,@arg)]
31
+ [_ (raise-syntax-error 0 "expected a pointer to a named type" s)]))
32
+
33
+ (define (define-methods-form [s datum]) -> datum
34
+ (syntax-match s
35
+ [(_ ((? identifier? op) (? identifier? suffix)) ...)
36
+ #`(begin #,@(for/list ([o op] [x suffix]) #`(define-syntax #,o operation-form '#,x)))]
37
+ [_ (raise-syntax-error 'define-methods "expected (define-methods [operation suffix] ...)" s)])))
38
+
39
+ (define-syntax method method-form)
40
+ (define-syntax method-call method-call-form)
41
+ (define-syntax define-methods define-methods-form)
@@ -1,54 +1,44 @@
1
1
  ;; Types as data at compile time, for macros that write code from a type's fields.
2
2
  (require-unqualified compiler/macros)
3
3
 
4
- (provide type-info variable-type type-datum)
4
+ (provide type-info variable-type type-datum method-name)
5
5
 
6
- ;; (kind name items parameters), kind struct, union, variant or enum, or 0 when t names none. A struct's or union's items
7
- ;; are (field . type), a variant's (case (field . type) ...) and an enum's (case). A type is i8 to u64, f32, f64, bool or
8
- ;; string, (ptr type), (slice type), (array type n), a struct's, union's, variant's or enum's identifier,
9
- ;; (identifier type ...) for one with parameters, or (other type). For (identifier type ...), the items have those types
10
- ;; in place of the parameters, and the parameters are ().
6
+ ;; (kind name items), kind struct, union, variant or enum, or 0 when t names none. A struct's or union's items are
7
+ ;; (field . type), a variant's (case (field . type) ...) and an enum's (case). A type is i8 to u64, f32, f64, bool or
8
+ ;; string, (ptr type), (slice type), (array type n), a struct's, union's, variant's or enum's identifier, or
9
+ ;; (other type).
11
10
  (define (type-info [t datum]) -> datum
12
- (let ([instance (pair? t)])
13
- (syntax-match (binding-of (if instance (car t) t))
14
- [('substrate-struct name (item ...) kind (p ...) . _)
15
- (let ([k (syntax-e kind)] [env (if instance (for/list ([q p] [a (cdr t)]) (cons (syntax-e q) a)) nil)])
16
- `(,k ,name ,(for/list ([i item]) (if (!= (memq k '(variant enum)) 0) (case-info i env) (field-info i env)))
17
- ,(if instance nil p)))]
18
- [('substrate-typedef u)
19
- (let ([d (parsed-type (syntax->datum u))])
20
- (if (or (identifier? d) (and (pair? d) (identifier? (car d)))) (type-info d) 0))]
21
- [_ 0])))
11
+ (syntax-match (binding-of t)
12
+ [('substrate-struct name (item ...) kind . _)
13
+ (let ([k (syntax-e kind)])
14
+ `(,k ,name ,(for/list ([i item]) (if (!= (memq k '(variant enum)) 0) (case-info i) (field-info i)))))]
15
+ [('substrate-typedef u) (let ([d (parsed-type (syntax->datum u))]) (if (identifier? d) (type-info d) 0))]
16
+ [_ 0]))
22
17
 
23
18
  (define (binding-of [t datum]) -> datum
24
19
  (if (and (identifier? t) (== (identifier-binding-kind t) 'compile-time-value)) (syntax-local-value t) 0))
25
20
 
26
- (define (case-info [c datum] [env datum]) -> datum
27
- (let ([l (syntax->list c)]) (cons (car l) (for/list ([f (cdr l)]) (field-info f env)))))
21
+ (define (case-info [c datum]) -> datum
22
+ (let ([l (syntax->list c)]) (cons (car l) (for/list ([f (cdr l)]) (field-info f)))))
28
23
 
29
- (define (field-info [f datum] [env datum]) -> datum
30
- (let ([l (syntax->list f)]) (cons (car l) (type-of (cadr l) env))))
24
+ (define (field-info [f datum]) -> datum
25
+ (let ([l (syntax->list f)]) (cons (car l) (type-of (cadr l)))))
31
26
 
32
27
  (define scalars datum '(i8 u8 i16 u16 i32 u32 i64 u64 f32 f64 bool string))
33
28
 
34
- (define (type-of [t datum] [env datum]) -> datum
29
+ (define (type-of [t datum]) -> datum
35
30
  (syntax-match t
36
31
  [(? identifier? x)
37
- (cond [(!= (memq (syntax-e x) scalars) 0) (syntax-e x)]
38
- [(!= (assq (syntax-e x) env) 0) (cdr (assq (syntax-e x) env))]
39
- [else (syntax-match (binding-of x)
40
- [('substrate-struct _ _ _ () . _) x]
41
- [('substrate-typedef u) (parsed-type (syntax->datum u))]
42
- [_ `(other ,t)])])]
43
- [('ptr u) `(ptr ,(type-of u env))]
44
- [('slice u) `(slice ,(type-of u env))]
45
- [('array u n) `(array ,(type-of u env) ,(syntax-e n))]
32
+ (if (!= (memq (syntax-e x) scalars) 0)
33
+ (syntax-e x)
34
+ (syntax-match (binding-of x)
35
+ [('substrate-struct . _) x]
36
+ [('substrate-typedef u) (parsed-type (syntax->datum u))]
37
+ [_ `(other ,t)]))]
38
+ [('ptr u) `(ptr ,(type-of u))]
39
+ [('slice u) `(slice ,(type-of u))]
40
+ [('array u n) `(array ,(type-of u) ,(syntax-e n))]
46
41
  [('quote u) (parsed-type (syntax->datum u))]
47
- [((? identifier? h) a ...)
48
- (syntax-match (binding-of h)
49
- [('substrate-struct _ _ _ (p ...) . _)
50
- (if (and (pair? p) (== (length p) (length a))) `(,h ,@(for/list ([u a]) (type-of u env))) `(other ,t))]
51
- [_ `(other ,t)])]
52
42
  [_ `(other ,t)]))
53
43
 
54
44
  (define (variable-type [x datum]) -> datum
@@ -57,11 +47,14 @@
57
47
  (define (type-datum [t datum]) -> datum
58
48
  (let ([d (syntax->datum (datum->syntax #'here t 0))]) (if (and (pair? d) (== (car d) 'other)) (cadr d) d)))
59
49
 
50
+ ;; The function std/meta/method calls for type t and a suffix: points-push! for points and -push!.
51
+ (define (method-name [t datum] [suffix datum]) -> datum
52
+ (datum->syntax t (string->symbol (format-message "~a~a" `(,(syntax-e t) ,suffix))) t))
53
+
60
54
  ;; A type as the checker holds it.
61
55
  (define (parsed-type [d datum]) -> datum
62
56
  (cond [(and (symbol? d) (!= (memq d scalars) 0)) d]
63
57
  [(and (pair? d) (!= (memq (car d) '(ptr slice)) 0)) `(,(car d) ,(parsed-type (cadr d)))]
64
58
  [(and (pair? d) (== (car d) 'array)) `(array ,(parsed-type (cadr d)) ,(caddr d))]
65
- [(and (pair? d) (memq (car d) '(struct enum)) (null? (cdddr d))) (name-identifier (cadr d))]
66
- [(and (pair? d) (== (car d) 'struct)) `(,(name-identifier (cadr d)) ,@(for/list ([a (cadddr d)]) (parsed-type a)))]
59
+ [(and (pair? d) (!= (memq (car d) '(struct enum)) 0)) (name-identifier (cadr d))]
67
60
  [else `(other ,d)]))
@@ -19,17 +19,17 @@
19
19
  ;; tracked-files adds the files package-tree finds. When a git repository tracks dir's marker file, it adds only the files
20
20
  ;; git tracks. It gives #f when it cannot read git's index.
21
21
  (define (tracked-files [a (ptr arena)] [dir string] [marker string] [files (ptr buffer)] [err (ptr buffer)]) -> bool
22
- (let ([all (make-buffer)] [nested (make-buffer)] [tracked (make-buffer)] [got i32 0])
22
+ (let ([all (make-buffer)] [nested (make-buffer)] [tracked (make-buffer)] [marked #f] [ok #f])
23
23
  (package-tree a dir (& all) (& nested))
24
- (set! got (git-tracked a dir marker (& tracked) err))
24
+ (set! ok (git-tracked a dir marker (& tracked) (& marked) err))
25
25
  (let ([ps (cast (ptr string) (-> all bytes))] [ts (cast (ptr string) (-> tracked bytes))]
26
26
  [t (/ (-> tracked length) (sizeof string))])
27
- (for ([i u32 0]) (and (>= got 0) (< i (/ (-> all length) (sizeof string)))) (+= i 1)
28
- (when (or (== got 0) (listed? ts t (@ ps i))) (added! files (@ ps i)))))
27
+ (for ([i u32 0]) (and ok (< i (/ (-> all length) (sizeof string)))) (+= i 1)
28
+ (when (or (not marked) (listed? ts t (@ ps i))) (added! files (@ ps i)))))
29
29
  (buffer-free! (& all))
30
30
  (buffer-free! (& nested))
31
31
  (buffer-free! (& tracked))
32
- (>= got 0)))
32
+ ok))
33
33
 
34
34
  (define (listed? [ts (ptr string)] [n u32] [p string]) -> bool
35
35
  (let ([lo u32 0] [hi n])
@@ -46,25 +46,24 @@
46
46
  (tree! a dir "" files nested))
47
47
 
48
48
  (define (tree! [a (ptr arena)] [dir string] [rel string] [files (ptr buffer)] [nested (ptr buffer)])
49
- (let ([here (if (== (-> rel length) 0) dir (joined-path dir rel))] [names (slice string)] [why string])
50
- (if (< (fs-list here (& names) (& why)) 0)
51
- (free (-> why bytes))
52
- (for ([i u32 0]) (< i (-> names length)) (+= i 1)
53
- (let* ([n (@ names i)] [r (if (== (-> rel length) 0) (kept a n) (kept-joined a rel n))] [full (joined-path dir r)]
54
- [info fs-info])
55
- (when (and (not (and (== (-> rel length) 0) (== n "build"))) (!= n ".git") (== (fs-lstat full (& info) 0) 0))
56
- (cond [(== (-> info kind) fs-file) (when (!= files 0) (added! files r))]
57
- [(== (-> info kind) fs-directory)
58
- (let ([own (joined-path full "package.sub")] [m manifest] [err (make-buffer)] [p nested-package])
59
- (if (and (== (fs-stat own (& info) 0) 0) (== (-> info kind) fs-file))
60
- (begin
61
- (set! (-> p name) (if (manifest-read a own (& m) (& err)) (-> m name) (kept a n)))
62
- (set! (-> p path) r)
63
- (buffer-add! nested (slice (cast (ptr u8) (& p)) (sizeof nested-package))))
64
- (tree! a dir r files nested))
65
- (buffer-free! (& err))
66
- (free (-> own bytes)))]))
67
- (free (-> full bytes)))))
49
+ (let ([here (if (== (-> rel length) 0) dir (joined-path dir rel))] [names (slice string)])
50
+ (when (fs-list here (& names) 0)
51
+ (for ([i u32 0]) (< i (-> names length)) (+= i 1)
52
+ (let* ([n (@ names i)] [r (if (== (-> rel length) 0) (kept a n) (kept-joined a rel n))] [full (joined-path dir r)]
53
+ [info fs-info])
54
+ (when (and (not (and (== (-> rel length) 0) (== n "build"))) (!= n ".git") (fs-lstat full (& info) 0))
55
+ (cond [(== (-> info kind) fs-file) (when (!= files 0) (added! files r))]
56
+ [(== (-> info kind) fs-directory)
57
+ (let ([own (joined-path full "package.sub")] [m manifest] [err (make-buffer)] [p nested-package])
58
+ (if (and (fs-stat own (& info) 0) (== (-> info kind) fs-file))
59
+ (begin
60
+ (set! (-> p name) (if (manifest-read a own (& m) (& err)) (-> m name) (kept a n)))
61
+ (set! (-> p path) r)
62
+ (buffer-add! nested (slice (cast (ptr u8) (& p)) (sizeof nested-package))))
63
+ (tree! a dir r files nested))
64
+ (buffer-free! (& err))
65
+ (free (-> own bytes)))]))
66
+ (free (-> full bytes)))))
68
67
  (unless (== (-> rel length) 0) (free (-> here bytes)))))
69
68
 
70
69
  (define (package-exports [a (ptr arena)] [dir string] [m (ptr manifest)] [exports (ptr buffer)] [err (ptr buffer)]) -> bool
@@ -88,7 +87,7 @@
88
87
 
89
88
  (define (file-in? [dir string] [p string]) -> bool
90
89
  (let ([full (joined-path dir p)] [info fs-info] [found #f])
91
- (set! found (and (== (fs-stat full (& info) 0) 0) (== (-> info kind) fs-file)))
90
+ (set! found (and (fs-stat full (& info) 0) (== (-> info kind) fs-file)))
92
91
  (free (-> full bytes))
93
92
  found))
94
93