2014-03-31 12:01:02 +00:00
|
|
|
// 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.
|
|
|
|
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/factory.h"
|
2014-03-31 12:01:02 +00:00
|
|
|
|
2014-07-07 13:27:37 +00:00
|
|
|
#include "src/allocation-site-scopes.h"
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/conversions.h"
|
|
|
|
#include "src/isolate-inl.h"
|
|
|
|
#include "src/macro-assembler.h"
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2009-05-25 10:05:56 +00:00
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2014-04-15 11:51:34 +00:00
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
Handle<T> Factory::New(Handle<Map> map, AllocationSpace space) {
|
|
|
|
CALL_HEAP_FUNCTION(
|
|
|
|
isolate(),
|
|
|
|
isolate()->heap()->Allocate(*map, space),
|
|
|
|
T);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
Handle<T> Factory::New(Handle<Map> map,
|
|
|
|
AllocationSpace space,
|
|
|
|
Handle<AllocationSite> allocation_site) {
|
|
|
|
CALL_HEAP_FUNCTION(
|
|
|
|
isolate(),
|
|
|
|
isolate()->heap()->Allocate(*map, space, *allocation_site),
|
|
|
|
T);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-17 13:15:22 +00:00
|
|
|
Handle<HeapObject> Factory::NewFillerObject(int size,
|
|
|
|
bool double_align,
|
|
|
|
AllocationSpace space) {
|
|
|
|
CALL_HEAP_FUNCTION(
|
|
|
|
isolate(),
|
|
|
|
isolate()->heap()->AllocateFillerObject(size, double_align, space),
|
|
|
|
HeapObject);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-07 12:43:35 +00:00
|
|
|
Handle<Box> Factory::NewBox(Handle<Object> value) {
|
|
|
|
Handle<Box> result = Handle<Box>::cast(NewStruct(BOX_TYPE));
|
|
|
|
result->set_value(*value);
|
|
|
|
return result;
|
2013-06-06 15:40:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-16 14:15:32 +00:00
|
|
|
Handle<Oddball> Factory::NewOddball(Handle<Map> map,
|
|
|
|
const char* to_string,
|
|
|
|
Handle<Object> to_number,
|
|
|
|
byte kind) {
|
|
|
|
Handle<Oddball> oddball = New<Oddball>(map, OLD_POINTER_SPACE);
|
|
|
|
Oddball::Initialize(isolate(), oddball, to_string, to_number, kind);
|
|
|
|
return oddball;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
Handle<FixedArray> Factory::NewFixedArray(int size, PretenureFlag pretenure) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(0 <= size);
|
2011-03-18 20:35:07 +00:00
|
|
|
CALL_HEAP_FUNCTION(
|
|
|
|
isolate(),
|
|
|
|
isolate()->heap()->AllocateFixedArray(size, pretenure),
|
|
|
|
FixedArray);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-04-14 14:46:15 +00:00
|
|
|
Handle<FixedArray> Factory::NewFixedArrayWithHoles(int size,
|
|
|
|
PretenureFlag pretenure) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(0 <= size);
|
2011-03-18 20:35:07 +00:00
|
|
|
CALL_HEAP_FUNCTION(
|
|
|
|
isolate(),
|
2014-04-14 16:05:19 +00:00
|
|
|
isolate()->heap()->AllocateFixedArrayWithFiller(size,
|
|
|
|
pretenure,
|
|
|
|
*the_hole_value()),
|
2011-03-18 20:35:07 +00:00
|
|
|
FixedArray);
|
2008-10-29 10:02:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-20 13:01:08 +00:00
|
|
|
Handle<FixedArray> Factory::NewUninitializedFixedArray(int size) {
|
|
|
|
CALL_HEAP_FUNCTION(
|
|
|
|
isolate(),
|
|
|
|
isolate()->heap()->AllocateUninitializedFixedArray(size),
|
|
|
|
FixedArray);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-24 05:29:00 +00:00
|
|
|
Handle<FixedArrayBase> Factory::NewFixedDoubleArray(int size,
|
2014-04-25 16:02:50 +00:00
|
|
|
PretenureFlag pretenure) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(0 <= size);
|
2011-06-09 10:03:35 +00:00
|
|
|
CALL_HEAP_FUNCTION(
|
|
|
|
isolate(),
|
|
|
|
isolate()->heap()->AllocateUninitializedFixedDoubleArray(size, pretenure),
|
2014-04-24 05:29:00 +00:00
|
|
|
FixedArrayBase);
|
2011-06-09 10:03:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-24 05:29:00 +00:00
|
|
|
Handle<FixedArrayBase> Factory::NewFixedDoubleArrayWithHoles(
|
2014-04-14 16:05:19 +00:00
|
|
|
int size,
|
|
|
|
PretenureFlag pretenure) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(0 <= size);
|
2014-04-24 05:29:00 +00:00
|
|
|
Handle<FixedArrayBase> array = NewFixedDoubleArray(size, pretenure);
|
|
|
|
if (size > 0) {
|
|
|
|
Handle<FixedDoubleArray> double_array =
|
|
|
|
Handle<FixedDoubleArray>::cast(array);
|
|
|
|
for (int i = 0; i < size; ++i) {
|
|
|
|
double_array->set_the_hole(i);
|
|
|
|
}
|
2014-04-14 16:05:19 +00:00
|
|
|
}
|
|
|
|
return array;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-10-14 13:35:06 +00:00
|
|
|
Handle<ConstantPoolArray> Factory::NewConstantPoolArray(
|
2014-06-03 16:22:10 +00:00
|
|
|
const ConstantPoolArray::NumberOfEntries& small) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(small.total_count() > 0);
|
2013-10-14 13:35:06 +00:00
|
|
|
CALL_HEAP_FUNCTION(
|
|
|
|
isolate(),
|
2014-06-03 16:22:10 +00:00
|
|
|
isolate()->heap()->AllocateConstantPoolArray(small),
|
|
|
|
ConstantPoolArray);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Handle<ConstantPoolArray> Factory::NewExtendedConstantPoolArray(
|
|
|
|
const ConstantPoolArray::NumberOfEntries& small,
|
|
|
|
const ConstantPoolArray::NumberOfEntries& extended) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(small.total_count() > 0);
|
|
|
|
DCHECK(extended.total_count() > 0);
|
2014-06-03 16:22:10 +00:00
|
|
|
CALL_HEAP_FUNCTION(
|
|
|
|
isolate(),
|
|
|
|
isolate()->heap()->AllocateExtendedConstantPoolArray(small, extended),
|
2013-10-14 13:35:06 +00:00
|
|
|
ConstantPoolArray);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-04 20:41:57 +00:00
|
|
|
Handle<OrderedHashSet> Factory::NewOrderedHashSet() {
|
|
|
|
return OrderedHashSet::Allocate(isolate(), 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Handle<OrderedHashMap> Factory::NewOrderedHashMap() {
|
|
|
|
return OrderedHashMap::Allocate(isolate(), 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-01-10 16:11:33 +00:00
|
|
|
Handle<AccessorPair> Factory::NewAccessorPair() {
|
2014-04-07 10:12:54 +00:00
|
|
|
Handle<AccessorPair> accessors =
|
|
|
|
Handle<AccessorPair>::cast(NewStruct(ACCESSOR_PAIR_TYPE));
|
|
|
|
accessors->set_getter(*the_hole_value(), SKIP_WRITE_BARRIER);
|
|
|
|
accessors->set_setter(*the_hole_value(), SKIP_WRITE_BARRIER);
|
|
|
|
return accessors;
|
2012-01-10 16:11:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-20 12:57:23 +00:00
|
|
|
Handle<TypeFeedbackInfo> Factory::NewTypeFeedbackInfo() {
|
2014-04-07 10:12:54 +00:00
|
|
|
Handle<TypeFeedbackInfo> info =
|
|
|
|
Handle<TypeFeedbackInfo>::cast(NewStruct(TYPE_FEEDBACK_INFO_TYPE));
|
|
|
|
info->initialize_storage();
|
|
|
|
return info;
|
2012-02-20 12:57:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-02-28 17:03:34 +00:00
|
|
|
// Internalized strings are created in the old generation (data space).
|
|
|
|
Handle<String> Factory::InternalizeUtf8String(Vector<const char> string) {
|
2014-01-17 10:27:57 +00:00
|
|
|
Utf8StringKey key(string, isolate()->heap()->HashSeed());
|
|
|
|
return InternalizeStringWithKey(&key);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
2013-07-05 09:52:11 +00:00
|
|
|
|
2013-02-28 17:03:34 +00:00
|
|
|
// Internalized strings are created in the old generation (data space).
|
|
|
|
Handle<String> Factory::InternalizeString(Handle<String> string) {
|
2014-04-23 15:43:39 +00:00
|
|
|
if (string->IsInternalizedString()) return string;
|
|
|
|
return StringTable::LookupString(isolate(), string);
|
2011-05-24 12:16:23 +00:00
|
|
|
}
|
|
|
|
|
2013-07-05 09:52:11 +00:00
|
|
|
|
2013-02-28 17:03:34 +00:00
|
|
|
Handle<String> Factory::InternalizeOneByteString(Vector<const uint8_t> string) {
|
2014-01-17 10:27:57 +00:00
|
|
|
OneByteStringKey key(string, isolate()->heap()->HashSeed());
|
|
|
|
return InternalizeStringWithKey(&key);
|
2010-12-22 20:14:19 +00:00
|
|
|
}
|
|
|
|
|
2011-05-24 12:16:23 +00:00
|
|
|
|
2013-02-28 17:03:34 +00:00
|
|
|
Handle<String> Factory::InternalizeOneByteString(
|
|
|
|
Handle<SeqOneByteString> string, int from, int length) {
|
2014-01-29 14:31:34 +00:00
|
|
|
SubStringKey<uint8_t> key(string, from, length);
|
2014-01-17 10:27:57 +00:00
|
|
|
return InternalizeStringWithKey(&key);
|
2011-05-24 12:16:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-02-28 17:03:34 +00:00
|
|
|
Handle<String> Factory::InternalizeTwoByteString(Vector<const uc16> string) {
|
2014-01-17 10:27:57 +00:00
|
|
|
TwoByteStringKey key(string, isolate()->heap()->HashSeed());
|
|
|
|
return InternalizeStringWithKey(&key);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<class StringTableKey>
|
|
|
|
Handle<String> Factory::InternalizeStringWithKey(StringTableKey* key) {
|
2014-04-23 15:43:39 +00:00
|
|
|
return StringTable::LookupKey(isolate(), key);
|
2010-12-22 20:14:19 +00:00
|
|
|
}
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2014-01-29 14:31:34 +00:00
|
|
|
template Handle<String> Factory::InternalizeStringWithKey<
|
|
|
|
SubStringKey<uint8_t> > (SubStringKey<uint8_t>* key);
|
|
|
|
template Handle<String> Factory::InternalizeStringWithKey<
|
|
|
|
SubStringKey<uint16_t> > (SubStringKey<uint16_t>* key);
|
|
|
|
|
|
|
|
|
2014-04-17 13:27:02 +00:00
|
|
|
MaybeHandle<String> Factory::NewStringFromOneByte(Vector<const uint8_t> string,
|
|
|
|
PretenureFlag pretenure) {
|
|
|
|
int length = string.length();
|
2014-06-18 13:26:02 +00:00
|
|
|
if (length == 1) return LookupSingleCharacterStringFromCode(string[0]);
|
2014-04-17 13:27:02 +00:00
|
|
|
Handle<SeqOneByteString> result;
|
|
|
|
ASSIGN_RETURN_ON_EXCEPTION(
|
2011-03-18 20:35:07 +00:00
|
|
|
isolate(),
|
2014-04-17 13:27:02 +00:00
|
|
|
result,
|
|
|
|
NewRawOneByteString(string.length(), pretenure),
|
2011-03-18 20:35:07 +00:00
|
|
|
String);
|
2014-04-17 13:27:02 +00:00
|
|
|
|
|
|
|
DisallowHeapAllocation no_gc;
|
|
|
|
// Copy the characters into the new object.
|
|
|
|
CopyChars(SeqOneByteString::cast(*result)->GetChars(),
|
|
|
|
string.start(),
|
|
|
|
length);
|
|
|
|
return result;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
2014-04-17 13:27:02 +00:00
|
|
|
MaybeHandle<String> Factory::NewStringFromUtf8(Vector<const char> string,
|
|
|
|
PretenureFlag pretenure) {
|
2014-04-16 18:28:03 +00:00
|
|
|
// Check for ASCII first since this is the common case.
|
|
|
|
const char* start = string.start();
|
|
|
|
int length = string.length();
|
|
|
|
int non_ascii_start = String::NonAsciiStart(start, length);
|
|
|
|
if (non_ascii_start >= length) {
|
|
|
|
// If the string is ASCII, we do not need to convert the characters
|
|
|
|
// since UTF8 is backwards compatible with ASCII.
|
|
|
|
return NewStringFromOneByte(Vector<const uint8_t>::cast(string), pretenure);
|
|
|
|
}
|
2014-06-18 13:26:02 +00:00
|
|
|
|
2014-04-16 18:28:03 +00:00
|
|
|
// Non-ASCII and we need to decode.
|
2014-06-18 13:26:02 +00:00
|
|
|
Access<UnicodeCache::Utf8Decoder>
|
|
|
|
decoder(isolate()->unicode_cache()->utf8_decoder());
|
|
|
|
decoder->Reset(string.start() + non_ascii_start,
|
|
|
|
length - non_ascii_start);
|
|
|
|
int utf16_length = decoder->Utf16Length();
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(utf16_length > 0);
|
2014-06-18 13:26:02 +00:00
|
|
|
// Allocate string.
|
|
|
|
Handle<SeqTwoByteString> result;
|
|
|
|
ASSIGN_RETURN_ON_EXCEPTION(
|
|
|
|
isolate(), result,
|
|
|
|
NewRawTwoByteString(non_ascii_start + utf16_length, pretenure),
|
2011-03-18 20:35:07 +00:00
|
|
|
String);
|
2014-06-18 13:26:02 +00:00
|
|
|
// Copy ascii portion.
|
|
|
|
uint16_t* data = result->GetChars();
|
|
|
|
const char* ascii_data = string.start();
|
|
|
|
for (int i = 0; i < non_ascii_start; i++) {
|
|
|
|
*data++ = *ascii_data++;
|
|
|
|
}
|
|
|
|
// Now write the remainder.
|
|
|
|
decoder->WriteUtf16(data, utf16_length);
|
|
|
|
return result;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-17 13:27:02 +00:00
|
|
|
MaybeHandle<String> Factory::NewStringFromTwoByte(Vector<const uc16> string,
|
|
|
|
PretenureFlag pretenure) {
|
2014-06-18 13:26:02 +00:00
|
|
|
int length = string.length();
|
|
|
|
const uc16* start = string.start();
|
|
|
|
if (String::IsOneByte(start, length)) {
|
2014-08-11 14:04:37 +00:00
|
|
|
if (length == 1) return LookupSingleCharacterStringFromCode(string[0]);
|
2014-06-18 13:26:02 +00:00
|
|
|
Handle<SeqOneByteString> result;
|
|
|
|
ASSIGN_RETURN_ON_EXCEPTION(
|
|
|
|
isolate(),
|
|
|
|
result,
|
|
|
|
NewRawOneByteString(length, pretenure),
|
|
|
|
String);
|
|
|
|
CopyChars(result->GetChars(), start, length);
|
|
|
|
return result;
|
|
|
|
} else {
|
|
|
|
Handle<SeqTwoByteString> result;
|
|
|
|
ASSIGN_RETURN_ON_EXCEPTION(
|
|
|
|
isolate(),
|
|
|
|
result,
|
|
|
|
NewRawTwoByteString(length, pretenure),
|
|
|
|
String);
|
|
|
|
CopyChars(result->GetChars(), start, length);
|
|
|
|
return result;
|
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-25 13:59:33 +00:00
|
|
|
Handle<String> Factory::NewInternalizedStringFromUtf8(Vector<const char> str,
|
|
|
|
int chars,
|
|
|
|
uint32_t hash_field) {
|
|
|
|
CALL_HEAP_FUNCTION(
|
|
|
|
isolate(),
|
|
|
|
isolate()->heap()->AllocateInternalizedStringFromUtf8(
|
|
|
|
str, chars, hash_field),
|
|
|
|
String);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
MUST_USE_RESULT Handle<String> Factory::NewOneByteInternalizedString(
|
|
|
|
Vector<const uint8_t> str,
|
|
|
|
uint32_t hash_field) {
|
|
|
|
CALL_HEAP_FUNCTION(
|
|
|
|
isolate(),
|
|
|
|
isolate()->heap()->AllocateOneByteInternalizedString(str, hash_field),
|
|
|
|
String);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
MUST_USE_RESULT Handle<String> Factory::NewTwoByteInternalizedString(
|
|
|
|
Vector<const uc16> str,
|
|
|
|
uint32_t hash_field) {
|
|
|
|
CALL_HEAP_FUNCTION(
|
|
|
|
isolate(),
|
|
|
|
isolate()->heap()->AllocateTwoByteInternalizedString(str, hash_field),
|
|
|
|
String);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Handle<String> Factory::NewInternalizedStringImpl(
|
2014-04-25 16:02:50 +00:00
|
|
|
Handle<String> string, int chars, uint32_t hash_field) {
|
2014-04-25 13:59:33 +00:00
|
|
|
CALL_HEAP_FUNCTION(
|
|
|
|
isolate(),
|
2014-04-25 16:02:50 +00:00
|
|
|
isolate()->heap()->AllocateInternalizedStringImpl(
|
|
|
|
*string, chars, hash_field),
|
2014-04-25 13:59:33 +00:00
|
|
|
String);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
MaybeHandle<Map> Factory::InternalizedStringMapForString(
|
|
|
|
Handle<String> string) {
|
|
|
|
// If the string is in new space it cannot be used as internalized.
|
|
|
|
if (isolate()->heap()->InNewSpace(*string)) return MaybeHandle<Map>();
|
|
|
|
|
|
|
|
// Find the corresponding internalized string map for strings.
|
|
|
|
switch (string->map()->instance_type()) {
|
|
|
|
case STRING_TYPE: return internalized_string_map();
|
|
|
|
case ASCII_STRING_TYPE: return ascii_internalized_string_map();
|
|
|
|
case EXTERNAL_STRING_TYPE: return external_internalized_string_map();
|
|
|
|
case EXTERNAL_ASCII_STRING_TYPE:
|
|
|
|
return external_ascii_internalized_string_map();
|
|
|
|
case EXTERNAL_STRING_WITH_ONE_BYTE_DATA_TYPE:
|
|
|
|
return external_internalized_string_with_one_byte_data_map();
|
|
|
|
case SHORT_EXTERNAL_STRING_TYPE:
|
|
|
|
return short_external_internalized_string_map();
|
|
|
|
case SHORT_EXTERNAL_ASCII_STRING_TYPE:
|
|
|
|
return short_external_ascii_internalized_string_map();
|
|
|
|
case SHORT_EXTERNAL_STRING_WITH_ONE_BYTE_DATA_TYPE:
|
|
|
|
return short_external_internalized_string_with_one_byte_data_map();
|
|
|
|
default: return MaybeHandle<Map>(); // No match found.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-03 12:30:08 +00:00
|
|
|
MaybeHandle<SeqOneByteString> Factory::NewRawOneByteString(
|
|
|
|
int length, PretenureFlag pretenure) {
|
2014-06-18 13:26:02 +00:00
|
|
|
if (length > String::kMaxLength || length < 0) {
|
|
|
|
return isolate()->Throw<SeqOneByteString>(NewInvalidStringLengthError());
|
|
|
|
}
|
2011-03-18 20:35:07 +00:00
|
|
|
CALL_HEAP_FUNCTION(
|
|
|
|
isolate(),
|
2012-11-21 10:01:05 +00:00
|
|
|
isolate()->heap()->AllocateRawOneByteString(length, pretenure),
|
2012-11-15 13:31:27 +00:00
|
|
|
SeqOneByteString);
|
2010-07-05 12:49:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-03 12:30:08 +00:00
|
|
|
MaybeHandle<SeqTwoByteString> Factory::NewRawTwoByteString(
|
|
|
|
int length, PretenureFlag pretenure) {
|
2014-06-18 13:26:02 +00:00
|
|
|
if (length > String::kMaxLength || length < 0) {
|
|
|
|
return isolate()->Throw<SeqTwoByteString>(NewInvalidStringLengthError());
|
|
|
|
}
|
2011-03-18 20:35:07 +00:00
|
|
|
CALL_HEAP_FUNCTION(
|
|
|
|
isolate(),
|
|
|
|
isolate()->heap()->AllocateRawTwoByteString(length, pretenure),
|
2011-06-27 09:02:34 +00:00
|
|
|
SeqTwoByteString);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-23 15:43:39 +00:00
|
|
|
Handle<String> Factory::LookupSingleCharacterStringFromCode(uint32_t code) {
|
|
|
|
if (code <= String::kMaxOneByteCharCodeU) {
|
|
|
|
{
|
|
|
|
DisallowHeapAllocation no_allocation;
|
|
|
|
Object* value = single_character_string_cache()->get(code);
|
|
|
|
if (value != *undefined_value()) {
|
|
|
|
return handle(String::cast(value), isolate());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
uint8_t buffer[1];
|
|
|
|
buffer[0] = static_cast<uint8_t>(code);
|
|
|
|
Handle<String> result =
|
|
|
|
InternalizeOneByteString(Vector<const uint8_t>(buffer, 1));
|
|
|
|
single_character_string_cache()->set(code, *result);
|
|
|
|
return result;
|
|
|
|
}
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(code <= String::kMaxUtf16CodeUnitU);
|
2014-04-23 15:43:39 +00:00
|
|
|
|
|
|
|
Handle<SeqTwoByteString> result = NewRawTwoByteString(1).ToHandleChecked();
|
|
|
|
result->SeqTwoByteStringSet(0, static_cast<uint16_t>(code));
|
|
|
|
return result;
|
2014-04-08 06:45:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-12-23 12:37:56 +00:00
|
|
|
// Returns true for a character in a range. Both limits are inclusive.
|
|
|
|
static inline bool Between(uint32_t character, uint32_t from, uint32_t to) {
|
|
|
|
// This makes uses of the the unsigned wraparound.
|
|
|
|
return character - from <= to - from;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline Handle<String> MakeOrFindTwoCharacterString(Isolate* isolate,
|
|
|
|
uint16_t c1,
|
|
|
|
uint16_t c2) {
|
|
|
|
// Numeric strings have a different hash algorithm not known by
|
|
|
|
// LookupTwoCharsStringIfExists, so we skip this step for such strings.
|
|
|
|
if (!Between(c1, '0', '9') || !Between(c2, '0', '9')) {
|
2014-04-30 17:27:40 +00:00
|
|
|
Handle<String> result;
|
|
|
|
if (StringTable::LookupTwoCharsStringIfExists(isolate, c1, c2).
|
|
|
|
ToHandle(&result)) {
|
|
|
|
return result;
|
2013-12-23 12:37:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now we know the length is 2, we might as well make use of that fact
|
|
|
|
// when building the new string.
|
|
|
|
if (static_cast<unsigned>(c1 | c2) <= String::kMaxOneByteCharCodeU) {
|
|
|
|
// We can do this.
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(IsPowerOf2(String::kMaxOneByteCharCodeU + 1)); // because of this.
|
2014-04-03 12:30:08 +00:00
|
|
|
Handle<SeqOneByteString> str =
|
|
|
|
isolate->factory()->NewRawOneByteString(2).ToHandleChecked();
|
2013-12-23 12:37:56 +00:00
|
|
|
uint8_t* dest = str->GetChars();
|
|
|
|
dest[0] = static_cast<uint8_t>(c1);
|
|
|
|
dest[1] = static_cast<uint8_t>(c2);
|
|
|
|
return str;
|
|
|
|
} else {
|
2014-04-03 12:30:08 +00:00
|
|
|
Handle<SeqTwoByteString> str =
|
|
|
|
isolate->factory()->NewRawTwoByteString(2).ToHandleChecked();
|
2013-12-23 12:37:56 +00:00
|
|
|
uc16* dest = str->GetChars();
|
|
|
|
dest[0] = c1;
|
|
|
|
dest[1] = c2;
|
|
|
|
return str;
|
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-21 09:24:30 +00:00
|
|
|
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();
|
|
|
|
String::WriteToFlat(*first, sink, 0, first->length());
|
|
|
|
String::WriteToFlat(*second, sink + first->length(), 0, second->length());
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-03 12:30:37 +00:00
|
|
|
MaybeHandle<String> Factory::NewConsString(Handle<String> left,
|
|
|
|
Handle<String> right) {
|
2013-12-23 12:37:56 +00:00
|
|
|
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) {
|
2014-04-15 11:51:34 +00:00
|
|
|
return isolate()->Throw<String>(NewInvalidStringLengthError());
|
2013-12-23 12:37:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
bool is_one_byte_data_in_two_byte_string = false;
|
|
|
|
if (!is_one_byte) {
|
|
|
|
// At least one of the strings uses two-byte representation so we
|
|
|
|
// can't use the fast case code for short ASCII strings below, but
|
|
|
|
// we can try to save memory if all chars actually fit in ASCII.
|
|
|
|
is_one_byte_data_in_two_byte_string =
|
|
|
|
left->HasOnlyOneByteChars() && right->HasOnlyOneByteChars();
|
|
|
|
if (is_one_byte_data_in_two_byte_string) {
|
|
|
|
isolate()->counters()->string_add_runtime_ext_to_ascii()->Increment();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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);
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(left->IsFlat());
|
|
|
|
DCHECK(right->IsFlat());
|
2013-12-23 12:37:56 +00:00
|
|
|
|
2014-03-25 09:09:24 +00:00
|
|
|
STATIC_ASSERT(ConsString::kMinLength <= String::kMaxLength);
|
2013-12-23 12:37:56 +00:00
|
|
|
if (is_one_byte) {
|
2014-04-03 12:30:08 +00:00
|
|
|
Handle<SeqOneByteString> result =
|
|
|
|
NewRawOneByteString(length).ToHandleChecked();
|
2013-12-23 12:37:56 +00:00
|
|
|
DisallowHeapAllocation no_gc;
|
|
|
|
uint8_t* dest = result->GetChars();
|
|
|
|
// Copy left part.
|
|
|
|
const uint8_t* src = left->IsExternalString()
|
|
|
|
? Handle<ExternalAsciiString>::cast(left)->GetChars()
|
|
|
|
: Handle<SeqOneByteString>::cast(left)->GetChars();
|
|
|
|
for (int i = 0; i < left_length; i++) *dest++ = src[i];
|
|
|
|
// Copy right part.
|
|
|
|
src = right->IsExternalString()
|
|
|
|
? Handle<ExternalAsciiString>::cast(right)->GetChars()
|
|
|
|
: Handle<SeqOneByteString>::cast(right)->GetChars();
|
|
|
|
for (int i = 0; i < right_length; i++) *dest++ = src[i];
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (is_one_byte_data_in_two_byte_string)
|
2014-04-03 12:30:08 +00:00
|
|
|
? ConcatStringContent<uint8_t>(
|
|
|
|
NewRawOneByteString(length).ToHandleChecked(), left, right)
|
|
|
|
: ConcatStringContent<uc16>(
|
|
|
|
NewRawTwoByteString(length).ToHandleChecked(), left, right);
|
2013-12-23 12:37:56 +00:00
|
|
|
}
|
|
|
|
|
2014-04-29 07:02:11 +00:00
|
|
|
Handle<Map> map = (is_one_byte || is_one_byte_data_in_two_byte_string)
|
|
|
|
? cons_ascii_string_map() : cons_string_map();
|
|
|
|
Handle<ConsString> result = New<ConsString>(map, NEW_SPACE);
|
2013-12-23 12:37:56 +00:00
|
|
|
|
|
|
|
DisallowHeapAllocation no_gc;
|
|
|
|
WriteBarrierMode mode = result->GetWriteBarrierMode(no_gc);
|
|
|
|
|
|
|
|
result->set_hash_field(String::kEmptyHashField);
|
|
|
|
result->set_length(length);
|
|
|
|
result->set_first(*left, mode);
|
|
|
|
result->set_second(*right, mode);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-06-27 09:02:34 +00:00
|
|
|
Handle<String> Factory::NewProperSubString(Handle<String> str,
|
2011-06-22 14:20:23 +00:00
|
|
|
int begin,
|
|
|
|
int end) {
|
2013-12-23 12:37:56 +00:00
|
|
|
#if VERIFY_HEAP
|
|
|
|
if (FLAG_verify_heap) str->StringVerify();
|
|
|
|
#endif
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(begin > 0 || end < str->length());
|
2013-12-23 12:37:56 +00:00
|
|
|
|
2014-04-22 11:19:27 +00:00
|
|
|
str = String::Flatten(str);
|
|
|
|
|
2013-12-23 12:37:56 +00:00
|
|
|
int length = end - begin;
|
|
|
|
if (length <= 0) return empty_string();
|
|
|
|
if (length == 1) {
|
2014-04-08 06:45:53 +00:00
|
|
|
return LookupSingleCharacterStringFromCode(str->Get(begin));
|
2013-12-23 12:37:56 +00:00
|
|
|
}
|
|
|
|
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()) {
|
2014-04-03 12:30:08 +00:00
|
|
|
Handle<SeqOneByteString> result =
|
|
|
|
NewRawOneByteString(length).ToHandleChecked();
|
2013-12-23 12:37:56 +00:00
|
|
|
uint8_t* dest = result->GetChars();
|
|
|
|
DisallowHeapAllocation no_gc;
|
|
|
|
String::WriteToFlat(*str, dest, begin, end);
|
|
|
|
return result;
|
|
|
|
} else {
|
2014-04-03 12:30:08 +00:00
|
|
|
Handle<SeqTwoByteString> result =
|
|
|
|
NewRawTwoByteString(length).ToHandleChecked();
|
2013-12-23 12:37:56 +00:00
|
|
|
uc16* dest = result->GetChars();
|
|
|
|
DisallowHeapAllocation 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();
|
|
|
|
}
|
|
|
|
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(str->IsSeqString() || str->IsExternalString());
|
2014-04-29 07:02:11 +00:00
|
|
|
Handle<Map> map = str->IsOneByteRepresentation() ? sliced_ascii_string_map()
|
|
|
|
: sliced_string_map();
|
|
|
|
Handle<SlicedString> slice = New<SlicedString>(map, NEW_SPACE);
|
2013-12-23 12:37:56 +00:00
|
|
|
|
|
|
|
slice->set_hash_field(String::kEmptyHashField);
|
|
|
|
slice->set_length(length);
|
|
|
|
slice->set_parent(*str);
|
|
|
|
slice->set_offset(offset);
|
|
|
|
return slice;
|
2011-06-22 14:20:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-03 12:41:37 +00:00
|
|
|
MaybeHandle<String> Factory::NewExternalStringFromAscii(
|
2011-09-21 13:28:09 +00:00
|
|
|
const ExternalAsciiString::Resource* resource) {
|
2014-04-16 13:35:36 +00:00
|
|
|
size_t length = resource->length();
|
|
|
|
if (length > static_cast<size_t>(String::kMaxLength)) {
|
2014-06-18 13:26:02 +00:00
|
|
|
return isolate()->Throw<String>(NewInvalidStringLengthError());
|
2014-04-16 13:35:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Handle<Map> map = external_ascii_string_map();
|
|
|
|
Handle<ExternalAsciiString> external_string =
|
|
|
|
New<ExternalAsciiString>(map, NEW_SPACE);
|
|
|
|
external_string->set_length(static_cast<int>(length));
|
|
|
|
external_string->set_hash_field(String::kEmptyHashField);
|
|
|
|
external_string->set_resource(resource);
|
|
|
|
|
|
|
|
return external_string;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-03 12:41:37 +00:00
|
|
|
MaybeHandle<String> Factory::NewExternalStringFromTwoByte(
|
2011-09-21 13:28:09 +00:00
|
|
|
const ExternalTwoByteString::Resource* resource) {
|
2014-04-16 13:35:36 +00:00
|
|
|
size_t length = resource->length();
|
|
|
|
if (length > static_cast<size_t>(String::kMaxLength)) {
|
2014-06-18 13:26:02 +00:00
|
|
|
return isolate()->Throw<String>(NewInvalidStringLengthError());
|
2014-04-16 13:35:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// For small strings we check whether the resource contains only
|
|
|
|
// one byte characters. If yes, we use a different string map.
|
|
|
|
static const size_t kOneByteCheckLengthLimit = 32;
|
|
|
|
bool is_one_byte = length <= kOneByteCheckLengthLimit &&
|
|
|
|
String::IsOneByte(resource->data(), static_cast<int>(length));
|
|
|
|
Handle<Map> map = is_one_byte ?
|
|
|
|
external_string_with_one_byte_data_map() : external_string_map();
|
|
|
|
Handle<ExternalTwoByteString> external_string =
|
|
|
|
New<ExternalTwoByteString>(map, NEW_SPACE);
|
|
|
|
external_string->set_length(static_cast<int>(length));
|
|
|
|
external_string->set_hash_field(String::kEmptyHashField);
|
|
|
|
external_string->set_resource(resource);
|
|
|
|
|
|
|
|
return external_string;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-03-01 10:34:31 +00:00
|
|
|
Handle<Symbol> Factory::NewSymbol() {
|
|
|
|
CALL_HEAP_FUNCTION(
|
|
|
|
isolate(),
|
|
|
|
isolate()->heap()->AllocateSymbol(),
|
|
|
|
Symbol);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
Provide private symbols through internal APIs
Adds a notion of private symbols, mainly intended for internal use, especially, self-hosting of built-in types that would otherwise require new C++ classes.
On the JS side (i.e., in built-ins), private properties can be created and accessed through a set of macros:
NEW_PRIVATE(print_name)
HAS_PRIVATE(obj, sym)
GET_PRIVATE(obj, sym)
SET_PRIVATE(obj, sym, val)
DELETE_PRIVATE(obj, sym)
In the V8 API, they are accessible via a new class Private, and respective HasPrivate/Get/Private/SetPrivate/DeletePrivate methods on calss Object.
These APIs are designed and restricted such that their implementation can later be replaced by whatever ES7+ will officially provide.
R=yangguo@chromium.org
BUG=
Review URL: https://codereview.chromium.org/48923002
git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@17683 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2013-11-13 10:34:06 +00:00
|
|
|
Handle<Symbol> Factory::NewPrivateSymbol() {
|
2014-04-29 13:58:55 +00:00
|
|
|
Handle<Symbol> symbol = NewSymbol();
|
|
|
|
symbol->set_is_private(true);
|
|
|
|
return symbol;
|
Provide private symbols through internal APIs
Adds a notion of private symbols, mainly intended for internal use, especially, self-hosting of built-in types that would otherwise require new C++ classes.
On the JS side (i.e., in built-ins), private properties can be created and accessed through a set of macros:
NEW_PRIVATE(print_name)
HAS_PRIVATE(obj, sym)
GET_PRIVATE(obj, sym)
SET_PRIVATE(obj, sym, val)
DELETE_PRIVATE(obj, sym)
In the V8 API, they are accessible via a new class Private, and respective HasPrivate/Get/Private/SetPrivate/DeletePrivate methods on calss Object.
These APIs are designed and restricted such that their implementation can later be replaced by whatever ES7+ will officially provide.
R=yangguo@chromium.org
BUG=
Review URL: https://codereview.chromium.org/48923002
git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@17683 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2013-11-13 10:34:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-08-12 15:28:20 +00:00
|
|
|
Handle<Symbol> Factory::NewPrivateOwnSymbol() {
|
|
|
|
Handle<Symbol> symbol = NewSymbol();
|
|
|
|
symbol->set_is_private(true);
|
|
|
|
symbol->set_is_own(true);
|
|
|
|
return symbol;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-08-17 09:03:08 +00:00
|
|
|
Handle<Context> Factory::NewNativeContext() {
|
2014-04-07 10:12:54 +00:00
|
|
|
Handle<FixedArray> array = NewFixedArray(Context::NATIVE_CONTEXT_SLOTS);
|
|
|
|
array->set_map_no_write_barrier(*native_context_map());
|
|
|
|
Handle<Context> context = Handle<Context>::cast(array);
|
|
|
|
context->set_js_array_maps(*undefined_value());
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(context->IsNativeContext());
|
2014-04-07 10:12:54 +00:00
|
|
|
return context;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-08-27 09:40:26 +00:00
|
|
|
Handle<Context> Factory::NewGlobalContext(Handle<JSFunction> function,
|
|
|
|
Handle<ScopeInfo> scope_info) {
|
2014-04-09 08:51:46 +00:00
|
|
|
Handle<FixedArray> array =
|
|
|
|
NewFixedArray(scope_info->ContextLength(), TENURED);
|
|
|
|
array->set_map_no_write_barrier(*global_context_map());
|
|
|
|
Handle<Context> context = Handle<Context>::cast(array);
|
|
|
|
context->set_closure(*function);
|
|
|
|
context->set_previous(function->context());
|
|
|
|
context->set_extension(*scope_info);
|
|
|
|
context->set_global_object(function->context()->global_object());
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(context->IsGlobalContext());
|
2014-04-09 08:51:46 +00:00
|
|
|
return context;
|
2012-08-27 09:40:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-09 08:59:03 +00:00
|
|
|
Handle<Context> Factory::NewModuleContext(Handle<ScopeInfo> scope_info) {
|
2014-04-07 10:12:54 +00:00
|
|
|
Handle<FixedArray> array =
|
|
|
|
NewFixedArray(scope_info->ContextLength(), TENURED);
|
|
|
|
array->set_map_no_write_barrier(*module_context_map());
|
|
|
|
// Instance link will be set later.
|
|
|
|
Handle<Context> context = Handle<Context>::cast(array);
|
|
|
|
context->set_extension(Smi::FromInt(0));
|
|
|
|
return context;
|
2012-04-16 14:43:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
Handle<Context> Factory::NewFunctionContext(int length,
|
2011-06-29 07:41:42 +00:00
|
|
|
Handle<JSFunction> function) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(length >= Context::MIN_CONTEXT_SLOTS);
|
2014-04-09 08:51:46 +00:00
|
|
|
Handle<FixedArray> array = NewFixedArray(length);
|
|
|
|
array->set_map_no_write_barrier(*function_context_map());
|
|
|
|
Handle<Context> context = Handle<Context>::cast(array);
|
|
|
|
context->set_closure(*function);
|
|
|
|
context->set_previous(function->context());
|
|
|
|
context->set_extension(Smi::FromInt(0));
|
|
|
|
context->set_global_object(function->context()->global_object());
|
|
|
|
return context;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-06-29 07:41:42 +00:00
|
|
|
Handle<Context> Factory::NewCatchContext(Handle<JSFunction> function,
|
|
|
|
Handle<Context> previous,
|
2011-06-16 06:37:49 +00:00
|
|
|
Handle<String> name,
|
|
|
|
Handle<Object> thrown_object) {
|
2014-04-09 08:51:46 +00:00
|
|
|
STATIC_ASSERT(Context::MIN_CONTEXT_SLOTS == Context::THROWN_OBJECT_INDEX);
|
|
|
|
Handle<FixedArray> array = NewFixedArray(Context::MIN_CONTEXT_SLOTS + 1);
|
|
|
|
array->set_map_no_write_barrier(*catch_context_map());
|
|
|
|
Handle<Context> context = Handle<Context>::cast(array);
|
|
|
|
context->set_closure(*function);
|
|
|
|
context->set_previous(*previous);
|
|
|
|
context->set_extension(*name);
|
|
|
|
context->set_global_object(previous->global_object());
|
|
|
|
context->set(Context::THROWN_OBJECT_INDEX, *thrown_object);
|
|
|
|
return context;
|
2011-06-09 11:26:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-06-29 07:41:42 +00:00
|
|
|
Handle<Context> Factory::NewWithContext(Handle<JSFunction> function,
|
|
|
|
Handle<Context> previous,
|
2014-04-09 08:51:46 +00:00
|
|
|
Handle<JSReceiver> extension) {
|
|
|
|
Handle<FixedArray> array = NewFixedArray(Context::MIN_CONTEXT_SLOTS);
|
|
|
|
array->set_map_no_write_barrier(*with_context_map());
|
|
|
|
Handle<Context> context = Handle<Context>::cast(array);
|
|
|
|
context->set_closure(*function);
|
|
|
|
context->set_previous(*previous);
|
|
|
|
context->set_extension(*extension);
|
|
|
|
context->set_global_object(previous->global_object());
|
|
|
|
return context;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-04-16 14:43:27 +00:00
|
|
|
Handle<Context> Factory::NewBlockContext(Handle<JSFunction> function,
|
|
|
|
Handle<Context> previous,
|
|
|
|
Handle<ScopeInfo> scope_info) {
|
2014-04-09 08:51:46 +00:00
|
|
|
Handle<FixedArray> array =
|
|
|
|
NewFixedArrayWithHoles(scope_info->ContextLength());
|
|
|
|
array->set_map_no_write_barrier(*block_context_map());
|
|
|
|
Handle<Context> context = Handle<Context>::cast(array);
|
|
|
|
context->set_closure(*function);
|
|
|
|
context->set_previous(*previous);
|
|
|
|
context->set_extension(*scope_info);
|
|
|
|
context->set_global_object(previous->global_object());
|
|
|
|
return context;
|
2011-08-11 16:29:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
Handle<Struct> Factory::NewStruct(InstanceType type) {
|
2011-03-18 20:35:07 +00:00
|
|
|
CALL_HEAP_FUNCTION(
|
|
|
|
isolate(),
|
|
|
|
isolate()->heap()->AllocateStruct(type),
|
|
|
|
Struct);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-16 12:10:56 +00:00
|
|
|
Handle<CodeCache> Factory::NewCodeCache() {
|
2014-04-16 14:04:54 +00:00
|
|
|
Handle<CodeCache> code_cache =
|
|
|
|
Handle<CodeCache>::cast(NewStruct(CODE_CACHE_TYPE));
|
|
|
|
code_cache->set_default_cache(*empty_fixed_array(), SKIP_WRITE_BARRIER);
|
|
|
|
code_cache->set_normal_type_cache(*undefined_value(), SKIP_WRITE_BARRIER);
|
|
|
|
return code_cache;
|
2014-04-16 12:10:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-11-14 17:30:48 +00:00
|
|
|
Handle<AliasedArgumentsEntry> Factory::NewAliasedArgumentsEntry(
|
|
|
|
int aliased_context_slot) {
|
|
|
|
Handle<AliasedArgumentsEntry> entry = Handle<AliasedArgumentsEntry>::cast(
|
|
|
|
NewStruct(ALIASED_ARGUMENTS_ENTRY_TYPE));
|
|
|
|
entry->set_aliased_context_slot(aliased_context_slot);
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-03-07 11:42:58 +00:00
|
|
|
Handle<DeclaredAccessorDescriptor> Factory::NewDeclaredAccessorDescriptor() {
|
|
|
|
return Handle<DeclaredAccessorDescriptor>::cast(
|
|
|
|
NewStruct(DECLARED_ACCESSOR_DESCRIPTOR_TYPE));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-02-12 14:33:08 +00:00
|
|
|
Handle<DeclaredAccessorInfo> Factory::NewDeclaredAccessorInfo() {
|
|
|
|
Handle<DeclaredAccessorInfo> info =
|
|
|
|
Handle<DeclaredAccessorInfo>::cast(
|
|
|
|
NewStruct(DECLARED_ACCESSOR_INFO_TYPE));
|
|
|
|
info->set_flag(0); // Must clear the flag, it was initialized as undefined.
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Handle<ExecutableAccessorInfo> Factory::NewExecutableAccessorInfo() {
|
|
|
|
Handle<ExecutableAccessorInfo> info =
|
|
|
|
Handle<ExecutableAccessorInfo>::cast(
|
|
|
|
NewStruct(EXECUTABLE_ACCESSOR_INFO_TYPE));
|
2008-07-03 15:10:15 +00:00
|
|
|
info->set_flag(0); // Must clear the flag, it was initialized as undefined.
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Handle<Script> Factory::NewScript(Handle<String> source) {
|
2009-03-10 12:05:20 +00:00
|
|
|
// Generate id for this script.
|
2011-03-18 20:35:07 +00:00
|
|
|
Heap* heap = isolate()->heap();
|
2013-06-25 14:57:47 +00:00
|
|
|
int id = heap->last_script_id()->value() + 1;
|
|
|
|
if (!Smi::IsValid(id) || id < 0) id = 1;
|
|
|
|
heap->set_last_script_id(Smi::FromInt(id));
|
2009-03-10 12:41:11 +00:00
|
|
|
|
2009-03-10 12:05:20 +00:00
|
|
|
// Create and initialize script object.
|
2011-05-19 11:47:34 +00:00
|
|
|
Handle<Foreign> wrapper = NewForeign(0, TENURED);
|
2008-07-03 15:10:15 +00:00
|
|
|
Handle<Script> script = Handle<Script>::cast(NewStruct(SCRIPT_TYPE));
|
|
|
|
script->set_source(*source);
|
2011-03-18 20:35:07 +00:00
|
|
|
script->set_name(heap->undefined_value());
|
2013-06-25 14:57:47 +00:00
|
|
|
script->set_id(Smi::FromInt(id));
|
2008-07-03 15:10:15 +00:00
|
|
|
script->set_line_offset(Smi::FromInt(0));
|
|
|
|
script->set_column_offset(Smi::FromInt(0));
|
2011-03-18 20:35:07 +00:00
|
|
|
script->set_context_data(heap->undefined_value());
|
2009-06-08 10:47:49 +00:00
|
|
|
script->set_type(Smi::FromInt(Script::TYPE_NORMAL));
|
2009-05-07 12:25:46 +00:00
|
|
|
script->set_wrapper(*wrapper);
|
2011-03-18 20:35:07 +00:00
|
|
|
script->set_line_ends(heap->undefined_value());
|
|
|
|
script->set_eval_from_shared(heap->undefined_value());
|
2009-06-08 10:47:49 +00:00
|
|
|
script->set_eval_from_instructions_offset(Smi::FromInt(0));
|
2013-07-30 17:00:05 +00:00
|
|
|
script->set_flags(Smi::FromInt(0));
|
2009-03-10 12:05:20 +00:00
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
return script;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-19 11:47:34 +00:00
|
|
|
Handle<Foreign> Factory::NewForeign(Address addr, PretenureFlag pretenure) {
|
2011-03-18 20:35:07 +00:00
|
|
|
CALL_HEAP_FUNCTION(isolate(),
|
2011-05-19 11:47:34 +00:00
|
|
|
isolate()->heap()->AllocateForeign(addr, pretenure),
|
|
|
|
Foreign);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-19 11:47:34 +00:00
|
|
|
Handle<Foreign> Factory::NewForeign(const AccessorDescriptor* desc) {
|
|
|
|
return NewForeign((Address) desc, TENURED);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-11-25 11:07:48 +00:00
|
|
|
Handle<ByteArray> Factory::NewByteArray(int length, PretenureFlag pretenure) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(0 <= length);
|
2011-03-18 20:35:07 +00:00
|
|
|
CALL_HEAP_FUNCTION(
|
|
|
|
isolate(),
|
|
|
|
isolate()->heap()->AllocateByteArray(length, pretenure),
|
|
|
|
ByteArray);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-10-20 15:26:17 +00:00
|
|
|
Handle<ExternalArray> Factory::NewExternalArray(int length,
|
|
|
|
ExternalArrayType array_type,
|
|
|
|
void* external_pointer,
|
|
|
|
PretenureFlag pretenure) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(0 <= length && length <= Smi::kMaxValue);
|
2011-03-18 20:35:07 +00:00
|
|
|
CALL_HEAP_FUNCTION(
|
|
|
|
isolate(),
|
|
|
|
isolate()->heap()->AllocateExternalArray(length,
|
|
|
|
array_type,
|
|
|
|
external_pointer,
|
|
|
|
pretenure),
|
|
|
|
ExternalArray);
|
2009-10-20 15:26:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-01-16 17:08:45 +00:00
|
|
|
Handle<FixedTypedArrayBase> Factory::NewFixedTypedArray(
|
|
|
|
int length,
|
|
|
|
ExternalArrayType array_type,
|
|
|
|
PretenureFlag pretenure) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(0 <= length && length <= Smi::kMaxValue);
|
2014-01-16 17:08:45 +00:00
|
|
|
CALL_HEAP_FUNCTION(
|
|
|
|
isolate(),
|
|
|
|
isolate()->heap()->AllocateFixedTypedArray(length,
|
|
|
|
array_type,
|
|
|
|
pretenure),
|
|
|
|
FixedTypedArrayBase);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-12 15:03:44 +00:00
|
|
|
Handle<Cell> Factory::NewCell(Handle<Object> value) {
|
|
|
|
AllowDeferredHandleDereference convert_to_cell;
|
|
|
|
CALL_HEAP_FUNCTION(
|
|
|
|
isolate(),
|
|
|
|
isolate()->heap()->AllocateCell(*value),
|
|
|
|
Cell);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-10-21 13:55:24 +00:00
|
|
|
Handle<PropertyCell> Factory::NewPropertyCellWithHole() {
|
2011-03-18 20:35:07 +00:00
|
|
|
CALL_HEAP_FUNCTION(
|
|
|
|
isolate(),
|
2013-10-21 13:55:24 +00:00
|
|
|
isolate()->heap()->AllocatePropertyCell(),
|
2013-06-14 16:06:12 +00:00
|
|
|
PropertyCell);
|
2010-12-07 11:31:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-10-21 13:55:24 +00:00
|
|
|
Handle<PropertyCell> Factory::NewPropertyCell(Handle<Object> value) {
|
|
|
|
AllowDeferredHandleDereference convert_to_cell;
|
|
|
|
Handle<PropertyCell> cell = NewPropertyCellWithHole();
|
|
|
|
PropertyCell::SetValueInferType(cell, value);
|
|
|
|
return cell;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-08 10:02:16 +00:00
|
|
|
Handle<AllocationSite> Factory::NewAllocationSite() {
|
2014-04-16 12:54:29 +00:00
|
|
|
Handle<Map> map = allocation_site_map();
|
|
|
|
Handle<AllocationSite> site = New<AllocationSite>(map, OLD_POINTER_SPACE);
|
|
|
|
site->Initialize();
|
|
|
|
|
|
|
|
// Link the site
|
|
|
|
site->set_weak_next(isolate()->heap()->allocation_sites_list());
|
|
|
|
isolate()->heap()->set_allocation_sites_list(*site);
|
|
|
|
return site;
|
2013-07-08 10:02:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-22 11:30:04 +00:00
|
|
|
Handle<Map> Factory::NewMap(InstanceType type,
|
|
|
|
int instance_size,
|
|
|
|
ElementsKind elements_kind) {
|
2011-03-18 20:35:07 +00:00
|
|
|
CALL_HEAP_FUNCTION(
|
|
|
|
isolate(),
|
2011-09-22 11:30:04 +00:00
|
|
|
isolate()->heap()->AllocateMap(type, instance_size, elements_kind),
|
2011-03-18 20:35:07 +00:00
|
|
|
Map);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-29 07:02:11 +00:00
|
|
|
Handle<JSObject> Factory::CopyJSObject(Handle<JSObject> object) {
|
|
|
|
CALL_HEAP_FUNCTION(isolate(),
|
|
|
|
isolate()->heap()->CopyJSObject(*object, NULL),
|
|
|
|
JSObject);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Handle<JSObject> Factory::CopyJSObjectWithAllocationSite(
|
|
|
|
Handle<JSObject> object,
|
|
|
|
Handle<AllocationSite> site) {
|
|
|
|
CALL_HEAP_FUNCTION(isolate(),
|
|
|
|
isolate()->heap()->CopyJSObject(
|
|
|
|
*object,
|
|
|
|
site.is_null() ? NULL : *site),
|
|
|
|
JSObject);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-28 15:33:16 +00:00
|
|
|
Handle<FixedArray> Factory::CopyFixedArrayWithMap(Handle<FixedArray> array,
|
|
|
|
Handle<Map> map) {
|
|
|
|
CALL_HEAP_FUNCTION(isolate(),
|
|
|
|
isolate()->heap()->CopyFixedArrayWithMap(*array, *map),
|
|
|
|
FixedArray);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-28 15:33:16 +00:00
|
|
|
Handle<FixedArray> Factory::CopyFixedArray(Handle<FixedArray> array) {
|
2014-03-17 13:42:37 +00:00
|
|
|
CALL_HEAP_FUNCTION(isolate(),
|
2014-04-28 15:33:16 +00:00
|
|
|
isolate()->heap()->CopyFixedArray(*array),
|
2014-03-17 13:42:37 +00:00
|
|
|
FixedArray);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-28 15:33:16 +00:00
|
|
|
Handle<FixedArray> Factory::CopyAndTenureFixedCOWArray(
|
|
|
|
Handle<FixedArray> array) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(isolate()->heap()->InNewSpace(*array));
|
2013-10-04 07:25:24 +00:00
|
|
|
CALL_HEAP_FUNCTION(isolate(),
|
2014-04-28 15:33:16 +00:00
|
|
|
isolate()->heap()->CopyAndTenureFixedCOWArray(*array),
|
2013-10-04 07:25:24 +00:00
|
|
|
FixedArray);
|
2012-11-13 12:27:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-10-19 11:36:55 +00:00
|
|
|
Handle<FixedDoubleArray> Factory::CopyFixedDoubleArray(
|
|
|
|
Handle<FixedDoubleArray> array) {
|
2014-04-28 15:33:16 +00:00
|
|
|
CALL_HEAP_FUNCTION(isolate(),
|
|
|
|
isolate()->heap()->CopyFixedDoubleArray(*array),
|
|
|
|
FixedDoubleArray);
|
2011-10-19 11:36:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-10-14 13:35:06 +00:00
|
|
|
Handle<ConstantPoolArray> Factory::CopyConstantPoolArray(
|
|
|
|
Handle<ConstantPoolArray> array) {
|
2014-04-28 15:33:16 +00:00
|
|
|
CALL_HEAP_FUNCTION(isolate(),
|
|
|
|
isolate()->heap()->CopyConstantPoolArray(*array),
|
|
|
|
ConstantPoolArray);
|
2013-10-14 13:35:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
Handle<Object> Factory::NewNumber(double value,
|
|
|
|
PretenureFlag pretenure) {
|
2014-04-16 14:04:54 +00:00
|
|
|
// We need to distinguish the minus zero value and this cannot be
|
|
|
|
// done after conversion to int. Doing this by comparing bit
|
|
|
|
// patterns is faster than using fpclassify() et al.
|
2014-07-01 15:02:31 +00:00
|
|
|
if (IsMinusZero(value)) return NewHeapNumber(-0.0, IMMUTABLE, pretenure);
|
2014-04-16 14:04:54 +00:00
|
|
|
|
|
|
|
int int_value = FastD2I(value);
|
|
|
|
if (value == int_value && Smi::IsValid(int_value)) {
|
|
|
|
return handle(Smi::FromInt(int_value), isolate());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Materialize the value in the heap.
|
2014-07-01 15:02:31 +00:00
|
|
|
return NewHeapNumber(value, IMMUTABLE, pretenure);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-21 10:18:47 +00:00
|
|
|
Handle<Object> Factory::NewNumberFromInt(int32_t value,
|
|
|
|
PretenureFlag pretenure) {
|
2014-04-16 14:04:54 +00:00
|
|
|
if (Smi::IsValid(value)) return handle(Smi::FromInt(value), isolate());
|
2014-07-01 15:02:31 +00:00
|
|
|
// Bypass NewNumber to avoid various redundant checks.
|
|
|
|
return NewHeapNumber(FastI2D(value), IMMUTABLE, pretenure);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-21 10:18:47 +00:00
|
|
|
Handle<Object> Factory::NewNumberFromUint(uint32_t value,
|
2014-04-30 12:25:18 +00:00
|
|
|
PretenureFlag pretenure) {
|
|
|
|
int32_t int32v = static_cast<int32_t>(value);
|
|
|
|
if (int32v >= 0 && Smi::IsValid(int32v)) {
|
|
|
|
return handle(Smi::FromInt(int32v), isolate());
|
|
|
|
}
|
2014-07-01 15:02:31 +00:00
|
|
|
return NewHeapNumber(FastUI2D(value), IMMUTABLE, pretenure);
|
2009-03-10 08:10:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-05-08 15:02:08 +00:00
|
|
|
Handle<HeapNumber> Factory::NewHeapNumber(double value,
|
2014-07-01 15:02:31 +00:00
|
|
|
MutableMode mode,
|
2013-05-08 15:02:08 +00:00
|
|
|
PretenureFlag pretenure) {
|
|
|
|
CALL_HEAP_FUNCTION(
|
|
|
|
isolate(),
|
2014-07-01 15:02:31 +00:00
|
|
|
isolate()->heap()->AllocateHeapNumber(value, mode, pretenure),
|
|
|
|
HeapNumber);
|
2013-05-08 15:02:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-06 13:28:22 +00:00
|
|
|
Handle<Object> Factory::NewTypeError(const char* message,
|
2008-07-03 15:10:15 +00:00
|
|
|
Vector< Handle<Object> > args) {
|
2013-06-06 13:28:22 +00:00
|
|
|
return NewError("MakeTypeError", message, args);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Handle<Object> Factory::NewTypeError(Handle<String> message) {
|
|
|
|
return NewError("$TypeError", message);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-06 13:28:22 +00:00
|
|
|
Handle<Object> Factory::NewRangeError(const char* message,
|
2008-07-03 15:10:15 +00:00
|
|
|
Vector< Handle<Object> > args) {
|
2013-06-06 13:28:22 +00:00
|
|
|
return NewError("MakeRangeError", message, args);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Handle<Object> Factory::NewRangeError(Handle<String> message) {
|
|
|
|
return NewError("$RangeError", message);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-06 13:28:22 +00:00
|
|
|
Handle<Object> Factory::NewSyntaxError(const char* message,
|
|
|
|
Handle<JSArray> args) {
|
|
|
|
return NewError("MakeSyntaxError", message, args);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Handle<Object> Factory::NewSyntaxError(Handle<String> message) {
|
|
|
|
return NewError("$SyntaxError", message);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-06 13:28:22 +00:00
|
|
|
Handle<Object> Factory::NewReferenceError(const char* message,
|
2008-07-03 15:10:15 +00:00
|
|
|
Vector< Handle<Object> > args) {
|
2013-06-06 13:28:22 +00:00
|
|
|
return NewError("MakeReferenceError", message, args);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-17 10:21:01 +00:00
|
|
|
Handle<Object> Factory::NewReferenceError(const char* message,
|
|
|
|
Handle<JSArray> args) {
|
|
|
|
return NewError("MakeReferenceError", message, args);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
Handle<Object> Factory::NewReferenceError(Handle<String> message) {
|
|
|
|
return NewError("$ReferenceError", message);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-03-15 12:06:53 +00:00
|
|
|
Handle<Object> Factory::NewError(const char* maker,
|
2013-06-06 13:28:22 +00:00
|
|
|
const char* message,
|
2013-03-15 12:06:53 +00:00
|
|
|
Vector< Handle<Object> > args) {
|
|
|
|
// Instantiate a closeable HandleScope for EscapeFrom.
|
2013-12-02 18:12:01 +00:00
|
|
|
v8::EscapableHandleScope scope(reinterpret_cast<v8::Isolate*>(isolate()));
|
2011-03-18 20:35:07 +00:00
|
|
|
Handle<FixedArray> array = NewFixedArray(args.length());
|
2008-10-09 13:34:17 +00:00
|
|
|
for (int i = 0; i < args.length(); i++) {
|
|
|
|
array->set(i, *args[i]);
|
|
|
|
}
|
2011-03-18 20:35:07 +00:00
|
|
|
Handle<JSArray> object = NewJSArrayWithElements(array);
|
2013-06-06 13:28:22 +00:00
|
|
|
Handle<Object> result = NewError(maker, message, object);
|
2008-07-03 15:10:15 +00:00
|
|
|
return result.EscapeFrom(&scope);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-06 13:28:22 +00:00
|
|
|
Handle<Object> Factory::NewEvalError(const char* message,
|
2008-07-03 15:10:15 +00:00
|
|
|
Vector< Handle<Object> > args) {
|
2013-06-06 13:28:22 +00:00
|
|
|
return NewError("MakeEvalError", message, args);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-06 13:28:22 +00:00
|
|
|
Handle<Object> Factory::NewError(const char* message,
|
2008-07-03 15:10:15 +00:00
|
|
|
Vector< Handle<Object> > args) {
|
2013-06-06 13:28:22 +00:00
|
|
|
return NewError("MakeError", message, args);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-06 13:28:22 +00:00
|
|
|
Handle<String> Factory::EmergencyNewError(const char* message,
|
2012-05-31 12:26:36 +00:00
|
|
|
Handle<JSArray> args) {
|
|
|
|
const int kBufferSize = 1000;
|
|
|
|
char buffer[kBufferSize];
|
|
|
|
size_t space = kBufferSize;
|
|
|
|
char* p = &buffer[0];
|
|
|
|
|
|
|
|
Vector<char> v(buffer, kBufferSize);
|
2014-06-13 16:43:27 +00:00
|
|
|
StrNCpy(v, message, space);
|
2013-06-06 13:28:22 +00:00
|
|
|
space -= Min(space, strlen(message));
|
2012-05-31 12:26:36 +00:00
|
|
|
p = &buffer[kBufferSize] - space;
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < ARRAY_SIZE(args); i++) {
|
|
|
|
if (space > 0) {
|
|
|
|
*p++ = ' ';
|
|
|
|
space--;
|
|
|
|
if (space > 0) {
|
2014-03-18 12:34:02 +00:00
|
|
|
Handle<String> arg_str = Handle<String>::cast(
|
2014-04-11 12:47:34 +00:00
|
|
|
Object::GetElement(isolate(), args, i).ToHandleChecked());
|
2013-12-09 07:41:20 +00:00
|
|
|
SmartArrayPointer<char> arg = arg_str->ToCString();
|
2012-06-19 13:45:30 +00:00
|
|
|
Vector<char> v2(p, static_cast<int>(space));
|
2014-06-13 16:43:27 +00:00
|
|
|
StrNCpy(v2, arg.get(), space);
|
2013-12-09 07:41:20 +00:00
|
|
|
space -= Min(space, strlen(arg.get()));
|
2012-05-31 12:26:36 +00:00
|
|
|
p = &buffer[kBufferSize] - space;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (space > 0) {
|
|
|
|
*p = '\0';
|
|
|
|
} else {
|
|
|
|
buffer[kBufferSize - 1] = '\0';
|
|
|
|
}
|
2014-04-17 13:27:02 +00:00
|
|
|
return NewStringFromUtf8(CStrVector(buffer), TENURED).ToHandleChecked();
|
2012-05-31 12:26:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
Handle<Object> Factory::NewError(const char* maker,
|
2013-06-06 13:28:22 +00:00
|
|
|
const char* message,
|
2008-07-03 15:10:15 +00:00
|
|
|
Handle<JSArray> args) {
|
2013-02-28 17:03:34 +00:00
|
|
|
Handle<String> make_str = InternalizeUtf8String(maker);
|
2014-04-11 12:47:34 +00:00
|
|
|
Handle<Object> fun_obj = Object::GetProperty(
|
|
|
|
isolate()->js_builtins_object(), make_str).ToHandleChecked();
|
2009-09-10 06:52:46 +00:00
|
|
|
// If the builtins haven't been properly configured yet this error
|
|
|
|
// constructor may not have been defined. Bail out.
|
2012-05-31 12:26:36 +00:00
|
|
|
if (!fun_obj->IsJSFunction()) {
|
2013-06-06 13:28:22 +00:00
|
|
|
return EmergencyNewError(message, args);
|
2012-05-31 12:26:36 +00:00
|
|
|
}
|
2009-09-10 06:52:46 +00:00
|
|
|
Handle<JSFunction> fun = Handle<JSFunction>::cast(fun_obj);
|
2013-06-06 13:28:22 +00:00
|
|
|
Handle<Object> message_obj = InternalizeUtf8String(message);
|
|
|
|
Handle<Object> argv[] = { message_obj, args };
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
// Invoke the JavaScript factory method. If an exception is thrown while
|
|
|
|
// running the factory method, use the exception as the result.
|
2014-04-11 10:41:09 +00:00
|
|
|
Handle<Object> result;
|
|
|
|
Handle<Object> exception;
|
|
|
|
if (!Execution::TryCall(fun,
|
|
|
|
isolate()->js_builtins_object(),
|
|
|
|
ARRAY_SIZE(argv),
|
|
|
|
argv,
|
|
|
|
&exception).ToHandle(&result)) {
|
|
|
|
return exception;
|
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Handle<Object> Factory::NewError(Handle<String> message) {
|
|
|
|
return NewError("$Error", message);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Handle<Object> Factory::NewError(const char* constructor,
|
|
|
|
Handle<String> message) {
|
2013-02-28 17:03:34 +00:00
|
|
|
Handle<String> constr = InternalizeUtf8String(constructor);
|
2014-04-11 12:47:34 +00:00
|
|
|
Handle<JSFunction> fun = Handle<JSFunction>::cast(Object::GetProperty(
|
|
|
|
isolate()->js_builtins_object(), constr).ToHandleChecked());
|
2011-10-06 09:31:38 +00:00
|
|
|
Handle<Object> argv[] = { message };
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
// Invoke the JavaScript factory method. If an exception is thrown while
|
|
|
|
// running the factory method, use the exception as the result.
|
2014-04-11 10:41:09 +00:00
|
|
|
Handle<Object> result;
|
|
|
|
Handle<Object> exception;
|
|
|
|
if (!Execution::TryCall(fun,
|
|
|
|
isolate()->js_builtins_object(),
|
|
|
|
ARRAY_SIZE(argv),
|
|
|
|
argv,
|
|
|
|
&exception).ToHandle(&result)) {
|
|
|
|
return exception;
|
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-06 11:26:35 +00:00
|
|
|
void Factory::InitializeFunction(Handle<JSFunction> function,
|
|
|
|
Handle<SharedFunctionInfo> info,
|
|
|
|
Handle<Context> context) {
|
|
|
|
function->initialize_properties();
|
|
|
|
function->initialize_elements();
|
|
|
|
function->set_shared(*info);
|
|
|
|
function->set_code(info->code());
|
|
|
|
function->set_context(*context);
|
|
|
|
function->set_prototype_or_initial_map(*the_hole_value());
|
|
|
|
function->set_literals_or_bindings(*empty_fixed_array());
|
|
|
|
function->set_next_function_link(*undefined_value());
|
2014-07-21 09:58:01 +00:00
|
|
|
if (info->is_arrow()) function->RemovePrototype();
|
2014-05-06 11:26:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Handle<JSFunction> Factory::NewFunction(Handle<Map> map,
|
|
|
|
Handle<SharedFunctionInfo> info,
|
|
|
|
Handle<Context> context,
|
|
|
|
PretenureFlag pretenure) {
|
|
|
|
AllocationSpace space = pretenure == TENURED ? OLD_POINTER_SPACE : NEW_SPACE;
|
|
|
|
Handle<JSFunction> result = New<JSFunction>(map, space);
|
|
|
|
InitializeFunction(result, info, context);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-09 17:39:54 +00:00
|
|
|
Handle<JSFunction> Factory::NewFunction(Handle<Map> map,
|
|
|
|
Handle<String> name,
|
|
|
|
MaybeHandle<Code> code) {
|
2014-07-01 12:12:34 +00:00
|
|
|
Handle<Context> context(isolate()->native_context());
|
2014-05-09 17:39:54 +00:00
|
|
|
Handle<SharedFunctionInfo> info = NewSharedFunctionInfo(name, code);
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK((info->strict_mode() == SLOPPY) &&
|
2014-05-09 17:39:54 +00:00
|
|
|
(map.is_identical_to(isolate()->sloppy_function_map()) ||
|
|
|
|
map.is_identical_to(
|
2014-05-09 17:59:15 +00:00
|
|
|
isolate()->sloppy_function_without_prototype_map()) ||
|
|
|
|
map.is_identical_to(
|
|
|
|
isolate()->sloppy_function_with_readonly_prototype_map())));
|
2014-05-09 17:39:54 +00:00
|
|
|
return NewFunction(map, info, context);
|
2014-05-06 11:26:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-09 16:39:33 +00:00
|
|
|
Handle<JSFunction> Factory::NewFunction(Handle<String> name) {
|
2014-05-09 17:39:54 +00:00
|
|
|
return NewFunction(
|
|
|
|
isolate()->sloppy_function_map(), name, MaybeHandle<Code>());
|
2014-05-06 11:26:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-09 17:39:54 +00:00
|
|
|
Handle<JSFunction> Factory::NewFunctionWithoutPrototype(Handle<String> name,
|
|
|
|
Handle<Code> code) {
|
|
|
|
return NewFunction(
|
|
|
|
isolate()->sloppy_function_without_prototype_map(), name, code);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Handle<JSFunction> Factory::NewFunction(Handle<String> name,
|
|
|
|
Handle<Code> code,
|
2014-05-09 17:59:15 +00:00
|
|
|
Handle<Object> prototype,
|
|
|
|
bool read_only_prototype) {
|
|
|
|
Handle<Map> map = read_only_prototype
|
|
|
|
? isolate()->sloppy_function_with_readonly_prototype_map()
|
|
|
|
: isolate()->sloppy_function_map();
|
|
|
|
Handle<JSFunction> result = NewFunction(map, name, code);
|
2014-05-09 17:39:54 +00:00
|
|
|
result->set_prototype_or_initial_map(*prototype);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Handle<JSFunction> Factory::NewFunction(Handle<String> name,
|
|
|
|
Handle<Code> code,
|
|
|
|
Handle<Object> prototype,
|
2008-07-03 15:10:15 +00:00
|
|
|
InstanceType type,
|
2014-05-09 17:59:15 +00:00
|
|
|
int instance_size,
|
|
|
|
bool read_only_prototype) {
|
2008-07-03 15:10:15 +00:00
|
|
|
// Allocate the function
|
2014-05-09 17:59:15 +00:00
|
|
|
Handle<JSFunction> function = NewFunction(
|
|
|
|
name, code, prototype, read_only_prototype);
|
2014-05-09 16:59:03 +00:00
|
|
|
|
|
|
|
Handle<Map> initial_map = NewMap(
|
|
|
|
type, instance_size, GetInitialFastElementsKind());
|
|
|
|
if (prototype->IsTheHole() && !function->shared()->is_generator()) {
|
|
|
|
prototype = NewFunctionPrototype(function);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
2014-08-07 16:14:22 +00:00
|
|
|
|
2014-08-11 14:00:58 +00:00
|
|
|
JSFunction::SetInitialMap(function, initial_map,
|
|
|
|
Handle<JSReceiver>::cast(prototype));
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
return function;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-29 17:48:07 +00:00
|
|
|
Handle<JSFunction> Factory::NewFunction(Handle<String> name,
|
2014-05-09 17:39:54 +00:00
|
|
|
Handle<Code> code,
|
2014-04-29 17:48:07 +00:00
|
|
|
InstanceType type,
|
2014-05-09 17:39:54 +00:00
|
|
|
int instance_size) {
|
|
|
|
return NewFunction(name, code, the_hole_value(), type, instance_size);
|
2014-04-29 17:48:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-06 11:26:35 +00:00
|
|
|
Handle<JSObject> Factory::NewFunctionPrototype(Handle<JSFunction> function) {
|
|
|
|
// Make sure to use globals from the function's context, since the function
|
|
|
|
// can be from a different context.
|
|
|
|
Handle<Context> native_context(function->context()->native_context());
|
|
|
|
Handle<Map> new_map;
|
|
|
|
if (function->shared()->is_generator()) {
|
|
|
|
// Generator prototypes can share maps since they don't have "constructor"
|
|
|
|
// properties.
|
|
|
|
new_map = handle(native_context->generator_object_prototype_map());
|
|
|
|
} else {
|
|
|
|
// Each function prototype gets a fresh map to avoid unwanted sharing of
|
|
|
|
// maps between prototypes of different constructors.
|
|
|
|
Handle<JSFunction> object_function(native_context->object_function());
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(object_function->has_initial_map());
|
2014-08-07 16:14:22 +00:00
|
|
|
new_map = handle(object_function->initial_map());
|
2014-05-06 11:26:35 +00:00
|
|
|
}
|
|
|
|
|
2014-08-11 14:00:58 +00:00
|
|
|
DCHECK(!new_map->is_prototype_map());
|
2014-05-06 11:26:35 +00:00
|
|
|
Handle<JSObject> prototype = NewJSObjectFromMap(new_map);
|
|
|
|
|
|
|
|
if (!function->shared()->is_generator()) {
|
2014-06-30 13:48:57 +00:00
|
|
|
JSObject::AddProperty(prototype, constructor_string(), function, DONT_ENUM);
|
2014-05-06 11:26:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return prototype;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Handle<JSFunction> Factory::NewFunctionFromSharedFunctionInfo(
|
|
|
|
Handle<SharedFunctionInfo> info,
|
|
|
|
Handle<Context> context,
|
|
|
|
PretenureFlag pretenure) {
|
|
|
|
int map_index = Context::FunctionMapIndex(info->strict_mode(),
|
|
|
|
info->is_generator());
|
|
|
|
Handle<Map> map(Map::cast(context->native_context()->get(map_index)));
|
|
|
|
Handle<JSFunction> result = NewFunction(map, info, context, pretenure);
|
|
|
|
|
|
|
|
if (info->ic_age() != isolate()->heap()->global_ic_age()) {
|
|
|
|
info->ResetForNewContext(isolate()->heap()->global_ic_age());
|
|
|
|
}
|
|
|
|
|
|
|
|
int index = info->SearchOptimizedCodeMap(context->native_context(),
|
|
|
|
BailoutId::None());
|
|
|
|
if (!info->bound() && index < 0) {
|
|
|
|
int number_of_literals = info->num_literals();
|
|
|
|
Handle<FixedArray> literals = NewFixedArray(number_of_literals, pretenure);
|
|
|
|
if (number_of_literals > 0) {
|
|
|
|
// Store the native context in the literals array prefix. This
|
|
|
|
// context will be used when creating object, regexp and array
|
|
|
|
// literals in this function.
|
|
|
|
literals->set(JSFunction::kLiteralNativeContextIndex,
|
|
|
|
context->native_context());
|
|
|
|
}
|
|
|
|
result->set_literals(*literals);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (index > 0) {
|
|
|
|
// Caching of optimized code enabled and optimized code found.
|
|
|
|
FixedArray* literals = info->GetLiteralsFromOptimizedCodeMap(index);
|
|
|
|
if (literals != NULL) result->set_literals(literals);
|
|
|
|
Code* code = info->GetCodeFromOptimizedCodeMap(index);
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(!code->marked_for_deoptimization());
|
2014-05-06 11:26:35 +00:00
|
|
|
result->ReplaceCode(code);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isolate()->use_crankshaft() &&
|
|
|
|
FLAG_always_opt &&
|
|
|
|
result->is_compiled() &&
|
2014-07-29 12:57:25 +00:00
|
|
|
!info->is_toplevel() &&
|
2014-05-06 11:26:35 +00:00
|
|
|
info->allows_lazy_compilation() &&
|
|
|
|
!info->optimization_disabled() &&
|
|
|
|
!isolate()->DebuggerHasBreakPoints()) {
|
|
|
|
result->MarkForOptimization();
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-03 10:36:55 +00:00
|
|
|
Handle<ScopeInfo> Factory::NewScopeInfo(int length) {
|
2014-04-07 10:12:54 +00:00
|
|
|
Handle<FixedArray> array = NewFixedArray(length, TENURED);
|
|
|
|
array->set_map_no_write_barrier(*scope_info_map());
|
|
|
|
Handle<ScopeInfo> scope_info = Handle<ScopeInfo>::cast(array);
|
|
|
|
return scope_info;
|
2011-08-11 16:29:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-11-13 12:27:03 +00:00
|
|
|
Handle<JSObject> Factory::NewExternal(void* value) {
|
2014-04-07 10:12:54 +00:00
|
|
|
Handle<Foreign> foreign = NewForeign(static_cast<Address>(value));
|
|
|
|
Handle<JSObject> external = NewJSObjectFromMap(external_map());
|
|
|
|
external->SetInternalField(0, *foreign);
|
|
|
|
return external;
|
2012-11-13 12:27:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-29 13:58:55 +00:00
|
|
|
Handle<Code> Factory::NewCodeRaw(int object_size, bool immovable) {
|
|
|
|
CALL_HEAP_FUNCTION(isolate(),
|
|
|
|
isolate()->heap()->AllocateCode(object_size, immovable),
|
2014-04-16 11:38:56 +00:00
|
|
|
Code);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-05-18 09:41:16 +00:00
|
|
|
Handle<Code> Factory::NewCode(const CodeDesc& desc,
|
|
|
|
Code::Flags flags,
|
2011-03-09 10:38:19 +00:00
|
|
|
Handle<Object> self_ref,
|
2013-04-18 09:50:46 +00:00
|
|
|
bool immovable,
|
2013-10-23 13:48:04 +00:00
|
|
|
bool crankshafted,
|
2014-05-12 13:47:01 +00:00
|
|
|
int prologue_offset,
|
|
|
|
bool is_debug) {
|
2014-04-16 11:38:56 +00:00
|
|
|
Handle<ByteArray> reloc_info = NewByteArray(desc.reloc_size, TENURED);
|
|
|
|
Handle<ConstantPoolArray> constant_pool =
|
|
|
|
desc.origin->NewConstantPool(isolate());
|
|
|
|
|
|
|
|
// Compute size.
|
|
|
|
int body_size = RoundUp(desc.instr_size, kObjectAlignment);
|
|
|
|
int obj_size = Code::SizeFor(body_size);
|
|
|
|
|
2014-04-29 13:58:55 +00:00
|
|
|
Handle<Code> code = NewCodeRaw(obj_size, immovable);
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(isolate()->code_range() == NULL ||
|
2014-06-12 12:01:01 +00:00
|
|
|
!isolate()->code_range()->valid() ||
|
2014-04-16 11:38:56 +00:00
|
|
|
isolate()->code_range()->contains(code->address()));
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
code->set_gc_metadata(Smi::FromInt(0));
|
|
|
|
code->set_ic_age(isolate()->heap()->global_ic_age());
|
|
|
|
code->set_instruction_size(desc.instr_size);
|
|
|
|
code->set_relocation_info(*reloc_info);
|
|
|
|
code->set_flags(flags);
|
|
|
|
code->set_raw_kind_specific_flags1(0);
|
|
|
|
code->set_raw_kind_specific_flags2(0);
|
|
|
|
code->set_is_crankshafted(crankshafted);
|
|
|
|
code->set_deoptimization_data(*empty_fixed_array(), SKIP_WRITE_BARRIER);
|
2014-07-21 13:51:42 +00:00
|
|
|
code->set_raw_type_feedback_info(Smi::FromInt(0));
|
2014-04-16 11:38:56 +00:00
|
|
|
code->set_next_code_link(*undefined_value());
|
|
|
|
code->set_handler_table(*empty_fixed_array(), SKIP_WRITE_BARRIER);
|
|
|
|
code->set_prologue_offset(prologue_offset);
|
|
|
|
if (code->kind() == Code::OPTIMIZED_FUNCTION) {
|
|
|
|
code->set_marked_for_deoptimization(false);
|
|
|
|
}
|
|
|
|
|
2014-05-12 13:47:01 +00:00
|
|
|
if (is_debug) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(code->kind() == Code::FUNCTION);
|
2014-05-12 13:47:01 +00:00
|
|
|
code->set_has_debug_break_slots(true);
|
|
|
|
}
|
|
|
|
|
2014-04-16 11:38:56 +00:00
|
|
|
desc.origin->PopulateConstantPool(*constant_pool);
|
|
|
|
code->set_constant_pool(*constant_pool);
|
|
|
|
|
|
|
|
// Allow self references to created code object by patching the handle to
|
|
|
|
// point to the newly allocated Code object.
|
|
|
|
if (!self_ref.is_null()) *(self_ref.location()) = *code;
|
|
|
|
|
|
|
|
// Migrate generated code.
|
|
|
|
// The generated code can contain Object** values (typically from handles)
|
|
|
|
// 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->CopyFrom(desc);
|
|
|
|
|
|
|
|
#ifdef VERIFY_HEAP
|
2014-04-30 12:25:18 +00:00
|
|
|
if (FLAG_verify_heap) code->ObjectVerify();
|
2014-04-16 11:38:56 +00:00
|
|
|
#endif
|
|
|
|
return code;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Handle<Code> Factory::CopyCode(Handle<Code> code) {
|
2011-03-18 20:35:07 +00:00
|
|
|
CALL_HEAP_FUNCTION(isolate(),
|
|
|
|
isolate()->heap()->CopyCode(*code),
|
|
|
|
Code);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-15 21:06:51 +00:00
|
|
|
Handle<Code> Factory::CopyCode(Handle<Code> code, Vector<byte> reloc_info) {
|
2011-03-18 20:35:07 +00:00
|
|
|
CALL_HEAP_FUNCTION(isolate(),
|
|
|
|
isolate()->heap()->CopyCode(*code, reloc_info),
|
|
|
|
Code);
|
2010-03-15 21:06:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
Handle<JSObject> Factory::NewJSObject(Handle<JSFunction> constructor,
|
|
|
|
PretenureFlag pretenure) {
|
2013-11-05 12:11:27 +00:00
|
|
|
JSFunction::EnsureHasInitialMap(constructor);
|
2011-03-18 20:35:07 +00:00
|
|
|
CALL_HEAP_FUNCTION(
|
|
|
|
isolate(),
|
|
|
|
isolate()->heap()->AllocateJSObject(*constructor, pretenure), JSObject);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-19 13:39:09 +00:00
|
|
|
Handle<JSObject> Factory::NewJSObjectWithMemento(
|
|
|
|
Handle<JSFunction> constructor,
|
|
|
|
Handle<AllocationSite> site) {
|
|
|
|
JSFunction::EnsureHasInitialMap(constructor);
|
|
|
|
CALL_HEAP_FUNCTION(
|
|
|
|
isolate(),
|
|
|
|
isolate()->heap()->AllocateJSObject(*constructor, NOT_TENURED, *site),
|
|
|
|
JSObject);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-09 08:59:03 +00:00
|
|
|
Handle<JSModule> Factory::NewJSModule(Handle<Context> context,
|
|
|
|
Handle<ScopeInfo> scope_info) {
|
2014-04-07 12:43:35 +00:00
|
|
|
// Allocate a fresh map. Modules do not have a prototype.
|
|
|
|
Handle<Map> map = NewMap(JS_MODULE_TYPE, JSModule::kSize);
|
|
|
|
// Allocate the object based on the map.
|
|
|
|
Handle<JSModule> module =
|
|
|
|
Handle<JSModule>::cast(NewJSObjectFromMap(map, TENURED));
|
|
|
|
module->set_context(*context);
|
|
|
|
module->set_scope_info(*scope_info);
|
|
|
|
return module;
|
2012-04-16 14:43:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-10-21 13:55:24 +00:00
|
|
|
Handle<GlobalObject> Factory::NewGlobalObject(Handle<JSFunction> constructor) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(constructor->has_initial_map());
|
2013-10-21 13:55:24 +00:00
|
|
|
Handle<Map> map(constructor->initial_map());
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(map->is_dictionary_map());
|
2013-10-21 13:55:24 +00:00
|
|
|
|
|
|
|
// Make sure no field properties are described in the initial map.
|
|
|
|
// This guarantees us that normalizing the properties does not
|
|
|
|
// require us to change property values to PropertyCells.
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(map->NextFreePropertyIndex() == 0);
|
2013-10-21 13:55:24 +00:00
|
|
|
|
|
|
|
// Make sure we don't have a ton of pre-allocated slots in the
|
|
|
|
// global objects. They will be unused once we normalize the object.
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(map->unused_property_fields() == 0);
|
|
|
|
DCHECK(map->inobject_properties() == 0);
|
2013-10-21 13:55:24 +00:00
|
|
|
|
|
|
|
// Initial size of the backing store to avoid resize of the storage during
|
|
|
|
// bootstrapping. The size differs between the JS global object ad the
|
|
|
|
// builtins object.
|
|
|
|
int initial_size = map->instance_type() == JS_GLOBAL_OBJECT_TYPE ? 64 : 512;
|
|
|
|
|
|
|
|
// Allocate a dictionary object for backing storage.
|
|
|
|
int at_least_space_for = map->NumberOfOwnDescriptors() * 2 + initial_size;
|
2014-04-25 07:56:13 +00:00
|
|
|
Handle<NameDictionary> dictionary =
|
|
|
|
NameDictionary::New(isolate(), at_least_space_for);
|
2013-10-21 13:55:24 +00:00
|
|
|
|
|
|
|
// The global object might be created from an object template with accessors.
|
|
|
|
// Fill these accessors into the dictionary.
|
|
|
|
Handle<DescriptorArray> descs(map->instance_descriptors());
|
|
|
|
for (int i = 0; i < map->NumberOfOwnDescriptors(); i++) {
|
|
|
|
PropertyDetails details = descs->GetDetails(i);
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(details.type() == CALLBACKS); // Only accessors are expected.
|
2013-10-21 13:55:24 +00:00
|
|
|
PropertyDetails d = PropertyDetails(details.attributes(), CALLBACKS, i + 1);
|
|
|
|
Handle<Name> name(descs->GetKey(i));
|
|
|
|
Handle<Object> value(descs->GetCallbacksObject(i), isolate());
|
|
|
|
Handle<PropertyCell> cell = NewPropertyCell(value);
|
2014-04-24 15:45:44 +00:00
|
|
|
// |dictionary| already contains enough space for all properties.
|
|
|
|
USE(NameDictionary::Add(dictionary, name, cell, d));
|
2013-10-21 13:55:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Allocate the global object and initialize it with the backing store.
|
2014-04-15 11:51:34 +00:00
|
|
|
Handle<GlobalObject> global = New<GlobalObject>(map, OLD_POINTER_SPACE);
|
2013-10-21 13:55:24 +00:00
|
|
|
isolate()->heap()->InitializeJSObjectFromMap(*global, *dictionary, *map);
|
|
|
|
|
|
|
|
// Create a new map for the global object.
|
|
|
|
Handle<Map> new_map = Map::CopyDropDescriptors(map);
|
|
|
|
new_map->set_dictionary_map(true);
|
|
|
|
|
|
|
|
// Set up the global object as a normalized object.
|
|
|
|
global->set_map(*new_map);
|
|
|
|
global->set_properties(*dictionary);
|
|
|
|
|
|
|
|
// Make sure result is a global object with properties in dictionary.
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(global->IsGlobalObject() && !global->HasFastProperties());
|
2013-10-21 13:55:24 +00:00
|
|
|
return global;
|
|
|
|
}
|
|
|
|
|
2009-06-30 10:05:36 +00:00
|
|
|
|
2014-03-17 15:01:45 +00:00
|
|
|
Handle<JSObject> Factory::NewJSObjectFromMap(
|
|
|
|
Handle<Map> map,
|
|
|
|
PretenureFlag pretenure,
|
|
|
|
bool alloc_props,
|
|
|
|
Handle<AllocationSite> allocation_site) {
|
2011-03-18 20:35:07 +00:00
|
|
|
CALL_HEAP_FUNCTION(
|
|
|
|
isolate(),
|
2014-03-17 15:01:45 +00:00
|
|
|
isolate()->heap()->AllocateJSObjectFromMap(
|
|
|
|
*map,
|
|
|
|
pretenure,
|
|
|
|
alloc_props,
|
|
|
|
allocation_site.is_null() ? NULL : *allocation_site),
|
2011-03-18 20:35:07 +00:00
|
|
|
JSObject);
|
2008-09-25 07:46:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-07 10:00:14 +00:00
|
|
|
Handle<JSArray> Factory::NewJSArray(ElementsKind elements_kind,
|
|
|
|
PretenureFlag pretenure) {
|
2014-04-07 12:43:35 +00:00
|
|
|
Context* native_context = isolate()->context()->native_context();
|
|
|
|
JSFunction* array_function = native_context->array_function();
|
|
|
|
Map* map = array_function->initial_map();
|
|
|
|
Map* transition_map = isolate()->get_initial_js_array_map(elements_kind);
|
|
|
|
if (transition_map != NULL) map = transition_map;
|
|
|
|
return Handle<JSArray>::cast(NewJSObjectFromMap(handle(map), pretenure));
|
2014-04-07 10:00:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-21 11:22:16 +00:00
|
|
|
Handle<JSArray> Factory::NewJSArray(ElementsKind elements_kind,
|
|
|
|
int length,
|
|
|
|
int capacity,
|
2014-03-27 16:41:09 +00:00
|
|
|
ArrayStorageAllocationMode mode,
|
2008-07-03 15:10:15 +00:00
|
|
|
PretenureFlag pretenure) {
|
2014-04-07 12:43:35 +00:00
|
|
|
Handle<JSArray> array = NewJSArray(elements_kind, pretenure);
|
|
|
|
NewJSArrayStorage(array, length, capacity, mode);
|
|
|
|
return array;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-12-09 08:50:19 +00:00
|
|
|
Handle<JSArray> Factory::NewJSArrayWithElements(Handle<FixedArrayBase> elements,
|
2012-01-26 21:47:57 +00:00
|
|
|
ElementsKind elements_kind,
|
2014-03-21 11:22:16 +00:00
|
|
|
int length,
|
2008-07-03 15:10:15 +00:00
|
|
|
PretenureFlag pretenure) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(length <= elements->length());
|
2014-04-15 11:51:34 +00:00
|
|
|
Handle<JSArray> array = NewJSArray(elements_kind, pretenure);
|
2014-04-07 10:00:14 +00:00
|
|
|
|
|
|
|
array->set_elements(*elements);
|
|
|
|
array->set_length(Smi::FromInt(length));
|
|
|
|
JSObject::ValidateElements(array);
|
|
|
|
return array;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-17 15:01:45 +00:00
|
|
|
void Factory::NewJSArrayStorage(Handle<JSArray> array,
|
2014-04-14 16:05:19 +00:00
|
|
|
int length,
|
|
|
|
int capacity,
|
|
|
|
ArrayStorageAllocationMode mode) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(capacity >= length);
|
2014-04-14 16:05:19 +00:00
|
|
|
|
|
|
|
if (capacity == 0) {
|
|
|
|
array->set_length(Smi::FromInt(0));
|
|
|
|
array->set_elements(*empty_fixed_array());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Handle<FixedArrayBase> elms;
|
|
|
|
ElementsKind elements_kind = array->GetElementsKind();
|
|
|
|
if (IsFastDoubleElementsKind(elements_kind)) {
|
|
|
|
if (mode == DONT_INITIALIZE_ARRAY_ELEMENTS) {
|
|
|
|
elms = NewFixedDoubleArray(capacity);
|
|
|
|
} else {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(mode == INITIALIZE_ARRAY_ELEMENTS_WITH_HOLE);
|
2014-04-14 16:05:19 +00:00
|
|
|
elms = NewFixedDoubleArrayWithHoles(capacity);
|
|
|
|
}
|
|
|
|
} else {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(IsFastSmiOrObjectElementsKind(elements_kind));
|
2014-04-14 16:05:19 +00:00
|
|
|
if (mode == DONT_INITIALIZE_ARRAY_ELEMENTS) {
|
|
|
|
elms = NewUninitializedFixedArray(capacity);
|
|
|
|
} else {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(mode == INITIALIZE_ARRAY_ELEMENTS_WITH_HOLE);
|
2014-04-14 16:05:19 +00:00
|
|
|
elms = NewFixedArrayWithHoles(capacity);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
array->set_elements(*elms);
|
|
|
|
array->set_length(Smi::FromInt(length));
|
2014-03-17 15:01:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-11-05 12:11:27 +00:00
|
|
|
Handle<JSGeneratorObject> Factory::NewJSGeneratorObject(
|
|
|
|
Handle<JSFunction> function) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(function->shared()->is_generator());
|
2013-11-05 12:11:27 +00:00
|
|
|
JSFunction::EnsureHasInitialMap(function);
|
|
|
|
Handle<Map> map(function->initial_map());
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(map->instance_type() == JS_GENERATOR_OBJECT_TYPE);
|
2014-04-08 06:15:20 +00:00
|
|
|
CALL_HEAP_FUNCTION(
|
|
|
|
isolate(),
|
|
|
|
isolate()->heap()->AllocateJSObjectFromMap(*map),
|
|
|
|
JSGeneratorObject);
|
2013-11-05 12:11:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-04-25 12:02:23 +00:00
|
|
|
Handle<JSArrayBuffer> Factory::NewJSArrayBuffer() {
|
2013-08-06 19:14:51 +00:00
|
|
|
Handle<JSFunction> array_buffer_fun(
|
2014-07-01 12:12:34 +00:00
|
|
|
isolate()->native_context()->array_buffer_fun());
|
2014-04-08 06:15:20 +00:00
|
|
|
CALL_HEAP_FUNCTION(
|
|
|
|
isolate(),
|
|
|
|
isolate()->heap()->AllocateJSObject(*array_buffer_fun),
|
|
|
|
JSArrayBuffer);
|
2013-04-25 12:02:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-21 13:02:38 +00:00
|
|
|
Handle<JSDataView> Factory::NewJSDataView() {
|
2013-08-06 19:14:51 +00:00
|
|
|
Handle<JSFunction> data_view_fun(
|
2014-07-01 12:12:34 +00:00
|
|
|
isolate()->native_context()->data_view_fun());
|
2014-04-08 06:15:20 +00:00
|
|
|
CALL_HEAP_FUNCTION(
|
|
|
|
isolate(),
|
|
|
|
isolate()->heap()->AllocateJSObject(*data_view_fun),
|
|
|
|
JSDataView);
|
2013-06-21 13:02:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-08-06 19:14:51 +00:00
|
|
|
static JSFunction* GetTypedArrayFun(ExternalArrayType type,
|
|
|
|
Isolate* isolate) {
|
|
|
|
Context* native_context = isolate->context()->native_context();
|
2013-04-29 11:09:03 +00:00
|
|
|
switch (type) {
|
2014-01-24 16:01:15 +00:00
|
|
|
#define TYPED_ARRAY_FUN(Type, type, TYPE, ctype, size) \
|
|
|
|
case kExternal##Type##Array: \
|
|
|
|
return native_context->type##_array_fun();
|
2013-04-29 11:09:03 +00:00
|
|
|
|
2014-01-24 16:01:15 +00:00
|
|
|
TYPED_ARRAYS(TYPED_ARRAY_FUN)
|
|
|
|
#undef TYPED_ARRAY_FUN
|
2013-05-02 11:36:48 +00:00
|
|
|
|
2013-04-29 11:09:03 +00:00
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
2013-08-06 19:14:51 +00:00
|
|
|
return NULL;
|
2013-04-29 11:09:03 +00:00
|
|
|
}
|
2013-08-06 19:14:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Handle<JSTypedArray> Factory::NewJSTypedArray(ExternalArrayType type) {
|
|
|
|
Handle<JSFunction> typed_array_fun_handle(GetTypedArrayFun(type, isolate()));
|
2014-04-08 06:15:20 +00:00
|
|
|
|
|
|
|
CALL_HEAP_FUNCTION(
|
|
|
|
isolate(),
|
|
|
|
isolate()->heap()->AllocateJSObject(*typed_array_fun_handle),
|
|
|
|
JSTypedArray);
|
2013-04-29 11:09:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-13 10:58:25 +00:00
|
|
|
Handle<JSProxy> Factory::NewJSProxy(Handle<Object> handler,
|
|
|
|
Handle<Object> prototype) {
|
2014-04-16 13:35:36 +00:00
|
|
|
// Allocate map.
|
|
|
|
// TODO(rossberg): Once we optimize proxies, think about a scheme to share
|
|
|
|
// maps. Will probably depend on the identity of the handler object, too.
|
|
|
|
Handle<Map> map = NewMap(JS_PROXY_TYPE, JSProxy::kSize);
|
|
|
|
map->set_prototype(*prototype);
|
|
|
|
|
|
|
|
// Allocate the proxy object.
|
|
|
|
Handle<JSProxy> result = New<JSProxy>(map, NEW_SPACE);
|
|
|
|
result->InitializeBody(map->instance_size(), Smi::FromInt(0));
|
|
|
|
result->set_handler(*handler);
|
|
|
|
result->set_hash(*undefined_value(), SKIP_WRITE_BARRIER);
|
|
|
|
return result;
|
2011-05-13 10:58:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-15 10:59:38 +00:00
|
|
|
Handle<JSProxy> Factory::NewJSFunctionProxy(Handle<Object> handler,
|
|
|
|
Handle<Object> call_trap,
|
|
|
|
Handle<Object> construct_trap,
|
|
|
|
Handle<Object> prototype) {
|
2014-04-16 13:35:36 +00:00
|
|
|
// Allocate map.
|
|
|
|
// TODO(rossberg): Once we optimize proxies, think about a scheme to share
|
|
|
|
// maps. Will probably depend on the identity of the handler object, too.
|
|
|
|
Handle<Map> map = NewMap(JS_FUNCTION_PROXY_TYPE, JSFunctionProxy::kSize);
|
|
|
|
map->set_prototype(*prototype);
|
|
|
|
|
|
|
|
// Allocate the proxy object.
|
|
|
|
Handle<JSFunctionProxy> result = New<JSFunctionProxy>(map, NEW_SPACE);
|
|
|
|
result->InitializeBody(map->instance_size(), Smi::FromInt(0));
|
|
|
|
result->set_handler(*handler);
|
|
|
|
result->set_hash(*undefined_value(), SKIP_WRITE_BARRIER);
|
|
|
|
result->set_call_trap(*call_trap);
|
|
|
|
result->set_construct_trap(*construct_trap);
|
|
|
|
return result;
|
2014-04-15 10:59:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-15 11:51:34 +00:00
|
|
|
void Factory::ReinitializeJSReceiver(Handle<JSReceiver> object,
|
|
|
|
InstanceType type,
|
|
|
|
int size) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(type >= FIRST_JS_OBJECT_TYPE);
|
2014-04-15 11:51:34 +00:00
|
|
|
|
|
|
|
// Allocate fresh map.
|
|
|
|
// TODO(rossberg): Once we optimize proxies, cache these maps.
|
|
|
|
Handle<Map> map = NewMap(type, size);
|
|
|
|
|
|
|
|
// Check that the receiver has at least the size of the fresh object.
|
|
|
|
int size_difference = object->map()->instance_size() - map->instance_size();
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(size_difference >= 0);
|
2014-04-15 11:51:34 +00:00
|
|
|
|
|
|
|
map->set_prototype(object->map()->prototype());
|
|
|
|
|
|
|
|
// Allocate the backing storage for the properties.
|
2014-04-28 08:38:51 +00:00
|
|
|
int prop_size = map->InitialPropertiesLength();
|
2014-04-15 11:51:34 +00:00
|
|
|
Handle<FixedArray> properties = NewFixedArray(prop_size, TENURED);
|
|
|
|
|
|
|
|
Heap* heap = isolate()->heap();
|
|
|
|
MaybeHandle<SharedFunctionInfo> shared;
|
|
|
|
if (type == JS_FUNCTION_TYPE) {
|
|
|
|
OneByteStringKey key(STATIC_ASCII_VECTOR("<freezing call trap>"),
|
|
|
|
heap->HashSeed());
|
|
|
|
Handle<String> name = InternalizeStringWithKey(&key);
|
2014-05-09 17:39:54 +00:00
|
|
|
shared = NewSharedFunctionInfo(name, MaybeHandle<Code>());
|
2014-04-15 11:51:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// In order to keep heap in consistent state there must be no allocations
|
|
|
|
// before object re-initialization is finished and filler object is installed.
|
|
|
|
DisallowHeapAllocation no_allocation;
|
|
|
|
|
2014-06-24 14:53:48 +00:00
|
|
|
// Put in filler if the new object is smaller than the old.
|
|
|
|
if (size_difference > 0) {
|
2014-07-28 18:15:35 +00:00
|
|
|
Address address = object->address();
|
|
|
|
heap->CreateFillerObjectAt(address + map->instance_size(), size_difference);
|
2014-06-24 14:53:48 +00:00
|
|
|
heap->AdjustLiveBytes(address, -size_difference, Heap::FROM_MUTATOR);
|
|
|
|
}
|
|
|
|
|
2014-04-15 11:51:34 +00:00
|
|
|
// Reset the map for the object.
|
2014-06-24 14:53:48 +00:00
|
|
|
object->synchronized_set_map(*map);
|
2014-04-15 11:51:34 +00:00
|
|
|
Handle<JSObject> jsobj = Handle<JSObject>::cast(object);
|
|
|
|
|
|
|
|
// Reinitialize the object from the constructor map.
|
|
|
|
heap->InitializeJSObjectFromMap(*jsobj, *properties, *map);
|
|
|
|
|
|
|
|
// Functions require some minimal initialization.
|
|
|
|
if (type == JS_FUNCTION_TYPE) {
|
|
|
|
map->set_function_with_prototype(true);
|
|
|
|
Handle<JSFunction> js_function = Handle<JSFunction>::cast(object);
|
2014-07-01 12:12:34 +00:00
|
|
|
Handle<Context> context(isolate()->native_context());
|
2014-05-06 11:26:35 +00:00
|
|
|
InitializeFunction(js_function, shared.ToHandleChecked(), context);
|
2014-04-15 11:51:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Factory::ReinitializeJSGlobalProxy(Handle<JSGlobalProxy> object,
|
|
|
|
Handle<JSFunction> constructor) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(constructor->has_initial_map());
|
2014-04-15 11:51:34 +00:00
|
|
|
Handle<Map> map(constructor->initial_map(), isolate());
|
|
|
|
|
2014-05-05 18:27:57 +00:00
|
|
|
// The proxy's hash should be retained across reinitialization.
|
|
|
|
Handle<Object> hash(object->hash(), isolate());
|
|
|
|
|
2014-04-15 11:51:34 +00:00
|
|
|
// Check that the already allocated object has the same size and type as
|
|
|
|
// objects allocated using the constructor.
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(map->instance_size() == object->map()->instance_size());
|
|
|
|
DCHECK(map->instance_type() == object->map()->instance_type());
|
2014-04-15 11:51:34 +00:00
|
|
|
|
|
|
|
// Allocate the backing storage for the properties.
|
2014-04-28 08:38:51 +00:00
|
|
|
int prop_size = map->InitialPropertiesLength();
|
2014-04-15 11:51:34 +00:00
|
|
|
Handle<FixedArray> properties = NewFixedArray(prop_size, TENURED);
|
|
|
|
|
|
|
|
// In order to keep heap in consistent state there must be no allocations
|
|
|
|
// before object re-initialization is finished.
|
|
|
|
DisallowHeapAllocation no_allocation;
|
|
|
|
|
|
|
|
// Reset the map for the object.
|
2014-06-24 14:53:48 +00:00
|
|
|
object->synchronized_set_map(*map);
|
2014-04-15 11:51:34 +00:00
|
|
|
|
|
|
|
Heap* heap = isolate()->heap();
|
|
|
|
// Reinitialize the object from the constructor map.
|
|
|
|
heap->InitializeJSObjectFromMap(*object, *properties, *map);
|
2014-05-05 18:27:57 +00:00
|
|
|
|
|
|
|
// Restore the saved hash.
|
|
|
|
object->set_hash(*hash);
|
2014-04-15 11:51:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-13 11:42:57 +00:00
|
|
|
void Factory::BecomeJSObject(Handle<JSReceiver> object) {
|
2014-04-15 11:51:34 +00:00
|
|
|
ReinitializeJSReceiver(object, JS_OBJECT_TYPE, JSObject::kHeaderSize);
|
2011-09-13 11:42:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Factory::BecomeJSFunction(Handle<JSReceiver> object) {
|
2014-04-15 11:51:34 +00:00
|
|
|
ReinitializeJSReceiver(object, JS_FUNCTION_TYPE, JSFunction::kSize);
|
2011-07-18 13:04:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-30 10:51:01 +00:00
|
|
|
Handle<FixedArray> Factory::NewTypeFeedbackVector(int slot_count) {
|
|
|
|
// Ensure we can skip the write barrier
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK_EQ(isolate()->heap()->uninitialized_symbol(),
|
2014-04-30 10:51:01 +00:00
|
|
|
*TypeFeedbackInfo::UninitializedSentinel(isolate()));
|
|
|
|
|
|
|
|
CALL_HEAP_FUNCTION(
|
|
|
|
isolate(),
|
|
|
|
isolate()->heap()->AllocateFixedArrayWithFiller(
|
|
|
|
slot_count,
|
|
|
|
TENURED,
|
|
|
|
*TypeFeedbackInfo::UninitializedSentinel(isolate())),
|
|
|
|
FixedArray);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-23 06:04:44 +00:00
|
|
|
Handle<SharedFunctionInfo> Factory::NewSharedFunctionInfo(
|
2014-07-21 09:58:01 +00:00
|
|
|
Handle<String> name, int number_of_literals, bool is_generator,
|
|
|
|
bool is_arrow, Handle<Code> code, Handle<ScopeInfo> scope_info,
|
2014-04-30 10:51:01 +00:00
|
|
|
Handle<FixedArray> feedback_vector) {
|
2014-05-09 17:39:54 +00:00
|
|
|
Handle<SharedFunctionInfo> shared = NewSharedFunctionInfo(name, code);
|
2010-07-13 13:06:33 +00:00
|
|
|
shared->set_scope_info(*scope_info);
|
2014-04-30 10:51:01 +00:00
|
|
|
shared->set_feedback_vector(*feedback_vector);
|
2014-07-21 09:58:01 +00:00
|
|
|
shared->set_is_arrow(is_arrow);
|
2010-03-23 06:04:44 +00:00
|
|
|
int literals_array_size = number_of_literals;
|
|
|
|
// If the function contains object, regexp or array literals,
|
|
|
|
// allocate extra space for a literals array prefix containing the
|
|
|
|
// context.
|
|
|
|
if (number_of_literals > 0) {
|
|
|
|
literals_array_size += JSFunction::kLiteralsPrefixSize;
|
|
|
|
}
|
|
|
|
shared->set_num_literals(literals_array_size);
|
2013-04-17 15:01:25 +00:00
|
|
|
if (is_generator) {
|
|
|
|
shared->set_instance_class_name(isolate()->heap()->Generator_string());
|
2013-08-07 09:33:09 +00:00
|
|
|
shared->DisableOptimization(kGenerator);
|
2013-04-17 15:01:25 +00:00
|
|
|
}
|
2010-03-23 06:04:44 +00:00
|
|
|
return shared;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-02-02 13:31:52 +00:00
|
|
|
Handle<JSMessageObject> Factory::NewJSMessageObject(
|
|
|
|
Handle<String> type,
|
|
|
|
Handle<JSArray> arguments,
|
|
|
|
int start_position,
|
|
|
|
int end_position,
|
|
|
|
Handle<Object> script,
|
|
|
|
Handle<Object> stack_frames) {
|
2014-04-16 13:35:36 +00:00
|
|
|
Handle<Map> map = message_object_map();
|
|
|
|
Handle<JSMessageObject> message = New<JSMessageObject>(map, NEW_SPACE);
|
|
|
|
message->set_properties(*empty_fixed_array(), SKIP_WRITE_BARRIER);
|
|
|
|
message->initialize_elements();
|
|
|
|
message->set_elements(*empty_fixed_array(), SKIP_WRITE_BARRIER);
|
|
|
|
message->set_type(*type);
|
|
|
|
message->set_arguments(*arguments);
|
|
|
|
message->set_start_position(start_position);
|
|
|
|
message->set_end_position(end_position);
|
|
|
|
message->set_script(*script);
|
|
|
|
message->set_stack_frames(*stack_frames);
|
|
|
|
return message;
|
2011-02-02 13:31:52 +00:00
|
|
|
}
|
|
|
|
|
2013-07-05 09:52:11 +00:00
|
|
|
|
2014-05-09 17:39:54 +00:00
|
|
|
Handle<SharedFunctionInfo> Factory::NewSharedFunctionInfo(
|
|
|
|
Handle<String> name,
|
|
|
|
MaybeHandle<Code> maybe_code) {
|
2014-04-16 13:35:36 +00:00
|
|
|
Handle<Map> map = shared_function_info_map();
|
|
|
|
Handle<SharedFunctionInfo> share = New<SharedFunctionInfo>(map,
|
|
|
|
OLD_POINTER_SPACE);
|
|
|
|
|
|
|
|
// Set pointer fields.
|
|
|
|
share->set_name(*name);
|
2014-05-09 17:39:54 +00:00
|
|
|
Handle<Code> code;
|
|
|
|
if (!maybe_code.ToHandle(&code)) {
|
|
|
|
code = handle(isolate()->builtins()->builtin(Builtins::kIllegal));
|
|
|
|
}
|
|
|
|
share->set_code(*code);
|
2014-04-16 13:35:36 +00:00
|
|
|
share->set_optimized_code_map(Smi::FromInt(0));
|
|
|
|
share->set_scope_info(ScopeInfo::Empty(isolate()));
|
|
|
|
Code* construct_stub =
|
|
|
|
isolate()->builtins()->builtin(Builtins::kJSConstructStubGeneric);
|
|
|
|
share->set_construct_stub(construct_stub);
|
|
|
|
share->set_instance_class_name(*Object_string());
|
|
|
|
share->set_function_data(*undefined_value(), SKIP_WRITE_BARRIER);
|
|
|
|
share->set_script(*undefined_value(), SKIP_WRITE_BARRIER);
|
|
|
|
share->set_debug_info(*undefined_value(), SKIP_WRITE_BARRIER);
|
|
|
|
share->set_inferred_name(*empty_string(), SKIP_WRITE_BARRIER);
|
2014-04-30 10:51:01 +00:00
|
|
|
share->set_feedback_vector(*empty_fixed_array(), SKIP_WRITE_BARRIER);
|
2014-04-23 07:07:54 +00:00
|
|
|
share->set_profiler_ticks(0);
|
2014-04-16 13:35:36 +00:00
|
|
|
share->set_ast_node_count(0);
|
|
|
|
share->set_counters(0);
|
|
|
|
|
|
|
|
// Set integer fields (smi or int, depending on the architecture).
|
|
|
|
share->set_length(0);
|
|
|
|
share->set_formal_parameter_count(0);
|
|
|
|
share->set_expected_nof_properties(0);
|
|
|
|
share->set_num_literals(0);
|
|
|
|
share->set_start_position_and_type(0);
|
|
|
|
share->set_end_position(0);
|
|
|
|
share->set_function_token_position(0);
|
|
|
|
// All compiler hints default to false or 0.
|
|
|
|
share->set_compiler_hints(0);
|
|
|
|
share->set_opt_count_and_bailout_reason(0);
|
|
|
|
|
|
|
|
return share;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-16 14:04:54 +00:00
|
|
|
static inline int NumberCacheHash(Handle<FixedArray> cache,
|
|
|
|
Handle<Object> number) {
|
|
|
|
int mask = (cache->length() >> 1) - 1;
|
|
|
|
if (number->IsSmi()) {
|
|
|
|
return Handle<Smi>::cast(number)->value() & mask;
|
|
|
|
} else {
|
|
|
|
DoubleRepresentation rep(number->Number());
|
|
|
|
return
|
|
|
|
(static_cast<int>(rep.bits) ^ static_cast<int>(rep.bits >> 32)) & mask;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Handle<Object> Factory::GetNumberStringCache(Handle<Object> number) {
|
|
|
|
DisallowHeapAllocation no_gc;
|
|
|
|
int hash = NumberCacheHash(number_string_cache(), number);
|
|
|
|
Object* key = number_string_cache()->get(hash * 2);
|
|
|
|
if (key == *number || (key->IsHeapNumber() && number->IsHeapNumber() &&
|
|
|
|
key->Number() == number->Number())) {
|
|
|
|
return Handle<String>(
|
|
|
|
String::cast(number_string_cache()->get(hash * 2 + 1)), isolate());
|
|
|
|
}
|
|
|
|
return undefined_value();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Factory::SetNumberStringCache(Handle<Object> number,
|
|
|
|
Handle<String> string) {
|
|
|
|
int hash = NumberCacheHash(number_string_cache(), number);
|
|
|
|
if (number_string_cache()->get(hash * 2) != *undefined_value()) {
|
|
|
|
int full_size = isolate()->heap()->FullSizeNumberStringCacheLength();
|
|
|
|
if (number_string_cache()->length() != full_size) {
|
|
|
|
// The first time we have a hash collision, we move to the full sized
|
|
|
|
// number string cache. The idea is to have a small number string
|
|
|
|
// cache in the snapshot to keep boot-time memory usage down.
|
|
|
|
// If we expand the number string cache already while creating
|
|
|
|
// the snapshot then that didn't work out.
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(!isolate()->serializer_enabled() || FLAG_extra_code != NULL);
|
2014-04-16 14:04:54 +00:00
|
|
|
Handle<FixedArray> new_cache = NewFixedArray(full_size, TENURED);
|
|
|
|
isolate()->heap()->set_number_string_cache(*new_cache);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
number_string_cache()->set(hash * 2, *number);
|
|
|
|
number_string_cache()->set(hash * 2 + 1, *string);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-15 11:16:02 +00:00
|
|
|
Handle<String> Factory::NumberToString(Handle<Object> number,
|
|
|
|
bool check_number_string_cache) {
|
2014-04-16 14:04:54 +00:00
|
|
|
isolate()->counters()->number_to_string_runtime()->Increment();
|
|
|
|
if (check_number_string_cache) {
|
|
|
|
Handle<Object> cached = GetNumberStringCache(number);
|
|
|
|
if (!cached->IsUndefined()) return Handle<String>::cast(cached);
|
|
|
|
}
|
2009-10-02 13:43:16 +00:00
|
|
|
|
2014-04-16 14:04:54 +00:00
|
|
|
char arr[100];
|
|
|
|
Vector<char> buffer(arr, ARRAY_SIZE(arr));
|
|
|
|
const char* str;
|
|
|
|
if (number->IsSmi()) {
|
|
|
|
int num = Handle<Smi>::cast(number)->value();
|
|
|
|
str = IntToCString(num, buffer);
|
|
|
|
} else {
|
|
|
|
double num = Handle<HeapNumber>::cast(number)->value();
|
|
|
|
str = DoubleToCString(num, buffer);
|
|
|
|
}
|
2009-10-02 13:43:16 +00:00
|
|
|
|
2014-04-16 14:04:54 +00:00
|
|
|
// We tenure the allocated string since it is referenced from the
|
|
|
|
// number-string cache which lives in the old space.
|
2014-04-17 13:27:02 +00:00
|
|
|
Handle<String> js_string = NewStringFromAsciiChecked(str, TENURED);
|
2014-04-16 14:04:54 +00:00
|
|
|
SetNumberStringCache(number, js_string);
|
|
|
|
return js_string;
|
2011-09-22 10:45:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-01 10:15:45 +00:00
|
|
|
Handle<DebugInfo> Factory::NewDebugInfo(Handle<SharedFunctionInfo> shared) {
|
|
|
|
// Get the original code of the function.
|
|
|
|
Handle<Code> code(shared->code());
|
|
|
|
|
|
|
|
// Create a copy of the code before allocating the debug info object to avoid
|
|
|
|
// allocation while setting up the debug info object.
|
|
|
|
Handle<Code> original_code(*Factory::CopyCode(code));
|
|
|
|
|
|
|
|
// Allocate initial fixed array for active break points before allocating the
|
|
|
|
// debug info object to avoid allocation while setting up the debug info
|
|
|
|
// object.
|
|
|
|
Handle<FixedArray> break_points(
|
2014-06-02 12:51:19 +00:00
|
|
|
NewFixedArray(DebugInfo::kEstimatedNofBreakPointsInFunction));
|
2008-09-01 10:15:45 +00:00
|
|
|
|
|
|
|
// Create and set up the debug info object. Debug info contains function, a
|
|
|
|
// copy of the original code, the executing code and initial fixed array for
|
|
|
|
// active break points.
|
|
|
|
Handle<DebugInfo> debug_info =
|
2011-03-18 20:35:07 +00:00
|
|
|
Handle<DebugInfo>::cast(NewStruct(DEBUG_INFO_TYPE));
|
2008-09-01 10:15:45 +00:00
|
|
|
debug_info->set_shared(*shared);
|
|
|
|
debug_info->set_original_code(*original_code);
|
|
|
|
debug_info->set_code(*code);
|
|
|
|
debug_info->set_break_points(*break_points);
|
|
|
|
|
|
|
|
// Link debug info to function.
|
|
|
|
shared->set_debug_info(*debug_info);
|
|
|
|
|
|
|
|
return debug_info;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-07-07 13:27:37 +00:00
|
|
|
Handle<JSObject> Factory::NewArgumentsObject(Handle<JSFunction> callee,
|
2008-07-03 15:10:15 +00:00
|
|
|
int length) {
|
2014-07-07 13:27:37 +00:00
|
|
|
bool strict_mode_callee = callee->shared()->strict_mode() == STRICT;
|
|
|
|
Handle<Map> map = strict_mode_callee ? isolate()->strict_arguments_map()
|
|
|
|
: isolate()->sloppy_arguments_map();
|
|
|
|
|
|
|
|
AllocationSiteUsageContext context(isolate(), Handle<AllocationSite>(),
|
|
|
|
false);
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(!isolate()->has_pending_exception());
|
2014-07-07 13:27:37 +00:00
|
|
|
Handle<JSObject> result = NewJSObjectFromMap(map);
|
|
|
|
Handle<Smi> value(Smi::FromInt(length), isolate());
|
2014-07-22 08:28:49 +00:00
|
|
|
Object::SetProperty(result, length_string(), value, STRICT).Assert();
|
2014-07-07 13:27:37 +00:00
|
|
|
if (!strict_mode_callee) {
|
2014-07-22 08:28:49 +00:00
|
|
|
Object::SetProperty(result, callee_string(), callee, STRICT).Assert();
|
2014-07-07 13:27:37 +00:00
|
|
|
}
|
|
|
|
return result;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Handle<JSFunction> Factory::CreateApiFunction(
|
2014-04-29 17:48:07 +00:00
|
|
|
Handle<FunctionTemplateInfo> obj,
|
|
|
|
Handle<Object> prototype,
|
|
|
|
ApiInstanceType instance_type) {
|
2011-03-23 13:40:07 +00:00
|
|
|
Handle<Code> code = isolate()->builtins()->HandleApiCall();
|
|
|
|
Handle<Code> construct_stub = isolate()->builtins()->JSConstructStubApi();
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2014-05-09 16:59:03 +00:00
|
|
|
Handle<JSFunction> result;
|
|
|
|
if (obj->remove_prototype()) {
|
2014-05-09 17:39:54 +00:00
|
|
|
result = NewFunctionWithoutPrototype(empty_string(), code);
|
2014-05-09 16:59:03 +00:00
|
|
|
} else {
|
|
|
|
int internal_field_count = 0;
|
|
|
|
if (!obj->instance_template()->IsUndefined()) {
|
|
|
|
Handle<ObjectTemplateInfo> instance_template =
|
|
|
|
Handle<ObjectTemplateInfo>(
|
|
|
|
ObjectTemplateInfo::cast(obj->instance_template()));
|
|
|
|
internal_field_count =
|
|
|
|
Smi::cast(instance_template->internal_field_count())->value();
|
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2014-05-09 16:59:03 +00:00
|
|
|
// TODO(svenpanne) Kill ApiInstanceType and refactor things by generalizing
|
|
|
|
// JSObject::GetHeaderSize.
|
|
|
|
int instance_size = kPointerSize * internal_field_count;
|
|
|
|
InstanceType type;
|
|
|
|
switch (instance_type) {
|
2014-07-01 12:12:34 +00:00
|
|
|
case JavaScriptObjectType:
|
2014-05-09 16:59:03 +00:00
|
|
|
type = JS_OBJECT_TYPE;
|
|
|
|
instance_size += JSObject::kHeaderSize;
|
|
|
|
break;
|
2014-07-01 12:12:34 +00:00
|
|
|
case GlobalObjectType:
|
2014-05-09 16:59:03 +00:00
|
|
|
type = JS_GLOBAL_OBJECT_TYPE;
|
|
|
|
instance_size += JSGlobalObject::kSize;
|
|
|
|
break;
|
2014-07-01 12:12:34 +00:00
|
|
|
case GlobalProxyType:
|
2014-05-09 16:59:03 +00:00
|
|
|
type = JS_GLOBAL_PROXY_TYPE;
|
|
|
|
instance_size += JSGlobalProxy::kSize;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
type = JS_OBJECT_TYPE; // Keep the compiler happy.
|
|
|
|
break;
|
|
|
|
}
|
2014-04-29 17:48:07 +00:00
|
|
|
|
2014-05-09 17:59:15 +00:00
|
|
|
result = NewFunction(empty_string(), code, prototype, type,
|
|
|
|
instance_size, obj->read_only_prototype());
|
2014-05-09 16:59:03 +00:00
|
|
|
}
|
2012-12-19 10:28:36 +00:00
|
|
|
|
|
|
|
result->shared()->set_length(obj->length());
|
2014-04-29 17:48:07 +00:00
|
|
|
Handle<Object> class_name(obj->class_name(), isolate());
|
2008-07-03 15:10:15 +00:00
|
|
|
if (class_name->IsString()) {
|
|
|
|
result->shared()->set_instance_class_name(*class_name);
|
|
|
|
result->shared()->set_name(*class_name);
|
|
|
|
}
|
2014-04-29 17:48:07 +00:00
|
|
|
result->shared()->set_function_data(*obj);
|
|
|
|
result->shared()->set_construct_stub(*construct_stub);
|
|
|
|
result->shared()->DontAdaptArguments();
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2014-04-29 17:48:07 +00:00
|
|
|
if (obj->remove_prototype()) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(result->shared()->IsApiFunction());
|
|
|
|
DCHECK(!result->has_initial_map());
|
|
|
|
DCHECK(!result->has_prototype());
|
2014-04-29 17:48:07 +00:00
|
|
|
return result;
|
|
|
|
}
|
2014-05-09 16:34:58 +00:00
|
|
|
|
2014-06-30 13:48:57 +00:00
|
|
|
if (prototype->IsTheHole()) {
|
|
|
|
#ifdef DEBUG
|
|
|
|
LookupIterator it(handle(JSObject::cast(result->prototype())),
|
2014-08-18 14:59:04 +00:00
|
|
|
constructor_string(), LookupIterator::CHECK_PROPERTY);
|
2014-06-30 13:48:57 +00:00
|
|
|
MaybeHandle<Object> maybe_prop = Object::GetProperty(&it);
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(it.IsFound());
|
|
|
|
DCHECK(maybe_prop.ToHandleChecked().is_identical_to(result));
|
2014-06-30 13:48:57 +00:00
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
JSObject::AddProperty(handle(JSObject::cast(result->prototype())),
|
|
|
|
constructor_string(), result, DONT_ENUM);
|
|
|
|
}
|
2014-05-09 16:34:58 +00:00
|
|
|
|
2014-04-29 17:48:07 +00:00
|
|
|
// Down from here is only valid for API functions that can be used as a
|
|
|
|
// constructor (don't set the "remove prototype" flag).
|
|
|
|
|
|
|
|
Handle<Map> map(result->initial_map());
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
// Mark as undetectable if needed.
|
|
|
|
if (obj->undetectable()) {
|
|
|
|
map->set_is_undetectable();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mark as hidden for the __proto__ accessor if needed.
|
|
|
|
if (obj->hidden_prototype()) {
|
|
|
|
map->set_is_hidden_prototype();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mark as needs_access_check if needed.
|
|
|
|
if (obj->needs_access_check()) {
|
2008-10-30 12:51:06 +00:00
|
|
|
map->set_is_access_check_needed(true);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set interceptor information in the map.
|
|
|
|
if (!obj->named_property_handler()->IsUndefined()) {
|
|
|
|
map->set_has_named_interceptor();
|
|
|
|
}
|
|
|
|
if (!obj->indexed_property_handler()->IsUndefined()) {
|
|
|
|
map->set_has_indexed_interceptor();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set instance call-as-function information in the map.
|
|
|
|
if (!obj->instance_call_handler()->IsUndefined()) {
|
|
|
|
map->set_has_instance_call_handler();
|
|
|
|
}
|
|
|
|
|
2013-09-04 07:45:36 +00:00
|
|
|
// Recursively copy parent instance templates' accessors,
|
|
|
|
// 'data' may be modified.
|
2012-09-18 13:25:12 +00:00
|
|
|
int max_number_of_additional_properties = 0;
|
2013-09-04 07:45:36 +00:00
|
|
|
int max_number_of_static_properties = 0;
|
2012-09-18 13:25:12 +00:00
|
|
|
FunctionTemplateInfo* info = *obj;
|
|
|
|
while (true) {
|
2013-09-04 07:45:36 +00:00
|
|
|
if (!info->instance_template()->IsUndefined()) {
|
|
|
|
Object* props =
|
|
|
|
ObjectTemplateInfo::cast(
|
|
|
|
info->instance_template())->property_accessors();
|
|
|
|
if (!props->IsUndefined()) {
|
|
|
|
Handle<Object> props_handle(props, isolate());
|
|
|
|
NeanderArray props_array(props_handle);
|
|
|
|
max_number_of_additional_properties += props_array.length();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!info->property_accessors()->IsUndefined()) {
|
|
|
|
Object* props = info->property_accessors();
|
|
|
|
if (!props->IsUndefined()) {
|
|
|
|
Handle<Object> props_handle(props, isolate());
|
|
|
|
NeanderArray props_array(props_handle);
|
|
|
|
max_number_of_static_properties += props_array.length();
|
|
|
|
}
|
2012-09-18 13:25:12 +00:00
|
|
|
}
|
|
|
|
Object* parent = info->parent_template();
|
|
|
|
if (parent->IsUndefined()) break;
|
|
|
|
info = FunctionTemplateInfo::cast(parent);
|
|
|
|
}
|
|
|
|
|
|
|
|
Map::EnsureDescriptorSlack(map, max_number_of_additional_properties);
|
|
|
|
|
2013-09-04 07:45:36 +00:00
|
|
|
// Use a temporary FixedArray to acculumate static accessors
|
|
|
|
int valid_descriptors = 0;
|
|
|
|
Handle<FixedArray> array;
|
|
|
|
if (max_number_of_static_properties > 0) {
|
|
|
|
array = NewFixedArray(max_number_of_static_properties);
|
|
|
|
}
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
while (true) {
|
2013-09-04 07:45:36 +00:00
|
|
|
// Install instance descriptors
|
|
|
|
if (!obj->instance_template()->IsUndefined()) {
|
|
|
|
Handle<ObjectTemplateInfo> instance =
|
|
|
|
Handle<ObjectTemplateInfo>(
|
|
|
|
ObjectTemplateInfo::cast(obj->instance_template()), isolate());
|
|
|
|
Handle<Object> props = Handle<Object>(instance->property_accessors(),
|
|
|
|
isolate());
|
|
|
|
if (!props->IsUndefined()) {
|
|
|
|
Map::AppendCallbackDescriptors(map, props);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Accumulate static accessors
|
|
|
|
if (!obj->property_accessors()->IsUndefined()) {
|
|
|
|
Handle<Object> props = Handle<Object>(obj->property_accessors(),
|
|
|
|
isolate());
|
|
|
|
valid_descriptors =
|
|
|
|
AccessorInfo::AppendUnique(props, array, valid_descriptors);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
2013-09-04 07:45:36 +00:00
|
|
|
// Climb parent chain
|
2013-02-25 14:46:09 +00:00
|
|
|
Handle<Object> parent = Handle<Object>(obj->parent_template(), isolate());
|
2008-07-03 15:10:15 +00:00
|
|
|
if (parent->IsUndefined()) break;
|
|
|
|
obj = Handle<FunctionTemplateInfo>::cast(parent);
|
|
|
|
}
|
|
|
|
|
2013-09-04 07:45:36 +00:00
|
|
|
// Install accumulated static accessors
|
|
|
|
for (int i = 0; i < valid_descriptors; i++) {
|
|
|
|
Handle<AccessorInfo> accessor(AccessorInfo::cast(array->get(i)));
|
2014-04-17 08:33:18 +00:00
|
|
|
JSObject::SetAccessor(result, accessor).Assert();
|
2013-09-04 07:45:36 +00:00
|
|
|
}
|
|
|
|
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(result->shared()->IsApiFunction());
|
2008-07-03 15:10:15 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-25 07:46:07 +00:00
|
|
|
Handle<MapCache> Factory::AddToMapCache(Handle<Context> context,
|
|
|
|
Handle<FixedArray> keys,
|
|
|
|
Handle<Map> map) {
|
2014-04-16 18:17:20 +00:00
|
|
|
Handle<MapCache> map_cache = handle(MapCache::cast(context->map_cache()));
|
|
|
|
Handle<MapCache> result = MapCache::Put(map_cache, keys, map);
|
|
|
|
context->set_map_cache(*result);
|
|
|
|
return result;
|
2008-09-25 07:46:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Handle<Map> Factory::ObjectLiteralMapFromCache(Handle<Context> context,
|
|
|
|
Handle<FixedArray> keys) {
|
|
|
|
if (context->map_cache()->IsUndefined()) {
|
2012-08-17 09:03:08 +00:00
|
|
|
// Allocate the new map cache for the native context.
|
2014-04-25 13:06:21 +00:00
|
|
|
Handle<MapCache> new_cache = MapCache::New(isolate(), 24);
|
2008-09-25 07:46:07 +00:00
|
|
|
context->set_map_cache(*new_cache);
|
|
|
|
}
|
2009-01-15 19:08:34 +00:00
|
|
|
// Check to see whether there is a matching element in the cache.
|
2008-09-25 07:46:07 +00:00
|
|
|
Handle<MapCache> cache =
|
|
|
|
Handle<MapCache>(MapCache::cast(context->map_cache()));
|
2012-11-16 08:38:11 +00:00
|
|
|
Handle<Object> result = Handle<Object>(cache->Lookup(*keys), isolate());
|
2008-09-25 07:46:07 +00:00
|
|
|
if (result->IsMap()) return Handle<Map>::cast(result);
|
|
|
|
// Create a new map and add it to the cache.
|
2014-04-02 13:30:36 +00:00
|
|
|
Handle<Map> map = Map::Create(
|
|
|
|
handle(context->object_function()), keys->length());
|
2008-09-25 07:46:07 +00:00
|
|
|
AddToMapCache(context, keys, map);
|
2014-04-02 13:30:36 +00:00
|
|
|
return map;
|
2008-09-25 07:46:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-11 14:00:55 +00:00
|
|
|
void Factory::SetRegExpAtomData(Handle<JSRegExp> regexp,
|
|
|
|
JSRegExp::Type type,
|
|
|
|
Handle<String> source,
|
|
|
|
JSRegExp::Flags flags,
|
|
|
|
Handle<Object> data) {
|
|
|
|
Handle<FixedArray> store = NewFixedArray(JSRegExp::kAtomDataSize);
|
|
|
|
|
2008-10-24 08:40:02 +00:00
|
|
|
store->set(JSRegExp::kTagIndex, Smi::FromInt(type));
|
|
|
|
store->set(JSRegExp::kSourceIndex, *source);
|
|
|
|
store->set(JSRegExp::kFlagsIndex, Smi::FromInt(flags.value()));
|
|
|
|
store->set(JSRegExp::kAtomPatternIndex, *data);
|
|
|
|
regexp->set_data(*store);
|
|
|
|
}
|
|
|
|
|
2009-03-11 14:00:55 +00:00
|
|
|
void Factory::SetRegExpIrregexpData(Handle<JSRegExp> regexp,
|
|
|
|
JSRegExp::Type type,
|
|
|
|
Handle<String> source,
|
|
|
|
JSRegExp::Flags flags,
|
|
|
|
int capture_count) {
|
|
|
|
Handle<FixedArray> store = NewFixedArray(JSRegExp::kIrregexpDataSize);
|
2011-07-05 06:19:53 +00:00
|
|
|
Smi* uninitialized = Smi::FromInt(JSRegExp::kUninitializedValue);
|
2009-03-11 14:00:55 +00:00
|
|
|
store->set(JSRegExp::kTagIndex, Smi::FromInt(type));
|
|
|
|
store->set(JSRegExp::kSourceIndex, *source);
|
|
|
|
store->set(JSRegExp::kFlagsIndex, Smi::FromInt(flags.value()));
|
2011-07-05 06:19:53 +00:00
|
|
|
store->set(JSRegExp::kIrregexpASCIICodeIndex, uninitialized);
|
|
|
|
store->set(JSRegExp::kIrregexpUC16CodeIndex, uninitialized);
|
|
|
|
store->set(JSRegExp::kIrregexpASCIICodeSavedIndex, uninitialized);
|
|
|
|
store->set(JSRegExp::kIrregexpUC16CodeSavedIndex, uninitialized);
|
2009-03-11 14:00:55 +00:00
|
|
|
store->set(JSRegExp::kIrregexpMaxRegisterCountIndex, Smi::FromInt(0));
|
|
|
|
store->set(JSRegExp::kIrregexpCaptureCountIndex,
|
|
|
|
Smi::FromInt(capture_count));
|
|
|
|
regexp->set_data(*store);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-24 08:40:02 +00:00
|
|
|
|
2014-04-11 10:41:09 +00:00
|
|
|
MaybeHandle<FunctionTemplateInfo> Factory::ConfigureInstance(
|
|
|
|
Handle<FunctionTemplateInfo> desc, Handle<JSObject> instance) {
|
2008-07-03 15:10:15 +00:00
|
|
|
// Configure the instance by adding the properties specified by the
|
|
|
|
// instance template.
|
2012-11-16 08:38:11 +00:00
|
|
|
Handle<Object> instance_template(desc->instance_template(), isolate());
|
2008-07-03 15:10:15 +00:00
|
|
|
if (!instance_template->IsUndefined()) {
|
2014-04-11 10:41:09 +00:00
|
|
|
RETURN_ON_EXCEPTION(
|
|
|
|
isolate(),
|
|
|
|
Execution::ConfigureInstance(isolate(), instance, instance_template),
|
|
|
|
FunctionTemplateInfo);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
2014-04-11 10:41:09 +00:00
|
|
|
return desc;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-23 08:00:06 +00:00
|
|
|
Handle<Object> Factory::GlobalConstantFor(Handle<String> name) {
|
2014-04-11 07:27:25 +00:00
|
|
|
if (String::Equals(name, undefined_string())) return undefined_value();
|
|
|
|
if (String::Equals(name, nan_string())) return nan_value();
|
|
|
|
if (String::Equals(name, infinity_string())) return infinity_value();
|
2011-09-23 08:00:06 +00:00
|
|
|
return Handle<Object>::null();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-10-10 09:21:48 +00:00
|
|
|
Handle<Object> Factory::ToBoolean(bool value) {
|
2013-02-25 14:46:09 +00:00
|
|
|
return value ? true_value() : false_value();
|
2011-10-10 09:21:48 +00:00
|
|
|
}
|
|
|
|
|
2014-04-17 17:45:32 +00:00
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
} } // namespace v8::internal
|