quickjs-oxide 0.4.0

A safe Rust JavaScript engine targeting semantic parity with QuickJS
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
use super::*;

#[test]
fn class_field_early_error_diagnostics_match_quickjs() {
    for (source, message, column) in [
        (
            "class C { field = arguments; }",
            "'arguments' identifier is not allowed in class field initializer",
            19,
        ),
        (
            "class C { static { arguments; } }",
            "'arguments' identifier is not allowed in class field initializer",
            20,
        ),
        (
            "class C { field = () => super(); }",
            "super() is only valid in a derived class constructor",
            30,
        ),
        ("class C { constructor; }", "invalid field name", 22),
        ("class C { static prototype; }", "invalid method name", 27),
        (
            "class C { static async prototype; }",
            "invalid property name",
            33,
        ),
        (
            "class C { static *prototype; }",
            "invalid property name",
            28,
        ),
        (
            "class C { static get prototype; }",
            "invalid property name",
            31,
        ),
        (
            "class C { static set prototype; }",
            "invalid property name",
            31,
        ),
        ("class C { get field; }", "invalid property name", 20),
        (
            "class C { static prototype() {} }",
            "invalid method name",
            27,
        ),
        ("class C { *constructor() {} }", "invalid method name", 23),
        ("class C { #constructor; }", "invalid method name", 23),
        (
            "class C { static #constructor; }",
            "invalid method name",
            30,
        ),
        (
            "class C { get #constructor() {} }",
            "invalid method name",
            27,
        ),
        (
            "class C { static get #constructor() {} }",
            "invalid method name",
            34,
        ),
        (
            r"class C { static #constr\u0075ctor; }",
            "invalid method name",
            35,
        ),
        (
            "class C { #constructor # }",
            "invalid first character of private name",
            24,
        ),
        (
            r"class C { async \u0023m() {} }",
            "invalid property name",
            17,
        ),
        ("class C { async @() {} }", "invalid property name", 17),
    ] {
        let error = compile_unlinked_script(source).unwrap_err();
        assert_eq!(error.kind(), ErrorKind::Syntax, "{source}");
        assert_eq!(error.message(), message, "{source}");
        let span = error
            .span()
            .expect("class-field syntax error lost its span");
        assert_eq!(
            (span.start.line, span.start.column),
            (1, column),
            "{source}"
        );
    }
}

#[test]
fn private_field_early_error_diagnostics_match_quickjs() {
    let undefined = compile_unlinked_script("class C { m(){ return this.#missing; } }")
        .expect_err("an undeclared private name must be rejected");
    assert_eq!(undefined.kind(), ErrorKind::Syntax);
    assert_eq!(undefined.message(), "undefined private field '#missing'");
    assert_eq!(undefined.span(), None);

    for (source, message, column) in [
        (
            "class C { #x; m(o){ delete o.#x; } }",
            "cannot delete a private class field",
            32,
        ),
        (
            "class C { #x; m(){ #x; } }",
            "unexpected token in expression: '#x'",
            20,
        ),
        (
            r"class C { #x; m(){ #\u0078; } }",
            r"unexpected token in expression: '#\u0078'",
            20,
        ),
        (
            "class C { #x; #x; }",
            "private class field is already defined",
            17,
        ),
        (
            "class C { static #x; static #x; }",
            "private class field is already defined",
            31,
        ),
        (
            "class C { #x; m(){ #x in {} = 0; } }",
            "invalid assignment left-hand side",
            31,
        ),
        (
            "class C { #x; m(){ #x in {} += 0; } }",
            "invalid assignment left-hand side",
            32,
        ),
    ] {
        let error = compile_unlinked_script(source).unwrap_err();
        assert_eq!(error.kind(), ErrorKind::Syntax, "{source}");
        assert_eq!(error.message(), message, "{source}");
        let span = error
            .span()
            .expect("private-field syntax error lost its span");
        assert_eq!(
            (span.start.line, span.start.column),
            (1, column),
            "{source}"
        );
    }
}

#[test]
fn invalid_assignment_diagnostics_advance_to_rhs_like_quickjs() {
    for (source, column) in [
        ("1 = 0", 5),
        ("f() += 0", 8),
        ("1 = @", 5),
        ("1 &&= 0", 7),
        ("f() ||= 0", 9),
        ("a?.b = 0", 8),
        ("a?.b += 0", 9),
        ("a?.b ??= 0", 10),
        ("class C { #x; m(){ #x in {} &&= 0; } }", 33),
    ] {
        let error = compile_unlinked_script(source).unwrap_err();
        assert_eq!(error.kind(), ErrorKind::Syntax, "{source}");
        assert_eq!(
            error.message(),
            "invalid assignment left-hand side",
            "{source}"
        );
        let span = error
            .span()
            .expect("invalid-assignment syntax error lost its span");
        assert_eq!(
            (span.start.line, span.start.column),
            (1, column),
            "{source}"
        );
    }
}

#[test]
fn invalid_for_in_of_assignment_target_diagnostics_match_quickjs() {
    for source in ["for(f() in {}) {}", "for(f() of []) {}"] {
        let error = compile_unlinked_script(source).unwrap_err();
        assert_eq!(error.kind(), ErrorKind::Syntax, "{source}");
        assert_eq!(
            error.message(),
            "invalid for in/of left hand-side",
            "{source}"
        );
        let span = error
            .span()
            .expect("invalid for-in/of target error lost its span");
        assert_eq!((span.start.line, span.start.column), (1, 9), "{source}");
    }

    let ordinary_assignment = compile_unlinked_script("f() = 1").unwrap_err();
    assert_eq!(ordinary_assignment.kind(), ErrorKind::Syntax);
    assert_eq!(
        ordinary_assignment.message(),
        "invalid assignment left-hand side"
    );
    let span = ordinary_assignment
        .span()
        .expect("invalid assignment error lost its span");
    assert_eq!((span.start.line, span.start.column), (1, 7));
}

