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

Side by Side 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 unified diff | Download patch
« no previous file with comments | « include/v8.h ('k') | test/cctest/test-api.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "src/api.h" 5 #include "src/api.h"
6 6
7 #include <string.h> // For memcpy, strlen. 7 #include <string.h> // For memcpy, strlen.
8 #ifdef V8_USE_ADDRESS_SANITIZER 8 #ifdef V8_USE_ADDRESS_SANITIZER
9 #include <sanitizer/asan_interface.h> 9 #include <sanitizer/asan_interface.h>
10 #endif // V8_USE_ADDRESS_SANITIZER 10 #endif // V8_USE_ADDRESS_SANITIZER
(...skipping 6113 matching lines...) Expand 10 before | Expand all | Expand 10 after
6124 return Utils::ToLocal(obj); 6124 return Utils::ToLocal(obj);
6125 } 6125 }
6126 6126
6127 6127
6128 size_t v8::Map::Size() const { 6128 size_t v8::Map::Size() const {
6129 i::Handle<i::JSMap> obj = Utils::OpenHandle(this); 6129 i::Handle<i::JSMap> obj = Utils::OpenHandle(this);
6130 return i::OrderedHashMap::cast(obj->table())->NumberOfElements(); 6130 return i::OrderedHashMap::cast(obj->table())->NumberOfElements();
6131 } 6131 }
6132 6132
6133 6133
6134 Local<Array> Map::AsArray() const {
6135 i::Handle<i::JSMap> obj = Utils::OpenHandle(this);
6136 i::Isolate* isolate = obj->GetIsolate();
6137 i::Factory* factory = isolate->factory();
6138 LOG_API(isolate, "Map::AsArray");
6139 ENTER_V8(isolate);
6140 i::Handle<i::OrderedHashMap> table(i::OrderedHashMap::cast(obj->table()));
6141 int length = table->NumberOfElements();
6142 i::Handle<i::FixedArray> result = factory->NewFixedArray(length);
6143 for (int i = 0; i < length; ++i) {
6144 if (table->KeyAt(i)->IsTheHole()) continue;
6145 i::HandleScope handle_scope(isolate);
6146 i::Handle<i::FixedArray> entry = factory->NewFixedArray(2);
6147 entry->set(0, table->KeyAt(i));
6148 entry->set(1, table->ValueAt(i));
6149 i::Handle<i::JSArray> entry_array =
6150 factory->NewJSArrayWithElements(entry, i::FAST_ELEMENTS, 2);
6151 result->set(i, *entry_array);
6152 }
6153 i::Handle<i::JSArray> result_array =
6154 factory->NewJSArrayWithElements(result, i::FAST_ELEMENTS, length);
6155 return Utils::ToLocal(result_array);
6156 }
6157
6158
6134 Local<v8::Set> v8::Set::New(Isolate* isolate) { 6159 Local<v8::Set> v8::Set::New(Isolate* isolate) {
6135 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate); 6160 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
6136 LOG_API(i_isolate, "Set::New"); 6161 LOG_API(i_isolate, "Set::New");
6137 ENTER_V8(i_isolate); 6162 ENTER_V8(i_isolate);
6138 i::Handle<i::JSSet> obj = i_isolate->factory()->NewJSSet(); 6163 i::Handle<i::JSSet> obj = i_isolate->factory()->NewJSSet();
6139 return Utils::ToLocal(obj); 6164 return Utils::ToLocal(obj);
6140 } 6165 }
6141 6166
6142 6167
6143 size_t v8::Set::Size() const { 6168 size_t v8::Set::Size() const {
6144 i::Handle<i::JSSet> obj = Utils::OpenHandle(this); 6169 i::Handle<i::JSSet> obj = Utils::OpenHandle(this);
6145 return i::OrderedHashSet::cast(obj->table())->NumberOfElements(); 6170 return i::OrderedHashSet::cast(obj->table())->NumberOfElements();
6146 } 6171 }
6147 6172
6148 6173
6174 Local<Array> Set::AsArray() const {
6175 i::Handle<i::JSSet> obj = Utils::OpenHandle(this);
6176 i::Isolate* isolate = obj->GetIsolate();
6177 i::Factory* factory = isolate->factory();
6178 LOG_API(isolate, "Set::AsArray");
6179 ENTER_V8(isolate);
6180 i::Handle<i::OrderedHashSet> table(i::OrderedHashSet::cast(obj->table()));
6181 int length = table->NumberOfElements();
6182 i::Handle<i::FixedArray> result = factory->NewFixedArray(length);
6183 for (int i = 0; i < length; ++i) {
6184 i::Object* key = table->KeyAt(i);
6185 if (!key->IsTheHole()) {
6186 result->set(i, key);
6187 }
6188 }
6189 i::Handle<i::JSArray> result_array =
6190 factory->NewJSArrayWithElements(result, i::FAST_ELEMENTS, length);
6191 return Utils::ToLocal(result_array);
6192 }
6193
6194
6149 bool Value::IsPromise() const { 6195 bool Value::IsPromise() const {
6150 auto self = Utils::OpenHandle(this); 6196 auto self = Utils::OpenHandle(this);
6151 return i::Object::IsPromise(self); 6197 return i::Object::IsPromise(self);
6152 } 6198 }
6153 6199
6154 6200
6155 MaybeLocal<Promise::Resolver> Promise::Resolver::New(Local<Context> context) { 6201 MaybeLocal<Promise::Resolver> Promise::Resolver::New(Local<Context> context) {
6156 PREPARE_FOR_EXECUTION(context, "Promise::Resolver::New", Resolver); 6202 PREPARE_FOR_EXECUTION(context, "Promise::Resolver::New", Resolver);
6157 i::Handle<i::Object> result; 6203 i::Handle<i::Object> result;
6158 has_pending_exception = !i::Execution::Call( 6204 has_pending_exception = !i::Execution::Call(
(...skipping 1994 matching lines...) Expand 10 before | Expand all | Expand 10 after
8153 Isolate* isolate = reinterpret_cast<Isolate*>(info.GetIsolate()); 8199 Isolate* isolate = reinterpret_cast<Isolate*>(info.GetIsolate());
8154 Address callback_address = 8200 Address callback_address =
8155 reinterpret_cast<Address>(reinterpret_cast<intptr_t>(callback)); 8201 reinterpret_cast<Address>(reinterpret_cast<intptr_t>(callback));
8156 VMState<EXTERNAL> state(isolate); 8202 VMState<EXTERNAL> state(isolate);
8157 ExternalCallbackScope call_scope(isolate, callback_address); 8203 ExternalCallbackScope call_scope(isolate, callback_address);
8158 callback(info); 8204 callback(info);
8159 } 8205 }
8160 8206
8161 8207
8162 } } // namespace v8::internal 8208 } } // namespace v8::internal
OLDNEW
« 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