@@ -39,7 +39,7 @@ static void EmitNullPropagation(Js::RegSlot targetObjectSlot, ByteCodeGenerator
3939 // if (targetObject == null) goto skipLabel;
4040 byteCodeGenerator->Writer ()->BrReg2 (
4141 Js::OpCode::BrEq_A, funcInfo->currentOptionalChainSkipLabel ,
42- targetObjectSlot, funcInfo->nullConstantRegister
42+ targetObjectSlot, funcInfo->undefinedConstantRegister
4343 );
4444}
4545
@@ -57,22 +57,30 @@ static void EmitOptionalChainWrapper(ParseNodeUni *pnodeOptChain, ByteCodeGenera
5757 Js::ByteCodeLabel skipLabel = byteCodeGenerator->Writer ()->DefineLabel ();
5858 funcInfo->currentOptionalChainSkipLabel = skipLabel;
5959
60- // Acquire slot for the result value
61- // Prefill it with `undefined` (Fallback for short-circuiting)
62- Js::RegSlot resultSlot = funcInfo->AcquireLoc (pnodeOptChain);
63- byteCodeGenerator->Writer ()->Reg1 (Js::OpCode::LdUndef, resultSlot);
64-
6560 // Copy values from wrapper to inner expression
6661 ParseNodePtr innerNode = pnodeOptChain->pnode1 ;
6762 innerNode->isUsed = pnodeOptChain->isUsed ;
68- innerNode->location = pnodeOptChain->location ;
6963
7064 // emit chain expression
7165 // Every `?.` node will call `EmitNullPropagation`
7266 // `EmitNullPropagation` short-circuits to `skipLabel` in case of a nullish value
7367 emitChainContent (innerNode);
68+ funcInfo->ReleaseLoc (innerNode);
69+
70+ // Acquire slot for the result value
71+ Js::RegSlot resultSlot = funcInfo->AcquireLoc (pnodeOptChain);
72+ // Copy chain result
73+ byteCodeGenerator->Writer ()->Reg2 (Js::OpCode::Ld_A_ReuseLoc, resultSlot, innerNode->location );
74+
75+ // Skip short-circuiting logic
76+ Js::ByteCodeLabel doneLabel = byteCodeGenerator->Writer ()->DefineLabel ();
77+ byteCodeGenerator->Writer ()->Br (doneLabel);
7478
79+ // Set `undefined` on short-circuiting
7580 byteCodeGenerator->Writer ()->MarkLabel (skipLabel);
81+ byteCodeGenerator->Writer ()->Reg2 (Js::OpCode::Ld_A_ReuseLoc, resultSlot, funcInfo->undefinedConstantRegister );
82+
83+ byteCodeGenerator->Writer ()->MarkLabel (doneLabel);
7684 funcInfo->currentOptionalChainSkipLabel = previousSkipLabel;
7785}
7886
0 commit comments