viperjs 0.3.0

An embeddable JavaScript engine in safe Rust, with zero runtime dependencies.
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
//! `for`-`in` and `for`-`of` (ECMAScript §14.7.5).
//!
//! The head is shared with the three-part loop and lives in [`super::for_statement`], because
//! nothing distinguishes the three productions until the token *after* the first clause: a `;`
//! makes it a `ForStatement`, and `in` or `of` a `ForInOfStatement`. That is also why the first
//! clause is read under `[~In]` — see [`super::expression::AllowIn`]. This file is what happens
//! once the answer is known.
//!
//! # The two forms differ in more than a word
//!
//! `for (a in b, c)` parses and `for (a of b, c)` does not, because the iterable of a `for`-`in`
//! is an `Expression[+In]` and that of a `for`-`of` is an `AssignmentExpression[+In]` — no comma.
//! Both are `[+In]`, so `for (a in b in c)` is `for (a in (b in c))`.
//!
//! `for`-`of` also carries a lookahead restriction its sibling does not:
//! `[lookahead ∉ { let, async of }]`. `for (let.a of b)` is refused and `for (let.a in b)` is
//! fine, because the restriction is on the *token* `let` rather than on anything it might begin —
//! and `for ((let) of b)` is fine again, a parenthesis being a different token. The `async` half
//! is a two-token restriction, so `for (async of b)` is refused while `for (async.x of b)` and
//! `for (async() of b)` are not.
//!
//! # A binding here is one name and takes no initialiser
//!
//! `ForBinding` is a `BindingIdentifier` or a `BindingPattern`, singular, with no `Initializer` in
//! the grammar at all. So `for (var a, b in c)` and `for (var a = 1 of b)` have no derivation.
//!
//! `for (var a = 1 in b)` is the exception, and it is refused here. Annex B.3.5 restores an
//! initialiser to exactly that one shape — `var`, `in`, a `BindingIdentifier` rather than a
//! pattern, and non-strict code — so refusing it is right everywhere except sloppy code on a host
//! that implements B.3.5.
//!
//! **The reason recorded here used to be that this parser did not track strictness. It does** —
//! `Parser::strict`, set by the Directive Prologue and accurate at this point — so that is no
//! longer what stands in the way. What does is the *runtime* half: §B.3.5 evaluates the
//! initialiser and assigns it **before** `ForIn/OfHeadEvaluation` runs, which is a shape the
//! compiler has nowhere to put, and the whole clause is worth one run of test262. Costed and left
//! deliberately; the condition here is one `&& !self.strict` away whenever the other half is
//! written.

use super::expression::AllowIn;
use super::{ParseError, ParseErrorKind, Parser};
use crate::ast::{Expr, ForInOfKind, ForInOfStatement, ForInOfTarget, StmtKind};
use crate::lexer::{Goal, ReservedWord, TokenKind};
use crate::span::Span;

