OLD | NEW |
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 7122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
7133 auto self = Utils::OpenHandle(this); | 7133 auto self = Utils::OpenHandle(this); |
7134 i::Handle<i::Object> result; | 7134 i::Handle<i::Object> result; |
7135 i::Handle<i::Object> argv[] = {Utils::OpenHandle(*key)}; | 7135 i::Handle<i::Object> argv[] = {Utils::OpenHandle(*key)}; |
7136 has_pending_exception = !i::Execution::Call(isolate, isolate->map_delete(), | 7136 has_pending_exception = !i::Execution::Call(isolate, isolate->map_delete(), |
7137 self, arraysize(argv), argv) | 7137 self, arraysize(argv), argv) |
7138 .ToHandle(&result); | 7138 .ToHandle(&result); |
7139 RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool); | 7139 RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool); |
7140 return Just(result->IsTrue(isolate)); | 7140 return Just(result->IsTrue(isolate)); |
7141 } | 7141 } |
7142 | 7142 |
7143 | 7143 namespace { |
7144 Local<Array> Map::AsArray() const { | 7144 i::Handle<i::JSArray> MapAsArray(i::Isolate* isolate, i::Object* table_obj, |
7145 i::Handle<i::JSMap> obj = Utils::OpenHandle(this); | 7145 int offset, int kind) { |
7146 i::Isolate* isolate = obj->GetIsolate(); | |
7147 i::Factory* factory = isolate->factory(); | 7146 i::Factory* factory = isolate->factory(); |
7148 LOG_API(isolate, Map, AsArray); | 7147 i::Handle<i::OrderedHashMap> table(i::OrderedHashMap::cast(table_obj)); |
7149 ENTER_V8(isolate); | 7148 if (offset >= table->NumberOfElements()) return factory->NewJSArray(0); |
7150 i::Handle<i::OrderedHashMap> table(i::OrderedHashMap::cast(obj->table())); | 7149 int length = (table->NumberOfElements() - offset) * |
7151 int length = table->NumberOfElements() * 2; | 7150 (kind == i::JSMapIterator::kKindEntries ? 2 : 1); |
7152 i::Handle<i::FixedArray> result = factory->NewFixedArray(length); | 7151 i::Handle<i::FixedArray> result = factory->NewFixedArray(length); |
7153 int result_index = 0; | 7152 int result_index = 0; |
7154 { | 7153 { |
7155 i::DisallowHeapAllocation no_gc; | 7154 i::DisallowHeapAllocation no_gc; |
7156 int capacity = table->UsedCapacity(); | 7155 int capacity = table->UsedCapacity(); |
7157 i::Oddball* the_hole = isolate->heap()->the_hole_value(); | 7156 i::Oddball* the_hole = isolate->heap()->the_hole_value(); |
7158 for (int i = 0; i < capacity; ++i) { | 7157 for (int i = 0; i < capacity; ++i) { |
7159 i::Object* key = table->KeyAt(i); | 7158 i::Object* key = table->KeyAt(i); |
7160 if (key == the_hole) continue; | 7159 if (key == the_hole) continue; |
7161 result->set(result_index++, key); | 7160 if (offset-- > 0) continue; |
7162 result->set(result_index++, table->ValueAt(i)); | 7161 if (kind == i::JSMapIterator::kKindEntries || |
| 7162 kind == i::JSMapIterator::kKindKeys) { |
| 7163 result->set(result_index++, key); |
| 7164 } |
| 7165 if (kind == i::JSMapIterator::kKindEntries || |
| 7166 kind == i::JSMapIterator::kKindValues) { |
| 7167 result->set(result_index++, table->ValueAt(i)); |
| 7168 } |
7163 } | 7169 } |
7164 } | 7170 } |
7165 DCHECK_EQ(result_index, result->length()); | 7171 DCHECK_EQ(result_index, result->length()); |
7166 DCHECK_EQ(result_index, length); | 7172 DCHECK_EQ(result_index, length); |
7167 i::Handle<i::JSArray> result_array = | 7173 return factory->NewJSArrayWithElements(result, i::FAST_ELEMENTS, length); |
7168 factory->NewJSArrayWithElements(result, i::FAST_ELEMENTS, length); | 7174 } |
7169 return Utils::ToLocal(result_array); | 7175 } // namespace |
| 7176 |
| 7177 Local<Array> Map::AsArray() const { |
| 7178 i::Handle<i::JSMap> obj = Utils::OpenHandle(this); |
| 7179 i::Isolate* isolate = obj->GetIsolate(); |
| 7180 LOG_API(isolate, Map, AsArray); |
| 7181 ENTER_V8(isolate); |
| 7182 return Utils::ToLocal( |
| 7183 MapAsArray(isolate, obj->table(), 0, i::JSMapIterator::kKindEntries)); |
7170 } | 7184 } |
7171 | 7185 |
7172 | 7186 |
7173 Local<v8::Set> v8::Set::New(Isolate* isolate) { | 7187 Local<v8::Set> v8::Set::New(Isolate* isolate) { |
7174 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate); | 7188 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate); |
7175 LOG_API(i_isolate, Set, New); | 7189 LOG_API(i_isolate, Set, New); |
7176 ENTER_V8_NO_SCRIPT_NO_EXCEPTION(i_isolate); | 7190 ENTER_V8_NO_SCRIPT_NO_EXCEPTION(i_isolate); |
7177 i::Handle<i::JSSet> obj = i_isolate->factory()->NewJSSet(); | 7191 i::Handle<i::JSSet> obj = i_isolate->factory()->NewJSSet(); |
7178 return Utils::ToLocal(obj); | 7192 return Utils::ToLocal(obj); |
7179 } | 7193 } |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
7225 auto self = Utils::OpenHandle(this); | 7239 auto self = Utils::OpenHandle(this); |
7226 i::Handle<i::Object> result; | 7240 i::Handle<i::Object> result; |
7227 i::Handle<i::Object> argv[] = {Utils::OpenHandle(*key)}; | 7241 i::Handle<i::Object> argv[] = {Utils::OpenHandle(*key)}; |
7228 has_pending_exception = !i::Execution::Call(isolate, isolate->set_delete(), | 7242 has_pending_exception = !i::Execution::Call(isolate, isolate->set_delete(), |
7229 self, arraysize(argv), argv) | 7243 self, arraysize(argv), argv) |
7230 .ToHandle(&result); | 7244 .ToHandle(&result); |
7231 RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool); | 7245 RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool); |
7232 return Just(result->IsTrue(isolate)); | 7246 return Just(result->IsTrue(isolate)); |
7233 } | 7247 } |
7234 | 7248 |
7235 | 7249 namespace { |
7236 Local<Array> Set::AsArray() const { | 7250 i::Handle<i::JSArray> SetAsArray(i::Isolate* isolate, i::Object* table_obj, |
7237 i::Handle<i::JSSet> obj = Utils::OpenHandle(this); | 7251 int offset) { |
7238 i::Isolate* isolate = obj->GetIsolate(); | |
7239 i::Factory* factory = isolate->factory(); | 7252 i::Factory* factory = isolate->factory(); |
7240 LOG_API(isolate, Set, AsArray); | 7253 i::Handle<i::OrderedHashSet> table(i::OrderedHashSet::cast(table_obj)); |
7241 ENTER_V8(isolate); | 7254 int length = table->NumberOfElements() - offset; |
7242 i::Handle<i::OrderedHashSet> table(i::OrderedHashSet::cast(obj->table())); | 7255 if (length <= 0) return factory->NewJSArray(0); |
7243 int length = table->NumberOfElements(); | |
7244 i::Handle<i::FixedArray> result = factory->NewFixedArray(length); | 7256 i::Handle<i::FixedArray> result = factory->NewFixedArray(length); |
7245 int result_index = 0; | 7257 int result_index = 0; |
7246 { | 7258 { |
7247 i::DisallowHeapAllocation no_gc; | 7259 i::DisallowHeapAllocation no_gc; |
7248 int capacity = table->UsedCapacity(); | 7260 int capacity = table->UsedCapacity(); |
7249 i::Oddball* the_hole = isolate->heap()->the_hole_value(); | 7261 i::Oddball* the_hole = isolate->heap()->the_hole_value(); |
7250 for (int i = 0; i < capacity; ++i) { | 7262 for (int i = 0; i < capacity; ++i) { |
7251 i::Object* key = table->KeyAt(i); | 7263 i::Object* key = table->KeyAt(i); |
7252 if (key == the_hole) continue; | 7264 if (key == the_hole) continue; |
| 7265 if (offset-- > 0) continue; |
7253 result->set(result_index++, key); | 7266 result->set(result_index++, key); |
7254 } | 7267 } |
7255 } | 7268 } |
7256 DCHECK_EQ(result_index, result->length()); | 7269 DCHECK_EQ(result_index, result->length()); |
7257 DCHECK_EQ(result_index, length); | 7270 DCHECK_EQ(result_index, length); |
7258 i::Handle<i::JSArray> result_array = | 7271 return factory->NewJSArrayWithElements(result, i::FAST_ELEMENTS, length); |
7259 factory->NewJSArrayWithElements(result, i::FAST_ELEMENTS, length); | 7272 } |
7260 return Utils::ToLocal(result_array); | 7273 } // namespace |
| 7274 |
| 7275 Local<Array> Set::AsArray() const { |
| 7276 i::Handle<i::JSSet> obj = Utils::OpenHandle(this); |
| 7277 i::Isolate* isolate = obj->GetIsolate(); |
| 7278 LOG_API(isolate, Set, AsArray); |
| 7279 ENTER_V8(isolate); |
| 7280 return Utils::ToLocal(SetAsArray(isolate, obj->table(), 0)); |
7261 } | 7281 } |
7262 | 7282 |
7263 | 7283 |
7264 MaybeLocal<Promise::Resolver> Promise::Resolver::New(Local<Context> context) { | 7284 MaybeLocal<Promise::Resolver> Promise::Resolver::New(Local<Context> context) { |
7265 PREPARE_FOR_EXECUTION(context, Promise_Resolver, New, Resolver); | 7285 PREPARE_FOR_EXECUTION(context, Promise_Resolver, New, Resolver); |
7266 i::Handle<i::Object> result; | 7286 i::Handle<i::Object> result; |
7267 has_pending_exception = | 7287 has_pending_exception = |
7268 !i::Execution::Call(isolate, isolate->promise_internal_constructor(), | 7288 !i::Execution::Call(isolate, isolate->promise_internal_constructor(), |
7269 isolate->factory()->undefined_value(), 0, NULL) | 7289 isolate->factory()->undefined_value(), 0, NULL) |
7270 .ToHandle(&result); | 7290 .ToHandle(&result); |
(...skipping 2076 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
9347 | 9367 |
9348 int debug::EstimatedValueSize(Isolate* v8_isolate, v8::Local<v8::Value> value) { | 9368 int debug::EstimatedValueSize(Isolate* v8_isolate, v8::Local<v8::Value> value) { |
9349 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate); | 9369 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate); |
9350 ENTER_V8(isolate); | 9370 ENTER_V8(isolate); |
9351 i::Handle<i::Object> object = Utils::OpenHandle(*value); | 9371 i::Handle<i::Object> object = Utils::OpenHandle(*value); |
9352 if (object->IsSmi()) return i::kPointerSize; | 9372 if (object->IsSmi()) return i::kPointerSize; |
9353 CHECK(object->IsHeapObject()); | 9373 CHECK(object->IsHeapObject()); |
9354 return i::Handle<i::HeapObject>::cast(object)->Size(); | 9374 return i::Handle<i::HeapObject>::cast(object)->Size(); |
9355 } | 9375 } |
9356 | 9376 |
| 9377 v8::MaybeLocal<v8::Array> debug::EntriesPreview(Isolate* v8_isolate, |
| 9378 v8::Local<v8::Value> value, |
| 9379 bool* is_key_value) { |
| 9380 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate); |
| 9381 ENTER_V8(isolate); |
| 9382 if (value->IsMap()) { |
| 9383 *is_key_value = true; |
| 9384 return value.As<Map>()->AsArray(); |
| 9385 } |
| 9386 if (value->IsSet()) { |
| 9387 *is_key_value = false; |
| 9388 return value.As<Set>()->AsArray(); |
| 9389 } |
| 9390 |
| 9391 i::Handle<i::Object> object = Utils::OpenHandle(*value); |
| 9392 if (object->IsJSWeakCollection()) { |
| 9393 *is_key_value = object->IsJSWeakMap(); |
| 9394 return Utils::ToLocal(i::JSWeakCollection::GetEntries( |
| 9395 i::Handle<i::JSWeakCollection>::cast(object), 0)); |
| 9396 } |
| 9397 if (object->IsJSMapIterator()) { |
| 9398 i::Handle<i::JSMapIterator> iterator = |
| 9399 i::Handle<i::JSMapIterator>::cast(object); |
| 9400 int iterator_kind = i::Smi::cast(iterator->kind())->value(); |
| 9401 *is_key_value = iterator_kind == i::JSMapIterator::kKindEntries; |
| 9402 if (!iterator->HasMore()) return v8::Array::New(v8_isolate); |
| 9403 return Utils::ToLocal(MapAsArray(isolate, iterator->table(), |
| 9404 i::Smi::cast(iterator->index())->value(), |
| 9405 iterator_kind)); |
| 9406 } |
| 9407 if (object->IsJSSetIterator()) { |
| 9408 i::Handle<i::JSSetIterator> it = i::Handle<i::JSSetIterator>::cast(object); |
| 9409 *is_key_value = false; |
| 9410 if (!it->HasMore()) return v8::Array::New(v8_isolate); |
| 9411 return Utils::ToLocal( |
| 9412 SetAsArray(isolate, it->table(), i::Smi::cast(it->index())->value())); |
| 9413 } |
| 9414 return v8::MaybeLocal<v8::Array>(); |
| 9415 } |
| 9416 |
9357 Local<String> CpuProfileNode::GetFunctionName() const { | 9417 Local<String> CpuProfileNode::GetFunctionName() const { |
9358 const i::ProfileNode* node = reinterpret_cast<const i::ProfileNode*>(this); | 9418 const i::ProfileNode* node = reinterpret_cast<const i::ProfileNode*>(this); |
9359 i::Isolate* isolate = node->isolate(); | 9419 i::Isolate* isolate = node->isolate(); |
9360 const i::CodeEntry* entry = node->entry(); | 9420 const i::CodeEntry* entry = node->entry(); |
9361 i::Handle<i::String> name = | 9421 i::Handle<i::String> name = |
9362 isolate->factory()->InternalizeUtf8String(entry->name()); | 9422 isolate->factory()->InternalizeUtf8String(entry->name()); |
9363 if (!entry->has_name_prefix()) { | 9423 if (!entry->has_name_prefix()) { |
9364 return ToApiHandle<String>(name); | 9424 return ToApiHandle<String>(name); |
9365 } else { | 9425 } else { |
9366 // We do not expect this to fail. Change this if it does. | 9426 // We do not expect this to fail. Change this if it does. |
(...skipping 672 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
10039 Address callback_address = | 10099 Address callback_address = |
10040 reinterpret_cast<Address>(reinterpret_cast<intptr_t>(callback)); | 10100 reinterpret_cast<Address>(reinterpret_cast<intptr_t>(callback)); |
10041 VMState<EXTERNAL> state(isolate); | 10101 VMState<EXTERNAL> state(isolate); |
10042 ExternalCallbackScope call_scope(isolate, callback_address); | 10102 ExternalCallbackScope call_scope(isolate, callback_address); |
10043 callback(info); | 10103 callback(info); |
10044 } | 10104 } |
10045 | 10105 |
10046 | 10106 |
10047 } // namespace internal | 10107 } // namespace internal |
10048 } // namespace v8 | 10108 } // namespace v8 |
OLD | NEW |