mirror of
https://github.com/jerryscript-project/jerryscript.git
synced 2025-12-15 16:29:21 +00:00
Implement Object.prototype.propertyIsEnumerable().
JerryScript-DCO-1.0-Signed-off-by: Zsolt Borbély zsborbely.u-szeged@partner.samsung.com
This commit is contained in:
parent
e5e876cf95
commit
b0d4acd026
@ -1,4 +1,5 @@
|
||||
/* Copyright 2014-2015 Samsung Electronics Co., Ltd.
|
||||
* Copyright 2015 University of Szeged.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@ -226,7 +227,44 @@ static ecma_completion_value_t
|
||||
ecma_builtin_object_prototype_object_property_is_enumerable (ecma_value_t this_arg, /**< this argument */
|
||||
ecma_value_t arg) /**< routine's first argument */
|
||||
{
|
||||
ECMA_BUILTIN_CP_UNIMPLEMENTED (this_arg, arg);
|
||||
ecma_completion_value_t return_value = ecma_make_empty_completion_value ();
|
||||
|
||||
/* 1. */
|
||||
ECMA_TRY_CATCH (to_string_val,
|
||||
ecma_op_to_string (arg),
|
||||
return_value);
|
||||
|
||||
/* 2. */
|
||||
ECMA_TRY_CATCH (obj_val,
|
||||
ecma_op_to_object (this_arg),
|
||||
return_value);
|
||||
|
||||
ecma_string_t *property_name_string_p = ecma_get_string_from_value (to_string_val);
|
||||
|
||||
ecma_object_t *obj_p = ecma_get_object_from_value (obj_val);
|
||||
|
||||
/* 3. */
|
||||
ecma_property_t *property_p = ecma_op_object_get_own_property (obj_p, property_name_string_p);
|
||||
|
||||
/* 4. */
|
||||
if (property_p != NULL)
|
||||
{
|
||||
bool is_enumerable = ecma_is_property_enumerable (property_p);
|
||||
|
||||
return_value = ecma_make_simple_completion_value (is_enumerable
|
||||
? ECMA_SIMPLE_VALUE_TRUE
|
||||
: ECMA_SIMPLE_VALUE_FALSE);
|
||||
}
|
||||
else
|
||||
{
|
||||
return_value = ecma_make_simple_completion_value (ECMA_SIMPLE_VALUE_FALSE);
|
||||
}
|
||||
|
||||
ECMA_FINALIZE (obj_val);
|
||||
|
||||
ECMA_FINALIZE (to_string_val);
|
||||
|
||||
return return_value;
|
||||
} /* ecma_builtin_object_prototype_object_property_is_enumerable */
|
||||
|
||||
/**
|
||||
|
||||
98
tests/jerry/object_prototype_propertyisenumerable.js
Normal file
98
tests/jerry/object_prototype_propertyisenumerable.js
Normal file
@ -0,0 +1,98 @@
|
||||
// Copyright 2015 Samsung Electronics Co., Ltd.
|
||||
// Copyright 2015 University of Szeged.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
var obj = {};
|
||||
|
||||
// Test if the toString fails.
|
||||
try {
|
||||
obj.propertyIsEnumerable({ toString: function() { throw new ReferenceError ("foo"); } });
|
||||
|
||||
assert (false);
|
||||
} catch (e) {
|
||||
assert (e.message === "foo");
|
||||
assert (e instanceof ReferenceError);
|
||||
}
|
||||
|
||||
// Test if the toObject fails.
|
||||
var obj1;
|
||||
try {
|
||||
obj1.propertyIsEnumerable("fail");
|
||||
|
||||
assert (false);
|
||||
} catch (e) {
|
||||
assert (e instanceof TypeError);
|
||||
}
|
||||
|
||||
var array = [];
|
||||
obj.prop = "bar";
|
||||
array[0] = "bar";
|
||||
|
||||
assert (obj.propertyIsEnumerable('prop') === true);
|
||||
assert (array.propertyIsEnumerable(0) === true);
|
||||
|
||||
assert (obj.propertyIsEnumerable('length') === false);
|
||||
assert (array.propertyIsEnumerable('length') === false);
|
||||
assert (Math.propertyIsEnumerable('random') === false);
|
||||
|
||||
// Creating a new property
|
||||
Object.defineProperty(obj, 'prop1', { value: 'foo', enumerable: true });
|
||||
Object.defineProperty(obj, 'prop2', { value: 'foo', enumerable: false });
|
||||
Object.defineProperty(obj, 'prop3', { value: 'foo' });
|
||||
assert (obj.propertyIsEnumerable('prop1') === true);
|
||||
assert (obj.propertyIsEnumerable('prop2') === false);
|
||||
assert (obj.propertyIsEnumerable('prop3') === false);
|
||||
|
||||
Object.defineProperty(array, 'prop1', { value: 'foo', enumerable: true });
|
||||
Object.defineProperty(array, 'prop2', { value: 'foo', enumerable: false });
|
||||
Object.defineProperty(array, 'prop3', { value: 'foo' });
|
||||
assert (array.propertyIsEnumerable('prop1') === true);
|
||||
assert (array.propertyIsEnumerable('prop2') === false);
|
||||
assert (array.propertyIsEnumerable('prop3') === false);
|
||||
|
||||
// Modify an existing one
|
||||
Object.defineProperty(obj, 'prop', { value: 'foo', enumerable: false });
|
||||
assert (obj.propertyIsEnumerable('prop') === false);
|
||||
Object.defineProperty(obj, 'prop', { value: 'foo', enumerable: true });
|
||||
assert (obj.propertyIsEnumerable('prop') === true);
|
||||
|
||||
Object.defineProperty(array, 0, { value: 'foo', enumerable: false });
|
||||
assert (array.propertyIsEnumerable(0) === false);
|
||||
Object.defineProperty(array, 0, { value: 'foo', enumerable: true });
|
||||
assert (array.propertyIsEnumerable(0) === true);
|
||||
|
||||
// Enumerability of inherited properties
|
||||
function construct1()
|
||||
{
|
||||
this.prop1 = 'foo';
|
||||
}
|
||||
|
||||
function construct2()
|
||||
{
|
||||
this.prop2 = 'foo';
|
||||
}
|
||||
|
||||
construct2.prototype = new construct1;
|
||||
construct2.prototype.constructor = construct2;
|
||||
|
||||
var obj2 = new construct2();
|
||||
obj2.prop3 = 'foo';
|
||||
|
||||
assert (obj2.propertyIsEnumerable('prop3') === true);
|
||||
assert (obj2.propertyIsEnumerable('prop2') === true);
|
||||
assert (obj2.propertyIsEnumerable('prop1') === false);
|
||||
|
||||
obj2.prop1 = 'foo';
|
||||
|
||||
assert (obj2.propertyIsEnumerable('prop1') === true);
|
||||
Loading…
x
Reference in New Issue
Block a user