just-engine 0.1.0

A ground-up ES6 JavaScript engine with tree-walking interpreter, bytecode VMs, and Cranelift JIT compiler
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
//! Math built-in object.
//!
//! Provides mathematical constants and functions.

use crate::runner::ds::error::JErrorType;
use crate::runner::ds::value::{JsValue, JsNumberType};
use crate::runner::plugin::registry::BuiltInRegistry;
use crate::runner::plugin::types::{BuiltInObject, EvalContext};

/// Register the Math object with the registry.
pub fn register(registry: &mut BuiltInRegistry) {
    let math = BuiltInObject::new("Math")
        .with_no_prototype()
        // Constants
        .add_property("E", JsValue::Number(JsNumberType::Float(std::f64::consts::E)))
        .add_property("LN10", JsValue::Number(JsNumberType::Float(std::f64::consts::LN_10)))
        .add_property("LN2", JsValue::Number(JsNumberType::Float(std::f64::consts::LN_2)))
        .add_property("LOG10E", JsValue::Number(JsNumberType::Float(std::f64::consts::LOG10_E)))
        .add_property("LOG2E", JsValue::Number(JsNumberType::Float(std::f64::consts::LOG2_E)))
        .add_property("PI", JsValue::Number(JsNumberType::Float(std::f64::consts::PI)))
        .add_property("SQRT1_2", JsValue::Number(JsNumberType::Float(std::f64::consts::FRAC_1_SQRT_2)))
        .add_property("SQRT2", JsValue::Number(JsNumberType::Float(std::f64::consts::SQRT_2)))
        // Methods
        .add_method("abs", math_abs)
        .add_method("floor", math_floor)
        .add_method("ceil", math_ceil)
        .add_method("round", math_round)
        .add_method("trunc", math_trunc)
        .add_method("sign", math_sign)
        .add_method("min", math_min)
        .add_method("max", math_max)
        .add_method("sqrt", math_sqrt)
        .add_method("cbrt", math_cbrt)
        .add_method("pow", math_pow)
        .add_method("exp", math_exp)
        .add_method("expm1", math_expm1)
        .add_method("log", math_log)
        .add_method("log10", math_log10)
        .add_method("log2", math_log2)
        .add_method("log1p", math_log1p)
        .add_method("sin", math_sin)
        .add_method("cos", math_cos)
        .add_method("tan", math_tan)
        .add_method("asin", math_asin)
        .add_method("acos", math_acos)
        .add_method("atan", math_atan)
        .add_method("atan2", math_atan2)
        .add_method("sinh", math_sinh)
        .add_method("cosh", math_cosh)
        .add_method("tanh", math_tanh)
        .add_method("asinh", math_asinh)
        .add_method("acosh", math_acosh)
        .add_method("atanh", math_atanh)
        .add_method("hypot", math_hypot)
        .add_method("random", math_random)
        .add_method("clz32", math_clz32)
        .add_method("imul", math_imul)
        .add_method("fround", math_fround);

    registry.register_object(math);
}

/// Convert JsValue to f64 for math operations.
fn to_f64(value: &JsValue) -> f64 {
    match value {
        JsValue::Number(JsNumberType::Integer(i)) => *i as f64,
        JsValue::Number(JsNumberType::Float(f)) => *f,
        JsValue::Number(JsNumberType::NaN) => f64::NAN,
        JsValue::Number(JsNumberType::PositiveInfinity) => f64::INFINITY,
        JsValue::Number(JsNumberType::NegativeInfinity) => f64::NEG_INFINITY,
        JsValue::Boolean(true) => 1.0,
        JsValue::Boolean(false) => 0.0,
        JsValue::Null => 0.0,
        JsValue::Undefined => f64::NAN,
        JsValue::String(s) => s.trim().parse().unwrap_or(f64::NAN),
        _ => f64::NAN,
    }
}

/// Convert f64 result to JsValue.
fn from_f64(f: f64) -> JsValue {
    if f.is_nan() {
        JsValue::Number(JsNumberType::NaN)
    } else if f == f64::INFINITY {
        JsValue::Number(JsNumberType::PositiveInfinity)
    } else if f == f64::NEG_INFINITY {
        JsValue::Number(JsNumberType::NegativeInfinity)
    } else if f.fract() == 0.0 && f.abs() < i64::MAX as f64 {
        JsValue::Number(JsNumberType::Integer(f as i64))
    } else {
        JsValue::Number(JsNumberType::Float(f))
    }
}

/// Math.abs
fn math_abs(
    _ctx: &mut EvalContext,
    _this: JsValue,
    args: Vec<JsValue>,
) -> Result<JsValue, JErrorType> {
    if args.is_empty() {
        return Ok(JsValue::Number(JsNumberType::NaN));
    }
    Ok(from_f64(to_f64(&args[0]).abs()))
}

/// Math.floor
fn math_floor(
    _ctx: &mut EvalContext,
    _this: JsValue,
    args: Vec<JsValue>,
) -> Result<JsValue, JErrorType> {
    if args.is_empty() {
        return Ok(JsValue::Number(JsNumberType::NaN));
    }
    Ok(from_f64(to_f64(&args[0]).floor()))
}

/// Math.ceil
fn math_ceil(
    _ctx: &mut EvalContext,
    _this: JsValue,
    args: Vec<JsValue>,
) -> Result<JsValue, JErrorType> {
    if args.is_empty() {
        return Ok(JsValue::Number(JsNumberType::NaN));
    }
    Ok(from_f64(to_f64(&args[0]).ceil()))
}

/// Math.round
fn math_round(
    _ctx: &mut EvalContext,
    _this: JsValue,
    args: Vec<JsValue>,
) -> Result<JsValue, JErrorType> {
    if args.is_empty() {
        return Ok(JsValue::Number(JsNumberType::NaN));
    }
    Ok(from_f64(to_f64(&args[0]).round()))
}

/// Math.trunc
fn math_trunc(
    _ctx: &mut EvalContext,
    _this: JsValue,
    args: Vec<JsValue>,
) -> Result<JsValue, JErrorType> {
    if args.is_empty() {
        return Ok(JsValue::Number(JsNumberType::NaN));
    }
    Ok(from_f64(to_f64(&args[0]).trunc()))
}

/// Math.sign
fn math_sign(
    _ctx: &mut EvalContext,
    _this: JsValue,
    args: Vec<JsValue>,
) -> Result<JsValue, JErrorType> {
    if args.is_empty() {
        return Ok(JsValue::Number(JsNumberType::NaN));
    }
    let x = to_f64(&args[0]);
    if x.is_nan() {
        Ok(JsValue::Number(JsNumberType::NaN))
    } else if x == 0.0 {
        Ok(JsValue::Number(JsNumberType::Integer(0)))
    } else if x > 0.0 {
        Ok(JsValue::Number(JsNumberType::Integer(1)))
    } else {
        Ok(JsValue::Number(JsNumberType::Integer(-1)))
    }
}

/// Math.min
fn math_min(
    _ctx: &mut EvalContext,
    _this: JsValue,
    args: Vec<JsValue>,
) -> Result<JsValue, JErrorType> {
    if args.is_empty() {
        return Ok(JsValue::Number(JsNumberType::PositiveInfinity));
    }

    let mut result = f64::INFINITY;
    for arg in &args {
        let x = to_f64(arg);
        if x.is_nan() {
            return Ok(JsValue::Number(JsNumberType::NaN));
        }
        if x < result {
            result = x;
        }
    }
    Ok(from_f64(result))
}

/// Math.max
fn math_max(
    _ctx: &mut EvalContext,
    _this: JsValue,
    args: Vec<JsValue>,
) -> Result<JsValue, JErrorType> {
    if args.is_empty() {
        return Ok(JsValue::Number(JsNumberType::NegativeInfinity));
    }

    let mut result = f64::NEG_INFINITY;
    for arg in &args {
        let x = to_f64(arg);
        if x.is_nan() {
            return Ok(JsValue::Number(JsNumberType::NaN));
        }
        if x > result {
            result = x;
        }
    }
    Ok(from_f64(result))
}

/// Math.sqrt
fn math_sqrt(
    _ctx: &mut EvalContext,
    _this: JsValue,
    args: Vec<JsValue>,
) -> Result<JsValue, JErrorType> {
    if args.is_empty() {
        return Ok(JsValue::Number(JsNumberType::NaN));
    }
    Ok(from_f64(to_f64(&args[0]).sqrt()))
}

/// Math.cbrt
fn math_cbrt(
    _ctx: &mut EvalContext,
    _this: JsValue,
    args: Vec<JsValue>,
) -> Result<JsValue, JErrorType> {
    if args.is_empty() {
        return Ok(JsValue::Number(JsNumberType::NaN));
    }
    Ok(from_f64(to_f64(&args[0]).cbrt()))
}

/// Math.pow
fn math_pow(
    _ctx: &mut EvalContext,
    _this: JsValue,
    args: Vec<JsValue>,
) -> Result<JsValue, JErrorType> {
    if args.len() < 2 {
        return Ok(JsValue::Number(JsNumberType::NaN));
    }
    let base = to_f64(&args[0]);
    let exp = to_f64(&args[1]);
    Ok(from_f64(base.powf(exp)))
}

/// Math.exp
fn math_exp(
    _ctx: &mut EvalContext,
    _this: JsValue,
    args: Vec<JsValue>,
) -> Result<JsValue, JErrorType> {
    if args.is_empty() {
        return Ok(JsValue::Number(JsNumberType::NaN));
    }
    Ok(from_f64(to_f64(&args[0]).exp()))
}

/// Math.expm1
fn math_expm1(
    _ctx: &mut EvalContext,
    _this: JsValue,
    args: Vec<JsValue>,
) -> Result<JsValue, JErrorType> {
    if args.is_empty() {
        return Ok(JsValue::Number(JsNumberType::NaN));
    }
    Ok(from_f64(to_f64(&args[0]).exp_m1()))
}

/// Math.log (natural logarithm)
fn math_log(
    _ctx: &mut EvalContext,
    _this: JsValue,
    args: Vec<JsValue>,
) -> Result<JsValue, JErrorType> {
    if args.is_empty() {
        return Ok(JsValue::Number(JsNumberType::NaN));
    }
    Ok(from_f64(to_f64(&args[0]).ln()))
}

/// Math.log10
fn math_log10(
    _ctx: &mut EvalContext,
    _this: JsValue,
    args: Vec<JsValue>,
) -> Result<JsValue, JErrorType> {
    if args.is_empty() {
        return Ok(JsValue::Number(JsNumberType::NaN));
    }
    Ok(from_f64(to_f64(&args[0]).log10()))
}

/// Math.log2
fn math_log2(
    _ctx: &mut EvalContext,
    _this: JsValue,
    args: Vec<JsValue>,
) -> Result<JsValue, JErrorType> {
    if args.is_empty() {
        return Ok(JsValue::Number(JsNumberType::NaN));
    }
    Ok(from_f64(to_f64(&args[0]).log2()))
}

/// Math.log1p
fn math_log1p(
    _ctx: &mut EvalContext,
    _this: JsValue,
    args: Vec<JsValue>,
) -> Result<JsValue, JErrorType> {
    if args.is_empty() {
        return Ok(JsValue::Number(JsNumberType::NaN));
    }
    Ok(from_f64(to_f64(&args[0]).ln_1p()))
}

/// Math.sin
fn math_sin(
    _ctx: &mut EvalContext,
    _this: JsValue,
    args: Vec<JsValue>,
) -> Result<JsValue, JErrorType> {
    if args.is_empty() {
        return Ok(JsValue::Number(JsNumberType::NaN));
    }
    Ok(from_f64(to_f64(&args[0]).sin()))
}

/// Math.cos
fn math_cos(
    _ctx: &mut EvalContext,
    _this: JsValue,
    args: Vec<JsValue>,
) -> Result<JsValue, JErrorType> {
    if args.is_empty() {
        return Ok(JsValue::Number(JsNumberType::NaN));
    }
    Ok(from_f64(to_f64(&args[0]).cos()))
}

/// Math.tan
fn math_tan(
    _ctx: &mut EvalContext,
    _this: JsValue,
    args: Vec<JsValue>,
) -> Result<JsValue, JErrorType> {
    if args.is_empty() {
        return Ok(JsValue::Number(JsNumberType::NaN));
    }
    Ok(from_f64(to_f64(&args[0]).tan()))
}

/// Math.asin
fn math_asin(
    _ctx: &mut EvalContext,
    _this: JsValue,
    args: Vec<JsValue>,
) -> Result<JsValue, JErrorType> {
    if args.is_empty() {
        return Ok(JsValue::Number(JsNumberType::NaN));
    }
    Ok(from_f64(to_f64(&args[0]).asin()))
}

/// Math.acos
fn math_acos(
    _ctx: &mut EvalContext,
    _this: JsValue,
    args: Vec<JsValue>,
) -> Result<JsValue, JErrorType> {
    if args.is_empty() {
        return Ok(JsValue::Number(JsNumberType::NaN));
    }
    Ok(from_f64(to_f64(&args[0]).acos()))
}

/// Math.atan
fn math_atan(
    _ctx: &mut EvalContext,
    _this: JsValue,
    args: Vec<JsValue>,
) -> Result<JsValue, JErrorType> {
    if args.is_empty() {
        return Ok(JsValue::Number(JsNumberType::NaN));
    }
    Ok(from_f64(to_f64(&args[0]).atan()))
}

/// Math.atan2
fn math_atan2(
    _ctx: &mut EvalContext,
    _this: JsValue,
    args: Vec<JsValue>,
) -> Result<JsValue, JErrorType> {
    if args.len() < 2 {
        return Ok(JsValue::Number(JsNumberType::NaN));
    }
    let y = to_f64(&args[0]);
    let x = to_f64(&args[1]);
    Ok(from_f64(y.atan2(x)))
}

/// Math.sinh
fn math_sinh(
    _ctx: &mut EvalContext,
    _this: JsValue,
    args: Vec<JsValue>,
) -> Result<JsValue, JErrorType> {
    if args.is_empty() {
        return Ok(JsValue::Number(JsNumberType::NaN));
    }
    Ok(from_f64(to_f64(&args[0]).sinh()))
}

/// Math.cosh
fn math_cosh(
    _ctx: &mut EvalContext,
    _this: JsValue,
    args: Vec<JsValue>,
) -> Result<JsValue, JErrorType> {
    if args.is_empty() {
        return Ok(JsValue::Number(JsNumberType::NaN));
    }
    Ok(from_f64(to_f64(&args[0]).cosh()))
}

/// Math.tanh
fn math_tanh(
    _ctx: &mut EvalContext,
    _this: JsValue,
    args: Vec<JsValue>,
) -> Result<JsValue, JErrorType> {
    if args.is_empty() {
        return Ok(JsValue::Number(JsNumberType::NaN));
    }
    Ok(from_f64(to_f64(&args[0]).tanh()))
}

/// Math.asinh
fn math_asinh(
    _ctx: &mut EvalContext,
    _this: JsValue,
    args: Vec<JsValue>,
) -> Result<JsValue, JErrorType> {
    if args.is_empty() {
        return Ok(JsValue::Number(JsNumberType::NaN));
    }
    Ok(from_f64(to_f64(&args[0]).asinh()))
}

/// Math.acosh
fn math_acosh(
    _ctx: &mut EvalContext,
    _this: JsValue,
    args: Vec<JsValue>,
) -> Result<JsValue, JErrorType> {
    if args.is_empty() {
        return Ok(JsValue::Number(JsNumberType::NaN));
    }
    Ok(from_f64(to_f64(&args[0]).acosh()))
}

/// Math.atanh
fn math_atanh(
    _ctx: &mut EvalContext,
    _this: JsValue,
    args: Vec<JsValue>,
) -> Result<JsValue, JErrorType> {
    if args.is_empty() {
        return Ok(JsValue::Number(JsNumberType::NaN));
    }
    Ok(from_f64(to_f64(&args[0]).atanh()))
}

/// Math.hypot
fn math_hypot(
    _ctx: &mut EvalContext,
    _this: JsValue,
    args: Vec<JsValue>,
) -> Result<JsValue, JErrorType> {
    if args.is_empty() {
        return Ok(JsValue::Number(JsNumberType::Integer(0)));
    }

    let mut sum_sq = 0.0f64;
    for arg in &args {
        let x = to_f64(arg);
        if x.is_infinite() {
            return Ok(JsValue::Number(JsNumberType::PositiveInfinity));
        }
        sum_sq += x * x;
    }

    Ok(from_f64(sum_sq.sqrt()))
}

/// Math.random
fn math_random(
    _ctx: &mut EvalContext,
    _this: JsValue,
    _args: Vec<JsValue>,
) -> Result<JsValue, JErrorType> {
    // Simple random number using system time as seed
    // For a real implementation, use a proper RNG
    use std::time::{SystemTime, UNIX_EPOCH};
    let seed = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map(|d| d.as_nanos() as u64)
        .unwrap_or(0);

    // Simple LCG (Linear Congruential Generator)
    let random = ((seed.wrapping_mul(6364136223846793005).wrapping_add(1442695040888963407)) as f64)
        / (u64::MAX as f64);

    Ok(JsValue::Number(JsNumberType::Float(random.abs())))
}

/// Math.clz32 - Count leading zeros in 32-bit integer.
fn math_clz32(
    _ctx: &mut EvalContext,
    _this: JsValue,
    args: Vec<JsValue>,
) -> Result<JsValue, JErrorType> {
    if args.is_empty() {
        return Ok(JsValue::Number(JsNumberType::Integer(32)));
    }
    let x = to_f64(&args[0]) as u32;
    Ok(JsValue::Number(JsNumberType::Integer(x.leading_zeros() as i64)))
}

/// Math.imul - 32-bit integer multiplication.
fn math_imul(
    _ctx: &mut EvalContext,
    _this: JsValue,
    args: Vec<JsValue>,
) -> Result<JsValue, JErrorType> {
    if args.len() < 2 {
        return Ok(JsValue::Number(JsNumberType::Integer(0)));
    }
    let a = to_f64(&args[0]) as i32;
    let b = to_f64(&args[1]) as i32;
    Ok(JsValue::Number(JsNumberType::Integer(a.wrapping_mul(b) as i64)))
}

/// Math.fround - Round to nearest 32-bit float.
fn math_fround(
    _ctx: &mut EvalContext,
    _this: JsValue,
    args: Vec<JsValue>,
) -> Result<JsValue, JErrorType> {
    if args.is_empty() {
        return Ok(JsValue::Number(JsNumberType::NaN));
    }
    let x = to_f64(&args[0]);
    Ok(from_f64((x as f32) as f64))
}