Skip to content

Commit b0d4acd

Browse files
Zsolt Borbélygalpeter
authored andcommitted
Implement Object.prototype.propertyIsEnumerable().
JerryScript-DCO-1.0-Signed-off-by: Zsolt Borbély zsborbely.u-szeged@partner.samsung.com
1 parent e5e876c commit b0d4acd

File tree

2 files changed

+137
-1
lines changed

2 files changed

+137
-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
@@ -1,4 +1,5 @@
11
/* Copyright 2014-2015 Samsung Electronics Co., Ltd.
2+
* Copyright 2015 University of Szeged.
23
*
34
* Licensed under the Apache License, Version 2.0 (the "License");
45
* you may not use this file except in compliance with the License.
@@ -226,7 +227,44 @@ static ecma_completion_value_t
226227
ecma_builtin_object_prototype_object_property_is_enumerable (ecma_value_t this_arg, /**< this argument */
227228
ecma_value_t arg) /**< routine's first argument */
228229
{
229-
ECMA_BUILTIN_CP_UNIMPLEMENTED (this_arg, arg);
230+
ecma_completion_value_t return_value = ecma_make_empty_completion_value ();
231+
232+
/* 1. */
233+
ECMA_TRY_CATCH (to_string_val,
234+
ecma_op_to_string (arg),
235+
return_value);
236+
237+
/* 2. */
238+
ECMA_TRY_CATCH (obj_val,
239+
ecma_op_to_object (this_arg),
240+
return_value);
241+
242+
ecma_string_t *property_name_string_p = ecma_get_string_from_value (to_string_val);
243+
244+
ecma_object_t *obj_p = ecma_get_object_from_value (obj_val);
245+
246+
/* 3. */
247+
ecma_property_t *property_p = ecma_op_object_get_own_property (obj_p, property_name_string_p);
248+
249+
/* 4. */
250+
if (property_p != NULL)
251+
{
252+
bool is_enumerable = ecma_is_property_enumerable (property_p);
253+
254+
return_value = ecma_make_simple_completion_value (is_enumerable
255+
? ECMA_SIMPLE_VALUE_TRUE
256+
: ECMA_SIMPLE_VALUE_FALSE);
257+
}
258+
else
259+
{
260+
return_value = ecma_make_simple_completion_value (ECMA_SIMPLE_VALUE_FALSE);
261+
}
262+
263+
ECMA_FINALIZE (obj_val);
264+
265+
ECMA_FINALIZE (to_string_val);
266+
267+
return return_value;
230268
} /* ecma_builtin_object_prototype_object_property_is_enumerable */
231269

232270
/**
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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 obj = {};
17+
18+
// Test if the toString fails.
19+
try {
20+
obj.propertyIsEnumerable({ toString: function() { throw new ReferenceError ("foo"); } });
21+
22+
assert (false);
23+
} catch (e) {
24+
assert (e.message === "foo");
25+
assert (e instanceof ReferenceError);
26+
}
27+
28+
// Test if the toObject fails.
29+
var obj1;
30+
try {
31+
obj1.propertyIsEnumerable("fail");
32+
33+
assert (false);
34+
} catch (e) {
35+
assert (e instanceof TypeError);
36+
}
37+
38+
var array = [];
39+
obj.prop = "bar";
40+
array[0] = "bar";
41+
42+
assert (obj.propertyIsEnumerable('prop') === true);
43+
assert (array.propertyIsEnumerable(0) === true);
44+
45+
assert (obj.propertyIsEnumerable('length') === false);
46+
assert (array.propertyIsEnumerable('length') === false);
47+
assert (Math.propertyIsEnumerable('random') === false);
48+
49+
// Creating a new property
50+
Object.defineProperty(obj, 'prop1', { value: 'foo', enumerable: true });
51+
Object.defineProperty(obj, 'prop2', { value: 'foo', enumerable: false });
52+
Object.defineProperty(obj, 'prop3', { value: 'foo' });
53+
assert (obj.propertyIsEnumerable('prop1') === true);
54+
assert (obj.propertyIsEnumerable('prop2') === false);
55+
assert (obj.propertyIsEnumerable('prop3') === false);
56+
57+
Object.defineProperty(array, 'prop1', { value: 'foo', enumerable: true });
58+
Object.defineProperty(array, 'prop2', { value: 'foo', enumerable: false });
59+
Object.defineProperty(array, 'prop3', { value: 'foo' });
60+
assert (array.propertyIsEnumerable('prop1') === true);
61+
assert (array.propertyIsEnumerable('prop2') === false);
62+
assert (array.propertyIsEnumerable('prop3') === false);
63+
64+
// Modify an existing one
65+
Object.defineProperty(obj, 'prop', { value: 'foo', enumerable: false });
66+
assert (obj.propertyIsEnumerable('prop') === false);
67+
Object.defineProperty(obj, 'prop', { value: 'foo', enumerable: true });
68+
assert (obj.propertyIsEnumerable('prop') === true);
69+
70+
Object.defineProperty(array, 0, { value: 'foo', enumerable: false });
71+
assert (array.propertyIsEnumerable(0) === false);
72+
Object.defineProperty(array, 0, { value: 'foo', enumerable: true });
73+
assert (array.propertyIsEnumerable(0) === true);
74+
75+
// Enumerability of inherited properties
76+
function construct1()
77+
{
78+
this.prop1 = 'foo';
79+
}
80+
81+
function construct2()
82+
{
83+
this.prop2 = 'foo';
84+
}
85+
86+
construct2.prototype = new construct1;
87+
construct2.prototype.constructor = construct2;
88+
89+
var obj2 = new construct2();
90+
obj2.prop3 = 'foo';
91+
92+
assert (obj2.propertyIsEnumerable('prop3') === true);
93+
assert (obj2.propertyIsEnumerable('prop2') === true);
94+
assert (obj2.propertyIsEnumerable('prop1') === false);
95+
96+
obj2.prop1 = 'foo';
97+
98+
assert (obj2.propertyIsEnumerable('prop1') === true);

0 commit comments

Comments
 (0)