From c2aba42e1bd790df41689368bc779a2a1e3b9186 Mon Sep 17 00:00:00 2001 From: Kristof Kosztyo Date: Thu, 18 Jun 2015 17:10:02 +0200 Subject: [PATCH] Implement Object.create function JerryScript-DCO-1.0-Signed-off-by: Kristof Kosztyo kkosztyo.u-szeged@partner.samsung.com --- .../builtin-objects/ecma-builtin-object.cpp | 41 ++++- .../ecma/operations/ecma-objects-general.cpp | 36 +++-- .../ecma/operations/ecma-objects-general.h | 3 +- tests/jerry/object-create.js | 146 ++++++++++++++++++ 4 files changed, 215 insertions(+), 11 deletions(-) create mode 100644 tests/jerry/object-create.js diff --git a/jerry-core/ecma/builtin-objects/ecma-builtin-object.cpp b/jerry-core/ecma/builtin-objects/ecma-builtin-object.cpp index f7e413fab1..d757cd8b4c 100644 --- a/jerry-core/ecma/builtin-objects/ecma-builtin-object.cpp +++ b/jerry-core/ecma/builtin-objects/ecma-builtin-object.cpp @@ -637,7 +637,46 @@ ecma_builtin_object_object_create (ecma_value_t this_arg, /**< 'this' argument * ecma_value_t arg1, /**< routine's first argument */ ecma_value_t arg2) /**< routine's second argument */ { - ECMA_BUILTIN_CP_UNIMPLEMENTED (this_arg, arg1, arg2); + ecma_completion_value_t ret_value = ecma_make_empty_completion_value (); + + // 1. + if (!ecma_is_value_object (arg1) && !ecma_is_value_null (arg1)) + { + ret_value = ecma_make_throw_obj_completion_value (ecma_new_standard_error (ECMA_ERROR_TYPE)); + } + else + { + ecma_object_t *obj_p = NULL; + + if (!ecma_is_value_null (arg1)) + { + obj_p = ecma_get_object_from_value (arg1); + } + // 2-3. + ecma_object_t *result_obj_p = ecma_op_create_object_object_noarg_and_set_prototype (obj_p); + + // 4. + if (!ecma_is_value_undefined (arg2)) + { + ECMA_TRY_CATCH (obj, + ecma_builtin_object_object_define_properties (this_arg, + ecma_make_object_value (result_obj_p), + arg2), + ret_value); + ECMA_FINALIZE (obj); + } + + // 5. + if (ecma_is_completion_value_empty (ret_value)) + { + ret_value = ecma_make_normal_completion_value (ecma_copy_value (ecma_make_object_value (result_obj_p), + true)); + } + + ecma_deref_object (result_obj_p); + } + + return ret_value; } /* ecma_builtin_object_object_create */ /** diff --git a/jerry-core/ecma/operations/ecma-objects-general.cpp b/jerry-core/ecma/operations/ecma-objects-general.cpp index 1be997c762..d42e9067d1 100644 --- a/jerry-core/ecma/operations/ecma-objects-general.cpp +++ b/jerry-core/ecma/operations/ecma-objects-general.cpp @@ -62,18 +62,10 @@ ecma_op_create_object_object_noarg (void) ecma_object_t *object_prototype_p = ecma_builtin_get (ECMA_BUILTIN_ID_OBJECT_PROTOTYPE); // 3., 4., 6., 7. - ecma_object_t *obj_p = ecma_create_object (object_prototype_p, true, ECMA_OBJECT_TYPE_GENERAL); + ecma_object_t *obj_p = ecma_op_create_object_object_noarg_and_set_prototype (object_prototype_p); ecma_deref_object (object_prototype_p); - /* - * [[Class]] property of ECMA_OBJECT_TYPE_GENERAL type objects - * without ECMA_INTERNAL_PROPERTY_CLASS internal property - * is "Object". - * - * See also: ecma_object_get_class_name - */ - return obj_p; } /* ecma_op_create_object_object_noarg */ @@ -109,6 +101,32 @@ ecma_op_create_object_object_arg (ecma_value_t value) /**< argument of construct } } /* ecma_op_create_object_object_arg */ +/** + * Object creation operation with no arguments. + * It sets the given prototype to the newly created object. + * + * See also: ECMA-262 v5, 15.2.2.1, 15.2.3.5 + * + * @return pointer to newly created object + */ +ecma_object_t* +ecma_op_create_object_object_noarg_and_set_prototype (ecma_object_t *object_prototype_p) /**< pointer to prototype of + the object + (can be NULL) */ +{ + ecma_object_t *obj_p = ecma_create_object (object_prototype_p, true, ECMA_OBJECT_TYPE_GENERAL); + + /* + * [[Class]] property of ECMA_OBJECT_TYPE_GENERAL type objects + * without ECMA_INTERNAL_PROPERTY_CLASS internal property + * is "Object". + * + * See also: ecma_object_get_class_name + */ + + return obj_p; +} /* ecma_op_create_object_object_noarg_and_set_prototype */ + /** * [[Get]] ecma general object's operation * diff --git a/jerry-core/ecma/operations/ecma-objects-general.h b/jerry-core/ecma/operations/ecma-objects-general.h index 3ed8e07396..4b5ad6faef 100644 --- a/jerry-core/ecma/operations/ecma-objects-general.h +++ b/jerry-core/ecma/operations/ecma-objects-general.h @@ -26,8 +26,9 @@ * @{ */ -extern ecma_object_t* ecma_op_create_object_object_noarg (void); +extern ecma_object_t *ecma_op_create_object_object_noarg (void); extern ecma_completion_value_t ecma_op_create_object_object_arg (ecma_value_t value); +extern ecma_object_t *ecma_op_create_object_object_noarg_and_set_prototype (ecma_object_t *object_prototype_p); extern ecma_completion_value_t ecma_op_general_object_get (ecma_object_t *obj_p, ecma_string_t *property_name_p); diff --git a/tests/jerry/object-create.js b/tests/jerry/object-create.js new file mode 100644 index 0000000000..fc7a496d4a --- /dev/null +++ b/tests/jerry/object-create.js @@ -0,0 +1,146 @@ +// 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. + +// Example where we create an object with a couple of sample properties. +// (Note that the second parameter maps keys to *property descriptors*.) +var o = Object.create(Object.prototype, { + // foo is a regular 'value property' + foo: { writable: true, configurable: true, value: 'hello' }, + // bar is a getter-and-setter (accessor) property + bar: { + configurable: false, + get: function() { return 10; }, + set: function(value) { console.log('Setting `o.bar` to', value); } + } +}); + +// create a new object whose prototype is a new, empty object +// and a adding single property 'p', with value 42 +var o = Object.create({}, { p: { value: 42 } }); +// by default properties ARE NOT writable, enumerable or configurable: +o.p = 24; +assert (o.p === 42); + +// to specify an ES3 property +var o2 = Object.create({}, { + p: { + value: 42, + writable: true, + enumerable: true, + configurable: true + } +}); + +assert (o2.p === 42); + +// Shape - superclass +function Shape() { + this.x = 0; + this.y = 0; +} + +// superclass method +Shape.prototype.move = function(x, y) { + this.x += x; + this.y += y; +}; + +// Rectangle - subclass +function Rectangle() { + Shape.call(this); // call super constructor. +} + +// subclass extends superclass +Rectangle.prototype = Object.create(Shape.prototype); +Rectangle.prototype.constructor = Rectangle; + +var rect = new Rectangle(); + +assert (rect instanceof Rectangle); +assert (rect instanceof Shape); +rect.move(1, 1); +assert (rect.x === 1) +assert (rect.y === 1); + +var obj = { + protoFunction: function() { + return 3; + } +}; + +Object.defineProperties(obj, { + "foo": { + value: 42, + writable: true, + }, + "a": { + value: "b", + configurable: true + }, + "bar": { + get: function() { + return this.foo; + }, + }, +}); + +var obj2 = Object.create(obj); + +assert (obj2.protoFunction() === 3); +assert (obj2.foo === 42); +assert (obj2.a === "b"); +assert (obj2.bar === 42); +assert (Object.getPrototypeOf (obj2) === obj); + + +var props = { + prop1: { + value: 1, + }, + hey: function () { + return "ho"; + } +}; + +var obj3 = Object.create(obj, props); +assert (obj3.prop1 === 1); +assert (obj3.protoFunction()); +try { + assert (obj3.hey === undefined); + obj3.hey(); + assert (false); +} catch (e) { + assert (e instanceof TypeError); +} + +// Create an object with null as prototype +var obj = Object.create(null) +assert (typeof (obj) === "object"); +// FIXME: enable this assertion after the #208 is fixed. +// assert (Object.getPrototypeOf (obj) === null); + +try { + Object.create() + assert (false); +} catch (e) { + assert (e instanceof TypeError); +} + +try { + Object.create(undefined) + assert (false); +} catch (e) { + assert (e instanceof TypeError); +}