blob: 27e33293bdca2d54139cf18eb99c8c7473bd9b52 [file] [log] [blame]
// Copyright 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "src/heap/factory.h"
#include "src/allocation-site-scopes.h"
#include "src/ast/ast-source-ranges.h"
#include "src/ast/ast.h"
#include "src/base/bits.h"
#include "src/builtins/accessors.h"
#include "src/builtins/constants-table-builder.h"
#include "src/codegen/compiler.h"
#include "src/execution/isolate-inl.h"
#include "src/hash-seed-inl.h"
#include "src/heap/heap-inl.h"
#include "src/heap/incremental-marking.h"
#include "src/heap/mark-compact-inl.h"
#include "src/heap/read-only-heap.h"
#include "src/ic/handler-configuration-inl.h"
#include "src/init/bootstrapper.h"
#include "src/interpreter/interpreter.h"
#include "src/logging/counters.h"
#include "src/logging/log.h"
#include "src/numbers/conversions.h"
#include "src/objects/allocation-site-inl.h"
#include "src/objects/api-callbacks.h"
#include "src/objects/arguments-inl.h"
#include "src/objects/bigint.h"
#include "src/objects/cell-inl.h"
#include "src/objects/debug-objects-inl.h"
#include "src/objects/embedder-data-array-inl.h"
#include "src/objects/feedback-cell-inl.h"
#include "src/objects/fixed-array-inl.h"
#include "src/objects/foreign-inl.h"
#include "src/objects/frame-array-inl.h"
#include "src/objects/instance-type-inl.h"
#include "src/objects/js-array-inl.h"
#include "src/objects/js-collection-inl.h"
#include "src/objects/js-generator-inl.h"
#include "src/objects/js-regexp-inl.h"
#include "src/objects/js-weak-refs-inl.h"
#include "src/objects/literal-objects-inl.h"
#include "src/objects/microtask-inl.h"
#include "src/objects/module-inl.h"
#include "src/objects/promise-inl.h"
#include "src/objects/scope-info.h"
#include "src/objects/stack-frame-info-inl.h"
#include "src/objects/struct-inl.h"
#include "src/objects/template-objects-inl.h"
#include "src/objects/transitions-inl.h"
#include "src/strings/unicode-inl.h"
namespace v8 {
namespace internal {
namespace {
int ComputeCodeObjectSize(const CodeDesc& desc) {
bool has_unwinding_info = desc.unwinding_info != nullptr;
DCHECK((has_unwinding_info && desc.unwinding_info_size > 0) ||
(!has_unwinding_info && desc.unwinding_info_size == 0));
int body_size = desc.instr_size;
int unwinding_info_size_field_size = kInt64Size;
if (has_unwinding_info) {
body_size = RoundUp(body_size, kInt64Size) + desc.unwinding_info_size +
unwinding_info_size_field_size;
}
int object_size = Code::SizeFor(RoundUp(body_size, kObjectAlignment));
DCHECK(IsAligned(static_cast<intptr_t>(object_size), kCodeAlignment));
return object_size;
}
} // namespace
Factory::CodeBuilder::CodeBuilder(Isolate* isolate, const CodeDesc& desc,
Code::Kind kind)
: isolate_(isolate),
code_desc_(desc),
kind_(kind),
source_position_table_(isolate_->factory()->empty_byte_array()) {}
MaybeHandle<Code> Factory::CodeBuilder::BuildInternal(
bool retry_allocation_or_fail) {
const auto factory = isolate_->factory();
// Allocate objects needed for code initialization.
Handle<ByteArray> reloc_info =
factory->NewByteArray(code_desc_.reloc_size, AllocationType::kOld);
Handle<CodeDataContainer> data_container;
// Use a canonical off-heap trampoline CodeDataContainer if possible.
const int32_t promise_rejection_flag =
Code::IsPromiseRejectionField::encode(true);
if (read_only_data_container_ &&
(kind_specific_flags_ == 0 ||
kind_specific_flags_ == promise_rejection_flag)) {
const ReadOnlyRoots roots(isolate_);
const auto canonical_code_data_container =
kind_specific_flags_ == 0
? roots.trampoline_trivial_code_data_container_handle()
: roots.trampoline_promise_rejection_code_data_container_handle();
DCHECK_EQ(canonical_code_data_container->kind_specific_flags(),
kind_specific_flags_);
data_container = canonical_code_data_container;
} else {
data_container = factory->NewCodeDataContainer(
0, read_only_data_container_ ? AllocationType::kReadOnly
: AllocationType::kOld);
data_container->set_kind_specific_flags(kind_specific_flags_);
}
Handle<Code> code;
{
int object_size = ComputeCodeObjectSize(code_desc_);
Heap* heap = isolate_->heap();
CodePageCollectionMemoryModificationScope code_allocation(heap);
HeapObject result;
if (retry_allocation_or_fail) {
result =
heap->AllocateRawWithRetryOrFail(object_size, AllocationType::kCode);
} else {
result =
heap->AllocateRawWithLightRetry(object_size, AllocationType::kCode);
// Return an empty handle if we cannot allocate the code object.
if (result.is_null()) return MaybeHandle<Code>();
}
if (!is_movable_) {
result = heap->EnsureImmovableCode(result, object_size);
}
// The code object has not been fully initialized yet. We rely on the
// fact that no allocation will happen from this point on.
DisallowHeapAllocation no_gc;
result.set_map_after_allocation(*factory->code_map(), SKIP_WRITE_BARRIER);
code = handle(Code::cast(result), isolate_);
DCHECK(IsAligned(code->address(), kCodeAlignment));
DCHECK_IMPLIES(
!heap->memory_allocator()->code_range().is_empty(),
heap->memory_allocator()->code_range().contains(code->address()));
constexpr bool kIsNotOffHeapTrampoline = false;
const bool has_unwinding_info = code_desc_.unwinding_info != nullptr;
code->set_raw_instruction_size(code_desc_.instr_size);
code->set_relocation_info(*reloc_info);
code->initialize_flags(kind_, has_unwinding_info, is_turbofanned_,
stack_slots_, kIsNotOffHeapTrampoline);
code->set_builtin_index(builtin_index_);
code->set_code_data_container(*data_container);
code->set_deoptimization_data(*deoptimization_data_);
code->set_source_position_table(*source_position_table_);
code->set_safepoint_table_offset(code_desc_.safepoint_table_offset);
code->set_handler_table_offset(code_desc_.handler_table_offset);
code->set_constant_pool_offset(code_desc_.constant_pool_offset);
code->set_code_comments_offset(code_desc_.code_comments_offset);
// Allow self references to created code object by patching the handle to
// point to the newly allocated Code object.
Handle<Object> self_reference;
if (self_reference_.ToHandle(&self_reference)) {
DCHECK(self_reference->IsOddball());
DCHECK(Oddball::cast(*self_reference).kind() ==
Oddball::kSelfReferenceMarker);
if (FLAG_embedded_builtins) {
auto builder = isolate_->builtins_constants_table_builder();
if (builder != nullptr)
builder->PatchSelfReference(self_reference, code);
}
*(self_reference.location()) = code->ptr();
}
// Migrate generated code.
// The generated code can contain embedded objects (typically from handles)
// in a pointer-to-tagged-value format (i.e. with indirection like a handle)
// that are dereferenced during the copy to point directly to the actual
// heap objects. These pointers can include references to the code object
// itself, through the self_reference parameter.
code->CopyFromNoFlush(heap, code_desc_);
code->clear_padding();
#ifdef VERIFY_HEAP
if (FLAG_verify_heap) code->ObjectVerify(isolate_);
#endif
// Flush the instruction cache before changing the permissions.
// Note: we do this before setting permissions to ReadExecute because on
// some older ARM kernels there is a bug which causes an access error on
// cache flush instructions to trigger access error on non-writable memory.
// See https://bugs.chromium.org/p/v8/issues/detail?id=8157
code->FlushICache();
}
return code;
}
MaybeHandle<Code> Factory::CodeBuilder::TryBuild() {
return BuildInternal(false);
}
Handle<Code> Factory::CodeBuilder::Build() {
return BuildInternal(true).ToHandleChecked();
}
HeapObject Factory::AllocateRawWithImmortalMap(int size,
AllocationType allocation,
Map map,
AllocationAlignment alignment) {
HeapObject result = isolate()->heap()->AllocateRawWithRetryOrFail(
size, allocation, alignment);
result.set_map_after_allocation(map, SKIP_WRITE_BARRIER);
return result;
}
HeapObject Factory::AllocateRawWithAllocationSite(
Handle<Map> map, AllocationType allocation,
Handle<AllocationSite> allocation_site) {
DCHECK(map->instance_type() != MAP_TYPE);
int size = map->instance_size();
if (!allocation_site.is_null()) size += AllocationMemento::kSize;
HeapObject result =
isolate()->heap()->AllocateRawWithRetryOrFail(size, allocation);
WriteBarrierMode write_barrier_mode = allocation == AllocationType::kYoung
? SKIP_WRITE_BARRIER
: UPDATE_WRITE_BARRIER;
result.set_map_after_allocation(*map, write_barrier_mode);
if (!allocation_site.is_null()) {
AllocationMemento alloc_memento = AllocationMemento::unchecked_cast(
Object(result.ptr() + map->instance_size()));
InitializeAllocationMemento(alloc_memento, *allocation_site);
}
return result;
}
void Factory::InitializeAllocationMemento(AllocationMemento memento,
AllocationSite allocation_site) {
memento.set_map_after_allocation(*allocation_memento_map(),
SKIP_WRITE_BARRIER);
memento.set_allocation_site(allocation_site, SKIP_WRITE_BARRIER);
if (FLAG_allocation_site_pretenuring) {
allocation_site.IncrementMementoCreateCount();
}
}
HeapObject Factory::AllocateRawArray(int size, AllocationType allocation) {
HeapObject result =
isolate()->heap()->AllocateRawWithRetryOrFail(size, allocation);
if (size > kMaxRegularHeapObjectSize && FLAG_use_marking_progress_bar) {
MemoryChunk* chunk = MemoryChunk::FromHeapObject(result);
chunk->SetFlag<AccessMode::ATOMIC>(MemoryChunk::HAS_PROGRESS_BAR);
}
return result;
}
HeapObject Factory::AllocateRawFixedArray(int length,
AllocationType allocation) {
if (length < 0 || length > FixedArray::kMaxLength) {
isolate()->heap()->FatalProcessOutOfMemory("invalid array length");
}
return AllocateRawArray(FixedArray::SizeFor(length), allocation);
}
HeapObject Factory::AllocateRawWeakArrayList(int capacity,
AllocationType allocation) {
if (capacity < 0 || capacity > WeakArrayList::kMaxCapacity) {
isolate()->heap()->FatalProcessOutOfMemory("invalid array length");
}
return AllocateRawArray(WeakArrayList::SizeForCapacity(capacity), allocation);
}
HeapObject Factory::New(Handle<Map> map, AllocationType allocation) {
DCHECK(map->instance_type() != MAP_TYPE);
int size = map->instance_size();
HeapObject result =
isolate()->heap()->AllocateRawWithRetryOrFail(size, allocation);
// New space objects are allocated white.
WriteBarrierMode write_barrier_mode = allocation == AllocationType::kYoung
? SKIP_WRITE_BARRIER
: UPDATE_WRITE_BARRIER;
result.set_map_after_allocation(*map, write_barrier_mode);
return result;
}
Handle<HeapObject> Factory::NewFillerObject(int size, bool double_align,
AllocationType allocation) {
AllocationAlignment alignment = double_align ? kDoubleAligned : kWordAligned;
Heap* heap = isolate()->heap();
HeapObject result =
heap->AllocateRawWithRetryOrFail(size, allocation, alignment);
heap->CreateFillerObjectAt(result.address(), size, ClearRecordedSlots::kNo);
return Handle<HeapObject>(result, isolate());
}
Handle<PrototypeInfo> Factory::NewPrototypeInfo() {
Handle<PrototypeInfo> result = Handle<PrototypeInfo>::cast(
NewStruct(PROTOTYPE_INFO_TYPE, AllocationType::kOld));
result->set_prototype_users(Smi::kZero);
result->set_registry_slot(PrototypeInfo::UNREGISTERED);
result->set_bit_field(0);
result->set_module_namespace(*undefined_value());
return result;
}
Handle<EnumCache> Factory::NewEnumCache(Handle<FixedArray> keys,
Handle<FixedArray> indices) {
Handle<EnumCache> result = Handle<EnumCache>::cast(
NewStruct(ENUM_CACHE_TYPE, AllocationType::kOld));
result->set_keys(*keys);
result->set_indices(*indices);
return result;
}
Handle<Tuple2> Factory::NewTuple2(Handle<Object> value1, Handle<Object> value2,
AllocationType allocation) {
Handle<Tuple2> result =
Handle<Tuple2>::cast(NewStruct(TUPLE2_TYPE, allocation));
result->set_value1(*value1);
result->set_value2(*value2);
return result;
}
Handle<Tuple3> Factory::NewTuple3(Handle<Object> value1, Handle<Object> value2,
Handle<Object> value3,
AllocationType allocation) {
Handle<Tuple3> result =
Handle<Tuple3>::cast(NewStruct(TUPLE3_TYPE, allocation));
result->set_value1(*value1);
result->set_value2(*value2);
result->set_value3(*value3);
return result;
}
Handle<ArrayBoilerplateDescription> Factory::NewArrayBoilerplateDescription(
ElementsKind elements_kind, Handle<FixedArrayBase> constant_values) {
Handle<ArrayBoilerplateDescription> result =
Handle<ArrayBoilerplateDescription>::cast(
NewStruct(ARRAY_BOILERPLATE_DESCRIPTION_TYPE, AllocationType::kOld));
result->set_elements_kind(elements_kind);
result->set_constant_elements(*constant_values);
return result;
}
Handle<TemplateObjectDescription> Factory::NewTemplateObjectDescription(
Handle<FixedArray> raw_strings, Handle<FixedArray> cooked_strings) {
DCHECK_EQ(raw_strings->length(), cooked_strings->length());
DCHECK_LT(0, raw_strings->length());
Handle<TemplateObjectDescription> result =
Handle<TemplateObjectDescription>::cast(
NewStruct(TEMPLATE_OBJECT_DESCRIPTION_TYPE, AllocationType::kOld));
result->set_raw_strings(*raw_strings);
result->set_cooked_strings(*cooked_strings);
return result;
}
Handle<Oddball> Factory::NewOddball(Handle<Map> map, const char* to_string,
Handle<Object> to_number,
const char* type_of, byte kind,
AllocationType allocation) {
Handle<Oddball> oddball(Oddball::cast(New(map, allocation)), isolate());
Oddball::Initialize(isolate(), oddball, to_string, to_number, type_of, kind);
return oddball;
}
Handle<Oddball> Factory::NewSelfReferenceMarker(AllocationType allocation) {
return NewOddball(self_reference_marker_map(), "self_reference_marker",
handle(Smi::FromInt(-1), isolate()), "undefined",
Oddball::kSelfReferenceMarker, allocation);
}
Handle<PropertyArray> Factory::NewPropertyArray(int length,
AllocationType allocation) {
DCHECK_LE(0, length);
if (length == 0) return empty_property_array();
HeapObject result = AllocateRawFixedArray(length, allocation);
result.set_map_after_allocation(*property_array_map(), SKIP_WRITE_BARRIER);
Handle<PropertyArray> array(PropertyArray::cast(result), isolate());
array->initialize_length(length);
MemsetTagged(array->data_start(), *undefined_value(), length);
return array;
}
Handle<FixedArray> Factory::NewFixedArrayWithFiller(RootIndex map_root_index,
int length, Object filler,
AllocationType allocation) {
HeapObject result = AllocateRawFixedArray(length, allocation);
DCHECK(RootsTable::IsImmortalImmovable(map_root_index));
Map map = Map::cast(isolate()->root(map_root_index));
result.set_map_after_allocation(map, SKIP_WRITE_BARRIER);
Handle<FixedArray> array(FixedArray::cast(result), isolate());
array->set_length(length);
MemsetTagged(array->data_start(), filler, length);
return array;
}
template <typename T>
Handle<T> Factory::NewFixedArrayWithMap(RootIndex map_root_index, int length,
AllocationType allocation) {
static_assert(std::is_base_of<FixedArray, T>::value,
"T must be a descendant of FixedArray");
// Zero-length case must be handled outside, where the knowledge about
// the map is.
DCHECK_LT(0, length);
return Handle<T>::cast(NewFixedArrayWithFiller(
map_root_index, length, *undefined_value(), allocation));
}
template <typename T>
Handle<T> Factory::NewWeakFixedArrayWithMap(RootIndex map_root_index,
int length,
AllocationType allocation) {
static_assert(std::is_base_of<WeakFixedArray, T>::value,
"T must be a descendant of WeakFixedArray");
// Zero-length case must be handled outside.
DCHECK_LT(0, length);
HeapObject result =
AllocateRawArray(WeakFixedArray::SizeFor(length), allocation);
Map map = Map::cast(isolate()->root(map_root_index));
result.set_map_after_allocation(map, SKIP_WRITE_BARRIER);
Handle<WeakFixedArray> array(WeakFixedArray::cast(result), isolate());
array->set_length(length);
MemsetTagged(ObjectSlot(array->data_start()), *undefined_value(), length);
return Handle<T>::cast(array);
}
template Handle<FixedArray> Factory::NewFixedArrayWithMap<FixedArray>(
RootIndex, int, AllocationType allocation);
Handle<FixedArray> Factory::NewFixedArray(int length,
AllocationType allocation) {
DCHECK_LE(0, length);
if (length == 0) return empty_fixed_array();
return NewFixedArrayWithFiller(RootIndex::kFixedArrayMap, length,
*undefined_value(), allocation);
}
Handle<WeakFixedArray> Factory::NewWeakFixedArray(int length,
AllocationType allocation) {
DCHECK_LE(0, length);
if (length == 0) return empty_weak_fixed_array();
HeapObject result =
AllocateRawArray(WeakFixedArray::SizeFor(length), allocation);
DCHECK(RootsTable::IsImmortalImmovable(RootIndex::kWeakFixedArrayMap));
result.set_map_after_allocation(*weak_fixed_array_map(), SKIP_WRITE_BARRIER);
Handle<WeakFixedArray> array(WeakFixedArray::cast(result), isolate());
array->set_length(length);
MemsetTagged(ObjectSlot(array->data_start()), *undefined_value(), length);
return array;
}
MaybeHandle<FixedArray> Factory::TryNewFixedArray(
int length, AllocationType allocation_type) {
DCHECK_LE(0, length);
if (length == 0) return empty_fixed_array();
int size = FixedArray::SizeFor(length);
Heap* heap = isolate()->heap();
AllocationResult allocation = heap->AllocateRaw(size, allocation_type);
HeapObject result;
if (!allocation.To(&result)) return MaybeHandle<FixedArray>();
if (size > kMaxRegularHeapObjectSize && FLAG_use_marking_progress_bar) {
MemoryChunk* chunk = MemoryChunk::FromHeapObject(result);
chunk->SetFlag<AccessMode::ATOMIC>(MemoryChunk::HAS_PROGRESS_BAR);
}
result.set_map_after_allocation(*fixed_array_map(), SKIP_WRITE_BARRIER);
Handle<FixedArray> array(FixedArray::cast(result), isolate());
array->set_length(length);
MemsetTagged(array->data_start(), ReadOnlyRoots(heap).undefined_value(),
length);
return array;
}
Handle<FixedArray> Factory::NewFixedArrayWithHoles(int length,
AllocationType allocation) {
DCHECK_LE(0, length);
if (length == 0) return empty_fixed_array();
return NewFixedArrayWithFiller(RootIndex::kFixedArrayMap, length,
*the_hole_value(), allocation);
}
Handle<FixedArray> Factory::NewUninitializedFixedArray(
int length, AllocationType allocation) {
DCHECK_LE(0, length);
if (length == 0) return empty_fixed_array();
// TODO(ulan): As an experiment this temporarily returns an initialized fixed
// array. After getting canary/performance coverage, either remove the
// function or revert to returning uninitilized array.
return NewFixedArrayWithFiller(RootIndex::kFixedArrayMap, length,
*undefined_value(), allocation);
}
Handle<ClosureFeedbackCellArray> Factory::NewClosureFeedbackCellArray(
int length, AllocationType allocation) {
if (length == 0) return empty_closure_feedback_cell_array();
Handle<ClosureFeedbackCellArray> feedback_cell_array =
NewFixedArrayWithMap<ClosureFeedbackCellArray>(
RootIndex::kClosureFeedbackCellArrayMap, length, allocation);
return feedback_cell_array;
}
Handle<FeedbackVector> Factory::NewFeedbackVector(
Handle<SharedFunctionInfo> shared,
Handle<ClosureFeedbackCellArray> closure_feedback_cell_array,
AllocationType allocation) {
int length = shared->feedback_metadata().slot_count();
DCHECK_LE(0, length);
int size = FeedbackVector::SizeFor(length);
HeapObject result =
AllocateRawWithImmortalMap(size, allocation, *feedback_vector_map());
Handle<FeedbackVector> vector(FeedbackVector::cast(result), isolate());
vector->set_shared_function_info(*shared);
vector->set_optimized_code_weak_or_smi(MaybeObject::FromSmi(Smi::FromEnum(
FLAG_log_function_events ? OptimizationMarker::kLogFirstExecution
: OptimizationMarker::kNone)));
vector->set_length(length);
vector->set_invocation_count(0);
vector->set_profiler_ticks(0);
vector->set_deopt_count(0);
vector->set_closure_feedback_cell_array(*closure_feedback_cell_array);
// TODO(leszeks): Initialize based on the feedback metadata.
MemsetTagged(ObjectSlot(vector->slots_start()), *undefined_value(), length);
return vector;
}
Handle<EmbedderDataArray> Factory::NewEmbedderDataArray(
int length, AllocationType allocation) {
DCHECK_LE(0, length);
int size = EmbedderDataArray::SizeFor(length);
HeapObject result =
AllocateRawWithImmortalMap(size, allocation, *embedder_data_array_map());
Handle<EmbedderDataArray> array(EmbedderDataArray::cast(result), isolate());
array->set_length(length);
if (length > 0) {
ObjectSlot start(array->slots_start());
ObjectSlot end(array->slots_end());
size_t slot_count = end - start;
MemsetTagged(start, *undefined_value(), slot_count);
}
return array;
}
Handle<ObjectBoilerplateDescription> Factory::NewObjectBoilerplateDescription(
int boilerplate, int all_properties, int index_keys, bool has_seen_proto) {
DCHECK_GE(boilerplate, 0);
DCHECK_GE(all_properties, index_keys);
DCHECK_GE(index_keys, 0);
int backing_store_size =
all_properties - index_keys - (has_seen_proto ? 1 : 0);
DCHECK_GE(backing_store_size, 0);
bool has_different_size_backing_store = boilerplate != backing_store_size;
// Space for name and value for every boilerplate property + LiteralType flag.
int size =
2 * boilerplate + ObjectBoilerplateDescription::kDescriptionStartIndex;
if (has_different_size_backing_store) {
// An extra entry for the backing store size.
size++;
}
Handle<ObjectBoilerplateDescription> description =
Handle<ObjectBoilerplateDescription>::cast(
NewFixedArrayWithMap(RootIndex::kObjectBoilerplateDescriptionMap,
size, AllocationType::kOld));
if (has_different_size_backing_store) {
DCHECK_IMPLIES((boilerplate == (all_properties - index_keys)),
has_seen_proto);
description->set_backing_store_size(isolate(), backing_store_size);
}
description->set_flags(0);
return description;
}
Handle<FixedArrayBase> Factory::NewFixedDoubleArray(int length,
AllocationType allocation) {
if (length == 0) return empty_fixed_array();
if (length < 0 || length > FixedDoubleArray::kMaxLength) {
isolate()->heap()->FatalProcessOutOfMemory("invalid array length");
}
int size = FixedDoubleArray::SizeFor(length);
Map map = *fixed_double_array_map();
HeapObject result =
AllocateRawWithImmortalMap(size, allocation, map, kDoubleAligned);
Handle<FixedDoubleArray> array(FixedDoubleArray::cast(result), isolate());
array->set_length(length);
return array;
}
Handle<FixedArrayBase> Factory::NewFixedDoubleArrayWithHoles(
int length, AllocationType allocation) {
DCHECK_LE(0, length);
Handle<FixedArrayBase> array = NewFixedDoubleArray(length, allocation);
if (length > 0) {
Handle<FixedDoubleArray>::cast(array)->FillWithHoles(0, length);
}
return array;
}
Handle<FeedbackMetadata> Factory::NewFeedbackMetadata(
int slot_count, int feedback_cell_count, AllocationType allocation) {
DCHECK_LE(0, slot_count);
int size = FeedbackMetadata::SizeFor(slot_count);
HeapObject result =
AllocateRawWithImmortalMap(size, allocation, *feedback_metadata_map());
Handle<FeedbackMetadata> data(FeedbackMetadata::cast(result), isolate());
data->set_slot_count(slot_count);
data->set_closure_feedback_cell_count(feedback_cell_count);
// Initialize the data section to 0.
int data_size = size - FeedbackMetadata::kHeaderSize;
Address data_start = data->address() + FeedbackMetadata::kHeaderSize;
memset(reinterpret_cast<byte*>(data_start), 0, data_size);
// Fields have been zeroed out but not initialized, so this object will not
// pass object verification at this point.
return data;
}
Handle<FrameArray> Factory::NewFrameArray(int number_of_frames,
AllocationType allocation) {
DCHECK_LE(0, number_of_frames);
Handle<FixedArray> result = NewFixedArrayWithHoles(
FrameArray::LengthFor(number_of_frames), allocation);
result->set(FrameArray::kFrameCountIndex, Smi::kZero);
return Handle<FrameArray>::cast(result);
}
template <typename T>
Handle<T> Factory::AllocateSmallOrderedHashTable(Handle<Map> map, int capacity,
AllocationType allocation) {
// Capacity must be a power of two, since we depend on being able
// to divide and multiple by 2 (kLoadFactor) to derive capacity
// from number of buckets. If we decide to change kLoadFactor
// to something other than 2, capacity should be stored as another
// field of this object.
DCHECK_EQ(T::kLoadFactor, 2);
capacity = base::bits::RoundUpToPowerOfTwo32(Max(T::kMinCapacity, capacity));
capacity = Min(capacity, T::kMaxCapacity);
DCHECK_LT(0, capacity);
DCHECK_EQ(0, capacity % T::kLoadFactor);
int size = T::SizeFor(capacity);
HeapObject result = AllocateRawWithImmortalMap(size, allocation, *map);
Handle<T> table(T::cast(result), isolate());
table->Initialize(isolate(), capacity);
return table;
}
Handle<SmallOrderedHashSet> Factory::NewSmallOrderedHashSet(
int capacity, AllocationType allocation) {
return AllocateSmallOrderedHashTable<SmallOrderedHashSet>(
small_ordered_hash_set_map(), capacity, allocation);
}
Handle<SmallOrderedHashMap> Factory::NewSmallOrderedHashMap(
int capacity, AllocationType allocation) {
return AllocateSmallOrderedHashTable<SmallOrderedHashMap>(
small_ordered_hash_map_map(), capacity, allocation);
}
Handle<SmallOrderedNameDictionary> Factory::NewSmallOrderedNameDictionary(
int capacity, AllocationType allocation) {
Handle<SmallOrderedNameDictionary> dict =
AllocateSmallOrderedHashTable<SmallOrderedNameDictionary>(
small_ordered_name_dictionary_map(), capacity, allocation);
dict->SetHash(PropertyArray::kNoHashSentinel);
return dict;
}
Handle<OrderedHashSet> Factory::NewOrderedHashSet() {
return OrderedHashSet::Allocate(isolate(), OrderedHashSet::kMinCapacity);
}
Handle<OrderedHashMap> Factory::NewOrderedHashMap() {
return OrderedHashMap::Allocate(isolate(), OrderedHashMap::kMinCapacity);
}
Handle<OrderedNameDictionary> Factory::NewOrderedNameDictionary() {
return OrderedNameDictionary::Allocate(isolate(),
OrderedNameDictionary::kMinCapacity);
}
Handle<AccessorPair> Factory::NewAccessorPair() {
Handle<AccessorPair> accessors = Handle<AccessorPair>::cast(
NewStruct(ACCESSOR_PAIR_TYPE, AllocationType::kOld));
accessors->set_getter(*null_value(), SKIP_WRITE_BARRIER);
accessors->set_setter(*null_value(), SKIP_WRITE_BARRIER);
return accessors;
}
// Internalized strings are created in the old generation (data space).
Handle<String> Factory::InternalizeUtf8String(
const Vector<const char>& string) {
Vector<const uint8_t> utf8_data = Vector<const uint8_t>::cast(string);
Utf8Decoder decoder(utf8_data);
if (decoder.is_ascii()) return InternalizeString(utf8_data);
if (decoder.is_one_byte()) {
std::unique_ptr<uint8_t[]> buffer(new uint8_t[decoder.utf16_length()]);
decoder.Decode(buffer.get(), utf8_data);
return InternalizeString(
Vector<const uint8_t>(buffer.get(), decoder.utf16_length()));
}
std::unique_ptr<uint16_t[]> buffer(new uint16_t[decoder.utf16_length()]);
decoder.Decode(buffer.get(), utf8_data);
return InternalizeString(
Vector<const uc16>(buffer.get(), decoder.utf16_length()));
}
template <typename Char>
Handle<String> Factory::InternalizeString(const Vector<const Char>& string,
bool convert_encoding) {
SequentialStringKey<Char> key(string, HashSeed(isolate()), convert_encoding);
return InternalizeStringWithKey(&key);
}
template EXPORT_TEMPLATE_DEFINE(V8_EXPORT_PRIVATE)
Handle<String> Factory::InternalizeString(
const Vector<const uint8_t>& string, bool convert_encoding);
template EXPORT_TEMPLATE_DEFINE(V8_EXPORT_PRIVATE)
Handle<String> Factory::InternalizeString(
const Vector<const uint16_t>& string, bool convert_encoding);
template <typename SeqString>
Handle<String> Factory::InternalizeString(Handle<SeqString> string, int from,
int length, bool convert_encoding) {
SeqSubStringKey<SeqString> key(isolate(), string, from, length,
convert_encoding);
return InternalizeStringWithKey(&key);
}
template Handle<String> Factory::InternalizeString(
Handle<SeqOneByteString> string, int from, int length,
bool convert_encoding);
template Handle<String> Factory::InternalizeString(
Handle<SeqTwoByteString> string, int from, int length,
bool convert_encoding);
template <class StringTableKey>
Handle<String> Factory::InternalizeStringWithKey(StringTableKey* key) {
return StringTable::LookupKey(isolate(), key);
}
MaybeHandle<String> Factory::NewStringFromOneByte(
const Vector<const uint8_t>& string, AllocationType allocation) {
DCHECK_NE(allocation, AllocationType::kReadOnly);
int length = string.length();
if (length == 0) return empty_string();
if (length == 1) return LookupSingleCharacterStringFromCode(string[0]);
Handle<SeqOneByteString> result;
ASSIGN_RETURN_ON_EXCEPTION(isolate(), result,
NewRawOneByteString(string.length(), allocation),
String);
DisallowHeapAllocation no_gc;
// Copy the characters into the new object.
CopyChars(SeqOneByteString::cast(*result).GetChars(no_gc), string.begin(),
length);
return result;
}
MaybeHandle<String> Factory::NewStringFromUtf8(const Vector<const char>& string,
AllocationType allocation) {
Vector<const uint8_t> utf8_data = Vector<const uint8_t>::cast(string);
Utf8Decoder decoder(utf8_data);
if (decoder.utf16_length() == 0) return empty_string();
if (decoder.is_one_byte()) {
// Allocate string.
Handle<SeqOneByteString> result;
ASSIGN_RETURN_ON_EXCEPTION(
isolate(), result,
NewRawOneByteString(decoder.utf16_length(), allocation), String);
DisallowHeapAllocation no_gc;
decoder.Decode(result->GetChars(no_gc), utf8_data);
return result;
}
// Allocate string.
Handle<SeqTwoByteString> result;
ASSIGN_RETURN_ON_EXCEPTION(
isolate(), result,
NewRawTwoByteString(decoder.utf16_length(), allocation), String);
DisallowHeapAllocation no_gc;
decoder.Decode(result->GetChars(no_gc), utf8_data);
return result;
}
MaybeHandle<String> Factory::NewStringFromUtf8SubString(
Handle<SeqOneByteString> str, int begin, int length,
AllocationType allocation) {
Vector<const uint8_t> utf8_data;
{
DisallowHeapAllocation no_gc;
utf8_data = Vector<const uint8_t>(str->GetChars(no_gc) + begin, length);
}
Utf8Decoder decoder(utf8_data);
if (length == 1) {
uint16_t t;
// Decode even in the case of length 1 since it can be a bad character.
decoder.Decode(&t, utf8_data);
return LookupSingleCharacterStringFromCode(t);
}
if (decoder.is_ascii()) {
// If the string is ASCII, we can just make a substring.
// TODO(v8): the allocation flag is ignored in this case.
return NewSubString(str, begin, begin + length);
}
DCHECK_GT(decoder.utf16_length(), 0);
if (decoder.is_one_byte()) {
// Allocate string.
Handle<SeqOneByteString> result;
ASSIGN_RETURN_ON_EXCEPTION(
isolate(), result,
NewRawOneByteString(decoder.utf16_length(), allocation), String);
DisallowHeapAllocation no_gc;
// Update pointer references, since the original string may have moved after
// allocation.
utf8_data = Vector<const uint8_t>(str->GetChars(no_gc) + begin, length);
decoder.Decode(result->GetChars(no_gc), utf8_data);
return result;
}
// Allocate string.
Handle<SeqTwoByteString> result;
ASSIGN_RETURN_ON_EXCEPTION(
isolate(), result,
NewRawTwoByteString(decoder.utf16_length(), allocation), String);
DisallowHeapAllocation no_gc;
// Update pointer references, since the original string may have moved after
// allocation.
utf8_data = Vector<const uint8_t>(str->GetChars(no_gc) + begin, length);
decoder.Decode(result->GetChars(no_gc), utf8_data);
return result;
}
MaybeHandle<String> Factory::NewStringFromTwoByte(const uc16* string,
int length,
AllocationType allocation) {
DCHECK_NE(allocation, AllocationType::kReadOnly);
if (length == 0) return empty_string();
if (String::IsOneByte(string, length)) {
if (length == 1) return LookupSingleCharacterStringFromCode(string[0]);
Handle<SeqOneByteString> result;
ASSIGN_RETURN_ON_EXCEPTION(isolate(), result,
NewRawOneByteString(length, allocation), String);
DisallowHeapAllocation no_gc;
CopyChars(result->GetChars(no_gc), string, length);
return result;
} else {
Handle<SeqTwoByteString> result;
ASSIGN_RETURN_ON_EXCEPTION(isolate(), result,
NewRawTwoByteString(length, allocation), String);
DisallowHeapAllocation no_gc;
CopyChars(result->GetChars(no_gc), string, length);
return result;
}
}
MaybeHandle<String> Factory::NewStringFromTwoByte(
const Vector<const uc16>& string, AllocationType allocation) {
return NewStringFromTwoByte(string.begin(), string.length(), allocation);
}
MaybeHandle<String> Factory::NewStringFromTwoByte(
const ZoneVector<uc16>* string, AllocationType allocation) {
return NewStringFromTwoByte(string->data(), static_cast<int>(string->size()),
allocation);
}
namespace {
bool inline IsOneByte(Handle<String> str) {
return str->IsOneByteRepresentation();
}
inline void WriteOneByteData(Handle<String> s, uint8_t* chars, int len) {
DCHECK(s->length() == len);
String::WriteToFlat(*s, chars, 0, len);
}
inline void WriteTwoByteData(Handle<String> s, uint16_t* chars, int len) {
DCHECK(s->length() == len);
String::WriteToFlat(*s, chars, 0, len);
}
} // namespace
Handle<SeqOneByteString> Factory::AllocateRawOneByteInternalizedString(
int length, uint32_t hash_field) {
CHECK_GE(String::kMaxLength, length);
// The canonical empty_string is the only zero-length string we allow.
DCHECK_IMPLIES(
length == 0,
isolate()->roots_table()[RootIndex::kempty_string] == kNullAddress);
Map map = *one_byte_internalized_string_map();
int size = SeqOneByteString::SizeFor(length);
HeapObject result =
AllocateRawWithImmortalMap(size,
isolate()->heap()->CanAllocateInReadOnlySpace()
? AllocationType::kReadOnly
: AllocationType::kOld,
map);
Handle<SeqOneByteString> answer(SeqOneByteString::cast(result), isolate());
answer->set_length(length);
answer->set_hash_field(hash_field);
DCHECK_EQ(size, answer->Size());
return answer;
}
Handle<String> Factory::AllocateTwoByteInternalizedString(
const Vector<const uc16>& str, uint32_t hash_field) {
Handle<SeqTwoByteString> result =
AllocateRawTwoByteInternalizedString(str.length(), hash_field);
DisallowHeapAllocation no_gc;
// Fill in the characters.
MemCopy(result->GetChars(no_gc), str.begin(), str.length() * kUC16Size);
return result;
}
Handle<SeqTwoByteString> Factory::AllocateRawTwoByteInternalizedString(
int length, uint32_t hash_field) {
CHECK_GE(String::kMaxLength, length);
DCHECK_NE(0, length); // Use Heap::empty_string() instead.
Map map = *internalized_string_map();
int size = SeqTwoByteString::SizeFor(length);
HeapObject result =
AllocateRawWithImmortalMap(size, AllocationType::kOld, map);
Handle<SeqTwoByteString> answer(SeqTwoByteString::cast(result), isolate());
answer->set_length(length);
answer->set_hash_field(hash_field);
DCHECK_EQ(size, result.Size());
return answer;
}
template <bool is_one_byte, typename T>
Handle<String> Factory::AllocateInternalizedStringImpl(T t, int chars,
uint32_t hash_field) {
DCHECK_LE(0, chars);
DCHECK_GE(String::kMaxLength, chars);
// Compute map and object size.
int size;
Map map;
if (is_one_byte) {
map = *one_byte_internalized_string_map();
size = SeqOneByteString::SizeFor(chars);
} else {
map = *internalized_string_map();
size = SeqTwoByteString::SizeFor(chars);
}
HeapObject result =
AllocateRawWithImmortalMap(size,
isolate()->heap()->CanAllocateInReadOnlySpace()
? AllocationType::kReadOnly
: AllocationType::kOld,
map);
Handle<String> answer(String::cast(result), isolate());
answer->set_length(chars);
answer->set_hash_field(hash_field);
DCHECK_EQ(size, answer->Size());
DisallowHeapAllocation no_gc;
if (is_one_byte) {
WriteOneByteData(t, SeqOneByteString::cast(*answer).GetChars(no_gc), chars);
} else {
WriteTwoByteData(t, SeqTwoByteString::cast(*answer).GetChars(no_gc), chars);
}
return answer;
}
Handle<String> Factory::NewOneByteInternalizedString(
const Vector<const uint8_t>& str, uint32_t hash_field) {
Handle<SeqOneByteString> result =
AllocateRawOneByteInternalizedString(str.length(), hash_field);
DisallowHeapAllocation no_allocation;
MemCopy(result->GetChars(no_allocation), str.begin(), str.length());
return result;
}
Handle<String> Factory::NewTwoByteInternalizedString(
const Vector<const uc16>& str, uint32_t hash_field) {
return AllocateTwoByteInternalizedString(str, hash_field);
}
Handle<String> Factory::NewInternalizedStringImpl(Handle<String> string,
int chars,
uint32_t hash_field) {
if (IsOneByte(string)) {
return AllocateInternalizedStringImpl<true>(string, chars, hash_field);
}
return AllocateInternalizedStringImpl<false>(string, chars, hash_field);
}
namespace {
MaybeHandle<Map> GetInternalizedStringMap(Factory* f, Handle<String> string) {
switch (string->map().instance_type()) {
case STRING_TYPE:
return f->internalized_string_map();
case ONE_BYTE_STRING_TYPE:
return f->one_byte_internalized_string_map();
case EXTERNAL_STRING_TYPE:
return f->external_internalized_string_map();
case EXTERNAL_ONE_BYTE_STRING_TYPE:
return f->external_one_byte_internalized_string_map();
case UNCACHED_EXTERNAL_STRING_TYPE:
return f->uncached_external_internalized_string_map();
case UNCACHED_EXTERNAL_ONE_BYTE_STRING_TYPE:
return f->uncached_external_one_byte_internalized_string_map();
default:
return MaybeHandle<Map>(); // No match found.
}
}
} // namespace
MaybeHandle<Map> Factory::InternalizedStringMapForString(
Handle<String> string) {
// If the string is in the young generation, it cannot be used as
// internalized.
if (Heap::InYoungGeneration(*string)) return MaybeHandle<Map>();
return GetInternalizedStringMap(this, string);
}
template <class StringClass>
Handle<StringClass> Factory::InternalizeExternalString(Handle<String> string) {
Handle<StringClass> cast_string = Handle<StringClass>::cast(string);
Handle<Map> map = GetInternalizedStringMap(this, string).ToHandleChecked();
Handle<StringClass> external_string(
StringClass::cast(New(map, AllocationType::kOld)), isolate());
external_string->set_length(cast_string->length());
external_string->set_hash_field(cast_string->hash_field());
external_string->SetResource(isolate(), nullptr);
isolate()->heap()->RegisterExternalString(*external_string);
return external_string;
}
template Handle<ExternalOneByteString>
Factory::InternalizeExternalString<ExternalOneByteString>(Handle<String>);
template Handle<ExternalTwoByteString>
Factory::InternalizeExternalString<ExternalTwoByteString>(Handle<String>);
MaybeHandle<SeqOneByteString> Factory::NewRawOneByteString(
int length, AllocationType allocation) {
if (length > String::kMaxLength || length < 0) {
THROW_NEW_ERROR(isolate(), NewInvalidStringLengthError(), SeqOneByteString);
}
DCHECK_GT(length, 0); // Use Factory::empty_string() instead.
int size = SeqOneByteString::SizeFor(length);
DCHECK_GE(SeqOneByteString::kMaxSize, size);
HeapObject result =
AllocateRawWithImmortalMap(size, allocation, *one_byte_string_map());
Handle<SeqOneByteString> string(SeqOneByteString::cast(result), isolate());
string->set_length(length);
string->set_hash_field(String::kEmptyHashField);
DCHECK_EQ(size, string->Size());
return string;
}
MaybeHandle<SeqTwoByteString> Factory::NewRawTwoByteString(
int length, AllocationType allocation) {
if (length > String::kMaxLength || length < 0) {
THROW_NEW_ERROR(isolate(), NewInvalidStringLengthError(), SeqTwoByteString);
}
DCHECK_GT(length, 0); // Use Factory::empty_string() instead.
int size = SeqTwoByteString::SizeFor(length);
DCHECK_GE(SeqTwoByteString::kMaxSize, size);
HeapObject result =
AllocateRawWithImmortalMap(size, allocation, *string_map());
Handle<SeqTwoByteString> string(SeqTwoByteString::cast(result), isolate());
string->set_length(length);
string->set_hash_field(String::kEmptyHashField);
DCHECK_EQ(size, string->Size());
return string;
}
Handle<String> Factory::LookupSingleCharacterStringFromCode(uint16_t code) {
if (code <= unibrow::Latin1::kMaxChar) {
{
DisallowHeapAllocation no_allocation;
Object value = single_character_string_cache()->get(code);
if (value != *undefined_value()) {
return handle(String::cast(value), isolate());
}
}
uint8_t buffer[] = {static_cast<uint8_t>(code)};
Handle<String> result = InternalizeString(Vector<const uint8_t>(buffer, 1));
single_character_string_cache()->set(code, *result);
return result;
}
uint16_t buffer[] = {code};
return InternalizeString(Vector<const uint16_t>(buffer, 1));
}
static inline Handle<String> MakeOrFindTwoCharacterString(Isolate* isolate,
uint16_t c1,
uint16_t c2) {
if ((c1 | c2) <= unibrow::Latin1::kMaxChar) {
uint8_t buffer[] = {static_cast<uint8_t>(c1), static_cast<uint8_t>(c2)};
return isolate->factory()->InternalizeString(
Vector<const uint8_t>(buffer, 2));
}
uint16_t buffer[] = {c1, c2};
return isolate->factory()->InternalizeString(
Vector<const uint16_t>(buffer, 2));
}
template <typename SinkChar, typename StringType>
Handle<String> ConcatStringContent(Handle<StringType> result,
Handle<String> first,
Handle<String> second) {
DisallowHeapAllocation pointer_stays_valid;
SinkChar* sink = result->GetChars(pointer_stays_valid);
String::WriteToFlat(*first, sink, 0, first->length());
String::WriteToFlat(*second, sink + first->length(), 0, second->length());
return result;
}
MaybeHandle<String> Factory::NewConsString(Handle<String> left,
Handle<String> right) {
if (left->IsThinString()) {
left = handle(Handle<ThinString>::cast(left)->actual(), isolate());
}
if (right->IsThinString()) {
right = handle(Handle<ThinString>::cast(right)->actual(), isolate());
}
int left_length = left->length();
if (left_length == 0) return right;
int right_length = right->length();
if (right_length == 0) return left;
int length = left_length + right_length;
if (length == 2) {
uint16_t c1 = left->Get(0);
uint16_t c2 = right->Get(0);
return MakeOrFindTwoCharacterString(isolate(), c1, c2);
}
// Make sure that an out of memory exception is thrown if the length
// of the new cons string is too large.
if (length > String::kMaxLength || length < 0) {
THROW_NEW_ERROR(isolate(), NewInvalidStringLengthError(), String);
}
bool left_is_one_byte = left->IsOneByteRepresentation();
bool right_is_one_byte = right->IsOneByteRepresentation();
bool is_one_byte = left_is_one_byte && right_is_one_byte;
// If the resulting string is small make a flat string.
if (length < ConsString::kMinLength) {
// Note that neither of the two inputs can be a slice because:
STATIC_ASSERT(ConsString::kMinLength <= SlicedString::kMinLength);
DCHECK(left->IsFlat());
DCHECK(right->IsFlat());
STATIC_ASSERT(ConsString::kMinLength <= String::kMaxLength);
if (is_one_byte) {
Handle<SeqOneByteString> result =
NewRawOneByteString(length).ToHandleChecked();
DisallowHeapAllocation no_gc;
uint8_t* dest = result->GetChars(no_gc);
// Copy left part.
const uint8_t* src =
left->IsExternalString()
? Handle<ExternalOneByteString>::cast(left)->GetChars()
: Handle<SeqOneByteString>::cast(left)->GetChars(no_gc);
for (int i = 0; i < left_length; i++) *dest++ = src[i];
// Copy right part.
src = right->IsExternalString()
? Handle<ExternalOneByteString>::cast(right)->GetChars()
: Handle<SeqOneByteString>::cast(right)->GetChars(no_gc);
for (int i = 0; i < right_length; i++) *dest++ = src[i];
return result;
}
return ConcatStringContent<uc16>(
NewRawTwoByteString(length).ToHandleChecked(), left, right);
}
return NewConsString(left, right, length, is_one_byte);
}
Handle<String> Factory::NewConsString(Handle<String> left, Handle<String> right,
int length, bool one_byte) {
DCHECK(!left->IsThinString());
DCHECK(!right->IsThinString());
DCHECK_GE(length, ConsString::kMinLength);
DCHECK_LE(length, String::kMaxLength);
Handle<ConsString> result(
ConsString::cast(
one_byte ? New(cons_one_byte_string_map(), AllocationType::kYoung)
: New(cons_string_map(), AllocationType::kYoung)),
isolate());
DisallowHeapAllocation no_gc;
WriteBarrierMode mode = result->GetWriteBarrierMode(no_gc);
result->set_hash_field(String::kEmptyHashField);
result->set_length(length);
result->set_first(isolate(), *left, mode);
result->set_second(isolate(), *right, mode);
return result;
}
Handle<String> Factory::NewSurrogatePairString(uint16_t lead, uint16_t trail) {
DCHECK_GE(lead, 0xD800);
DCHECK_LE(lead, 0xDBFF);
DCHECK_GE(trail, 0xDC00);
DCHECK_LE(trail, 0xDFFF);
Handle<SeqTwoByteString> str =
isolate()->factory()->NewRawTwoByteString(2).ToHandleChecked();
DisallowHeapAllocation no_allocation;
uc16* dest = str->GetChars(no_allocation);
dest[0] = lead;
dest[1] = trail;
return str;
}
Handle<String> Factory::NewProperSubString(Handle<String> str, int begin,
int end) {
#if VERIFY_HEAP
if (FLAG_verify_heap) str->StringVerify(isolate());
#endif
DCHECK(begin > 0 || end < str->length());
str = String::Flatten(isolate(), str);
int length = end - begin;
if (length <= 0) return empty_string();
if (length == 1) {
return LookupSingleCharacterStringFromCode(str->Get(begin));
}
if (length == 2) {
// Optimization for 2-byte strings often used as keys in a decompression
// dictionary. Check whether we already have the string in the string
// table to prevent creation of many unnecessary strings.
uint16_t c1 = str->Get(begin);
uint16_t c2 = str->Get(begin + 1);
return MakeOrFindTwoCharacterString(isolate(), c1, c2);
}
if (!FLAG_string_slices || length < SlicedString::kMinLength) {
if (str->IsOneByteRepresentation()) {
Handle<SeqOneByteString> result =
NewRawOneByteString(length).ToHandleChecked();
DisallowHeapAllocation no_gc;
uint8_t* dest = result->GetChars(no_gc);
String::WriteToFlat(*str, dest, begin, end);
return result;
} else {
Handle<SeqTwoByteString> result =
NewRawTwoByteString(length).ToHandleChecked();
DisallowHeapAllocation no_gc;
uc16* dest = result->GetChars(no_gc);
String::WriteToFlat(*str, dest, begin, end);
return result;
}
}
int offset = begin;
if (str->IsSlicedString()) {
Handle<SlicedString> slice = Handle<SlicedString>::cast(str);
str = Handle<String>(slice->parent(), isolate());
offset += slice->offset();
}
if (str->IsThinString()) {
Handle<ThinString> thin = Handle<ThinString>::cast(str);
str = handle(thin->actual(), isolate());
}
DCHECK(str->IsSeqString() || str->IsExternalString());
Handle<Map> map = str->IsOneByteRepresentation()
? sliced_one_byte_string_map()
: sliced_string_map();
Handle<SlicedString> slice(
SlicedString::cast(New(map, AllocationType::kYoung)), isolate());
slice->set_hash_field(String::kEmptyHashField);
slice->set_length(length);
slice->set_parent(isolate(), *str);
slice->set_offset(offset);
return slice;
}
MaybeHandle<String> Factory::NewExternalStringFromOneByte(
const ExternalOneByteString::Resource* resource) {
size_t length = resource->length();
if (length > static_cast<size_t>(String::kMaxLength)) {
THROW_NEW_ERROR(isolate(), NewInvalidStringLengthError(), String);
}
if (length == 0) return empty_string();
Handle<Map> map = resource->IsCacheable()
? external_one_byte_string_map()
: uncached_external_one_byte_string_map();
Handle<ExternalOneByteString> external_string(
ExternalOneByteString::cast(New(map, AllocationType::kOld)), isolate());
external_string->set_length(static_cast<int>(length));
external_string->set_hash_field(String::kEmptyHashField);
external_string->SetResource(isolate(), resource);
isolate()->heap()->RegisterExternalString(*external_string);
return external_string;
}
MaybeHandle<String> Factory::NewExternalStringFromTwoByte(
const ExternalTwoByteString::Resource* resource) {
size_t length = resource->length();
if (length > static_cast<size_t>(String::kMaxLength)) {
THROW_NEW_ERROR(isolate(), NewInvalidStringLengthError(), String);
}
if (length == 0) return empty_string();
Handle<Map> map = resource->IsCacheable() ? external_string_map()
: uncached_external_string_map();
Handle<ExternalTwoByteString> external_string(
ExternalTwoByteString::cast(New(map, AllocationType::kOld)), isolate());
external_string->set_length(static_cast<int>(length));
external_string->set_hash_field(String::kEmptyHashField);
external_string->SetResource(isolate(), resource);
isolate()->heap()->RegisterExternalString(*external_string);
return external_string;
}
Handle<ExternalOneByteString> Factory::NewNativeSourceString(
const ExternalOneByteString::Resource* resource) {
size_t length = resource->length();
DCHECK_LE(length, static_cast<size_t>(String::kMaxLength));
Handle<Map> map = native_source_string_map();
Handle<ExternalOneByteString> external_string(
ExternalOneByteString::cast(New(map, AllocationType::kOld)), isolate());
external_string->set_length(static_cast<int>(length));
external_string->set_hash_field(String::kEmptyHashField);
external_string->SetResource(isolate(), resource);
isolate()->heap()->RegisterExternalString(*external_string);
return external_string;
}
Handle<JSStringIterator> Factory::NewJSStringIterator(Handle<String> string) {
Handle<Map> map(isolate()->native_context()->initial_string_iterator_map(),
isolate());
Handle<String> flat_string = String::Flatten(isolate(), string);
Handle<JSStringIterator> iterator =
Handle<JSStringIterator>::cast(NewJSObjectFromMap(map));
iterator->set_string(*flat_string);
iterator->set_index(0);
return iterator;
}
Handle<Symbol> Factory::NewSymbol(AllocationType allocation) {
DCHECK(allocation != AllocationType::kYoung);
// Statically ensure that it is safe to allocate symbols in paged spaces.
STATIC_ASSERT(Symbol::kSize <= kMaxRegularHeapObjectSize);
HeapObject result =
AllocateRawWithImmortalMap(Symbol::kSize, allocation, *symbol_map());
// Generate a random hash value.
int hash = isolate()->GenerateIdentityHash(Name::kHashBitMask);
Handle<Symbol> symbol(Symbol::cast(result), isolate());
symbol->set_hash_field(Name::kIsNotArrayIndexMask |
(hash << Name::kHashShift));
symbol->set_name(*undefined_value());
symbol->set_flags(0);
DCHECK(!symbol->is_private());
return symbol;
}
Handle<Symbol> Factory::NewPrivateSymbol(AllocationType allocation) {
DCHECK(allocation != AllocationType::kYoung);
Handle<Symbol> symbol = NewSymbol(allocation);
symbol->set_is_private(true);
return symbol;
}
Handle<Symbol> Factory::NewPrivateNameSymbol(Handle<String> name) {
Handle<Symbol> symbol = NewSymbol();
symbol->set_is_private_name();
symbol->set_name(*name);
return symbol;
}
Handle<Context> Factory::NewContext(RootIndex map_root_index, int size,
int variadic_part_length,
AllocationType allocation) {
DCHECK(RootsTable::IsImmortalImmovable(map_root_index));
DCHECK_LE(Context::kTodoHeaderSize, size);
DCHECK(IsAligned(size, kTaggedSize));
DCHECK_LE(Context::MIN_CONTEXT_SLOTS, variadic_part_length);
DCHECK_LE(Context::SizeFor(variadic_part_length), size);
Map map = Map::cast(isolate()->root(map_root_index));
HeapObject result = AllocateRawWithImmortalMap(size, allocation, map);
Handle<Context> context(Context::cast(result), isolate());
context->set_length(variadic_part_length);
DCHECK_EQ(context->SizeFromMap(map), size);
if (size > Context::kTodoHeaderSize) {
ObjectSlot start = context->RawField(Context::kTodoHeaderSize);
ObjectSlot end = context->RawField(size);
size_t slot_count = end - start;
MemsetTagged(start, *undefined_value(), slot_count);
}
return context;
}
Handle<NativeContext> Factory::NewNativeContext() {
Handle<NativeContext> context = Handle<NativeContext>::cast(
NewContext(RootIndex::kNativeContextMap, NativeContext::kSize,
NativeContext::NATIVE_CONTEXT_SLOTS, AllocationType::kOld));
context->set_scope_info(ReadOnlyRoots(isolate()).empty_scope_info());
context->set_previous(Context::unchecked_cast(Smi::zero()));
context->set_extension(*the_hole_value());
context->set_native_context(*context);
context->set_errors_thrown(Smi::zero());
context->set_math_random_index(Smi::zero());
context->set_serialized_objects(*empty_fixed_array());
context->set_microtask_queue(nullptr);
return context;
}
Handle<Context> Factory::NewScriptContext(Handle<NativeContext> outer,
Handle<ScopeInfo> scope_info) {
DCHECK_EQ(scope_info->scope_type(), SCRIPT_SCOPE);
int variadic_part_length = scope_info->ContextLength();
Handle<Context> context = NewContext(
RootIndex::kScriptContextMap, Context::SizeFor(variadic_part_length),
variadic_part_length, AllocationType::kOld);
context->set_scope_info(*scope_info);
context->set_previous(*outer);
context->set_extension(*the_hole_value());
context->set_native_context(*outer);
DCHECK(context->IsScriptContext());
return context;
}
Handle<ScriptContextTable> Factory::NewScriptContextTable() {
Handle<ScriptContextTable> context_table =
NewFixedArrayWithMap<ScriptContextTable>(
RootIndex::kScriptContextTableMap, ScriptContextTable::kMinLength);
context_table->set_used(0);
return context_table;
}
Handle<Context> Factory::NewModuleContext(Handle<Module> module,
Handle<NativeContext> outer,
Handle<ScopeInfo> scope_info) {
DCHECK_EQ(scope_info->scope_type(), MODULE_SCOPE);
int variadic_part_length = scope_info->ContextLength();
Handle<Context> context = NewContext(
RootIndex::kModuleContextMap, Context::SizeFor(variadic_part_length),
variadic_part_length, AllocationType::kOld);
context->set_scope_info(*scope_info);
context->set_previous(*outer);
context->set_extension(*module);
context->set_native_context(*outer);
DCHECK(context->IsModuleContext());
return context;
}
Handle<Context> Factory::NewFunctionContext(Handle<Context> outer,
Handle<ScopeInfo> scope_info) {
RootIndex mapRootIndex;
switch (scope_info->scope_type()) {
case EVAL_SCOPE:
mapRootIndex = RootIndex::kEvalContextMap;
break;
case FUNCTION_SCOPE:
mapRootIndex = RootIndex::kFunctionContextMap;
break;
default:
UNREACHABLE();
}
int variadic_part_length = scope_info->ContextLength();
Handle<Context> context =
NewContext(mapRootIndex, Context::SizeFor(variadic_part_length),
variadic_part_length, AllocationType::kYoung);
context->set_scope_info(*scope_info);
context->set_previous(*outer);
context->set_extension(*the_hole_value());
context->set_native_context(outer->native_context());
return context;
}
Handle<Context> Factory::NewCatchContext(Handle<Context> previous,
Handle<ScopeInfo> scope_info,
Handle<Object> thrown_object) {
DCHECK_EQ(scope_info->scope_type(), CATCH_SCOPE);
STATIC_ASSERT(Context::MIN_CONTEXT_SLOTS == Context::THROWN_OBJECT_INDEX);
// TODO(ishell): Take the details from CatchContext class.
int variadic_part_length = Context::MIN_CONTEXT_SLOTS + 1;
Handle<Context> context = NewContext(
RootIndex::kCatchContextMap, Context::SizeFor(variadic_part_length),
variadic_part_length, AllocationType::kYoung);
context->set_scope_info(*scope_info);
context->set_previous(*previous);
context->set_extension(*the_hole_value());
context->set_native_context(previous->native_context());
context->set(Context::THROWN_OBJECT_INDEX, *thrown_object);
return context;
}
Handle<Context> Factory::NewDebugEvaluateContext(Handle<Context> previous,
Handle<ScopeInfo> scope_info,
Handle<JSReceiver> extension,
Handle<Context> wrapped,
Handle<StringSet> whitelist) {
STATIC_ASSERT(Context::WHITE_LIST_INDEX == Context::MIN_CONTEXT_SLOTS + 1);
DCHECK(scope_info->IsDebugEvaluateScope());
Handle<HeapObject> ext = extension.is_null()
? Handle<HeapObject>::cast(the_hole_value())
: Handle<HeapObject>::cast(extension);
// TODO(ishell): Take the details from DebugEvaluateContextContext class.
int variadic_part_length = Context::MIN_CONTEXT_SLOTS + 2;
Handle<Context> c = NewContext(RootIndex::kDebugEvaluateContextMap,
Context::SizeFor(variadic_part_length),
variadic_part_length, AllocationType::kYoung);
c->set_scope_info(*scope_info);
c->set_previous(*previous);
c->set_native_context(previous->native_context());
c->set_extension(*ext);
if (!wrapped.is_null()) c->set(Context::WRAPPED_CONTEXT_INDEX, *wrapped);
if (!whitelist.is_null()) c->set(Context::WHITE_LIST_INDEX, *whitelist);
return c;
}
Handle<Context> Factory::NewWithContext(Handle<Context> previous,
Handle<ScopeInfo> scope_info,
Handle<JSReceiver> extension) {
DCHECK_EQ(scope_info->scope_type(), WITH_SCOPE);
// TODO(ishell): Take the details from WithContext class.
int variadic_part_length = Context::MIN_CONTEXT_SLOTS;
Handle<Context> context = NewContext(
RootIndex::kWithContextMap, Context::SizeFor(variadic_part_length),
variadic_part_length, AllocationType::kYoung);
context->set_scope_info(*scope_info);
context->set_previous(*previous);
context->set_extension(*extension);
context->set_native_context(previous->native_context());
return context;
}
Handle<Context> Factory::NewBlockContext(Handle<Context> previous,
Handle<ScopeInfo> scope_info) {
DCHECK_IMPLIES(scope_info->scope_type() != BLOCK_SCOPE,
scope_info->scope_type() == CLASS_SCOPE);
int variadic_part_length = scope_info->ContextLength();
Handle<Context> context = NewContext(
RootIndex::kBlockContextMap, Context::SizeFor(variadic_part_length),
variadic_part_length, AllocationType::kYoung);
context->set_scope_info(*scope_info);
context->set_previous(*previous);
context->set_extension(*the_hole_value());
context->set_native_context(previous->native_context());
return context;
}
Handle<Context> Factory::NewBuiltinContext(Handle<NativeContext> native_context,
int variadic_part_length) {
DCHECK_LE(Context::MIN_CONTEXT_SLOTS, variadic_part_length);
Handle<Context> context = NewContext(
RootIndex::kFunctionContextMap, Context::SizeFor(variadic_part_length),
variadic_part_length, AllocationType::kYoung);
context->set_scope_info(ReadOnlyRoots(isolate()).empty_scope_info());
context->set_previous(*native_context);
context->set_extension(*the_hole_value());
context->set_native_context(*native_context);
return context;
}
Handle<Struct> Factory::NewStruct(InstanceType type,
AllocationType allocation) {
Map map;
switch (type) {
#define MAKE_CASE(TYPE, Name, name) \
case TYPE: \
map = *name##_map(); \
break;
STRUCT_LIST(MAKE_CASE)
#undef MAKE_CASE
default:
UNREACHABLE();
}
int size = map.instance_size();
HeapObject result = AllocateRawWithImmortalMap(size, allocation, map);
Handle<Struct> str(Struct::cast(result), isolate());
str->InitializeBody(size);
return str;
}
Handle<AliasedArgumentsEntry> Factory::NewAliasedArgumentsEntry(
int aliased_context_slot) {
Handle<AliasedArgumentsEntry> entry = Handle<AliasedArgumentsEntry>::cast(
NewStruct(ALIASED_ARGUMENTS_ENTRY_TYPE, AllocationType::kYoung));
entry->set_aliased_context_slot(aliased_context_slot);
return entry;
}
Handle<AccessorInfo> Factory::NewAccessorInfo() {
Handle<AccessorInfo> info = Handle<AccessorInfo>::cast(
NewStruct(ACCESSOR_INFO_TYPE, AllocationType::kOld));
info->set_name(*empty_string());
info->set_flags(0); // Must clear the flags, it was initialized as undefined.
info->set_is_sloppy(true);
info->set_initial_property_attributes(NONE);
return info;
}
Handle<Script> Factory::NewScript(Handle<String> source,
AllocationType allocation) {
return NewScriptWithId(source, isolate()->heap()->NextScriptId(), allocation);
}
Handle<Script> Factory::NewScriptWithId(Handle<String> source, int script_id,
AllocationType allocation) {
DCHECK(allocation == AllocationType::kOld ||
allocation == AllocationType::kReadOnly);
// Create and initialize script object.
Heap* heap = isolate()->heap();
ReadOnlyRoots roots(heap);
Handle<Script> script =
Handle<Script>::cast(NewStruct(SCRIPT_TYPE, allocation));
script->set_source(*source);
script->set_name(roots.undefined_value());
script->set_id(script_id);
script->set_line_offset(0);
script->set_column_offset(0);
script->set_context_data(roots.undefined_value());
script->set_type(Script::TYPE_NORMAL);
script->set_line_ends(roots.undefined_value());
script->set_eval_from_shared_or_wrapped_arguments(roots.undefined_value());
script->set_eval_from_position(0);
script->set_shared_function_infos(*empty_weak_fixed_array(),
SKIP_WRITE_BARRIER);
script->set_flags(0);
script->set_host_defined_options(*empty_fixed_array());
Handle<WeakArrayList> scripts = script_list();
scripts = WeakArrayList::AddToEnd(isolate(), scripts,
MaybeObjectHandle::Weak(script));
heap->set_script_list(*scripts);
LOG(isolate(), ScriptEvent(Logger::ScriptEventType::kCreate, script_id));
TRACE_EVENT_OBJECT_CREATED_WITH_ID(
TRACE_DISABLED_BY_DEFAULT("v8.compile"), "Script",
TRACE_ID_WITH_SCOPE(Script::kTraceScope, script_id));
return script;
}
Handle<Script> Factory::CloneScript(Handle<Script> script) {
Heap* heap = isolate()->heap();
int script_id = isolate()->heap()->NextScriptId();
Handle<Script> new_script =
Handle<Script>::cast(NewStruct(SCRIPT_TYPE, AllocationType::kOld));
new_script->set_source(script->source());
new_script->set_name(script->name());
new_script->set_id(script_id);
new_script->set_line_offset(script->line_offset());
new_script->set_column_offset(script->column_offset());
new_script->set_context_data(script->context_data());
new_script->set_type(script->type());
new_script->set_line_ends(ReadOnlyRoots(heap).undefined_value());
new_script->set_eval_from_shared_or_wrapped_arguments(
script->eval_from_shared_or_wrapped_arguments());
new_script->set_shared_function_infos(*empty_weak_fixed_array(),
SKIP_WRITE_BARRIER);
new_script->set_eval_from_position(script->eval_from_position());
new_script->set_flags(script->flags());
new_script->set_host_defined_options(script->host_defined_options());
Handle<WeakArrayList> scripts = script_list();
scripts = WeakArrayList::AddToEnd(isolate(), scripts,
MaybeObjectHandle::Weak(new_script));
heap->set_script_list(*scripts);
LOG(isolate(), ScriptEvent(Logger::ScriptEventType::kCreate, script_id));
return new_script;
}
Handle<CallableTask> Factory::NewCallableTask(Handle<JSReceiver> callable,
Handle<Context> context) {
DCHECK(callable->IsCallable());
Handle<CallableTask> microtask =
Handle<CallableTask>::cast(NewStruct(CALLABLE_TASK_TYPE));
microtask->set_callable(*callable);
microtask->set_context(*context);
return microtask;
}
Handle<CallbackTask> Factory::NewCallbackTask(Handle<Foreign> callback,
Handle<Foreign> data) {
Handle<CallbackTask> microtask =
Handle<CallbackTask>::cast(NewStruct(CALLBACK_TASK_TYPE));
microtask->set_callback(*callback);
microtask->set_data(*data);
return microtask;
}
Handle<PromiseResolveThenableJobTask> Factory::NewPromiseResolveThenableJobTask(
Handle<JSPromise> promise_to_resolve, Handle<JSReceiver> then,
Handle<JSReceiver> thenable, Handle<Context> context) {
DCHECK(then->IsCallable());
Handle<PromiseResolveThenableJobTask> microtask =
Handle<PromiseResolveThenableJobTask>::cast(
NewStruct(PROMISE_RESOLVE_THENABLE_JOB_TASK_TYPE));
microtask->set_promise_to_resolve(*promise_to_resolve);
microtask->set_then(*then);
microtask->set_thenable(*thenable);
microtask->set_context(*context);
return microtask;
}
Handle<FinalizationGroupCleanupJobTask>
Factory::NewFinalizationGroupCleanupJobTask(
Handle<JSFinalizationGroup> finalization_group) {
Handle<FinalizationGroupCleanupJobTask> microtask =
Handle<FinalizationGroupCleanupJobTask>::cast(
NewStruct(FINALIZATION_GROUP_CLEANUP_JOB_TASK_TYPE));
microtask->set_finalization_group(*finalization_group);
return microtask;
}
Handle<Foreign> Factory::NewForeign(Address addr, AllocationType allocation) {
// Statically ensure that it is safe to allocate foreigns in paged spaces.
STATIC_ASSERT(Foreign::kSize <= kMaxRegularHeapObjectSize);
Map map = *foreign_map();
HeapObject result =
AllocateRawWithImmortalMap(map.instance_size(), allocation, map);
Handle<Foreign> foreign(Foreign::cast(result), isolate());
foreign->set_foreign_address(addr);
return foreign;
}
Handle<ByteArray> Factory::NewByteArray(int length, AllocationType allocation) {
DCHECK_LE(0, length);
if (length > ByteArray::kMaxLength) {
isolate()->heap()->FatalProcessOutOfMemory("invalid array length");
}
int size = ByteArray::SizeFor(length);
HeapObject result =
AllocateRawWithImmortalMap(size, allocation, *byte_array_map());
Handle<ByteArray> array(ByteArray::cast(result), isolate());
array->set_length(length);
array->clear_padding();
return array;
}
Handle<BytecodeArray> Factory::NewBytecodeArray(
int length, const byte* raw_bytecodes, int frame_size, int parameter_count,
Handle<FixedArray> constant_pool) {
DCHECK_LE(0, length);
if (length > BytecodeArray::kMaxLength) {
isolate()->heap()->FatalProcessOutOfMemory("invalid array length");
}
// Bytecode array is AllocationType::kOld, so constant pool array should be
// too.
DCHECK(!Heap::InYoungGeneration(*constant_pool));
int size = BytecodeArray::SizeFor(length);
HeapObject result = AllocateRawWithImmortalMap(size, AllocationType::kOld,
*bytecode_array_map());
Handle<BytecodeArray> instance(BytecodeArray::cast(result), isolate());
instance->set_length(length);
instance->set_frame_size(frame_size);
instance->set_parameter_count(parameter_count);
instance->set_incoming_new_target_or_generator_register(
interpreter::Register::invalid_value());
instance->set_osr_loop_nesting_level(0);
instance->set_bytecode_age(BytecodeArray::kNoAgeBytecodeAge);
instance->set_constant_pool(*constant_pool);
instance->set_handler_table(*empty_byte_array());
instance->set_source_position_table(*undefined_value());
CopyBytes(reinterpret_cast<byte*>(instance->GetFirstBytecodeAddress()),
raw_bytecodes, length);
instance->clear_padding();
return instance;
}
Handle<FixedTypedArrayBase> Factory::NewFixedTypedArrayWithExternalPointer(
ExternalArrayType array_type, void* external_pointer,
AllocationType allocation) {
int size = FixedTypedArrayBase::kHeaderSize;
HeapObject result = AllocateRawWithImmortalMap(
size, allocation,
ReadOnlyRoots(isolate()).MapForFixedTypedArray(array_type));
Handle<FixedTypedArrayBase> elements(FixedTypedArrayBase::cast(result),
isolate());
elements->set_base_pointer(Smi::kZero, SKIP_WRITE_BARRIER);
elements->set_external_pointer(external_pointer);
elements->set_number_of_elements_onheap_only(0);
return elements;
}
Handle<FixedTypedArrayBase> Factory::NewFixedTypedArray(
size_t length, size_t byte_length, ExternalArrayType array_type,
bool initialize, AllocationType allocation) {
// TODO(7881): Smi length check
DCHECK(0 <= length && length <= Smi::kMaxValue);
CHECK(byte_length <= kMaxInt - FixedTypedArrayBase::kDataOffset);
size_t size =
OBJECT_POINTER_ALIGN(byte_length + FixedTypedArrayBase::kDataOffset);
Map map = ReadOnlyRoots(isolate()).MapForFixedTypedArray(array_type);
AllocationAlignment alignment =
array_type == kExternalFloat64Array ? kDoubleAligned : kWordAligned;
HeapObject object = AllocateRawWithImmortalMap(static_cast<int>(size),
allocation, map, alignment);
Handle<FixedTypedArrayBase> elements(FixedTypedArrayBase::cast(object),
isolate());
elements->set_base_pointer(*elements, SKIP_WRITE_BARRIER);
elements->set_external_pointer(
FixedTypedArrayBase::ExternalPointerPtrForOnHeapArray());
elements->set_number_of_elements_onheap_only(static_cast<int>(length));
if (initialize) memset(elements->DataPtr(), 0, elements->DataSize());
return elements;
}
Handle<Cell> Factory::NewCell(Handle<Object> value) {
AllowDeferredHandleDereference convert_to_cell;
STATIC_ASSERT(Cell::kSize <= kMaxRegularHeapObjectSize);
HeapObject result = AllocateRawWithImmortalMap(
Cell::kSize, AllocationType::kOld, *cell_map());
Handle<Cell> cell(Cell::cast(result), isolate());
cell->set_value(*value);
return cell;
}
Handle<FeedbackCell> Factory::NewNoClosuresCell(Handle<HeapObject> value) {
AllowDeferredHandleDereference convert_to_cell;
HeapObject result = AllocateRawWithImmortalMap(FeedbackCell::kAlignedSize,
AllocationType::kOld, *no_closures_cell_map());
Handle<FeedbackCell> cell(FeedbackCell::cast(result), isolate());
cell->set_value(*value);
cell->set_interrupt_budget(FeedbackCell::GetInitialInterruptBudget());
cell->clear_padding();
return cell;
}
Handle<FeedbackCell> Factory::NewOneClosureCell(Handle<HeapObject> value) {
AllowDeferredHandleDereference convert_to_cell;
HeapObject result = AllocateRawWithImmortalMap(FeedbackCell::kAlignedSize,
AllocationType::kOld, *one_closure_cell_map());
Handle<FeedbackCell> cell(FeedbackCell::cast(result), isolate());
cell->set_value(*value);
cell->set_interrupt_budget(FeedbackCell::GetInitialInterruptBudget());
cell->clear_padding();
return cell;
}
Handle<FeedbackCell> Factory::NewManyClosuresCell(Handle<HeapObject> value) {
AllowDeferredHandleDereference convert_to_cell;
HeapObject result = AllocateRawWithImmortalMap(FeedbackCell::kAlignedSize,
AllocationType::kOld, *many_closures_cell_map());
Handle<FeedbackCell> cell(FeedbackCell::cast(result), isolate());
cell->set_value(*value);
cell->set_interrupt_budget(FeedbackCell::GetInitialInterruptBudget());
cell->clear_padding();
return cell;
}
Handle<PropertyCell> Factory::NewPropertyCell(Handle<Name> name,
AllocationType allocation) {
DCHECK(name->IsUniqueName());
STATIC_ASSERT(PropertyCell::kSize <= kMaxRegularHeapObjectSize);
HeapObject result = AllocateRawWithImmortalMap(
PropertyCell::kSize, allocation, *global_property_cell_map());
Handle<PropertyCell> cell(PropertyCell::cast(result), isolate());
cell->set_dependent_code(DependentCode::cast(*empty_weak_fixed_array()),
SKIP_WRITE_BARRIER);
cell->set_property_details(PropertyDetails(Smi::zero()));
cell->set_name(*name);
cell->set_value(*the_hole_value());
return cell;
}
Handle<DescriptorArray> Factory::NewDescriptorArray(int number_of_descriptors,
int slack,
AllocationType allocation) {
DCHECK(Heap::IsRegularObjectAllocation(allocation));
int number_of_all_descriptors = number_of_descriptors + slack;
// Zero-length case must be handled outside.
DCHECK_LT(0, number_of_all_descriptors);
int size = DescriptorArray::SizeFor(number_of_all_descriptors);
HeapObject obj =
isolate()->heap()->AllocateRawWithRetryOrFail(size, allocation);
obj.set_map_after_allocation(*descriptor_array_map(), SKIP_WRITE_BARRIER);
DescriptorArray array = DescriptorArray::cast(obj);
array.Initialize(*empty_enum_cache(), *undefined_value(),
number_of_descriptors, slack);
return Handle<DescriptorArray>(array, isolate());
}
Handle<TransitionArray> Factory::NewTransitionArray(int number_of_transitions,
int slack) {
int capacity = TransitionArray::LengthFor(number_of_transitions + slack);
Handle<TransitionArray> array = NewWeakFixedArrayWithMap<TransitionArray>(
RootIndex::kTransitionArrayMap, capacity, AllocationType::kOld);
// Transition arrays are AllocationType::kOld. When black allocation is on we
// have to add the transition array to the list of
// encountered_transition_arrays.
Heap* heap = isolate()->heap();
if (heap->incremental_marking()->black_allocation()) {
heap->mark_compact_collector()->AddTransitionArray(*array);
}
array->WeakFixedArray::Set(TransitionArray::kPrototypeTransitionsIndex,
MaybeObject::FromObject(Smi::kZero));
array->WeakFixedArray::Set(
TransitionArray::kTransitionLengthIndex,
MaybeObject::FromObject(Smi::FromInt(number_of_transitions)));
return array;
}
Handle<AllocationSite> Factory::NewAllocationSite(bool with_weak_next) {
Handle<Map> map = with_weak_next ? allocation_site_map()
: allocation_site_without_weaknext_map();
Handle<AllocationSite> site(
AllocationSite::cast(New(map, AllocationType::kOld)), isolate());
site->Initialize();
if (with_weak_next) {
// Link the site
site->set_weak_next(isolate()->heap()->allocation_sites_list());
isolate()->heap()->set_allocation_sites_list(*site);
}
return site;
}
Handle<Map> Factory::NewMap(InstanceType type, int instance_size,
ElementsKind elements_kind,
int inobject_properties) {
STATIC_ASSERT(LAST_JS_OBJECT_TYPE == LAST_TYPE);
DCHECK_IMPLIES(InstanceTypeChecker::IsJSObject(type) &&
!Map::CanHaveFastTransitionableElementsKind(type),
IsDictionaryElementsKind(elements_kind) ||
IsTerminalElementsKind(elements_kind));
HeapObject result = isolate()->heap()->AllocateRawWithRetryOrFail(
Map::kSize, AllocationType::kMap);
result.set_map_after_allocation(*meta_map(), SKIP_WRITE_BARRIER);
return handle(InitializeMap(Map::cast(result), type, instance_size,
elements_kind, inobject_properties),
isolate());
}
Map Factory::InitializeMap(Map map, InstanceType type, int instance_size,
ElementsKind elements_kind,
int inobject_properties) {
map.set_instance_type(type);
map.set_prototype(*null_value(), SKIP_WRITE_BARRIER);
map.set_constructor_or_backpointer(*null_value(), SKIP_WRITE_BARRIER);
map.set_instance_size(instance_size);
if (map.IsJSObjectMap()) {
DCHECK(!ReadOnlyHeap::Contains(map));
map.SetInObjectPropertiesStartInWords(instance_size / kTaggedSize -
inobject_properties);
DCHECK_EQ(map.GetInObjectProperties(), inobject_properties);
map.set_prototype_validity_cell(*invalid_prototype_validity_cell());
} else {
DCHECK_EQ(inobject_properties, 0);
map.set_inobject_properties_start_or_constructor_function_index(0);
map.set_prototype_validity_cell(Smi::FromInt(Map::kPrototypeChainValid));
}
map.set_dependent_code(DependentCode::cast(*empty_weak_fixed_array()),
SKIP_WRITE_BARRIER);
map.set_raw_transitions(MaybeObject::FromSmi(Smi::zero()));
map.SetInObjectUnusedPropertyFields(inobject_properties);
map.SetInstanceDescriptors(isolate(), *empty_descriptor_array(), 0);
if (FLAG_unbox_double_fields) {
map.set_layout_descriptor(LayoutDescriptor::FastPointerLayout());
}
// Must be called only after |instance_type|, |instance_size| and
// |layout_descriptor| are set.
map.set_visitor_id(Map::GetVisitorId(map));
map.set_bit_field(0);
map.set_bit_field2(Map::IsExtensibleBit::kMask);
int bit_field3 = Map::EnumLengthBits::encode(kInvalidEnumCacheSentinel) |
Map::OwnsDescriptorsBit::encode(true) |
Map::ConstructionCounterBits::encode(Map::kNoSlackTracking);
map.set_bit_field3(bit_field3);
DCHECK(!map.is_in_retained_map_list());
map.clear_padding();
map.set_elements_kind(elements_kind);
map.set_new_target_is_base(true);
isolate()->counters()->maps_created()->Increment();
if (FLAG_trace_maps) LOG(isolate(), MapCreate(map));
return map;
}
Handle<JSObject> Factory::CopyJSObject(Handle<JSObject> source) {
return CopyJSObjectWithAllocationSite(source, Handle<AllocationSite>());
}
Handle<JSObject> Factory::CopyJSObjectWithAllocationSite(
Handle<JSObject> source, Handle<AllocationSite> site) {
Handle<Map> map(source->map(), isolate());
// We can only clone regexps, normal objects, api objects, errors or arrays.
// Copying anything else will break invariants.
CHECK(map->instance_type() == JS_REGEXP_TYPE ||
map->instance_type() == JS_OBJECT_TYPE ||
map->instance_type() == JS_ERROR_TYPE ||
map->instance_type() == JS_ARRAY_TYPE ||
map->instance_type() == JS_API_OBJECT_TYPE ||
map->instance_type() == WASM_GLOBAL_TYPE ||
map->instance_type() == WASM_INSTANCE_TYPE ||
map->instance_type() == WASM_MEMORY_TYPE ||
map->instance_type() == WASM_MODULE_TYPE ||
map->instance_type() == WASM_TABLE_TYPE ||
map->instance_type() == JS_SPECIAL_API_OBJECT_TYPE);
DCHECK(site.is_null() || AllocationSite::CanTrack(map->instance_type()));
int object_size = map->instance_size();
int adjusted_object_size =
site.is_null() ? object_size : object_size + AllocationMemento::kSize;
HeapObject raw_clone = isolate()->heap()->AllocateRawWithRetryOrFail(
adjusted_object_size, AllocationType::kYoung);
DCHECK(Heap::InYoungGeneration(raw_clone));
// Since we know the clone is allocated in new space, we can copy
// the contents without worrying about updating the write barrier.
Heap::CopyBlock(raw_clone.address(), source->address(), object_size);
Handle<JSObject> clone(JSObject::cast(raw_clone), isolate());
if (!site.is_null()) {
AllocationMemento alloc_memento = AllocationMemento::unchecked_cast(
Object(raw_clone.ptr() + object_size));
InitializeAllocationMemento(alloc_memento, *site);
}
SLOW_DCHECK(clone->GetElementsKind() == source->GetElementsKind());
FixedArrayBase elements = source->elements();
// Update elements if necessary.
if (elements.length() > 0) {
FixedArrayBase elem;
if (elements.map() == *fixed_cow_array_map()) {
elem = elements;
} else if (source->HasDoubleElements()) {
elem = *CopyFixedDoubleArray(
handle(FixedDoubleArray::cast(elements), isolate()));
} else {
elem = *CopyFixedArray(handle(FixedArray::cast(elements), isolate()));
}
clone->set_elements(elem);
}
// Update properties if necessary.
if (source->HasFastProperties()) {
PropertyArray properties = source->property_array();
if (properties.length() > 0) {
// TODO(gsathya): Do not copy hash code.
Handle<PropertyArray> prop = CopyArrayWithMap(
handle(properties, isolate()), handle(properties.map(), isolate()));
clone->set_raw_properties_or_hash(*prop);
}
} else {
Handle<FixedArray> properties(
FixedArray::cast(source->property_dictionary()), isolate());
Handle<FixedArray> prop = CopyFixedArray(properties);
clone->set_raw_properties_or_hash(*prop);
}
return clone;
}
namespace {
template <typename T>
void initialize_length(Handle<T> array, int length) {
array->set_length(length);
}
template <>
void initialize_length<PropertyArray>(Handle<PropertyArray> array, int length) {
array->initialize_length(length);
}
} // namespace
template <typename T>
Handle<T> Factory::CopyArrayWithMap(Handle<T> src, Handle<Map> map) {
int len = src->length();
HeapObject obj = AllocateRawFixedArray(len, AllocationType::kYoung);
obj.set_map_after_allocation(*map, SKIP_WRITE_BARRIER);
Handle<T> result(T::cast(obj), isolate());
initialize_length(result, len);
DisallowHeapAllocation no_gc;
WriteBarrierMode mode = result->GetWriteBarrierMode(no_gc);
result->CopyElements(isolate(), 0, *src, 0, len, mode);
return result;
}
template <typename T>
Handle<T> Factory::CopyArrayAndGrow(Handle<T> src, int grow_by,
AllocationType allocation) {
DCHECK_LT(0, grow_by);
DCHECK_LE(grow_by, kMaxInt - src->length());
int old_len = src->length();
int new_len = old_len + grow_by;
HeapObject obj = AllocateRawFixedArray(new_len, allocation);
obj.set_map_after_allocation(src->map(), SKIP_WRITE_BARRIER);
Handle<T> result(T::cast(obj), isolate());
initialize_length(result, new_len);
// Copy the content.
DisallowHeapAllocation no_gc;
WriteBarrierMode mode = obj.GetWriteBarrierMode(no_gc);
result->CopyElements(isolate(), 0, *src, 0, old_len, mode);
MemsetTagged(ObjectSlot(result->data_start() + old_len),
ReadOnlyRoots(isolate()).undefined_value(), grow_by);
return result;
}
Handle<FixedArray> Factory::CopyFixedArrayWithMap(Handle<FixedArray> array,
Handle<Map> map) {
return CopyArrayWithMap(array, map);
}
Handle<FixedArray> Factory::CopyFixedArrayAndGrow(Handle<FixedArray> array,
int grow_by,
AllocationType allocation) {
return CopyArrayAndGrow(array, grow_by, allocation);
}
Handle<WeakFixedArray> Factory::CopyWeakFixedArrayAndGrow(
Handle<WeakFixedArray> src, int grow_by, AllocationType allocation) {
DCHECK(!src->IsTransitionArray()); // Compacted by GC, this code doesn't work
return CopyArrayAndGrow(src, grow_by, allocation);
}
Handle<WeakArrayList> Factory::CopyWeakArrayListAndGrow(
Handle<WeakArrayList> src, int grow_by, AllocationType allocation) {
int old_capacity = src->capacity();
int new_capacity = old_capacity + grow_by;
DCHECK_GE(new_capacity, old_capacity);
HeapObject obj = AllocateRawWeakArrayList(new_capacity, allocation);
obj.set_map_after_allocation(src->map(), SKIP_WRITE_BARRIER);
WeakArrayList result = WeakArrayList::cast(obj);
int old_len = src->length();
result.set_length(old_len);
result.set_capacity(new_capacity);
// Copy the content.
DisallowHeapAllocation no_gc;
WriteBarrierMode mode = obj.GetWriteBarrierMode(no_gc);
result.CopyElements(isolate(), 0, *src, 0, old_len, mode);
MemsetTagged(ObjectSlot(result.data_start() + old_len),
ReadOnlyRoots(isolate()).undefined_value(),
new_capacity - old_len);
return Handle<WeakArrayList>(result, isolate());
}
Handle<PropertyArray> Factory::CopyPropertyArrayAndGrow(
Handle<PropertyArray> array, int grow_by, AllocationType allocation) {
return CopyArrayAndGrow(array, grow_by, allocation);
}
Handle<FixedArray> Factory::CopyFixedArrayUpTo(Handle<FixedArray> array,
int new_len,
AllocationType allocation) {
DCHECK_LE(0, new_len);
DCHECK_LE(new_len, array->length());
if (new_len == 0) return empty_fixed_array();
HeapObject obj = AllocateRawFixedArray(new_len, allocation);
obj.set_map_after_allocation(*fixed_array_map(), SKIP_WRITE_BARRIER);
Handle<FixedArray> result(FixedArray::cast(obj), isolate());
result->set_length(new_len);
// Copy the content.
DisallowHeapAllocation no_gc;
WriteBarrierMode mode = result->GetWriteBarrierMode(no_gc);
result->CopyElements(isolate(), 0, *array, 0, new_len, mode);
return result;
}
Handle<FixedArray> Factory::CopyFixedArray(Handle<FixedArray> array) {
if (array->length() == 0) return array;
return CopyArrayWithMap(array, handle(array->map(), isolate()));
}
Handle<FixedArray> Factory::CopyAndTenureFixedCOWArray(
Handle<FixedArray> array) {
DCHECK(Heap::InYoungGeneration(*array));
Handle<FixedArray> result =
CopyFixedArrayUpTo(array, array->length(), AllocationType::kOld);
// TODO(mvstanton): The map is set twice because of protection against calling
// set() on a COW FixedArray. Issue v8:3221 created to track this, and
// we might then be able to remove this whole method.
result->set_map_after_allocation(*fixed_cow_array_map(), SKIP_WRITE_BARRIER);
return result;
}
Handle<FixedDoubleArray> Factory::CopyFixedDoubleArray(
Handle<FixedDoubleArray> array) {
int len = array->length();
if (len == 0) return array;
Handle<FixedDoubleArray> result = Handle<FixedDoubleArray>::cast(
NewFixedDoubleArray(len, AllocationType::kYoung));
Heap::CopyBlock(
result->address() + FixedDoubleArray::kLengthOffset,
array->address() + FixedDoubleArray::kLengthOffset,
FixedDoubleArray::SizeFor(len) - FixedDoubleArray::kLengthOffset);
return result;
}
Handle<Object> Factory::NewNumber(double value, AllocationType allocation) {
// Materialize as a SMI if possible.
int32_t int_value;
if (DoubleToSmiInteger(value, &int_value)) {
return handle(Smi::FromInt(int_value), isolate());
}
return NewHeapNumber(value, allocation);
}
Handle<Object> Factory::NewNumberFromInt(int32_t value,
AllocationType allocation) {
if (Smi::IsValid(value)) return handle(Smi::FromInt(value), isolate());
// Bypass NewNumber to avoid various redundant checks.
return NewHeapNumber(FastI2D(value), allocation);
}
Handle<Object> Factory::NewNumberFromUint(uint32_t value,
AllocationType allocation) {
int32_t int32v = static_cast<int32_t>(value);
if (int32v >= 0 && Smi::IsValid(int32v)) {
return handle(Smi::FromInt(int32v), isolate());
}
return NewHeapNumber(FastUI2D(value), allocation);
}
Handle<HeapNumber> Factory::NewHeapNumber(AllocationType allocation) {
STATIC_ASSERT(HeapNumber::kSize <= kMaxRegularHeapObjectSize);
Map map = *heap_number_map();
HeapObject result = AllocateRawWithImmortalMap(HeapNumber::kSize, allocation,
map, kDoubleUnaligned);
return handle(HeapNumber::cast(result), isolate());
}
Handle<MutableHeapNumber> Factory::NewMutableHeapNumber(
AllocationType allocation) {
STATIC_ASSERT(HeapNumber::kSize <= kMaxRegularHeapObjectSize);
Map map = *mutable_heap_number_map();
HeapObject result = AllocateRawWithImmortalMap(
MutableHeapNumber::kSize, allocation, map, kDoubleUnaligned);
return handle(MutableHeapNumber::cast(result), isolate());
}
Handle<FreshlyAllocatedBigInt> Factory::NewBigInt(int length,
AllocationType allocation) {
if (length < 0 || length > BigInt::kMaxLength) {
isolate()->heap()->FatalProcessOutOfMemory("invalid BigInt length");
}
HeapObject result = AllocateRawWithImmortalMap(BigInt::SizeFor(length),
allocation, *bigint_map());
FreshlyAllocatedBigInt bigint = FreshlyAllocatedBigInt::cast(result);
bigint.clear_padding();
return handle(bigint, isolate());
}
Handle<Object> Factory::NewError(Handle<JSFunction> constructor,
MessageTemplate template_index,
Handle<Object> arg0, Handle<Object> arg1,
Handle<Object> arg2) {
HandleScope scope(isolate());
if (isolate()->bootstrapper()->IsActive()) {
// During bootstrapping we cannot construct error objects.
return scope.CloseAndEscape(NewStringFromAsciiChecked(
MessageFormatter::TemplateString(template_index)));
}
if (arg0.is_null()) arg0 = undefined_value();
if (arg1.is_null()) arg1 = undefined_value();
if (arg2.is_null()) arg2 = undefined_value();
Handle<Object> result;
if (!ErrorUtils::MakeGenericError(isolate(), constructor, template_index,
arg0, arg1, arg2, SKIP_NONE)
.ToHandle(&result)) {
// If an exception is thrown while
// running the factory method, use the exception as the result.
DCHECK(isolate()->has_pending_exception());
result = handle(isolate()->pending_exception(), isolate());
isolate()->clear_pending_exception();
}
return scope.CloseAndEscape(result);
}
Handle<Object> Factory::NewError(Handle<JSFunction> constructor,
Handle<String> message) {
// Construct a new error object. If an exception is thrown, use the exception
// as the result.
Handle<Object> no_caller;
MaybeHandle<Object> maybe_error =
ErrorUtils::Construct(isolate(), constructor, constructor, message,
SKIP_NONE, no_caller, false);
if (maybe_error.is_null()) {
DCHECK(isolate()->has_pending_exception());
maybe_error = handle(isolate()->pending_exception(), isolate());
isolate()->clear_pending_exception();
}
return maybe_error.ToHandleChecked();
}
Handle<Object> Factory::NewInvalidStringLengthError() {
if (FLAG_correctness_fuzzer_suppressions) {
FATAL("Aborting on invalid string length");
}
// Invalidate the "string length" protector.
if (isolate()->IsStringLengthOverflowIntact()) {
isolate()->InvalidateStringLengthOverflowProtector();
}
return NewRangeError(MessageTemplate::kInvalidStringLength);
}
#define DEFINE_ERROR(NAME, name) \
Handle<Object> Factory::New##NAME(MessageTemplate template_index, \
Handle<Object> arg0, Handle<Object> arg1, \
Handle<Object> arg2) { \
return NewError(isolate()->name##_function(), template_index, arg0, arg1, \
arg2); \
}
DEFINE_ERROR(Error, error)
DEFINE_ERROR(EvalError, eval_error)
DEFINE_ERROR(RangeError, range_error)
DEFINE_ERROR(ReferenceError, reference_error)
DEFINE_ERROR(SyntaxError, syntax_error)
DEFINE_ERROR(TypeError, type_error)
DEFINE_ERROR(WasmCompileError, wasm_compile_error)
DEFINE_ERROR(WasmLinkError, wasm_link_error)
DEFINE_ERROR(WasmRuntimeError, wasm_runtime_error)
#undef DEFINE_ERROR
Handle<JSFunction> Factory::NewFunction(Handle<Map> map,
Handle<SharedFunctionInfo> info,
Handle<Context> context,
AllocationType allocation) {
Handle<JSFunction> function(JSFunction::cast(New(map, allocation)),
isolate());
function->initialize_properties();
function->initialize_elements();
function->set_shared(*info);
function->set_code(info->GetCode());
function->set_context(*context);
function->set_raw_feedback_cell(*many_closures_cell());
int header_size;
if (map->has_prototype_slot()) {
header_size = JSFunction::kSizeWithPrototype;
function->set_prototype_or_initial_map(*the_hole_value());