From 6c4e988a069cdd3f1aeb9d2f02925d8e85207903 Mon Sep 17 00:00:00 2001 From: Peter Gal Date: Tue, 12 May 2015 17:25:16 +0200 Subject: [PATCH] Implement Object.property.hasOwnProperty JerryScript-DCO-1.0-Signed-off-by: Peter Gal pgal.u-szeged@partner.samsung.com --- .../ecma-builtin-object-prototype.cpp | 33 +++++++++++- .../jerry/object_prototype_hasownproperty.js | 53 +++++++++++++++++++ 2 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 tests/jerry/object_prototype_hasownproperty.js diff --git a/jerry-core/ecma/builtin-objects/ecma-builtin-object-prototype.cpp b/jerry-core/ecma/builtin-objects/ecma-builtin-object-prototype.cpp index 18c394d15..e22ddf25b 100644 --- a/jerry-core/ecma/builtin-objects/ecma-builtin-object-prototype.cpp +++ b/jerry-core/ecma/builtin-objects/ecma-builtin-object-prototype.cpp @@ -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 */ /** diff --git a/tests/jerry/object_prototype_hasownproperty.js b/tests/jerry/object_prototype_hasownproperty.js new file mode 100644 index 000000000..5ab593228 --- /dev/null +++ b/tests/jerry/object_prototype_hasownproperty.js @@ -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);