Skip to content

Commit

Permalink
Fix delete operator result
Browse files Browse the repository at this point in the history
  • Loading branch information
ShortDevelopment committed Dec 27, 2024
1 parent 72aac4a commit 174e7bf
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
10 changes: 7 additions & 3 deletions lib/Runtime/ByteCode/ByteCodeEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ static void EmitNullPropagation(Js::RegSlot targetObjectSlot, ByteCodeGenerator
/// Use this function to emit the whole expression.
/// </summary>
template <class TEmitProc>
static void EmitOptionalChainWrapper(ParseNodeUni *pnodeOptChain, ByteCodeGenerator *byteCodeGenerator, FuncInfo *funcInfo, TEmitProc emitChainContent) {
static void EmitOptionalChainWrapper(ParseNodeUni *pnodeOptChain, ByteCodeGenerator *byteCodeGenerator, FuncInfo *funcInfo, TEmitProc emitChainContent, bool trueOnShortCircuit = false) {
Assert(knopOptChain == pnodeOptChain->nop);

Js::ByteCodeLabel previousSkipLabel = funcInfo->currentOptionalChainSkipLabel;
Expand Down Expand Up @@ -88,7 +88,11 @@ static void EmitOptionalChainWrapper(ParseNodeUni *pnodeOptChain, ByteCodeGenera
Assert(Js::Constants::NoRegister != pnodeOptChain->location);

// Set `undefined` on short-circuiting
byteCodeGenerator->Writer()->Reg2(Js::OpCode::Ld_A_ReuseLoc, pnodeOptChain->location, funcInfo->undefinedConstantRegister);
// The `delete` operator set `trueOnShortCircuit=true` to return `true` instead of `undefined`
if (trueOnShortCircuit)
byteCodeGenerator->Writer()->Reg1(Js::OpCode::LdTrue_ReuseLoc, pnodeOptChain->location);
else
byteCodeGenerator->Writer()->Reg2(Js::OpCode::Ld_A_ReuseLoc, pnodeOptChain->location, funcInfo->undefinedConstantRegister);
byteCodeGenerator->Writer()->MarkLabel(doneLabel);
}

Expand Down Expand Up @@ -11221,7 +11225,7 @@ static void EmitDelete(ParseNode *pnode, ParseNode *pexpr, ByteCodeGenerator *by
case knopOptChain:
EmitOptionalChainWrapper(pexpr->AsParseNodeUni(), byteCodeGenerator, funcInfo, [&](ParseNode *innerNode) {
EmitDelete(innerNode, innerNode, byteCodeGenerator, funcInfo);
});
}, /* trueOnShortCircuit: */ true);
pnode->location = pexpr->location;
break;
case knopName:
Expand Down
5 changes: 5 additions & 0 deletions test/es12/optional-delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ const tests = [
assert.isTrue(delete obj.doesNotExist?.something);
assert.strictEqual(undefined, obj.doesNotExist?.something);

assert.strictEqual(42, obj?.something);
assert.strictEqual(undefined, obj?.something.doesNotExist);
assert.isTrue(delete obj?.something.doesNotExist);
assert.strictEqual(undefined, obj?.something.doesNotExist);

assert.strictEqual(undefined, obj?.doesNotExist);
assert.strictEqual(undefined, obj?.doesNotExist?.something);
assert.isTrue(delete obj?.doesNotExist?.something);
Expand Down

0 comments on commit 174e7bf

Please sign in to comment.