strata-js 0.1.1

Pure-Rust ECMAScript runtime, DOM bridge, Promise microtasks, and dynamic value engine (Mica / Strata)
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
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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
//! Pure-Rust Arbitrary Precision Integer Engine for ECMAScript BigInt (GOAL-48).

use std::cmp::Ordering;
use std::fmt;

/// Arbitrary-precision integer adhering to ECMAScript BigInt semantics.
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct JsBigInt {
    /// True for non-negative (>= 0), false for negative (< 0).
    pub is_positive: bool,
    /// Little-endian base 2^64 limbs. Normalized such that the most significant
    /// limb is non-zero, unless the value is zero, where limbs is empty.
    pub limbs: Vec<u64>,
}

impl JsBigInt {
    /// Creates a BigInt representing zero.
    pub const fn zero() -> Self {
        Self {
            is_positive: true,
            limbs: Vec::new(),
        }
    }

    /// Creates a BigInt representing one.
    pub fn one() -> Self {
        Self {
            is_positive: true,
            limbs: vec![1],
        }
    }

    /// Checks if this BigInt is zero.
    pub fn is_zero(&self) -> bool {
        self.limbs.is_empty() || self.limbs.iter().all(|&l| l == 0)
    }

    /// Creates a BigInt from a signed 64-bit integer.
    pub fn from_i64(val: i64) -> Self {
        if val == 0 {
            return Self::zero();
        }
        let is_positive = val >= 0;
        let abs = if val == i64::MIN {
            (i64::MAX as u64) + 1
        } else {
            val.unsigned_abs()
        };
        Self {
            is_positive,
            limbs: vec![abs],
        }
    }

    /// Creates a BigInt from an unsigned 64-bit integer.
    pub fn from_u64(val: u64) -> Self {
        if val == 0 {
            Self::zero()
        } else {
            Self {
                is_positive: true,
                limbs: vec![val],
            }
        }
    }

    /// Creates a BigInt from a signed 128-bit integer.
    pub fn from_i128(val: i128) -> Self {
        if val == 0 {
            return Self::zero();
        }
        let is_positive = val >= 0;
        let abs = if val == i128::MIN {
            (i128::MAX as u128) + 1
        } else {
            val.unsigned_abs()
        };
        Self::from_u128(abs, is_positive)
    }

    /// Creates a BigInt from an unsigned 128-bit integer and sign.
    pub fn from_u128(abs: u128, is_positive: bool) -> Self {
        if abs == 0 {
            return Self::zero();
        }
        let low = abs as u64;
        let high = (abs >> 64) as u64;
        let mut limbs = vec![low];
        if high > 0 {
            limbs.push(high);
        }
        Self { is_positive, limbs }
    }

    /// Parses a string into a BigInt, supporting optional sign, base prefixes
    /// (0x, 0b, 0o), and optional trailing 'n'.
    pub fn parse(s: &str) -> Result<Self, String> {
        let trimmed = s.trim();
        if trimmed.is_empty() {
            return Err("SyntaxError: Cannot convert empty string to a BigInt".to_string());
        }

        let without_suffix = trimmed.strip_suffix('n').unwrap_or(trimmed);
        if without_suffix.is_empty() {
            return Err("SyntaxError: Invalid BigInt literal".to_string());
        }

        let (is_positive, body) = if let Some(rest) = without_suffix.strip_prefix('-') {
            (false, rest)
        } else if let Some(rest) = without_suffix.strip_prefix('+') {
            (true, rest)
        } else {
            (true, without_suffix)
        };

        if body.is_empty() {
            return Err("SyntaxError: Invalid BigInt literal".to_string());
        }

        let (radix, digits) =
            if let Some(hex) = body.strip_prefix("0x").or_else(|| body.strip_prefix("0X")) {
                (16u32, hex)
            } else if let Some(bin) = body.strip_prefix("0b").or_else(|| body.strip_prefix("0B")) {
                (2u32, bin)
            } else if let Some(oct) = body.strip_prefix("0o").or_else(|| body.strip_prefix("0O")) {
                (8u32, oct)
            } else {
                (10u32, body)
            };

        if digits.is_empty() {
            return Err("SyntaxError: Invalid BigInt literal".to_string());
        }

        let mut result = Self::zero();
        let radix_big = Self::from_u64(radix as u64);

        for ch in digits.chars() {
            if ch == '_' {
                continue; // Support numeric separators (e.g. 1_000n)
            }
            let digit = ch.to_digit(radix).ok_or_else(|| {
                format!("SyntaxError: Invalid character '{ch}' in BigInt literal")
            })?;
            result = result.mul_abs(&radix_big);
            result = result.add_abs(&Self::from_u64(digit as u64));
        }

        result.is_positive = if result.is_zero() { true } else { is_positive };
        result.normalize();
        Ok(result)
    }

    /// Normalizes internal representation by stripping trailing zero limbs.
    fn normalize(&mut self) {
        while let Some(&0) = self.limbs.last() {
            self.limbs.pop();
        }
        if self.limbs.is_empty() {
            self.is_positive = true;
        }
    }

    /// Compares absolute magnitudes.
    pub fn cmp_abs(&self, other: &Self) -> Ordering {
        if self.limbs.len() != other.limbs.len() {
            return self.limbs.len().cmp(&other.limbs.len());
        }
        for (a, b) in self.limbs.iter().rev().zip(other.limbs.iter().rev()) {
            if a != b {
                return a.cmp(b);
            }
        }
        Ordering::Equal
    }

    /// Adds absolute values.
    fn add_abs(&self, other: &Self) -> Self {
        let max_len = self.limbs.len().max(other.limbs.len());
        let mut limbs = Vec::with_capacity(max_len + 1);
        let mut carry = 0u64;

        for i in 0..max_len {
            let a = *self.limbs.get(i).unwrap_or(&0);
            let b = *other.limbs.get(i).unwrap_or(&0);
            let sum = (a as u128) + (b as u128) + (carry as u128);
            limbs.push(sum as u64);
            carry = (sum >> 64) as u64;
        }
        if carry > 0 {
            limbs.push(carry);
        }

        let mut res = Self {
            is_positive: true,
            limbs,
        };
        res.normalize();
        res
    }

    /// Subtracts absolute values (assumes self >= other).
    fn sub_abs(&self, other: &Self) -> Self {
        let mut limbs = Vec::with_capacity(self.limbs.len());
        let mut borrow = 0i128;

        for i in 0..self.limbs.len() {
            let a = *self.limbs.get(i).unwrap_or(&0) as i128;
            let b = *other.limbs.get(i).unwrap_or(&0) as i128;
            let diff = a - b - borrow;
            if diff < 0 {
                limbs.push((diff + (1i128 << 64)) as u64);
                borrow = 1;
            } else {
                limbs.push(diff as u64);
                borrow = 0;
            }
        }

        let mut res = Self {
            is_positive: true,
            limbs,
        };
        res.normalize();
        res
    }

    /// Multiplies absolute values.
    fn mul_abs(&self, other: &Self) -> Self {
        if self.is_zero() || other.is_zero() {
            return Self::zero();
        }
        let mut limbs = vec![0u64; self.limbs.len() + other.limbs.len()];

        for (i, &a) in self.limbs.iter().enumerate() {
            let mut carry = 0u128;
            for (j, &b) in other.limbs.iter().enumerate() {
                let cur = limbs[i + j] as u128;
                let prod = (a as u128) * (b as u128) + cur + carry;
                limbs[i + j] = prod as u64;
                carry = prod >> 64;
            }
            if carry > 0 {
                limbs[i + other.limbs.len()] += carry as u64;
            }
        }

        let mut res = Self {
            is_positive: true,
            limbs,
        };
        res.normalize();
        res
    }

    /// Adds two BigInts respecting signs.
    pub fn add(&self, other: &Self) -> Self {
        if self.is_positive == other.is_positive {
            let mut res = self.add_abs(other);
            res.is_positive = self.is_positive;
            res
        } else {
            match self.cmp_abs(other) {
                Ordering::Equal => Self::zero(),
                Ordering::Greater => {
                    let mut res = self.sub_abs(other);
                    res.is_positive = self.is_positive;
                    res
                }
                Ordering::Less => {
                    let mut res = other.sub_abs(self);
                    res.is_positive = other.is_positive;
                    res
                }
            }
        }
    }

    /// Negates a BigInt.
    pub fn neg(&self) -> Self {
        if self.is_zero() {
            self.clone()
        } else {
            let mut res = self.clone();
            res.is_positive = !res.is_positive;
            res
        }
    }

    /// Subtracts two BigInts respecting signs.
    pub fn sub(&self, other: &Self) -> Self {
        self.add(&other.neg())
    }

    /// Multiplies two BigInts respecting signs.
    pub fn mul(&self, other: &Self) -> Self {
        if self.is_zero() || other.is_zero() {
            return Self::zero();
        }
        let mut res = self.mul_abs(other);
        res.is_positive = self.is_positive == other.is_positive;
        res
    }

    /// Divides and computes remainder of absolute values using binary restoring division.
    fn div_rem_abs(&self, other: &Self) -> Result<(Self, Self), String> {
        if other.is_zero() {
            return Err("RangeError: Division by zero".to_string());
        }
        if self.cmp_abs(other) == Ordering::Less {
            return Ok((Self::zero(), self.clone()));
        }

        let mut quotient = Self::zero();
        let mut remainder = Self::zero();

        // Total bits in self
        let total_bits = self.bit_length();

        for bit_idx in (0..total_bits).rev() {
            remainder = remainder.shl(1);
            if self.test_bit(bit_idx) {
                remainder = remainder.add_abs(&Self::one());
            }

            if remainder.cmp_abs(other) != Ordering::Less {
                remainder = remainder.sub_abs(other);
                quotient.set_bit(bit_idx);
            }
        }

        quotient.normalize();
        remainder.normalize();
        Ok((quotient, remainder))
    }

    /// Integer division.
    pub fn div(&self, other: &Self) -> Result<Self, String> {
        let (mut q, _) = self.div_rem_abs(other)?;
        if !q.is_zero() {
            q.is_positive = self.is_positive == other.is_positive;
        }
        Ok(q)
    }

    /// Remainder operation.
    pub fn rem(&self, other: &Self) -> Result<Self, String> {
        let (_, mut r) = self.div_rem_abs(other)?;
        if !r.is_zero() {
            r.is_positive = self.is_positive;
        }
        Ok(r)
    }

    /// Exponentiation (pow) adhering to ECMAScript standard.
    pub fn pow(&self, exp: &Self) -> Result<Self, String> {
        if !exp.is_positive && !exp.is_zero() {
            return Err("RangeError: Exponent must be non-negative".to_string());
        }
        if exp.is_zero() {
            return Ok(Self::one());
        }
        if self.is_zero() {
            return Ok(Self::zero());
        }

        let mut base = self.clone();
        let mut exponent = exp.clone();
        let mut result = Self::one();

        while !exponent.is_zero() {
            if exponent.test_bit(0) {
                result = result.mul(&base);
            }
            base = base.mul(&base);
            exponent = exponent.shr(1);
        }

        Ok(result)
    }

    /// Bitwise NOT (~).
    pub fn bit_not(&self) -> Self {
        // In ECMAScript: ~x === -(x + 1)
        let one = Self::one();
        let plus_one = self.add(&one);
        let mut res = plus_one;
        if !res.is_zero() {
            res.is_positive = !res.is_positive;
        }
        res
    }

    /// Bitwise shift left.
    pub fn shl(&self, shift: usize) -> Self {
        if self.is_zero() || shift == 0 {
            return self.clone();
        }
        let limb_shift = shift / 64;
        let bit_shift = (shift % 64) as u32;

        let mut limbs = vec![0u64; limb_shift];
        let mut carry = 0u64;

        for &limb in &self.limbs {
            let shifted = if bit_shift == 0 {
                limb
            } else {
                (limb << bit_shift) | carry
            };
            limbs.push(shifted);
            carry = if bit_shift == 0 {
                0
            } else {
                limb >> (64 - bit_shift)
            };
        }
        if carry > 0 {
            limbs.push(carry);
        }

        let mut res = Self {
            is_positive: self.is_positive,
            limbs,
        };
        res.normalize();
        res
    }

    /// Bitwise shift right.
    pub fn shr(&self, shift: usize) -> Self {
        if self.is_zero() || shift == 0 {
            return self.clone();
        }
        let limb_shift = shift / 64;
        let bit_shift = (shift % 64) as u32;

        if limb_shift >= self.limbs.len() {
            return if self.is_positive {
                Self::zero()
            } else {
                Self::from_i64(-1)
            };
        }

        let mut limbs = Vec::new();
        let source = &self.limbs[limb_shift..];

        for i in 0..source.len() {
            let cur = source[i];
            let next = source.get(i + 1).copied().unwrap_or(0);
            let shifted = if bit_shift == 0 {
                cur
            } else {
                (cur >> bit_shift) | (next << (64 - bit_shift))
            };
            limbs.push(shifted);
        }

        let mut res = Self {
            is_positive: self.is_positive,
            limbs,
        };
        res.normalize();
        res
    }