impl Parser<'_> {
    /// Whether an `in` or `of` follows the first clause of a `for` header.
    ///
    /// `of` is not a reserved word — it is an ordinary identifier everywhere else — so it is
    /// recognised by its text, and only when written without escapes: §5.1.5.1 makes a terminal
    /// match literal source characters, the same rule that keeps `let` from being a declaration.
    pub(super) fn for_in_of_operator(&self) -> Option<ForInOfKind> {
        if self.current.kind == TokenKind::Keyword(ReservedWord::In) {
            return Some(ForInOfKind::In);
        }
        if self.at_contextual("of") {
            return Some(ForInOfKind::Of);
        }
        None
    }

    /// Everything after the `in` or `of`, with `left` already parsed.
    pub(super) fn parse_for_in_of_tail(
        &mut self,
        left: ForInOfTarget,
        kind: ForInOfKind,
    ) -> Result<(StmtKind, Span), ParseError> {
        self.advance(Goal::RegExp)?;
        self.enter()?;
        // The one real difference between the two productions: `in` takes an `Expression` and
        // `of` an `AssignmentExpression`, so a comma sequences in the first and ends the header
        // in the second. Both are `[+In]` — the restriction was on the clause before this one.
        let right = match kind {
            ForInOfKind::In => self.parse_expression(AllowIn::Yes),
            ForInOfKind::Of => self.parse_assignment(AllowIn::Yes),
        };
        self.leave();
        let right = right?;
        self.eat(TokenKind::RParen, Goal::RegExp, "`)`")?;
        let body = self.parse_statement(super::LabelledFunction::Refused)?;
        if let ForInOfTarget::Declaration(declaration) = &left
            && declaration.kind.is_lexical()
        {
            // §14.7.5.1, the same rule §14.7.4.1 states for the three-part form: a header
            // declaration is its own scope, and a `var` in the body passes through it.
            super::scope::check_header_against_body(declaration, std::slice::from_ref(&body))?;
        }
        let end = body.span;
        Ok((
            StmtKind::ForInOf(Box::new(ForInOfStatement {
                kind,
                // Set by [`Parser::parse_for`] once the header has said which production won.
                is_await: false,
                left,
                right,
                body,
            })),
            end,
        ))
    }

    /// §14.7.5.1: the target of a `for`-`in` or `for`-`of` must be assignable.
    ///
    /// Stated as "a Syntax Error if the AssignmentTargetType is invalid", which is the same test
    /// an assignment applies — so `for (1 in b)` and `for (a + b in c)` are refused for the
    /// reason `1 = b` is, while `for ((a) in b)` is fine because parentheses do not change what
    /// a thing is.
    pub(super) fn check_for_in_of_target(&self, left: &Expr) -> Result<(), ParseError> {
        if super::operator::is_simple_assignment_target(left) {
            // A head like `for (eval in x)` assigns to the name on every turn of the loop, so
            // §13.1.1 reaches it — and this is the only place it can be asked, the target here
            // never passing through the assignment level or through a refinement.
            if let crate::ast::ExprKind::Identifier(name) = &left.kind {
                self.check_target_name(name, left.span)?;
            }
            return Ok(());
        }
        Err(ParseError {
            kind: ParseErrorKind::InvalidAssignmentTarget,
            span: left.span,
        })
    }
}

#[cfg(test)]
mod tests {
    use crate::parser::test_support::*;
    use crate::parser::{ParseErrorKind, parse_script};

    #[test]
    fn both_forms_take_a_target_or_a_binding() {
        assert_eq!(statements("for (a in b);"), ["(for-in a b <empty>)"]);
        assert_eq!(statements("for (a of b);"), ["(for-of a b <empty>)"]);
        assert_eq!(
            statements("for (var a in b);"),
            ["(for-in (var a) b <empty>)"]
        );
        assert_eq!(
            statements("for (var a of b);"),
            ["(for-of (var a) b <empty>)"]
        );
        assert_eq!(
            statements("for (let a in b);"),
            ["(for-in (let a) b <empty>)"]
        );
        assert_eq!(
            statements("for (let a of b);"),
            ["(for-of (let a) b <empty>)"]
        );
        assert_eq!(
            statements("for (const a in b);"),
            ["(for-in (const a) b <empty>)"]
        );
        assert_eq!(
            statements("for (const a of b) c;"),
            ["(for-of (const a) b c)"]
        );
        // A `LeftHandSideExpression` is more than a name.
        assert_eq!(
            statements("for (a.b in c);"),
            ["(for-in (. a b) c <empty>)"]
        );
        assert_eq!(
            statements("for (a[b] of c);"),
            ["(for-of ([] a b) c <empty>)"]
        );
        assert_eq!(statements("for ((a) in b);"), ["(for-in a b <empty>)"]);
        assert_eq!(statements("for (a in b) { c; }"), ["(for-in a b {c})"]);
        for source in [
            "for (a in);",
            "for (a of);",
            "for (in b);",
            "for (a in b",
            "for (a of b",
        ] {
            assert!(parse_script(source).is_err(), "{source:?}");
        }
    }

    #[test]
    fn the_iterable_of_an_in_is_an_expression_and_that_of_an_of_is_not() {
        // `in` takes `Expression[+In]`, `of` takes `AssignmentExpression[+In]`. So a comma
        // sequences in the first and simply ends the header in the second.
        assert_eq!(
            statements("for (a in b, c);"),
            ["(for-in a (, b c) <empty>)"]
        );
        assert!(parse_script("for (a of b, c);").is_err());
        assert!(parse_script("for (var a of b, c);").is_err());
        assert_eq!(
            statements("for (a of (b, c));"),
            ["(for-of a (, b c) <empty>)"]
        );
        // Both are `[+In]`: the restriction was on the clause before, and does not reach here.
        assert_eq!(
            statements("for (a in b in c);"),
            ["(for-in a (in b c) <empty>)"]
        );
        assert!(parse_script("for (a of b of c);").is_err());
        // An assignment is an `AssignmentExpression`, so both take one.
        assert_eq!(
            statements("for (a of b = c);"),
            ["(for-of a (= b c) <empty>)"]
        );
        assert_eq!(
            statements("for (var a in b = c);"),
            ["(for-in (var a) (= b c) <empty>)"]
        );
    }

