Commit a4694a42 authored by Igor Sheludko's avatar Igor Sheludko Committed by Commit Bot

[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/548077Reviewed-by: 's avatarToon Verwaest <verwaest@chromium.org>
Commit-Queue: Igor Sheludko <ishell@chromium.org>
Cr-Commit-Position: refs/heads/master@{#46335}
parent e2862ab5
...@@ -332,11 +332,11 @@ void InstallFunction(Handle<JSObject> target, Handle<JSFunction> function, ...@@ -332,11 +332,11 @@ void InstallFunction(Handle<JSObject> target, Handle<JSFunction> function,
Handle<JSFunction> CreateFunction(Isolate* isolate, Handle<String> name, Handle<JSFunction> CreateFunction(Isolate* isolate, Handle<String> name,
InstanceType type, int instance_size, InstanceType type, int instance_size,
MaybeHandle<JSObject> maybe_prototype, MaybeHandle<Object> maybe_prototype,
Builtins::Name call) { Builtins::Name call) {
Factory* factory = isolate->factory(); Factory* factory = isolate->factory();
Handle<Code> call_code(isolate->builtins()->builtin(call)); Handle<Code> call_code(isolate->builtins()->builtin(call));
Handle<JSObject> prototype; Handle<Object> prototype;
Handle<JSFunction> result = Handle<JSFunction> result =
maybe_prototype.ToHandle(&prototype) maybe_prototype.ToHandle(&prototype)
? factory->NewFunction(name, call_code, prototype, type, ? factory->NewFunction(name, call_code, prototype, type,
...@@ -348,7 +348,7 @@ Handle<JSFunction> CreateFunction(Isolate* isolate, Handle<String> name, ...@@ -348,7 +348,7 @@ Handle<JSFunction> CreateFunction(Isolate* isolate, Handle<String> name,
Handle<JSFunction> InstallFunction(Handle<JSObject> target, Handle<Name> name, Handle<JSFunction> InstallFunction(Handle<JSObject> target, Handle<Name> name,
InstanceType type, int instance_size, InstanceType type, int instance_size,
MaybeHandle<JSObject> maybe_prototype, MaybeHandle<Object> maybe_prototype,
Builtins::Name call, Builtins::Name call,
PropertyAttributes attributes) { PropertyAttributes attributes) {
Handle<String> name_string = Name::ToFunctionName(name).ToHandleChecked(); Handle<String> name_string = Name::ToFunctionName(name).ToHandleChecked();
...@@ -361,7 +361,7 @@ Handle<JSFunction> InstallFunction(Handle<JSObject> target, Handle<Name> name, ...@@ -361,7 +361,7 @@ Handle<JSFunction> InstallFunction(Handle<JSObject> target, Handle<Name> name,
Handle<JSFunction> InstallFunction(Handle<JSObject> target, const char* name, Handle<JSFunction> InstallFunction(Handle<JSObject> target, const char* name,
InstanceType type, int instance_size, InstanceType type, int instance_size,
MaybeHandle<JSObject> maybe_prototype, MaybeHandle<Object> maybe_prototype,
Builtins::Name call) { Builtins::Name call) {
Factory* const factory = target->GetIsolate()->factory(); Factory* const factory = target->GetIsolate()->factory();
PropertyAttributes attributes = DONT_ENUM; PropertyAttributes attributes = DONT_ENUM;
...@@ -3122,19 +3122,35 @@ void Genesis::InitializeGlobal(Handle<JSGlobalObject> global_object, ...@@ -3122,19 +3122,35 @@ void Genesis::InitializeGlobal(Handle<JSGlobalObject> global_object,
} }
{ // -- W e a k M a p { // -- W e a k M a p
Handle<JSFunction> js_weak_map_fun = InstallFunction( Handle<JSFunction> js_weak_map_fun =
global, "WeakMap", JS_WEAK_MAP_TYPE, JSWeakMap::kSize, InstallFunction(global, "WeakMap", JS_WEAK_MAP_TYPE, JSWeakMap::kSize,
isolate->initial_object_prototype(), Builtins::kIllegal); factory->the_hole_value(), Builtins::kIllegal);
InstallWithIntrinsicDefaultProto(isolate, js_weak_map_fun, InstallWithIntrinsicDefaultProto(isolate, js_weak_map_fun,
Context::JS_WEAK_MAP_FUN_INDEX); 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 { // -- W e a k S e t
Handle<JSFunction> js_weak_set_fun = InstallFunction( Handle<JSFunction> js_weak_set_fun =
global, "WeakSet", JS_WEAK_SET_TYPE, JSWeakSet::kSize, InstallFunction(global, "WeakSet", JS_WEAK_SET_TYPE, JSWeakSet::kSize,
isolate->initial_object_prototype(), Builtins::kIllegal); factory->the_hole_value(), Builtins::kIllegal);
InstallWithIntrinsicDefaultProto(isolate, js_weak_set_fun, InstallWithIntrinsicDefaultProto(isolate, js_weak_set_fun,
Context::JS_WEAK_SET_FUN_INDEX); 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 { // -- P r o x y
......
...@@ -13,10 +13,8 @@ ...@@ -13,10 +13,8 @@
var GetExistingHash; var GetExistingHash;
var GetHash; var GetHash;
var GlobalObject = global.Object;
var GlobalWeakMap = global.WeakMap; var GlobalWeakMap = global.WeakMap;
var GlobalWeakSet = global.WeakSet; var GlobalWeakSet = global.WeakSet;
var toStringTagSymbol = utils.ImportNow("to_string_tag_symbol");
utils.Import(function(from) { utils.Import(function(from) {
GetExistingHash = from.GetExistingHash; GetExistingHash = from.GetExistingHash;
...@@ -48,7 +46,11 @@ function WeakMapConstructor(iterable) { ...@@ -48,7 +46,11 @@ function WeakMapConstructor(iterable) {
} }
function WeakMapGet(key) { // Set up the non-enumerable functions on the WeakMap prototype object.
DEFINE_METHODS(
GlobalWeakMap.prototype,
{
get(key) {
if (!IS_WEAKMAP(this)) { if (!IS_WEAKMAP(this)) {
throw %make_type_error(kIncompatibleMethodReceiver, throw %make_type_error(kIncompatibleMethodReceiver,
'WeakMap.prototype.get', this); 'WeakMap.prototype.get', this);
...@@ -57,20 +59,18 @@ function WeakMapGet(key) { ...@@ -57,20 +59,18 @@ function WeakMapGet(key) {
var hash = GetExistingHash(key); var hash = GetExistingHash(key);
if (IS_UNDEFINED(hash)) return UNDEFINED; if (IS_UNDEFINED(hash)) return UNDEFINED;
return %WeakCollectionGet(this, key, hash); return %WeakCollectionGet(this, key, hash);
} }
function WeakMapSet(key, value) { set(key, value) {
if (!IS_WEAKMAP(this)) { if (!IS_WEAKMAP(this)) {
throw %make_type_error(kIncompatibleMethodReceiver, throw %make_type_error(kIncompatibleMethodReceiver,
'WeakMap.prototype.set', this); 'WeakMap.prototype.set', this);
} }
if (!IS_RECEIVER(key)) throw %make_type_error(kInvalidWeakMapKey); if (!IS_RECEIVER(key)) throw %make_type_error(kInvalidWeakMapKey);
return %WeakCollectionSet(this, key, value, GetHash(key)); return %WeakCollectionSet(this, key, value, GetHash(key));
} }
function WeakMapHas(key) { has(key) {
if (!IS_WEAKMAP(this)) { if (!IS_WEAKMAP(this)) {
throw %make_type_error(kIncompatibleMethodReceiver, throw %make_type_error(kIncompatibleMethodReceiver,
'WeakMap.prototype.has', this); 'WeakMap.prototype.has', this);
...@@ -79,10 +79,9 @@ function WeakMapHas(key) { ...@@ -79,10 +79,9 @@ function WeakMapHas(key) {
var hash = GetExistingHash(key); var hash = GetExistingHash(key);
if (IS_UNDEFINED(hash)) return false; if (IS_UNDEFINED(hash)) return false;
return %WeakCollectionHas(this, key, hash); return %WeakCollectionHas(this, key, hash);
} }
function WeakMapDelete(key) { delete(key) {
if (!IS_WEAKMAP(this)) { if (!IS_WEAKMAP(this)) {
throw %make_type_error(kIncompatibleMethodReceiver, throw %make_type_error(kIncompatibleMethodReceiver,
'WeakMap.prototype.delete', this); 'WeakMap.prototype.delete', this);
...@@ -91,26 +90,14 @@ function WeakMapDelete(key) { ...@@ -91,26 +90,14 @@ function WeakMapDelete(key) {
var hash = GetExistingHash(key); var hash = GetExistingHash(key);
if (IS_UNDEFINED(hash)) return false; if (IS_UNDEFINED(hash)) return false;
return %WeakCollectionDelete(this, key, hash); return %WeakCollectionDelete(this, key, hash);
} }
}
);
// ------------------------------------------------------------------- // -------------------------------------------------------------------
%SetCode(GlobalWeakMap, WeakMapConstructor); %SetCode(GlobalWeakMap, WeakMapConstructor);
%FunctionSetLength(GlobalWeakMap, 0); %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 // Harmony WeakSet
...@@ -134,17 +121,20 @@ function WeakSetConstructor(iterable) { ...@@ -134,17 +121,20 @@ function WeakSetConstructor(iterable) {
} }
function WeakSetAdd(value) { // Set up the non-enumerable functions on the WeakSet prototype object.
DEFINE_METHODS(
GlobalWeakSet.prototype,
{
add(value) {
if (!IS_WEAKSET(this)) { if (!IS_WEAKSET(this)) {
throw %make_type_error(kIncompatibleMethodReceiver, throw %make_type_error(kIncompatibleMethodReceiver,
'WeakSet.prototype.add', this); 'WeakSet.prototype.add', this);
} }
if (!IS_RECEIVER(value)) throw %make_type_error(kInvalidWeakSetValue); if (!IS_RECEIVER(value)) throw %make_type_error(kInvalidWeakSetValue);
return %WeakCollectionSet(this, value, true, GetHash(value)); return %WeakCollectionSet(this, value, true, GetHash(value));
} }
function WeakSetHas(value) { has(value) {
if (!IS_WEAKSET(this)) { if (!IS_WEAKSET(this)) {
throw %make_type_error(kIncompatibleMethodReceiver, throw %make_type_error(kIncompatibleMethodReceiver,
'WeakSet.prototype.has', this); 'WeakSet.prototype.has', this);
...@@ -153,10 +143,9 @@ function WeakSetHas(value) { ...@@ -153,10 +143,9 @@ function WeakSetHas(value) {
var hash = GetExistingHash(value); var hash = GetExistingHash(value);
if (IS_UNDEFINED(hash)) return false; if (IS_UNDEFINED(hash)) return false;
return %WeakCollectionHas(this, value, hash); return %WeakCollectionHas(this, value, hash);
} }
function WeakSetDelete(value) { delete(value) {
if (!IS_WEAKSET(this)) { if (!IS_WEAKSET(this)) {
throw %make_type_error(kIncompatibleMethodReceiver, throw %make_type_error(kIncompatibleMethodReceiver,
'WeakSet.prototype.delete', this); 'WeakSet.prototype.delete', this);
...@@ -165,24 +154,13 @@ function WeakSetDelete(value) { ...@@ -165,24 +154,13 @@ function WeakSetDelete(value) {
var hash = GetExistingHash(value); var hash = GetExistingHash(value);
if (IS_UNDEFINED(hash)) return false; if (IS_UNDEFINED(hash)) return false;
return %WeakCollectionDelete(this, value, hash); return %WeakCollectionDelete(this, value, hash);
} }
}
);
// ------------------------------------------------------------------- // -------------------------------------------------------------------
%SetCode(GlobalWeakSet, WeakSetConstructor); %SetCode(GlobalWeakSet, WeakSetConstructor);
%FunctionSetLength(GlobalWeakSet, 0); %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
]);
}) })
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