javascript 0.3.0

A JavaScript engine implementation in Rust
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
use javascript::*;

// Initialize logger for this integration test binary so `RUST_LOG` is honored.
// Using `ctor` ensures initialization runs before tests start.
#[ctor::ctor(unsafe)]
fn __init_test_logger() {
    let _ = env_logger::Builder::from_env(env_logger::Env::default()).is_test(true).try_init();
}

#[cfg(test)]
mod number_tests {
    use super::*;

    #[test]
    fn test_number_max_value() {
        let script = "Number.MAX_VALUE";
        let result = evaluate_script(script, false, None::<&std::path::Path>).unwrap();
        assert_eq!(result, "1.7976931348623157e+308");
    }

    #[test]
    fn test_number_min_value() {
        let script = "Number.MIN_VALUE";
        let result = evaluate_script(script, false, None::<&std::path::Path>).unwrap();
        assert_eq!(result, "5e-324");
    }

    #[test]
    fn test_number_nan() {
        let script = "Number.NaN";
        let result = evaluate_script(script, false, None::<&std::path::Path>).unwrap();
        assert_eq!(result, "NaN");
    }

    #[test]
    fn test_number_positive_infinity() {
        let script = "Number.POSITIVE_INFINITY";
        let result = evaluate_script(script, false, None::<&std::path::Path>).unwrap();
        assert_eq!(result, "Infinity");
    }

    #[test]
    fn test_number_negative_infinity() {
        let script = "Number.NEGATIVE_INFINITY";
        let result = evaluate_script(script, false, None::<&std::path::Path>).unwrap();
        assert_eq!(result, "-Infinity");
    }

    #[test]
    fn test_number_epsilon() {
        let script = "Number.EPSILON";
        let result = evaluate_script(script, false, None::<&std::path::Path>).unwrap();
        assert_eq!(result, "2.220446049250313e-16");
    }

    #[test]
    fn test_number_max_safe_integer() {
        let script = "Number.MAX_SAFE_INTEGER";
        let result = evaluate_script(script, false, None::<&std::path::Path>).unwrap();
        assert_eq!(result, "9007199254740991");
    }

    #[test]
    fn test_number_min_safe_integer() {
        let script = "Number.MIN_SAFE_INTEGER";
        let result = evaluate_script(script, false, None::<&std::path::Path>).unwrap();
        assert_eq!(result, "-9007199254740991");
    }

    #[test]
    fn test_number_is_nan() {
        // Test with NaN
        let script = "Number.isNaN(NaN)";
        let result = evaluate_script(script, false, None::<&std::path::Path>).unwrap();
        assert_eq!(result, "true");

        // Test with number
        let script = "Number.isNaN(42)";
        let result = evaluate_script(script, false, None::<&std::path::Path>).unwrap();
        assert_eq!(result, "false");

        // Test with string that parses to NaN
        let script = "Number.isNaN('not a number')";
        let result = evaluate_script(script, false, None::<&std::path::Path>).unwrap();
        assert_eq!(result, "false");

        // Test with undefined
        let script = "Number.isNaN(undefined)";
        let result = evaluate_script(script, false, None::<&std::path::Path>).unwrap();
        assert_eq!(result, "false");
    }

    #[test]
    fn test_number_is_finite() {
        // Test with finite number
        let script = "Number.isFinite(42)";
        let result = evaluate_script(script, false, None::<&std::path::Path>).unwrap();
        assert_eq!(result, "true");

        // Test with Infinity
        let script = "Number.isFinite(Infinity)";
        let result = evaluate_script(script, false, None::<&std::path::Path>).unwrap();
        assert_eq!(result, "false");

        // Test with NaN
        let script = "Number.isFinite(NaN)";
        let result = evaluate_script(script, false, None::<&std::path::Path>).unwrap();
        assert_eq!(result, "false");

        // Test with string
        let script = "Number.isFinite('42')";
        let result = evaluate_script(script, false, None::<&std::path::Path>).unwrap();
        assert_eq!(result, "false");
    }

    #[test]
    fn test_number_is_integer() {
        // Test with integer
        let script = "Number.isInteger(42)";
        let result = evaluate_script(script, false, None::<&std::path::Path>).unwrap();
        assert_eq!(result, "true");

        // Test with float
        let script = "Number.isInteger(42.5)";
        let result = evaluate_script(script, false, None::<&std::path::Path>).unwrap();
        assert_eq!(result, "false");

        // Test with Infinity
        let script = "Number.isInteger(Infinity)";
        let result = evaluate_script(script, false, None::<&std::path::Path>).unwrap();
        assert_eq!(result, "false");

        // Test with NaN
        let script = "Number.isInteger(NaN)";
        let result = evaluate_script(script, false, None::<&std::path::Path>).unwrap();
        assert_eq!(result, "false");

        // Test with string
        let script = "Number.isInteger('42')";
        let result = evaluate_script(script, false, None::<&std::path::Path>).unwrap();
        assert_eq!(result, "false");
    }

    #[test]
    fn test_number_is_safe_integer() {
        // Test with safe integer
        let script = "Number.isSafeInteger(42)";
        let result = evaluate_script(script, false, None::<&std::path::Path>).unwrap();
        assert_eq!(result, "true");

        // Test with MAX_SAFE_INTEGER
        let script = "Number.isSafeInteger(9007199254740991)";
        let result = evaluate_script(script, false, None::<&std::path::Path>).unwrap();
        assert_eq!(result, "true");

        // Test with MIN_SAFE_INTEGER
        let script = "Number.isSafeInteger(-9007199254740991)";
        let result = evaluate_script(script, false, None::<&std::path::Path>).unwrap();
        assert_eq!(result, "true");

        // Test with unsafe integer (too large)
        let script = "Number.isSafeInteger(9007199254740992)";
        let result = evaluate_script(script, false, None::<&std::path::Path>).unwrap();
        assert_eq!(result, "false");

        // Test with float
        let script = "Number.isSafeInteger(42.5)";
        let result = evaluate_script(script, false, None::<&std::path::Path>).unwrap();
        assert_eq!(result, "false");

        // Test with Infinity
        let script = "Number.isSafeInteger(Infinity)";
        let result = evaluate_script(script, false, None::<&std::path::Path>).unwrap();
        assert_eq!(result, "false");
    }

    #[test]
    fn test_number_parse_float() {
        // Test with valid float string
        let script = "Number.parseFloat('3.16')";
        let result = evaluate_script(script, false, None::<&std::path::Path>).unwrap();
        assert_eq!(result, "3.16");

        // Test with integer string
        let script = "Number.parseFloat('42')";
        let result = evaluate_script(script, false, None::<&std::path::Path>).unwrap();
        assert_eq!(result, "42");

        // Test with invalid string
        let script = "Number.parseFloat('not a number')";
        let result = evaluate_script(script, false, None::<&std::path::Path>).unwrap();
        assert_eq!(result, "NaN");

        // Test with number
        let script = "Number.parseFloat(42.5)";
        let result = evaluate_script(script, false, None::<&std::path::Path>).unwrap();
        assert_eq!(result, "42.5");

        // Test with whitespace
        let script = "Number.parseFloat('  3.16  ')";
        let result = evaluate_script(script, false, None::<&std::path::Path>).unwrap();
        assert_eq!(result, "3.16");
    }

    #[test]
    fn test_shift_edge_cases_and_bigint_mixing() {
        // Left shift with large shift amount (masked by 0x1f)
        let res = evaluate_script("let a = 1; a <<= 33; a", false, None::<&std::path::Path>).unwrap();
        assert_eq!(res, "2");

        // Left shift with negative shift amount -> ToUint32(-1) & 0x1f == 31
        let res = evaluate_script("let a = 1; a <<= -1; a", false, None::<&std::path::Path>).unwrap();
        assert_eq!(res, "-2147483648");

        // Unsigned right shift on negative number
        let res = evaluate_script("let a = -1; a >>>= 1; a", false, None::<&std::path::Path>).unwrap();
        assert_eq!(res, "2147483647");

        // Mixing BigInt with Number in shift should throw TypeError
        let res = evaluate_script("let a = 1n; let b = 2; a <<= b", false, None::<&std::path::Path>);
        match res {
            Err(err) => match err.kind() {
                javascript::JSErrorKind::TypeError { message, .. } => assert!(message.contains("Cannot mix BigInt")),
                _ => panic!("Expected TypeError for mixing BigInt and Number in <<=, got {:?}", err),
            },
            other => panic!("Expected TypeError for mixing BigInt and Number in <<=, got {:?}", other),
        }

        // Unsigned right shift on BigInt should throw TypeError with specific message
        let res = evaluate_script("let a = 1n; a >>>= 1n", false, None::<&std::path::Path>);
        match res {
            Err(err) => match err.kind() {
                javascript::JSErrorKind::TypeError { message, .. } => assert!(message.contains("Unsigned right shift")),
                _ => panic!("Expected TypeError for BigInt >>>=, got {:?}", err),
            },
            other => panic!("Expected TypeError for BigInt >>>=, got {:?}", other),
        }
    }

