quickjs-oxide 0.4.1

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
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
use super::*;

#[test]
fn strict_and_escaped_reserved_binding_names_are_rejected_late() {
    for source in [
        "(function(implements) { 'use strict'; return implements; })(1)",
        "'use strict'; (function(let) { return let; })(1)",
        "(function() { 'use strict'; var eval = 1; return eval; })()",
        "(function() { 'use strict'; return impl\\u0065ments; })()",
        "(function(\\u0069f) { return \\u0069f; })(1)",
    ] {
        assert!(
            compile_unlinked_script(source).is_err(),
            "accepted {source:?}"
        );
    }
    assert_eq!(
        evaluate_in_context("(function(implements) { return implements; })(1)"),
        Value::Int(1)
    );
    assert_eq!(
        evaluate_in_context("(function(impl\\u0065ments) { return impl\\u0065ments; })(1)"),
        Value::Int(1)
    );
}

#[test]
fn always_reserved_words_use_quickjs_syntax_diagnostics() {
    for (source, message) in [
        ("enum;", "unsupported keyword: enum"),
        ("'use strict'; enum;", "unsupported keyword: enum"),
        ("export;", "unsupported keyword: export"),
        ("'use strict'; export;", "unsupported keyword: export"),
        ("extends;", "unsupported keyword: extends"),
        ("'use strict'; extends;", "unsupported keyword: extends"),
        ("import;", "expecting '('"),
        ("'use strict'; import;", "expecting '('"),
        ("(enum);", "unexpected token in expression: 'enum'"),
        ("void export;", "unexpected token in expression: 'export'"),
        ("1 + extends;", "unexpected token in expression: 'extends'"),
    ] {
        let error = compile_unlinked_script(source).unwrap_err();
        assert_eq!(error.kind(), ErrorKind::Syntax, "{source:?}: {error}");
        assert_eq!(error.message(), message, "{source:?}");
    }

    for (source, message) in [
        ("var enum;", "variable name expected"),
        ("'use strict'; let export;", "variable name expected"),
        ("const extends = 1;", "variable name expected"),
        ("function f(import){}", "missing formal parameter"),
        ("try{}catch(enum){}", "identifier expected"),
        ("let { enum: export } = {};", "invalid destructuring target"),
    ] {
        let error = compile_unlinked_script(source).unwrap_err();
        assert_eq!(error.kind(), ErrorKind::Syntax, "{source:?}: {error}");
        assert_eq!(error.message(), message, "{source:?}");
    }

    for (source, word) in [
        (r"var en\u0075m;", "enum"),
        (r"exp\u006frt;", "export"),
        (r"var ext\u0065nds;", "extends"),
        (r"imp\u006frt;", "import"),
    ] {
        let error = compile_unlinked_script(source).unwrap_err();
        assert_eq!(error.kind(), ErrorKind::Syntax, "{source:?}: {error}");
        assert_eq!(
            error.message(),
            format!("'{word}' is a reserved identifier"),
            "{source:?}"
        );
    }
}

#[test]
fn statement_and_binary_keywords_in_expression_position_are_syntax_errors() {
    for keyword in [
        "return",
        "instanceof",
        "do",
        "while",
        "break",
        "continue",
        "switch",
        "throw",
        "try",
        "with",
    ] {
        let source = format!("var x = {keyword};");
        let error = compile_unlinked_script(&source).unwrap_err();
        assert_eq!(error.kind(), ErrorKind::Syntax, "{source:?}: {error}");
        assert_eq!(
            error.message(),
            format!("unexpected token in expression: '{keyword}'"),
            "{source:?}",
        );
        let span = error
            .span()
            .expect("keyword SyntaxError lost its source span");
        assert_eq!(span.start.byte_offset, 8, "{source:?}");
        assert_eq!(span.end.byte_offset, 8 + keyword.len(), "{source:?}");
    }
}

