nish 0.46.0 → 0.47.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.
package/std/net/static.ts CHANGED
@@ -1411,9 +1411,14 @@ export class StaticFiles {
1411
1411
  }
1412
1412
  return
1413
1413
  }
1414
- if (status !== 304) {
1414
+ const path: u8[] = this.path
1415
+ const m: i32 = this.pathLength
1416
+ // `0 <= pathLength <= path.length` holds once `staticResolve` sets it;
1417
+ // the guard keeps `String.fromBytes` proven for a `--deny-panics` caller.
1418
+ if (status !== 304 && m >= 0 && m <= toI32(path.length)) {
1419
+ const type: string = staticContentType(String.fromBytes(path, 0, m))
1415
1420
  names.push(httpFieldBytes("content-type"))
1416
- values.push(httpFieldBytes(staticContentType(String.fromBytes(this.path, 0, this.pathLength))))
1421
+ values.push(httpFieldBytes(type))
1417
1422
  }
1418
1423
  if (this.etagLength > 0) {
1419
1424
  names.push(httpFieldBytes("etag"))
package/std/random.ts CHANGED
@@ -22,19 +22,15 @@
22
22
  * mixing function is the shape of SplitMix (Steele, Lea and Flood, "Fast
23
23
  * splittable pseudorandom number generators", OOPSLA 2014), on 32 bits.
24
24
  *
25
- * **The same bits under Node.** The two operations that wrap are spelled
26
- * `wrappingAdd` and `wrappingMul` from `nish:unsafe`, which define the
27
- * two's-complement wrap natively and answer `(a + b) | 0` and `Math.imul(a, b)`
28
- * under Node (docs/LANGUAGE.md, "nish:unsafe"); every shift has a constant
25
+ * **The same bits under Node.** A wrapping multiply is `Math.imul`, the low
26
+ * 32 bits of the product both ways. Every other sum, difference and remainder
27
+ * is taken in `f64` on the state read as an unsigned count below 2^32, where
28
+ * each answer is an integer below 2^33 and so exact, and comes back to an
29
+ * `i32` only once a compare has put it in range. Every shift has a constant
29
30
  * count, and `^` and `>>> 16` answer the same 32 bits in JavaScript as in the
30
- * compiled program.
31
- *
32
- * `nextInt` reads its span and its draws as unsigned counts with no `u32`
33
- * (whose conversions saturate under Node): the sign bit flipped orders them
34
- * under a signed compare, and the remainder is taken in pieces that stay below
35
- * 2^31. `nextF64` builds its double from integers below 2^53, so its products
36
- * and sums are exact both ways. The program a test prints under Node is the one
37
- * it prints compiled (docs/RUN_UNDER_NODE.md).
31
+ * compiled program. `nextF64` builds its double from integers below 2^53, so
32
+ * its products and sums are exact both ways. The program a test prints under
33
+ * Node is the one it prints compiled (docs/RUN_UNDER_NODE.md).
38
34
  *
39
35
  * **It is not for cryptography**: a key or a nonce comes from
40
36
  * `crypto.getRandomValues`.
@@ -45,10 +41,9 @@
45
41
  * would be a write their caller can see, and is refused as any such write is;
46
42
  * give each task its own, seeded from its index.
47
43
  */
48
- import { wrappingAdd, wrappingMul, wrappingSub } from "nish:unsafe"
49
44
 
50
- /** 0x9E3779B9 as an `i32`: the Weyl increment, odd, so the walk visits every state. */
51
- const GOLDEN: i32 = -1640531527
45
+ /** 0x9E3779B9, the Weyl increment: odd, so the walk visits every state. */
46
+ const GOLDEN: f64 = 2654435769.0
52
47
 
53
48
  /** 0x85EBCA6B as an `i32`: MurmurHash3's first finaliser multiplier (public domain). */
54
49
  const MIX_1: i32 = -2048144789
@@ -59,45 +54,32 @@ const MIX_2: i32 = -1028477387
59
54
  /** 2^26, the weight of the high draw in `nextF64`'s 53 bits. */
60
55
  const TWO_26: f64 = 67108864.0
61
56
 
62
- /** 2^53, which scales a 53-bit integer into [0, 1). */
63
- const TWO_53: f64 = 9007199254740992.0
57
+ /** 2^31: an unsigned count at least this is a negative `i32`. */
58
+ const TWO_31: f64 = 2147483648.0
64
59
 
65
- /** The sign bit: `x ^ SIGN` orders the bits of `x` as an unsigned count under a signed compare. */
66
- const SIGN: i32 = -2147483648
60
+ /** 2^32, the number of states. */
61
+ const TWO_32: f64 = 4294967296.0
67
62
 
68
- /** 2^31 - 1, the `i32` just below 2^31, from which `remainder` finds 2^31 mod m. */
69
- const MAX: i32 = 2147483647
63
+ /** 2^53, which scales a 53-bit integer into [0, 1). */
64
+ const TWO_53: f64 = 9007199254740992.0
70
65
 
71
- /** Whether `a < b` when both are read as unsigned 32-bit counts. */
72
- const below = (a: i32, b: i32): boolean => (a ^ SIGN) < (b ^ SIGN)
66
+ /** The bits of `x` read as an unsigned count, from 0 to 2^32 - 1. */
67
+ const unsigned = (x: i32): f64 => (x < 0 ? toF64(x) + TWO_32 : toF64(x))
73
68
 
74
69
  /**
75
- * `a mod m` with both read as unsigned 32-bit counts and `m` not 0. A count
76
- * of 2^31 or more is a negative `i32`, so it is taken apart into pieces a
77
- * signed `%` answers the same in JavaScript, and every sum stays below 2^31.
70
+ * The `i32` whose bits read as the unsigned count `u`, which is an integer
71
+ * from 0 to 2^32 - 1: below 2^31 it is `u`, and from there `u - 2^32`, so
72
+ * each `toI32` converts a value inside the `i32` range.
78
73
  */
79
- const remainder = (a: i32, m: i32): i32 => {
80
- if (m < 0) {
81
- // m is 2^31 or more, so a is less than 2m: at most one m comes off.
82
- return below(a, m) ? a : wrappingSub(a, m)
83
- }
84
- if (a >= 0) {
85
- return a % m
86
- }
87
- // a is 2^31 + low, so a mod m is low plus 2^31 mod m, added mod m.
88
- const low: i32 = (a ^ SIGN) % m
89
- const high: i32 = wrappingAdd(MAX % m, 1) % m
90
- const room: i32 = wrappingSub(m, high)
91
- return low >= room ? wrappingSub(low, room) : wrappingAdd(low, high)
92
- }
74
+ const bits = (u: f64): i32 => (u < TWO_31 ? toI32(u) : toI32(u - TWO_32))
93
75
 
94
76
  /** MurmurHash3's `fmix32`: every bit of `h` into every bit of the answer, a bijection on 32 bits. */
95
77
  const mix = (h: i32): i32 => {
96
78
  let x: i32 = h
97
79
  x = x ^ (x >>> 16)
98
- x = wrappingMul(x, MIX_1)
80
+ x = Math.imul(x, MIX_1)
99
81
  x = x ^ (x >>> 13)
100
- x = wrappingMul(x, MIX_2)
82
+ x = Math.imul(x, MIX_2)
101
83
  x = x ^ (x >>> 16)
102
84
  return x
103
85
  }
@@ -128,7 +110,8 @@ export class Rng {
128
110
  * steps once.
129
111
  */
130
112
  nextI32(): i32 {
131
- this.state = wrappingAdd(this.state, GOLDEN)
113
+ const next: f64 = unsigned(this.state) + GOLDEN
114
+ this.state = bits(next < TWO_32 ? next : next - TWO_32)
132
115
  return mix(this.state)
133
116
  }
134
117
 
@@ -168,16 +151,16 @@ export class Rng {
168
151
  if (lo > hi) {
169
152
  panic("Rng.nextInt: lo is greater than hi")
170
153
  }
171
- const span: i32 = wrappingAdd(wrappingSub(hi, lo), 1)
172
- if (span === 0) {
154
+ // From 1 to 2^32, and lo plus any remainder of it is at most hi.
155
+ const span: f64 = toF64(hi) - toF64(lo) + 1.0
156
+ if (span === TWO_32) {
173
157
  return this.nextI32()
174
158
  }
175
- // 2^32 mod span, as (2^32 - span) mod span.
176
- const refused: i32 = remainder(wrappingSub(0, span), span)
177
- let draw: i32 = this.nextI32()
178
- while (below(draw, refused)) {
179
- draw = this.nextI32()
159
+ const refused: f64 = TWO_32 % span
160
+ let draw: f64 = unsigned(this.nextI32())
161
+ while (draw < refused) {
162
+ draw = unsigned(this.nextI32())
180
163
  }
181
- return wrappingAdd(lo, remainder(draw, span))
164
+ return toI32(toF64(lo) + (draw % span))
182
165
  }
183
166
  }