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
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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
use crate::engine::compiler::parser::diagnostics::IdentifierContext;
use crate::engine::compiler::parser::diagnostics::source_offset;

use crate::engine::api::error::Error;
use crate::engine::code::bytecode::{DefineMethodKind, Instruction};
use crate::engine::code::function::metadata::FunctionKind as BytecodeFunctionKind;
use crate::engine::compiler::lexer::Identifier;
use crate::engine::compiler::lexer::LexContext;
use crate::engine::compiler::lexer::Punctuator;
use crate::engine::compiler::lexer::Span;
use crate::engine::compiler::lexer::TokenKind;
use crate::engine::compiler::model::ir::function::FunctionIrOptions;
use crate::engine::compiler::model::ir::function::FunctionKind;
use crate::engine::compiler::model::ir::function::FunctionSourceInfo;
use crate::engine::compiler::model::ir::function::ParentLink;
use crate::engine::compiler::model::ir::function::SuperCapabilities;
use crate::engine::compiler::model::ir::{FunctionId, IrConstant, IrOp, SpannedIrOp};
use crate::engine::compiler::names::NameId;
use crate::engine::compiler::parser::builder::FunctionBuilder;
use crate::engine::compiler::parser::context::AnonymousFunctionDefinition;
use crate::engine::compiler::parser::context::Parser;
use crate::engine::compiler::relocation::insert_hoist_fragment;
use crate::engine::compiler::stack_guard::ParserStackFrame;
use crate::source::SourceOffset;

pub(super) struct ParsedFunctionDefinition {
    pub(super) constant: u32,
    pub(super) child: FunctionId,
    pub(super) name: Option<(NameId, Span)>,
}