#[test]
fn statement_keywords_in_primary_expression_use_quickjs_syntax_diagnostics() {
    for (source, keyword, column) in [
        ("({[if (0) 0;]})", "if", 4),
        ("[for (x of [1]) x]", "for", 2),
    ] {
        let error = compile_unlinked_script(source).unwrap_err();
        assert_eq!(error.kind(), ErrorKind::Syntax, "{source:?}: {error}");
        assert_eq!(
            error.message(),
            format!("unexpected token in expression: '{keyword}'"),
            "{source:?}"
        );
        let span = error
            .span()
            .unwrap_or_else(|| panic!("missing syntax span for {source:?}"));
        assert_eq!(
            (span.start.line, span.start.column),
            (1, column),
            "{source:?}"
        );
    }
}

#[test]
fn reserved_property_names_and_import_calls_remain_distinct() {
    for source in [
        "import('module')",
        "import /* trivia */ ('module')",
        "import\n('module')",
        "import('module',)",
        "import('module', {})",
        "import('module', {},)",
        "import(('mod' + 'ule')).then",
        "import('module')?.foo",
        "import('module')?.[0]",
        "import('module')?.()",
        "import('module')`tag`",
        "new (import('module'))",
        "new C(import('module'))",
    ] {
        compile_unlinked_script(source)
            .unwrap_or_else(|error| panic!("valid ImportCall {source:?} failed: {error}"));
    }

    for (source, message) in [
        ("import binding from 'module';", "expecting '('"),
        ("import { binding } from 'module';", "expecting '('"),
        ("import.meta", "import.meta only valid in module code"),
        (
            "import /* trivia */ . meta",
            "import.meta only valid in module code",
        ),
        ("import.foo", "meta expected"),
        (r"import.\u006deta", "meta expected"),
        ("import()", "unexpected token in expression: ')'"),
        ("import(,)", "unexpected token in expression: ','"),
        ("import(...source)", "unexpected token in expression: '...'"),
        ("import(source extra)", "expecting ')'"),
        ("import(source, {}, extra)", "expecting ')'"),
        ("new import(source)", "invalid use of 'import()'"),
        ("new import()", "invalid use of 'import()'"),
        ("new/*gap*/import('module')", "invalid use of 'import()'"),
        ("new\nimport('module')", "invalid use of 'import()'"),
        (
            "new import /* gap */ ('module')",
            "invalid use of 'import()'",
        ),
        (
            "(() => new/*gap*/import('module'))",
            "invalid use of 'import()'",
        ),
        ("import(source) = 1", "invalid assignment left-hand side"),
        ("import(source)++", "invalid increment/decrement operand"),
        ("++import(source)", "invalid increment/decrement operand"),
        ("import?.(source)", "expecting '('"),
        ("import`source`", "expecting '('"),
        ("import('module')?.`tag`", "expecting field name"),
        (
            "import('module')?.foo`tag`",
            "template literal cannot appear in an optional chain",
        ),
        (
            "new (import('module'))?.foo",
            "new keyword cannot be used with an optional chain",
        ),
        ("import('module'); enum;", "unsupported keyword: enum"),
        (
            "import('module'); class C { method(){ return this.#missing; } }",
            "undefined private field '#missing'",
        ),
        (
            "import('module'); class C { method(object){ return #missing in object; } }",
            "undefined private field '#missing'",
        ),
    ] {
        let error = compile_unlinked_script(source).unwrap_err();
        assert_eq!(error.kind(), ErrorKind::Syntax, "{source:?}: {error}");
        assert_eq!(error.message(), message, "{source:?}");
    }

    let detached_error =
        compile_script("import('module'); class C { method(){ return this.#missing; } }")
            .unwrap_err();
    assert_eq!(detached_error.kind(), ErrorKind::Syntax);
    assert_eq!(
        detached_error.message(),
        "undefined private field '#missing'"
    );

    for source in [
        "({enum:20,export:22}).enum+({enum:20,export:22}).export",
        "({extends(){return 20},import(){return 22}}).extends()+({extends(){return 20},import(){return 22}}).import()",
        r"({en\u0075m:42}).en\u0075m",
        r"({exp\u006frt(){return 42}}).exp\u006frt()",
        "class C extends Object {}; 42",
        "class C { extends(){return 42} import(){return 42} }; new C().extends()",
        "({enum:42})?.enum",
    ] {
        assert_eq!(evaluate_in_context(source), Value::Int(42), "{source:?}");
    }
}

