Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optional chaining #6973

Draft
wants to merge 33 commits into
base: master
Choose a base branch
from
Draft
Changes from 1 commit
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
b2f5f1b
Basic parsing
ShortDevelopment Apr 13, 2024
8e9abda
Config flag
ShortDevelopment Apr 13, 2024
2f84eb1
Parse call
ShortDevelopment Apr 13, 2024
6538ef0
Basic byte-code emission
ShortDevelopment Apr 13, 2024
a33799e
Check for `null` not truthy
ShortDevelopment Apr 15, 2024
caccbc6
Fix copyright
ShortDevelopment Apr 15, 2024
2cde756
Use less branches
ShortDevelopment Apr 15, 2024
f833213
More copyright fixes
ShortDevelopment Apr 15, 2024
d2377a4
Don't break ternary with decimal numbers
ShortDevelopment Apr 15, 2024
6a2c0ec
Honor `buildAST`
ShortDevelopment Apr 15, 2024
e3820ec
Tagged template in optional chain is syntax error
ShortDevelopment Apr 15, 2024
cc54764
short-circuit indexer expressions
ShortDevelopment Apr 15, 2024
9e11b34
Simple method call
ShortDevelopment Apr 18, 2024
2498b2d
Comments and review
ShortDevelopment Apr 18, 2024
63093df
Merge branch 'master' into feat-optional-chaining
ShortDevelopment Apr 18, 2024
6e7d935
Fix optChain right before function call
ShortDevelopment Apr 20, 2024
9369845
Fix `this` propagation
ShortDevelopment Apr 21, 2024
ba32362
Basic tests
ShortDevelopment Apr 24, 2024
c8297ec
Fix jit `_ReuseLoc`
ShortDevelopment May 4, 2024
8baa9e6
Don't use `LdMethodFld`
ShortDevelopment May 4, 2024
8c4eddf
Add call tests + Split into multiple files
ShortDevelopment May 7, 2024
f48dc1b
Treat `eval?.()` as indirect `eval`
ShortDevelopment May 10, 2024
05790ae
Started optional-deletion
ShortDevelopment May 28, 2024
3b5d8d5
Merge branch 'master' into feat-optional-chaining
ShortDevelopment Jul 4, 2024
a18431e
Test optional-call in eval
ShortDevelopment Jul 6, 2024
d96b76b
Ensure `isUsed` is set if `MustProduceValue`
ShortDevelopment Jul 6, 2024
0abf9c2
Copy `isUsed` for robustness
ShortDevelopment Jul 6, 2024
a2709e7
Test for opt-call of root function
ShortDevelopment Jul 6, 2024
8210f62
Async tests
ShortDevelopment Jul 6, 2024
629cb8a
Add test for indirect eval
ShortDevelopment Oct 13, 2024
55dea9f
Add tests for jit
ShortDevelopment Oct 13, 2024
2aaa1c9
Test opt-chain arguments
ShortDevelopment Oct 13, 2024
835a4fa
Copy `isNullPropagating` in `CopyPnode`
ShortDevelopment Oct 13, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 46 additions & 27 deletions lib/Runtime/ByteCode/ByteCodeEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "Language/AsmJs.h"
#include "ConfigFlagsList.h"

void Emit(ParseNode *pnode, ByteCodeGenerator *byteCodeGenerator, FuncInfo *funcInfo, BOOL fReturnValue, bool isConstructorCall = false, bool isTopLevel = false);
void EmitReference(ParseNode *pnode, ByteCodeGenerator *byteCodeGenerator, FuncInfo *funcInfo);
void EmitAssignment(ParseNode *asgnNode, ParseNode *lhs, Js::RegSlot rhsLocation, ByteCodeGenerator *byteCodeGenerator, FuncInfo *funcInfo);
void EmitLoad(ParseNode *rhs, ByteCodeGenerator *byteCodeGenerator, FuncInfo *funcInfo);
Expand All @@ -26,6 +27,7 @@ void VisitClearTmpRegs(ParseNode * pnode, ByteCodeGenerator * byteCodeGenerator,
///
/// It should be called on every <c>?.</c> location.
/// A call to this function is only valid from a node-emission inside a `knopOptChain` node.
/// See EmitOptionalChain.
/// </summary>
static void EmitNullPropagation(Js::RegSlot targetObjectSlot, ByteCodeGenerator *byteCodeGenerator, FuncInfo *funcInfo, bool isNullPropagating) {
if (!isNullPropagating)
Expand All @@ -41,6 +43,39 @@ static void EmitNullPropagation(Js::RegSlot targetObjectSlot, ByteCodeGenerator
);
}

/// <summary>
/// The whole optional-chain expression will be wrapped in a UniNode with `knopOptChain`.
/// Use this function to emit the whole expression.
/// </summary>
template <class TEmitProc>
static void EmitOptionalChainWrapper(ParseNodeUni *pnodeOptChain, ByteCodeGenerator *byteCodeGenerator, FuncInfo *funcInfo, TEmitProc emitChainContent) {
ShortDevelopment marked this conversation as resolved.
Show resolved Hide resolved
Assert(knopOptChain == pnodeOptChain->nop);

Js::ByteCodeLabel previousSkipLabel = funcInfo->currentOptionalChainSkipLabel;

// Create a label that can skip the whole chain and store it in `funcInfo`
Js::ByteCodeLabel skipLabel = byteCodeGenerator->Writer()->DefineLabel();
funcInfo->currentOptionalChainSkipLabel = skipLabel;

// Acquire slot for the result value
// Prefill it with `undefined` (Fallback for short-circuiting)
Js::RegSlot resultSlot = funcInfo->AcquireLoc(pnodeOptChain);
byteCodeGenerator->Writer()->Reg1(Js::OpCode::LdUndef, resultSlot);

// Copy values from wrapper to inner expression
ParseNodePtr innerNode = pnodeOptChain->pnode1;
innerNode->isUsed = pnodeOptChain->isUsed;
innerNode->location = pnodeOptChain->location;
ShortDevelopment marked this conversation as resolved.
Show resolved Hide resolved

// emit chain expression
// Every `?.` node will call `EmitNullPropagation`
// `EmitNullPropagation` short-circuits to `skipLabel` in case of a nullish value
emitChainContent(innerNode);

byteCodeGenerator->Writer()->MarkLabel(skipLabel);
funcInfo->currentOptionalChainSkipLabel = previousSkipLabel;
}

bool CallTargetIsArray(ParseNode *pnode)
{
return pnode->nop == knopName && pnode->AsParseNodeName()->PropertyIdFromNameNode() == Js::PropertyIds::Array;
Expand Down Expand Up @@ -272,7 +307,6 @@ bool IsArguments(ParseNode *pnode)
}

bool ApplyEnclosesArgs(ParseNode* fncDecl, ByteCodeGenerator* byteCodeGenerator);
void Emit(ParseNode* pnode, ByteCodeGenerator* byteCodeGenerator, FuncInfo* funcInfo, BOOL fReturnValue, bool isConstructorCall = false, bool isTopLevel = false);
void EmitBinaryOpnds(ParseNode* pnode1, ParseNode* pnode2, ByteCodeGenerator* byteCodeGenerator, FuncInfo* funcInfo, Js::RegSlot computedPropertyLocation = Js::Constants::NoRegister, bool isNullPropagating = false);
bool IsExpressionStatement(ParseNode* stmt, const Js::ScriptContext *const scriptContext);
void EmitInvoke(Js::RegSlot location, Js::RegSlot callObjLocation, Js::PropertyId propertyId, ByteCodeGenerator* byteCodeGenerator, FuncInfo* funcInfo);
Expand Down Expand Up @@ -8078,6 +8112,12 @@ void EmitCallTarget(

switch (pnodeTarget->nop)
{
case knopOptChain: {
EmitOptionalChainWrapper(pnodeTarget->AsParseNodeUni(), byteCodeGenerator, funcInfo, [&](ParseNodePtr innerNode) {
EmitCallTarget(innerNode, fSideEffectArgs, thisLocation, releaseThisLocation, callObjLocation, byteCodeGenerator, funcInfo, callApplyCallSiteId);
});
break;
}
case knopDot:
{
ParseNodeBin * pnodeBinTarget = pnodeTarget->AsParseNodeBin();
Expand Down Expand Up @@ -11632,34 +11672,13 @@ void Emit(ParseNode* pnode, ByteCodeGenerator* byteCodeGenerator, FuncInfo* func
ENDSTATEMENET_IFTOPLEVEL(isTopLevel, pnode);
break;
}
// The whole optional-chain expression will be wrapped in a UniNode with `knopOptChain`.
case knopOptChain: {
Js::ByteCodeLabel previousSkipLabel = funcInfo->currentOptionalChainSkipLabel;

// Create a label that can skip the whole chain and store in `funcInfo`
Js::ByteCodeLabel skipLabel = byteCodeGenerator->Writer()->DefineLabel();
funcInfo->currentOptionalChainSkipLabel = skipLabel;

// Acquire slot for the result value
// Prefill it with `undefined` (Fallback for short-circuiting)
Js::RegSlot resultSlot = funcInfo->AcquireLoc(pnode);
byteCodeGenerator->Writer()->Reg1(Js::OpCode::LdUndef, resultSlot);

// emit chain expression
// Every `?.` node will call `EmitNullPropagation`
// `EmitNullPropagation` short-circuits to `skipLabel` in case of a nullish value
ParseNodePtr innerNode = pnode->AsParseNodeUni()->pnode1;
Emit(innerNode, byteCodeGenerator, funcInfo, false);

// Copy the expression result
// Only reached if we did not short-circuit
byteCodeGenerator->Writer()->Reg2(Js::OpCode::Ld_A, resultSlot, innerNode->location);
funcInfo->ReleaseLoc(innerNode);

byteCodeGenerator->Writer()->MarkLabel(skipLabel);
funcInfo->currentOptionalChainSkipLabel = previousSkipLabel;
case knopOptChain:
EmitOptionalChainWrapper(pnode->AsParseNodeUni(), byteCodeGenerator, funcInfo, [&](ParseNodePtr innerNode) {
Emit(innerNode, byteCodeGenerator, funcInfo, false);
});
break;
}

// this is MemberExpression as rvalue
case knopDot:
{
Expand Down
Loading