quickjs-pure 0.8.0

Embeddable JavaScript engine written in pure Rust (QuickJS-compatible)
Documentation
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
//! `Math`.
//!
//! Two behaviours are easy to get wrong and both are covered by the reference's test
//! suite: `Math.round` rounds halves *up* rather than away from zero, so
//! `Math.round(-0.5)` is `-0` and not `-1`; and `Math.min`/`max` return `NaN` if any
//! argument is `NaN`, even though `Math.min(1, NaN)` could be read as `1`.

use crate::builtins::{arg, define_method, install_tag};
use crate::interp::{Interp, JsResult};
use crate::object::Property;
use crate::value::{Value, to_int32};

/// Install `Math`.
pub fn install(interp: &mut Interp<'_>) {
    let object_proto = interp.realm.object_proto.clone();
    let math = interp.new_object(Some(object_proto));
    // §21.3: `Object.prototype.toString.call(Math)` is `[object Math]`.
    install_tag(&math, "Math", interp);

    for (name, value) in [
        ("PI", std::f64::consts::PI),
        ("E", std::f64::consts::E),
        ("LN2", std::f64::consts::LN_2),
        ("LN10", std::f64::consts::LN_10),
        ("LOG2E", std::f64::consts::LOG2_E),
        ("LOG10E", std::f64::consts::LOG10_E),
        ("SQRT2", std::f64::consts::SQRT_2),
        ("SQRT1_2", std::f64::consts::FRAC_1_SQRT_2),
    ] {
        math.define(
            name,
            Property::with_attributes(Value::Float(value), false, false),
        );
    }
    for (name, length, func) in [
        ("abs", 1, abs as crate::object::NativeFn),
        ("floor", 1, floor),
        ("ceil", 1, ceil),
        ("round", 1, round),
        ("trunc", 1, trunc),
        ("sign", 1, sign),
        ("sqrt", 1, sqrt),
        ("cbrt", 1, cbrt),
        ("pow", 2, pow),
        ("exp", 1, exp),
        ("log", 1, log),
        ("log2", 1, log2),
        ("log10", 1, log10),
        ("sin", 1, sin),
        ("cos", 1, cos),
        ("tan", 1, tan),
        ("asin", 1, asin),
        ("acos", 1, acos),
        ("atan", 1, atan),
        ("atan2", 2, atan2),
        ("min", 2, min),
        ("max", 2, max),
        ("imul", 2, imul),
        ("hypot", 2, hypot),
        ("fround", 1, fround),
        ("clz32", 1, clz32),
        ("random", 0, random),
        ("sumPrecise", 1, sum_precise),
    ] {
        define_method(&math, name, length, func, interp);
    }

    let global = interp.realm.global.clone();
    global.define(
        "Math",
        Property::with_attributes(Value::Object(math), true, false),
    );
}

/// `ToNumber` for a `Math` argument.
///
/// `Value::to_number` is total and answers `NaN` for a BigInt, which left the engine
/// silently wrong here: `Math.max(1n)` is a `TypeError`, not `NaN`, because there is no
/// lossless number for a 2^70 integer. Every other `ToNumber` caller was moved onto
/// [`Interp::to_js_number`] when this was found; `Math` was missed because it does its own
/// coercion, so it kept answering `NaN` long after the rest of the engine refused.
/// `ToUint32`, which is `ToInt32` reinterpreted.
fn to_uint32(value: f64) -> u32 {
    to_int32(value) as u32
}

fn to_number(interp: &mut Interp<'_>, value: &Value) -> JsResult<f64> {
    interp.to_js_number(value)
}

/// [`to_number`] for the common case where the argument is already owned.
fn number_of(interp: &mut Interp<'_>, value: Value) -> JsResult<f64> {
    to_number(interp, &value)
}

fn unary(interp: &mut Interp<'_>, args: &[Value], f: impl Fn(f64) -> f64) -> JsResult<Value> {
    let value = number_of(interp, arg(args, 0))?;
    Ok(Value::Float(f(value)))
}

fn abs(interp: &mut Interp<'_>, _this: Value, args: &[Value]) -> JsResult<Value> {
    // `abs(-0)` is `+0`, and `abs` of an integer stays an integer.
    Ok(match arg(args, 0) {
        Value::Int(value) => Value::Int(value.saturating_abs()),
        other => Value::Float(number_of(interp, other)?.abs()),
    })
}

