[runtime] Cleanup native methods creation in js/weak-collection.js.

This CL replaces usages of utils.InstallFunctions and utils.InstallGetter()
with the DEFINE_METHOD* macros that ensure that the native function is
created in proper form from the beginning. Thus the function will not
require further reconfiguring like adding a computed name or removing of
'prototype' property.

This CL is one of a series of cleanup CL which are the preliminary steps for
improving function closures creation.

Bug: v8:6459
Change-Id: If5b1733454f10aef5da7f335273c632e7eabb728
Reviewed-on: https://chromium-review.googlesource.com/548077
Reviewed-by: Toon Verwaest <verwaest@chromium.org>
Commit-Queue: Igor Sheludko <ishell@chromium.org>
Cr-Commit-Position: refs/heads/master@{#46335}
This commit is contained in:
Igor Sheludko 2017-06-29 16:21:59 +02:00 committed by Commit Bot
parent e2862ab586
commit a4694a42bb
2 changed files with 107 additions and 113 deletions

View File

@ -332,11 +332,11 @@ void InstallFunction(Handle<JSObject> target, Handle<JSFunction> function,
Handle<JSFunction> CreateFunction(Isolate* isolate, Handle<String> name,
InstanceType type, int instance_size,
MaybeHandle<JSObject> maybe_prototype,
MaybeHandle<Object> maybe_prototype,
Builtins::Name call) {
Factory* factory = isolate->factory();
Handle<Code> call_code(isolate->builtins()->builtin(call));
Handle<JSObject> prototype;
Handle<Object> prototype;
Handle<JSFunction> result =
maybe_prototype.ToHandle(&prototype)
? factory->NewFunction(name, call_code, prototype, type,
@ -348,7 +348,7 @@ Handle<JSFunction> CreateFunction(Isolate* isolate, Handle<String> name,
Handle<JSFunction> InstallFunction(Handle<JSObject> target, Handle<Name> name,
InstanceType type, int instance_size,
MaybeHandle<JSObject> maybe_prototype,
MaybeHandle<Object> maybe_prototype,
Builtins::Name call,
PropertyAttributes attributes) {
Handle<String> name_string = Name::ToFunctionName(name).ToHandleChecked();
@ -361,7 +361,7 @@ Handle<JSFunction> InstallFunction(Handle<JSObject> target, Handle<Name> name,
Handle<JSFunction> InstallFunction(Handle<JSObject> target, const char* name,
InstanceType type, int instance_size,
MaybeHandle<JSObject> maybe_prototype,
MaybeHandle<Object> maybe_prototype,
Builtins::Name call) {
Factory* const factory = target->GetIsolate()->factory();
PropertyAttributes attributes = DONT_ENUM;
@ -3122,19 +3122,35 @@ void Genesis::InitializeGlobal(Handle<JSGlobalObject> global_object,
}
{ // -- W e a k M a p
Handle<JSFunction> js_weak_map_fun = InstallFunction(
global, "WeakMap", JS_WEAK_MAP_TYPE, JSWeakMap::kSize,
isolate->initial_object_prototype(), Builtins::kIllegal);
Handle<JSFunction> js_weak_map_fun =
InstallFunction(global, "WeakMap", JS_WEAK_MAP_TYPE, JSWeakMap::kSize,
factory->the_hole_value(), Builtins::kIllegal);
InstallWithIntrinsicDefaultProto(isolate, js_weak_map_fun,
Context::JS_WEAK_MAP_FUN_INDEX);
// Setup %WeakMapPrototype%.
Handle<JSObject> prototype(JSObject::cast(js_weak_map_fun->prototype()),
isolate);
JSObject::AddProperty(
prototype, factory->to_string_tag_symbol(),
factory->NewStringFromAsciiChecked("WeakMap"),
static_cast<PropertyAttributes>(DONT_ENUM | READ_ONLY));
}
{ // -- W e a k S e t
Handle<JSFunction> js_weak_set_fun = InstallFunction(
global, "WeakSet", JS_WEAK_SET_TYPE, JSWeakSet::kSize,
isolate->initial_object_prototype(), Builtins::kIllegal);
Handle<JSFunction> js_weak_set_fun =
InstallFunction(global, "WeakSet", JS_WEAK_SET_TYPE, JSWeakSet::kSize,
factory->the_hole_value(), Builtins::kIllegal);
InstallWithIntrinsicDefaultProto(isolate, js_weak_set_fun,
Context::JS_WEAK_SET_FUN_INDEX);
// Setup %WeakSetPrototype%.
Handle<JSObject> prototype(JSObject::cast(js_weak_set_fun->prototype()),
isolate);
JSObject::AddProperty(
prototype, factory->to_string_tag_symbol(),
factory->NewStringFromAsciiChecked("WeakSet"),
static_cast<PropertyAttributes>(DONT_ENUM | READ_ONLY));
}
{ // -- P r o x y

View File

@ -13,10 +13,8 @@
var GetExistingHash;
var GetHash;
var GlobalObject = global.Object;
var GlobalWeakMap = global.WeakMap;
var GlobalWeakSet = global.WeakSet;
var toStringTagSymbol = utils.ImportNow("to_string_tag_symbol");
utils.Import(function(from) {
GetExistingHash = from.GetExistingHash;
@ -48,69 +46,58 @@ function WeakMapConstructor(iterable) {
}
function WeakMapGet(key) {
if (!IS_WEAKMAP(this)) {
throw %make_type_error(kIncompatibleMethodReceiver,
'WeakMap.prototype.get', this);
// Set up the non-enumerable functions on the WeakMap prototype object.
DEFINE_METHODS(
GlobalWeakMap.prototype,
{
get(key) {
if (!IS_WEAKMAP(this)) {
throw %make_type_error(kIncompatibleMethodReceiver,
'WeakMap.prototype.get', this);
}
if (!IS_RECEIVER(key)) return UNDEFINED;
var hash = GetExistingHash(key);
if (IS_UNDEFINED(hash)) return UNDEFINED;
return %WeakCollectionGet(this, key, hash);
}
set(key, value) {
if (!IS_WEAKMAP(this)) {
throw %make_type_error(kIncompatibleMethodReceiver,
'WeakMap.prototype.set', this);
}
if (!IS_RECEIVER(key)) throw %make_type_error(kInvalidWeakMapKey);
return %WeakCollectionSet(this, key, value, GetHash(key));
}
has(key) {
if (!IS_WEAKMAP(this)) {
throw %make_type_error(kIncompatibleMethodReceiver,
'WeakMap.prototype.has', this);
}
if (!IS_RECEIVER(key)) return false;
var hash = GetExistingHash(key);
if (IS_UNDEFINED(hash)) return false;
return %WeakCollectionHas(this, key, hash);
}
delete(key) {
if (!IS_WEAKMAP(this)) {
throw %make_type_error(kIncompatibleMethodReceiver,
'WeakMap.prototype.delete', this);
}
if (!IS_RECEIVER(key)) return false;
var hash = GetExistingHash(key);
if (IS_UNDEFINED(hash)) return false;
return %WeakCollectionDelete(this, key, hash);
}
}
if (!IS_RECEIVER(key)) return UNDEFINED;
var hash = GetExistingHash(key);
if (IS_UNDEFINED(hash)) return UNDEFINED;
return %WeakCollectionGet(this, key, hash);
}
function WeakMapSet(key, value) {
if (!IS_WEAKMAP(this)) {
throw %make_type_error(kIncompatibleMethodReceiver,
'WeakMap.prototype.set', this);
}
if (!IS_RECEIVER(key)) throw %make_type_error(kInvalidWeakMapKey);
return %WeakCollectionSet(this, key, value, GetHash(key));
}
function WeakMapHas(key) {
if (!IS_WEAKMAP(this)) {
throw %make_type_error(kIncompatibleMethodReceiver,
'WeakMap.prototype.has', this);
}
if (!IS_RECEIVER(key)) return false;
var hash = GetExistingHash(key);
if (IS_UNDEFINED(hash)) return false;
return %WeakCollectionHas(this, key, hash);
}
function WeakMapDelete(key) {
if (!IS_WEAKMAP(this)) {
throw %make_type_error(kIncompatibleMethodReceiver,
'WeakMap.prototype.delete', this);
}
if (!IS_RECEIVER(key)) return false;
var hash = GetExistingHash(key);
if (IS_UNDEFINED(hash)) return false;
return %WeakCollectionDelete(this, key, hash);
}
);
// -------------------------------------------------------------------
%SetCode(GlobalWeakMap, WeakMapConstructor);
%FunctionSetLength(GlobalWeakMap, 0);
%FunctionSetPrototype(GlobalWeakMap, new GlobalObject());
%AddNamedProperty(GlobalWeakMap.prototype, "constructor", GlobalWeakMap,
DONT_ENUM);
%AddNamedProperty(GlobalWeakMap.prototype, toStringTagSymbol, "WeakMap",
DONT_ENUM | READ_ONLY);
// Set up the non-enumerable functions on the WeakMap prototype object.
utils.InstallFunctions(GlobalWeakMap.prototype, DONT_ENUM, [
"get", WeakMapGet,
"set", WeakMapSet,
"has", WeakMapHas,
"delete", WeakMapDelete
]);
// -------------------------------------------------------------------
// Harmony WeakSet
@ -134,55 +121,46 @@ function WeakSetConstructor(iterable) {
}
function WeakSetAdd(value) {
if (!IS_WEAKSET(this)) {
throw %make_type_error(kIncompatibleMethodReceiver,
'WeakSet.prototype.add', this);
// Set up the non-enumerable functions on the WeakSet prototype object.
DEFINE_METHODS(
GlobalWeakSet.prototype,
{
add(value) {
if (!IS_WEAKSET(this)) {
throw %make_type_error(kIncompatibleMethodReceiver,
'WeakSet.prototype.add', this);
}
if (!IS_RECEIVER(value)) throw %make_type_error(kInvalidWeakSetValue);
return %WeakCollectionSet(this, value, true, GetHash(value));
}
has(value) {
if (!IS_WEAKSET(this)) {
throw %make_type_error(kIncompatibleMethodReceiver,
'WeakSet.prototype.has', this);
}
if (!IS_RECEIVER(value)) return false;
var hash = GetExistingHash(value);
if (IS_UNDEFINED(hash)) return false;
return %WeakCollectionHas(this, value, hash);
}
delete(value) {
if (!IS_WEAKSET(this)) {
throw %make_type_error(kIncompatibleMethodReceiver,
'WeakSet.prototype.delete', this);
}
if (!IS_RECEIVER(value)) return false;
var hash = GetExistingHash(value);
if (IS_UNDEFINED(hash)) return false;
return %WeakCollectionDelete(this, value, hash);
}
}
if (!IS_RECEIVER(value)) throw %make_type_error(kInvalidWeakSetValue);
return %WeakCollectionSet(this, value, true, GetHash(value));
}
function WeakSetHas(value) {
if (!IS_WEAKSET(this)) {
throw %make_type_error(kIncompatibleMethodReceiver,
'WeakSet.prototype.has', this);
}
if (!IS_RECEIVER(value)) return false;
var hash = GetExistingHash(value);
if (IS_UNDEFINED(hash)) return false;
return %WeakCollectionHas(this, value, hash);
}
function WeakSetDelete(value) {
if (!IS_WEAKSET(this)) {
throw %make_type_error(kIncompatibleMethodReceiver,
'WeakSet.prototype.delete', this);
}
if (!IS_RECEIVER(value)) return false;
var hash = GetExistingHash(value);
if (IS_UNDEFINED(hash)) return false;
return %WeakCollectionDelete(this, value, hash);
}
);
// -------------------------------------------------------------------
%SetCode(GlobalWeakSet, WeakSetConstructor);
%FunctionSetLength(GlobalWeakSet, 0);
%FunctionSetPrototype(GlobalWeakSet, new GlobalObject());
%AddNamedProperty(GlobalWeakSet.prototype, "constructor", GlobalWeakSet,
DONT_ENUM);
%AddNamedProperty(GlobalWeakSet.prototype, toStringTagSymbol, "WeakSet",
DONT_ENUM | READ_ONLY);
// Set up the non-enumerable functions on the WeakSet prototype object.
utils.InstallFunctions(GlobalWeakSet.prototype, DONT_ENUM, [
"add", WeakSetAdd,
"has", WeakSetHas,
"delete", WeakSetDelete
]);
})