#[test]
fn class_field_initializers_reset_async_lexing_to_the_normal_hidden_child() {
    let source = r#"
        var aw\u0061it = 40;
        async function build() {
            class Fields {
                instance = await + 1;
                static staticField = await + 2;
                #private = await + 3;
                static #staticPrivate = await + 4;
                arrow = () => await + 5;
                static escaped = aw\u0061it + 6;
                [await Promise.resolve("computed")] = 47;
                read() { return this.#private; }
                static read() { return this.#staticPrivate; }
            }
            return Fields;
        }
    "#;
    let tree = Parser::parse(source, JsString::from_static("<class-field-await-test>"))
        .expect("class field await IdentifierReferences should compile");

    let initializers = tree
        .functions
        .iter()
        .filter(|function| {
            matches!(
                function.class_initializer_kind,
                Some(ClassInitializerKind::InstanceFields | ClassInitializerKind::StaticElements)
            )
        })
        .collect::<Vec<_>>();
    assert_eq!(initializers.len(), 2);
    assert!(initializers.iter().all(|function| {
        function.kind == FunctionKind::Method
            && function.execution_kind == BytecodeFunctionKind::Normal
            && function.strict
            && function
                .ops
                .iter()
                .all(|operation| !matches!(operation.op, super::IrOp::Bytecode(Instruction::Await)))
    }));

    let build = tree
        .functions
        .iter()
        .find(|function| {
            function
                .function_name
                .is_some_and(|id| tree.names.name(id) == "build")
                && function.execution_kind == BytecodeFunctionKind::Async
        })
        .expect("outer async function");
    assert_eq!(
        build
            .ops
            .iter()
            .filter(|operation| matches!(operation.op, super::IrOp::Bytecode(Instruction::Await)))
            .count(),
        1,
        "the computed key should retain the outer async context"
    );

    let field_arrow = tree
        .functions
        .iter()
        .find(|function| function.kind == FunctionKind::Arrow)
        .expect("field initializer arrow");
    assert_eq!(field_arrow.execution_kind, BytecodeFunctionKind::Normal);
    assert!(
        field_arrow
            .ops
            .iter()
            .all(|operation| !matches!(operation.op, super::IrOp::Bytecode(Instruction::Await)))
    );
}

#[test]
fn class_field_await_context_diagnostics_keep_neighboring_boundaries() {
    for (source, expected) in [
        (
            "async function build(){ return class { [await] = 1; }; }",
            "unexpected token in expression: ']'",
        ),
        (
            "async function build(){ return class { field = await 1; }; }",
            "expecting ';'",
        ),
        (
            "async function build(){ return class { static { await; } }; }",
            "unexpected 'await' keyword",
        ),
        (
            "class Fields { static { await; } }",
            "unexpected 'await' keyword",
        ),
    ] {
        let error = compile_unlinked_script(source).unwrap_err();
        assert_eq!(error.kind(), ErrorKind::Syntax, "{source:?}");
        assert_eq!(error.message(), expected, "{source:?}");
    }
}

#[test]
fn class_static_block_diagnostic_precedence_matches_quickjs() {
    for (source, message, column) in [
        (
            "class C { static { (await => 0); } }",
            "unexpected 'await' keyword",
            21,
        ),
        (
            "class C { static { (class await {}); } }",
            "expecting '{'",
            27,
        ),
        ("class C { static { ({ await }); } }", "expecting ':'", 29),
        (
            "class C { static { await: 0; } }",
            "unexpected 'await' keyword",
            20,
        ),
        (
            "class C { static { class await {} } }",
            "class statement requires a name",
            26,
        ),
        (
            "class C { static { return; } }",
            "return in a static initializer block",
            20,
        ),
        (
            "class C { static { const await = 0; } }",
            "variable name expected",
            26,
        ),
        (
            "class C { static { function await() {} } }",
            "function name expected",
            29,
        ),
        (
            "class C { static { let await; } }",
            "variable name expected",
            24,
        ),
        (
            "class C { static { try {} catch (await) {} } }",
            "identifier expected",
            34,
        ),
        (
            "class C { static { var [await] = []; } }",
            "invalid destructuring target",
            25,
        ),
        (
            "class C { static { var {await} = {}; } }",
            "invalid destructuring target",
            32,
        ),
        (
            "class C { static { var await; } }",
            "variable name expected",
            24,
        ),
        (
            "class C { static { await; } }",
            "unexpected 'await' keyword",
            20,
        ),
        (
            r"class C { static { ({ \u0061wait }); } }",
            "expecting ':'",
            34,
        ),
        (
            r"class C { static { try {} catch (\u0061wait) {} } }",
            "identifier expected",
            34,
        ),
    ] {
        let error = compile_unlinked_script(source).unwrap_err();
        assert_eq!(error.kind(), ErrorKind::Syntax, "{source}");
        assert_eq!(error.message(), message, "{source}");
        let span = error
            .span()
            .expect("class-static-block syntax error lost its span");
        assert_eq!(
            (span.start.line, span.start.column),
            (1, column),
            "{source}"
        );
    }
}