fn floor(interp: &mut Interp<'_>, _this: Value, args: &[Value]) -> JsResult<Value> {
    unary(interp, args, f64::floor)
}

fn ceil(interp: &mut Interp<'_>, _this: Value, args: &[Value]) -> JsResult<Value> {
    unary(interp, args, f64::ceil)
}

/// `Math.round`, which rounds halves toward `+∞`.
///
/// `(x + 0.5).floor()` would be wrong for negatives (`-0.5` would become `-1`), so
/// the half is added and the result clamped back for zero to keep `-0`.
fn round(interp: &mut Interp<'_>, _this: Value, args: &[Value]) -> JsResult<Value> {
    let value = number_of(interp, arg(args, 0))?;
    if !value.is_finite() {
        return Ok(Value::Float(value));
    }
    let rounded = (value + 0.5).floor();
    // `Math.round(-0.4)` is `-0`, not `0`.
    let rounded = if rounded == 0.0 && value < 0.0 {
        -0.0
    } else {
        rounded
    };
    let _ = interp;
    Ok(Value::Float(rounded))
}

fn trunc(interp: &mut Interp<'_>, _this: Value, args: &[Value]) -> JsResult<Value> {
    unary(interp, args, f64::trunc)
}

fn sign(interp: &mut Interp<'_>, _this: Value, args: &[Value]) -> JsResult<Value> {
    let value = number_of(interp, arg(args, 0))?;
    Ok(Value::Float(if value.is_nan() {
        f64::NAN
    } else if value > 0.0 {
        1.0
    } else if value < 0.0 {
        -1.0
    } else {
        // `sign(-0)` is `-0`, `sign(0)` is `0`.
        value
    }))
}

fn sqrt(interp: &mut Interp<'_>, _this: Value, args: &[Value]) -> JsResult<Value> {
    unary(interp, args, f64::sqrt)
}

fn cbrt(interp: &mut Interp<'_>, _this: Value, args: &[Value]) -> JsResult<Value> {
    unary(interp, args, f64::cbrt)
}

fn pow(interp: &mut Interp<'_>, _this: Value, args: &[Value]) -> JsResult<Value> {
    let base = number_of(interp, arg(args, 0))?;
    let exponent = number_of(interp, arg(args, 1))?;
    Ok(Value::Float(base.powf(exponent)))
}

fn exp(interp: &mut Interp<'_>, _this: Value, args: &[Value]) -> JsResult<Value> {
    unary(interp, args, f64::exp)
}

fn log(interp: &mut Interp<'_>, _this: Value, args: &[Value]) -> JsResult<Value> {
    unary(interp, args, f64::ln)
}

fn log2(interp: &mut Interp<'_>, _this: Value, args: &[Value]) -> JsResult<Value> {
    unary(interp, args, f64::log2)
}

fn log10(interp: &mut Interp<'_>, _this: Value, args: &[Value]) -> JsResult<Value> {
    unary(interp, args, f64::log10)
}

fn sin(interp: &mut Interp<'_>, _this: Value, args: &[Value]) -> JsResult<Value> {
    unary(interp, args, f64::sin)
}

fn cos(interp: &mut Interp<'_>, _this: Value, args: &[Value]) -> JsResult<Value> {
    unary(interp, args, f64::cos)
}

fn tan(interp: &mut Interp<'_>, _this: Value, args: &[Value]) -> JsResult<Value> {
    unary(interp, args, f64::tan)
}

fn asin(interp: &mut Interp<'_>, _this: Value, args: &[Value]) -> JsResult<Value> {
    unary(interp, args, f64::asin)
}

fn acos(interp: &mut Interp<'_>, _this: Value, args: &[Value]) -> JsResult<Value> {
    unary(interp, args, f64::acos)
}

fn atan(interp: &mut Interp<'_>, _this: Value, args: &[Value]) -> JsResult<Value> {
    unary(interp, args, f64::atan)
}

fn atan2(interp: &mut Interp<'_>, _this: Value, args: &[Value]) -> JsResult<Value> {
    let y = number_of(interp, arg(args, 0))?;
    let x = number_of(interp, arg(args, 1))?;
    let _ = interp;
    Ok(Value::Float(y.atan2(x)))
}

/// `Math.min`, which propagates `NaN` and normalises `-0`.
fn min(interp: &mut Interp<'_>, _this: Value, args: &[Value]) -> JsResult<Value> {
    if args.is_empty() {
        return Ok(Value::Float(f64::INFINITY));
    }
    let mut best = f64::INFINITY;
    for value in args {
        let number = to_number(interp, value)?;
        if number.is_nan() {
            return Ok(Value::Float(f64::NAN));
        }
        // `Math.min(0, -0)` is `-0`, so an equal comparison keeps the negative one.
        if number < best || (number == best && number.is_sign_negative()) {
            best = number;
        }
    }
    let _ = interp;
    Ok(Value::Float(best))
}

fn max(interp: &mut Interp<'_>, _this: Value, args: &[Value]) -> JsResult<Value> {
    if args.is_empty() {
        return Ok(Value::Float(f64::NEG_INFINITY));
    }
    let mut best = f64::NEG_INFINITY;
    for value in args {
        let number = to_number(interp, value)?;
        if number.is_nan() {
            return Ok(Value::Float(f64::NAN));
        }
        if number > best || (number == best && !number.is_sign_negative()) {
            best = number;
        }
    }
    let _ = interp;
    Ok(Value::Float(best))
}

/// `Math.imul`, the 32-bit wrapping multiply.
fn imul(interp: &mut Interp<'_>, _this: Value, args: &[Value]) -> JsResult<Value> {
    // `ToInt32` runs `ToNumber` first, so a BigInt argument is refused here too.
    let a = to_int32(number_of(interp, arg(args, 0))?);
    let b = to_int32(number_of(interp, arg(args, 1))?);
    Ok(Value::Int(a.wrapping_mul(b)))
}

/// `Math.hypot`, which avoids intermediate overflow by scaling.
fn hypot(interp: &mut Interp<'_>, _this: Value, args: &[Value]) -> JsResult<Value> {
    let mut max = 0.0f64;
    let mut found_infinite = false;
    let mut found_nan = false;
    let numbers: Vec<f64> = {
        let mut numbers = Vec::with_capacity(args.len());
        for value in args {
            numbers.push(to_number(interp, value)?);
        }
        numbers
    };
    for number in &numbers {
        if number.is_infinite() {
            found_infinite = true;
        }
        if number.is_nan() {
            found_nan = true;
        }
        max = max.max(number.abs());
    }
    let _ = interp;
    if found_infinite {
        return Ok(Value::Float(f64::INFINITY));
    }
    if found_nan {
        return Ok(Value::Float(f64::NAN));
    }
    if max == 0.0 {
        return Ok(Value::Float(0.0));
    }
    let sum: f64 = numbers.iter().map(|value| (value / max).powi(2)).sum();
    Ok(Value::Float(max * sum.sqrt()))
}

/// `Math.fround`, rounding to the nearest `f32`.
fn fround(interp: &mut Interp<'_>, _this: Value, args: &[Value]) -> JsResult<Value> {
    let value = number_of(interp, arg(args, 0))?;
    let _ = interp;
    Ok(Value::Float(value as f32 as f64))
}

/// `Math.clz32`, the count of leading zero bits.
fn clz32(interp: &mut Interp<'_>, _this: Value, args: &[Value]) -> JsResult<Value> {
    let value = to_uint32(number_of(interp, arg(args, 0))?);
    Ok(Value::Int(value.leading_zeros() as i32))
}

/// `Math.sumPrecise(values)`: the correctly rounded sum of an iterable.
///
/// Not a compensated accumulator. The defining property of this function is that the
/// result is the exact sum rounded *once*, so `[1, 2^-53, 2^-1074]` is
/// `1.0000000000000002`: the tiny terms break the tie that `2^-53` alone would lose,
/// while Kahan/Neumaier summation still rounds that final `1 + 2^-53` back to `1`.
///
/// The accumulator is a three-term expansion — `hi` plus two correction terms that
/// together carry roughly 159 bits — which is enough for the sums a script builds out
/// of doubles. Each addition is a `two_sum`, pushing what the hardware rounded into
/// the next term down.
fn sum_precise(interp: &mut Interp<'_>, _this: Value, args: &[Value]) -> JsResult<Value> {
    let Some(iterable) = arg(args, 0).as_object().cloned() else {
        return Err(interp.type_error("Math.sumPrecise argument is not iterable"));
    };
    let length = iterable.array_length().unwrap_or(0);
    let mut accumulator = Accumulator::default();
    let mut seen = false;
    for index in 0..length {
        let Some(element) = iterable.array_get(index) else {
            continue;
        };
        if !matches!(element, Value::Int(_) | Value::Float(_)) {
            return Err(interp.type_error("Math.sumPrecise element is not a number"));
        }
        seen = true;
        accumulator.add(number_of(interp, element.clone())?);
    }
    // The empty sum is `-0`, which is what the reference returns and what the
    // addition identity for this operation is defined as.
    if !seen {
        return Ok(Value::Float(-0.0));
    }
    Ok(Value::Float(accumulator.finish()))
}

