Skip to content

Commit 5c2e159

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

File tree

2 files changed

+97
-1
lines changed

2 files changed

+97
-1
lines changed

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

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,50 @@ 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+
ecma_completion_value_t return_value = ecma_make_empty_completion_value ();
177+
178+
/* 1. */
179+
ECMA_TRY_CATCH (to_string_val,
180+
ecma_op_to_string (arg),
181+
return_value);
182+
183+
/* 2. */
184+
ECMA_TRY_CATCH (obj_val,
185+
ecma_op_to_object (this_arg),
186+
return_value);
187+
188+
ecma_string_t *property_name_string_p = ecma_get_string_from_value (to_string_val);
189+
190+
ecma_object_t *obj_p = ecma_get_object_from_value (obj_val);
191+
192+
/* 3. */
193+
ecma_property_t *property_p = ecma_op_object_get_own_property (obj_p, property_name_string_p);
194+
195+
if (property_p == NULL)
196+
{
197+
return_value = ecma_make_simple_completion_value (ECMA_SIMPLE_VALUE_FALSE);
198+
}
199+
else if (property_p->type == ECMA_PROPERTY_NAMEDDATA)
200+
{
201+
ecma_value_t property_value = ecma_get_named_data_property_value (property_p);
202+
203+
return_value = ecma_make_simple_completion_value (!ecma_is_value_undefined (property_value)
204+
? ECMA_SIMPLE_VALUE_TRUE
205+
: ECMA_SIMPLE_VALUE_FALSE);
206+
}
207+
else if (property_p->type == ECMA_PROPERTY_NAMEDACCESSOR)
208+
{
209+
ecma_object_t *property_object = ecma_get_named_accessor_property_getter (property_p);
210+
211+
return_value = ecma_make_simple_completion_value (property_object != NULL
212+
? ECMA_SIMPLE_VALUE_TRUE
213+
: ECMA_SIMPLE_VALUE_FALSE);
214+
}
215+
ECMA_FINALIZE (obj_val)
216+
217+
ECMA_FINALIZE (to_string_val)
218+
219+
return return_value;
177220
} /* ecma_builtin_object_prototype_object_has_own_property */
178221

179222
/**
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright 2015 Samsung Electronics Co., Ltd.
2+
// Copyright 2015 University of Szeged.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
var obj1 = {};
17+
obj1.prop = "hi";
18+
19+
assert (obj1.hasOwnProperty('prop') === true);
20+
assert (obj1.hasOwnProperty('NO_PROP') === false);
21+
22+
23+
// Test if the toString fails.
24+
try {
25+
obj1.hasOwnProperty({toString: function() { throw new ReferenceError ("foo"); }});
26+
27+
assert (false);
28+
} catch (e) {
29+
assert (e.message === "foo");
30+
assert (e instanceof ReferenceError);
31+
}
32+
33+
// Test if the toObject fails.
34+
35+
var obj2;
36+
try {
37+
obj2.hasOwnProperty("fail");
38+
39+
assert (false);
40+
} catch (e) {
41+
assert (e instanceof TypeError);
42+
}
43+
44+
var obj_undef;
45+
var obj3 = {};
46+
Object.defineProperty(obj3, 'Test', { 'get' : function () {throw new ReferenceError ("foo"); } });
47+
assert (obj3.hasOwnProperty("Test") === true);
48+
49+
Object.defineProperty(obj3, 'Test2', { 'get' : function () { return 0/0; } });
50+
assert (obj3.hasOwnProperty("Test2") === true);
51+
52+
Object.defineProperty(obj3, 'Test4', { 'get' : function () { return obj_undef; } });
53+
assert (obj3.hasOwnProperty("Test4") === true);

0 commit comments

Comments
 (0)