From 281635cb40648103b538e33611e2a158e598def3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Csaba=20Osztrogon=C3=A1c?= Date: Mon, 22 Jun 2020 16:31:13 +0200 Subject: [PATCH] Fix the types of builtin prototype objects (#3922) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ES2015 spec made an incompatible change, many builtin prototypes became no longer valid instances of their respective classes. But unfortunately it broke the web, so ES2016 reverted String, Boolean and Number prototype to the original behaviour (ES5.1). JerryScript should match the latest spec. references: - ES11 19.3.3 Properties of the Boolean Prototype Object - ES11 20.1.3 Properties of the Number Prototype Object - ES11 21.1.3 Properties of the String Prototype Object JerryScript-DCO-1.0-Signed-off-by: Csaba Osztrogonác oszi@inf.u-szeged.hu --- .../ecma/builtin-objects/ecma-builtins.c | 2 +- .../ecma/builtin-objects/ecma-builtins.inc.h | 75 ++++++------------- jerry-core/ecma/operations/ecma-objects.c | 3 - tests/jerry/builtin-prototypes.js | 23 ++++++ tests/jerry/es.next/builtin-prototypes.js | 24 ------ tests/jerry/es5.1/builtin-prototypes.js | 9 --- tests/test262-es6-excludelist.xml | 54 ------------- 7 files changed, 45 insertions(+), 145 deletions(-) create mode 100644 tests/jerry/builtin-prototypes.js diff --git a/jerry-core/ecma/builtin-objects/ecma-builtins.c b/jerry-core/ecma/builtin-objects/ecma-builtins.c index 8b3037a31..8ffc75feb 100644 --- a/jerry-core/ecma/builtin-objects/ecma-builtins.c +++ b/jerry-core/ecma/builtin-objects/ecma-builtins.c @@ -446,7 +446,6 @@ ecma_instantiate_builtin (ecma_builtin_id_t obj_builtin_id) /**< built-in id */ } #endif /* ENABLED (JERRY_BUILTIN_ARRAY) */ -#if !ENABLED (JERRY_ESNEXT) #if ENABLED (JERRY_BUILTIN_STRING) case ECMA_BUILTIN_ID_STRING_PROTOTYPE: { @@ -483,6 +482,7 @@ ecma_instantiate_builtin (ecma_builtin_id_t obj_builtin_id) /**< built-in id */ } #endif /* ENABLED (JERRY_BUILTIN_BOOLEAN) */ +#if !ENABLED (JERRY_ESNEXT) #if ENABLED (JERRY_BUILTIN_DATE) case ECMA_BUILTIN_ID_DATE_PROTOTYPE: { diff --git a/jerry-core/ecma/builtin-objects/ecma-builtins.inc.h b/jerry-core/ecma/builtin-objects/ecma-builtins.inc.h index 081be2106..f94190fb8 100644 --- a/jerry-core/ecma/builtin-objects/ecma-builtins.inc.h +++ b/jerry-core/ecma/builtin-objects/ecma-builtins.inc.h @@ -47,33 +47,6 @@ BUILTIN_ROUTINE (ECMA_BUILTIN_ID_ARRAY, #endif /* ENABLED (JERRY_BUILTIN_ARRAY) */ #if ENABLED (JERRY_ESNEXT) -# if ENABLED (JERRY_BUILTIN_STRING) -/* The String.prototype object (21.1.3) */ -BUILTIN (ECMA_BUILTIN_ID_STRING_PROTOTYPE, - ECMA_OBJECT_TYPE_GENERAL, - ECMA_BUILTIN_ID_OBJECT_PROTOTYPE, - true, - string_prototype) -# endif /* ENABLED (JERRY_BUILTIN_STRING) */ - -# if ENABLED (JERRY_BUILTIN_BOOLEAN) -/* The Boolean.prototype object (19.3.3) */ -BUILTIN (ECMA_BUILTIN_ID_BOOLEAN_PROTOTYPE, - ECMA_OBJECT_TYPE_GENERAL, - ECMA_BUILTIN_ID_OBJECT_PROTOTYPE, - true, - boolean_prototype) -# endif /* ENABLED (JERRY_BUILTIN_BOOLEAN) */ - -# if ENABLED (JERRY_BUILTIN_NUMBER) -/* The Number.prototype object (20.1.3) */ -BUILTIN (ECMA_BUILTIN_ID_NUMBER_PROTOTYPE, - ECMA_OBJECT_TYPE_GENERAL, - ECMA_BUILTIN_ID_OBJECT_PROTOTYPE, - true, - number_prototype) -# endif /* ENABLED (JERRY_BUILTIN_NUMBER) */ - # if ENABLED (JERRY_BUILTIN_DATE) /* The Date.prototype object (20.3.4) */ BUILTIN (ECMA_BUILTIN_ID_DATE_PROTOTYPE, @@ -92,33 +65,6 @@ BUILTIN (ECMA_BUILTIN_ID_REGEXP_PROTOTYPE, regexp_prototype) # endif /* ENABLED (JERRY_BUILTIN_REGEXP) */ #else /* !ENABLED (JERRY_ESNEXT) */ -# if ENABLED (JERRY_BUILTIN_STRING) -/* The String.prototype object (15.5.4) */ -BUILTIN (ECMA_BUILTIN_ID_STRING_PROTOTYPE, - ECMA_OBJECT_TYPE_CLASS, - ECMA_BUILTIN_ID_OBJECT_PROTOTYPE, - true, - string_prototype) -# endif /* ENABLED (JERRY_BUILTIN_STRING) */ - -# if ENABLED (JERRY_BUILTIN_BOOLEAN) -/* The Boolean.prototype object (15.6.4) */ -BUILTIN (ECMA_BUILTIN_ID_BOOLEAN_PROTOTYPE, - ECMA_OBJECT_TYPE_CLASS, - ECMA_BUILTIN_ID_OBJECT_PROTOTYPE, - true, - boolean_prototype) -# endif /* ENABLED (JERRY_BUILTIN_BOOLEAN) */ - -# if ENABLED (JERRY_BUILTIN_NUMBER) -/* The Number.prototype object (15.7.4) */ -BUILTIN (ECMA_BUILTIN_ID_NUMBER_PROTOTYPE, - ECMA_OBJECT_TYPE_CLASS, - ECMA_BUILTIN_ID_OBJECT_PROTOTYPE, - true, - number_prototype) -# endif /* ENABLED (JERRY_BUILTIN_NUMBER) */ - # if ENABLED (JERRY_BUILTIN_DATE) /* The Date.prototype object (15.9.4) */ BUILTIN (ECMA_BUILTIN_ID_DATE_PROTOTYPE, @@ -139,6 +85,13 @@ BUILTIN (ECMA_BUILTIN_ID_REGEXP_PROTOTYPE, #endif /* !ENABLED (JERRY_ESNEXT) */ #if ENABLED (JERRY_BUILTIN_STRING) +/* The String.prototype object (15.5.4) */ +BUILTIN (ECMA_BUILTIN_ID_STRING_PROTOTYPE, + ECMA_OBJECT_TYPE_CLASS, + ECMA_BUILTIN_ID_OBJECT_PROTOTYPE, + true, + string_prototype) + /* The String object (15.5.1) */ BUILTIN_ROUTINE (ECMA_BUILTIN_ID_STRING, ECMA_OBJECT_TYPE_FUNCTION, @@ -148,6 +101,13 @@ BUILTIN_ROUTINE (ECMA_BUILTIN_ID_STRING, #endif /* ENABLED (JERRY_BUILTIN_STRING) */ #if ENABLED (JERRY_BUILTIN_BOOLEAN) +/* The Boolean.prototype object (15.6.4) */ +BUILTIN (ECMA_BUILTIN_ID_BOOLEAN_PROTOTYPE, + ECMA_OBJECT_TYPE_CLASS, + ECMA_BUILTIN_ID_OBJECT_PROTOTYPE, + true, + boolean_prototype) + /* The Boolean object (15.6.1) */ BUILTIN_ROUTINE (ECMA_BUILTIN_ID_BOOLEAN, ECMA_OBJECT_TYPE_FUNCTION, @@ -157,6 +117,13 @@ BUILTIN_ROUTINE (ECMA_BUILTIN_ID_BOOLEAN, #endif /* ENABLED (JERRY_BUILTIN_BOOLEAN) */ #if ENABLED (JERRY_BUILTIN_NUMBER) +/* The Number.prototype object (15.7.4) */ +BUILTIN (ECMA_BUILTIN_ID_NUMBER_PROTOTYPE, + ECMA_OBJECT_TYPE_CLASS, + ECMA_BUILTIN_ID_OBJECT_PROTOTYPE, + true, + number_prototype) + /* The Number object (15.7.1) */ BUILTIN_ROUTINE (ECMA_BUILTIN_ID_NUMBER, ECMA_OBJECT_TYPE_FUNCTION, diff --git a/jerry-core/ecma/operations/ecma-objects.c b/jerry-core/ecma/operations/ecma-objects.c index cf1ebfeaf..394637ee4 100644 --- a/jerry-core/ecma/operations/ecma-objects.c +++ b/jerry-core/ecma/operations/ecma-objects.c @@ -2648,9 +2648,6 @@ ecma_object_check_class_name_is_object (ecma_object_t *obj_p) /**< object */ || ecma_builtin_is (obj_p, ECMA_BUILTIN_ID_TYPE_ERROR_PROTOTYPE) || ecma_builtin_is (obj_p, ECMA_BUILTIN_ID_URI_ERROR_PROTOTYPE) || ecma_builtin_is (obj_p, ECMA_BUILTIN_ID_ERROR_PROTOTYPE) - || ecma_builtin_is (obj_p, ECMA_BUILTIN_ID_STRING_PROTOTYPE) - || ecma_builtin_is (obj_p, ECMA_BUILTIN_ID_BOOLEAN_PROTOTYPE) - || ecma_builtin_is (obj_p, ECMA_BUILTIN_ID_NUMBER_PROTOTYPE) || ecma_builtin_is (obj_p, ECMA_BUILTIN_ID_DATE_PROTOTYPE) || ecma_builtin_is (obj_p, ECMA_BUILTIN_ID_REGEXP_PROTOTYPE) || ecma_builtin_is (obj_p, ECMA_BUILTIN_ID_SYMBOL_PROTOTYPE) diff --git a/tests/jerry/builtin-prototypes.js b/tests/jerry/builtin-prototypes.js new file mode 100644 index 000000000..b73b701e4 --- /dev/null +++ b/tests/jerry/builtin-prototypes.js @@ -0,0 +1,23 @@ +/* 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. + */ + +assert (Object.prototype.toString.call (String.prototype) === '[object String]'); +assert (String.prototype.toString() === ""); + +assert (Object.prototype.toString.call (Boolean.prototype) === '[object Boolean]'); +assert (Boolean.prototype.valueOf() === false); + +assert (Object.prototype.toString.call (Number.prototype) === '[object Number]'); +assert (Number.prototype.valueOf() === 0); diff --git a/tests/jerry/es.next/builtin-prototypes.js b/tests/jerry/es.next/builtin-prototypes.js index 0a852d0e4..3266c5a94 100644 --- a/tests/jerry/es.next/builtin-prototypes.js +++ b/tests/jerry/es.next/builtin-prototypes.js @@ -14,9 +14,6 @@ */ var prototypes = [ - String.prototype, - Boolean.prototype, - Number.prototype, Date.prototype, RegExp.prototype, Error.prototype, @@ -32,27 +29,6 @@ for (proto of prototypes) { assert (Object.prototype.toString.call (proto) === '[object Object]'); } -try { - String.prototype.toString(); - assert (false); -} catch (e) { - assert (e instanceof TypeError); -} - -try { - Boolean.prototype.valueOf(); - assert (false); -} catch (e) { - assert (e instanceof TypeError); -} - -try { - Number.prototype.valueOf(); - assert (false); -} catch (e) { - assert (e instanceof TypeError); -} - try { Date.prototype.valueOf(); assert (false); diff --git a/tests/jerry/es5.1/builtin-prototypes.js b/tests/jerry/es5.1/builtin-prototypes.js index c4be5bfed..c43ec439e 100644 --- a/tests/jerry/es5.1/builtin-prototypes.js +++ b/tests/jerry/es5.1/builtin-prototypes.js @@ -13,15 +13,6 @@ * limitations under the License. */ -assert (Object.prototype.toString.call (String.prototype) === '[object String]'); -assert (String.prototype.toString() === ""); - -assert (Object.prototype.toString.call (Boolean.prototype) === '[object Boolean]'); -assert (Boolean.prototype.valueOf() === false); - -assert (Object.prototype.toString.call (Number.prototype) === '[object Number]'); -assert (Number.prototype.valueOf() === 0); - assert (Object.prototype.toString.call (Date.prototype) === '[object Date]'); assert (isNaN(Date.prototype.valueOf())); diff --git a/tests/test262-es6-excludelist.xml b/tests/test262-es6-excludelist.xml index b3478cf8c..582256876 100644 --- a/tests/test262-es6-excludelist.xml +++ b/tests/test262-es6-excludelist.xml @@ -28,12 +28,6 @@ - - - - - - @@ -44,51 +38,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -151,9 +100,6 @@ - - -