    #[test]
    fn test_bigint_shift_and_bitwise_mixing_errors() {
        // Huge BigInt shift amount should produce an evaluation error (invalid bigint shift)
        let res = evaluate_script(
            "let a = 1n; a <<= 100000000000000000000000000000000000000n",
            false,
            None::<&std::path::Path>,
        );
        match res {
            Err(err) => match err.kind() {
                javascript::JSErrorKind::EvaluationError { message, .. } => {
                    assert!(
                        message.contains("invalid bigint shift") || message.contains("invalid bigint"),
                        "message={}",
                        message
                    )
                }
                _ => panic!("Expected EvaluationError for huge BigInt shift, got {:?}", err),
            },
            other => panic!("Expected EvaluationError for huge BigInt shift, got {:?}", other),
        }

        // Negative BigInt shift: 1n << -1n is equivalent to 1n >> 1n = 0n (per spec)
        let res = evaluate_script("let a = 1n; a <<= -1n; a", false, None::<&std::path::Path>);
        match res {
            Ok(v) => assert_eq!(v, "0", "1n << -1n should equal 0n"),
            Err(err) => panic!("Negative BigInt shift should succeed, got error: {:?}", err),
        }

        // Mixing BigInt and Number in bitwise XOR should throw TypeError
        let res = evaluate_script("let a = 1n; let b = 2; a ^= b", false, None::<&std::path::Path>);
        match res {
            Err(err) => match err.kind() {
                javascript::JSErrorKind::TypeError { message, .. } => assert!(message.contains("Cannot mix BigInt")),
                _ => panic!("Expected TypeError for BigInt ^ Number mixing, got {:?}", err),
            },
            other => panic!("Expected TypeError for BigInt ^ Number mixing, got {:?}", other),
        }
    }

    #[test]
    fn test_number_parse_int() {
        // Test with valid integer string
        let script = "Number.parseInt('42')";
        let result = evaluate_script(script, false, None::<&std::path::Path>).unwrap();
        assert_eq!(result, "42");

        // Test with float string (should truncate)
        let script = "Number.parseInt('42.5')";
        let result = evaluate_script(script, false, None::<&std::path::Path>).unwrap();
        assert_eq!(result, "42");

        // Test with invalid string
        let script = "Number.parseInt('not a number')";
        let result = evaluate_script(script, false, None::<&std::path::Path>).unwrap();
        assert_eq!(result, "NaN");

        // Test with radix
        let script = "Number.parseInt('101', 2)";
        let result = evaluate_script(script, false, None::<&std::path::Path>).unwrap();
        assert_eq!(result, "5"); // 101 in binary is 5

        // Test with hex
        let script = "Number.parseInt('FF', 16)";
        let result = evaluate_script(script, false, None::<&std::path::Path>).unwrap();
        assert_eq!(result, "255");

        // Test with number
        let script = "Number.parseInt(42.7)";
        let result = evaluate_script(script, false, None::<&std::path::Path>).unwrap();
        assert_eq!(result, "42");
    }

    #[test]
    fn test_number_constructor_no_args() {
        let script = "Number()";
        let result = evaluate_script(script, false, None::<&std::path::Path>).unwrap();
        assert_eq!(result, "0");
    }

