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.
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
#include "factory.h"
|
2014-03-31 12:01:02 +00:00
|
|
|
|
2013-07-02 15:30:33 +00:00
|
|
|
#include "isolate-inl.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
|
|
|
|
2013-06-06 15:40:28 +00:00
|
|
|
Handle<Box> Factory::NewBox(Handle<Object> value, PretenureFlag pretenure) {
|
|
|
|
CALL_HEAP_FUNCTION(
|
|
|
|
isolate(),
|
|
|
|
isolate()->heap()->AllocateBox(*value, pretenure),
|
|
|
|
Box);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
Handle<FixedArray> Factory::NewFixedArray(int size, PretenureFlag pretenure) {
|
|
|
|
ASSERT(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) {
|
2008-10-29 10:02:09 +00:00
|
|
|
ASSERT(0 <= size);
|
2011-03-18 20:35:07 +00:00
|
|
|
CALL_HEAP_FUNCTION(
|
|
|
|
isolate(),
|
|
|
|
isolate()->heap()->AllocateFixedArrayWithHoles(size, pretenure),
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-10-19 11:36:55 +00:00
|
|
|
Handle<FixedDoubleArray> Factory::NewFixedDoubleArray(int size,
|
|
|
|
PretenureFlag pretenure) {
|
2011-06-09 10:03:35 +00:00
|
|
|
ASSERT(0 <= size);
|
|
|
|
CALL_HEAP_FUNCTION(
|
|
|
|
isolate(),
|
|
|
|
isolate()->heap()->AllocateUninitializedFixedDoubleArray(size, pretenure),
|
2011-10-19 11:36:55 +00:00
|
|
|
FixedDoubleArray);
|
2011-06-09 10:03:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-10-14 13:35:06 +00:00
|
|
|
Handle<ConstantPoolArray> Factory::NewConstantPoolArray(
|
|
|
|
int number_of_int64_entries,
|
2014-03-10 19:05:43 +00:00
|
|
|
int number_of_code_ptr_entries,
|
|
|
|
int number_of_heap_ptr_entries,
|
2013-10-14 13:35:06 +00:00
|
|
|
int number_of_int32_entries) {
|
2014-03-10 19:05:43 +00:00
|
|
|
ASSERT(number_of_int64_entries > 0 || number_of_code_ptr_entries > 0 ||
|
|
|
|
number_of_heap_ptr_entries > 0 || number_of_int32_entries > 0);
|
2013-10-14 13:35:06 +00:00
|
|
|
CALL_HEAP_FUNCTION(
|
|
|
|
isolate(),
|
|
|
|
isolate()->heap()->AllocateConstantPoolArray(number_of_int64_entries,
|
2014-03-10 19:05:43 +00:00
|
|
|
number_of_code_ptr_entries,
|
|
|
|
number_of_heap_ptr_entries,
|
2013-10-14 13:35:06 +00:00
|
|
|
number_of_int32_entries),
|
|
|
|
ConstantPoolArray);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-03-04 15:00:57 +00:00
|
|
|
Handle<NameDictionary> Factory::NewNameDictionary(int at_least_space_for) {
|
2008-10-28 14:47:50 +00:00
|
|
|
ASSERT(0 <= at_least_space_for);
|
2011-03-18 20:35:07 +00:00
|
|
|
CALL_HEAP_FUNCTION(isolate(),
|
2013-03-12 07:06:36 +00:00
|
|
|
NameDictionary::Allocate(isolate()->heap(),
|
|
|
|
at_least_space_for),
|
2013-03-04 15:00:57 +00:00
|
|
|
NameDictionary);
|
2009-07-02 06:50:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-01-16 09:44:35 +00:00
|
|
|
Handle<SeededNumberDictionary> Factory::NewSeededNumberDictionary(
|
|
|
|
int at_least_space_for) {
|
2009-07-02 06:50:43 +00:00
|
|
|
ASSERT(0 <= at_least_space_for);
|
2011-03-18 20:35:07 +00:00
|
|
|
CALL_HEAP_FUNCTION(isolate(),
|
2013-03-12 07:06:36 +00:00
|
|
|
SeededNumberDictionary::Allocate(isolate()->heap(),
|
|
|
|
at_least_space_for),
|
2012-01-16 09:44:35 +00:00
|
|
|
SeededNumberDictionary);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Handle<UnseededNumberDictionary> Factory::NewUnseededNumberDictionary(
|
|
|
|
int at_least_space_for) {
|
|
|
|
ASSERT(0 <= at_least_space_for);
|
|
|
|
CALL_HEAP_FUNCTION(isolate(),
|
2013-03-12 07:06:36 +00:00
|
|
|
UnseededNumberDictionary::Allocate(isolate()->heap(),
|
|
|
|
at_least_space_for),
|
2012-01-16 09:44:35 +00:00
|
|
|
UnseededNumberDictionary);
|
2008-10-28 14:47:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-10-25 14:14:56 +00:00
|
|
|
Handle<ObjectHashSet> Factory::NewObjectHashSet(int at_least_space_for) {
|
|
|
|
ASSERT(0 <= at_least_space_for);
|
|
|
|
CALL_HEAP_FUNCTION(isolate(),
|
2013-03-12 07:06:36 +00:00
|
|
|
ObjectHashSet::Allocate(isolate()->heap(),
|
|
|
|
at_least_space_for),
|
2011-10-25 14:14:56 +00:00
|
|
|
ObjectHashSet);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-11-05 11:47:11 +00:00
|
|
|
Handle<ObjectHashTable> Factory::NewObjectHashTable(
|
|
|
|
int at_least_space_for,
|
|
|
|
MinimumCapacity capacity_option) {
|
2011-07-28 17:21:22 +00:00
|
|
|
ASSERT(0 <= at_least_space_for);
|
|
|
|
CALL_HEAP_FUNCTION(isolate(),
|
2013-03-12 07:06:36 +00:00
|
|
|
ObjectHashTable::Allocate(isolate()->heap(),
|
2013-11-05 11:47:11 +00:00
|
|
|
at_least_space_for,
|
|
|
|
capacity_option),
|
2011-07-28 17:21:22 +00:00
|
|
|
ObjectHashTable);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-10-04 07:25:24 +00:00
|
|
|
Handle<WeakHashTable> Factory::NewWeakHashTable(int at_least_space_for) {
|
|
|
|
ASSERT(0 <= at_least_space_for);
|
|
|
|
CALL_HEAP_FUNCTION(
|
|
|
|
isolate(),
|
|
|
|
WeakHashTable::Allocate(isolate()->heap(),
|
|
|
|
at_least_space_for,
|
2013-11-05 11:47:11 +00:00
|
|
|
USE_DEFAULT_MINIMUM_CAPACITY,
|
2013-10-04 07:25:24 +00:00
|
|
|
TENURED),
|
|
|
|
WeakHashTable);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-09-18 13:25:12 +00:00
|
|
|
Handle<DescriptorArray> Factory::NewDescriptorArray(int number_of_descriptors,
|
|
|
|
int slack) {
|
2008-07-03 15:10:15 +00:00
|
|
|
ASSERT(0 <= number_of_descriptors);
|
2011-03-18 20:35:07 +00:00
|
|
|
CALL_HEAP_FUNCTION(isolate(),
|
2013-09-04 10:34:42 +00:00
|
|
|
DescriptorArray::Allocate(
|
|
|
|
isolate(), number_of_descriptors, slack),
|
2008-07-03 15:10:15 +00:00
|
|
|
DescriptorArray);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-12-07 11:31:57 +00:00
|
|
|
Handle<DeoptimizationInputData> Factory::NewDeoptimizationInputData(
|
|
|
|
int deopt_entry_count,
|
|
|
|
PretenureFlag pretenure) {
|
|
|
|
ASSERT(deopt_entry_count > 0);
|
2011-03-18 20:35:07 +00:00
|
|
|
CALL_HEAP_FUNCTION(isolate(),
|
2013-09-11 07:14:41 +00:00
|
|
|
DeoptimizationInputData::Allocate(isolate(),
|
|
|
|
deopt_entry_count,
|
2010-12-07 11:31:57 +00:00
|
|
|
pretenure),
|
|
|
|
DeoptimizationInputData);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Handle<DeoptimizationOutputData> Factory::NewDeoptimizationOutputData(
|
|
|
|
int deopt_entry_count,
|
|
|
|
PretenureFlag pretenure) {
|
|
|
|
ASSERT(deopt_entry_count > 0);
|
2011-03-18 20:35:07 +00:00
|
|
|
CALL_HEAP_FUNCTION(isolate(),
|
2013-09-11 07:14:41 +00:00
|
|
|
DeoptimizationOutputData::Allocate(isolate(),
|
|
|
|
deopt_entry_count,
|
2010-12-07 11:31:57 +00:00
|
|
|
pretenure),
|
|
|
|
DeoptimizationOutputData);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
accessors->set_access_flags(Smi::FromInt(0), 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();
|
|
|
|
info->set_feedback_vector(*empty_fixed_array(), SKIP_WRITE_BARRIER);
|
|
|
|
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) {
|
2011-05-24 12:16:23 +00:00
|
|
|
CALL_HEAP_FUNCTION(isolate(),
|
2013-02-28 17:03:34 +00:00
|
|
|
isolate()->heap()->InternalizeString(*string),
|
2011-05-24 12:16:23 +00:00
|
|
|
String);
|
|
|
|
}
|
|
|
|
|
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) {
|
2011-03-18 20:35:07 +00:00
|
|
|
CALL_HEAP_FUNCTION(isolate(),
|
2014-01-17 10:27:57 +00:00
|
|
|
isolate()->heap()->InternalizeStringWithKey(key),
|
2011-03-18 20:35:07 +00:00
|
|
|
String);
|
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);
|
|
|
|
|
|
|
|
|
2013-01-09 10:30:54 +00:00
|
|
|
Handle<String> Factory::NewStringFromOneByte(Vector<const uint8_t> string,
|
|
|
|
PretenureFlag pretenure) {
|
2011-03-18 20:35:07 +00:00
|
|
|
CALL_HEAP_FUNCTION(
|
|
|
|
isolate(),
|
2012-11-21 10:01:05 +00:00
|
|
|
isolate()->heap()->AllocateStringFromOneByte(string, pretenure),
|
2011-03-18 20:35:07 +00:00
|
|
|
String);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Handle<String> Factory::NewStringFromUtf8(Vector<const char> string,
|
2012-09-04 12:23:22 +00:00
|
|
|
PretenureFlag pretenure) {
|
2011-03-18 20:35:07 +00:00
|
|
|
CALL_HEAP_FUNCTION(
|
|
|
|
isolate(),
|
2012-09-04 12:23:22 +00:00
|
|
|
isolate()->heap()->AllocateStringFromUtf8(string, pretenure),
|
2011-03-18 20:35:07 +00:00
|
|
|
String);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-18 07:14:02 +00:00
|
|
|
Handle<String> Factory::NewStringFromTwoByte(Vector<const uc16> string,
|
|
|
|
PretenureFlag pretenure) {
|
2011-03-18 20:35:07 +00:00
|
|
|
CALL_HEAP_FUNCTION(
|
|
|
|
isolate(),
|
|
|
|
isolate()->heap()->AllocateStringFromTwoByte(string, pretenure),
|
|
|
|
String);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-03 12:30:08 +00:00
|
|
|
MaybeHandle<SeqOneByteString> Factory::NewRawOneByteString(
|
|
|
|
int length, PretenureFlag pretenure) {
|
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) {
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
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')) {
|
|
|
|
String* result;
|
|
|
|
StringTable* table = isolate->heap()->string_table();
|
|
|
|
if (table->LookupTwoCharsStringIfExists(c1, c2, &result)) {
|
|
|
|
return handle(result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
ASSERT(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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-12-23 12:37:56 +00:00
|
|
|
Handle<ConsString> Factory::NewRawConsString(String::Encoding encoding) {
|
|
|
|
Handle<Map> map = (encoding == String::ONE_BYTE_ENCODING)
|
|
|
|
? cons_ascii_string_map() : cons_string_map();
|
|
|
|
CALL_HEAP_FUNCTION(isolate(),
|
|
|
|
isolate()->heap()->Allocate(*map, NEW_SPACE),
|
|
|
|
ConsString);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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-03 12:30:37 +00:00
|
|
|
return isolate()->Throw<String>(
|
|
|
|
isolate()->factory()->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);
|
|
|
|
ASSERT(left->IsFlat());
|
|
|
|
ASSERT(right->IsFlat());
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
Handle<ConsString> result = NewRawConsString(
|
|
|
|
(is_one_byte || is_one_byte_data_in_two_byte_string)
|
|
|
|
? String::ONE_BYTE_ENCODING
|
|
|
|
: String::TWO_BYTE_ENCODING);
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-21 09:24:30 +00:00
|
|
|
Handle<String> Factory::NewFlatConcatString(Handle<String> first,
|
|
|
|
Handle<String> second) {
|
|
|
|
int total_length = first->length() + second->length();
|
2013-11-11 15:28:47 +00:00
|
|
|
if (first->IsOneByteRepresentation() && second->IsOneByteRepresentation()) {
|
2013-06-21 09:24:30 +00:00
|
|
|
return ConcatStringContent<uint8_t>(
|
2014-04-03 12:30:08 +00:00
|
|
|
NewRawOneByteString(total_length).ToHandleChecked(), first, second);
|
2013-06-21 09:24:30 +00:00
|
|
|
} else {
|
|
|
|
return ConcatStringContent<uc16>(
|
2014-04-03 12:30:08 +00:00
|
|
|
NewRawTwoByteString(total_length).ToHandleChecked(), first, second);
|
2013-06-21 09:24:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-12-23 12:37:56 +00:00
|
|
|
Handle<SlicedString> Factory::NewRawSlicedString(String::Encoding encoding) {
|
|
|
|
Handle<Map> map = (encoding == String::ONE_BYTE_ENCODING)
|
|
|
|
? sliced_ascii_string_map() : sliced_string_map();
|
2011-03-18 20:35:07 +00:00
|
|
|
CALL_HEAP_FUNCTION(isolate(),
|
2013-12-23 12:37:56 +00:00
|
|
|
isolate()->heap()->Allocate(*map, NEW_SPACE),
|
|
|
|
SlicedString);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
2011-06-22 14:20:23 +00:00
|
|
|
ASSERT(begin > 0 || end < str->length());
|
2013-12-23 12:37:56 +00:00
|
|
|
|
|
|
|
int length = end - begin;
|
|
|
|
if (length <= 0) return empty_string();
|
|
|
|
if (length == 1) {
|
|
|
|
return LookupSingleCharacterStringFromCode(isolate(), str->Get(begin));
|
|
|
|
}
|
|
|
|
if (length == 2) {
|
|
|
|
// Optimization for 2-byte strings often used as keys in a decompression
|
|
|
|
// dictionary. Check whether we already have the string in the string
|
|
|
|
// table to prevent creation of many unnecessary strings.
|
|
|
|
uint16_t c1 = str->Get(begin);
|
|
|
|
uint16_t c2 = str->Get(begin + 1);
|
|
|
|
return MakeOrFindTwoCharacterString(isolate(), c1, c2);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!FLAG_string_slices || length < SlicedString::kMinLength) {
|
|
|
|
if (str->IsOneByteRepresentation()) {
|
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;
|
|
|
|
|
|
|
|
while (str->IsConsString()) {
|
|
|
|
Handle<ConsString> cons = Handle<ConsString>::cast(str);
|
|
|
|
int split = cons->first()->length();
|
|
|
|
if (split <= offset) {
|
|
|
|
// Slice is fully contained in the second part.
|
|
|
|
str = Handle<String>(cons->second(), isolate());
|
|
|
|
offset -= split; // Adjust for offset.
|
|
|
|
continue;
|
|
|
|
} else if (offset + length <= split) {
|
|
|
|
// Slice is fully contained in the first part.
|
|
|
|
str = Handle<String>(cons->first(), isolate());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (str->IsSlicedString()) {
|
|
|
|
Handle<SlicedString> slice = Handle<SlicedString>::cast(str);
|
|
|
|
str = Handle<String>(slice->parent(), isolate());
|
|
|
|
offset += slice->offset();
|
|
|
|
} else {
|
|
|
|
str = FlattenGetString(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
ASSERT(str->IsSeqString() || str->IsExternalString());
|
|
|
|
Handle<SlicedString> slice = NewRawSlicedString(
|
|
|
|
str->IsOneByteRepresentation() ? String::ONE_BYTE_ENCODING
|
|
|
|
: String::TWO_BYTE_ENCODING);
|
|
|
|
|
|
|
|
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) {
|
2011-03-18 20:35:07 +00:00
|
|
|
CALL_HEAP_FUNCTION(
|
|
|
|
isolate(),
|
|
|
|
isolate()->heap()->AllocateExternalStringFromAscii(resource),
|
|
|
|
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) {
|
2011-03-18 20:35:07 +00:00
|
|
|
CALL_HEAP_FUNCTION(
|
|
|
|
isolate(),
|
|
|
|
isolate()->heap()->AllocateExternalStringFromTwoByte(resource),
|
|
|
|
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() {
|
|
|
|
CALL_HEAP_FUNCTION(
|
|
|
|
isolate(),
|
|
|
|
isolate()->heap()->AllocatePrivateSymbol(),
|
|
|
|
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());
|
|
|
|
ASSERT(context->IsNativeContext());
|
|
|
|
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) {
|
|
|
|
CALL_HEAP_FUNCTION(
|
|
|
|
isolate(),
|
|
|
|
isolate()->heap()->AllocateGlobalContext(*function, *scope_info),
|
|
|
|
Context);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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) {
|
2011-03-18 20:35:07 +00:00
|
|
|
CALL_HEAP_FUNCTION(
|
|
|
|
isolate(),
|
2011-06-29 07:41:42 +00:00
|
|
|
isolate()->heap()->AllocateFunctionContext(length, *function),
|
2011-03-18 20:35:07 +00:00
|
|
|
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) {
|
2011-06-09 11:26:01 +00:00
|
|
|
CALL_HEAP_FUNCTION(
|
|
|
|
isolate(),
|
2011-06-29 07:41:42 +00:00
|
|
|
isolate()->heap()->AllocateCatchContext(*function,
|
|
|
|
*previous,
|
|
|
|
*name,
|
|
|
|
*thrown_object),
|
2011-06-09 11:26:01 +00:00
|
|
|
Context);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-06-29 07:41:42 +00:00
|
|
|
Handle<Context> Factory::NewWithContext(Handle<JSFunction> function,
|
|
|
|
Handle<Context> previous,
|
2011-06-09 11:26:01 +00:00
|
|
|
Handle<JSObject> extension) {
|
2011-03-18 20:35:07 +00:00
|
|
|
CALL_HEAP_FUNCTION(
|
|
|
|
isolate(),
|
2011-06-29 07:41:42 +00:00
|
|
|
isolate()->heap()->AllocateWithContext(*function, *previous, *extension),
|
2011-03-18 20:35:07 +00:00
|
|
|
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) {
|
2011-08-11 16:29:28 +00:00
|
|
|
CALL_HEAP_FUNCTION(
|
|
|
|
isolate(),
|
|
|
|
isolate()->heap()->AllocateBlockContext(*function,
|
|
|
|
*previous,
|
|
|
|
*scope_info),
|
|
|
|
Context);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
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) {
|
2008-07-03 15:10:15 +00:00
|
|
|
ASSERT(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-01-16 17:08:45 +00:00
|
|
|
ASSERT(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) {
|
|
|
|
ASSERT(0 <= length && length <= Smi::kMaxValue);
|
|
|
|
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() {
|
|
|
|
CALL_HEAP_FUNCTION(
|
|
|
|
isolate(),
|
|
|
|
isolate()->heap()->AllocateAllocationSite(),
|
|
|
|
AllocationSite);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Handle<JSObject> Factory::NewFunctionPrototype(Handle<JSFunction> function) {
|
2013-11-05 13:34:14 +00:00
|
|
|
// 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());
|
|
|
|
ASSERT(object_function->has_initial_map());
|
|
|
|
new_map = Map::Copy(handle(object_function->initial_map()));
|
|
|
|
}
|
|
|
|
|
|
|
|
Handle<JSObject> prototype = NewJSObjectFromMap(new_map);
|
|
|
|
|
|
|
|
if (!function->shared()->is_generator()) {
|
|
|
|
JSObject::SetLocalPropertyIgnoreAttributes(prototype,
|
|
|
|
constructor_string(),
|
|
|
|
function,
|
2014-04-04 12:06:11 +00:00
|
|
|
DONT_ENUM).Assert();
|
2013-11-05 13:34:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return prototype;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Handle<FixedArray> Factory::CopyFixedArray(Handle<FixedArray> array) {
|
2011-03-18 20:35:07 +00:00
|
|
|
CALL_HEAP_FUNCTION(isolate(), array->Copy(), FixedArray);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-17 13:42:37 +00:00
|
|
|
Handle<FixedArray> Factory::CopyAndTenureFixedCOWArray(
|
|
|
|
Handle<FixedArray> array) {
|
|
|
|
ASSERT(isolate()->heap()->InNewSpace(*array));
|
|
|
|
CALL_HEAP_FUNCTION(isolate(),
|
|
|
|
isolate()->heap()->CopyAndTenureFixedCOWArray(*array),
|
|
|
|
FixedArray);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-11-13 12:27:03 +00:00
|
|
|
Handle<FixedArray> Factory::CopySizeFixedArray(Handle<FixedArray> array,
|
2013-10-04 07:25:24 +00:00
|
|
|
int new_length,
|
|
|
|
PretenureFlag pretenure) {
|
|
|
|
CALL_HEAP_FUNCTION(isolate(),
|
|
|
|
array->CopySize(new_length, pretenure),
|
|
|
|
FixedArray);
|
2012-11-13 12:27:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-10-19 11:36:55 +00:00
|
|
|
Handle<FixedDoubleArray> Factory::CopyFixedDoubleArray(
|
|
|
|
Handle<FixedDoubleArray> array) {
|
|
|
|
CALL_HEAP_FUNCTION(isolate(), array->Copy(), FixedDoubleArray);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-10-14 13:35:06 +00:00
|
|
|
Handle<ConstantPoolArray> Factory::CopyConstantPoolArray(
|
|
|
|
Handle<ConstantPoolArray> array) {
|
|
|
|
CALL_HEAP_FUNCTION(isolate(), array->Copy(), ConstantPoolArray);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-23 06:04:44 +00:00
|
|
|
Handle<JSFunction> Factory::BaseNewFunctionFromSharedFunctionInfo(
|
|
|
|
Handle<SharedFunctionInfo> function_info,
|
2009-12-16 15:43:20 +00:00
|
|
|
Handle<Map> function_map,
|
|
|
|
PretenureFlag pretenure) {
|
2011-03-18 20:35:07 +00:00
|
|
|
CALL_HEAP_FUNCTION(
|
|
|
|
isolate(),
|
|
|
|
isolate()->heap()->AllocateFunction(*function_map,
|
|
|
|
*function_info,
|
|
|
|
isolate()->heap()->the_hole_value(),
|
|
|
|
pretenure),
|
2008-07-03 15:10:15 +00:00
|
|
|
JSFunction);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-04-11 16:28:19 +00:00
|
|
|
static Handle<Map> MapForNewFunction(Isolate *isolate,
|
|
|
|
Handle<SharedFunctionInfo> function_info) {
|
|
|
|
Context *context = isolate->context()->native_context();
|
2014-03-11 14:41:22 +00:00
|
|
|
int map_index = Context::FunctionMapIndex(function_info->strict_mode(),
|
2013-04-11 16:28:19 +00:00
|
|
|
function_info->is_generator());
|
|
|
|
return Handle<Map>(Map::cast(context->get(map_index)));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-23 06:04:44 +00:00
|
|
|
Handle<JSFunction> Factory::NewFunctionFromSharedFunctionInfo(
|
|
|
|
Handle<SharedFunctionInfo> function_info,
|
2009-12-16 15:43:20 +00:00
|
|
|
Handle<Context> context,
|
|
|
|
PretenureFlag pretenure) {
|
2010-03-23 06:04:44 +00:00
|
|
|
Handle<JSFunction> result = BaseNewFunctionFromSharedFunctionInfo(
|
2011-03-17 20:28:17 +00:00
|
|
|
function_info,
|
2013-04-11 16:28:19 +00:00
|
|
|
MapForNewFunction(isolate(), function_info),
|
2011-03-17 20:28:17 +00:00
|
|
|
pretenure);
|
|
|
|
|
2012-03-26 13:08:08 +00:00
|
|
|
if (function_info->ic_age() != isolate()->heap()->global_ic_age()) {
|
|
|
|
function_info->ResetForNewContext(isolate()->heap()->global_ic_age());
|
|
|
|
}
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
result->set_context(*context);
|
2012-06-14 14:06:22 +00:00
|
|
|
|
2013-12-23 14:42:42 +00:00
|
|
|
int index = function_info->SearchOptimizedCodeMap(context->native_context(),
|
|
|
|
BailoutId::None());
|
2012-06-22 13:55:15 +00:00
|
|
|
if (!function_info->bound() && index < 0) {
|
|
|
|
int number_of_literals = function_info->num_literals();
|
|
|
|
Handle<FixedArray> literals = NewFixedArray(number_of_literals, pretenure);
|
|
|
|
if (number_of_literals > 0) {
|
2012-08-17 09:03:08 +00:00
|
|
|
// Store the native context in the literals array prefix. This
|
2012-06-22 13:55:15 +00:00
|
|
|
// context will be used when creating object, regexp and array
|
|
|
|
// literals in this function.
|
2012-08-17 09:03:08 +00:00
|
|
|
literals->set(JSFunction::kLiteralNativeContextIndex,
|
|
|
|
context->native_context());
|
2011-10-17 12:44:16 +00:00
|
|
|
}
|
2012-06-22 13:55:15 +00:00
|
|
|
result->set_literals(*literals);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
2012-06-14 14:06:22 +00:00
|
|
|
|
|
|
|
if (index > 0) {
|
|
|
|
// Caching of optimized code enabled and optimized code found.
|
2013-12-23 14:30:35 +00:00
|
|
|
FixedArray* literals =
|
|
|
|
function_info->GetLiteralsFromOptimizedCodeMap(index);
|
|
|
|
if (literals != NULL) result->set_literals(literals);
|
2014-02-28 12:27:31 +00:00
|
|
|
Code* code = function_info->GetCodeFromOptimizedCodeMap(index);
|
2014-03-03 11:11:39 +00:00
|
|
|
ASSERT(!code->marked_for_deoptimization());
|
2014-02-28 12:27:31 +00:00
|
|
|
result->ReplaceCode(code);
|
2012-06-14 14:06:22 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2013-09-03 08:49:44 +00:00
|
|
|
if (isolate()->use_crankshaft() &&
|
2010-12-07 11:31:57 +00:00
|
|
|
FLAG_always_opt &&
|
|
|
|
result->is_compiled() &&
|
|
|
|
!function_info->is_toplevel() &&
|
2012-03-19 10:16:38 +00:00
|
|
|
function_info->allows_lazy_compilation() &&
|
2013-07-02 15:30:33 +00:00
|
|
|
!function_info->optimization_disabled() &&
|
|
|
|
!isolate()->DebuggerHasBreakPoints()) {
|
2013-12-23 14:30:35 +00:00
|
|
|
result->MarkForOptimization();
|
2010-12-07 11:31:57 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Handle<Object> Factory::NewNumber(double value,
|
|
|
|
PretenureFlag pretenure) {
|
2011-03-18 20:35:07 +00:00
|
|
|
CALL_HEAP_FUNCTION(
|
|
|
|
isolate(),
|
|
|
|
isolate()->heap()->NumberFromDouble(value, pretenure), Object);
|
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) {
|
2011-03-18 20:35:07 +00:00
|
|
|
CALL_HEAP_FUNCTION(
|
|
|
|
isolate(),
|
2011-11-21 10:18:47 +00:00
|
|
|
isolate()->heap()->NumberFromInt32(value, pretenure), Object);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-21 10:18:47 +00:00
|
|
|
Handle<Object> Factory::NewNumberFromUint(uint32_t value,
|
|
|
|
PretenureFlag pretenure) {
|
2011-03-18 20:35:07 +00:00
|
|
|
CALL_HEAP_FUNCTION(
|
|
|
|
isolate(),
|
2011-11-21 10:18:47 +00:00
|
|
|
isolate()->heap()->NumberFromUint32(value, pretenure), Object);
|
2009-03-10 08:10:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-05-08 15:02:08 +00:00
|
|
|
Handle<HeapNumber> Factory::NewHeapNumber(double value,
|
|
|
|
PretenureFlag pretenure) {
|
|
|
|
CALL_HEAP_FUNCTION(
|
|
|
|
isolate(),
|
|
|
|
isolate()->heap()->AllocateHeapNumber(value, pretenure), HeapNumber);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
Handle<JSObject> Factory::NewNeanderObject() {
|
2011-03-18 20:35:07 +00:00
|
|
|
CALL_HEAP_FUNCTION(
|
|
|
|
isolate(),
|
|
|
|
isolate()->heap()->AllocateJSObjectFromMap(
|
|
|
|
isolate()->heap()->neander_map()),
|
|
|
|
JSObject);
|
2008-07-03 15:10:15 +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);
|
2013-06-06 13:28:22 +00:00
|
|
|
OS::StrNCpy(v, message, space);
|
|
|
|
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-03-20 12:22:13 +00:00
|
|
|
Object::GetElementNoExceptionThrown(isolate(), args, i));
|
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));
|
2013-12-09 07:41:20 +00:00
|
|
|
OS::StrNCpy(v2, arg.get(), space);
|
|
|
|
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';
|
|
|
|
}
|
|
|
|
Handle<String> error_string = NewStringFromUtf8(CStrVector(buffer), TENURED);
|
|
|
|
return error_string;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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);
|
2011-03-18 20:35:07 +00:00
|
|
|
Handle<Object> fun_obj(
|
2013-02-25 14:46:09 +00:00
|
|
|
isolate()->js_builtins_object()->GetPropertyNoExceptionThrown(*make_str),
|
|
|
|
isolate());
|
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.
|
|
|
|
bool caught_exception;
|
|
|
|
Handle<Object> result = Execution::TryCall(fun,
|
2011-10-06 09:31:38 +00:00
|
|
|
isolate()->js_builtins_object(),
|
|
|
|
ARRAY_SIZE(argv),
|
|
|
|
argv,
|
|
|
|
&caught_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);
|
2011-03-18 20:35:07 +00:00
|
|
|
Handle<JSFunction> fun = Handle<JSFunction>(
|
|
|
|
JSFunction::cast(isolate()->js_builtins_object()->
|
|
|
|
GetPropertyNoExceptionThrown(*constr)));
|
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.
|
|
|
|
bool caught_exception;
|
|
|
|
Handle<Object> result = Execution::TryCall(fun,
|
2011-10-06 09:31:38 +00:00
|
|
|
isolate()->js_builtins_object(),
|
|
|
|
ARRAY_SIZE(argv),
|
|
|
|
argv,
|
|
|
|
&caught_exception);
|
2008-07-03 15:10:15 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Handle<JSFunction> Factory::NewFunction(Handle<String> name,
|
|
|
|
InstanceType type,
|
|
|
|
int instance_size,
|
|
|
|
Handle<Code> code,
|
|
|
|
bool force_initial_map) {
|
|
|
|
// Allocate the function
|
|
|
|
Handle<JSFunction> function = NewFunction(name, the_hole_value());
|
2010-08-11 08:12:53 +00:00
|
|
|
|
2012-01-13 13:09:52 +00:00
|
|
|
// Set up the code pointer in both the shared function info and in
|
2010-08-11 08:12:53 +00:00
|
|
|
// the function itself.
|
|
|
|
function->shared()->set_code(*code);
|
2008-07-03 15:10:15 +00:00
|
|
|
function->set_code(*code);
|
|
|
|
|
|
|
|
if (force_initial_map ||
|
|
|
|
type != JS_OBJECT_TYPE ||
|
|
|
|
instance_size != JSObject::kHeaderSize) {
|
|
|
|
Handle<Map> initial_map = NewMap(type, instance_size);
|
|
|
|
Handle<JSObject> prototype = NewFunctionPrototype(function);
|
|
|
|
initial_map->set_prototype(*prototype);
|
|
|
|
function->set_initial_map(*initial_map);
|
|
|
|
initial_map->set_constructor(*function);
|
|
|
|
} else {
|
|
|
|
ASSERT(!function->has_initial_map());
|
|
|
|
ASSERT(!function->has_prototype());
|
|
|
|
}
|
|
|
|
|
|
|
|
return function;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Handle<JSFunction> Factory::NewFunctionWithPrototype(Handle<String> name,
|
|
|
|
InstanceType type,
|
|
|
|
int instance_size,
|
|
|
|
Handle<JSObject> prototype,
|
|
|
|
Handle<Code> code,
|
|
|
|
bool force_initial_map) {
|
2010-08-11 08:12:53 +00:00
|
|
|
// Allocate the function.
|
2008-07-03 15:10:15 +00:00
|
|
|
Handle<JSFunction> function = NewFunction(name, prototype);
|
|
|
|
|
2012-01-13 13:09:52 +00:00
|
|
|
// Set up the code pointer in both the shared function info and in
|
2010-08-11 08:12:53 +00:00
|
|
|
// the function itself.
|
|
|
|
function->shared()->set_code(*code);
|
2008-07-03 15:10:15 +00:00
|
|
|
function->set_code(*code);
|
|
|
|
|
|
|
|
if (force_initial_map ||
|
|
|
|
type != JS_OBJECT_TYPE ||
|
|
|
|
instance_size != JSObject::kHeaderSize) {
|
2011-09-22 11:30:04 +00:00
|
|
|
Handle<Map> initial_map = NewMap(type,
|
|
|
|
instance_size,
|
2012-05-23 14:24:29 +00:00
|
|
|
GetInitialFastElementsKind());
|
2008-07-03 15:10:15 +00:00
|
|
|
function->set_initial_map(*initial_map);
|
|
|
|
initial_map->set_constructor(*function);
|
|
|
|
}
|
|
|
|
|
2013-07-18 07:59:48 +00:00
|
|
|
JSFunction::SetPrototype(function, prototype);
|
2008-07-03 15:10:15 +00:00
|
|
|
return function;
|
|
|
|
}
|
|
|
|
|
2008-10-30 11:12:08 +00:00
|
|
|
|
2010-04-28 12:05:40 +00:00
|
|
|
Handle<JSFunction> Factory::NewFunctionWithoutPrototype(Handle<String> name,
|
|
|
|
Handle<Code> code) {
|
2014-03-11 14:41:22 +00:00
|
|
|
Handle<JSFunction> function = NewFunctionWithoutPrototype(name, SLOPPY);
|
2010-08-11 08:12:53 +00:00
|
|
|
function->shared()->set_code(*code);
|
2010-04-28 12:05:40 +00:00
|
|
|
function->set_code(*code);
|
|
|
|
ASSERT(!function->has_initial_map());
|
|
|
|
ASSERT(!function->has_prototype());
|
|
|
|
return function;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
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,
|
|
|
|
int prologue_offset) {
|
2011-03-18 20:35:07 +00:00
|
|
|
CALL_HEAP_FUNCTION(isolate(),
|
|
|
|
isolate()->heap()->CreateCode(
|
2013-10-23 13:48:04 +00:00
|
|
|
desc, flags, self_ref, immovable, crankshafted,
|
|
|
|
prologue_offset),
|
2011-03-18 20:35:07 +00:00
|
|
|
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) {
|
2012-04-16 14:43:27 +00:00
|
|
|
CALL_HEAP_FUNCTION(
|
|
|
|
isolate(),
|
2012-07-09 08:59:03 +00:00
|
|
|
isolate()->heap()->AllocateJSModule(*context, *scope_info), JSModule);
|
2012-04-16 14:43:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-10-21 13:55:24 +00:00
|
|
|
// TODO(mstarzinger): Temporary wrapper until handlified.
|
|
|
|
static Handle<NameDictionary> NameDictionaryAdd(Handle<NameDictionary> dict,
|
|
|
|
Handle<Name> name,
|
|
|
|
Handle<Object> value,
|
|
|
|
PropertyDetails details) {
|
|
|
|
CALL_HEAP_FUNCTION(dict->GetIsolate(),
|
|
|
|
dict->Add(*name, *value, details),
|
|
|
|
NameDictionary);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Handle<GlobalObject> NewGlobalObjectFromMap(Isolate* isolate,
|
|
|
|
Handle<Map> map) {
|
|
|
|
CALL_HEAP_FUNCTION(isolate,
|
|
|
|
isolate->heap()->Allocate(*map, OLD_POINTER_SPACE),
|
2009-07-01 11:44:37 +00:00
|
|
|
GlobalObject);
|
2009-06-30 10:05:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-10-21 13:55:24 +00:00
|
|
|
Handle<GlobalObject> Factory::NewGlobalObject(Handle<JSFunction> constructor) {
|
|
|
|
ASSERT(constructor->has_initial_map());
|
|
|
|
Handle<Map> map(constructor->initial_map());
|
|
|
|
ASSERT(map->is_dictionary_map());
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
ASSERT(map->NextFreePropertyIndex() == 0);
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
ASSERT(map->unused_property_fields() == 0);
|
|
|
|
ASSERT(map->inobject_properties() == 0);
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
Handle<NameDictionary> dictionary = NewNameDictionary(at_least_space_for);
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
ASSERT(details.type() == CALLBACKS); // Only accessors are expected.
|
|
|
|
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);
|
|
|
|
NameDictionaryAdd(dictionary, name, cell, d);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allocate the global object and initialize it with the backing store.
|
|
|
|
Handle<GlobalObject> global = NewGlobalObjectFromMap(isolate(), map);
|
|
|
|
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.
|
|
|
|
ASSERT(global->IsGlobalObject() && !global->HasFastProperties());
|
|
|
|
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) {
|
|
|
|
CALL_HEAP_FUNCTION(isolate(),
|
|
|
|
isolate()->heap()->AllocateJSArray(elements_kind,
|
|
|
|
pretenure),
|
|
|
|
JSArray);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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) {
|
2011-03-18 20:35:07 +00:00
|
|
|
CALL_HEAP_FUNCTION(isolate(),
|
2012-01-26 21:47:57 +00:00
|
|
|
isolate()->heap()->AllocateJSArrayAndStorage(
|
|
|
|
elements_kind,
|
2014-03-21 11:22:16 +00:00
|
|
|
length,
|
2012-01-26 21:47:57 +00:00
|
|
|
capacity,
|
2014-03-27 16:41:09 +00:00
|
|
|
mode,
|
2012-01-26 21:47:57 +00:00
|
|
|
pretenure),
|
2011-03-18 20:35:07 +00:00
|
|
|
JSArray);
|
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-03-21 11:22:16 +00:00
|
|
|
ASSERT(length <= elements->length());
|
2014-04-07 10:00:14 +00:00
|
|
|
Handle<JSArray> array =
|
|
|
|
isolate()->factory()->NewJSArray(elements_kind, pretenure);
|
|
|
|
|
|
|
|
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,
|
|
|
|
int length,
|
|
|
|
int capacity,
|
|
|
|
ArrayStorageAllocationMode mode) {
|
|
|
|
CALL_HEAP_FUNCTION_VOID(isolate(),
|
|
|
|
isolate()->heap()->AllocateJSArrayStorage(*array,
|
|
|
|
length,
|
|
|
|
capacity,
|
|
|
|
mode));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-11-05 12:11:27 +00:00
|
|
|
Handle<JSGeneratorObject> Factory::NewJSGeneratorObject(
|
|
|
|
Handle<JSFunction> function) {
|
|
|
|
ASSERT(function->shared()->is_generator());
|
|
|
|
JSFunction::EnsureHasInitialMap(function);
|
|
|
|
Handle<Map> map(function->initial_map());
|
|
|
|
ASSERT(map->instance_type() == JS_GENERATOR_OBJECT_TYPE);
|
|
|
|
CALL_HEAP_FUNCTION(
|
|
|
|
isolate(),
|
|
|
|
isolate()->heap()->AllocateJSObjectFromMap(*map),
|
|
|
|
JSGeneratorObject);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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(
|
|
|
|
isolate()->context()->native_context()->array_buffer_fun());
|
2013-04-25 12:02:23 +00:00
|
|
|
CALL_HEAP_FUNCTION(
|
|
|
|
isolate(),
|
2013-08-06 19:14:51 +00:00
|
|
|
isolate()->heap()->AllocateJSObject(*array_buffer_fun),
|
2013-04-25 12:02:23 +00:00
|
|
|
JSArrayBuffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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(
|
|
|
|
isolate()->context()->native_context()->data_view_fun());
|
2013-06-21 13:02:38 +00:00
|
|
|
CALL_HEAP_FUNCTION(
|
|
|
|
isolate(),
|
2013-08-06 19:14:51 +00:00
|
|
|
isolate()->heap()->AllocateJSObject(*data_view_fun),
|
2013-06-21 13:02:38 +00:00
|
|
|
JSDataView);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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()));
|
2013-04-29 11:09:03 +00:00
|
|
|
|
|
|
|
CALL_HEAP_FUNCTION(
|
|
|
|
isolate(),
|
2013-08-06 19:14:51 +00:00
|
|
|
isolate()->heap()->AllocateJSObject(*typed_array_fun_handle),
|
2013-04-29 11:09:03 +00:00
|
|
|
JSTypedArray);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-13 10:58:25 +00:00
|
|
|
Handle<JSProxy> Factory::NewJSProxy(Handle<Object> handler,
|
|
|
|
Handle<Object> prototype) {
|
|
|
|
CALL_HEAP_FUNCTION(
|
|
|
|
isolate(),
|
|
|
|
isolate()->heap()->AllocateJSProxy(*handler, *prototype),
|
|
|
|
JSProxy);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-13 11:42:57 +00:00
|
|
|
void Factory::BecomeJSObject(Handle<JSReceiver> object) {
|
2011-07-18 13:04:52 +00:00
|
|
|
CALL_HEAP_FUNCTION_VOID(
|
|
|
|
isolate(),
|
2011-09-13 11:42:57 +00:00
|
|
|
isolate()->heap()->ReinitializeJSReceiver(
|
|
|
|
*object, JS_OBJECT_TYPE, JSObject::kHeaderSize));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Factory::BecomeJSFunction(Handle<JSReceiver> object) {
|
|
|
|
CALL_HEAP_FUNCTION_VOID(
|
|
|
|
isolate(),
|
|
|
|
isolate()->heap()->ReinitializeJSReceiver(
|
|
|
|
*object, JS_FUNCTION_TYPE, JSFunction::kSize));
|
2011-07-18 13:04:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-23 06:04:44 +00:00
|
|
|
Handle<SharedFunctionInfo> Factory::NewSharedFunctionInfo(
|
2010-07-13 13:06:33 +00:00
|
|
|
Handle<String> name,
|
|
|
|
int number_of_literals,
|
2013-04-17 15:01:25 +00:00
|
|
|
bool is_generator,
|
2010-07-13 13:06:33 +00:00
|
|
|
Handle<Code> code,
|
2014-03-17 08:31:21 +00:00
|
|
|
Handle<ScopeInfo> scope_info) {
|
2010-03-23 06:04:44 +00:00
|
|
|
Handle<SharedFunctionInfo> shared = NewSharedFunctionInfo(name);
|
|
|
|
shared->set_code(*code);
|
2010-07-13 13:06:33 +00:00
|
|
|
shared->set_scope_info(*scope_info);
|
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) {
|
2011-03-18 20:35:07 +00:00
|
|
|
CALL_HEAP_FUNCTION(isolate(),
|
|
|
|
isolate()->heap()->AllocateJSMessageObject(*type,
|
|
|
|
*arguments,
|
|
|
|
start_position,
|
|
|
|
end_position,
|
|
|
|
*script,
|
|
|
|
*stack_frames),
|
2011-02-02 13:31:52 +00:00
|
|
|
JSMessageObject);
|
|
|
|
}
|
|
|
|
|
2013-07-05 09:52:11 +00:00
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
Handle<SharedFunctionInfo> Factory::NewSharedFunctionInfo(Handle<String> name) {
|
2011-03-18 20:35:07 +00:00
|
|
|
CALL_HEAP_FUNCTION(isolate(),
|
|
|
|
isolate()->heap()->AllocateSharedFunctionInfo(*name),
|
2008-07-03 15:10:15 +00:00
|
|
|
SharedFunctionInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-10-02 13:43:16 +00:00
|
|
|
Handle<String> Factory::NumberToString(Handle<Object> number) {
|
2011-03-18 20:35:07 +00:00
|
|
|
CALL_HEAP_FUNCTION(isolate(),
|
|
|
|
isolate()->heap()->NumberToString(*number), String);
|
2009-10-02 13:43:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-22 10:45:37 +00:00
|
|
|
Handle<String> Factory::Uint32ToString(uint32_t value) {
|
|
|
|
CALL_HEAP_FUNCTION(isolate(),
|
|
|
|
isolate()->heap()->Uint32ToString(value), String);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-01-16 09:44:35 +00:00
|
|
|
Handle<SeededNumberDictionary> Factory::DictionaryAtNumberPut(
|
|
|
|
Handle<SeededNumberDictionary> dictionary,
|
|
|
|
uint32_t key,
|
|
|
|
Handle<Object> value) {
|
|
|
|
CALL_HEAP_FUNCTION(isolate(),
|
|
|
|
dictionary->AtNumberPut(key, *value),
|
|
|
|
SeededNumberDictionary);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Handle<UnseededNumberDictionary> Factory::DictionaryAtNumberPut(
|
|
|
|
Handle<UnseededNumberDictionary> dictionary,
|
2009-07-02 06:50:43 +00:00
|
|
|
uint32_t key,
|
|
|
|
Handle<Object> value) {
|
2011-03-18 20:35:07 +00:00
|
|
|
CALL_HEAP_FUNCTION(isolate(),
|
|
|
|
dictionary->AtNumberPut(key, *value),
|
2012-01-16 09:44:35 +00:00
|
|
|
UnseededNumberDictionary);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Handle<JSFunction> Factory::NewFunctionHelper(Handle<String> name,
|
|
|
|
Handle<Object> prototype) {
|
|
|
|
Handle<SharedFunctionInfo> function_share = NewSharedFunctionInfo(name);
|
2011-03-18 20:35:07 +00:00
|
|
|
CALL_HEAP_FUNCTION(
|
|
|
|
isolate(),
|
2014-03-11 14:41:22 +00:00
|
|
|
isolate()->heap()->AllocateFunction(*isolate()->sloppy_function_map(),
|
2011-03-18 20:35:07 +00:00
|
|
|
*function_share,
|
|
|
|
*prototype),
|
|
|
|
JSFunction);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Handle<JSFunction> Factory::NewFunction(Handle<String> name,
|
|
|
|
Handle<Object> prototype) {
|
|
|
|
Handle<JSFunction> fun = NewFunctionHelper(name, prototype);
|
2012-08-17 09:03:08 +00:00
|
|
|
fun->set_context(isolate()->context()->native_context());
|
2008-07-03 15:10:15 +00:00
|
|
|
return fun;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-04-28 12:05:40 +00:00
|
|
|
Handle<JSFunction> Factory::NewFunctionWithoutPrototypeHelper(
|
2011-03-17 20:28:17 +00:00
|
|
|
Handle<String> name,
|
2014-03-11 14:41:22 +00:00
|
|
|
StrictMode strict_mode) {
|
2010-04-28 12:05:40 +00:00
|
|
|
Handle<SharedFunctionInfo> function_share = NewSharedFunctionInfo(name);
|
2014-03-11 14:41:22 +00:00
|
|
|
Handle<Map> map = strict_mode == SLOPPY
|
|
|
|
? isolate()->sloppy_function_without_prototype_map()
|
|
|
|
: isolate()->strict_function_without_prototype_map();
|
2011-03-18 20:35:07 +00:00
|
|
|
CALL_HEAP_FUNCTION(isolate(),
|
|
|
|
isolate()->heap()->AllocateFunction(
|
2011-03-17 20:28:17 +00:00
|
|
|
*map,
|
2010-04-28 12:05:40 +00:00
|
|
|
*function_share,
|
|
|
|
*the_hole_value()),
|
|
|
|
JSFunction);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-03-17 20:28:17 +00:00
|
|
|
Handle<JSFunction> Factory::NewFunctionWithoutPrototype(
|
|
|
|
Handle<String> name,
|
2014-03-11 14:41:22 +00:00
|
|
|
StrictMode strict_mode) {
|
|
|
|
Handle<JSFunction> fun = NewFunctionWithoutPrototypeHelper(name, strict_mode);
|
2012-08-17 09:03:08 +00:00
|
|
|
fun->set_context(isolate()->context()->native_context());
|
2010-04-28 12:05:40 +00:00
|
|
|
return fun;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-01-18 14:13:58 +00:00
|
|
|
Handle<Object> Factory::ToObject(Handle<Object> object) {
|
2013-09-04 07:05:11 +00:00
|
|
|
CALL_HEAP_FUNCTION(isolate(), object->ToObject(isolate()), Object);
|
2010-01-18 14:13:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
Handle<Object> Factory::ToObject(Handle<Object> object,
|
2012-08-17 09:03:08 +00:00
|
|
|
Handle<Context> native_context) {
|
|
|
|
CALL_HEAP_FUNCTION(isolate(), object->ToObject(*native_context), Object);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-04-20 16:36:13 +00:00
|
|
|
#ifdef ENABLE_DEBUGGER_SUPPORT
|
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(
|
2011-03-18 20:35:07 +00:00
|
|
|
NewFixedArray(Debug::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;
|
|
|
|
}
|
2009-04-20 16:36:13 +00:00
|
|
|
#endif
|
2008-09-01 10:15:45 +00:00
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
Handle<JSObject> Factory::NewArgumentsObject(Handle<Object> callee,
|
|
|
|
int length) {
|
2011-03-18 20:35:07 +00:00
|
|
|
CALL_HEAP_FUNCTION(
|
|
|
|
isolate(),
|
|
|
|
isolate()->heap()->AllocateArgumentsObject(*callee, length), JSObject);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Handle<JSFunction> Factory::CreateApiFunction(
|
Split window support from V8.
Here is a description of the background and design of split window in Chrome and V8:
https://docs.google.com/a/google.com/Doc?id=chhjkpg_47fwddxbfr
This change list splits the window object into two parts: 1) an inner window object used as the global object of contexts; 2) an outer window object exposed to JavaScript and accessible by the name 'window'. Firefox did it awhile ago, here are some discussions: https://wiki.mozilla.org/Gecko:SplitWindow. One additional benefit of splitting window in Chrome is that accessing global variables don't need security checks anymore, it can improve applications that use many global variables.
V8 support of split window:
There are a small number of changes on V8 api to support split window:
Security context is removed from V8, so does related API functions;
A global object can be detached from its context and reused by a new context;
Access checks on an object template can be turned on/off by default;
An object can turn on its access checks later;
V8 has a new object type, ApiGlobalObject, which is the outer window object type. The existing JSGlobalObject becomes the inner window object type. Security checks are moved from JSGlobalObject to ApiGlobalObject. ApiGlobalObject is the one exposed to JavaScript, it is accessible through Context::Global(). ApiGlobalObject's prototype is set to JSGlobalObject so that property lookups are forwarded to JSGlobalObject. ApiGlobalObject forwards all other property access requests to JSGlobalObject, such as SetProperty, DeleteProperty, etc.
Security token is moved to a global context, and ApiGlobalObject has a reference to its global context. JSGlobalObject has a reference to its global context as well. When accessing properties on a global object in JavaScript, the domain security check is performed by comparing the security token of the lexical context (Top::global_context()) to the token of global object's context. The check is only needed when the receiver is a window object, such as 'window.document'. Accessing global variables, such as 'var foo = 3; foo' does not need checks because the receiver is the inner window object.
When an outer window is detached from its global context (when a frame navigates away from a page), it is completely detached from the inner window. A new context is created for the new page, and the outer global object is reused. At this point, the access check on the DOMWindow wrapper of the old context is turned on. The code in old context is still able to access DOMWindow properties, but it has to go through domain security checks.
It is debatable on how to implement the outer window object. Currently each property access function has to check if the receiver is ApiGlobalObject type. This approach might be error-prone that one may forget to check the receiver when adding new functions. It is unlikely a performance issue because accessing global variables are more common than 'window.foo' style coding.
I am still working on the ARM port, and I'd like to hear comments and suggestions on the best way to support it in V8.
Review URL: http://codereview.chromium.org/7366
git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@540 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2008-10-21 19:07:58 +00:00
|
|
|
Handle<FunctionTemplateInfo> obj, 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
|
|
|
|
2008-07-16 07:07:30 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2013-08-09 08:22:46 +00:00
|
|
|
// TODO(svenpanne) Kill ApiInstanceType and refactor things by generalizing
|
|
|
|
// JSObject::GetHeaderSize.
|
2008-07-16 07:07:30 +00:00
|
|
|
int instance_size = kPointerSize * internal_field_count;
|
2013-08-09 08:22:46 +00:00
|
|
|
InstanceType type;
|
Split window support from V8.
Here is a description of the background and design of split window in Chrome and V8:
https://docs.google.com/a/google.com/Doc?id=chhjkpg_47fwddxbfr
This change list splits the window object into two parts: 1) an inner window object used as the global object of contexts; 2) an outer window object exposed to JavaScript and accessible by the name 'window'. Firefox did it awhile ago, here are some discussions: https://wiki.mozilla.org/Gecko:SplitWindow. One additional benefit of splitting window in Chrome is that accessing global variables don't need security checks anymore, it can improve applications that use many global variables.
V8 support of split window:
There are a small number of changes on V8 api to support split window:
Security context is removed from V8, so does related API functions;
A global object can be detached from its context and reused by a new context;
Access checks on an object template can be turned on/off by default;
An object can turn on its access checks later;
V8 has a new object type, ApiGlobalObject, which is the outer window object type. The existing JSGlobalObject becomes the inner window object type. Security checks are moved from JSGlobalObject to ApiGlobalObject. ApiGlobalObject is the one exposed to JavaScript, it is accessible through Context::Global(). ApiGlobalObject's prototype is set to JSGlobalObject so that property lookups are forwarded to JSGlobalObject. ApiGlobalObject forwards all other property access requests to JSGlobalObject, such as SetProperty, DeleteProperty, etc.
Security token is moved to a global context, and ApiGlobalObject has a reference to its global context. JSGlobalObject has a reference to its global context as well. When accessing properties on a global object in JavaScript, the domain security check is performed by comparing the security token of the lexical context (Top::global_context()) to the token of global object's context. The check is only needed when the receiver is a window object, such as 'window.document'. Accessing global variables, such as 'var foo = 3; foo' does not need checks because the receiver is the inner window object.
When an outer window is detached from its global context (when a frame navigates away from a page), it is completely detached from the inner window. A new context is created for the new page, and the outer global object is reused. At this point, the access check on the DOMWindow wrapper of the old context is turned on. The code in old context is still able to access DOMWindow properties, but it has to go through domain security checks.
It is debatable on how to implement the outer window object. Currently each property access function has to check if the receiver is ApiGlobalObject type. This approach might be error-prone that one may forget to check the receiver when adding new functions. It is unlikely a performance issue because accessing global variables are more common than 'window.foo' style coding.
I am still working on the ARM port, and I'd like to hear comments and suggestions on the best way to support it in V8.
Review URL: http://codereview.chromium.org/7366
git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@540 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2008-10-21 19:07:58 +00:00
|
|
|
switch (instance_type) {
|
|
|
|
case JavaScriptObject:
|
|
|
|
type = JS_OBJECT_TYPE;
|
|
|
|
instance_size += JSObject::kHeaderSize;
|
|
|
|
break;
|
|
|
|
case InnerGlobalObject:
|
|
|
|
type = JS_GLOBAL_OBJECT_TYPE;
|
|
|
|
instance_size += JSGlobalObject::kSize;
|
|
|
|
break;
|
|
|
|
case OuterGlobalObject:
|
|
|
|
type = JS_GLOBAL_PROXY_TYPE;
|
|
|
|
instance_size += JSGlobalProxy::kSize;
|
|
|
|
break;
|
|
|
|
default:
|
2013-08-09 08:22:46 +00:00
|
|
|
UNREACHABLE();
|
|
|
|
type = JS_OBJECT_TYPE; // Keep the compiler happy.
|
Split window support from V8.
Here is a description of the background and design of split window in Chrome and V8:
https://docs.google.com/a/google.com/Doc?id=chhjkpg_47fwddxbfr
This change list splits the window object into two parts: 1) an inner window object used as the global object of contexts; 2) an outer window object exposed to JavaScript and accessible by the name 'window'. Firefox did it awhile ago, here are some discussions: https://wiki.mozilla.org/Gecko:SplitWindow. One additional benefit of splitting window in Chrome is that accessing global variables don't need security checks anymore, it can improve applications that use many global variables.
V8 support of split window:
There are a small number of changes on V8 api to support split window:
Security context is removed from V8, so does related API functions;
A global object can be detached from its context and reused by a new context;
Access checks on an object template can be turned on/off by default;
An object can turn on its access checks later;
V8 has a new object type, ApiGlobalObject, which is the outer window object type. The existing JSGlobalObject becomes the inner window object type. Security checks are moved from JSGlobalObject to ApiGlobalObject. ApiGlobalObject is the one exposed to JavaScript, it is accessible through Context::Global(). ApiGlobalObject's prototype is set to JSGlobalObject so that property lookups are forwarded to JSGlobalObject. ApiGlobalObject forwards all other property access requests to JSGlobalObject, such as SetProperty, DeleteProperty, etc.
Security token is moved to a global context, and ApiGlobalObject has a reference to its global context. JSGlobalObject has a reference to its global context as well. When accessing properties on a global object in JavaScript, the domain security check is performed by comparing the security token of the lexical context (Top::global_context()) to the token of global object's context. The check is only needed when the receiver is a window object, such as 'window.document'. Accessing global variables, such as 'var foo = 3; foo' does not need checks because the receiver is the inner window object.
When an outer window is detached from its global context (when a frame navigates away from a page), it is completely detached from the inner window. A new context is created for the new page, and the outer global object is reused. At this point, the access check on the DOMWindow wrapper of the old context is turned on. The code in old context is still able to access DOMWindow properties, but it has to go through domain security checks.
It is debatable on how to implement the outer window object. Currently each property access function has to check if the receiver is ApiGlobalObject type. This approach might be error-prone that one may forget to check the receiver when adding new functions. It is unlikely a performance issue because accessing global variables are more common than 'window.foo' style coding.
I am still working on the ARM port, and I'd like to hear comments and suggestions on the best way to support it in V8.
Review URL: http://codereview.chromium.org/7366
git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@540 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2008-10-21 19:07:58 +00:00
|
|
|
break;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Handle<JSFunction> result =
|
2013-02-28 17:03:34 +00:00
|
|
|
NewFunction(Factory::empty_string(),
|
2011-03-18 20:35:07 +00:00
|
|
|
type,
|
|
|
|
instance_size,
|
|
|
|
code,
|
|
|
|
true);
|
2012-12-19 10:28:36 +00:00
|
|
|
|
|
|
|
// Set length.
|
|
|
|
result->shared()->set_length(obj->length());
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
// Set class name.
|
2013-02-25 14:46:09 +00:00
|
|
|
Handle<Object> class_name = Handle<Object>(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);
|
|
|
|
}
|
|
|
|
|
|
|
|
Handle<Map> map = Handle<Map>(result->initial_map());
|
|
|
|
|
|
|
|
// 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();
|
|
|
|
}
|
|
|
|
|
|
|
|
result->shared()->set_function_data(*obj);
|
2010-01-15 12:25:24 +00:00
|
|
|
result->shared()->set_construct_stub(*construct_stub);
|
2008-09-16 10:12:32 +00:00
|
|
|
result->shared()->DontAdaptArguments();
|
2008-07-03 15:10:15 +00:00
|
|
|
|
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)));
|
|
|
|
JSObject::SetAccessor(result, accessor);
|
|
|
|
}
|
|
|
|
|
2010-03-11 16:24:31 +00:00
|
|
|
ASSERT(result->shared()->IsApiFunction());
|
2008-07-03 15:10:15 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-25 07:46:07 +00:00
|
|
|
Handle<MapCache> Factory::NewMapCache(int at_least_space_for) {
|
2011-03-18 20:35:07 +00:00
|
|
|
CALL_HEAP_FUNCTION(isolate(),
|
2013-03-12 07:06:36 +00:00
|
|
|
MapCache::Allocate(isolate()->heap(),
|
|
|
|
at_least_space_for),
|
|
|
|
MapCache);
|
2008-09-25 07:46:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-10-25 15:22:03 +00:00
|
|
|
MUST_USE_RESULT static MaybeObject* UpdateMapCacheWith(Context* context,
|
|
|
|
FixedArray* keys,
|
|
|
|
Map* map) {
|
|
|
|
Object* result;
|
|
|
|
{ MaybeObject* maybe_result =
|
|
|
|
MapCache::cast(context->map_cache())->Put(keys, map);
|
|
|
|
if (!maybe_result->ToObject(&result)) return maybe_result;
|
|
|
|
}
|
|
|
|
context->set_map_cache(MapCache::cast(result));
|
2008-09-25 07:46:07 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Handle<MapCache> Factory::AddToMapCache(Handle<Context> context,
|
|
|
|
Handle<FixedArray> keys,
|
|
|
|
Handle<Map> map) {
|
2011-03-18 20:35:07 +00:00
|
|
|
CALL_HEAP_FUNCTION(isolate(),
|
|
|
|
UpdateMapCacheWith(*context, *keys, *map), MapCache);
|
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.
|
2008-09-25 07:46:07 +00:00
|
|
|
Handle<MapCache> new_cache = NewMapCache(24);
|
|
|
|
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
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
void Factory::ConfigureInstance(Handle<FunctionTemplateInfo> desc,
|
|
|
|
Handle<JSObject> instance,
|
|
|
|
bool* pending_exception) {
|
|
|
|
// 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()) {
|
2013-09-03 06:59:01 +00:00
|
|
|
Execution::ConfigureInstance(isolate(),
|
|
|
|
instance,
|
2008-07-03 15:10:15 +00:00
|
|
|
instance_template,
|
|
|
|
pending_exception);
|
|
|
|
} else {
|
|
|
|
*pending_exception = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-23 08:00:06 +00:00
|
|
|
Handle<Object> Factory::GlobalConstantFor(Handle<String> name) {
|
|
|
|
Heap* h = isolate()->heap();
|
2013-02-28 17:03:34 +00:00
|
|
|
if (name->Equals(h->undefined_string())) return undefined_value();
|
|
|
|
if (name->Equals(h->nan_string())) return nan_value();
|
|
|
|
if (name->Equals(h->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
|
|
|
}
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
} } // namespace v8::internal
|