    #[test]
    fn a_target_that_cannot_be_assigned_to_is_refused() {
        // §14.7.5.1, which applies the same AssignmentTargetType test an assignment does.
        assert_eq!(
            script_error("for (1 in b);").kind,
            ParseErrorKind::InvalidAssignmentTarget
        );
        for source in [
            "for (a + b in c);",
            "for ((a, b) in c);",
            "for (this of b);",
            "for (1 of b);",
        ] {
            assert_eq!(
                script_error(source).kind,
                ParseErrorKind::InvalidAssignmentTarget,
                "{source:?}"
            );
        }
        // …and parentheses do not change what a thing is.
        assert!(parse_script("for ((a) in b);").is_ok());
        assert!(parse_script("for ((a.b) of c);").is_ok());
        // A call is refused here for the same reason `f() = 1` is refused: §8.6.4 gives a
        // `CallExpression` an AssignmentTargetType of `invalid`, and only returns `web-compat`
        // instead under a Normative Optional taken by hosts that are web browsers. ViperJS is not
        // one — GOAL.md is explicit about the hosts it is for — so it takes the default, which is
        // also what every host must do in strict code. V8 accepts `for (f() in b)`; this is the
        // divergence, and it is deliberate.
        assert_eq!(
            script_error("for (f() in b);").kind,
            ParseErrorKind::InvalidAssignmentTarget
        );
        assert_eq!(
            script_error("f() = 1;").kind,
            ParseErrorKind::InvalidAssignmentTarget
        );
    }

    #[test]
    fn a_for_of_target_may_not_begin_with_let_or_be_the_word_async() {
        // `[lookahead ∉ { let, async of }]`, which `for`-`in` does not have. The `let` half is a
        // one-token restriction, so it catches anything beginning with the word…
        assert_eq!(
            script_error("for (let.a of b);").kind,
            ParseErrorKind::ForOfTargetBeginsWithLet
        );
        // …and `for`-`in` is unrestricted, which is the comparison that shows it is the token
        // being refused rather than the expression.
        assert_eq!(
            statements("for (let.a in b);"),
            ["(for-in (. let a) b <empty>)"]
        );
        assert_eq!(statements("for (let in b);"), ["(for-in let b <empty>)"]);
        // A parenthesis is a different token, so it escapes the restriction entirely.
        assert_eq!(statements("for ((let) of b);"), ["(for-of let b <empty>)"]);
        // The `async` half is a *two*-token restriction: it is the sequence `async of` that has
        // no derivation, not anything starting with `async`. It needs no code of its own — §15.9's
        // arrow head commits on `async` followed by an identifier, so what fails is the `=>` that
        // an arrow head owes. Which is what the grammar says: with the for-of alternative excluded
        // by the lookahead, the head has to be an `Expression`, and `async of` begins only one.
        assert_eq!(
            script_error("for (async of b);").kind,
            ParseErrorKind::Unexpected {
                expected: "`=>`",
                found: crate::lexer::TokenKind::Identifier {
                    contains_escape: false
                },
            }
        );
        // …and the three-part alternative was never restricted, so this is an ordinary loop whose
        // initialiser happens to be an async arrow.
        assert_eq!(
            statements("for (async of => 1;;);"),
            ["(for (async=> [of] 1) ; ; <empty>)"]
        );
        assert_eq!(
            statements("for (async.x of b);"),
            ["(for-of (. async x) b <empty>)"]
        );
        assert_eq!(
            statements("for (async in b);"),
            ["(for-in async b <empty>)"]
        );
        assert_eq!(
            statements("for ((async) of b);"),
            ["(for-of async b <empty>)"]
        );
        // §5.1.5.1 again: a lookahead restriction compares tokens against terminals, and a
        // terminal matches literal source characters — so an escaped spelling is a different
        // token and slips both halves of the restriction. The same rule that keeps
        // `\u006cet x = 1` from being a declaration.
        assert_eq!(
            statements(r"for (\u0061sync of b);"),
            ["(for-of async b <empty>)"]
        );
        assert_eq!(
            statements(r"for (\u006cet.a of b);"),
            ["(for-of (. let a) b <empty>)"]
        );
    }

