Add non-standard behaviour support for Proxies (#4562)
JerryScript-DCO-1.0-Signed-off-by: Zoltan Herczeg zherczeg.u-szeged@partner.samsung.com
This commit is contained in:
+44
-1
@@ -2221,13 +2221,56 @@ jerry_create_proxy (const jerry_value_t target, /**< target argument */
|
||||
}
|
||||
|
||||
#if ENABLED (JERRY_BUILTIN_PROXY)
|
||||
ecma_object_t *proxy_p = ecma_proxy_create (target, handler);
|
||||
ecma_object_t *proxy_p = ecma_proxy_create (target, handler, 0);
|
||||
return jerry_return (proxy_p == NULL ? ECMA_VALUE_ERROR : ecma_make_object_value (proxy_p));
|
||||
#else /* !ENABLED (JERRY_BUILTIN_PROXY) */
|
||||
return jerry_throw (ecma_raise_type_error (ECMA_ERR_MSG ("Proxy is not supported")));
|
||||
#endif /* ENABLED (JERRY_BUILTIN_PROXY) */
|
||||
} /* jerry_create_proxy */
|
||||
|
||||
#if ENABLED (JERRY_BUILTIN_PROXY)
|
||||
|
||||
JERRY_STATIC_ASSERT ((int) JERRY_PROXY_SKIP_GET_CHECKS == (int) ECMA_PROXY_SKIP_GET_CHECKS,
|
||||
jerry_and_ecma_proxy_skip_get_checks_must_be_equal);
|
||||
JERRY_STATIC_ASSERT ((int) JERRY_PROXY_SKIP_GET_OWN_PROPERTY_CHECKS == (int) ECMA_PROXY_SKIP_GET_OWN_PROPERTY_CHECKS,
|
||||
jerry_and_ecma_proxy_skip_get_own_property_checks_must_be_equal);
|
||||
|
||||
#endif /* ENABLED (JERRY_BUILTIN_PROXY) */
|
||||
|
||||
/**
|
||||
* Create a new Proxy object with the given target, handler, and special options
|
||||
*
|
||||
* Note:
|
||||
* returned value must be freed with jerry_release_value, when it is no longer needed.
|
||||
*
|
||||
* @return value of the created Proxy object
|
||||
*/
|
||||
jerry_value_t
|
||||
jerry_create_special_proxy (const jerry_value_t target, /**< target argument */
|
||||
const jerry_value_t handler, /**< handler argument */
|
||||
uint32_t options) /**< jerry_proxy_object_options_t option bits */
|
||||
{
|
||||
jerry_assert_api_available ();
|
||||
|
||||
if (ecma_is_value_error_reference (target)
|
||||
|| ecma_is_value_error_reference (handler))
|
||||
{
|
||||
return jerry_throw (ecma_raise_type_error (ECMA_ERR_MSG (wrong_args_msg_p)));
|
||||
}
|
||||
|
||||
#if ENABLED (JERRY_BUILTIN_PROXY)
|
||||
const uint32_t options_mask = (JERRY_PROXY_SKIP_GET_CHECKS
|
||||
| JERRY_PROXY_SKIP_GET_OWN_PROPERTY_CHECKS);
|
||||
options &= options_mask;
|
||||
|
||||
ecma_object_t *proxy_p = ecma_proxy_create (target, handler, options);
|
||||
return jerry_return (proxy_p == NULL ? ECMA_VALUE_ERROR : ecma_make_object_value (proxy_p));
|
||||
#else /* !ENABLED (JERRY_BUILTIN_PROXY) */
|
||||
JERRY_UNUSED (options);
|
||||
return jerry_throw (ecma_raise_type_error (ECMA_ERR_MSG ("Proxy is not supported")));
|
||||
#endif /* ENABLED (JERRY_BUILTIN_PROXY) */
|
||||
} /* jerry_create_special_proxy */
|
||||
|
||||
/**
|
||||
* Create string from a valid UTF-8 string
|
||||
*
|
||||
|
||||
@@ -887,9 +887,7 @@ ecma_gc_mark (ecma_object_t *object_p) /**< object to mark from */
|
||||
case ECMA_OBJECT_TYPE_PROXY:
|
||||
{
|
||||
ecma_gc_mark_proxy_object (object_p);
|
||||
/* The protoype of the proxy should be "empty" (aside from the tag bits every other bit should be zero). */
|
||||
JERRY_ASSERT ((object_p->u2.prototype_cp & ~JMEM_TAG_MASK) == 0);
|
||||
/* Make sure that the prototype is not checked below. */
|
||||
/* Prototype of proxy object is a bit set. */
|
||||
proto_cp = JMEM_CP_NULL;
|
||||
break;
|
||||
}
|
||||
@@ -1696,8 +1694,6 @@ ecma_gc_free_object (ecma_object_t *object_p) /**< object to free */
|
||||
#if ENABLED (JERRY_BUILTIN_PROXY)
|
||||
case ECMA_OBJECT_TYPE_PROXY:
|
||||
{
|
||||
/* The protoype of the proxy should be "empty" (aside from the tag bits every other bit should be zero). */
|
||||
JERRY_ASSERT ((object_p->u2.prototype_cp & ~JMEM_TAG_MASK) == 0);
|
||||
ext_object_size = sizeof (ecma_proxy_object_t);
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -2176,6 +2176,18 @@ do \
|
||||
#define ECMA_PROPERTY_POINTER_ERROR ((ecma_property_t *) 0x01)
|
||||
|
||||
#if ENABLED (JERRY_BUILTIN_PROXY)
|
||||
|
||||
/**
|
||||
* Proxy object flags.
|
||||
*/
|
||||
typedef enum
|
||||
{
|
||||
ECMA_PROXY_SKIP_GET_CHECKS = (1u << 0), /**< skip [[Get]] result checks */
|
||||
ECMA_PROXY_SKIP_GET_OWN_PROPERTY_CHECKS = (1u << 1), /**< skip [[GetOwnProperty]] result checks */
|
||||
ECMA_PROXY_IS_CALLABLE = (1u << 2), /**< proxy is callable */
|
||||
ECMA_PROXY_IS_CONSTRUCTABLE = (1u << 3), /**< proxy is constructable */
|
||||
} ecma_proxy_flag_types_t;
|
||||
|
||||
/**
|
||||
* Description of Proxy objects.
|
||||
*
|
||||
|
||||
@@ -101,7 +101,8 @@ ecma_builtin_proxy_dispatch_construct (const ecma_value_t *arguments_list_p, /**
|
||||
|
||||
/* 2. */
|
||||
ecma_object_t *proxy_p = ecma_proxy_create (arguments_list_len > 0 ? arguments_list_p[0] : ECMA_VALUE_UNDEFINED,
|
||||
arguments_list_len > 1 ? arguments_list_p[1] : ECMA_VALUE_UNDEFINED);
|
||||
arguments_list_len > 1 ? arguments_list_p[1] : ECMA_VALUE_UNDEFINED,
|
||||
0);
|
||||
|
||||
if (JERRY_UNLIKELY (proxy_p == NULL))
|
||||
{
|
||||
|
||||
@@ -106,7 +106,7 @@ ecma_op_object_is_callable (ecma_object_t *obj_p) /**< ecma object */
|
||||
#if ENABLED (JERRY_BUILTIN_PROXY)
|
||||
if (ECMA_OBJECT_TYPE_IS_PROXY (type))
|
||||
{
|
||||
return ECMA_GET_FIRST_BIT_FROM_POINTER_TAG (obj_p->u2.prototype_cp) != 0;
|
||||
return (obj_p->u2.prototype_cp & ECMA_PROXY_IS_CALLABLE) != 0;
|
||||
}
|
||||
#endif /* ENABLED (JERRY_BUILTIN_PROXY) */
|
||||
|
||||
@@ -215,7 +215,7 @@ ecma_object_check_constructor (ecma_object_t *obj_p) /**< ecma object */
|
||||
#if ENABLED (JERRY_BUILTIN_PROXY)
|
||||
if (ECMA_OBJECT_TYPE_IS_PROXY (type))
|
||||
{
|
||||
if (ECMA_GET_SECOND_BIT_FROM_POINTER_TAG (obj_p->u2.prototype_cp) == 0)
|
||||
if (!(obj_p->u2.prototype_cp & ECMA_PROXY_IS_CONSTRUCTABLE))
|
||||
{
|
||||
return ECMA_ERR_MSG ("Proxy target is not a constructor");
|
||||
}
|
||||
|
||||
@@ -48,7 +48,8 @@
|
||||
*/
|
||||
ecma_object_t *
|
||||
ecma_proxy_create (ecma_value_t target, /**< proxy target */
|
||||
ecma_value_t handler) /**< proxy handler */
|
||||
ecma_value_t handler, /**< proxy handler */
|
||||
uint32_t options) /**< ecma_proxy_flag_types_t option bits */
|
||||
{
|
||||
/* ES2015: 1, 3. */
|
||||
/* ES11+: 1 - 2. */
|
||||
@@ -65,17 +66,19 @@ ecma_proxy_create (ecma_value_t target, /**< proxy target */
|
||||
|
||||
ecma_proxy_object_t *proxy_obj_p = (ecma_proxy_object_t *) obj_p;
|
||||
|
||||
obj_p->u2.prototype_cp = (jmem_cpointer_t) options;
|
||||
|
||||
/* ES2015: 7. */
|
||||
/* ES11+: 5. */
|
||||
if (ecma_op_is_callable (target))
|
||||
{
|
||||
ECMA_SET_FIRST_BIT_TO_POINTER_TAG (obj_p->u2.prototype_cp);
|
||||
obj_p->u2.prototype_cp |= ECMA_PROXY_IS_CALLABLE;
|
||||
|
||||
/* ES2015: 7.b. */
|
||||
/* ES11+: 5.b. */
|
||||
if (ecma_is_constructor (target))
|
||||
{
|
||||
ECMA_SET_SECOND_BIT_TO_POINTER_TAG (obj_p->u2.prototype_cp);
|
||||
obj_p->u2.prototype_cp |= ECMA_PROXY_IS_CONSTRUCTABLE;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -150,7 +153,7 @@ ecma_proxy_create_revocable (ecma_value_t target, /**< target argument */
|
||||
ecma_value_t handler) /**< handler argument */
|
||||
{
|
||||
/* 1. */
|
||||
ecma_object_t *proxy_p = ecma_proxy_create (target, handler);
|
||||
ecma_object_t *proxy_p = ecma_proxy_create (target, handler, 0);
|
||||
|
||||
/* 2. */
|
||||
if (proxy_p == NULL)
|
||||
@@ -688,6 +691,25 @@ ecma_proxy_object_get_own_property_descriptor (ecma_object_t *obj_p, /**< proxy
|
||||
return ecma_raise_type_error (ECMA_ERR_MSG ("Trap is neither an object nor undefined"));
|
||||
}
|
||||
|
||||
if (obj_p->u2.prototype_cp & ECMA_PROXY_SKIP_GET_OWN_PROPERTY_CHECKS)
|
||||
{
|
||||
if (ecma_is_value_undefined (trap_result))
|
||||
{
|
||||
return ECMA_VALUE_FALSE;
|
||||
}
|
||||
|
||||
ecma_value_t result_val = ecma_op_to_property_descriptor (trap_result, prop_desc_p);
|
||||
ecma_free_value (trap_result);
|
||||
|
||||
if (ECMA_IS_VALUE_ERROR (result_val))
|
||||
{
|
||||
return result_val;
|
||||
}
|
||||
|
||||
ecma_op_to_complete_property_descriptor (prop_desc_p);
|
||||
return ECMA_VALUE_TRUE;
|
||||
}
|
||||
|
||||
/* 12. */
|
||||
ecma_property_descriptor_t target_desc;
|
||||
ecma_value_t target_status = ecma_op_object_get_own_property_descriptor (target_obj_p, prop_name_p, &target_desc);
|
||||
@@ -1118,7 +1140,8 @@ ecma_proxy_object_get (ecma_object_t *obj_p, /**< proxy object */
|
||||
ecma_deref_object (func_obj_p);
|
||||
|
||||
/* 10. */
|
||||
if (ECMA_IS_VALUE_ERROR (trap_result))
|
||||
if (ECMA_IS_VALUE_ERROR (trap_result)
|
||||
|| (obj_p->u2.prototype_cp & ECMA_PROXY_SKIP_GET_CHECKS))
|
||||
{
|
||||
return trap_result;
|
||||
}
|
||||
@@ -1147,9 +1170,9 @@ ecma_proxy_object_get (ecma_object_t *obj_p, /**< proxy object */
|
||||
ret_value = ecma_raise_type_error (ECMA_ERR_MSG ("Incorrect value is returned by a Proxy 'get' trap"));
|
||||
}
|
||||
else if (!(target_desc.flags & ECMA_PROP_IS_CONFIGURABLE)
|
||||
&& (target_desc.flags & (ECMA_PROP_IS_GET_DEFINED | ECMA_PROP_IS_SET_DEFINED))
|
||||
&& target_desc.get_p == NULL
|
||||
&& !ecma_is_value_undefined (trap_result))
|
||||
&& (target_desc.flags & (ECMA_PROP_IS_GET_DEFINED | ECMA_PROP_IS_SET_DEFINED))
|
||||
&& target_desc.get_p == NULL
|
||||
&& !ecma_is_value_undefined (trap_result))
|
||||
{
|
||||
ret_value = ecma_raise_type_error (ECMA_ERR_MSG ("Property of a Proxy is non-configurable and "
|
||||
"does not have a getter function"));
|
||||
|
||||
@@ -29,7 +29,8 @@
|
||||
|
||||
ecma_object_t *
|
||||
ecma_proxy_create (ecma_value_t target,
|
||||
ecma_value_t handler);
|
||||
ecma_value_t handler,
|
||||
uint32_t options);
|
||||
|
||||
ecma_object_t *
|
||||
ecma_proxy_create_revocable (ecma_value_t target,
|
||||
|
||||
@@ -471,6 +471,15 @@ typedef enum
|
||||
JERRY_ITERATOR_TYPE_SET, /**< Set iterator */
|
||||
} jerry_iterator_type_t;
|
||||
|
||||
/**
|
||||
* JerryScript special Proxy object options.
|
||||
*/
|
||||
typedef enum
|
||||
{
|
||||
JERRY_PROXY_SKIP_GET_CHECKS = (1u << 0), /**< skip [[Get]] result checks */
|
||||
JERRY_PROXY_SKIP_GET_OWN_PROPERTY_CHECKS = (1u << 1), /**< skip [[GetOwnProperty]] result checks */
|
||||
} jerry_proxy_object_options_t;
|
||||
|
||||
/**
|
||||
* JerryScript object property filter options.
|
||||
*/
|
||||
@@ -597,6 +606,8 @@ jerry_value_t jerry_create_null (void);
|
||||
jerry_value_t jerry_create_object (void);
|
||||
jerry_value_t jerry_create_promise (void);
|
||||
jerry_value_t jerry_create_proxy (const jerry_value_t target, const jerry_value_t handler);
|
||||
jerry_value_t jerry_create_special_proxy (const jerry_value_t target, const jerry_value_t handler,
|
||||
uint32_t options);
|
||||
jerry_value_t jerry_create_regexp (const jerry_char_t *pattern, uint16_t flags);
|
||||
jerry_value_t jerry_create_regexp_sz (const jerry_char_t *pattern, jerry_size_t pattern_size, uint16_t flags);
|
||||
jerry_value_t jerry_create_string_from_utf8 (const jerry_char_t *str_p);
|
||||
|
||||
Reference in New Issue
Block a user