Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(18)

Unified Diff: src/api.cc

Issue 1148383007: Add {Map,Set}::AsArray to the API (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Rebased Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « include/v8.h ('k') | test/cctest/test-api.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/api.cc
diff --git a/src/api.cc b/src/api.cc
index dbc782c2dbe6802be7892cccf0a84017c923fd7c..7cf416cfbd240bfd8f85b8ec9dc331b5649df117 100644
--- a/src/api.cc
+++ b/src/api.cc
@@ -6131,6 +6131,31 @@ size_t v8::Map::Size() const {
}
+Local<Array> Map::AsArray() const {
+ i::Handle<i::JSMap> obj = Utils::OpenHandle(this);
+ i::Isolate* isolate = obj->GetIsolate();
+ i::Factory* factory = isolate->factory();
+ LOG_API(isolate, "Map::AsArray");
+ ENTER_V8(isolate);
+ i::Handle<i::OrderedHashMap> table(i::OrderedHashMap::cast(obj->table()));
+ int length = table->NumberOfElements();
+ i::Handle<i::FixedArray> result = factory->NewFixedArray(length);
+ for (int i = 0; i < length; ++i) {
+ if (table->KeyAt(i)->IsTheHole()) continue;
+ i::HandleScope handle_scope(isolate);
+ i::Handle<i::FixedArray> entry = factory->NewFixedArray(2);
+ entry->set(0, table->KeyAt(i));
+ entry->set(1, table->ValueAt(i));
+ i::Handle<i::JSArray> entry_array =
+ factory->NewJSArrayWithElements(entry, i::FAST_ELEMENTS, 2);
+ result->set(i, *entry_array);
+ }
+ i::Handle<i::JSArray> result_array =
+ factory->NewJSArrayWithElements(result, i::FAST_ELEMENTS, length);
+ return Utils::ToLocal(result_array);
+}
+
+
Local<v8::Set> v8::Set::New(Isolate* isolate) {
i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
LOG_API(i_isolate, "Set::New");
@@ -6146,6 +6171,27 @@ size_t v8::Set::Size() const {
}
+Local<Array> Set::AsArray() const {
+ i::Handle<i::JSSet> obj = Utils::OpenHandle(this);
+ i::Isolate* isolate = obj->GetIsolate();
+ i::Factory* factory = isolate->factory();
+ LOG_API(isolate, "Set::AsArray");
+ ENTER_V8(isolate);
+ i::Handle<i::OrderedHashSet> table(i::OrderedHashSet::cast(obj->table()));
+ int length = table->NumberOfElements();
+ i::Handle<i::FixedArray> result = factory->NewFixedArray(length);
+ for (int i = 0; i < length; ++i) {
+ i::Object* key = table->KeyAt(i);
+ if (!key->IsTheHole()) {
+ result->set(i, key);
+ }
+ }
+ i::Handle<i::JSArray> result_array =
+ factory->NewJSArrayWithElements(result, i::FAST_ELEMENTS, length);
+ return Utils::ToLocal(result_array);
+}
+
+
bool Value::IsPromise() const {
auto self = Utils::OpenHandle(this);
return i::Object::IsPromise(self);
« no previous file with comments | « include/v8.h ('k') | test/cctest/test-api.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698