Do not emit JSOp::RetRval for arrow function with expression body
Categories
(Core :: JavaScript Engine, defect, P3)
Tracking
()
People
(Reporter: arai, Assigned: mohamedatef1698, Mentored)
References
(Blocks 1 open bug)
Details
(Whiteboard: [lang=c++] )
Attachments
(1 file)
Reporter | ||
Comment 1•8 years ago
|
||
Reporter | ||
Updated•8 years ago
|
Reporter | ||
Comment 2•2 years ago
|
||
(the opcode are renamed and now it's JSOp::RetRval
)
Things to do here:
- Verify there's no code that expects the unreachable
JSOp::RetRval
at the end of script - Locate the code path that emits
JSOp::RetVal
at the end of arrow function - Modify the code not to emit it, if it's arrow function with expression body
JSOp::RetRval
itself is emitted in the following function, in BytecodeEmitter
class.
struct MOZ_STACK_CLASS BytecodeEmitter {
...
[[nodiscard]] bool emitReturnRval() { return emit1(JSOp::RetRval); }
Opcodes.h contains document for each opcode.
/*
* Stop execution and return the current stack frame's `returnValue`. If no
* `JSOp::SetRval` instruction has been executed in this stack frame, this
* is `undefined`.
*
* Also emitted at end of every script so consumers don't need to worry
* about running off the end.
*
* If the current script is a derived class constructor, `returnValue` must
* be an object. The script can use `JSOp::CheckReturn` to ensure this.
*
* Category: Control flow
* Type: Return
* Operands:
* Stack: =>
*/ \
MACRO(RetRval, ret_rval, NULL, 1, 0, 0, JOF_BYTE) \
If you have any question, feel free to ask here, or in https://chat.mozilla.org/#/room/#spidermonkey:mozilla.org
Reporter | ||
Updated•2 years ago
|
Assignee | ||
Comment 3•2 years ago
|
||
I moved
if (!bce_->emitReturnRval()) {
return false;
}
inside this condition if (!funbox_->hasExprBody())
https://searchfox.org/mozilla-central/source/js/src/frontend/FunctionEmitter.cpp#628
it works for arrow functions but for a test case like this:
function foo(){var x = 5; return x;}
it doesn't work, and produce the unreachable RetRval
.
but if we remove this part completely it removes the RetRval
from the empty arrow functions.
Am I on the right path?
Assignee | ||
Comment 4•2 years ago
|
||
(In reply to Mohamed Atef from comment #3)
I moved
if (!bce_->emitReturnRval()) { return false; }
what about deleting this part?
I think It works fine.
inside this condition
if (!funbox_->hasExprBody())
https://searchfox.org/mozilla-central/source/js/src/frontend/FunctionEmitter.cpp#628
it works for arrow functions but for a test case like this:
function foo(){var x = 5; return x;}
it doesn't work, and produce the unreachableRetRval
.
but if we remove this part completely it removes theRetRval
from the empty arrow functions.
Am I on the right path?
Assignee | ||
Comment 5•2 years ago
|
||
(In reply to Mohamed Atef from comment #4)
(In reply to Mohamed Atef from comment #3)
I moved
if (!bce_->emitReturnRval()) { return false; }
what about deleting this part?
I think It works fine.
sorry for that.
We should emit RetRval if it's an empty function, right?inside this condition
if (!funbox_->hasExprBody())
https://searchfox.org/mozilla-central/source/js/src/frontend/FunctionEmitter.cpp#628
it works for arrow functions but for a test case like this:
function foo(){var x = 5; return x;}
it doesn't work, and produce the unreachableRetRval
.
but if we remove this part completely it removes theRetRval
from the empty arrow functions.
Am I on the right path?
Assignee | ||
Comment 6•2 years ago
|
||
(In reply to Mohamed Atef from comment #3)
I moved
if (!bce_->emitReturnRval()) { return false; }
inside this condition
if (!funbox_->hasExprBody())
https://searchfox.org/mozilla-central/source/js/src/frontend/FunctionEmitter.cpp#628
it works for arrow functions but for a test case like this:
function foo(){var x = 5; return x;}
it doesn't work, and produce the unreachableRetRval
.
from trying several test cases ()=>{var x; return x;} doesn't have an expression body.
I think the solution mentioned above is what we want.
but what if the function doesn't have an expression body?
but if we remove this part completely it removes theRetRval
from the empty arrow functions.
Am I on the right path?
Reporter | ||
Comment 7•2 years ago
|
||
Good point.
Currently, JSOp::RetRval
is always emitted at the end of function to catch all code paths that doesn't have explicit return
statement (JSOp::Return
), so that the function returns undefined
.
Ultimately we should emit JSOp::RetRval
only if it's reachable, but so far this bug focuses to arrow function with expression body case, as a first step, given arrow function with expression body is known to always have JSOp::Return
, and it doesn't require any code flow analysis or whatever.
And the fix you've done sounds good.
So, don't worry about function foo(){var x = 5; return x;}
case or ()=>{var x; return x;}
case. We can look into it in followup bug.
Reporter | ||
Updated•2 years ago
|
Assignee | ||
Comment 8•2 years ago
|
||
Comment 10•2 years ago
|
||
bugherder |
Updated•2 years ago
|
Description
•