Skip to content
Open
Changes from 1 commit
Commits
Show all changes
37 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
72aac4a
Tests for `delete` operator
ShortDevelopment Dec 27, 2024
174e7bf
Fix `delete` operator result
ShortDevelopment Dec 27, 2024
4b61a6f
Merge branch 'master' into feat-optional-chaining
ShortDevelopment Feb 23, 2025
eaa44b6
Test optional property is only called once
ShortDevelopment Apr 16, 2025
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) {
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;

// 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