    /// Bit length (index of highest set bit + 1).
    pub fn bit_length(&self) -> usize {
        if let Some(&last) = self.limbs.last() {
            (self.limbs.len() - 1) * 64 + (64 - last.leading_zeros() as usize)
        } else {
            0
        }
    }

    /// Converts non-negative single limb BigInt to u64 if fits.
    pub fn to_u64(&self) -> Option<u64> {
        if !self.is_positive {
            return None;
        }
        if self.limbs.is_empty() {
            Some(0)
        } else if self.limbs.len() == 1 {
            Some(self.limbs[0])
        } else {
            None
        }
    }

    /// Bitwise AND (&).
    pub fn bit_and(&self, other: &Self) -> Self {
        let max_len = self.limbs.len().min(other.limbs.len());
        let mut limbs = Vec::with_capacity(max_len);
        for i in 0..max_len {
            limbs.push(self.limbs[i] & other.limbs[i]);
        }
        let is_positive = self.is_positive && other.is_positive;
        let mut res = Self { is_positive, limbs };
        res.normalize();
        res
    }

    /// Bitwise OR (|).
    pub fn bit_or(&self, other: &Self) -> Self {
        let max_len = self.limbs.len().max(other.limbs.len());
        let mut limbs = Vec::with_capacity(max_len);
        for i in 0..max_len {
            let a = self.limbs.get(i).copied().unwrap_or(0);
            let b = other.limbs.get(i).copied().unwrap_or(0);
            limbs.push(a | b);
        }
        let is_positive = self.is_positive || other.is_positive;
        let mut res = Self { is_positive, limbs };
        res.normalize();
        res
    }

    /// Bitwise XOR (^).
    pub fn bit_xor(&self, other: &Self) -> Self {
        let max_len = self.limbs.len().max(other.limbs.len());
        let mut limbs = Vec::with_capacity(max_len);
        for i in 0..max_len {
            let a = self.limbs.get(i).copied().unwrap_or(0);
            let b = other.limbs.get(i).copied().unwrap_or(0);
            limbs.push(a ^ b);
        }
        let is_positive = self.is_positive == other.is_positive;
        let mut res = Self { is_positive, limbs };
        res.normalize();
        res
    }

    /// Tests if a bit at `index` is 1.
    pub fn test_bit(&self, index: usize) -> bool {
        let limb_idx = index / 64;
        let bit_idx = index % 64;
        if let Some(&limb) = self.limbs.get(limb_idx) {
            (limb & (1u64 << bit_idx)) != 0
        } else {
            false
        }
    }

    /// Sets a bit at `index` to 1.
    pub fn set_bit(&mut self, index: usize) {
        let limb_idx = index / 64;
        let bit_idx = index % 64;
        if limb_idx >= self.limbs.len() {
            self.limbs.resize(limb_idx + 1, 0);
        }
        self.limbs[limb_idx] |= 1u64 << bit_idx;
    }

    /// Converts to standard decimal string representation.
    pub fn to_decimal_string(&self) -> String {
        if self.is_zero() {
            return "0".to_string();
        }

        let mut temp = self.clone();
        temp.is_positive = true;
        let ten = Self::from_u64(10);
        let mut digits = Vec::new();

        while !temp.is_zero() {
            let (q, r) = temp.div_rem_abs(&ten).unwrap();
            let d = r.limbs.first().copied().unwrap_or(0);
            digits.push((b'0' + d as u8) as char);
            temp = q;
        }

        if !self.is_positive {
            digits.push('-');
        }

        digits.iter().rev().collect()
    }
}

impl PartialOrd for JsBigInt {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for JsBigInt {
    fn cmp(&self, other: &Self) -> Ordering {
        if self.is_positive != other.is_positive {
            return if self.is_positive {
                Ordering::Greater
            } else {
                Ordering::Less
            };
        }
        let mag = self.cmp_abs(other);
        if self.is_positive {
            mag
        } else {
            mag.reverse()
        }
    }
}

impl fmt::Debug for JsBigInt {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}n", self.to_decimal_string())
    }
}

impl fmt::Display for JsBigInt {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.to_decimal_string())
    }
}

impl std::str::FromStr for JsBigInt {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Self::parse(s)
    }
}