Skip to content

Conversation

@shvaikalesh
Copy link
Member

@shvaikalesh shvaikalesh commented Feb 7, 2024

26302cf

[JSC] emitReturn() should load `this` value from arrow function lexical environment prior to TDZ check
https://bugs.webkit.org/show_bug.cgi?id=268864
<rdar://problem/122430056>

Reviewed by Justin Michaud.

This change:

  1) Hoists first TDZ check of emitReturn() up to FunctionNode::emitBytecode(), and refactors it
     leveraging semantically equivalent ensureThis(), which makes automatically-inserted return
     equivalent to `return this`.
     I confirmed this to be the only call site of emitReturn() with unchecked thisRegister() as `src`.
     This is non-observable.

  2) Adds missing emitLoadThisFromArrowFunctionLexicalEnvironment() before second TDZ check, and
     refactors it using ensureThis().
     This is an observable change that prevents ReferenceError being thrown on totally valid and
     rather sane code of calling super() inside an arrow function before explicit `return`.
     Aligns JSC with the spec [1], V8, and SpiderMonkey.

  3) Since when `from == ReturnFrom::Finally` is true, `src` is always completionValueRegister(),
     meaning the check ^^ is useless. Removes it altogether with BytecodeGenerator::ReturnFrom.

[1]: https://tc39.es/ecma262/#sec-ecmascript-function-objects-construct-argumentslist-newtarget (step 12)

* JSTests/stress/regress-268864.js: Added.
* JSTests/test262/expectations.yaml: Mark 6 tests as passing.
* Source/JavaScriptCore/bytecompiler/BytecodeGenerator.cpp:
(JSC::BytecodeGenerator::emitReturn):
(JSC::BytecodeGenerator::emitFinallyCompletion):
* Source/JavaScriptCore/bytecompiler/BytecodeGenerator.h:
* Source/JavaScriptCore/bytecompiler/NodesCodegen.cpp:
(JSC::FunctionNode::emitBytecode):

Canonical link: https://commits.webkit.org/275425@main

18cf617

Misc iOS, tvOS & watchOS macOS Linux Windows
✅ 🧪 style 🛠 ios 🛠 mac 🛠 wpe 🛠 wincairo
🛠 ios-sim 🛠 mac-AS-debug 🧪 wpe-wk2
🧪 webkitperl 🧪 ios-wk2 🧪 api-mac 🧪 api-wpe
🧪 ios-wk2-wpt 🧪 mac-wk1 🛠 gtk
🛠 🧪 jsc 🧪 api-ios 🧪 mac-wk2 🧪 gtk-wk2
🛠 🧪 jsc-arm64 🛠 tv 🧪 mac-AS-debug-wk2 🧪 api-gtk
🛠 tv-sim 🧪 mac-wk2-stress 🛠 jsc-armv7
🛠 watch 🧪 jsc-armv7-tests
✅ 🛠 🧪 unsafe-merge 🛠 watch-sim

@shvaikalesh shvaikalesh requested a review from a team as a code owner February 7, 2024 00:05
@shvaikalesh shvaikalesh self-assigned this Feb 7, 2024
@shvaikalesh shvaikalesh added the JavaScriptCore For bugs in JavaScriptCore, the JS engine used by WebKit, other than kxmlcore issues. label Feb 7, 2024
Copy link
Contributor

@justinmichaud justinmichaud left a comment

Choose a reason for hiding this comment

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

r=me

@shvaikalesh shvaikalesh force-pushed the eng/JSC-emitReturn-should-load-this-value-from-arrow-function-lexical-environment-prior-to-TDZ-check branch from 11e828a to 18cf617 Compare February 28, 2024 06:36
@shvaikalesh shvaikalesh added skip-ews Applied to prevent a change from being run on EWS unsafe-merge-queue Applied to send a pull request to merge-queue, but skip building and testing labels Feb 28, 2024
…al environment prior to TDZ check

https://bugs.webkit.org/show_bug.cgi?id=268864
<rdar://problem/122430056>

Reviewed by Justin Michaud.

This change:

  1) Hoists first TDZ check of emitReturn() up to FunctionNode::emitBytecode(), and refactors it
     leveraging semantically equivalent ensureThis(), which makes automatically-inserted return
     equivalent to `return this`.
     I confirmed this to be the only call site of emitReturn() with unchecked thisRegister() as `src`.
     This is non-observable.

  2) Adds missing emitLoadThisFromArrowFunctionLexicalEnvironment() before second TDZ check, and
     refactors it using ensureThis().
     This is an observable change that prevents ReferenceError being thrown on totally valid and
     rather sane code of calling super() inside an arrow function before explicit `return`.
     Aligns JSC with the spec [1], V8, and SpiderMonkey.

  3) Since when `from == ReturnFrom::Finally` is true, `src` is always completionValueRegister(),
     meaning the check ^^ is useless. Removes it altogether with BytecodeGenerator::ReturnFrom.

[1]: https://tc39.es/ecma262/#sec-ecmascript-function-objects-construct-argumentslist-newtarget (step 12)

* JSTests/stress/regress-268864.js: Added.
* JSTests/test262/expectations.yaml: Mark 6 tests as passing.
* Source/JavaScriptCore/bytecompiler/BytecodeGenerator.cpp:
(JSC::BytecodeGenerator::emitReturn):
(JSC::BytecodeGenerator::emitFinallyCompletion):
* Source/JavaScriptCore/bytecompiler/BytecodeGenerator.h:
* Source/JavaScriptCore/bytecompiler/NodesCodegen.cpp:
(JSC::FunctionNode::emitBytecode):

Canonical link: https://commits.webkit.org/275425@main
@webkit-commit-queue webkit-commit-queue force-pushed the eng/JSC-emitReturn-should-load-this-value-from-arrow-function-lexical-environment-prior-to-TDZ-check branch from 18cf617 to 26302cf Compare February 28, 2024 06:39
@webkit-commit-queue
Copy link
Collaborator

Committed 275425@main (26302cf): https://commits.webkit.org/275425@main

Reviewed commits have been landed. Closing PR #23965 and removing active labels.

@webkit-commit-queue webkit-commit-queue removed the unsafe-merge-queue Applied to send a pull request to merge-queue, but skip building and testing label Feb 28, 2024
@webkit-commit-queue webkit-commit-queue merged commit 26302cf into WebKit:main Feb 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

JavaScriptCore For bugs in JavaScriptCore, the JS engine used by WebKit, other than kxmlcore issues. skip-ews Applied to prevent a change from being run on EWS

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants