Skip to content

Commit 39f9727

Browse files
committed
[MERGE #5989 @akroshg] Exposing HasOwnItem API.
Merge pull request #5989 from akroshg:hasownitem This will tell if the provided index is in the object.
2 parents e149067 + 199ce62 commit 39f9727

File tree

4 files changed

+77
-0
lines changed

4 files changed

+77
-0
lines changed

bin/NativeTests/JsRTApiTest.cpp

+38
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,44 @@ namespace JsRTApiTest
252252
JsRTApiTest::RunWithAttributes(JsRTApiTest::DeleteObjectIndexedPropertyBug);
253253
}
254254

255+
void HasOwnItemTest(JsRuntimeAttributes attributes, JsRuntimeHandle runtime)
256+
{
257+
JsValueRef object;
258+
REQUIRE(JsRunScript(_u("var obj = {a: [1,2], \"1\": 111}; obj.__proto__[3] = 333; obj;"), JS_SOURCE_CONTEXT_NONE, _u(""), &object) == JsNoError);
259+
260+
JsPropertyIdRef idRef = JS_INVALID_REFERENCE;
261+
JsValueRef result = JS_INVALID_REFERENCE;
262+
// delete property "a" triggers PathTypeHandler -> SimpleDictionaryTypeHandler
263+
REQUIRE(JsGetPropertyIdFromName(_u("a"), &idRef) == JsNoError);
264+
REQUIRE(JsGetProperty(object, idRef, &result) == JsNoError);
265+
bool hasOwnItem = false;
266+
REQUIRE(JsHasOwnItem(result, 0, &hasOwnItem) == JsNoError);
267+
CHECK(hasOwnItem);
268+
269+
REQUIRE(JsHasOwnItem(result, 1, &hasOwnItem) == JsNoError);
270+
CHECK(hasOwnItem);
271+
272+
REQUIRE(JsHasOwnItem(result, 2, &hasOwnItem) == JsNoError);
273+
CHECK(!hasOwnItem); // It does not have item on index 2 - so we should not be able to find that.
274+
275+
REQUIRE(JsHasOwnItem(object, 1, &hasOwnItem) == JsNoError);
276+
CHECK(hasOwnItem);
277+
278+
REQUIRE(JsHasOwnItem(object, 3, &hasOwnItem) == JsNoError);
279+
CHECK(!hasOwnItem); // index 3 is on prototype.
280+
281+
bool has = false;
282+
JsValueRef indexRef = JS_INVALID_REFERENCE;
283+
REQUIRE(JsIntToNumber(3, &indexRef) == JsNoError);
284+
REQUIRE(JsHasIndexedProperty(object, indexRef, &has) == JsNoError);
285+
CHECK(has); // index 3 is prototype - so it should be able to find that.
286+
}
287+
288+
TEST_CASE("ApiTest_HasOwnItemTest", "[ApiTest]")
289+
{
290+
JsRTApiTest::RunWithAttributes(JsRTApiTest::HasOwnItemTest);
291+
}
292+
255293
void CALLBACK ExternalObjectFinalizeCallback(void *data)
256294
{
257295
CHECK(data == (void *)0xdeadbeef);

lib/Jsrt/ChakraCore.h

+19
Original file line numberDiff line numberDiff line change
@@ -961,6 +961,25 @@ CHAKRA_API
961961
_Out_ bool *hasOwnProperty);
962962

963963
/// <summary>
964+
/// Determines whether an object has a non-inherited property.
965+
/// </summary>
966+
/// <remarks>
967+
/// Requires an active script context.
968+
/// </remarks>
969+
/// <param name="object">The object that may contain the item.</param>
970+
/// <param name="index">The index to find.</param>
971+
/// <param name="hasOwnProperty">Whether the object has the non-inherited
972+
/// property.</param> <returns>
973+
/// The code <c>JsNoError</c> if the operation succeeded, a failure code
974+
/// otherwise.
975+
/// </returns>
976+
CHAKRA_API
977+
JsHasOwnItem(
978+
_In_ JsValueRef object,
979+
_In_ uint32_t index,
980+
_Out_ bool* hasOwnItem);
981+
982+
/// <summary>
964983
/// Write JS string value into char string buffer without a null terminator
965984
/// </summary>
966985
/// <remarks>

lib/Jsrt/Core/JsrtCore.cpp

+19
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,25 @@ JsExternalizeArrayBuffer(
516516
});
517517
}
518518

519+
CHAKRA_API
520+
JsHasOwnItem(_In_ JsValueRef object,
521+
_In_ uint32_t index,
522+
_Out_ bool* hasOwnItem)
523+
{
524+
return ContextAPIWrapper<true>(
525+
[&](Js::ScriptContext* scriptContext,
526+
TTDRecorder& _actionEntryPopper) -> JsErrorCode {
527+
528+
VALIDATE_INCOMING_OBJECT(object, scriptContext);
529+
PARAM_NOT_NULL(hasOwnItem);
530+
531+
*hasOwnItem = !!Js::JavascriptOperators::HasOwnItem(
532+
Js::VarTo<Js::RecyclableObject>(object), index);
533+
534+
return JsNoError;
535+
});
536+
}
537+
519538
CHAKRA_API
520539
JsDetachArrayBuffer(
521540
_In_ JsValueRef arrayBuffer)

lib/Jsrt/JsrtCommonExports.inc

+1
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@
135135
JsGetPropertyIdSymbolIterator
136136
JsGetWeakReferenceValue
137137
JsHasOwnProperty
138+
JsHasOwnItem
138139
JsIsConstructor
139140
JsObjectDefineProperty
140141
JsObjectDefinePropertyFull

0 commit comments

Comments
 (0)