boa_engine 0.22.0

Boa is a Javascript lexer, parser and compiler written in Rust. Currently, it has support for some of the language.
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
use crate::{JsNativeErrorKind, TestAction, run_test_actions};
use indoc::indoc;

#[test]
fn construct() {
    run_test_actions([
        TestAction::assert_eq("(new Set()).size", 0),
        TestAction::assert_eq("(new Set(['one', 'two'])).size", 2),
    ]);
}

#[test]
fn clone() {
    run_test_actions([
        TestAction::run(indoc! {r#"
                let original = new Set(["one", "two"]);
                let clone = new Set(original);
            "#}),
        TestAction::assert_eq("clone.size", 2),
        TestAction::assert_eq(
            indoc! {r#"
                original.add("three");
                original.size
            "#},
            3,
        ),
        TestAction::assert_eq("clone.size", 2),
    ]);
}

#[test]
fn symbol_iterator() {
    run_test_actions([
        TestAction::run_harness(),
        TestAction::run(indoc! {r#"
                const set1 = new Set();
                set1.add('foo');
                set1.add('bar');
            "#}),
        TestAction::assert(indoc! {r#"
                arrayEquals(
                    Array.from(set1[Symbol.iterator]()),
                    ["foo", "bar"]
                )
            "#}),
    ]);
}

#[test]
fn entries() {
    run_test_actions([
        TestAction::run_harness(),
        TestAction::run(indoc! {r#"
                const set1 = new Set();
                set1.add('foo');
                set1.add('bar')
            "#}),
        TestAction::assert(indoc! {r#"
                arrayEquals(
                    Array.from(set1.entries()),
                    [["foo", "foo"], ["bar", "bar"]]
                )
            "#}),
    ]);
}

#[test]
fn merge() {
    run_test_actions([
        TestAction::run(indoc! {r#"
                let first = new Set(["one", "two"]);
                let second = new Set(["three", "four"]);
                let third = new Set(["four", "five"]);
                let merged1 = new Set([...first, ...second]);
                let merged2 = new Set([...second, ...third]);
            "#}),
        TestAction::assert_eq("merged1.size", 4),
        TestAction::assert_eq("merged2.size", 3),
    ]);
}

#[test]
fn clear() {
    run_test_actions([
        TestAction::run(indoc! {r#"
            let set = new Set(["one", "two"]);
            set.clear();
        "#}),
        TestAction::assert_eq("set.size", 0),
    ]);
}

#[test]
fn delete() {
    run_test_actions([
        TestAction::run("let set = new Set(['one', 'two'])"),
        TestAction::assert("set.delete('one')"),
        TestAction::assert_eq("set.size", 1),
        TestAction::assert("!set.delete('one')"),
    ]);
}

#[test]
fn has() {
    run_test_actions([
        TestAction::run("let set = new Set(['one', 'two']);"),
        TestAction::assert("set.has('one')"),
        TestAction::assert("set.has('two')"),
        TestAction::assert("!set.has('three')"),
        TestAction::assert("!set.has()"),
    ]);
}

#[test]
fn values_and_keys() {
    run_test_actions([
        TestAction::run_harness(),
        TestAction::run(indoc! {r#"
                const set1 = new Set();
                set1.add('foo');
                set1.add('bar');
            "#}),
        TestAction::assert(indoc! {r#"
            arrayEquals(
                Array.from(set1.values()),
                ["foo", "bar"]
            )
        "#}),
        TestAction::assert("set1.values == set1.keys"),
    ]);
}

#[test]
fn for_each() {
    run_test_actions([
        TestAction::run(indoc! {r#"
                let set = new Set([5, 10, 15]);
                let value1Sum = 0;
                let value2Sum = 0;
                let sizeSum = 0;
                function callingCallback(value1, value2, set) {
                    value1Sum += value1;
                    value2Sum += value2;
                    sizeSum += set.size;
                }
                set.forEach(callingCallback);
            "#}),
        TestAction::assert_eq("value1Sum", 30),
        TestAction::assert_eq("value2Sum", 30),
        TestAction::assert_eq("sizeSum", 9),
    ]);
}

#[test]
fn recursive_display() {
    run_test_actions([
        TestAction::run(indoc! {r#"
                let set = new Set();
                let array = new Array([set]);
                set.add(set);
            "#}),
        TestAction::assert_with_op("set", |v, _| v.display().to_string() == "Set { Set(1) }"),
        TestAction::assert_with_op("set.add(array)", |v, _| {
            v.display().to_string() == "Set { Set(2), Array(1) }"
        }),
    ]);
}

#[test]
fn not_a_function() {
    run_test_actions([TestAction::assert_native_error(
        "Set()",
        JsNativeErrorKind::Type,
        "calling a builtin Set constructor without new is forbidden",
    )]);
}

#[test]
fn difference() {
    run_test_actions([
        TestAction::run(indoc! {r#"
            let setA = new Set([1, 3, 5, 7, 9]);
            let setB = new Set([1, 4, 9]);
        "#}),
        TestAction::assert_with_op("setA.difference(setB)", |v, _| {
            v.display().to_string() == "Set { 3, 5, 7 }"
        }),
        TestAction::assert_with_op("setB.difference(setA)", |v, _| {
            v.display().to_string() == "Set { 4 }"
        }),
    ]);
}

#[test]
fn difference_equal_set() {
    run_test_actions([
        TestAction::run(indoc! {r#"
            let setA = new Set([1, 3, 5, 7, 9]);
            let setB = new Set([1, 4, 5, 7, 9]);
        "#}),
        TestAction::assert_with_op("setA.difference(setB)", |v, _| {
            v.display().to_string() == "Set { 3 }"
        }),
        TestAction::assert_with_op("setB.difference(setA)", |v, _| {
            v.display().to_string() == "Set { 4 }"
        }),
    ]);
}

#[test]
fn difference_empty() {
    run_test_actions([
        TestAction::run(indoc! {r#"
           let setA = new Set([1, 3, 5, 7, 9]);
           let setB = new Set([]);
        "#}),
        TestAction::assert_with_op("setA.difference(setB)", |v, _| {
            v.display().to_string() == "Set { 1, 3, 5, 7, 9 }"
        }),
        TestAction::assert_with_op("setB.difference(setA)", |v, _| {
            v.display().to_string() == "Set(0)"
        }),
    ]);
}

#[test]
fn intersection() {
    run_test_actions([
        TestAction::run(indoc! {r#"
            let setA = new Set([1,2,3]);
            let setB = new Set([1,4,3]);
            let setC = new Set([]);
            "#}),
        TestAction::assert_with_op("setA.intersection(setB)", |v, _| {
            v.display().to_string() == "Set { 1, 3 }"
        }),
        TestAction::assert_with_op("setB.intersection(setA)", |v, _| {
            v.display().to_string() == "Set { 1, 3 }"
        }),
        TestAction::assert_with_op("setA.intersection(setA)", |v, _| {
            v.display().to_string() == "Set { 1, 2, 3 }"
        }),
        TestAction::assert_with_op("setB.intersection(setB)", |v, _| {
            v.display().to_string() == "Set { 1, 4, 3 }"
        }),
        TestAction::assert_with_op("setB.intersection(setC)", |v, _| {
            v.display().to_string() == "Set(0)"
        }),
        TestAction::assert_with_op("setA.intersection(setC)", |v, _| {
            v.display().to_string() == "Set(0)"
        }),
    ]);
}

#[test]
fn is_dist_joint_from() {
    run_test_actions([
        TestAction::run(indoc! {r#"
            let setA = new Set([1, 2, 3]);
            let setB = new Set([1, 4, 6]);
            let setC = new Set([4, 8, 15, 16 ,23 ,42]);
            "#}),
        TestAction::assert_with_op("setA.isDisjointFrom(setB)", |v, _| {
            !v.as_boolean().unwrap_or(false)
        }),
        TestAction::assert_with_op("setA.isDisjointFrom(setC)", |v, _| {
            v.as_boolean().unwrap_or(true)
        }),
    ]);
}

#[test]
fn is_subset_of() {
    run_test_actions([
        TestAction::run(indoc! {r#"
            let setA = new Set([4, 8, 15]);
            let setB = new Set([1, 4, 6]);
            let setC = new Set([4, 8, 15, 16 ,23 ,42]);
            let setD = new Set([16]);
            let setE = new Set([]);
            "#}),
        TestAction::assert_with_op("setA.isSubsetOf(setB)", |v, _| {
            !v.as_boolean().unwrap_or(false)
        }),
        TestAction::assert_with_op("setA.isSubsetOf(setC)", |v, _| {
            v.as_boolean().unwrap_or(true)
        }),
        TestAction::assert_with_op("setB.isSubsetOf(setC)", |v, _| {
            !v.as_boolean().unwrap_or(false)
        }),
        TestAction::assert_with_op("setC.isSubsetOf(setC)", |v, _| {
            v.as_boolean().unwrap_or(true)
        }),
        TestAction::assert_with_op("setD.isSubsetOf(setC)", |v, _| {
            v.as_boolean().unwrap_or(true)
        }),
        TestAction::assert_with_op("setE.isSubsetOf(setC)", |v, _| {
            v.as_boolean().unwrap_or(true)
        }),
        TestAction::assert_with_op("setA.isSubsetOf(setE)", |v, _| {
            !v.as_boolean().unwrap_or(false)
        }),
    ]);
}

#[test]
fn is_superset_of() {
    run_test_actions([
        TestAction::run(indoc! {r#"
            let setA = new Set(["JavaScript", "HTML", "CSS"]);
            let setB = new Set(["HTML", "CSS"]);
            "#}),
        TestAction::assert_with_op("setA.isSupersetOf(setB)", |v, _| {
            v.as_boolean().unwrap_or(false)
        }),
        TestAction::assert_with_op("setB.isSupersetOf(setA)", |v, _| {
            !v.as_boolean().unwrap_or(false)
        }),
    ]);
}

#[test]
fn symmetric_difference_different_sets_strings() {
    run_test_actions([
        TestAction::run(indoc! {r#"
            let setA = new Set(["JavaScript", "HTML", "CSS"]);
            let setB = new Set(["Python", "Java", "JavaScript", "PHP"]);
            "#}),
        TestAction::assert_with_op("setA.symmetricDifference(setB)", |v, _| {
            v.display().to_string() == "Set { \"HTML\", \"CSS\", \"Python\", \"Java\", \"PHP\" }"
        }),
        TestAction::assert_with_op("setB.symmetricDifference(setA)", |v, _| {
            v.display().to_string() == "Set { \"Python\", \"Java\", \"PHP\", \"HTML\", \"CSS\" }"
        }),
    ]);
}

#[test]
fn symmetric_difference_different_sets_numbers() {
    run_test_actions([
        TestAction::run(indoc! {r#"
            let setC = new Set([2, 4, 6, 8]);
            let setD = new Set([1, 4, 9]);
            "#}),
        TestAction::assert_with_op("setC.symmetricDifference(setD)", |v, _| {
            v.display().to_string() == "Set { 2, 6, 8, 1, 9 }"
        }),
        TestAction::assert_with_op("setD.symmetricDifference(setC)", |v, _| {
            v.display().to_string() == "Set { 1, 9, 2, 6, 8 }"
        }),
    ]);
}

#[test]
fn symmetric_difference_same_set() {
    run_test_actions([
        TestAction::run(indoc! {r#"
            let setA = new Set(["JavaScript", "HTML", "CSS"]);
            let setACopy = setA;
        "#}),
        TestAction::assert_with_op("setA.symmetricDifference(setACopy)", |v, _| {
            v.display().to_string() == "Set(0)"
        }),
    ]);
}

// Alternative test that programmatically creates a new Set with the same content.
#[test]
fn symmetric_difference_with_identical_content() {
    run_test_actions([
        TestAction::run(indoc! {r#"
            let setA = new Set(["JavaScript", "HTML", "CSS"]);
            // Создаем функцию, которая вернет новый Set с тем же содержимым
            function getIdenticalSet() {
                return new Set(Array.from(setA));
            }
            "#}),
        // We use a function to get a new Set object with the same content.
        TestAction::assert_with_op("setA.symmetricDifference(getIdenticalSet())", |v, _| {
            v.display().to_string() == "Set(0)"
        }),
    ]);
}

#[test]
fn union() {
    run_test_actions([
        TestAction::run(indoc! {r#"
            let setA = new Set([2, 4, 6, 8]);
            let setB = new Set([1, 4, 9]);
            "#}),
        TestAction::assert_with_op("setA.union(setB)", |v, _| {
            v.display().to_string() == "Set { 2, 4, 6, 8, 1, 9 }"
        }),
        TestAction::assert_with_op("setB.union(setA)", |v, _| {
            v.display().to_string() == "Set { 1, 4, 9, 2, 6, 8 }"
        }),
    ]);
}

#[test]
fn union_same_set() {
    run_test_actions([
        TestAction::run(indoc! {r#"
            let setA = new Set([1, 4, 9]);
            "#}),
        TestAction::assert_with_op("setA.union(setA)", |v, _| {
            v.display().to_string() == "Set { 1, 4, 9 }"
        }),
    ]);
}

/// `Set.prototype.intersection` — happy-path: elements present in both sets.
#[test]
fn intersection_common_elements() {
    run_test_actions([
        TestAction::run(indoc! {r#"
            let setA = new Set([1, 2, 3, 4]);
            let setB = new Set([3, 4, 5, 6]);
        "#}),
        // Both directions must yield the same logical result (order follows `this`).
        TestAction::assert_with_op("setA.intersection(setB)", |v, _| {
            v.display().to_string() == "Set { 3, 4 }"
        }),
        TestAction::assert_with_op("setB.intersection(setA)", |v, _| {
            v.display().to_string() == "Set { 3, 4 }"
        }),
        // Intersecting a set with itself yields an identical set.
        TestAction::assert_with_op("setA.intersection(setA)", |v, _| {
            v.display().to_string() == "Set { 1, 2, 3, 4 }"
        }),
    ]);
}

/// `Set.prototype.intersection` — disjoint sets produce an empty result.
#[test]
fn intersection_no_common_elements() {
    run_test_actions([
        TestAction::run(indoc! {r#"
            let setA = new Set([1, 2, 3]);
            let setB = new Set([4, 5, 6]);
            let setEmpty = new Set([]);
        "#}),
        // Two sets with nothing in common.
        TestAction::assert_with_op("setA.intersection(setB)", |v, _| {
            v.display().to_string() == "Set(0)"
        }),
        // Intersection with an empty set is always empty.
        TestAction::assert_with_op("setA.intersection(setEmpty)", |v, _| {
            v.display().to_string() == "Set(0)"
        }),
        TestAction::assert_with_op("setEmpty.intersection(setA)", |v, _| {
            v.display().to_string() == "Set(0)"
        }),
    ]);
}

/// Set.prototype.intersection — this is not a Set → \
#[test]
fn intersection_this_not_a_set() {
    run_test_actions([TestAction::assert_native_error(
        "Set.prototype.intersection.call({}, new Set([1, 2, 3]))",
        JsNativeErrorKind::Type,
        "method `Set.prototype.intersection` called on incompatible receiver",
    )]);
}

/// Set.prototype.intersection — other is not a valid Set-like object → \
#[test]
fn intersection_other_not_set_like() {
    run_test_actions([
        // Primitive value — GetSetRecord rejects non-objects.
        TestAction::assert_native_error(
            "new Set([1, 2]).intersection(42)",
            JsNativeErrorKind::Type,
            "Set operation called with non-object argument",
        ),
        // Plain object whose `size` is `undefined` (NaN after ToNumber).
        TestAction::assert_native_error(
            "new Set([1, 2]).intersection({})",
            JsNativeErrorKind::Type,
            "size is undefined",
        ),
        // `has` is present but not callable.
        TestAction::assert_native_error(
            "new Set([1, 2]).intersection({ size: 0, has: 1, keys: () => [][Symbol.iterator]() })",
            JsNativeErrorKind::Type,
            "Set-like object must have a callable 'has' method",
        ),
        // `keys` is present but not callable.
        TestAction::assert_native_error(
            "new Set([1, 2]).intersection({ size: 0, has: () => false, keys: 1 })",
            JsNativeErrorKind::Type,
            "Set-like object must have a callable 'keys' method",
        ),
    ]);
}