/// A three-term exact-enough floating point accumulator.
#[derive(Default)]
struct Accumulator {
    hi: f64,
    mid: f64,
    lo: f64,
}

impl Accumulator {
    fn add(&mut self, value: f64) {
        let (hi, error) = two_sum(self.hi, value);
        self.hi = hi;
        // The error is at most half an ulp of `hi`, so it belongs one term down; the
        // previous `mid` may drop further still.
        let (mid, error) = two_sum(self.mid, error);
        self.mid = mid;
        self.lo += error;
    }

    /// Round the expansion once, breaking ties with whatever is left below.
    fn finish(&self) -> f64 {
        let s = self.hi;
        // An overflowed sum is already the answer; adding the corrections to an
        // infinity would turn it into `NaN`.
        if s.is_infinite() {
            return s;
        }
        if self.mid == 0.0 && self.lo == 0.0 {
            return s;
        }
        // Half an ulp at `s`'s own magnitude, which is the tie threshold.
        let upward = next_after(s) - s;
        let downward = s - next_after_toward_zero(s);
        // The tail is `mid + lo`; compare it with the two half-ulps.
        let tail = self.mid + self.lo;
        if tail > 0.0 {
            if tail > upward / 2.0 || (tail == upward / 2.0 && self.lo != 0.0) {
                return next_after(s);
            }
        } else if tail < 0.0
            && (-tail > downward / 2.0 || (-tail == downward / 2.0 && self.lo != 0.0))
        {
            return next_after_toward_zero(s);
        }
        s
    }
}

/// `a + b` plus what the addition lost.
fn two_sum(a: f64, b: f64) -> (f64, f64) {
    let sum = a + b;
    let recovered = b - (sum - a);
    (sum, recovered)
}

/// The next representable double away from zero.
fn next_after(value: f64) -> f64 {
    if value == 0.0 {
        return f64::from_bits(1);
    }
    if value > 0.0 {
        f64::from_bits(value.to_bits() + 1)
    } else {
        f64::from_bits(value.to_bits() - 1)
    }
}

/// The next representable double toward zero.
fn next_after_toward_zero(value: f64) -> f64 {
    if value == 0.0 {
        return 0.0;
    }
    if value > 0.0 {
        f64::from_bits(value.to_bits() - 1)
    } else {
        f64::from_bits(value.to_bits() + 1)
    }
}

/// `Math.random`.
///
/// A xorshift generator seeded from the clock, matching neither the reference's
/// algorithm nor its sequence — no test can compare random numbers, so the only
/// requirement is that successive calls differ.
fn random(interp: &mut Interp<'_>, _this: Value, _args: &[Value]) -> JsResult<Value> {
    // An `AtomicU64` rather than a `static mut`: the interpreter is single-threaded, so
    // `Relaxed` costs nothing here, and it keeps this out of the `unsafe` the engine
    // confines to its low-level modules.
    static STATE: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
    use std::sync::atomic::Ordering;
    let mut state = STATE.load(Ordering::Relaxed);
    if state == 0 {
        state = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .map(|elapsed| elapsed.as_nanos() as u64)
            .unwrap_or(0x2545_F491_4F6C_DD1D)
            | 1;
    }
    state ^= state << 13;
    state ^= state >> 7;
    state ^= state << 17;
    STATE.store(state, Ordering::Relaxed);
    let state = state;
    let _ = interp;
    Ok(Value::Float((state >> 11) as f64 / (1u64 << 53) as f64))
}