Skip to content

Commit 26c82fa

Browse files
committed
Implement Object.property.hasOwnProperty
JerryScript-DCO-1.0-Signed-off-by: Peter Gal [email protected]
1 parent b8e4328 commit 26c82fa

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed

jerry-core/ecma/builtin-objects/ecma-builtin-object-prototype.cpp

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,45 @@ static ecma_completion_value_t
173173
ecma_builtin_object_prototype_object_has_own_property (ecma_value_t this_arg, /**< this argument */
174174
ecma_value_t arg) /**< first argument */
175175
{
176-
ECMA_BUILTIN_CP_UNIMPLEMENTED (this_arg, arg);
176+
/* 1. ToString(arg) */
177+
ecma_completion_value_t property_name_ret = ecma_op_to_string (arg);
178+
if (!ecma_is_completion_value_normal (property_name_ret))
179+
{
180+
return property_name_ret;
181+
}
182+
183+
ecma_string_t *property_name_string_p = ecma_get_string_from_completion_value (property_name_ret);
184+
185+
/* 2. ToObject(this) */
186+
ecma_completion_value_t obj_this = ecma_op_to_object (this_arg);
187+
if (!ecma_is_completion_value_normal (obj_this))
188+
{
189+
ecma_free_completion_value (property_name_ret);
190+
return obj_this;
191+
}
192+
193+
ecma_object_t *obj_p = ecma_get_object_from_completion_value (obj_this);
194+
195+
/* 3. GetOwnProperty call */
196+
ecma_property_t *property_p = ecma_op_object_get_own_property (obj_p, property_name_string_p);
197+
198+
ecma_free_completion_value (property_name_ret);
199+
ecma_free_completion_value (obj_this);
200+
201+
if (property_p == NULL)
202+
{
203+
return ecma_make_simple_completion_value (ECMA_SIMPLE_VALUE_FALSE);
204+
}
205+
206+
JERRY_ASSERT (property_p->type == ECMA_PROPERTY_NAMEDDATA);
207+
ecma_value_t property_value = ecma_get_named_data_property_value (property_p);
208+
209+
if (ecma_is_value_undefined (property_value))
210+
{
211+
return ecma_make_simple_completion_value (ECMA_SIMPLE_VALUE_FALSE);
212+
}
213+
214+
return ecma_make_simple_completion_value (ECMA_SIMPLE_VALUE_TRUE);
177215
} /* ecma_builtin_object_prototype_object_has_own_property */
178216

179217
/**
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
var obj1 = {};
3+
obj1.prop = "hi";
4+
5+
assert (obj1.hasOwnProperty('prop') === true);
6+
assert (obj1.hasOwnProperty('NO_PROP') === false);
7+
8+
9+
// Test if the toString fails.
10+
try {
11+
obj1.hasOwnProperty({toString: function() { throw new ReferenceError ("foo"); }});
12+
13+
assert (false);
14+
} catch (e) {
15+
assert (e.message === "foo");
16+
assert (e instanceof ReferenceError);
17+
}
18+
19+
// Test if the toObject fails.
20+
21+
var obj2;
22+
try {
23+
print(obj2.hasOwnProperty("fail"));
24+
25+
assert (false);
26+
} catch (e) {
27+
assert (e instanceof TypeError);
28+
}

0 commit comments

Comments
 (0)