pub(super) struct FunctionDefinitionHeader<'source> {
    pub(super) span: Span,
    pub(super) name: Option<(Identifier<'source>, Span)>,
    pub(super) execution_kind: BytecodeFunctionKind,
    parent_context: LexContext,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
struct FunctionDefinitionOptions {
    kind: FunctionKind,
    execution_kind: BytecodeFunctionKind,
    private_name_binding: bool,
    class_constructor: bool,
    derived_class_constructor: bool,
    reject_duplicate_parameters: bool,
    allow_trailing_parameter_comma: bool,
    object_method_kind: Option<DefineMethodKind>,
    accessor_parameter_count: Option<usize>,
}

impl FunctionDefinitionOptions {
    const fn ordinary(private_name_binding: bool) -> Self {
        Self {
            kind: FunctionKind::Ordinary,
            execution_kind: BytecodeFunctionKind::Normal,
            private_name_binding,
            class_constructor: false,
            derived_class_constructor: false,
            reject_duplicate_parameters: false,
            allow_trailing_parameter_comma: true,
            object_method_kind: None,
            accessor_parameter_count: None,
        }
    }

    const fn object_method(method_kind: DefineMethodKind) -> Self {
        Self {
            kind: FunctionKind::Method,
            execution_kind: BytecodeFunctionKind::Normal,
            private_name_binding: false,
            class_constructor: false,
            derived_class_constructor: false,
            // QuickJS applies the ordinary-method UniqueFormalParameters early
            // error even when the surrounding source and body are sloppy.
            // Accessors first enforce their zero/one-parameter arity.
            reject_duplicate_parameters: matches!(method_kind, DefineMethodKind::Method),
            allow_trailing_parameter_comma: true,
            object_method_kind: Some(method_kind),
            accessor_parameter_count: match method_kind {
                DefineMethodKind::Method => None,
                DefineMethodKind::Getter => Some(0),
                DefineMethodKind::Setter => Some(1),
            },
        }
    }

    const fn generator_method() -> Self {
        Self {
            kind: FunctionKind::Method,
            execution_kind: BytecodeFunctionKind::Generator,
            private_name_binding: false,
            class_constructor: false,
            derived_class_constructor: false,
            reject_duplicate_parameters: true,
            allow_trailing_parameter_comma: true,
            object_method_kind: Some(DefineMethodKind::Method),
            accessor_parameter_count: None,
        }
    }

    const fn async_method() -> Self {
        let mut options = Self::object_method(DefineMethodKind::Method);
        options.execution_kind = BytecodeFunctionKind::Async;
        options
    }

    const fn async_generator_method() -> Self {
        let mut options = Self::generator_method();
        options.execution_kind = BytecodeFunctionKind::AsyncGenerator;
        options
    }
}

impl<'source> Parser<'source> {
    pub(super) fn parse_function_expression(&mut self) -> Result<(), Error> {
        let parsed = self.parse_function_definition(false, true)?;
        self.emit(IrOp::MakeClosure(parsed.constant))?;
        self.anonymous_function_definition = parsed
            .name
            .is_none()
            .then_some(AnonymousFunctionDefinition::Function);
        Ok(())
    }

    /// Parse the common ordinary-function grammar and publish its child
    /// constant in the defining function. The caller decides whether that
    /// constant is evaluated in expression position or recorded for Program
    /// declaration hoisting.
    pub(super) fn parse_function_definition(
        &mut self,
        require_name: bool,
        private_name_binding: bool,
    ) -> Result<ParsedFunctionDefinition, Error> {
        let header = self.parse_function_definition_header(require_name)?;
        self.parse_function_definition_tail(header, private_name_binding)
    }

    pub(super) fn parse_function_definition_header(
        &mut self,
        require_name: bool,
    ) -> Result<FunctionDefinitionHeader<'source>, Error> {
        let parent_context = self.lexer.context();
        let span = self.current().span;
        let async_function = self.async_function_ahead();
        if async_function {
            self.advance()?;
            if self.current().line_terminator_before
                || !matches!(
                    self.current().kind,
                    TokenKind::Keyword(crate::engine::compiler::lexer::Keyword::Function)
                )
            {
                return Err(Error::internal(
                    "async function lookahead disagreed with committed tokens",
                ));
            }
        }
        self.advance()?;
        let execution_kind = if self.is_punctuator(Punctuator::Multiply) {
            self.advance()?;
            if async_function {
                BytecodeFunctionKind::AsyncGenerator
            } else {
                BytecodeFunctionKind::Generator
            }
        } else if async_function {
            BytecodeFunctionKind::Async
        } else {
            BytecodeFunctionKind::Normal
        };
        let name = match self.current().kind {
            TokenKind::Identifier(identifier) => {
                let span = self.current().span;
                self.validate_identifier(
                    &identifier,
                    span,
                    false,
                    IdentifierContext::FunctionName,
                )?;
                self.advance()?;
                Some((identifier, span))
            }
            // QuickJS consumes a contextual TOK_YIELD as the name of any
            // sloppy FunctionExpression. This is deliberately token-sensitive:
            // an outer generator produces TOK_YIELD and therefore permits
            // `(function* yield(){})`, while the same spelling at top level is
            // TOK_IDENT and is rejected below for a generator expression.
            TokenKind::Keyword(crate::engine::compiler::lexer::Keyword::Yield)
                if !require_name && !parent_context.strict =>
            {
                let span = self.current().span;
                let identifier = Identifier {
                    raw: "yield",
                    has_escape: false,
                    keyword_hint: Some(crate::engine::compiler::lexer::Keyword::Yield),
                    escaped_reserved_word: false,
                };
                self.advance()?;
                Some((identifier, span))
            }
            // QuickJS has the same token-sensitive FunctionExpression escape
            // hatch for TOK_AWAIT when the enclosing script is not a module.
            // An async parent lexes this spelling as TOK_AWAIT and therefore
            // admits it, while top-level `await` arrives as TOK_IDENT and the
            // async-expression early error below still rejects it.
            TokenKind::Keyword(crate::engine::compiler::lexer::Keyword::Await)
                if !require_name && !parent_context.module =>
            {
                let span = self.current().span;
                let identifier = Identifier {
                    raw: "await",
                    has_escape: false,
                    keyword_hint: Some(crate::engine::compiler::lexer::Keyword::Await),
                    escaped_reserved_word: false,
                };
                self.advance()?;
                Some((identifier, span))
            }
            _ => None,
        };
        if require_name && name.is_none() {
            return Err(self.syntax_here("function name expected"));
        }
        Ok(FunctionDefinitionHeader {
            span,
            name,
            execution_kind,
            parent_context,
        })
    }

    pub(super) fn parse_function_definition_tail(
        &mut self,
        header: FunctionDefinitionHeader<'source>,
        private_name_binding: bool,
    ) -> Result<ParsedFunctionDefinition, Error> {
        // Preserve QuickJS's token-sensitive declaration/expression asymmetry.
        // A top-level generator-expression name arrives as TOK_IDENT and is
        // reserved, while contextual TOK_YIELD from an outer sloppy generator
        // is admitted by quickjs.c:36419-36439.
        if private_name_binding
            && matches!(
                header.execution_kind,
                BytecodeFunctionKind::Generator | BytecodeFunctionKind::AsyncGenerator
            )
            && let Some((identifier, span)) = &header.name
            && self.identical_name(identifier, "yield")
            && (!header.parent_context.generator
                || header.parent_context.strict
                || identifier.has_escape
                || identifier.keyword_hint != Some(crate::engine::compiler::lexer::Keyword::Yield))
        {
            return Err(Error::syntax("'yield' is a reserved identifier", *span));
        }
        // Pinned QuickJS rejects an identifier-token `await` as an async
        // FunctionExpression name. The contextual TOK_AWAIT admitted above is
        // a separate legacy path and must survive this early error.
        if private_name_binding
            && matches!(
                header.execution_kind,
                BytecodeFunctionKind::Async | BytecodeFunctionKind::AsyncGenerator
            )
            && let Some((identifier, span)) = &header.name
            && self.identical_name(identifier, "await")
            && (!header.parent_context.async_function
                || header.parent_context.module
                || identifier.has_escape
                || identifier.keyword_hint != Some(crate::engine::compiler::lexer::Keyword::Await))
        {
            return Err(Error::syntax("'await' is a reserved identifier", *span));
        }
        let mut options = FunctionDefinitionOptions::ordinary(private_name_binding);
        options.execution_kind = header.execution_kind;
        self.parse_function_definition_tail_with_options(header, options)
    }

    /// Parse a synchronous object-literal method or accessor after its property
    /// name has been consumed. Unlike a named function expression, the property
    /// key is only the eventual public `.name`; it must never create a private
    /// self binding inside the function body.
    pub(super) fn parse_object_method_definition(
        &mut self,
        function_span: Span,
        method_kind: DefineMethodKind,
    ) -> Result<FunctionId, Error> {
        let parsed = self.parse_function_definition_tail_with_options(
            FunctionDefinitionHeader {
                span: function_span,
                name: None,
                execution_kind: BytecodeFunctionKind::Normal,
                parent_context: self.lexer.context(),
            },
            FunctionDefinitionOptions::object_method(method_kind),
        )?;
        self.emit(IrOp::MakeClosure(parsed.constant))?;
        self.anonymous_function_definition = None;
        Ok(parsed.child)
    }

    /// Parse one public or private synchronous generator method after its
    /// property name has been consumed. Grammar role remains `Method`; only
    /// callable execution metadata changes to `Generator`.
    pub(super) fn parse_generator_method_definition(
        &mut self,
        function_span: Span,
    ) -> Result<FunctionId, Error> {
        if !self.is_punctuator(Punctuator::LeftParen) {
            return Err(self.syntax_here("invalid property name"));
        }
        let parsed = self.parse_function_definition_tail_with_options(
            FunctionDefinitionHeader {
                span: function_span,
                name: None,
                execution_kind: BytecodeFunctionKind::Generator,
                parent_context: self.lexer.context(),
            },
            FunctionDefinitionOptions::generator_method(),
        )?;
        self.emit(IrOp::MakeClosure(parsed.constant))?;
        self.anonymous_function_definition = None;
        Ok(parsed.child)
    }

    /// Parse one public ordinary async method after its property name has been
    /// consumed. QuickJS keeps the grammar role as `Method` and changes only
    /// the callable execution kind, so HomeObject and method naming continue
    /// through the ordinary DefineMethod publication path.
    pub(super) fn parse_async_method_definition(
        &mut self,
        function_span: Span,
    ) -> Result<FunctionId, Error> {
        if !self.is_punctuator(Punctuator::LeftParen) {
            return Err(self.syntax_here("invalid property name"));
        }
        let parsed = self.parse_function_definition_tail_with_options(
            FunctionDefinitionHeader {
                span: function_span,
                name: None,
                execution_kind: BytecodeFunctionKind::Async,
                parent_context: self.lexer.context(),
            },
            FunctionDefinitionOptions::async_method(),
        )?;
        self.emit(IrOp::MakeClosure(parsed.constant))?;
        self.anonymous_function_definition = None;
        Ok(parsed.child)
    }

    /// Parse one public object-literal or class async-generator method after
    /// its property name has been consumed. The grammar role remains `Method`
    /// while the independent AsyncGenerator execution kind selects the
    /// Promise-backed iterator driver and intrinsic prototype graph.
    pub(super) fn parse_async_generator_method_definition(
        &mut self,
        function_span: Span,
    ) -> Result<FunctionId, Error> {
        if !self.is_punctuator(Punctuator::LeftParen) {
            return Err(self.syntax_here("invalid property name"));
        }
        let parsed = self.parse_function_definition_tail_with_options(
            FunctionDefinitionHeader {
                span: function_span,
                name: None,
                execution_kind: BytecodeFunctionKind::AsyncGenerator,
                parent_context: self.lexer.context(),
            },
            FunctionDefinitionOptions::async_generator_method(),
        )?;
        self.emit(IrOp::MakeClosure(parsed.constant))?;
        self.anonymous_function_definition = None;
        Ok(parsed.child)
    }

    /// Parse a base or derived class constructor using QuickJS's
    /// concise-method binding model. A derived constructor additionally owns
    /// the one-shot lexical `this` binding and allows `super()` through arrows
    /// and direct eval.
    pub(super) fn parse_class_constructor_definition(
        &mut self,
        function_span: Span,
        derived: bool,
    ) -> Result<ParsedFunctionDefinition, Error> {
        let mut options = FunctionDefinitionOptions::object_method(DefineMethodKind::Method);
        options.class_constructor = true;
        options.derived_class_constructor = derived;
        self.parse_function_definition_tail_with_options(
            FunctionDefinitionHeader {
                span: function_span,
                name: None,
                execution_kind: BytecodeFunctionKind::Normal,
                parent_context: self.lexer.context(),
            },
            options,
        )
    }

    fn parse_function_definition_tail_with_options(
        &mut self,
        header: FunctionDefinitionHeader<'source>,
        options: FunctionDefinitionOptions,
    ) -> Result<ParsedFunctionDefinition, Error> {
        let FunctionDefinitionHeader {
            span: function_span,
            name: function_name_token,
            execution_kind,
            parent_context,
        } = header;
        if execution_kind != options.execution_kind {
            return Err(Error::internal(
                "function header and definition execution kinds disagree",
            ));
        }
        let parent = self.current_function;
        let parent_strict = self.functions[parent].strict;
        let function_name = function_name_token
            .as_ref()
            .map(|(identifier, _)| self.intern_identifier(identifier));
        let child = self.functions.len();
        let parent_scope = self.functions[parent].context.current_scope;
        let super_capabilities = match options.kind {
            FunctionKind::Method if options.derived_class_constructor => {
                SuperCapabilities::CALL_AND_PROPERTY
            }
            FunctionKind::Method => SuperCapabilities::PROPERTY,
            FunctionKind::Ordinary => SuperCapabilities::NONE,
            _ => {
                return Err(Error::internal(
                    "ordinary function parser received an invalid function kind",
                ));
            }
        };
        // Parameter initializers are function code. Establish the child before
        // consuming `(` so every initializer, nested closure and pseudo-binding
        // reference is authored in the callee rather than its parent.
        self.functions.push(FunctionBuilder::new(
            Some(ParentLink {
                function: parent,
                definition_scope: parent_scope,
            }),
            options.kind,
            FunctionSourceInfo {
                span: function_span,
                definition: source_offset(function_span)?,
                range: None,
            },
            FunctionIrOptions {
                function_name,
                private_name_binding: options.private_name_binding && function_name_token.is_some(),
                class_constructor: options.class_constructor,
                derived_class_constructor: options.derived_class_constructor,
                defined_argument_count: 0,
                has_simple_parameter_list: true,
                rest_parameter: None,
                parameters: Vec::new(),
                strict: parent_strict,
                super_capabilities,
            },
            &mut self.names,
        )?);
        self.functions[child].execution_kind = options.execution_kind;
        self.current_function = child;
        let mut child_context = parent_context;
        child_context.strict = parent_strict;
        child_context.generator = matches!(
            options.execution_kind,
            BytecodeFunctionKind::Generator | BytecodeFunctionKind::AsyncGenerator
        );
        child_context.async_function = matches!(
            options.execution_kind,
            BytecodeFunctionKind::Async | BytecodeFunctionKind::AsyncGenerator
        );
        self.relex_current_with_context(child_context)?;
        let parameter_scan = self.parenthesized_parameter_scan();
        if parameter_scan.is_some_and(|scan| scan.has_assignment) {
            self.activate_parameter_environment_from_scan(
                parameter_scan.and_then(|scan| scan.bound_name_count),
            )?;
        }
        self.expect_punctuator(Punctuator::LeftParen)?;

        let mut parameter_tokens = Vec::new();
        let mut parameter_list_end_span = self.current().span;
        if self.is_punctuator(Punctuator::RightParen) {
            self.advance()?;
        } else {
            loop {
                let is_rest = self.consume_punctuator(Punctuator::Ellipsis)?;
                if let Some(method_kind) = options.object_method_kind
                    && is_rest
                    && matches!(method_kind, DefineMethodKind::Setter)
                {
                    return Err(Error::syntax(
                        "invalid number of arguments for getter or setter",
                        self.current().span,
                    ));
                }
                let pattern = match self.current().kind {
                    TokenKind::Punctuator(Punctuator::LeftBracket) => Some(true),
                    TokenKind::Punctuator(Punctuator::LeftBrace) => Some(false),
                    _ => None,
                };
                if let Some(array_pattern) = pattern {
                    let span = self.current().span;
                    self.activate_pattern_parameter_initialization()?;
                    if is_rest {
                        let start = self.register_rest_pattern_parameter()?;
                        let has_initializer = if array_pattern {
                            self.parse_array_rest_parameter_binding_pattern(start)?
                        } else {
                            self.parse_object_rest_parameter_binding_pattern(start)?
                        };
                        self.finish_pattern_parameter_length(start, has_initializer)?;
                        if !self.is_punctuator(Punctuator::RightParen) {
                            return Err(self.syntax_here("expecting ')'"));
                        }
                        parameter_list_end_span = self.current().span;
                        self.advance()?;
                        break;
                    }
                    let argument = self.append_pattern_parameter(span)?;
                    let has_initializer = if array_pattern {
                        self.parse_array_parameter_binding_pattern(argument)?
                    } else {
                        self.parse_object_parameter_binding_pattern(argument)?
                    };
                    self.finish_pattern_parameter_length(argument, has_initializer)?;

                    if !self.consume_punctuator(Punctuator::Comma)? {
                        if !self.is_punctuator(Punctuator::RightParen) {
                            return Err(Error::syntax("expecting ','", self.current().span));
                        }
                        parameter_list_end_span = self.current().span;
                        self.advance()?;
                        break;
                    }
                    if self.is_punctuator(Punctuator::RightParen) {
                        if options.allow_trailing_parameter_comma {
                            parameter_list_end_span = self.current().span;
                            self.advance()?;
                            break;
                        }
                        return Err(self.unsupported_here(
                            "a trailing comma in this parameter list is not implemented yet",
                        ));
                    }
                    continue;
                }
                let token = *self.current();
                let TokenKind::Identifier(identifier) = token.kind else {
                    return Err(self.syntax_here("missing formal parameter"));
                };
                self.validate_identifier(
                    &identifier,
                    token.span,
                    false,
                    IdentifierContext::Argument,
                )?;
                parameter_tokens.push((identifier, token.span));
                let parameter = self.intern_identifier(&identifier);
                self.advance()?;
                if is_rest {
                    self.register_rest_identifier_parameter(parameter, token.span)?;
                    if !self.is_punctuator(Punctuator::RightParen) {
                        return Err(self.syntax_here("expecting ')'"));
                    }
                    parameter_list_end_span = self.current().span;
                    self.advance()?;
                    break;
                }
                if self.is_punctuator(Punctuator::Equal) {
                    self.parse_default_identifier_parameter(parameter, token.span)?;
                } else {
                    self.register_plain_identifier_parameter(parameter, token.span)?;
                }
                if !self.consume_punctuator(Punctuator::Comma)? {
                    if !self.is_punctuator(Punctuator::RightParen) {
                        return Err(Error::syntax("expecting ','", self.current().span));
                    }
                    parameter_list_end_span = self.current().span;
                    self.advance()?;
                    break;
                }
                if self.is_punctuator(Punctuator::RightParen) {
                    if options.allow_trailing_parameter_comma {
                        parameter_list_end_span = self.current().span;
                        self.advance()?;
                        break;
                    }
                    return Err(self.unsupported_here(
                        "a trailing comma in this simple parameter list is not implemented yet",
                    ));
                }
            }
        }
        if options
            .accessor_parameter_count
            .is_some_and(|expected| self.functions[child].parameters.len() != expected)
        {
            return Err(Error::syntax(
                "invalid number of arguments for getter or setter",
                parameter_list_end_span,
            ));
        }
        self.expect_punctuator(Punctuator::LeftBrace)?;

        let has_use_strict = self.directive_prologue_has_use_strict(parent_strict)?;
        let strict = parent_strict || has_use_strict;
        child_context.strict = strict;
        self.relex_current_with_context(child_context)?;
        let has_simple_parameter_list = self.functions[child].has_simple_parameter_list;
        if has_use_strict && !has_simple_parameter_list {
            return Err(Error::syntax(
                "\"use strict\" not allowed in function with default or destructuring parameter",
                self.current().span,
            ));
        }
        if strict {
            let strict_validation_span = self.current().span;
            if let Some((identifier, _)) = &function_name_token {
                self.validate_identifier(
                    identifier,
                    strict_validation_span,
                    true,
                    IdentifierContext::FunctionName,
                )?;
            }
            for (identifier, _) in &parameter_tokens {
                self.validate_identifier(
                    identifier,
                    strict_validation_span,
                    true,
                    IdentifierContext::Argument,
                )?;
            }
        }
        if options.reject_duplicate_parameters || strict || !has_simple_parameter_list {
            let parameters = &self.functions[child].parameter_names;
            for (index, parameter) in parameters.iter().enumerate() {
                if parameters[..index].contains(parameter) {
                    return Err(Error::syntax(
                        "duplicate argument names not allowed in this context",
                        self.current().span,
                    ));
                }
            }
        }
        self.functions[child].strict = strict;
        if options.derived_class_constructor {
            self.functions[child].allocate_derived_constructor_pseudo_bindings(&mut self.names)?;
        }
        if options.class_constructor {
            // Parameter parsing may replace the initial body scope with a
            // parentless Parameter Environment or a FunctionRoot pattern
            // segment. Install the guard only after that shape is final: it
            // must follow the Parameter Environment's TDZ reset, but precede
            // every default, destructuring, and rest initializer. Emitting it
            // before parsing would both block BindingPattern activation and
            // let the later rest-prologue pass move Rest ahead of the guard.
            let guard_at = if let Some(parameter_scope) = self.functions[child].parameter_scope {
                self.functions[child]
                    .ops
                    .iter()
                    .position(|operation| {
                        matches!(operation.op, IrOp::EnterScope(scope) if scope == parameter_scope)
                    })
                    .and_then(|entry| entry.checked_add(1))
                    .ok_or_else(|| Error::internal("class constructor lost its parameter scope"))?
            } else {
                0
            };
            let mut guard = vec![SpannedIrOp {
                op: IrOp::Bytecode(Instruction::CheckCtor),
                pc_site: None,
            }];
            if !options.derived_class_constructor {
                guard.extend([
                    SpannedIrOp {
                        op: IrOp::Bytecode(Instruction::PushThis),
                        pc_site: None,
                    },
                    SpannedIrOp {
                        op: IrOp::Bytecode(Instruction::PushActiveFunction),
                        pc_site: None,
                    },
                    SpannedIrOp {
                        op: IrOp::Bytecode(Instruction::CallClassInstanceInitializer),
                        pc_site: None,
                    },
                    SpannedIrOp {
                        op: IrOp::Bytecode(Instruction::Drop),
                        pc_site: None,
                    },
                ]);
            }
            insert_hoist_fragment(&mut self.functions[child], guard_at, guard)?;
        }
        self.finish_identifier_parameter_environment()?;
        if matches!(
            options.execution_kind,
            BytecodeFunctionKind::Generator | BytecodeFunctionKind::AsyncGenerator
        ) {
            self.insert_generator_initial_yield()?;
        }
        self.functions[child].context.in_function_body = true;
        self.parse_recursion(ParserStackFrame::FunctionBody, Self::parse_function_body)?;
        let closing_brace = self.current().span;
        self.relex_current_with_context(parent_context)?;
        self.expect_punctuator(Punctuator::RightBrace)?;
        self.functions[child].source.range = Some(
            source_offset(function_span)?
                ..SourceOffset::try_from_usize(closing_brace.end.byte_offset)
                    .map_err(|error| Error::internal(error.to_string()))?,
        );
        self.functions[child].finish_parsing()?;
        self.current_function = parent;

        let constant = self.add_constant(IrConstant::Child(child))?;
        Ok(ParsedFunctionDefinition {
            constant,
            child,
            name: function_name_token
                .map(|(identifier, span)| (self.intern_identifier(&identifier), span)),
        })
    }
}