Skip to content

Commit c9eb597

Browse files
committed
Use the right target for Object's internal array get item
When a built in is trying to fill in index property from the prototype, we need to pass in the original object to get the value of the element if the prototype is an object with and internal es5 array, so that accessors will get the right target instead of the internal es5 array.
1 parent 097edcd commit c9eb597

File tree

4 files changed

+23
-57
lines changed

4 files changed

+23
-57
lines changed

lib/Runtime/Library/JavascriptArray.cpp

Lines changed: 1 addition & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -10055,59 +10055,6 @@ namespace Js
1005510055
}
1005610056
#endif
1005710057

10058-
template <typename Fn>
10059-
void JavascriptArray::ForEachOwnArrayIndexOfObject(RecyclableObject* obj, uint32 startIndex, uint32 limitIndex, Fn fn)
10060-
{
10061-
Assert(DynamicObject::IsAnyArray(obj) || JavascriptOperators::IsObject(obj));
10062-
10063-
JavascriptArray* arr = nullptr;
10064-
if (DynamicObject::IsAnyArray(obj))
10065-
{
10066-
arr = JavascriptArray::FromAnyArray(obj);
10067-
}
10068-
else if (DynamicType::Is(obj->GetTypeId()))
10069-
{
10070-
DynamicObject* dynobj = DynamicObject::FromVar(obj);
10071-
arr = dynobj->GetObjectArray();
10072-
}
10073-
10074-
if (arr != nullptr)
10075-
{
10076-
if (JavascriptArray::Is(arr))
10077-
{
10078-
ArrayElementEnumerator e(arr, startIndex, limitIndex);
10079-
10080-
while(e.MoveNext<Var>())
10081-
{
10082-
fn(e.GetIndex(), e.GetItem<Var>());
10083-
}
10084-
}
10085-
else
10086-
{
10087-
ScriptContext* scriptContext = obj->GetScriptContext();
10088-
10089-
Assert(ES5Array::Is(arr));
10090-
10091-
ES5Array* es5Array = ES5Array::FromVar(arr);
10092-
ES5ArrayIndexEnumerator<true> e(es5Array);
10093-
10094-
while (e.MoveNext())
10095-
{
10096-
uint32 index = e.GetIndex();
10097-
10098-
if (index < startIndex) continue;
10099-
else if (index >= limitIndex) break;
10100-
10101-
Var value = nullptr;
10102-
if (JavascriptOperators::GetOwnItem(es5Array, index, &value, scriptContext))
10103-
{
10104-
fn(index, value);
10105-
}
10106-
}
10107-
}
10108-
}
10109-
}
10110-
1011110058
template <typename T, typename Fn>
1011210059
void JavascriptArray::ForEachOwnMissingArrayIndexOfObject(JavascriptArray *baseArray, JavascriptArray *destArray, RecyclableObject* obj, uint32 startIndex, uint32 limitIndex, T destIndex, Fn fn)
1011310060
{
@@ -10166,7 +10113,7 @@ namespace Js
1016610113
if (destArray == nullptr || !destArray->DirectGetItemAt(n, &oldValue))
1016710114
{
1016810115
Var value = nullptr;
10169-
if (JavascriptOperators::GetOwnItem(es5Array, index, &value, scriptContext))
10116+
if (JavascriptOperators::GetOwnItem(obj, index, &value, scriptContext))
1017010117
{
1017110118
fn(index, value);
1017210119
}

lib/Runtime/Library/JavascriptArray.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -572,9 +572,6 @@ namespace Js
572572
static int __cdecl CompareElements(void* context, const void* elem1, const void* elem2);
573573
void SortElements(Element* elements, uint32 left, uint32 right);
574574

575-
template <typename Fn>
576-
static void ForEachOwnArrayIndexOfObject(RecyclableObject* obj, uint32 startIndex, uint32 limitIndex, Fn fn);
577-
578575
template <typename T, typename Fn>
579576
static void ForEachOwnMissingArrayIndexOfObject(JavascriptArray *baseArr, JavascriptArray *destArray, RecyclableObject* obj, uint32 startIndex, uint32 limitIndex, T destIndex, Fn fn);
580577

test/es5/es5array_objproto_builtin.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//-------------------------------------------------------------------------------------------------------
2+
// Copyright (C) Microsoft. All rights reserved.
3+
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
4+
//-------------------------------------------------------------------------------------------------------
5+
6+
var ary = Array(1);
7+
ary.prop = "Got array property. Failed";
8+
Object.prototype.prop = "pass";
9+
Array.prototype.prop = "Got array prototype. Failed";
10+
Object.defineProperty(Object.prototype, 0, {
11+
get: function () {
12+
print(this.prop);
13+
return 3;
14+
}
15+
});
16+
ary.slice();
17+

test/es5/rlexe.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,11 @@
284284
<baseline>es5array_objproto.baseline</baseline>
285285
</default>
286286
</test>
287+
<test>
288+
<default>
289+
<files>es5array_objproto_builtin.js</files>
290+
</default>
291+
</test>
287292
<test>
288293
<default>
289294
<files>es5array_arrayproto.js</files>

0 commit comments

Comments
 (0)