Implement WeakRef Object (#4546)

Related test262 test-cases has been removed from skip list.
Execpt two test-case since they need other feature to work (Finalization registry and async GC)

JerryScript-DCO-1.0-Signed-off-by: Bence Gabor Kis kisbg@inf.u-szeged.hu
This commit is contained in:
kisbg
2021-03-01 12:46:54 +00:00
committed by GitHub
parent 9556701742
commit 129ca4946c
24 changed files with 500 additions and 108 deletions
@@ -233,14 +233,20 @@ OBJECT_VALUE (LIT_MAGIC_STRING_SET_UL,
OBJECT_VALUE (LIT_MAGIC_STRING_WEAKMAP_UL,
ECMA_BUILTIN_ID_WEAKMAP,
ECMA_PROPERTY_CONFIGURABLE_WRITABLE)
#endif /* JERRY_BUILTIN_SET */
#endif /* JERRY_BUILTIN_WEAKMAP */
#if JERRY_BUILTIN_WEAKREF
OBJECT_VALUE (LIT_MAGIC_STRING_WEAKREF_UL,
ECMA_BUILTIN_ID_WEAKREF,
ECMA_PROPERTY_CONFIGURABLE_WRITABLE)
#endif /* JERRY_BUILTIN_WEAKREF */
#if JERRY_BUILTIN_WEAKSET
/* ECMA-262 v6, 23.1.1.1 */
OBJECT_VALUE (LIT_MAGIC_STRING_WEAKSET_UL,
ECMA_BUILTIN_ID_WEAKSET,
ECMA_PROPERTY_CONFIGURABLE_WRITABLE)
#endif /* JERRY_BUILTIN_SET */
#endif /* JERRY_BUILTIN_WEAKSET */
#if JERRY_ESNEXT
/* ECMA-262 v6, 19.4.1.1 */
@@ -0,0 +1,72 @@
/* 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.
*/
#include "ecma-container-object.h"
#include "ecma-exceptions.h"
#if JERRY_BUILTIN_WEAKREF
#define ECMA_BUILTINS_INTERNAL
#include "ecma-builtins-internal.h"
/**
* This object has a custom dispatch function.
*/
#define BUILTIN_INC_HEADER_NAME "ecma-builtin-weakref-prototype.inc.h"
#define BUILTIN_UNDERSCORED_ID weakref_prototype
#include "ecma-builtin-internal-routines-template.inc.h"
/** \addtogroup ecma ECMA
* @{
*
* \addtogroup ecmabuiltins
* @{
*
* \addtogroup weakref ECMA WeakRef object built-in
* @{
*/
/**
* Deref checks weakRef target
*
* @return weakRef target
* error - otherwise
*/
static ecma_value_t
ecma_builtin_weakref_prototype_object_deref (ecma_value_t this_arg) /**< this argument */
{
if (!ecma_is_value_object (this_arg))
{
return ecma_raise_type_error ("Target is not Object");
}
ecma_object_t *object_p = ecma_get_object_from_value (this_arg);
ecma_extended_object_t *this_ext_obj = (ecma_extended_object_t *) object_p;
if (!ecma_object_class_is (object_p, LIT_MAGIC_STRING_WEAKREF_UL))
{
return ecma_raise_type_error ("Target is not weakRef");
}
return ecma_copy_value (this_ext_obj->u.class_prop.u.target);
} /* ecma_builtin_weakref_prototype_object_deref */
/**
* @}
* @}
* @}
*/
#endif /* JERRY_BUILTIN_WEAKREF */
@@ -0,0 +1,41 @@
/* 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.
*/
/*
* WeakSet.prototype built-in description
*/
#include "ecma-builtin-helpers-macro-defines.inc.h"
#if JERRY_BUILTIN_WEAKREF
/* Object properties:
* (property name, object pointer getter) */
OBJECT_VALUE (LIT_MAGIC_STRING_CONSTRUCTOR,
ECMA_BUILTIN_ID_WEAKREF,
ECMA_PROPERTY_CONFIGURABLE_WRITABLE)
STRING_VALUE (LIT_GLOBAL_SYMBOL_TO_STRING_TAG,
LIT_MAGIC_STRING_WEAKREF_UL,
ECMA_PROPERTY_FLAG_CONFIGURABLE)
/* Routine properties:
* (property name, C routine name, arguments number or NON_FIXED, value of the routine's length property) */
ROUTINE (LIT_MAGIC_STRING_DEREF, ecma_builtin_weakref_prototype_object_deref, 0, 0)
#endif /* JERRY_BUILTIN_WEAKREF */
#include "ecma-builtin-helpers-macro-undefs.inc.h"
@@ -0,0 +1,97 @@
/* 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.
*/
#include "ecma-builtins.h"
#include "ecma-exceptions.h"
#include "jcontext.h"
#include "ecma-function-object.h"
#include "ecma-gc.h"
#if JERRY_BUILTIN_WEAKREF
#define ECMA_BUILTINS_INTERNAL
#include "ecma-builtins-internal.h"
#define BUILTIN_INC_HEADER_NAME "ecma-builtin-weakref.inc.h"
#define BUILTIN_UNDERSCORED_ID weakref
#include "ecma-builtin-internal-routines-template.inc.h"
/** \addtogroup ecma ECMA
* @{
*
* \addtogroup ecmabuiltins
* @{
*
* \addtogroup weakref ECMA WeakRef object built-in
* @{
*/
/**
* Handle calling [[Call]] of built-in WeakRef object
*
* @return ecma value
*/
ecma_value_t
ecma_builtin_weakref_dispatch_call (const ecma_value_t *arguments_list_p, /**< arguments list */
uint32_t arguments_list_len) /**< number of arguments */
{
JERRY_ASSERT (arguments_list_len == 0 || arguments_list_p != NULL);
return ecma_raise_type_error (ECMA_ERR_MSG ("Constructor WeakRef requires 'new'."));
} /* ecma_builtin_weakref_dispatch_call */
/**
* Handle calling [[Construct]] of built-in WeakRef object
*
* @return ecma value
*/
ecma_value_t
ecma_builtin_weakref_dispatch_construct (const ecma_value_t *arguments_list_p, /**< arguments list */
uint32_t arguments_list_len) /**< number of arguments */
{
if (arguments_list_len == 0 || !ecma_is_value_object (arguments_list_p[0]))
{
return ecma_raise_type_error (ECMA_ERR_MSG ("WeakRef target must be an object"));
}
JERRY_ASSERT (JERRY_CONTEXT (current_new_target_p) != NULL);
ecma_object_t *proto_p = ecma_op_get_prototype_from_constructor (JERRY_CONTEXT (current_new_target_p),
ECMA_BUILTIN_ID_WEAKREF_PROTOTYPE);
if (JERRY_UNLIKELY (proto_p == NULL))
{
return ECMA_VALUE_ERROR;
}
ecma_object_t *object_p = ecma_create_object (proto_p,
sizeof (ecma_extended_object_t),
ECMA_OBJECT_TYPE_CLASS);
ecma_deref_object (proto_p);
ecma_extended_object_t *ext_obj_p = (ecma_extended_object_t *) object_p;
ext_obj_p->u.class_prop.class_id = (uint16_t) LIT_MAGIC_STRING_WEAKREF_UL;
ext_obj_p->u.class_prop.u.target = arguments_list_p[0];
ecma_op_object_set_weak (ecma_get_object_from_value (arguments_list_p[0]), object_p);
return ecma_make_object_value (object_p);
} /* ecma_builtin_weakref_dispatch_construct */
/**
* @}
* @}
* @}
*/
#endif /* JERRY_BUILTIN_WEAKREF */
@@ -0,0 +1,44 @@
/* 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.
*/
/*
* WeakRef built-in description
*/
#include "ecma-builtin-helpers-macro-defines.inc.h"
#if JERRY_BUILTIN_WEAKREF
/* Number properties:
* (property name, number value, writable, enumerable, configurable) */
NUMBER_VALUE (LIT_MAGIC_STRING_LENGTH,
1,
ECMA_PROPERTY_FLAG_CONFIGURABLE)
STRING_VALUE (LIT_MAGIC_STRING_NAME,
LIT_MAGIC_STRING_WEAKREF_UL,
ECMA_PROPERTY_FLAG_CONFIGURABLE)
/* Object properties:
* (property name, object pointer getter) */
OBJECT_VALUE (LIT_MAGIC_STRING_PROTOTYPE,
ECMA_BUILTIN_ID_WEAKREF_PROTOTYPE,
ECMA_PROPERTY_FIXED)
#endif /* JERRY_BUILTIN_WEAKREF */
#include "ecma-builtin-helpers-macro-undefs.inc.h"
@@ -565,7 +565,25 @@ BUILTIN_ROUTINE (ECMA_BUILTIN_ID_WEAKSET,
#endif /* JERRY_BUILTIN_WEAKSET */
#if JERRY_BUILTIN_PROXY
#if JERRY_BUILTIN_WEAKREF
/* The WeakRef prototype object */
BUILTIN (ECMA_BUILTIN_ID_WEAKREF_PROTOTYPE,
ECMA_OBJECT_TYPE_GENERAL,
ECMA_BUILTIN_ID_OBJECT_PROTOTYPE,
true,
weakref_prototype)
/* The WeakRef routine */
BUILTIN_ROUTINE (ECMA_BUILTIN_ID_WEAKREF,
ECMA_OBJECT_TYPE_NATIVE_FUNCTION,
ECMA_BUILTIN_ID_FUNCTION_PROTOTYPE,
true,
weakref)
#endif /* JERRY_BUILTIN_WEAKREF */
#if (JERRY_BUILTIN_PROXY)
/* The Proxy routine (ECMA-262 v6, 26.2.1) */
BUILTIN_ROUTINE (ECMA_BUILTIN_ID_PROXY,
ECMA_OBJECT_TYPE_NATIVE_FUNCTION,