#[test]
fn parser_driven_lexing_preserves_quickjs_error_priority_and_locations() {
    let cases = [
        (
            r"(function(){ var \u0069f\u{}=14; })()",
            "'if' is a reserved identifier",
            1,
            18,
        ),
        (
            r"(function(){ var if\u{}=14; })()",
            "'if' is a reserved identifier",
            1,
            18,
        ),
        (
            r"(function(){ var if\x61=1; })()",
            "variable name expected",
            1,
            18,
        ),
        (
            r"(function(){ var \u{}=1; })()",
            "variable name expected",
            1,
            18,
        ),
        (r"(function(){ var a\u{}=1; })()", "expecting ';'", 1, 19),
        (
            "(function(){ var 'unterminated })()",
            "unexpected end of string",
            1,
            18,
        ),
        (
            "(function(a 'unterminated){})",
            "unexpected end of string",
            1,
            13,
        ),
        (
            "(function(){ return (1 'unterminated); })()",
            "unexpected end of string",
            1,
            24,
        ),
        (
            "(function(eval){ \"use strict\"; \"x\"; \"unterminated })()",
            "unexpected end of string",
            1,
            37,
        ),
        (
            "(function(){ \"use strict\"; (function(eval){ \"x\"; \"unterminated })() })()",
            "unexpected end of string",
            1,
            50,
        ),
        // A string terminated by a raw line terminator is reported at the
        // opening quote, matching pinned QuickJS's token-pointer location
        // rather than at the newline character (B12/C6).
        (
            "(function(){ \"unterminated\n})()",
            "unexpected end of string",
            1,
            14,
        ),
        (
            "(function(){ 'unterminated\n})()",
            "unexpected end of string",
            1,
            14,
        ),
        (
            "(function(){ \"unterminated\r\n})()",
            "unexpected end of string",
            1,
            14,
        ),
        (
            "var t = 1;\n(function(){ \"unterminated\n})()",
            "unexpected end of string",
            2,
            14,
        ),
        // A trailing backslash before the line terminator takes the same
        // label and is also anchored at the opening quote.
        (
            "(function(){ \"unterminated\\\n})()",
            "unexpected end of string",
            1,
            14,
        ),
        // Malformed fixed-width and braced escapes share the pinned wording
        // and point at the backslash (B12/C5).
        (
            "(function(){ \"\\u00\"; })()",
            "malformed escape sequence in string literal",
            1,
            15,
        ),
        (
            "(function(){ \"\\xZZ\"; })()",
            "malformed escape sequence in string literal",
            1,
            15,
        ),
        (
            "(function(){ \"\\u{}\"; })()",
            "malformed escape sequence in string literal",
            1,
            15,
        ),
        (
            "(function(){ \"\\u{110000}\"; })()",
            "malformed escape sequence in string literal",
            1,
            15,
        ),
        (
            "(function(){ \"\\u\"; })()",
            "malformed escape sequence in string literal",
            1,
            15,
        ),
        (
            "(function(){ \"\\x\"; })()",
            "malformed escape sequence in string literal",
            1,
            15,
        ),
    ];

    for (source, message, line, column) in cases {
        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()
            .unwrap_or_else(|| panic!("missing span for {source}"));
        assert_eq!(
            (span.start.line, span.start.column),
            (line, column),
            "{source}"
        );
    }

    let reached_lex_error =
        compile_unlinked_script("(function(){ throw\n'unterminated })()").unwrap_err();
    assert_eq!(reached_lex_error.message(), "unexpected end of string");
    let reached_span = reached_lex_error.span().unwrap();
    assert_eq!((reached_span.start.line, reached_span.start.column), (2, 1));

    let raw_token_error = compile_unlinked_script("(function(){ throw\n\\u{}; })()").unwrap_err();
    assert_eq!(
        raw_token_error.message(),
        "line terminator not allowed after throw"
    );
    let raw_span = raw_token_error.span().unwrap();
    assert_eq!((raw_span.start.line, raw_span.start.column), (2, 1));
}

