OLD | NEW |
1 // Copyright 2014 the V8 project authors. All rights reserved. | 1 // Copyright 2014 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/factory.h" | 5 #include "src/factory.h" |
6 | 6 |
7 #include "src/allocation-site-scopes.h" | 7 #include "src/allocation-site-scopes.h" |
8 #include "src/base/bits.h" | 8 #include "src/base/bits.h" |
9 #include "src/bootstrapper.h" | 9 #include "src/bootstrapper.h" |
10 #include "src/conversions.h" | 10 #include "src/conversions.h" |
(...skipping 1578 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1589 isolate(), | 1589 isolate(), |
1590 isolate()->heap()->AllocateJSObjectFromMap( | 1590 isolate()->heap()->AllocateJSObjectFromMap( |
1591 *map, | 1591 *map, |
1592 pretenure, | 1592 pretenure, |
1593 allocation_site.is_null() ? NULL : *allocation_site), | 1593 allocation_site.is_null() ? NULL : *allocation_site), |
1594 JSObject); | 1594 JSObject); |
1595 } | 1595 } |
1596 | 1596 |
1597 | 1597 |
1598 Handle<JSArray> Factory::NewJSArray(ElementsKind elements_kind, | 1598 Handle<JSArray> Factory::NewJSArray(ElementsKind elements_kind, |
1599 Strength strength, | |
1600 PretenureFlag pretenure) { | 1599 PretenureFlag pretenure) { |
1601 Map* map = isolate()->get_initial_js_array_map(elements_kind, strength); | 1600 Map* map = isolate()->get_initial_js_array_map(elements_kind); |
1602 if (map == nullptr) { | 1601 if (map == nullptr) { |
1603 DCHECK(strength == Strength::WEAK); | |
1604 Context* native_context = isolate()->context()->native_context(); | 1602 Context* native_context = isolate()->context()->native_context(); |
1605 JSFunction* array_function = native_context->array_function(); | 1603 JSFunction* array_function = native_context->array_function(); |
1606 map = array_function->initial_map(); | 1604 map = array_function->initial_map(); |
1607 } | 1605 } |
1608 return Handle<JSArray>::cast(NewJSObjectFromMap(handle(map), pretenure)); | 1606 return Handle<JSArray>::cast(NewJSObjectFromMap(handle(map), pretenure)); |
1609 } | 1607 } |
1610 | 1608 |
1611 | |
1612 Handle<JSArray> Factory::NewJSArray(ElementsKind elements_kind, int length, | 1609 Handle<JSArray> Factory::NewJSArray(ElementsKind elements_kind, int length, |
1613 int capacity, Strength strength, | 1610 int capacity, |
1614 ArrayStorageAllocationMode mode, | 1611 ArrayStorageAllocationMode mode, |
1615 PretenureFlag pretenure) { | 1612 PretenureFlag pretenure) { |
1616 Handle<JSArray> array = NewJSArray(elements_kind, strength, pretenure); | 1613 Handle<JSArray> array = NewJSArray(elements_kind, pretenure); |
1617 NewJSArrayStorage(array, length, capacity, mode); | 1614 NewJSArrayStorage(array, length, capacity, mode); |
1618 return array; | 1615 return array; |
1619 } | 1616 } |
1620 | 1617 |
1621 | |
1622 Handle<JSArray> Factory::NewJSArrayWithElements(Handle<FixedArrayBase> elements, | 1618 Handle<JSArray> Factory::NewJSArrayWithElements(Handle<FixedArrayBase> elements, |
1623 ElementsKind elements_kind, | 1619 ElementsKind elements_kind, |
1624 int length, Strength strength, | 1620 int length, |
1625 PretenureFlag pretenure) { | 1621 PretenureFlag pretenure) { |
1626 DCHECK(length <= elements->length()); | 1622 DCHECK(length <= elements->length()); |
1627 Handle<JSArray> array = NewJSArray(elements_kind, strength, pretenure); | 1623 Handle<JSArray> array = NewJSArray(elements_kind, pretenure); |
1628 | 1624 |
1629 array->set_elements(*elements); | 1625 array->set_elements(*elements); |
1630 array->set_length(Smi::FromInt(length)); | 1626 array->set_length(Smi::FromInt(length)); |
1631 JSObject::ValidateElements(array); | 1627 JSObject::ValidateElements(array); |
1632 return array; | 1628 return array; |
1633 } | 1629 } |
1634 | 1630 |
1635 | 1631 |
1636 void Factory::NewJSArrayStorage(Handle<JSArray> array, | 1632 void Factory::NewJSArrayStorage(Handle<JSArray> array, |
1637 int length, | 1633 int length, |
(...skipping 655 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2293 // TODO(adamk): Currently the map is only created three times per | 2289 // TODO(adamk): Currently the map is only created three times per |
2294 // isolate. If it's created more often, the map should be moved into the | 2290 // isolate. If it's created more often, the map should be moved into the |
2295 // strong root list. | 2291 // strong root list. |
2296 Handle<Map> map = NewMap(JS_WEAK_MAP_TYPE, JSWeakMap::kSize); | 2292 Handle<Map> map = NewMap(JS_WEAK_MAP_TYPE, JSWeakMap::kSize); |
2297 return Handle<JSWeakMap>::cast(NewJSObjectFromMap(map)); | 2293 return Handle<JSWeakMap>::cast(NewJSObjectFromMap(map)); |
2298 } | 2294 } |
2299 | 2295 |
2300 | 2296 |
2301 Handle<Map> Factory::ObjectLiteralMapFromCache(Handle<Context> context, | 2297 Handle<Map> Factory::ObjectLiteralMapFromCache(Handle<Context> context, |
2302 int number_of_properties, | 2298 int number_of_properties, |
2303 bool is_strong, | |
2304 bool* is_result_from_cache) { | 2299 bool* is_result_from_cache) { |
2305 const int kMapCacheSize = 128; | 2300 const int kMapCacheSize = 128; |
2306 | 2301 |
2307 // We do not cache maps for too many properties or when running builtin code. | 2302 // We do not cache maps for too many properties or when running builtin code. |
2308 if (number_of_properties > kMapCacheSize || | 2303 if (number_of_properties > kMapCacheSize || |
2309 isolate()->bootstrapper()->IsActive()) { | 2304 isolate()->bootstrapper()->IsActive()) { |
2310 *is_result_from_cache = false; | 2305 *is_result_from_cache = false; |
2311 Handle<Map> map = Map::Create(isolate(), number_of_properties); | 2306 Handle<Map> map = Map::Create(isolate(), number_of_properties); |
2312 if (is_strong) map->set_is_strong(); | |
2313 return map; | 2307 return map; |
2314 } | 2308 } |
2315 *is_result_from_cache = true; | 2309 *is_result_from_cache = true; |
2316 if (number_of_properties == 0) { | 2310 if (number_of_properties == 0) { |
2317 // Reuse the initial map of the Object function if the literal has no | 2311 // Reuse the initial map of the Object function if the literal has no |
2318 // predeclared properties, or the strong map if strong. | 2312 // predeclared properties. |
2319 return handle(is_strong | 2313 return handle(context->object_function()->initial_map(), isolate()); |
2320 ? context->js_object_strong_map() | |
2321 : context->object_function()->initial_map(), isolate()); | |
2322 } | 2314 } |
2323 | 2315 |
2324 int cache_index = number_of_properties - 1; | 2316 int cache_index = number_of_properties - 1; |
2325 Handle<Object> maybe_cache(is_strong ? context->strong_map_cache() | 2317 Handle<Object> maybe_cache(context->map_cache(), isolate()); |
2326 : context->map_cache(), isolate()); | |
2327 if (maybe_cache->IsUndefined()) { | 2318 if (maybe_cache->IsUndefined()) { |
2328 // Allocate the new map cache for the native context. | 2319 // Allocate the new map cache for the native context. |
2329 maybe_cache = NewFixedArray(kMapCacheSize, TENURED); | 2320 maybe_cache = NewFixedArray(kMapCacheSize, TENURED); |
2330 if (is_strong) { | 2321 context->set_map_cache(*maybe_cache); |
2331 context->set_strong_map_cache(*maybe_cache); | |
2332 } else { | |
2333 context->set_map_cache(*maybe_cache); | |
2334 } | |
2335 } else { | 2322 } else { |
2336 // Check to see whether there is a matching element in the cache. | 2323 // Check to see whether there is a matching element in the cache. |
2337 Handle<FixedArray> cache = Handle<FixedArray>::cast(maybe_cache); | 2324 Handle<FixedArray> cache = Handle<FixedArray>::cast(maybe_cache); |
2338 Object* result = cache->get(cache_index); | 2325 Object* result = cache->get(cache_index); |
2339 if (result->IsWeakCell()) { | 2326 if (result->IsWeakCell()) { |
2340 WeakCell* cell = WeakCell::cast(result); | 2327 WeakCell* cell = WeakCell::cast(result); |
2341 if (!cell->cleared()) { | 2328 if (!cell->cleared()) { |
2342 return handle(Map::cast(cell->value()), isolate()); | 2329 return handle(Map::cast(cell->value()), isolate()); |
2343 } | 2330 } |
2344 } | 2331 } |
2345 } | 2332 } |
2346 // Create a new map and add it to the cache. | 2333 // Create a new map and add it to the cache. |
2347 Handle<FixedArray> cache = Handle<FixedArray>::cast(maybe_cache); | 2334 Handle<FixedArray> cache = Handle<FixedArray>::cast(maybe_cache); |
2348 Handle<Map> map = Map::Create(isolate(), number_of_properties); | 2335 Handle<Map> map = Map::Create(isolate(), number_of_properties); |
2349 if (is_strong) map->set_is_strong(); | |
2350 Handle<WeakCell> cell = NewWeakCell(map); | 2336 Handle<WeakCell> cell = NewWeakCell(map); |
2351 cache->set(cache_index, *cell); | 2337 cache->set(cache_index, *cell); |
2352 return map; | 2338 return map; |
2353 } | 2339 } |
2354 | 2340 |
2355 | 2341 |
2356 void Factory::SetRegExpAtomData(Handle<JSRegExp> regexp, | 2342 void Factory::SetRegExpAtomData(Handle<JSRegExp> regexp, |
2357 JSRegExp::Type type, | 2343 JSRegExp::Type type, |
2358 Handle<String> source, | 2344 Handle<String> source, |
2359 JSRegExp::Flags flags, | 2345 JSRegExp::Flags flags, |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2397 } | 2383 } |
2398 | 2384 |
2399 | 2385 |
2400 Handle<Object> Factory::ToBoolean(bool value) { | 2386 Handle<Object> Factory::ToBoolean(bool value) { |
2401 return value ? true_value() : false_value(); | 2387 return value ? true_value() : false_value(); |
2402 } | 2388 } |
2403 | 2389 |
2404 | 2390 |
2405 } // namespace internal | 2391 } // namespace internal |
2406 } // namespace v8 | 2392 } // namespace v8 |
OLD | NEW |