Skip to content

Implement optional chaining.#1024

Open
xeioex wants to merge 3 commits intonginx:masterfrom
xeioex:optional_chaining
Open

Implement optional chaining.#1024
xeioex wants to merge 3 commits intonginx:masterfrom
xeioex:optional_chaining

Conversation

@xeioex
Copy link
Contributor

@xeioex xeioex commented Feb 11, 2026

This closes #835 feature request on Github.

This closes nginx#835 feature request on Github.
Unlike regular compound assignments (+=, -=), these operators
short-circuit: the RHS is not evaluated and no assignment occurs
if the logical condition is already satisfied.
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds runtime and parsing support for JavaScript optional chaining (?.) in njs, addressing the behavior reported in issue #835 (e.g. f?.() / obj?.prop short-circuiting on null/undefined).

Changes:

  • Introduces a new VM opcode (NJS_VMCODE_OPTIONAL_CHAIN) to implement optional-chain short-circuiting at runtime.
  • Updates the parser and bytecode generator to build/emit optional-chaining AST + vmcode (including ?.() call forms).
  • Adds a comprehensive set of unit tests covering property access, calls, delete semantics, and interactions with ??.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/test/njs_unit_test.c Adds unit tests for optional chaining behavior across many forms/corner cases.
src/njs_vmcode.h Adds NJS_VMCODE_OPTIONAL_CHAIN opcode to the VM opcode enum.
src/njs_vmcode.c Implements interpreter handling for the optional-chain opcode and wires it into the dispatch table.
src/njs_parser.c Extends parsing to recognize/construct optional-chaining AST nodes and delete behavior adjustments.
src/njs_lexer.h Introduces AST token types for optional chaining nodes (OPTIONAL_CHAIN, OPTIONAL_CHAIN_REF).
src/njs_generator.c Generates bytecode for optional chaining, including short-circuit jump patching and this handling in method-call cases.
src/njs_disassembler.c Adds disassembly formatting for the new optional-chain opcode.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 7 out of 7 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support optional chaining

2 participants