#[test]
fn primary_expression_slashes_are_rescanned_as_complete_regexp_tokens() {
    // Pinned QuickJS makes this decision in its primary-expression parser:
    // `/` and `/=` are ordinary punctuators until the grammar requires an
    // operand, at which point it rewinds and scans the complete literal.
    for source in [
        "/start/g;",
        "/=prefix/m;",
        "Function.value = /rhs/gi;",
        "(function(){ return /ret/m; })",
        "Function(/argument/s);",
        "true ? /consequent/u : 0;",
        "false ? 0 : /alternate/y;",
        "false || /logical/d;",
        "1 / /denominator/u;",
    ] {
        compile_unlinked_script(source)
            .unwrap_or_else(|error| panic!("RegExp literal {source:?} failed: {error}"));
    }
    let script = compile_unlinked_script("/start/g;").unwrap();
    assert!(matches!(script.code()[0], Instruction::RegExp(0)));

    let invalid_pattern = compile_unlinked_script("/(/").unwrap_err();
    assert_eq!(invalid_pattern.kind(), ErrorKind::Syntax);
    assert_eq!(invalid_pattern.message(), "expecting ')'");
    let span = invalid_pattern.span().expect("literal SyntaxError span");
    assert_eq!((span.start.line, span.start.column), (1, 1));
    assert_eq!((span.start.byte_offset, span.end.byte_offset), (0, 3));

    let invalid_flags = compile_unlinked_script("/a/gg").unwrap_err();
    assert_eq!(invalid_flags.kind(), ErrorKind::Syntax);
    assert_eq!(invalid_flags.message(), "invalid regular expression flags");

    for (source, expected) in [
        ("/a", "unexpected end of regexp"),
        ("/a\n/", "unexpected line terminator in regexp"),
        ("/a\\\n/", "unexpected line terminator in regexp"),
    ] {
        let error = compile_unlinked_script(source).unwrap_err();
        assert_eq!(error.kind(), ErrorKind::Syntax, "{source:?}");
        assert_eq!(error.message(), expected, "{source:?}");
    }

    compile_unlinked_script("/(?=a)/").expect("forward lookahead literal should compile");
    compile_unlinked_script("/(?<=a)/").expect("backward lookaround literal should compile");
    compile_unlinked_script("1 / /denominator/v;")
        .expect("Unicode Sets RegExp literal should compile after slash rescanning");

    // The same slash tokens remain operators when the expression parser
    // has already produced their left operand.
    assert_eq!(evaluate("84 / 2"), Value::Int(42));
    assert_eq!(
        evaluate_in_context("(function(){ var value=84; value /= 2; return value; })()"),
        Value::Int(42)
    );
}

#[test]
fn declined_arrow_probes_keep_the_arrow_parameter_diagnostic() {
    for (source, message, column) in [
        ("(function(){}) => 1", "missing formal parameter", 2),
        (
            "(function(){ return return }) => 1",
            "missing formal parameter",
            2,
        ),
        (
            "(function(){ 'use strict'; 010 }) => 1",
            "missing formal parameter",
            2,
        ),
        ("((a)) => 1", "missing formal parameter", 2),
        ("(1 +) => 1", "missing formal parameter", 2),
        ("async (1) => 2", "missing formal parameter", 8),
        ("f((function(){}) => 1)", "missing formal parameter", 4),
        ("(1)\n=> 2", "unexpected token in expression: '=>'", 1),
        ("(1 + ) + 2", "unexpected token in expression: ')'", 6),
        ("(}) => 1", "unexpected token in expression: '}'", 2),
        ("(function(){} => 1)", "expecting ')'", 15),
    ] {
        let error = compile_unlinked_script(source).unwrap_err();
        assert_eq!(error.kind(), ErrorKind::Syntax, "{source:?}: {error}");
        assert_eq!(error.message(), message, "{source:?}");
        assert_eq!(error.span().unwrap().start.column, column, "{source:?}");
    }
}