Implement Object.property.hasOwnProperty

JerryScript-DCO-1.0-Signed-off-by: Peter Gal pgal.u-szeged@partner.samsung.com
This commit is contained in:
Peter Gal
2015-05-12 17:25:16 +02:00
parent b5d8444c2c
commit 6c4e988a06
2 changed files with 85 additions and 1 deletions
@@ -173,7 +173,38 @@ static ecma_completion_value_t
ecma_builtin_object_prototype_object_has_own_property (ecma_value_t this_arg, /**< this argument */
ecma_value_t arg) /**< 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);
if (property_p != NULL)
{
return_value = ecma_make_simple_completion_value (ECMA_SIMPLE_VALUE_TRUE);
}
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_has_own_property */
/**
@@ -0,0 +1,53 @@
// 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 obj1 = {};
obj1.prop = "hi";
assert (obj1.hasOwnProperty('prop') === true);
assert (obj1.hasOwnProperty('NO_PROP') === false);
// Test if the toString fails.
try {
obj1.hasOwnProperty({toString: function() { throw new ReferenceError ("foo"); }});
assert (false);
} catch (e) {
assert (e.message === "foo");
assert (e instanceof ReferenceError);
}
// Test if the toObject fails.
var obj2;
try {
obj2.hasOwnProperty("fail");
assert (false);
} catch (e) {
assert (e instanceof TypeError);
}
var obj_undef;
var obj3 = {};
Object.defineProperty(obj3, 'Test', { 'get' : function () {throw new ReferenceError ("foo"); } });
assert (obj3.hasOwnProperty("Test") === true);
Object.defineProperty(obj3, 'Test2', { 'get' : function () { return 0/0; } });
assert (obj3.hasOwnProperty("Test2") === true);
Object.defineProperty(obj3, 'Test4', { 'get' : function () { return obj_undef; } });
assert (obj3.hasOwnProperty("Test4") === true);