Skip to content

Commit 7febf0a

Browse files
committed
[JSC] DFG Array.push with 0-argument should throw an error when length is not writable
https://bugs.webkit.org/show_bug.cgi?id=328086 rdar://190437846 Reviewed by Keith Miller. Array.prototype.push with no arguments still performs Set(O, "length", len, true), which throws a TypeError when length is not writable. DFG fixup turned array.push() into GetArrayLength for any array mode that supports self length, including ArrayStorage, which is the only kind of array whose length can be non-writable, so the TypeError was lost in DFG and FTL code. This patch applies this optimization only for Int32, Double, and Contiguous arrays as their length is not frozen. Test: JSTests/stress/array-push-no-arguments-throws-on-read-only-length.js * JSTests/stress/array-push-no-arguments-throws-on-read-only-length.js: Added. (push): (make): (i.catch): * Source/JavaScriptCore/dfg/DFGArrayMode.h: (JSC::DFG::arrayModeImpliesWritableLength): * Source/JavaScriptCore/dfg/DFGByteCodeParser.cpp: (JSC::DFG::ByteCodeParser::handleIntrinsicCall): * Source/JavaScriptCore/dfg/DFGFixupPhase.cpp: (JSC::DFG::FixupPhase::fixupNode): Canonical link: https://commits.webkit.org/324204@main
1 parent 0083ac4 commit 7febf0a

4 files changed

Lines changed: 43 additions & 1 deletion

File tree

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// array.push() with no arguments still sets "length", which throws when length is not writable.
2+
3+
function push(array) { return array.push(); }
4+
noInline(push);
5+
6+
function make(i) {
7+
if (i % 3 == 0)
8+
return Object.freeze(["a", "b"]);
9+
if (i % 3 == 1) {
10+
const array = [1, 2];
11+
Object.defineProperty(array, "length", { writable: false });
12+
return array;
13+
}
14+
return ["x", "y"];
15+
}
16+
17+
for (let i = 0; i < testLoopCount; ++i) {
18+
let result;
19+
try {
20+
result = push(make(i));
21+
} catch (e) {
22+
result = e.constructor.name;
23+
}
24+
const expected = i % 3 == 2 ? 2 : "TypeError";
25+
if (result !== expected)
26+
throw new Error("iteration " + i + ": expected " + expected + " but got " + result);
27+
}

‎Source/JavaScriptCore/dfg/DFGArrayMode.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,12 @@ Array::Type LEGACY_NODELETE refineTypedArrayType(Array::Type, TypedArrayType);
115115

116116
bool LEGACY_NODELETE permitsBoundsCheckLowering(Array::Type);
117117

118+
// Only arrays with ArrayStorage can have a non-writable length.
119+
inline bool arrayModeImpliesWritableLength(Array::Type type)
120+
{
121+
return type == Array::Int32 || type == Array::Double || type == Array::Contiguous;
122+
}
123+
118124
class ArrayMode {
119125
public:
120126
ArrayMode()

‎Source/JavaScriptCore/dfg/DFGByteCodeParser.cpp‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3179,6 +3179,11 @@ auto ByteCodeParser::handleIntrinsicCall(Node* callee, Operand resultOperand, Ca
31793179
if (!arrayMode.isJSArray())
31803180
return CallOptimizationResult::DidNothing;
31813181

3182+
// push() with no arguments still sets length, which throws when length is not writable.
3183+
// Only ArrayStorage arrays can have such a length, so leave those to the generic call.
3184+
if (argumentCountIncludingThis == 1 && !arrayModeImpliesWritableLength(arrayMode.type()))
3185+
return CallOptimizationResult::DidNothing;
3186+
31823187
insertChecks();
31833188

31843189
addVarArgChild(nullptr); // For storage.

‎Source/JavaScriptCore/dfg/DFGFixupPhase.cpp‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1733,8 +1733,12 @@ class FixupPhase : public Phase {
17331733
blessArrayOperation(arrayEdge, Edge(), storageEdge);
17341734
fixEdge<KnownCellUse>(arrayEdge);
17351735

1736-
// Convert `array.push()` to GetArrayLength.
1736+
// Convert `array.push()` to GetArrayLength. It must still throw when length is not
1737+
// writable, which only ArrayStorage arrays can be, so those exit and let the baseline
1738+
// code do the push.
17371739
if (!elementCount && node->arrayMode().supportsSelfLength()) {
1740+
if (!arrayModeImpliesWritableLength(node->arrayMode().type()))
1741+
m_insertionSet.insertNode(m_indexInBlock, SpecNone, ForceOSRExit, node->origin);
17381742
node->setOpAndDefaultFlags(GetArrayLength);
17391743
node->child1() = arrayEdge;
17401744
node->child2() = storageEdge;

0 commit comments

Comments
 (0)