    #[test]
    fn test_number_constructor_with_number() {
        let script = "Number(42.5)";
        let result = evaluate_script(script, false, None::<&std::path::Path>).unwrap();
        assert_eq!(result, "42.5");
    }

    #[test]
    fn test_number_constructor_with_string() {
        let script = "Number('42.5')";
        let result = evaluate_script(script, false, None::<&std::path::Path>).unwrap();
        assert_eq!(result, "42.5");
    }

    #[test]
    fn test_number_constructor_with_boolean() {
        let script = "Number(true)";
        let result = evaluate_script(script, false, None::<&std::path::Path>).unwrap();
        assert_eq!(result, "1");

        let script = "Number(false)";
        let result = evaluate_script(script, false, None::<&std::path::Path>).unwrap();
        assert_eq!(result, "0");
    }

    #[test]
    fn test_number_constructor_with_invalid_string() {
        let script = "Number('not a number')";
        let result = evaluate_script(script, false, None::<&std::path::Path>).unwrap();
        assert_eq!(result, "NaN");
    }

    #[test]
    fn test_number_constructor_with_undefined() {
        let script = "Number(undefined)";
        let result = evaluate_script(script, false, None::<&std::path::Path>).unwrap();
        assert_eq!(result, "NaN");
    }

    #[test]
    fn test_number_object_properties_exist() {
        // Test that all expected properties exist on the Number object
        let properties = vec![
            "MAX_VALUE",
            "MIN_VALUE",
            "NaN",
            "POSITIVE_INFINITY",
            "NEGATIVE_INFINITY",
            "EPSILON",
            "MAX_SAFE_INTEGER",
            "MIN_SAFE_INTEGER",
            "isNaN",
            "isFinite",
            "isInteger",
            "isSafeInteger",
            "parseFloat",
            "parseInt",
        ];

        for prop in properties {
            let script = format!("typeof Number.{}", prop);
            let result = evaluate_script(&script, false, None::<&std::path::Path>).unwrap();
            assert_ne!(result, "undefined", "Number.{} should exist", prop);
        }
    }

    #[test]
    fn test_bitwise_xor_numbers() {
        let script = "5 ^ 3";
        let result = evaluate_script(script, false, None::<&std::path::Path>).unwrap();
        assert_eq!(result, "6");
    }

    #[test]
    fn test_bitwise_xor_negative_numbers() {
        let script = "-5 ^ 3";
        let result = evaluate_script(script, false, None::<&std::path::Path>).unwrap();
        assert_eq!(result, "-8");
    }

    #[test]
    fn test_bitwise_xor_assignment() {
        let script = "let a = 5; a ^= 3; a";
        let result = evaluate_script(script, false, None::<&std::path::Path>).unwrap();
        assert_eq!(result, "6");
    }

    #[test]
    fn test_bitwise_compound_assignments() {
        // Test bitwise AND assignment (&=)
        let script1 = "let a = 5; a &= 3; a";
        let result1 = evaluate_script(script1, false, None::<&std::path::Path>).unwrap();
        assert_eq!(result1, "1");

        // Test bitwise OR assignment (|=)
        let script2 = "let b = 5; b |= 3; b";
        let result2 = evaluate_script(script2, false, None::<&std::path::Path>).unwrap();
        assert_eq!(result2, "7");

        // Test bitwise XOR assignment (^=)
        let script3 = "let c = 5; c ^= 3; c";
        let result3 = evaluate_script(script3, false, None::<&std::path::Path>).unwrap();
        assert_eq!(result3, "6");

        // Test left shift assignment (<<=)
        let script4 = "let d = 5; d <<= 1; d";
        let result4 = evaluate_script(script4, false, None::<&std::path::Path>).unwrap();
        assert_eq!(result4, "10");

        // Test right shift assignment (>>=)
        let script5 = "let e = 5; e >>= 1; e";
        let result5 = evaluate_script(script5, false, None::<&std::path::Path>).unwrap();
        assert_eq!(result5, "2");

        // Test unsigned right shift assignment (>>>=)
        let script6 = "let f = -5; f >>>= 1; f";
        let result6 = evaluate_script(script6, false, None::<&std::path::Path>).unwrap();
        assert_eq!(result6, "2147483645");
    }
}