Commit fb5cbc2e authored by littledan's avatar littledan Committed by Commit bot

Add a --harmony-species flag, defining @@species on constructors

This patch adds the basis for subclassing TypedArrays, Arrays and
ArrayBuffers through the @@species hook, added in ES2015. This is
the first patch in a series. This patch simply defines the
@@species Symbol and installs it on the appropriate constructors.
The behavior is guarded behind the --harmony-species flag.

R=cbruni
BUG=v8:4093
LOG=Y
CQ_INCLUDE_TRYBOTS=tryserver.chromium.linux:linux_chromium_rel_ng;tryserver.blink:linux_blink_rel
TBR=hpayer@chromium.org

Review URL: https://codereview.chromium.org/1558543002

Cr-Commit-Position: refs/heads/master@{#33095}
parent 4f947115
......@@ -286,6 +286,7 @@ action("js2c_experimental") {
"src/js/harmony-object-observe.js",
"src/js/harmony-sharedarraybuffer.js",
"src/js/harmony-simd.js",
"src/js/harmony-species.js",
"src/js/harmony-unicode-regexps.js",
"src/js/promise-extra.js"
]
......
......@@ -2145,6 +2145,7 @@ void Bootstrapper::ExportExperimentalFromRuntime(Isolate* isolate,
INITIALIZE_FLAG(FLAG_harmony_tostring)
INITIALIZE_FLAG(FLAG_harmony_tolength)
INITIALIZE_FLAG(FLAG_harmony_species)
#undef INITIALIZE_FLAG
}
......@@ -2388,6 +2389,13 @@ Handle<JSFunction> Genesis::InstallArrayBuffer(Handle<JSObject> target,
}
void Genesis::InitializeGlobal_harmony_species() {
if (!FLAG_harmony_species) return;
InstallPublicSymbol(factory(), native_context(), "species",
factory()->species_symbol());
}
Handle<JSFunction> Genesis::InstallInternalArray(Handle<JSObject> target,
const char* name,
ElementsKind elements_kind) {
......@@ -2719,6 +2727,8 @@ bool Genesis::InstallExperimentalNatives() {
static const char* harmony_sloppy_natives[] = {nullptr};
static const char* harmony_sloppy_function_natives[] = {nullptr};
static const char* harmony_sloppy_let_natives[] = {nullptr};
static const char* harmony_species_natives[] = {"native harmony-species.js",
nullptr};
static const char* harmony_unicode_regexps_natives[] = {
"native harmony-unicode-regexps.js", nullptr};
static const char* harmony_default_parameters_natives[] = {nullptr};
......
......@@ -198,14 +198,15 @@ DEFINE_IMPLICATION(es_staging, harmony_regexp_lookbehind)
DEFINE_IMPLICATION(es_staging, move_object_start)
// Features that are still work in progress (behind individual flags).
#define HARMONY_INPROGRESS(V) \
V(harmony_modules, "harmony modules") \
V(harmony_unicode_regexps, "harmony unicode regexps") \
V(harmony_function_name, "harmony Function name inference") \
V(harmony_sharedarraybuffer, "harmony sharedarraybuffer") \
V(harmony_simd, "harmony simd") \
V(harmony_do_expressions, "harmony do-expressions") \
V(harmony_regexp_subclass, "harmony regexp subclassing")
#define HARMONY_INPROGRESS(V) \
V(harmony_modules, "harmony modules") \
V(harmony_unicode_regexps, "harmony unicode regexps") \
V(harmony_function_name, "harmony Function name inference") \
V(harmony_sharedarraybuffer, "harmony sharedarraybuffer") \
V(harmony_simd, "harmony simd") \
V(harmony_do_expressions, "harmony do-expressions") \
V(harmony_regexp_subclass, "harmony regexp subclassing") \
V(harmony_species, "harmony Symbol.species")
// Features that are complete (but still behind --harmony/es-staging flag).
#define HARMONY_STAGED(V) \
......
......@@ -379,14 +379,15 @@ namespace internal {
V(strong_function_transition_symbol) \
V(uninitialized_symbol)
#define PUBLIC_SYMBOL_LIST(V) \
V(has_instance_symbol, Symbol.hasInstance) \
V(iterator_symbol, Symbol.iterator) \
V(match_symbol, Symbol.match) \
V(replace_symbol, Symbol.replace) \
V(search_symbol, Symbol.search) \
V(split_symbol, Symbol.split) \
V(to_primitive_symbol, Symbol.toPrimitive) \
#define PUBLIC_SYMBOL_LIST(V) \
V(has_instance_symbol, Symbol.hasInstance) \
V(iterator_symbol, Symbol.iterator) \
V(match_symbol, Symbol.match) \
V(replace_symbol, Symbol.replace) \
V(search_symbol, Symbol.search) \
V(species_symbol, Symbol.species) \
V(split_symbol, Symbol.split) \
V(to_primitive_symbol, Symbol.toPrimitive) \
V(unscopables_symbol, Symbol.unscopables)
// Well-Known Symbols are "Public" symbols, which have a bit set which causes
......
// Copyright 2015 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
(function(global, utils, extrasUtils) {
"use strict";
%CheckIsBootstrapping();
var GlobalArray = global.Array;
// It is important that this file is run after src/js/typedarray.js,
// otherwise GlobalTypedArray would be Object, and we would break
// old versions of Zepto.
var GlobalTypedArray = global.Uint8Array.__proto__;
var GlobalMap = global.Map;
var GlobalSet = global.Set;
var GlobalArrayBuffer = global.ArrayBuffer;
var GlobalPromise = global.Promise;
var GlobalRegExp = global.RegExp;
var speciesSymbol = utils.ImportNow("species_symbol");
function ArraySpecies() {
return this;
}
function TypedArraySpecies() {
return this;
}
function MapSpecies() {
return this;
}
function SetSpecies() {
return this;
}
function ArrayBufferSpecies() {
return this;
}
function PromiseSpecies() {
return this;
}
function RegExpSpecies() {
return this;
}
utils.InstallGetter(GlobalArray, speciesSymbol, ArraySpecies, DONT_ENUM);
utils.InstallGetter(GlobalTypedArray, speciesSymbol, TypedArraySpecies, DONT_ENUM);
utils.InstallGetter(GlobalMap, speciesSymbol, MapSpecies, DONT_ENUM);
utils.InstallGetter(GlobalSet, speciesSymbol, SetSpecies, DONT_ENUM);
utils.InstallGetter(GlobalArrayBuffer, speciesSymbol, ArrayBufferSpecies,
DONT_ENUM);
utils.InstallGetter(GlobalPromise, speciesSymbol, PromiseSpecies, DONT_ENUM);
utils.InstallGetter(GlobalRegExp, speciesSymbol, RegExpSpecies, DONT_ENUM);
});
......@@ -221,6 +221,7 @@ function PostNatives(utils) {
"regexp_flags_symbol",
"to_string_tag_symbol",
"object_to_string",
"species_symbol",
];
var filtered_exports = {};
......
// Copyright 2015 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Flags: --harmony-species
// Test the ES2015 @@species feature
'use strict';
let TypedArray = Uint8Array.__proto__;
// The @@species property exists on the right objects and has the right values
let classesWithSpecies = [RegExp, Array, TypedArray, ArrayBuffer, Map, Set, Promise];
let classesWithoutSpecies = [Object, Function, String, Number, Symbol, WeakMap, WeakSet];
for (let constructor of classesWithSpecies) {
assertEquals(constructor, constructor[Symbol.species]);
assertThrows(function() { constructor[Symbol.species] = undefined }, TypeError);
let descriptor = Object.getOwnPropertyDescriptor(constructor, Symbol.species);
assertTrue(descriptor.configurable);
assertFalse(descriptor.enumerable);
assertEquals(undefined, descriptor.writable);
assertEquals(undefined, descriptor.set);
assertEquals('function', typeof descriptor.get);
}
// @@species is defined with distinct getters
assertEquals(classesWithSpecies.length,
new Set(classesWithSpecies.map(constructor =>
Object.getOwnPropertyDescriptor(
constructor, Symbol.species).get)
).size);
for (let constructor of classesWithoutSpecies)
assertEquals(undefined, constructor[Symbol.species]);
......@@ -1927,6 +1927,7 @@
'../../src/js/harmony-object-observe.js',
'../../src/js/harmony-sharedarraybuffer.js',
'../../src/js/harmony-simd.js',
'../../src/js/harmony-species.js',
'../../src/js/harmony-unicode-regexps.js',
'../../src/js/promise-extra.js',
],
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment