Commit 19f7cc49 authored by Sathya Gunasekaran's avatar Sathya Gunasekaran Committed by Commit Bot

[Collections] Port Map.p.has to CSA/C++

Bug: v8:5717
Change-Id: I87cfef5d6d69106fd454338e77c2c703fcdba67b
Reviewed-on: https://chromium-review.googlesource.com/520576Reviewed-by: 's avatarJakob Gruber <jgruber@chromium.org>
Reviewed-by: 's avatarCamillo Bruni <cbruni@chromium.org>
Commit-Queue: Sathya Gunasekaran <gsathya@chromium.org>
Cr-Commit-Position: refs/heads/master@{#45674}
parent 3b7ff7a5
......@@ -1596,6 +1596,13 @@ ExternalReference ExternalReference::orderedhashtable_get_raw(
return ExternalReference(Redirect(isolate, FUNCTION_ADDR(f)));
}
template <typename CollectionType, int entrysize>
ExternalReference ExternalReference::orderedhashtable_has_raw(
Isolate* isolate) {
auto f = OrderedHashTable<CollectionType, entrysize>::HasKey;
return ExternalReference(Redirect(isolate, FUNCTION_ADDR(f)));
}
ExternalReference ExternalReference::try_internalize_string_function(
Isolate* isolate) {
return ExternalReference(Redirect(
......@@ -1631,6 +1638,11 @@ ExternalReference::orderedhashtable_get_raw<OrderedHashMap, 2>(Isolate*);
template ExternalReference
ExternalReference::orderedhashtable_get_raw<OrderedHashSet, 1>(Isolate*);
template ExternalReference
ExternalReference::orderedhashtable_has_raw<OrderedHashMap, 2>(Isolate*);
template ExternalReference
ExternalReference::orderedhashtable_has_raw<OrderedHashSet, 1>(Isolate*);
ExternalReference ExternalReference::page_flags(Page* page) {
return ExternalReference(reinterpret_cast<Address>(page) +
MemoryChunk::kFlagsOffset);
......
......@@ -1027,6 +1027,9 @@ class ExternalReference BASE_EMBEDDED {
template <typename CollectionType, int entrysize>
static ExternalReference orderedhashtable_get_raw(Isolate* isolate);
template <typename CollectionType, int entrysize>
static ExternalReference orderedhashtable_has_raw(Isolate* isolate);
static ExternalReference page_flags(Page* page);
static ExternalReference ForDeoptEntry(Address entry);
......
......@@ -2950,6 +2950,11 @@ void Genesis::InitializeGlobal(Handle<JSGlobalObject> global_object,
InstallWithIntrinsicDefaultProto(isolate, map_get,
Context::MAP_GET_METHOD_INDEX);
Handle<JSFunction> map_has =
SimpleInstallFunction(prototype, "has", Builtins::kMapHas, 1, true);
InstallWithIntrinsicDefaultProto(isolate, map_has,
Context::MAP_HAS_METHOD_INDEX);
InstallSpeciesGetter(js_map_fun);
}
......
......@@ -19,9 +19,10 @@ class CollectionsBuiltinsAssembler : public CodeStubAssembler {
protected:
Node* AllocateJSMap(Node* js_map_function);
template <typename CollectionType, int entrysize>
Node* CallGetRaw(Node* const table, Node* const key);
template <typename CollectionType, int entrysize>
Node* CallHasRaw(Node* const table, Node* const key);
};
Node* CollectionsBuiltinsAssembler::AllocateJSMap(Node* js_map_function) {
......@@ -174,6 +175,25 @@ Node* CollectionsBuiltinsAssembler::CallGetRaw(Node* const table,
return result;
}
template <typename CollectionType, int entrysize>
Node* CollectionsBuiltinsAssembler::CallHasRaw(Node* const table,
Node* const key) {
Node* const function_addr = ExternalConstant(
ExternalReference::orderedhashtable_has_raw<CollectionType, entrysize>(
isolate()));
Node* const isolate_ptr =
ExternalConstant(ExternalReference::isolate_address(isolate()));
MachineType type_ptr = MachineType::Pointer();
MachineType type_tagged = MachineType::AnyTagged();
Node* const result =
CallCFunction3(type_tagged, type_ptr, type_tagged, type_tagged,
function_addr, isolate_ptr, table, key);
return result;
}
TF_BUILTIN(MapGet, CollectionsBuiltinsAssembler) {
Node* const receiver = Parameter(Descriptor::kReceiver);
Node* const key = Parameter(Descriptor::kKey);
......@@ -184,5 +204,17 @@ TF_BUILTIN(MapGet, CollectionsBuiltinsAssembler) {
Node* const table = LoadObjectField(receiver, JSMap::kTableOffset);
Return(CallGetRaw<OrderedHashMap, 2>(table, key));
}
TF_BUILTIN(MapHas, CollectionsBuiltinsAssembler) {
Node* const receiver = Parameter(Descriptor::kReceiver);
Node* const key = Parameter(Descriptor::kKey);
Node* const context = Parameter(Descriptor::kContext);
ThrowIfNotInstanceType(context, receiver, JS_MAP_TYPE, "Map.prototype.has");
Node* const table = LoadObjectField(receiver, JSMap::kTableOffset);
Return(CallHasRaw<OrderedHashMap, 2>(table, key));
}
} // namespace internal
} // namespace v8
......@@ -541,6 +541,7 @@ namespace internal {
/* Map */ \
TFJ(MapConstructor, 1, kIterable) \
TFJ(MapGet, 1, kKey) \
TFJ(MapHas, 1, kKey) \
\
/* Math */ \
/* ES6 #sec-math.abs */ \
......
......@@ -277,6 +277,12 @@ void ExternalReferenceTable::AddReferences(Isolate* isolate) {
Add(ExternalReference::orderedhashtable_get_raw<OrderedHashSet, 1>(isolate)
.address(),
"orderedhashtable_get_raw<OrderedHashSet, 1>");
Add(ExternalReference::orderedhashtable_has_raw<OrderedHashMap, 2>(isolate)
.address(),
"orderedhashtable_has_raw<OrderedHashMap, 2>");
Add(ExternalReference::orderedhashtable_has_raw<OrderedHashSet, 1>(isolate)
.address(),
"orderedhashtable_has_raw<OrderedHashSet, 1>");
Add(ExternalReference::log_enter_external_function(isolate).address(),
"Logger::EnterExternal");
Add(ExternalReference::log_leave_external_function(isolate).address(),
......
......@@ -324,18 +324,6 @@ function MapSet(key, value) {
}
function MapHas(key) {
if (!IS_MAP(this)) {
throw %make_type_error(kIncompatibleMethodReceiver,
'Map.prototype.has', this);
}
var table = %_JSCollectionGetTable(this);
var numBuckets = ORDERED_HASH_TABLE_BUCKET_COUNT(table);
var hash = GetHash(key);
return MapFindEntry(table, numBuckets, key, hash) !== NOT_FOUND;
}
function MapDelete(key) {
if (!IS_MAP(this)) {
throw %make_type_error(kIncompatibleMethodReceiver,
......@@ -401,7 +389,6 @@ function MapForEach(f, receiver) {
utils.InstallGetter(GlobalMap.prototype, "size", MapGetSize);
utils.InstallFunctions(GlobalMap.prototype, DONT_ENUM, [
"set", MapSet,
"has", MapHas,
"delete", MapDelete,
"clear", MapClearJS,
"forEach", MapForEach
......@@ -412,7 +399,6 @@ utils.InstallFunctions(GlobalMap.prototype, DONT_ENUM, [
%InstallToContext([
"map_set", MapSet,
"map_has", MapHas,
"map_delete", MapDelete,
"set_add", SetAdd,
"set_has", SetHas,
......
......@@ -18742,19 +18742,18 @@ Handle<Derived> OrderedHashTable<Derived, entrysize>::Clear(
}
template <class Derived, int entrysize>
bool OrderedHashTable<Derived, entrysize>::HasKey(Handle<Derived> table,
Handle<Object> key) {
Object* OrderedHashTable<Derived, entrysize>::HasKey(Isolate* isolate,
Derived* table,
Object* key) {
DisallowHeapAllocation no_gc;
Isolate* isolate = table->GetIsolate();
Object* raw_key = *key;
int entry = table->KeyToFirstEntry(isolate, raw_key);
int entry = table->KeyToFirstEntry(isolate, key);
// Walk the chain in the bucket to find the key.
while (entry != kNotFound) {
Object* candidate_key = table->KeyAt(entry);
if (candidate_key->SameValueZero(raw_key)) return true;
if (candidate_key->SameValueZero(key)) return isolate->heap()->true_value();
entry = table->NextChainEntry(entry);
}
return false;
return isolate->heap()->false_value();
}
template <class Derived, int entrysize>
......@@ -18885,8 +18884,8 @@ template Handle<OrderedHashSet> OrderedHashTable<OrderedHashSet, 1>::Shrink(
template Handle<OrderedHashSet> OrderedHashTable<OrderedHashSet, 1>::Clear(
Handle<OrderedHashSet> table);
template bool OrderedHashTable<OrderedHashSet, 1>::HasKey(
Handle<OrderedHashSet> table, Handle<Object> key);
template Object* OrderedHashTable<OrderedHashSet, 1>::HasKey(
Isolate* isolate, OrderedHashSet* table, Object* key);
template Object* OrderedHashTable<OrderedHashSet, 1>::Get(Isolate* isolate,
OrderedHashSet* table,
......@@ -18904,8 +18903,8 @@ template Handle<OrderedHashMap> OrderedHashTable<OrderedHashMap, 2>::Shrink(
template Handle<OrderedHashMap> OrderedHashTable<OrderedHashMap, 2>::Clear(
Handle<OrderedHashMap> table);
template bool OrderedHashTable<OrderedHashMap, 2>::HasKey(
Handle<OrderedHashMap> table, Handle<Object> key);
template Object* OrderedHashTable<OrderedHashMap, 2>::HasKey(
Isolate* isolate, OrderedHashMap* table, Object* key);
template Object* OrderedHashTable<OrderedHashMap, 2>::Get(Isolate* isolate,
OrderedHashMap* table,
......
......@@ -399,7 +399,7 @@ class OrderedHashTable : public FixedArray {
static Handle<Derived> Clear(Handle<Derived> table);
// Returns a true if the OrderedHashTable contains the key
static bool HasKey(Handle<Derived> table, Handle<Object> key);
static Object* HasKey(Isolate* isolate, Derived* table, Object* key);
// Returns a value if the OrderedHashTable contains the key,
// otherwise returns undefined.
......
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