    #[test]
    fn a_binding_is_one_name_and_the_grammar_gives_it_no_initialiser() {
        // `ForBinding` is singular and has no `Initializer` in the grammar at all.
        assert_eq!(
            script_error("for (var a, b in c);").kind,
            ParseErrorKind::ForInOfBindsSeveralNames
        );
        assert_eq!(
            script_error("for (let a, b of c);").kind,
            ParseErrorKind::ForInOfBindsSeveralNames
        );
        for source in [
            "for (var a = 1 of b);",
            "for (let a = 1 in b);",
            "for (let a = 1 of b);",
            "for (const a = 1 of b);",
        ] {
            assert_eq!(
                script_error(source).kind,
                ParseErrorKind::ForInOfBindingHasInitializer,
                "{source:?}"
            );
        }
        // Annex B.3.5 restores an initialiser to exactly one shape — `var`, `in`, non-strict —
        // and it is still refused here. Not for want of strictness, which this parser now has:
        // §B.3.5 assigns the initialiser *before* the head is evaluated, and that is the half the
        // compiler cannot yet express. The module doc says which line changes when it can.
        assert_eq!(
            script_error("for (var a = 1 in b);").kind,
            ParseErrorKind::ForInOfBindingHasInitializer
        );
        // A `const` with no initialiser is fine here and nowhere else, the binding coming from
        // the iteration rather than from an `=`.
        assert!(parse_script("for (const a of b);").is_ok());
        assert!(parse_script("for (const a in b);").is_ok());
        // A pattern is the other `ForBinding` alternative, and takes no initialiser either —
        // the value comes from the iteration whatever shape it is taken apart into.
        assert!(parse_script("for (let [a] of b);").is_ok());
        assert!(parse_script("for (var {a} in b);").is_ok());
        assert_eq!(
            script_error("for (let [a] = 1 of b);").kind,
            ParseErrorKind::ForInOfBindingHasInitializer
        );
    }

    #[test]
    fn the_header_is_a_scope_between_the_enclosing_one_and_the_body() {
        // §14.7.5.1, word for word the rule §14.7.4.1 states for the three-part form.
        for source in [
            "for (let a in b) { var a; }",
            "for (let a of b) { var a; }",
            "for (const a in b) { var a; }",
            "for (let a in b) var a;",
            "for (let a of b) { { if (x) var a; } }",
        ] {
            assert_eq!(
                script_error(source).kind,
                ParseErrorKind::ConflictingVarAndLexicalDeclaration,
                "{source:?}"
            );
        }
        assert!(parse_script("for (let a in b) { let a; }").is_ok());
        assert!(parse_script("for (var a in b) { var a; }").is_ok());
        assert!(parse_script("let a; for (let a in b);").is_ok());
        assert_eq!(
            script_error("let a; for (var a in b);").kind,
            ParseErrorKind::ConflictingVarAndLexicalDeclaration
        );
        // Both are iteration statements, so both take `break` and `continue`.
        assert!(parse_script("for (a in b) break;").is_ok());
        assert!(parse_script("for (a of b) continue;").is_ok());
        assert!(parse_script("for (a of b) { switch (x) { case 1: continue; } }").is_ok());
        assert_eq!(
            script_error("for (a in b) {} continue;").kind,
            ParseErrorKind::ContinueOutsideLoop
        );
    }

    #[test]
    fn no_header_however_truncated_can_panic() {
        let cases = [
            "for (a in".to_string(),
            "for (a of".to_string(),
            "for (var a in".to_string(),
            "for (let a of".to_string(),
            "for (of".to_string(),
            "for (in".to_string(),
            "for (a in b) ".repeat(1000),
            "for (a of b) ".repeat(1000),
        ];
        for source in &cases {
            let _ = parse_script(source);
        }
        assert_eq!(
            script_error(&"for (a of b) ".repeat(1000)).kind,
            ParseErrorKind::TooDeeplyNested
        );
    }
}