mirror of
https://github.com/jerryscript-project/jerryscript.git
synced 2025-12-15 16:29:21 +00:00
Implement Object.prototype.{__defineGetter__, __defineSetter__} (#4032)
__defineGetter__ is based on ECMA-262 v11, B.2.2.2 __defineSetter__ is based on ECMA-262 v11, B.2.2.3 JerryScript-DCO-1.0-Signed-off-by: Adam Szilagyi aszilagy@inf.u-szeged.hu
This commit is contained in:
parent
b27f40303b
commit
b4a4619a6c
@ -51,7 +51,11 @@ enum
|
||||
ECMA_OBJECT_PROTOTYPE_IS_PROTOTYPE_OF,
|
||||
ECMA_OBJECT_PROTOTYPE_HAS_OWN_PROPERTY,
|
||||
ECMA_OBJECT_PROTOTYPE_PROPERTY_IS_ENUMERABLE,
|
||||
ECMA_OBJECT_PROTOTYPE_SET_PROTO
|
||||
ECMA_OBJECT_PROTOTYPE_SET_PROTO,
|
||||
#if ENABLED (JERRY_ESNEXT) && ENABLED (JERRY_BUILTIN_ANNEXB)
|
||||
ECMA_OBJECT_PROTOTYPE_DEFINE_GETTER,
|
||||
ECMA_OBJECT_PROTOTYPE_DEFINE_SETTER,
|
||||
#endif /* ENABLED (JERRY_ESNEXT) && ENABLED (JERRY_BUILTIN_ANNEXB) */
|
||||
};
|
||||
|
||||
#define BUILTIN_INC_HEADER_NAME "ecma-builtin-object-prototype.inc.h"
|
||||
@ -203,6 +207,63 @@ ecma_builtin_object_prototype_object_property_is_enumerable (ecma_object_t *obj_
|
||||
return ecma_make_boolean_value (is_enumerable);
|
||||
} /* ecma_builtin_object_prototype_object_property_is_enumerable */
|
||||
|
||||
#if ENABLED (JERRY_ESNEXT) && ENABLED (JERRY_BUILTIN_ANNEXB)
|
||||
/**
|
||||
* The Object.prototype object's '__defineGetter__' and '__defineSetter__' routine
|
||||
*
|
||||
* See also:
|
||||
* ECMA-262 v11, B.2.2.2
|
||||
* ECMA-262 v11, B.2.2.3
|
||||
*
|
||||
* @return ECMA_VALUE_ERROR - if the operation fails,
|
||||
* ECMA_VALUE_UNDEFINED - otherwise
|
||||
*/
|
||||
static ecma_value_t
|
||||
ecma_builtin_object_prototype_define_getter_setter (ecma_object_t *obj_p, /**< this argument */
|
||||
ecma_string_t *prop_name_p, /**< property name */
|
||||
ecma_value_t accessor, /**< getter/setter function */
|
||||
bool define_getter) /**< true - defineGetter method
|
||||
false - defineSetter method */
|
||||
{
|
||||
/* 2. */
|
||||
if (!ecma_op_is_callable (accessor))
|
||||
{
|
||||
return ecma_raise_type_error (ECMA_ERR_MSG ("Getter is not callable."));
|
||||
}
|
||||
|
||||
ecma_object_t *accessor_obj_p = ecma_get_object_from_value (accessor);
|
||||
|
||||
/* 3. */
|
||||
ecma_property_descriptor_t desc = ecma_make_empty_property_descriptor ();
|
||||
desc.flags |= (ECMA_PROP_IS_ENUMERABLE
|
||||
| ECMA_PROP_IS_CONFIGURABLE
|
||||
| ECMA_PROP_IS_ENUMERABLE_DEFINED
|
||||
| ECMA_PROP_IS_CONFIGURABLE_DEFINED);
|
||||
|
||||
if (define_getter)
|
||||
{
|
||||
desc.get_p = accessor_obj_p;
|
||||
desc.flags |= ECMA_PROP_IS_GET_DEFINED;
|
||||
}
|
||||
else
|
||||
{
|
||||
desc.set_p = accessor_obj_p;
|
||||
desc.flags |= ECMA_PROP_IS_SET_DEFINED;
|
||||
}
|
||||
|
||||
/* 5. */
|
||||
ecma_value_t define_prop = ecma_op_object_define_own_property (obj_p, prop_name_p, &desc);
|
||||
|
||||
if (ECMA_IS_VALUE_ERROR (define_prop))
|
||||
{
|
||||
return define_prop;
|
||||
}
|
||||
|
||||
/* 6. */
|
||||
return ECMA_VALUE_UNDEFINED;
|
||||
} /* ecma_builtin_object_prototype_define_getter_setter */
|
||||
#endif /* ENABLED (JERRY_ESNEXT) && ENABLED (JERRY_BUILTIN_ANNEXB) */
|
||||
|
||||
/**
|
||||
* Dispatcher of the built-in's routines
|
||||
*
|
||||
@ -307,6 +368,16 @@ ecma_builtin_object_prototype_dispatch_routine (uint16_t builtin_routine_id, /**
|
||||
{
|
||||
ret_value = ecma_builtin_object_prototype_object_has_own_property (obj_p, prop_name_p);
|
||||
}
|
||||
#if ENABLED (JERRY_ESNEXT) && ENABLED (JERRY_BUILTIN_ANNEXB)
|
||||
else if (builtin_routine_id == ECMA_OBJECT_PROTOTYPE_DEFINE_GETTER)
|
||||
{
|
||||
ret_value = ecma_builtin_object_prototype_define_getter_setter (obj_p, prop_name_p, arguments_list_p[1], true);
|
||||
}
|
||||
else if (builtin_routine_id == ECMA_OBJECT_PROTOTYPE_DEFINE_SETTER)
|
||||
{
|
||||
ret_value = ecma_builtin_object_prototype_define_getter_setter (obj_p, prop_name_p, arguments_list_p[1], false);
|
||||
}
|
||||
#endif /* ENABLED (JERRY_ESNEXT) && ENABLED (JERRY_BUILTIN_ANNEXB) */
|
||||
else
|
||||
{
|
||||
ret_value = ecma_builtin_object_prototype_object_property_is_enumerable (obj_p, prop_name_p);
|
||||
|
||||
@ -41,4 +41,9 @@ ROUTINE (LIT_MAGIC_STRING_HAS_OWN_PROPERTY_UL, ECMA_OBJECT_PROTOTYPE_HAS_OWN_PRO
|
||||
ROUTINE (LIT_MAGIC_STRING_IS_PROTOTYPE_OF_UL, ECMA_OBJECT_PROTOTYPE_IS_PROTOTYPE_OF, 1, 1)
|
||||
ROUTINE (LIT_MAGIC_STRING_PROPERTY_IS_ENUMERABLE_UL, ECMA_OBJECT_PROTOTYPE_PROPERTY_IS_ENUMERABLE, 1, 1)
|
||||
|
||||
#if ENABLED (JERRY_ESNEXT) && ENABLED (JERRY_BUILTIN_ANNEXB)
|
||||
ROUTINE (LIT_MAGIC_STRING_DEFINE_GETTER, ECMA_OBJECT_PROTOTYPE_DEFINE_GETTER, 2, 2)
|
||||
ROUTINE (LIT_MAGIC_STRING_DEFINE_SETTER, ECMA_OBJECT_PROTOTYPE_DEFINE_SETTER, 2, 2)
|
||||
#endif /* ENABLED (JERRY_ESNEXT) && ENABLED (JERRY_BUILTIN_ANNEXB) */
|
||||
|
||||
#include "ecma-builtin-helpers-macro-undefs.inc.h"
|
||||
|
||||
@ -879,6 +879,10 @@ LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_SET_MILLISECONDS_UL, "setMilliseconds")
|
||||
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_MAX_SAFE_INTEGER_U, "MAX_SAFE_INTEGER")
|
||||
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_MIN_SAFE_INTEGER_U, "MIN_SAFE_INTEGER")
|
||||
#endif
|
||||
#if ENABLED (JERRY_ESNEXT) && ENABLED (JERRY_BUILTIN_ANNEXB)
|
||||
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_DEFINE_GETTER, "__defineGetter__")
|
||||
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_DEFINE_SETTER, "__defineSetter__")
|
||||
#endif
|
||||
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_DEFINE_PROPERTIES_UL, "defineProperties")
|
||||
#if ENABLED (JERRY_BUILTIN_TYPEDARRAY)
|
||||
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_BYTES_PER_ELEMENT_U, "BYTES_PER_ELEMENT")
|
||||
@ -1057,11 +1061,15 @@ LIT_MAGIC_STRING_FIRST_STRING_WITH_SIZE (15, LIT_MAGIC_STRING_STRING_ITERATOR_UL
|
||||
LIT_MAGIC_STRING_FIRST_STRING_WITH_SIZE (15, LIT_MAGIC_STRING_GET_MILLISECONDS_UL)
|
||||
#elif ENABLED (JERRY_BUILTIN_NUMBER) && ENABLED (JERRY_ESNEXT)
|
||||
LIT_MAGIC_STRING_FIRST_STRING_WITH_SIZE (15, LIT_MAGIC_STRING_MAX_SAFE_INTEGER_U)
|
||||
#elif ENABLED (JERRY_ESNEXT) && ENABLED (JERRY_BUILTIN_ANNEXB)
|
||||
LIT_MAGIC_STRING_FIRST_STRING_WITH_SIZE (15, LIT_MAGIC_STRING_DEFINE_GETTER)
|
||||
#else
|
||||
LIT_MAGIC_STRING_FIRST_STRING_WITH_SIZE (15, LIT_MAGIC_STRING_DEFINE_PROPERTIES_UL)
|
||||
#endif
|
||||
#if ENABLED (JERRY_BUILTIN_NUMBER) && ENABLED (JERRY_ESNEXT)
|
||||
LIT_MAGIC_STRING_FIRST_STRING_WITH_SIZE (16, LIT_MAGIC_STRING_MAX_SAFE_INTEGER_U)
|
||||
#elif ENABLED (JERRY_ESNEXT) && ENABLED (JERRY_BUILTIN_ANNEXB)
|
||||
LIT_MAGIC_STRING_FIRST_STRING_WITH_SIZE (16, LIT_MAGIC_STRING_DEFINE_GETTER)
|
||||
#else
|
||||
LIT_MAGIC_STRING_FIRST_STRING_WITH_SIZE (16, LIT_MAGIC_STRING_DEFINE_PROPERTIES_UL)
|
||||
#endif
|
||||
|
||||
@ -353,6 +353,8 @@ LIT_MAGIC_STRING_SET_MILLISECONDS_UL = "setMilliseconds"
|
||||
LIT_MAGIC_STRING_DEFINE_PROPERTIES_UL = "defineProperties"
|
||||
LIT_MAGIC_STRING_MAX_SAFE_INTEGER_U = "MAX_SAFE_INTEGER"
|
||||
LIT_MAGIC_STRING_MIN_SAFE_INTEGER_U = "MIN_SAFE_INTEGER"
|
||||
LIT_MAGIC_STRING_DEFINE_GETTER = "__defineGetter__"
|
||||
LIT_MAGIC_STRING_DEFINE_SETTER = "__defineSetter__"
|
||||
LIT_MAGIC_STRING_BYTES_PER_ELEMENT_U = "BYTES_PER_ELEMENT"
|
||||
LIT_MAGIC_STRING_NEGATIVE_INFINITY_U = "NEGATIVE_INFINITY"
|
||||
LIT_MAGIC_STRING_POSITIVE_INFINITY_U = "POSITIVE_INFINITY"
|
||||
|
||||
51
tests/jerry/es.next/object-prototype-define-getter.js
Normal file
51
tests/jerry/es.next/object-prototype-define-getter.js
Normal file
@ -0,0 +1,51 @@
|
||||
// Copyright JS Foundation and other contributors, http://js.foundation
|
||||
//
|
||||
// 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 o = {};
|
||||
o.__defineGetter__('foo', function() { return 5; });
|
||||
assert(o.foo === 5);
|
||||
|
||||
var obj = {};
|
||||
function bar() { return "bar"; }
|
||||
Object.prototype.__defineGetter__.call(obj, "foo", bar);
|
||||
var prop = Object.getOwnPropertyDescriptor(obj, "foo");
|
||||
assert(prop.get == bar);
|
||||
assert(!prop.writable);
|
||||
assert(prop.configurable);
|
||||
assert(prop.enumerable);
|
||||
|
||||
var obj = {};
|
||||
var sym = Symbol();
|
||||
function bar() { return "bar"; }
|
||||
Object.prototype.__defineGetter__.call(obj, sym, bar);
|
||||
var prop = Object.getOwnPropertyDescriptor(obj, sym);
|
||||
assert(prop.get == bar);
|
||||
assert(!prop.writable);
|
||||
assert(prop.configurable);
|
||||
assert(prop.enumerable);;
|
||||
|
||||
var key = '__accessors_test__';
|
||||
Object.prototype.__defineGetter__.call(1, key, function(){});
|
||||
|
||||
try {
|
||||
Object.prototype.__defineGetter__.call(null, key, function(){});
|
||||
assert(false);
|
||||
} catch(e){
|
||||
assert(e instanceof TypeError);
|
||||
}
|
||||
|
||||
var def = [];
|
||||
var p = new Proxy({}, { defineProperty: function(o, v, desc) { def.push(v); Object.defineProperty(o, v, desc); return true; }});
|
||||
Object.prototype.__defineGetter__.call(p, "foo", Object);
|
||||
assert(def + '' === "foo");
|
||||
53
tests/jerry/es.next/object-prototype-define-setter.js
Normal file
53
tests/jerry/es.next/object-prototype-define-setter.js
Normal file
@ -0,0 +1,53 @@
|
||||
// Copyright JS Foundation and other contributors, http://js.foundation
|
||||
//
|
||||
// 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 o = {};
|
||||
o.__defineSetter__('foo', function(val) { this.bar = val; });
|
||||
o.foo = 5;
|
||||
assert(o.foo === undefined); // undefined
|
||||
assert(o.bar === 5);
|
||||
|
||||
var obj = {};
|
||||
function bar() {}
|
||||
Object.prototype.__defineSetter__.call(obj, "foo", bar);
|
||||
var prop = Object.getOwnPropertyDescriptor(obj, "foo");
|
||||
assert(prop.set === bar);
|
||||
assert(!prop.writable);
|
||||
assert(prop.configurable);
|
||||
assert(prop.enumerable);
|
||||
|
||||
var obj = {};
|
||||
var sym = Symbol();
|
||||
function bar(baz) {}
|
||||
Object.prototype.__defineSetter__.call(obj, sym, bar);
|
||||
var prop = Object.getOwnPropertyDescriptor(obj, sym);
|
||||
assert(prop.set === bar);
|
||||
assert(!prop.writable);
|
||||
assert(prop.configurable);
|
||||
assert(prop.enumerable);
|
||||
|
||||
var key = '__accessors_test__';
|
||||
Object.prototype.__defineSetter__.call(1, key, function(){});
|
||||
|
||||
try {
|
||||
Object.prototype.__defineSetter__.call(null, key, function(){});
|
||||
assert(false);
|
||||
} catch (e) {
|
||||
assert(e instanceof TypeError);
|
||||
}
|
||||
|
||||
var def = [];
|
||||
var p = new Proxy({}, { defineProperty: function(o, v, desc) { def.push(v); Object.defineProperty(o, v, desc); return true; }});
|
||||
Object.prototype.__defineSetter__.call(p, "foo", Object);
|
||||
assert(def + '' === "foo");
|
||||
Loading…
x
Reference in New Issue
Block a user