2014-03-31 12:01:02 +00:00
|
|
|
// Copyright 2014 the V8 project authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/factory.h"
|
2014-03-31 12:01:02 +00:00
|
|
|
|
2014-07-07 13:27:37 +00:00
|
|
|
#include "src/allocation-site-scopes.h"
|
2014-09-02 13:36:35 +00:00
|
|
|
#include "src/base/bits.h"
|
2015-04-09 22:40:16 +00:00
|
|
|
#include "src/bootstrapper.h"
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/conversions.h"
|
2015-09-01 09:25:19 +00:00
|
|
|
#include "src/isolate-inl.h"
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/macro-assembler.h"
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2009-05-25 10:05:56 +00:00
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2014-04-15 11:51:34 +00:00
|
|
|
|
2015-09-28 18:08:17 +00:00
|
|
|
// Calls the FUNCTION_CALL function and retries it up to three times
|
|
|
|
// to guarantee that any allocations performed during the call will
|
|
|
|
// succeed if there's enough memory.
|
|
|
|
//
|
|
|
|
// Warning: Do not use the identifiers __object__, __maybe_object__,
|
|
|
|
// __allocation__ or __scope__ in a call to this macro.
|
|
|
|
|
|
|
|
#define RETURN_OBJECT_UNLESS_RETRY(ISOLATE, TYPE) \
|
|
|
|
if (__allocation__.To(&__object__)) { \
|
|
|
|
DCHECK(__object__ != (ISOLATE)->heap()->exception()); \
|
|
|
|
return Handle<TYPE>(TYPE::cast(__object__), ISOLATE); \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define CALL_HEAP_FUNCTION(ISOLATE, FUNCTION_CALL, TYPE) \
|
|
|
|
do { \
|
|
|
|
AllocationResult __allocation__ = FUNCTION_CALL; \
|
|
|
|
Object* __object__ = NULL; \
|
|
|
|
RETURN_OBJECT_UNLESS_RETRY(ISOLATE, TYPE) \
|
|
|
|
/* Two GCs before panicking. In newspace will almost always succeed. */ \
|
|
|
|
for (int __i__ = 0; __i__ < 2; __i__++) { \
|
|
|
|
(ISOLATE)->heap()->CollectGarbage(__allocation__.RetrySpace(), \
|
|
|
|
"allocation failure"); \
|
|
|
|
__allocation__ = FUNCTION_CALL; \
|
|
|
|
RETURN_OBJECT_UNLESS_RETRY(ISOLATE, TYPE) \
|
|
|
|
} \
|
|
|
|
(ISOLATE)->counters()->gc_last_resort_from_handles()->Increment(); \
|
|
|
|
(ISOLATE)->heap()->CollectAllAvailableGarbage("last resort gc"); \
|
|
|
|
{ \
|
|
|
|
AlwaysAllocateScope __scope__(ISOLATE); \
|
|
|
|
__allocation__ = FUNCTION_CALL; \
|
|
|
|
} \
|
|
|
|
RETURN_OBJECT_UNLESS_RETRY(ISOLATE, TYPE) \
|
|
|
|
/* TODO(1181417): Fix this. */ \
|
|
|
|
v8::internal::Heap::FatalProcessOutOfMemory("CALL_AND_RETRY_LAST", true); \
|
|
|
|
return Handle<TYPE>(); \
|
|
|
|
} while (false)
|
|
|
|
|
|
|
|
|
2014-04-15 11:51:34 +00:00
|
|
|
template<typename T>
|
|
|
|
Handle<T> Factory::New(Handle<Map> map, AllocationSpace space) {
|
|
|
|
CALL_HEAP_FUNCTION(
|
|
|
|
isolate(),
|
|
|
|
isolate()->heap()->Allocate(*map, space),
|
|
|
|
T);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
Handle<T> Factory::New(Handle<Map> map,
|
|
|
|
AllocationSpace space,
|
|
|
|
Handle<AllocationSite> allocation_site) {
|
|
|
|
CALL_HEAP_FUNCTION(
|
|
|
|
isolate(),
|
|
|
|
isolate()->heap()->Allocate(*map, space, *allocation_site),
|
|
|
|
T);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-17 13:15:22 +00:00
|
|
|
Handle<HeapObject> Factory::NewFillerObject(int size,
|
|
|
|
bool double_align,
|
|
|
|
AllocationSpace space) {
|
|
|
|
CALL_HEAP_FUNCTION(
|
|
|
|
isolate(),
|
|
|
|
isolate()->heap()->AllocateFillerObject(size, double_align, space),
|
|
|
|
HeapObject);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-07 12:43:35 +00:00
|
|
|
Handle<Box> Factory::NewBox(Handle<Object> value) {
|
|
|
|
Handle<Box> result = Handle<Box>::cast(NewStruct(BOX_TYPE));
|
|
|
|
result->set_value(*value);
|
|
|
|
return result;
|
2013-06-06 15:40:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-04-07 10:42:45 +00:00
|
|
|
Handle<PrototypeInfo> Factory::NewPrototypeInfo() {
|
|
|
|
Handle<PrototypeInfo> result =
|
|
|
|
Handle<PrototypeInfo>::cast(NewStruct(PROTOTYPE_INFO_TYPE));
|
|
|
|
result->set_prototype_users(WeakFixedArray::Empty());
|
2015-08-08 22:56:15 +00:00
|
|
|
result->set_registry_slot(PrototypeInfo::UNREGISTERED);
|
2015-04-07 10:42:45 +00:00
|
|
|
result->set_validity_cell(Smi::FromInt(0));
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
[es6] Parameter scopes for sloppy eval
This CL is a nightmare! For the utterly irrelevant edge case of a sloppy function with non-simple parameters and a call to direct eval, like here,
let x = 1;
function f(g = () => x) {
var y
eval("var x = 2")
return g() + x // f() = 3
}
we have to do all of the following, on top of the declaration block ("varblock") contexts we already introduce around the body:
- Introduce the ability for varblock contexts to have both a ScopeInfo and an extension object (e.g., the body varblock in the example will contain both a static var y and a dynamic var x). No other scope needs that. Since there are no context slots left, a special new struct is introduced that pairs up scope info and extension object.
- When declaring lookup slots in the runtime, this new struct is allocated in the case where an extension object has to be added to a block scope (at which point the block's extension slot still contains a plain ScopeInfo).
- While at it, introduce some abstraction to access context extension slots in a more controlled manner, in order to keep special-casing to a minimum.
- Make sure that even empty varblock contexts do not get optimised away when they contain a sloppy eval, so that they can host the potential extension object.
- Extend dynamic search for declaration contexts (used by sloppy direct eval) to recognize varblock contexts.
- In the parser, if a function has a sloppy direct eval, introduce an additional varblock scope around each non-simple (desugared) parameter, as required by the spec to contain possible dynamic var bindings.
- In the pattern rewriter, add the ability to hoist the named variables the pattern declares to an outer scope. That is required because the actual destructuring has to be evaluated inside the protecting varblock scope, but the bindings that the desugaring introduces are in the outer scope.
- ScopeInfos need to save the information whether a block is a varblock, to make sloppy eval calls work correctly that deserialise them as part of the scope chain.
- Add the ability to materialize block scopes with extension objects in the debugger. Likewise, enable setting extension variables in block scopes via the debugger interface.
- While at it, refactor and unify some respective code in the debugger.
Sorry, this CL is large. I could try to split it up, but everything is rather entangled.
@mstarzinger: Please review the changes to contexts.
@yangguo: Please have a look at the debugger stuff.
R=littledan@chromium.org, mstarzinger@chromium.org, yangguo@chromium.org
BUG=v8:811,v8:2160
LOG=N
Review URL: https://codereview.chromium.org/1292753007
Cr-Commit-Position: refs/heads/master@{#30295}
2015-08-21 10:58:35 +00:00
|
|
|
Handle<SloppyBlockWithEvalContextExtension>
|
|
|
|
Factory::NewSloppyBlockWithEvalContextExtension(
|
|
|
|
Handle<ScopeInfo> scope_info, Handle<JSObject> extension) {
|
|
|
|
DCHECK(scope_info->is_declaration_scope());
|
|
|
|
Handle<SloppyBlockWithEvalContextExtension> result =
|
|
|
|
Handle<SloppyBlockWithEvalContextExtension>::cast(
|
|
|
|
NewStruct(SLOPPY_BLOCK_WITH_EVAL_CONTEXT_EXTENSION_TYPE));
|
|
|
|
result->set_scope_info(*scope_info);
|
|
|
|
result->set_extension(*extension);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-08-11 10:47:24 +00:00
|
|
|
Handle<Oddball> Factory::NewOddball(Handle<Map> map, const char* to_string,
|
2014-04-16 14:15:32 +00:00
|
|
|
Handle<Object> to_number,
|
2015-08-11 10:47:24 +00:00
|
|
|
const char* type_of, byte kind) {
|
2015-04-07 11:31:57 +00:00
|
|
|
Handle<Oddball> oddball = New<Oddball>(map, OLD_SPACE);
|
2015-08-11 10:47:24 +00:00
|
|
|
Oddball::Initialize(isolate(), oddball, to_string, to_number, type_of, kind);
|
2014-04-16 14:15:32 +00:00
|
|
|
return oddball;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
Handle<FixedArray> Factory::NewFixedArray(int size, PretenureFlag pretenure) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(0 <= size);
|
2011-03-18 20:35:07 +00:00
|
|
|
CALL_HEAP_FUNCTION(
|
|
|
|
isolate(),
|
|
|
|
isolate()->heap()->AllocateFixedArray(size, pretenure),
|
|
|
|
FixedArray);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-04-14 14:46:15 +00:00
|
|
|
Handle<FixedArray> Factory::NewFixedArrayWithHoles(int size,
|
|
|
|
PretenureFlag pretenure) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(0 <= size);
|
2011-03-18 20:35:07 +00:00
|
|
|
CALL_HEAP_FUNCTION(
|
|
|
|
isolate(),
|
2014-04-14 16:05:19 +00:00
|
|
|
isolate()->heap()->AllocateFixedArrayWithFiller(size,
|
|
|
|
pretenure,
|
|
|
|
*the_hole_value()),
|
2011-03-18 20:35:07 +00:00
|
|
|
FixedArray);
|
2008-10-29 10:02:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-20 13:01:08 +00:00
|
|
|
Handle<FixedArray> Factory::NewUninitializedFixedArray(int size) {
|
|
|
|
CALL_HEAP_FUNCTION(
|
|
|
|
isolate(),
|
|
|
|
isolate()->heap()->AllocateUninitializedFixedArray(size),
|
|
|
|
FixedArray);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-24 05:29:00 +00:00
|
|
|
Handle<FixedArrayBase> Factory::NewFixedDoubleArray(int size,
|
2014-04-25 16:02:50 +00:00
|
|
|
PretenureFlag pretenure) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(0 <= size);
|
2011-06-09 10:03:35 +00:00
|
|
|
CALL_HEAP_FUNCTION(
|
|
|
|
isolate(),
|
|
|
|
isolate()->heap()->AllocateUninitializedFixedDoubleArray(size, pretenure),
|
2014-04-24 05:29:00 +00:00
|
|
|
FixedArrayBase);
|
2011-06-09 10:03:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-24 05:29:00 +00:00
|
|
|
Handle<FixedArrayBase> Factory::NewFixedDoubleArrayWithHoles(
|
2014-04-14 16:05:19 +00:00
|
|
|
int size,
|
|
|
|
PretenureFlag pretenure) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(0 <= size);
|
2014-04-24 05:29:00 +00:00
|
|
|
Handle<FixedArrayBase> array = NewFixedDoubleArray(size, pretenure);
|
|
|
|
if (size > 0) {
|
|
|
|
Handle<FixedDoubleArray> double_array =
|
|
|
|
Handle<FixedDoubleArray>::cast(array);
|
|
|
|
for (int i = 0; i < size; ++i) {
|
|
|
|
double_array->set_the_hole(i);
|
|
|
|
}
|
2014-04-14 16:05:19 +00:00
|
|
|
}
|
|
|
|
return array;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-04 20:41:57 +00:00
|
|
|
Handle<OrderedHashSet> Factory::NewOrderedHashSet() {
|
2014-12-10 18:47:24 +00:00
|
|
|
return OrderedHashSet::Allocate(isolate(), OrderedHashSet::kMinCapacity);
|
2014-04-04 20:41:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Handle<OrderedHashMap> Factory::NewOrderedHashMap() {
|
2014-12-10 18:47:24 +00:00
|
|
|
return OrderedHashMap::Allocate(isolate(), OrderedHashMap::kMinCapacity);
|
2014-04-04 20:41:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-01-10 16:11:33 +00:00
|
|
|
Handle<AccessorPair> Factory::NewAccessorPair() {
|
2014-04-07 10:12:54 +00:00
|
|
|
Handle<AccessorPair> accessors =
|
|
|
|
Handle<AccessorPair>::cast(NewStruct(ACCESSOR_PAIR_TYPE));
|
|
|
|
accessors->set_getter(*the_hole_value(), SKIP_WRITE_BARRIER);
|
|
|
|
accessors->set_setter(*the_hole_value(), SKIP_WRITE_BARRIER);
|
|
|
|
return accessors;
|
2012-01-10 16:11:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-20 12:57:23 +00:00
|
|
|
Handle<TypeFeedbackInfo> Factory::NewTypeFeedbackInfo() {
|
2014-04-07 10:12:54 +00:00
|
|
|
Handle<TypeFeedbackInfo> info =
|
|
|
|
Handle<TypeFeedbackInfo>::cast(NewStruct(TYPE_FEEDBACK_INFO_TYPE));
|
|
|
|
info->initialize_storage();
|
|
|
|
return info;
|
2012-02-20 12:57:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-02-28 17:03:34 +00:00
|
|
|
// Internalized strings are created in the old generation (data space).
|
|
|
|
Handle<String> Factory::InternalizeUtf8String(Vector<const char> string) {
|
2014-01-17 10:27:57 +00:00
|
|
|
Utf8StringKey key(string, isolate()->heap()->HashSeed());
|
|
|
|
return InternalizeStringWithKey(&key);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
2013-07-05 09:52:11 +00:00
|
|
|
|
2013-02-28 17:03:34 +00:00
|
|
|
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-08-19 08:53:38 +00:00
|
|
|
SeqOneByteSubStringKey key(string, from, length);
|
2014-01-17 10:27:57 +00:00
|
|
|
return InternalizeStringWithKey(&key);
|
2011-05-24 12:16:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-02-28 17:03:34 +00:00
|
|
|
Handle<String> Factory::InternalizeTwoByteString(Vector<const uc16> string) {
|
2014-01-17 10:27:57 +00:00
|
|
|
TwoByteStringKey key(string, isolate()->heap()->HashSeed());
|
|
|
|
return InternalizeStringWithKey(&key);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<class StringTableKey>
|
|
|
|
Handle<String> Factory::InternalizeStringWithKey(StringTableKey* key) {
|
2014-04-23 15:43:39 +00:00
|
|
|
return StringTable::LookupKey(isolate(), key);
|
2010-12-22 20:14:19 +00:00
|
|
|
}
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2014-04-17 13:27:02 +00:00
|
|
|
MaybeHandle<String> Factory::NewStringFromOneByte(Vector<const uint8_t> string,
|
|
|
|
PretenureFlag pretenure) {
|
|
|
|
int length = string.length();
|
2014-06-18 13:26:02 +00:00
|
|
|
if (length == 1) return LookupSingleCharacterStringFromCode(string[0]);
|
2014-04-17 13:27:02 +00:00
|
|
|
Handle<SeqOneByteString> result;
|
|
|
|
ASSIGN_RETURN_ON_EXCEPTION(
|
2011-03-18 20:35:07 +00:00
|
|
|
isolate(),
|
2014-04-17 13:27:02 +00:00
|
|
|
result,
|
|
|
|
NewRawOneByteString(string.length(), pretenure),
|
2011-03-18 20:35:07 +00:00
|
|
|
String);
|
2014-04-17 13:27:02 +00:00
|
|
|
|
|
|
|
DisallowHeapAllocation no_gc;
|
|
|
|
// Copy the characters into the new object.
|
|
|
|
CopyChars(SeqOneByteString::cast(*result)->GetChars(),
|
|
|
|
string.start(),
|
|
|
|
length);
|
|
|
|
return result;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
2014-04-17 13:27:02 +00:00
|
|
|
MaybeHandle<String> Factory::NewStringFromUtf8(Vector<const char> string,
|
|
|
|
PretenureFlag pretenure) {
|
2014-04-16 18:28:03 +00:00
|
|
|
// Check for ASCII first since this is the common case.
|
|
|
|
const char* start = string.start();
|
|
|
|
int length = string.length();
|
|
|
|
int non_ascii_start = String::NonAsciiStart(start, length);
|
|
|
|
if (non_ascii_start >= length) {
|
|
|
|
// If the string is ASCII, we do not need to convert the characters
|
|
|
|
// since UTF8 is backwards compatible with ASCII.
|
|
|
|
return NewStringFromOneByte(Vector<const uint8_t>::cast(string), pretenure);
|
|
|
|
}
|
2014-06-18 13:26:02 +00:00
|
|
|
|
2014-04-16 18:28:03 +00:00
|
|
|
// Non-ASCII and we need to decode.
|
2014-06-18 13:26:02 +00:00
|
|
|
Access<UnicodeCache::Utf8Decoder>
|
|
|
|
decoder(isolate()->unicode_cache()->utf8_decoder());
|
|
|
|
decoder->Reset(string.start() + non_ascii_start,
|
|
|
|
length - non_ascii_start);
|
2015-02-05 07:54:24 +00:00
|
|
|
int utf16_length = static_cast<int>(decoder->Utf16Length());
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(utf16_length > 0);
|
2014-06-18 13:26:02 +00:00
|
|
|
// Allocate string.
|
|
|
|
Handle<SeqTwoByteString> result;
|
|
|
|
ASSIGN_RETURN_ON_EXCEPTION(
|
|
|
|
isolate(), result,
|
|
|
|
NewRawTwoByteString(non_ascii_start + utf16_length, pretenure),
|
2011-03-18 20:35:07 +00:00
|
|
|
String);
|
2014-09-10 12:38:12 +00:00
|
|
|
// Copy ASCII portion.
|
2014-06-18 13:26:02 +00:00
|
|
|
uint16_t* data = result->GetChars();
|
|
|
|
const char* ascii_data = string.start();
|
|
|
|
for (int i = 0; i < non_ascii_start; i++) {
|
|
|
|
*data++ = *ascii_data++;
|
|
|
|
}
|
|
|
|
// Now write the remainder.
|
|
|
|
decoder->WriteUtf16(data, utf16_length);
|
|
|
|
return result;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-17 13:27:02 +00:00
|
|
|
MaybeHandle<String> Factory::NewStringFromTwoByte(Vector<const uc16> string,
|
|
|
|
PretenureFlag pretenure) {
|
2014-06-18 13:26:02 +00:00
|
|
|
int length = string.length();
|
|
|
|
const uc16* start = string.start();
|
|
|
|
if (String::IsOneByte(start, length)) {
|
2014-08-11 14:04:37 +00:00
|
|
|
if (length == 1) return LookupSingleCharacterStringFromCode(string[0]);
|
2014-06-18 13:26:02 +00:00
|
|
|
Handle<SeqOneByteString> result;
|
|
|
|
ASSIGN_RETURN_ON_EXCEPTION(
|
|
|
|
isolate(),
|
|
|
|
result,
|
|
|
|
NewRawOneByteString(length, pretenure),
|
|
|
|
String);
|
|
|
|
CopyChars(result->GetChars(), start, length);
|
|
|
|
return result;
|
|
|
|
} else {
|
|
|
|
Handle<SeqTwoByteString> result;
|
|
|
|
ASSIGN_RETURN_ON_EXCEPTION(
|
|
|
|
isolate(),
|
|
|
|
result,
|
|
|
|
NewRawTwoByteString(length, pretenure),
|
|
|
|
String);
|
|
|
|
CopyChars(result->GetChars(), start, length);
|
|
|
|
return result;
|
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-25 13:59:33 +00:00
|
|
|
Handle<String> Factory::NewInternalizedStringFromUtf8(Vector<const char> str,
|
|
|
|
int chars,
|
|
|
|
uint32_t hash_field) {
|
|
|
|
CALL_HEAP_FUNCTION(
|
|
|
|
isolate(),
|
|
|
|
isolate()->heap()->AllocateInternalizedStringFromUtf8(
|
|
|
|
str, chars, hash_field),
|
|
|
|
String);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
MUST_USE_RESULT Handle<String> Factory::NewOneByteInternalizedString(
|
|
|
|
Vector<const uint8_t> str,
|
|
|
|
uint32_t hash_field) {
|
|
|
|
CALL_HEAP_FUNCTION(
|
|
|
|
isolate(),
|
|
|
|
isolate()->heap()->AllocateOneByteInternalizedString(str, hash_field),
|
|
|
|
String);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-08-19 08:53:38 +00:00
|
|
|
MUST_USE_RESULT Handle<String> Factory::NewOneByteInternalizedSubString(
|
|
|
|
Handle<SeqOneByteString> string, int offset, int length,
|
|
|
|
uint32_t hash_field) {
|
|
|
|
CALL_HEAP_FUNCTION(
|
|
|
|
isolate(), isolate()->heap()->AllocateOneByteInternalizedString(
|
|
|
|
Vector<const uint8_t>(string->GetChars() + offset, length),
|
|
|
|
hash_field),
|
|
|
|
String);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-25 13:59:33 +00:00
|
|
|
MUST_USE_RESULT Handle<String> Factory::NewTwoByteInternalizedString(
|
|
|
|
Vector<const uc16> str,
|
|
|
|
uint32_t hash_field) {
|
|
|
|
CALL_HEAP_FUNCTION(
|
|
|
|
isolate(),
|
|
|
|
isolate()->heap()->AllocateTwoByteInternalizedString(str, hash_field),
|
|
|
|
String);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Handle<String> Factory::NewInternalizedStringImpl(
|
2014-04-25 16:02:50 +00:00
|
|
|
Handle<String> string, int chars, uint32_t hash_field) {
|
2014-04-25 13:59:33 +00:00
|
|
|
CALL_HEAP_FUNCTION(
|
|
|
|
isolate(),
|
2014-04-25 16:02:50 +00:00
|
|
|
isolate()->heap()->AllocateInternalizedStringImpl(
|
|
|
|
*string, chars, hash_field),
|
2014-04-25 13:59:33 +00:00
|
|
|
String);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
MaybeHandle<Map> Factory::InternalizedStringMapForString(
|
|
|
|
Handle<String> string) {
|
|
|
|
// If the string is in new space it cannot be used as internalized.
|
|
|
|
if (isolate()->heap()->InNewSpace(*string)) return MaybeHandle<Map>();
|
|
|
|
|
|
|
|
// Find the corresponding internalized string map for strings.
|
|
|
|
switch (string->map()->instance_type()) {
|
|
|
|
case STRING_TYPE: return internalized_string_map();
|
2014-09-10 12:38:12 +00:00
|
|
|
case ONE_BYTE_STRING_TYPE:
|
|
|
|
return one_byte_internalized_string_map();
|
2014-04-25 13:59:33 +00:00
|
|
|
case EXTERNAL_STRING_TYPE: return external_internalized_string_map();
|
2014-09-10 12:38:12 +00:00
|
|
|
case EXTERNAL_ONE_BYTE_STRING_TYPE:
|
|
|
|
return external_one_byte_internalized_string_map();
|
2014-04-25 13:59:33 +00:00
|
|
|
case EXTERNAL_STRING_WITH_ONE_BYTE_DATA_TYPE:
|
|
|
|
return external_internalized_string_with_one_byte_data_map();
|
|
|
|
case SHORT_EXTERNAL_STRING_TYPE:
|
|
|
|
return short_external_internalized_string_map();
|
2014-09-10 12:38:12 +00:00
|
|
|
case SHORT_EXTERNAL_ONE_BYTE_STRING_TYPE:
|
|
|
|
return short_external_one_byte_internalized_string_map();
|
2014-04-25 13:59:33 +00:00
|
|
|
case SHORT_EXTERNAL_STRING_WITH_ONE_BYTE_DATA_TYPE:
|
|
|
|
return short_external_internalized_string_with_one_byte_data_map();
|
|
|
|
default: return MaybeHandle<Map>(); // No match found.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-03 12:30:08 +00:00
|
|
|
MaybeHandle<SeqOneByteString> Factory::NewRawOneByteString(
|
|
|
|
int length, PretenureFlag pretenure) {
|
2014-06-18 13:26:02 +00:00
|
|
|
if (length > String::kMaxLength || length < 0) {
|
2014-09-01 09:11:44 +00:00
|
|
|
THROW_NEW_ERROR(isolate(), NewInvalidStringLengthError(), SeqOneByteString);
|
2014-06-18 13:26:02 +00:00
|
|
|
}
|
2011-03-18 20:35:07 +00:00
|
|
|
CALL_HEAP_FUNCTION(
|
|
|
|
isolate(),
|
2012-11-21 10:01:05 +00:00
|
|
|
isolate()->heap()->AllocateRawOneByteString(length, pretenure),
|
2012-11-15 13:31:27 +00:00
|
|
|
SeqOneByteString);
|
2010-07-05 12:49:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-03 12:30:08 +00:00
|
|
|
MaybeHandle<SeqTwoByteString> Factory::NewRawTwoByteString(
|
|
|
|
int length, PretenureFlag pretenure) {
|
2014-06-18 13:26:02 +00:00
|
|
|
if (length > String::kMaxLength || length < 0) {
|
2014-09-01 09:11:44 +00:00
|
|
|
THROW_NEW_ERROR(isolate(), NewInvalidStringLengthError(), SeqTwoByteString);
|
2014-06-18 13:26:02 +00:00
|
|
|
}
|
2011-03-18 20:35:07 +00:00
|
|
|
CALL_HEAP_FUNCTION(
|
|
|
|
isolate(),
|
|
|
|
isolate()->heap()->AllocateRawTwoByteString(length, pretenure),
|
2011-06-27 09:02:34 +00:00
|
|
|
SeqTwoByteString);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-23 15:43:39 +00:00
|
|
|
Handle<String> Factory::LookupSingleCharacterStringFromCode(uint32_t code) {
|
|
|
|
if (code <= String::kMaxOneByteCharCodeU) {
|
|
|
|
{
|
|
|
|
DisallowHeapAllocation no_allocation;
|
|
|
|
Object* value = single_character_string_cache()->get(code);
|
|
|
|
if (value != *undefined_value()) {
|
|
|
|
return handle(String::cast(value), isolate());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
uint8_t buffer[1];
|
|
|
|
buffer[0] = static_cast<uint8_t>(code);
|
|
|
|
Handle<String> result =
|
|
|
|
InternalizeOneByteString(Vector<const uint8_t>(buffer, 1));
|
|
|
|
single_character_string_cache()->set(code, *result);
|
|
|
|
return result;
|
|
|
|
}
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(code <= String::kMaxUtf16CodeUnitU);
|
2014-04-23 15:43:39 +00:00
|
|
|
|
|
|
|
Handle<SeqTwoByteString> result = NewRawTwoByteString(1).ToHandleChecked();
|
|
|
|
result->SeqTwoByteStringSet(0, static_cast<uint16_t>(code));
|
|
|
|
return result;
|
2014-04-08 06:45:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-12-23 12:37:56 +00:00
|
|
|
// Returns true for a character in a range. Both limits are inclusive.
|
|
|
|
static inline bool Between(uint32_t character, uint32_t from, uint32_t to) {
|
|
|
|
// This makes uses of the the unsigned wraparound.
|
|
|
|
return character - from <= to - from;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline Handle<String> MakeOrFindTwoCharacterString(Isolate* isolate,
|
|
|
|
uint16_t c1,
|
|
|
|
uint16_t c2) {
|
|
|
|
// Numeric strings have a different hash algorithm not known by
|
|
|
|
// LookupTwoCharsStringIfExists, so we skip this step for such strings.
|
|
|
|
if (!Between(c1, '0', '9') || !Between(c2, '0', '9')) {
|
2014-04-30 17:27:40 +00:00
|
|
|
Handle<String> result;
|
|
|
|
if (StringTable::LookupTwoCharsStringIfExists(isolate, c1, c2).
|
|
|
|
ToHandle(&result)) {
|
|
|
|
return result;
|
2013-12-23 12:37:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now we know the length is 2, we might as well make use of that fact
|
|
|
|
// when building the new string.
|
|
|
|
if (static_cast<unsigned>(c1 | c2) <= String::kMaxOneByteCharCodeU) {
|
|
|
|
// We can do this.
|
2014-09-02 13:36:35 +00:00
|
|
|
DCHECK(base::bits::IsPowerOfTwo32(String::kMaxOneByteCharCodeU +
|
|
|
|
1)); // because of this.
|
2014-04-03 12:30:08 +00:00
|
|
|
Handle<SeqOneByteString> str =
|
|
|
|
isolate->factory()->NewRawOneByteString(2).ToHandleChecked();
|
2013-12-23 12:37:56 +00:00
|
|
|
uint8_t* dest = str->GetChars();
|
|
|
|
dest[0] = static_cast<uint8_t>(c1);
|
|
|
|
dest[1] = static_cast<uint8_t>(c2);
|
|
|
|
return str;
|
|
|
|
} else {
|
2014-04-03 12:30:08 +00:00
|
|
|
Handle<SeqTwoByteString> str =
|
|
|
|
isolate->factory()->NewRawTwoByteString(2).ToHandleChecked();
|
2013-12-23 12:37:56 +00:00
|
|
|
uc16* dest = str->GetChars();
|
|
|
|
dest[0] = c1;
|
|
|
|
dest[1] = c2;
|
|
|
|
return str;
|
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-21 09:24:30 +00:00
|
|
|
template<typename SinkChar, typename StringType>
|
|
|
|
Handle<String> ConcatStringContent(Handle<StringType> result,
|
|
|
|
Handle<String> first,
|
|
|
|
Handle<String> second) {
|
|
|
|
DisallowHeapAllocation pointer_stays_valid;
|
|
|
|
SinkChar* sink = result->GetChars();
|
|
|
|
String::WriteToFlat(*first, sink, 0, first->length());
|
|
|
|
String::WriteToFlat(*second, sink + first->length(), 0, second->length());
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-03 12:30:37 +00:00
|
|
|
MaybeHandle<String> Factory::NewConsString(Handle<String> left,
|
|
|
|
Handle<String> right) {
|
2013-12-23 12:37:56 +00:00
|
|
|
int left_length = left->length();
|
|
|
|
if (left_length == 0) return right;
|
|
|
|
int right_length = right->length();
|
|
|
|
if (right_length == 0) return left;
|
|
|
|
|
|
|
|
int length = left_length + right_length;
|
|
|
|
|
|
|
|
if (length == 2) {
|
|
|
|
uint16_t c1 = left->Get(0);
|
|
|
|
uint16_t c2 = right->Get(0);
|
|
|
|
return MakeOrFindTwoCharacterString(isolate(), c1, c2);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure that an out of memory exception is thrown if the length
|
|
|
|
// of the new cons string is too large.
|
|
|
|
if (length > String::kMaxLength || length < 0) {
|
2014-09-01 09:11:44 +00:00
|
|
|
THROW_NEW_ERROR(isolate(), NewInvalidStringLengthError(), String);
|
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
|
2014-09-10 12:38:12 +00:00
|
|
|
// can't use the fast case code for short one-byte strings below, but
|
|
|
|
// we can try to save memory if all chars actually fit in one-byte.
|
2013-12-23 12:37:56 +00:00
|
|
|
is_one_byte_data_in_two_byte_string =
|
|
|
|
left->HasOnlyOneByteChars() && right->HasOnlyOneByteChars();
|
|
|
|
if (is_one_byte_data_in_two_byte_string) {
|
2014-09-10 12:38:12 +00:00
|
|
|
isolate()->counters()->string_add_runtime_ext_to_one_byte()->Increment();
|
2013-12-23 12:37:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the resulting string is small make a flat string.
|
|
|
|
if (length < ConsString::kMinLength) {
|
|
|
|
// Note that neither of the two inputs can be a slice because:
|
|
|
|
STATIC_ASSERT(ConsString::kMinLength <= SlicedString::kMinLength);
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(left->IsFlat());
|
|
|
|
DCHECK(right->IsFlat());
|
2013-12-23 12:37:56 +00:00
|
|
|
|
2014-03-25 09:09:24 +00:00
|
|
|
STATIC_ASSERT(ConsString::kMinLength <= String::kMaxLength);
|
2013-12-23 12:37:56 +00:00
|
|
|
if (is_one_byte) {
|
2014-04-03 12:30:08 +00:00
|
|
|
Handle<SeqOneByteString> result =
|
|
|
|
NewRawOneByteString(length).ToHandleChecked();
|
2013-12-23 12:37:56 +00:00
|
|
|
DisallowHeapAllocation no_gc;
|
|
|
|
uint8_t* dest = result->GetChars();
|
|
|
|
// Copy left part.
|
2014-09-10 12:38:12 +00:00
|
|
|
const uint8_t* src =
|
|
|
|
left->IsExternalString()
|
|
|
|
? Handle<ExternalOneByteString>::cast(left)->GetChars()
|
|
|
|
: Handle<SeqOneByteString>::cast(left)->GetChars();
|
2013-12-23 12:37:56 +00:00
|
|
|
for (int i = 0; i < left_length; i++) *dest++ = src[i];
|
|
|
|
// Copy right part.
|
|
|
|
src = right->IsExternalString()
|
2014-09-10 12:38:12 +00:00
|
|
|
? Handle<ExternalOneByteString>::cast(right)->GetChars()
|
|
|
|
: Handle<SeqOneByteString>::cast(right)->GetChars();
|
2013-12-23 12:37:56 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-09-29 08:08:14 +00:00
|
|
|
Handle<ConsString> result =
|
|
|
|
(is_one_byte || is_one_byte_data_in_two_byte_string)
|
|
|
|
? New<ConsString>(cons_one_byte_string_map(), NEW_SPACE)
|
|
|
|
: New<ConsString>(cons_string_map(), NEW_SPACE);
|
2013-12-23 12:37:56 +00:00
|
|
|
|
|
|
|
DisallowHeapAllocation no_gc;
|
|
|
|
WriteBarrierMode mode = result->GetWriteBarrierMode(no_gc);
|
|
|
|
|
|
|
|
result->set_hash_field(String::kEmptyHashField);
|
|
|
|
result->set_length(length);
|
|
|
|
result->set_first(*left, mode);
|
|
|
|
result->set_second(*right, mode);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-06-27 09:02:34 +00:00
|
|
|
Handle<String> Factory::NewProperSubString(Handle<String> str,
|
2011-06-22 14:20:23 +00:00
|
|
|
int begin,
|
|
|
|
int end) {
|
2013-12-23 12:37:56 +00:00
|
|
|
#if VERIFY_HEAP
|
|
|
|
if (FLAG_verify_heap) str->StringVerify();
|
|
|
|
#endif
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(begin > 0 || end < str->length());
|
2013-12-23 12:37:56 +00:00
|
|
|
|
2014-04-22 11:19:27 +00:00
|
|
|
str = String::Flatten(str);
|
|
|
|
|
2013-12-23 12:37:56 +00:00
|
|
|
int length = end - begin;
|
|
|
|
if (length <= 0) return empty_string();
|
|
|
|
if (length == 1) {
|
2014-04-08 06:45:53 +00:00
|
|
|
return LookupSingleCharacterStringFromCode(str->Get(begin));
|
2013-12-23 12:37:56 +00:00
|
|
|
}
|
|
|
|
if (length == 2) {
|
|
|
|
// Optimization for 2-byte strings often used as keys in a decompression
|
|
|
|
// dictionary. Check whether we already have the string in the string
|
|
|
|
// table to prevent creation of many unnecessary strings.
|
|
|
|
uint16_t c1 = str->Get(begin);
|
|
|
|
uint16_t c2 = str->Get(begin + 1);
|
|
|
|
return MakeOrFindTwoCharacterString(isolate(), c1, c2);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!FLAG_string_slices || length < SlicedString::kMinLength) {
|
|
|
|
if (str->IsOneByteRepresentation()) {
|
2014-04-03 12:30:08 +00:00
|
|
|
Handle<SeqOneByteString> result =
|
|
|
|
NewRawOneByteString(length).ToHandleChecked();
|
2013-12-23 12:37:56 +00:00
|
|
|
uint8_t* dest = result->GetChars();
|
|
|
|
DisallowHeapAllocation no_gc;
|
|
|
|
String::WriteToFlat(*str, dest, begin, end);
|
|
|
|
return result;
|
|
|
|
} else {
|
2014-04-03 12:30:08 +00:00
|
|
|
Handle<SeqTwoByteString> result =
|
|
|
|
NewRawTwoByteString(length).ToHandleChecked();
|
2013-12-23 12:37:56 +00:00
|
|
|
uc16* dest = result->GetChars();
|
|
|
|
DisallowHeapAllocation no_gc;
|
|
|
|
String::WriteToFlat(*str, dest, begin, end);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int offset = begin;
|
|
|
|
|
|
|
|
if (str->IsSlicedString()) {
|
|
|
|
Handle<SlicedString> slice = Handle<SlicedString>::cast(str);
|
|
|
|
str = Handle<String>(slice->parent(), isolate());
|
|
|
|
offset += slice->offset();
|
|
|
|
}
|
|
|
|
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(str->IsSeqString() || str->IsExternalString());
|
2014-09-10 12:38:12 +00:00
|
|
|
Handle<Map> map = str->IsOneByteRepresentation()
|
|
|
|
? sliced_one_byte_string_map()
|
|
|
|
: sliced_string_map();
|
2014-04-29 07:02:11 +00:00
|
|
|
Handle<SlicedString> slice = New<SlicedString>(map, NEW_SPACE);
|
2013-12-23 12:37:56 +00:00
|
|
|
|
|
|
|
slice->set_hash_field(String::kEmptyHashField);
|
|
|
|
slice->set_length(length);
|
|
|
|
slice->set_parent(*str);
|
|
|
|
slice->set_offset(offset);
|
|
|
|
return slice;
|
2011-06-22 14:20:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-09-10 12:38:12 +00:00
|
|
|
MaybeHandle<String> Factory::NewExternalStringFromOneByte(
|
|
|
|
const ExternalOneByteString::Resource* resource) {
|
2014-04-16 13:35:36 +00:00
|
|
|
size_t length = resource->length();
|
|
|
|
if (length > static_cast<size_t>(String::kMaxLength)) {
|
2014-09-01 09:11:44 +00:00
|
|
|
THROW_NEW_ERROR(isolate(), NewInvalidStringLengthError(), String);
|
2014-04-16 13:35:36 +00:00
|
|
|
}
|
|
|
|
|
2016-01-12 06:28:09 +00:00
|
|
|
Handle<Map> map;
|
|
|
|
if (resource->IsCompressible()) {
|
|
|
|
// TODO(hajimehoshi): Rename this to 'uncached_external_one_byte_string_map'
|
|
|
|
map = short_external_one_byte_string_map();
|
|
|
|
} else {
|
|
|
|
map = external_one_byte_string_map();
|
|
|
|
}
|
2014-09-10 12:38:12 +00:00
|
|
|
Handle<ExternalOneByteString> external_string =
|
|
|
|
New<ExternalOneByteString>(map, NEW_SPACE);
|
2014-04-16 13:35:36 +00:00
|
|
|
external_string->set_length(static_cast<int>(length));
|
|
|
|
external_string->set_hash_field(String::kEmptyHashField);
|
|
|
|
external_string->set_resource(resource);
|
|
|
|
|
|
|
|
return external_string;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-03 12:41:37 +00:00
|
|
|
MaybeHandle<String> Factory::NewExternalStringFromTwoByte(
|
2011-09-21 13:28:09 +00:00
|
|
|
const ExternalTwoByteString::Resource* resource) {
|
2014-04-16 13:35:36 +00:00
|
|
|
size_t length = resource->length();
|
|
|
|
if (length > static_cast<size_t>(String::kMaxLength)) {
|
2014-09-01 09:11:44 +00:00
|
|
|
THROW_NEW_ERROR(isolate(), NewInvalidStringLengthError(), String);
|
2014-04-16 13:35:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// For small strings we check whether the resource contains only
|
|
|
|
// one byte characters. If yes, we use a different string map.
|
|
|
|
static const size_t kOneByteCheckLengthLimit = 32;
|
|
|
|
bool is_one_byte = length <= kOneByteCheckLengthLimit &&
|
|
|
|
String::IsOneByte(resource->data(), static_cast<int>(length));
|
2016-01-12 06:28:09 +00:00
|
|
|
Handle<Map> map;
|
|
|
|
if (resource->IsCompressible()) {
|
|
|
|
// TODO(hajimehoshi): Rename these to 'uncached_external_string_...'.
|
|
|
|
map = is_one_byte ? short_external_string_with_one_byte_data_map()
|
|
|
|
: short_external_string_map();
|
|
|
|
} else {
|
|
|
|
map = is_one_byte ? external_string_with_one_byte_data_map()
|
|
|
|
: external_string_map();
|
|
|
|
}
|
2014-04-16 13:35:36 +00:00
|
|
|
Handle<ExternalTwoByteString> external_string =
|
|
|
|
New<ExternalTwoByteString>(map, NEW_SPACE);
|
|
|
|
external_string->set_length(static_cast<int>(length));
|
|
|
|
external_string->set_hash_field(String::kEmptyHashField);
|
|
|
|
external_string->set_resource(resource);
|
|
|
|
|
|
|
|
return external_string;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-03-01 10:34:31 +00:00
|
|
|
Handle<Symbol> Factory::NewSymbol() {
|
|
|
|
CALL_HEAP_FUNCTION(
|
|
|
|
isolate(),
|
|
|
|
isolate()->heap()->AllocateSymbol(),
|
|
|
|
Symbol);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-10-29 14:17:24 +00:00
|
|
|
Handle<Symbol> Factory::NewPrivateSymbol() {
|
2014-04-29 13:58:55 +00:00
|
|
|
Handle<Symbol> symbol = NewSymbol();
|
|
|
|
symbol->set_is_private(true);
|
2014-08-12 15:28:20 +00:00
|
|
|
return symbol;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-08-17 09:03:08 +00:00
|
|
|
Handle<Context> Factory::NewNativeContext() {
|
2015-07-06 16:36:28 +00:00
|
|
|
Handle<FixedArray> array =
|
|
|
|
NewFixedArray(Context::NATIVE_CONTEXT_SLOTS, TENURED);
|
2014-04-07 10:12:54 +00:00
|
|
|
array->set_map_no_write_barrier(*native_context_map());
|
|
|
|
Handle<Context> context = Handle<Context>::cast(array);
|
2015-11-27 16:59:28 +00:00
|
|
|
context->set_native_context(*context);
|
2015-11-06 08:07:52 +00:00
|
|
|
context->set_errors_thrown(Smi::FromInt(0));
|
2015-12-09 08:54:47 +00:00
|
|
|
Handle<WeakCell> weak_cell = NewWeakCell(context);
|
|
|
|
context->set_self_weak_cell(*weak_cell);
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(context->IsNativeContext());
|
2014-04-07 10:12:54 +00:00
|
|
|
return context;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-11-12 11:34:09 +00:00
|
|
|
Handle<Context> Factory::NewScriptContext(Handle<JSFunction> function,
|
2012-08-27 09:40:26 +00:00
|
|
|
Handle<ScopeInfo> scope_info) {
|
2014-04-09 08:51:46 +00:00
|
|
|
Handle<FixedArray> array =
|
|
|
|
NewFixedArray(scope_info->ContextLength(), TENURED);
|
2014-11-12 11:34:09 +00:00
|
|
|
array->set_map_no_write_barrier(*script_context_map());
|
2014-04-09 08:51:46 +00:00
|
|
|
Handle<Context> context = Handle<Context>::cast(array);
|
|
|
|
context->set_closure(*function);
|
|
|
|
context->set_previous(function->context());
|
|
|
|
context->set_extension(*scope_info);
|
2015-11-27 16:59:28 +00:00
|
|
|
context->set_native_context(function->native_context());
|
2014-11-12 11:34:09 +00:00
|
|
|
DCHECK(context->IsScriptContext());
|
2014-04-09 08:51:46 +00:00
|
|
|
return context;
|
2012-08-27 09:40:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-11-12 11:34:09 +00:00
|
|
|
Handle<ScriptContextTable> Factory::NewScriptContextTable() {
|
2014-11-07 16:29:13 +00:00
|
|
|
Handle<FixedArray> array = NewFixedArray(1);
|
2014-11-12 11:34:09 +00:00
|
|
|
array->set_map_no_write_barrier(*script_context_table_map());
|
|
|
|
Handle<ScriptContextTable> context_table =
|
|
|
|
Handle<ScriptContextTable>::cast(array);
|
2014-11-07 16:29:13 +00:00
|
|
|
context_table->set_used(0);
|
|
|
|
return context_table;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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);
|
2015-11-30 13:23:04 +00:00
|
|
|
context->set_extension(*the_hole_value());
|
2014-04-07 10:12:54 +00:00
|
|
|
return context;
|
2012-04-16 14:43:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
Handle<Context> Factory::NewFunctionContext(int length,
|
2011-06-29 07:41:42 +00:00
|
|
|
Handle<JSFunction> function) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(length >= Context::MIN_CONTEXT_SLOTS);
|
2014-04-09 08:51:46 +00:00
|
|
|
Handle<FixedArray> array = NewFixedArray(length);
|
|
|
|
array->set_map_no_write_barrier(*function_context_map());
|
|
|
|
Handle<Context> context = Handle<Context>::cast(array);
|
|
|
|
context->set_closure(*function);
|
|
|
|
context->set_previous(function->context());
|
2015-11-30 13:23:04 +00:00
|
|
|
context->set_extension(*the_hole_value());
|
2015-11-27 16:59:28 +00:00
|
|
|
context->set_native_context(function->native_context());
|
2014-04-09 08:51:46 +00:00
|
|
|
return context;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-06-29 07:41:42 +00:00
|
|
|
Handle<Context> Factory::NewCatchContext(Handle<JSFunction> function,
|
|
|
|
Handle<Context> previous,
|
2011-06-16 06:37:49 +00:00
|
|
|
Handle<String> name,
|
|
|
|
Handle<Object> thrown_object) {
|
2014-04-09 08:51:46 +00:00
|
|
|
STATIC_ASSERT(Context::MIN_CONTEXT_SLOTS == Context::THROWN_OBJECT_INDEX);
|
|
|
|
Handle<FixedArray> array = NewFixedArray(Context::MIN_CONTEXT_SLOTS + 1);
|
|
|
|
array->set_map_no_write_barrier(*catch_context_map());
|
|
|
|
Handle<Context> context = Handle<Context>::cast(array);
|
|
|
|
context->set_closure(*function);
|
|
|
|
context->set_previous(*previous);
|
|
|
|
context->set_extension(*name);
|
2015-11-27 16:59:28 +00:00
|
|
|
context->set_native_context(previous->native_context());
|
2014-04-09 08:51:46 +00:00
|
|
|
context->set(Context::THROWN_OBJECT_INDEX, *thrown_object);
|
|
|
|
return context;
|
2011-06-09 11:26:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-06-29 07:41:42 +00:00
|
|
|
Handle<Context> Factory::NewWithContext(Handle<JSFunction> function,
|
|
|
|
Handle<Context> previous,
|
2014-04-09 08:51:46 +00:00
|
|
|
Handle<JSReceiver> extension) {
|
|
|
|
Handle<FixedArray> array = NewFixedArray(Context::MIN_CONTEXT_SLOTS);
|
|
|
|
array->set_map_no_write_barrier(*with_context_map());
|
|
|
|
Handle<Context> context = Handle<Context>::cast(array);
|
|
|
|
context->set_closure(*function);
|
|
|
|
context->set_previous(*previous);
|
|
|
|
context->set_extension(*extension);
|
2015-11-27 16:59:28 +00:00
|
|
|
context->set_native_context(previous->native_context());
|
2014-04-09 08:51:46 +00:00
|
|
|
return context;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-04-16 14:43:27 +00:00
|
|
|
Handle<Context> Factory::NewBlockContext(Handle<JSFunction> function,
|
|
|
|
Handle<Context> previous,
|
|
|
|
Handle<ScopeInfo> scope_info) {
|
2016-01-14 18:04:00 +00:00
|
|
|
Handle<FixedArray> array = NewFixedArray(scope_info->ContextLength());
|
2014-04-09 08:51:46 +00:00
|
|
|
array->set_map_no_write_barrier(*block_context_map());
|
|
|
|
Handle<Context> context = Handle<Context>::cast(array);
|
|
|
|
context->set_closure(*function);
|
|
|
|
context->set_previous(*previous);
|
|
|
|
context->set_extension(*scope_info);
|
2015-11-27 16:59:28 +00:00
|
|
|
context->set_native_context(previous->native_context());
|
2014-04-09 08:51:46 +00:00
|
|
|
return context;
|
2011-08-11 16:29:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
Handle<Struct> Factory::NewStruct(InstanceType type) {
|
2011-03-18 20:35:07 +00:00
|
|
|
CALL_HEAP_FUNCTION(
|
|
|
|
isolate(),
|
|
|
|
isolate()->heap()->AllocateStruct(type),
|
|
|
|
Struct);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-16 12:10:56 +00:00
|
|
|
Handle<CodeCache> Factory::NewCodeCache() {
|
2014-04-16 14:04:54 +00:00
|
|
|
Handle<CodeCache> code_cache =
|
|
|
|
Handle<CodeCache>::cast(NewStruct(CODE_CACHE_TYPE));
|
|
|
|
code_cache->set_default_cache(*empty_fixed_array(), SKIP_WRITE_BARRIER);
|
|
|
|
code_cache->set_normal_type_cache(*undefined_value(), SKIP_WRITE_BARRIER);
|
|
|
|
return code_cache;
|
2014-04-16 12:10:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-11-14 17:30:48 +00:00
|
|
|
Handle<AliasedArgumentsEntry> Factory::NewAliasedArgumentsEntry(
|
|
|
|
int aliased_context_slot) {
|
|
|
|
Handle<AliasedArgumentsEntry> entry = Handle<AliasedArgumentsEntry>::cast(
|
|
|
|
NewStruct(ALIASED_ARGUMENTS_ENTRY_TYPE));
|
|
|
|
entry->set_aliased_context_slot(aliased_context_slot);
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-01-18 15:08:36 +00:00
|
|
|
Handle<AccessorInfo> Factory::NewAccessorInfo() {
|
|
|
|
Handle<AccessorInfo> info =
|
|
|
|
Handle<AccessorInfo>::cast(NewStruct(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
|
|
|
// Create and initialize script object.
|
2015-03-26 09:50:34 +00:00
|
|
|
Heap* heap = isolate()->heap();
|
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());
|
2015-03-26 09:50:34 +00:00
|
|
|
script->set_id(isolate()->heap()->NextScriptId());
|
2015-09-28 13:10:13 +00:00
|
|
|
script->set_line_offset(0);
|
|
|
|
script->set_column_offset(0);
|
2011-03-18 20:35:07 +00:00
|
|
|
script->set_context_data(heap->undefined_value());
|
2015-09-28 13:10:13 +00:00
|
|
|
script->set_type(Script::TYPE_NORMAL);
|
2014-10-15 10:11:08 +00:00
|
|
|
script->set_wrapper(heap->undefined_value());
|
2011-03-18 20:35:07 +00:00
|
|
|
script->set_line_ends(heap->undefined_value());
|
|
|
|
script->set_eval_from_shared(heap->undefined_value());
|
2015-09-28 13:10:13 +00:00
|
|
|
script->set_eval_from_instructions_offset(0);
|
2015-06-25 12:19:55 +00:00
|
|
|
script->set_shared_function_infos(Smi::FromInt(0));
|
2015-09-28 13:10:13 +00:00
|
|
|
script->set_flags(0);
|
2009-03-10 12:05:20 +00:00
|
|
|
|
2015-08-20 08:04:41 +00:00
|
|
|
heap->set_script_list(*WeakFixedArray::Add(script_list(), script));
|
2008-07-03 15:10:15 +00:00
|
|
|
return script;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-19 11:47:34 +00:00
|
|
|
Handle<Foreign> Factory::NewForeign(Address addr, PretenureFlag pretenure) {
|
2011-03-18 20:35:07 +00:00
|
|
|
CALL_HEAP_FUNCTION(isolate(),
|
2011-05-19 11:47:34 +00:00
|
|
|
isolate()->heap()->AllocateForeign(addr, pretenure),
|
|
|
|
Foreign);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-19 11:47:34 +00:00
|
|
|
Handle<Foreign> Factory::NewForeign(const AccessorDescriptor* desc) {
|
|
|
|
return NewForeign((Address) desc, TENURED);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-11-25 11:07:48 +00:00
|
|
|
Handle<ByteArray> Factory::NewByteArray(int length, PretenureFlag pretenure) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(0 <= length);
|
2011-03-18 20:35:07 +00:00
|
|
|
CALL_HEAP_FUNCTION(
|
|
|
|
isolate(),
|
|
|
|
isolate()->heap()->AllocateByteArray(length, pretenure),
|
|
|
|
ByteArray);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-08-27 11:11:09 +00:00
|
|
|
Handle<BytecodeArray> Factory::NewBytecodeArray(
|
|
|
|
int length, const byte* raw_bytecodes, int frame_size, int parameter_count,
|
|
|
|
Handle<FixedArray> constant_pool) {
|
2015-07-24 12:02:41 +00:00
|
|
|
DCHECK(0 <= length);
|
2015-08-27 11:11:09 +00:00
|
|
|
CALL_HEAP_FUNCTION(isolate(), isolate()->heap()->AllocateBytecodeArray(
|
|
|
|
length, raw_bytecodes, frame_size,
|
|
|
|
parameter_count, *constant_pool),
|
2015-07-24 12:02:41 +00:00
|
|
|
BytecodeArray);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-07-28 09:29:34 +00:00
|
|
|
Handle<FixedTypedArrayBase> Factory::NewFixedTypedArrayWithExternalPointer(
|
|
|
|
int length, ExternalArrayType array_type, void* external_pointer,
|
|
|
|
PretenureFlag pretenure) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(0 <= length && length <= Smi::kMaxValue);
|
2011-03-18 20:35:07 +00:00
|
|
|
CALL_HEAP_FUNCTION(
|
2015-07-28 09:29:34 +00:00
|
|
|
isolate(), isolate()->heap()->AllocateFixedTypedArrayWithExternalPointer(
|
|
|
|
length, array_type, external_pointer, pretenure),
|
|
|
|
FixedTypedArrayBase);
|
2009-10-20 15:26:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-01-16 17:08:45 +00:00
|
|
|
Handle<FixedTypedArrayBase> Factory::NewFixedTypedArray(
|
2015-06-03 07:41:28 +00:00
|
|
|
int length, ExternalArrayType array_type, bool initialize,
|
2014-01-16 17:08:45 +00:00
|
|
|
PretenureFlag pretenure) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(0 <= length && length <= Smi::kMaxValue);
|
2015-06-03 07:41:28 +00:00
|
|
|
CALL_HEAP_FUNCTION(isolate(), isolate()->heap()->AllocateFixedTypedArray(
|
|
|
|
length, array_type, initialize, pretenure),
|
|
|
|
FixedTypedArrayBase);
|
2014-01-16 17:08:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-17 13:27:25 +00:00
|
|
|
Handle<PropertyCell> Factory::NewPropertyCell() {
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-10-14 14:43:45 +00:00
|
|
|
Handle<WeakCell> Factory::NewWeakCell(Handle<HeapObject> value) {
|
2015-04-29 08:36:05 +00:00
|
|
|
// It is safe to dereference the value because we are embedding it
|
|
|
|
// in cell and not inspecting its fields.
|
2014-10-14 14:43:45 +00:00
|
|
|
AllowDeferredHandleDereference convert_to_cell;
|
|
|
|
CALL_HEAP_FUNCTION(isolate(), isolate()->heap()->AllocateWeakCell(*value),
|
|
|
|
WeakCell);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-03 13:18:19 +00:00
|
|
|
Handle<TransitionArray> Factory::NewTransitionArray(int capacity) {
|
|
|
|
CALL_HEAP_FUNCTION(isolate(),
|
|
|
|
isolate()->heap()->AllocateTransitionArray(capacity),
|
|
|
|
TransitionArray);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-08 10:02:16 +00:00
|
|
|
Handle<AllocationSite> Factory::NewAllocationSite() {
|
2014-04-16 12:54:29 +00:00
|
|
|
Handle<Map> map = allocation_site_map();
|
2015-04-07 11:31:57 +00:00
|
|
|
Handle<AllocationSite> site = New<AllocationSite>(map, OLD_SPACE);
|
2014-04-16 12:54:29 +00:00
|
|
|
site->Initialize();
|
|
|
|
|
|
|
|
// Link the site
|
|
|
|
site->set_weak_next(isolate()->heap()->allocation_sites_list());
|
|
|
|
isolate()->heap()->set_allocation_sites_list(*site);
|
|
|
|
return site;
|
2013-07-08 10:02:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-22 11:30:04 +00:00
|
|
|
Handle<Map> Factory::NewMap(InstanceType type,
|
|
|
|
int instance_size,
|
|
|
|
ElementsKind elements_kind) {
|
2011-03-18 20:35:07 +00:00
|
|
|
CALL_HEAP_FUNCTION(
|
|
|
|
isolate(),
|
2011-09-22 11:30:04 +00:00
|
|
|
isolate()->heap()->AllocateMap(type, instance_size, elements_kind),
|
2011-03-18 20:35:07 +00:00
|
|
|
Map);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-29 07:02:11 +00:00
|
|
|
Handle<JSObject> Factory::CopyJSObject(Handle<JSObject> object) {
|
|
|
|
CALL_HEAP_FUNCTION(isolate(),
|
|
|
|
isolate()->heap()->CopyJSObject(*object, NULL),
|
|
|
|
JSObject);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Handle<JSObject> Factory::CopyJSObjectWithAllocationSite(
|
|
|
|
Handle<JSObject> object,
|
|
|
|
Handle<AllocationSite> site) {
|
|
|
|
CALL_HEAP_FUNCTION(isolate(),
|
|
|
|
isolate()->heap()->CopyJSObject(
|
|
|
|
*object,
|
|
|
|
site.is_null() ? NULL : *site),
|
|
|
|
JSObject);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-28 15:33:16 +00:00
|
|
|
Handle<FixedArray> Factory::CopyFixedArrayWithMap(Handle<FixedArray> array,
|
|
|
|
Handle<Map> map) {
|
|
|
|
CALL_HEAP_FUNCTION(isolate(),
|
|
|
|
isolate()->heap()->CopyFixedArrayWithMap(*array, *map),
|
|
|
|
FixedArray);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-08-04 17:48:42 +00:00
|
|
|
Handle<FixedArray> Factory::CopyFixedArrayAndGrow(Handle<FixedArray> array,
|
2015-08-05 08:53:17 +00:00
|
|
|
int grow_by,
|
|
|
|
PretenureFlag pretenure) {
|
|
|
|
CALL_HEAP_FUNCTION(isolate(), isolate()->heap()->CopyFixedArrayAndGrow(
|
|
|
|
*array, grow_by, pretenure),
|
2015-08-04 17:48:42 +00:00
|
|
|
FixedArray);
|
|
|
|
}
|
|
|
|
|
2016-02-11 18:58:49 +00:00
|
|
|
Handle<FixedArray> Factory::CopyFixedArrayUpTo(Handle<FixedArray> array,
|
|
|
|
int new_len,
|
|
|
|
PretenureFlag pretenure) {
|
|
|
|
CALL_HEAP_FUNCTION(isolate(), isolate()->heap()->CopyFixedArrayUpTo(
|
|
|
|
*array, new_len, pretenure),
|
|
|
|
FixedArray);
|
|
|
|
}
|
2015-08-04 17:48:42 +00:00
|
|
|
|
2014-04-28 15:33:16 +00:00
|
|
|
Handle<FixedArray> Factory::CopyFixedArray(Handle<FixedArray> array) {
|
2014-03-17 13:42:37 +00:00
|
|
|
CALL_HEAP_FUNCTION(isolate(),
|
2014-04-28 15:33:16 +00:00
|
|
|
isolate()->heap()->CopyFixedArray(*array),
|
2014-03-17 13:42:37 +00:00
|
|
|
FixedArray);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-28 15:33:16 +00:00
|
|
|
Handle<FixedArray> Factory::CopyAndTenureFixedCOWArray(
|
|
|
|
Handle<FixedArray> array) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(isolate()->heap()->InNewSpace(*array));
|
2013-10-04 07:25:24 +00:00
|
|
|
CALL_HEAP_FUNCTION(isolate(),
|
2014-04-28 15:33:16 +00:00
|
|
|
isolate()->heap()->CopyAndTenureFixedCOWArray(*array),
|
2013-10-04 07:25:24 +00:00
|
|
|
FixedArray);
|
2012-11-13 12:27:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-10-19 11:36:55 +00:00
|
|
|
Handle<FixedDoubleArray> Factory::CopyFixedDoubleArray(
|
|
|
|
Handle<FixedDoubleArray> array) {
|
2014-04-28 15:33:16 +00:00
|
|
|
CALL_HEAP_FUNCTION(isolate(),
|
|
|
|
isolate()->heap()->CopyFixedDoubleArray(*array),
|
|
|
|
FixedDoubleArray);
|
2011-10-19 11:36:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
Handle<Object> Factory::NewNumber(double value,
|
|
|
|
PretenureFlag pretenure) {
|
2014-04-16 14:04:54 +00:00
|
|
|
// We need to distinguish the minus zero value and this cannot be
|
|
|
|
// done after conversion to int. Doing this by comparing bit
|
|
|
|
// patterns is faster than using fpclassify() et al.
|
2015-02-05 04:58:25 +00:00
|
|
|
if (IsMinusZero(value)) return NewHeapNumber(-0.0, IMMUTABLE, pretenure);
|
2014-04-16 14:04:54 +00:00
|
|
|
|
2014-12-18 11:12:57 +00:00
|
|
|
int int_value = FastD2IChecked(value);
|
2014-04-16 14:04:54 +00:00
|
|
|
if (value == int_value && Smi::IsValid(int_value)) {
|
|
|
|
return handle(Smi::FromInt(int_value), isolate());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Materialize the value in the heap.
|
2014-07-01 15:02:31 +00:00
|
|
|
return NewHeapNumber(value, IMMUTABLE, pretenure);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-21 10:18:47 +00:00
|
|
|
Handle<Object> Factory::NewNumberFromInt(int32_t value,
|
|
|
|
PretenureFlag pretenure) {
|
2014-04-16 14:04:54 +00:00
|
|
|
if (Smi::IsValid(value)) return handle(Smi::FromInt(value), isolate());
|
2014-07-01 15:02:31 +00:00
|
|
|
// Bypass NewNumber to avoid various redundant checks.
|
|
|
|
return NewHeapNumber(FastI2D(value), IMMUTABLE, pretenure);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-21 10:18:47 +00:00
|
|
|
Handle<Object> Factory::NewNumberFromUint(uint32_t value,
|
2014-04-30 12:25:18 +00:00
|
|
|
PretenureFlag pretenure) {
|
|
|
|
int32_t int32v = static_cast<int32_t>(value);
|
|
|
|
if (int32v >= 0 && Smi::IsValid(int32v)) {
|
|
|
|
return handle(Smi::FromInt(int32v), isolate());
|
|
|
|
}
|
2014-07-01 15:02:31 +00:00
|
|
|
return NewHeapNumber(FastUI2D(value), IMMUTABLE, pretenure);
|
2009-03-10 08:10:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-05-08 15:02:08 +00:00
|
|
|
Handle<HeapNumber> Factory::NewHeapNumber(double value,
|
2014-07-01 15:02:31 +00:00
|
|
|
MutableMode mode,
|
2013-05-08 15:02:08 +00:00
|
|
|
PretenureFlag pretenure) {
|
|
|
|
CALL_HEAP_FUNCTION(
|
|
|
|
isolate(),
|
2014-07-01 15:02:31 +00:00
|
|
|
isolate()->heap()->AllocateHeapNumber(value, mode, pretenure),
|
|
|
|
HeapNumber);
|
2013-05-08 15:02:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-08-17 18:58:38 +00:00
|
|
|
#define SIMD128_NEW_DEF(TYPE, Type, type, lane_count, lane_type) \
|
|
|
|
Handle<Type> Factory::New##Type(lane_type lanes[lane_count], \
|
|
|
|
PretenureFlag pretenure) { \
|
|
|
|
CALL_HEAP_FUNCTION( \
|
|
|
|
isolate(), isolate()->heap()->Allocate##Type(lanes, pretenure), Type); \
|
|
|
|
}
|
|
|
|
SIMD128_TYPES(SIMD128_NEW_DEF)
|
|
|
|
#undef SIMD128_NEW_DEF
|
2015-06-03 23:56:21 +00:00
|
|
|
|
|
|
|
|
2015-08-17 11:02:31 +00:00
|
|
|
Handle<Object> Factory::NewError(Handle<JSFunction> constructor,
|
2015-04-16 07:01:20 +00:00
|
|
|
MessageTemplate::Template template_index,
|
|
|
|
Handle<Object> arg0, Handle<Object> arg1,
|
|
|
|
Handle<Object> arg2) {
|
|
|
|
HandleScope scope(isolate());
|
2015-06-10 17:42:01 +00:00
|
|
|
if (isolate()->bootstrapper()->IsActive()) {
|
2015-08-17 11:02:31 +00:00
|
|
|
// During bootstrapping we cannot construct error objects.
|
|
|
|
return scope.CloseAndEscape(NewStringFromAsciiChecked(
|
|
|
|
MessageTemplate::TemplateString(template_index)));
|
2015-06-10 17:42:01 +00:00
|
|
|
}
|
2015-04-16 07:01:20 +00:00
|
|
|
|
2015-08-17 11:02:31 +00:00
|
|
|
Handle<JSFunction> fun = isolate()->make_error_function();
|
2015-04-16 07:01:20 +00:00
|
|
|
Handle<Object> message_type(Smi::FromInt(template_index), isolate());
|
|
|
|
if (arg0.is_null()) arg0 = undefined_value();
|
|
|
|
if (arg1.is_null()) arg1 = undefined_value();
|
|
|
|
if (arg2.is_null()) arg2 = undefined_value();
|
2015-08-17 11:02:31 +00:00
|
|
|
Handle<Object> argv[] = {constructor, message_type, arg0, arg1, arg2};
|
2015-04-16 07:01:20 +00:00
|
|
|
|
|
|
|
// Invoke the JavaScript factory method. If an exception is thrown while
|
|
|
|
// running the factory method, use the exception as the result.
|
|
|
|
Handle<Object> result;
|
|
|
|
MaybeHandle<Object> exception;
|
2015-10-23 12:26:49 +00:00
|
|
|
if (!Execution::TryCall(isolate(), fun, undefined_value(), arraysize(argv),
|
|
|
|
argv, &exception)
|
2015-08-17 11:02:31 +00:00
|
|
|
.ToHandle(&result)) {
|
2015-04-16 07:01:20 +00:00
|
|
|
Handle<Object> exception_obj;
|
|
|
|
if (exception.ToHandle(&exception_obj)) {
|
|
|
|
result = exception_obj;
|
|
|
|
} else {
|
|
|
|
result = undefined_value();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return scope.CloseAndEscape(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-08-14 15:12:34 +00:00
|
|
|
Handle<Object> Factory::NewError(Handle<JSFunction> constructor,
|
2015-02-24 09:01:33 +00:00
|
|
|
Handle<String> message) {
|
2011-10-06 09:31:38 +00:00
|
|
|
Handle<Object> argv[] = { message };
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
// Invoke the JavaScript factory method. If an exception is thrown while
|
|
|
|
// running the factory method, use the exception as the result.
|
2014-04-11 10:41:09 +00:00
|
|
|
Handle<Object> result;
|
2014-09-01 09:11:44 +00:00
|
|
|
MaybeHandle<Object> exception;
|
2015-10-23 12:26:49 +00:00
|
|
|
if (!Execution::TryCall(isolate(), constructor, undefined_value(),
|
|
|
|
arraysize(argv), argv, &exception)
|
2015-08-14 10:22:16 +00:00
|
|
|
.ToHandle(&result)) {
|
2015-02-24 08:30:22 +00:00
|
|
|
Handle<Object> exception_obj;
|
|
|
|
if (exception.ToHandle(&exception_obj)) return exception_obj;
|
|
|
|
return undefined_value();
|
2014-04-11 10:41:09 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-09-01 09:25:19 +00:00
|
|
|
#define DEFINE_ERROR(NAME, name) \
|
|
|
|
Handle<Object> Factory::New##NAME(MessageTemplate::Template template_index, \
|
|
|
|
Handle<Object> arg0, Handle<Object> arg1, \
|
|
|
|
Handle<Object> arg2) { \
|
|
|
|
return NewError(isolate()->name##_function(), template_index, arg0, arg1, \
|
|
|
|
arg2); \
|
|
|
|
}
|
|
|
|
DEFINE_ERROR(Error, error)
|
|
|
|
DEFINE_ERROR(EvalError, eval_error)
|
|
|
|
DEFINE_ERROR(RangeError, range_error)
|
|
|
|
DEFINE_ERROR(ReferenceError, reference_error)
|
|
|
|
DEFINE_ERROR(SyntaxError, syntax_error)
|
|
|
|
DEFINE_ERROR(TypeError, type_error)
|
|
|
|
#undef DEFINE_ERROR
|
|
|
|
|
|
|
|
|
2015-11-25 12:41:35 +00:00
|
|
|
Handle<JSFunction> Factory::NewFunction(Handle<Map> map,
|
|
|
|
Handle<SharedFunctionInfo> info,
|
|
|
|
Handle<Context> context,
|
|
|
|
PretenureFlag pretenure) {
|
|
|
|
AllocationSpace space = pretenure == TENURED ? OLD_SPACE : NEW_SPACE;
|
|
|
|
Handle<JSFunction> function = New<JSFunction>(map, space);
|
|
|
|
|
2014-05-06 11:26:35 +00:00
|
|
|
function->initialize_properties();
|
|
|
|
function->initialize_elements();
|
|
|
|
function->set_shared(*info);
|
|
|
|
function->set_code(info->code());
|
|
|
|
function->set_context(*context);
|
|
|
|
function->set_prototype_or_initial_map(*the_hole_value());
|
2016-02-05 10:48:27 +00:00
|
|
|
function->set_literals(LiteralsArray::cast(*empty_fixed_array()));
|
2015-05-29 08:06:19 +00:00
|
|
|
function->set_next_function_link(*undefined_value(), SKIP_WRITE_BARRIER);
|
2015-11-25 12:41:35 +00:00
|
|
|
isolate()->heap()->InitializeJSObjectBody(*function, *map, JSFunction::kSize);
|
|
|
|
return function;
|
2014-05-06 11:26:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-09 17:39:54 +00:00
|
|
|
Handle<JSFunction> Factory::NewFunction(Handle<Map> map,
|
|
|
|
Handle<String> name,
|
|
|
|
MaybeHandle<Code> code) {
|
2014-07-01 12:12:34 +00:00
|
|
|
Handle<Context> context(isolate()->native_context());
|
2015-11-24 17:16:40 +00:00
|
|
|
Handle<SharedFunctionInfo> info =
|
|
|
|
NewSharedFunctionInfo(name, code, map->is_constructor());
|
2015-12-08 16:04:08 +00:00
|
|
|
DCHECK(is_sloppy(info->language_mode()));
|
2016-01-12 09:45:25 +00:00
|
|
|
DCHECK(!map->IsUndefined());
|
2015-12-08 16:04:08 +00:00
|
|
|
DCHECK(
|
|
|
|
map.is_identical_to(isolate()->sloppy_function_map()) ||
|
|
|
|
map.is_identical_to(isolate()->sloppy_function_without_prototype_map()) ||
|
|
|
|
map.is_identical_to(
|
|
|
|
isolate()->sloppy_function_with_readonly_prototype_map()) ||
|
|
|
|
map.is_identical_to(isolate()->strict_function_map()) ||
|
2016-01-12 09:45:25 +00:00
|
|
|
// TODO(titzer): wasm_function_map() could be undefined here. ugly.
|
|
|
|
(*map == context->get(Context::WASM_FUNCTION_MAP_INDEX)) ||
|
2015-12-08 16:04:08 +00:00
|
|
|
map.is_identical_to(isolate()->proxy_function_map()));
|
2014-05-09 17:39:54 +00:00
|
|
|
return NewFunction(map, info, context);
|
2014-05-06 11:26:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-09 16:39:33 +00:00
|
|
|
Handle<JSFunction> Factory::NewFunction(Handle<String> name) {
|
2014-05-09 17:39:54 +00:00
|
|
|
return NewFunction(
|
|
|
|
isolate()->sloppy_function_map(), name, MaybeHandle<Code>());
|
2014-05-06 11:26:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-09 17:39:54 +00:00
|
|
|
Handle<JSFunction> Factory::NewFunctionWithoutPrototype(Handle<String> name,
|
2015-04-09 22:40:16 +00:00
|
|
|
Handle<Code> code,
|
|
|
|
bool is_strict) {
|
|
|
|
Handle<Map> map = is_strict
|
|
|
|
? isolate()->strict_function_without_prototype_map()
|
|
|
|
: isolate()->sloppy_function_without_prototype_map();
|
|
|
|
return NewFunction(map, name, code);
|
2014-05-09 17:39:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-04-09 22:40:16 +00:00
|
|
|
Handle<JSFunction> Factory::NewFunction(Handle<String> name, Handle<Code> code,
|
2014-05-09 17:59:15 +00:00
|
|
|
Handle<Object> prototype,
|
2015-04-09 22:40:16 +00:00
|
|
|
bool read_only_prototype,
|
|
|
|
bool is_strict) {
|
|
|
|
// In strict mode, readonly strict map is only available during bootstrap
|
|
|
|
DCHECK(!is_strict || !read_only_prototype ||
|
|
|
|
isolate()->bootstrapper()->IsActive());
|
|
|
|
Handle<Map> map =
|
|
|
|
is_strict ? isolate()->strict_function_map()
|
|
|
|
: read_only_prototype
|
|
|
|
? isolate()->sloppy_function_with_readonly_prototype_map()
|
|
|
|
: isolate()->sloppy_function_map();
|
2014-05-09 17:59:15 +00:00
|
|
|
Handle<JSFunction> result = NewFunction(map, name, code);
|
2014-05-09 17:39:54 +00:00
|
|
|
result->set_prototype_or_initial_map(*prototype);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-11-13 14:12:03 +00:00
|
|
|
Handle<JSFunction> Factory::NewFunction(Handle<String> name, Handle<Code> code,
|
2014-05-09 17:39:54 +00:00
|
|
|
Handle<Object> prototype,
|
2014-11-13 14:12:03 +00:00
|
|
|
InstanceType type, int instance_size,
|
|
|
|
bool read_only_prototype,
|
2015-04-09 22:40:16 +00:00
|
|
|
bool install_constructor,
|
|
|
|
bool is_strict) {
|
2008-07-03 15:10:15 +00:00
|
|
|
// Allocate the function
|
2015-04-09 22:40:16 +00:00
|
|
|
Handle<JSFunction> function =
|
|
|
|
NewFunction(name, code, prototype, read_only_prototype, is_strict);
|
2014-05-09 16:59:03 +00:00
|
|
|
|
2014-09-25 08:25:25 +00:00
|
|
|
ElementsKind elements_kind =
|
|
|
|
type == JS_ARRAY_TYPE ? FAST_SMI_ELEMENTS : FAST_HOLEY_SMI_ELEMENTS;
|
|
|
|
Handle<Map> initial_map = NewMap(type, instance_size, elements_kind);
|
2014-11-13 14:12:03 +00:00
|
|
|
if (!function->shared()->is_generator()) {
|
|
|
|
if (prototype->IsTheHole()) {
|
|
|
|
prototype = NewFunctionPrototype(function);
|
|
|
|
} else if (install_constructor) {
|
|
|
|
JSObject::AddProperty(Handle<JSObject>::cast(prototype),
|
|
|
|
constructor_string(), function, DONT_ENUM);
|
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
2014-08-07 16:14:22 +00:00
|
|
|
|
2014-08-11 14:00:58 +00:00
|
|
|
JSFunction::SetInitialMap(function, initial_map,
|
|
|
|
Handle<JSReceiver>::cast(prototype));
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
return function;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-29 17:48:07 +00:00
|
|
|
Handle<JSFunction> Factory::NewFunction(Handle<String> name,
|
2014-05-09 17:39:54 +00:00
|
|
|
Handle<Code> code,
|
2014-04-29 17:48:07 +00:00
|
|
|
InstanceType type,
|
2014-05-09 17:39:54 +00:00
|
|
|
int instance_size) {
|
|
|
|
return NewFunction(name, code, the_hole_value(), type, instance_size);
|
2014-04-29 17:48:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-06 11:26:35 +00:00
|
|
|
Handle<JSObject> Factory::NewFunctionPrototype(Handle<JSFunction> function) {
|
|
|
|
// Make sure to use globals from the function's context, since the function
|
|
|
|
// can be from a different context.
|
|
|
|
Handle<Context> native_context(function->context()->native_context());
|
|
|
|
Handle<Map> new_map;
|
|
|
|
if (function->shared()->is_generator()) {
|
|
|
|
// Generator prototypes can share maps since they don't have "constructor"
|
|
|
|
// properties.
|
|
|
|
new_map = handle(native_context->generator_object_prototype_map());
|
|
|
|
} else {
|
|
|
|
// Each function prototype gets a fresh map to avoid unwanted sharing of
|
|
|
|
// maps between prototypes of different constructors.
|
|
|
|
Handle<JSFunction> object_function(native_context->object_function());
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(object_function->has_initial_map());
|
2014-08-07 16:14:22 +00:00
|
|
|
new_map = handle(object_function->initial_map());
|
2014-05-06 11:26:35 +00:00
|
|
|
}
|
|
|
|
|
2014-08-11 14:00:58 +00:00
|
|
|
DCHECK(!new_map->is_prototype_map());
|
2014-05-06 11:26:35 +00:00
|
|
|
Handle<JSObject> prototype = NewJSObjectFromMap(new_map);
|
|
|
|
|
|
|
|
if (!function->shared()->is_generator()) {
|
2014-06-30 13:48:57 +00:00
|
|
|
JSObject::AddProperty(prototype, constructor_string(), function, DONT_ENUM);
|
2014-05-06 11:26:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return prototype;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Handle<JSFunction> Factory::NewFunctionFromSharedFunctionInfo(
|
|
|
|
Handle<SharedFunctionInfo> info,
|
|
|
|
Handle<Context> context,
|
|
|
|
PretenureFlag pretenure) {
|
2015-02-04 09:34:05 +00:00
|
|
|
int map_index =
|
|
|
|
Context::FunctionMapIndex(info->language_mode(), info->kind());
|
2015-11-03 16:42:21 +00:00
|
|
|
Handle<Map> initial_map(Map::cast(context->native_context()->get(map_index)));
|
|
|
|
|
|
|
|
return NewFunctionFromSharedFunctionInfo(initial_map, info, context,
|
|
|
|
pretenure);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Handle<JSFunction> Factory::NewFunctionFromSharedFunctionInfo(
|
|
|
|
Handle<Map> initial_map, Handle<SharedFunctionInfo> info,
|
|
|
|
Handle<Context> context, PretenureFlag pretenure) {
|
|
|
|
DCHECK_EQ(JS_FUNCTION_TYPE, initial_map->instance_type());
|
|
|
|
Handle<JSFunction> result =
|
|
|
|
NewFunction(initial_map, info, context, pretenure);
|
2014-05-06 11:26:35 +00:00
|
|
|
|
|
|
|
if (info->ic_age() != isolate()->heap()->global_ic_age()) {
|
|
|
|
info->ResetForNewContext(isolate()->heap()->global_ic_age());
|
|
|
|
}
|
|
|
|
|
2015-04-07 15:44:16 +00:00
|
|
|
if (FLAG_always_opt && info->allows_lazy_compilation()) {
|
|
|
|
result->MarkForOptimization();
|
|
|
|
}
|
|
|
|
|
2015-06-25 08:28:19 +00:00
|
|
|
CodeAndLiterals cached = info->SearchOptimizedCodeMap(
|
|
|
|
context->native_context(), BailoutId::None());
|
|
|
|
if (cached.code != nullptr) {
|
|
|
|
// Caching of optimized code enabled and optimized code found.
|
|
|
|
DCHECK(!cached.code->marked_for_deoptimization());
|
|
|
|
DCHECK(result->shared()->is_compiled());
|
|
|
|
result->ReplaceCode(cached.code);
|
|
|
|
}
|
|
|
|
|
2015-09-23 08:46:09 +00:00
|
|
|
if (cached.literals != nullptr) {
|
|
|
|
result->set_literals(cached.literals);
|
2016-02-05 10:48:27 +00:00
|
|
|
} else {
|
2014-05-06 11:26:35 +00:00
|
|
|
int number_of_literals = info->num_literals();
|
2015-09-29 10:15:31 +00:00
|
|
|
Handle<LiteralsArray> literals =
|
2016-02-05 10:48:27 +00:00
|
|
|
LiteralsArray::New(isolate(), handle(info->feedback_vector()),
|
|
|
|
number_of_literals, pretenure);
|
2014-05-06 11:26:35 +00:00
|
|
|
result->set_literals(*literals);
|
2015-10-12 16:10:41 +00:00
|
|
|
|
2015-09-23 08:46:09 +00:00
|
|
|
// Cache context-specific literals.
|
2015-10-12 16:10:41 +00:00
|
|
|
Handle<Context> native_context(context->native_context());
|
2015-12-10 17:03:22 +00:00
|
|
|
SharedFunctionInfo::AddLiteralsToOptimizedCodeMap(info, native_context,
|
|
|
|
literals);
|
2014-05-06 11:26:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-03 10:36:55 +00:00
|
|
|
Handle<ScopeInfo> Factory::NewScopeInfo(int length) {
|
2014-04-07 10:12:54 +00:00
|
|
|
Handle<FixedArray> array = NewFixedArray(length, TENURED);
|
|
|
|
array->set_map_no_write_barrier(*scope_info_map());
|
|
|
|
Handle<ScopeInfo> scope_info = Handle<ScopeInfo>::cast(array);
|
|
|
|
return scope_info;
|
2011-08-11 16:29:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-11-13 12:27:03 +00:00
|
|
|
Handle<JSObject> Factory::NewExternal(void* value) {
|
2014-04-07 10:12:54 +00:00
|
|
|
Handle<Foreign> foreign = NewForeign(static_cast<Address>(value));
|
|
|
|
Handle<JSObject> external = NewJSObjectFromMap(external_map());
|
|
|
|
external->SetInternalField(0, *foreign);
|
|
|
|
return external;
|
2012-11-13 12:27:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-29 13:58:55 +00:00
|
|
|
Handle<Code> Factory::NewCodeRaw(int object_size, bool immovable) {
|
|
|
|
CALL_HEAP_FUNCTION(isolate(),
|
|
|
|
isolate()->heap()->AllocateCode(object_size, immovable),
|
2014-04-16 11:38:56 +00:00
|
|
|
Code);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-05-18 09:41:16 +00:00
|
|
|
Handle<Code> Factory::NewCode(const CodeDesc& desc,
|
|
|
|
Code::Flags flags,
|
2011-03-09 10:38:19 +00:00
|
|
|
Handle<Object> self_ref,
|
2013-04-18 09:50:46 +00:00
|
|
|
bool immovable,
|
2013-10-23 13:48:04 +00:00
|
|
|
bool crankshafted,
|
2014-05-12 13:47:01 +00:00
|
|
|
int prologue_offset,
|
|
|
|
bool is_debug) {
|
2014-04-16 11:38:56 +00:00
|
|
|
Handle<ByteArray> reloc_info = NewByteArray(desc.reloc_size, TENURED);
|
|
|
|
|
|
|
|
// Compute size.
|
|
|
|
int body_size = RoundUp(desc.instr_size, kObjectAlignment);
|
|
|
|
int obj_size = Code::SizeFor(body_size);
|
|
|
|
|
2014-04-29 13:58:55 +00:00
|
|
|
Handle<Code> code = NewCodeRaw(obj_size, immovable);
|
2015-06-12 11:42:06 +00:00
|
|
|
DCHECK(isolate()->code_range() == NULL || !isolate()->code_range()->valid() ||
|
|
|
|
isolate()->code_range()->contains(code->address()) ||
|
|
|
|
obj_size <= isolate()->heap()->code_space()->AreaSize());
|
2014-04-16 11:38:56 +00:00
|
|
|
|
|
|
|
// The code object has not been fully initialized yet. We rely on the
|
|
|
|
// fact that no allocation will happen from this point on.
|
|
|
|
DisallowHeapAllocation no_gc;
|
|
|
|
code->set_gc_metadata(Smi::FromInt(0));
|
2015-02-19 09:56:06 +00:00
|
|
|
code->set_ic_age(isolate()->heap()->global_ic_age());
|
2014-04-16 11:38:56 +00:00
|
|
|
code->set_instruction_size(desc.instr_size);
|
|
|
|
code->set_relocation_info(*reloc_info);
|
|
|
|
code->set_flags(flags);
|
|
|
|
code->set_raw_kind_specific_flags1(0);
|
|
|
|
code->set_raw_kind_specific_flags2(0);
|
|
|
|
code->set_is_crankshafted(crankshafted);
|
|
|
|
code->set_deoptimization_data(*empty_fixed_array(), SKIP_WRITE_BARRIER);
|
2014-07-21 13:51:42 +00:00
|
|
|
code->set_raw_type_feedback_info(Smi::FromInt(0));
|
2014-04-16 11:38:56 +00:00
|
|
|
code->set_next_code_link(*undefined_value());
|
|
|
|
code->set_handler_table(*empty_fixed_array(), SKIP_WRITE_BARRIER);
|
|
|
|
code->set_prologue_offset(prologue_offset);
|
2015-11-26 10:43:28 +00:00
|
|
|
code->set_constant_pool_offset(desc.instr_size - desc.constant_pool_size);
|
|
|
|
|
2014-04-16 11:38:56 +00:00
|
|
|
if (code->kind() == Code::OPTIMIZED_FUNCTION) {
|
|
|
|
code->set_marked_for_deoptimization(false);
|
|
|
|
}
|
|
|
|
|
2014-05-12 13:47:01 +00:00
|
|
|
if (is_debug) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(code->kind() == Code::FUNCTION);
|
2014-05-12 13:47:01 +00:00
|
|
|
code->set_has_debug_break_slots(true);
|
|
|
|
}
|
|
|
|
|
2014-04-16 11:38:56 +00:00
|
|
|
// Allow self references to created code object by patching the handle to
|
|
|
|
// point to the newly allocated Code object.
|
|
|
|
if (!self_ref.is_null()) *(self_ref.location()) = *code;
|
|
|
|
|
|
|
|
// Migrate generated code.
|
|
|
|
// The generated code can contain Object** values (typically from handles)
|
|
|
|
// that are dereferenced during the copy to point directly to the actual heap
|
|
|
|
// objects. These pointers can include references to the code object itself,
|
|
|
|
// through the self_reference parameter.
|
|
|
|
code->CopyFrom(desc);
|
|
|
|
|
|
|
|
#ifdef VERIFY_HEAP
|
2014-04-30 12:25:18 +00:00
|
|
|
if (FLAG_verify_heap) code->ObjectVerify();
|
2014-04-16 11:38:56 +00:00
|
|
|
#endif
|
|
|
|
return code;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Handle<Code> Factory::CopyCode(Handle<Code> code) {
|
2011-03-18 20:35:07 +00:00
|
|
|
CALL_HEAP_FUNCTION(isolate(),
|
|
|
|
isolate()->heap()->CopyCode(*code),
|
|
|
|
Code);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-15 21:06:51 +00:00
|
|
|
Handle<Code> Factory::CopyCode(Handle<Code> code, Vector<byte> reloc_info) {
|
2011-03-18 20:35:07 +00:00
|
|
|
CALL_HEAP_FUNCTION(isolate(),
|
|
|
|
isolate()->heap()->CopyCode(*code, reloc_info),
|
|
|
|
Code);
|
2010-03-15 21:06:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
Handle<JSObject> Factory::NewJSObject(Handle<JSFunction> constructor,
|
|
|
|
PretenureFlag pretenure) {
|
2013-11-05 12:11:27 +00:00
|
|
|
JSFunction::EnsureHasInitialMap(constructor);
|
2011-03-18 20:35:07 +00:00
|
|
|
CALL_HEAP_FUNCTION(
|
|
|
|
isolate(),
|
|
|
|
isolate()->heap()->AllocateJSObject(*constructor, pretenure), JSObject);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-19 13:39:09 +00:00
|
|
|
Handle<JSObject> Factory::NewJSObjectWithMemento(
|
|
|
|
Handle<JSFunction> constructor,
|
|
|
|
Handle<AllocationSite> site) {
|
|
|
|
JSFunction::EnsureHasInitialMap(constructor);
|
|
|
|
CALL_HEAP_FUNCTION(
|
|
|
|
isolate(),
|
|
|
|
isolate()->heap()->AllocateJSObject(*constructor, NOT_TENURED, *site),
|
|
|
|
JSObject);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-09 08:59:03 +00:00
|
|
|
Handle<JSModule> Factory::NewJSModule(Handle<Context> context,
|
|
|
|
Handle<ScopeInfo> scope_info) {
|
2014-04-07 12:43:35 +00:00
|
|
|
// Allocate a fresh map. Modules do not have a prototype.
|
|
|
|
Handle<Map> map = NewMap(JS_MODULE_TYPE, JSModule::kSize);
|
|
|
|
// Allocate the object based on the map.
|
|
|
|
Handle<JSModule> module =
|
|
|
|
Handle<JSModule>::cast(NewJSObjectFromMap(map, TENURED));
|
|
|
|
module->set_context(*context);
|
|
|
|
module->set_scope_info(*scope_info);
|
|
|
|
return module;
|
2012-04-16 14:43:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-11-02 14:57:59 +00:00
|
|
|
Handle<JSGlobalObject> Factory::NewJSGlobalObject(
|
|
|
|
Handle<JSFunction> constructor) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(constructor->has_initial_map());
|
2013-10-21 13:55:24 +00:00
|
|
|
Handle<Map> map(constructor->initial_map());
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(map->is_dictionary_map());
|
2013-10-21 13:55:24 +00:00
|
|
|
|
|
|
|
// Make sure no field properties are described in the initial map.
|
|
|
|
// This guarantees us that normalizing the properties does not
|
|
|
|
// require us to change property values to PropertyCells.
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(map->NextFreePropertyIndex() == 0);
|
2013-10-21 13:55:24 +00:00
|
|
|
|
|
|
|
// Make sure we don't have a ton of pre-allocated slots in the
|
|
|
|
// global objects. They will be unused once we normalize the object.
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(map->unused_property_fields() == 0);
|
2015-08-11 19:36:04 +00:00
|
|
|
DCHECK(map->GetInObjectProperties() == 0);
|
2013-10-21 13:55:24 +00:00
|
|
|
|
|
|
|
// Initial size of the backing store to avoid resize of the storage during
|
|
|
|
// bootstrapping. The size differs between the JS global object ad the
|
|
|
|
// builtins object.
|
2015-11-02 13:04:20 +00:00
|
|
|
int initial_size = 64;
|
2013-10-21 13:55:24 +00:00
|
|
|
|
|
|
|
// Allocate a dictionary object for backing storage.
|
|
|
|
int at_least_space_for = map->NumberOfOwnDescriptors() * 2 + initial_size;
|
2015-06-01 16:24:59 +00:00
|
|
|
Handle<GlobalDictionary> dictionary =
|
|
|
|
GlobalDictionary::New(isolate(), at_least_space_for);
|
2013-10-21 13:55:24 +00:00
|
|
|
|
|
|
|
// The global object might be created from an object template with accessors.
|
|
|
|
// Fill these accessors into the dictionary.
|
|
|
|
Handle<DescriptorArray> descs(map->instance_descriptors());
|
|
|
|
for (int i = 0; i < map->NumberOfOwnDescriptors(); i++) {
|
|
|
|
PropertyDetails details = descs->GetDetails(i);
|
2015-01-19 17:49:13 +00:00
|
|
|
// Only accessors are expected.
|
|
|
|
DCHECK_EQ(ACCESSOR_CONSTANT, details.type());
|
2015-03-17 13:27:25 +00:00
|
|
|
PropertyDetails d(details.attributes(), ACCESSOR_CONSTANT, i + 1,
|
|
|
|
PropertyCellType::kMutable);
|
2013-10-21 13:55:24 +00:00
|
|
|
Handle<Name> name(descs->GetKey(i));
|
2015-03-17 13:27:25 +00:00
|
|
|
Handle<PropertyCell> cell = NewPropertyCell();
|
|
|
|
cell->set_value(descs->GetCallbacksObject(i));
|
2014-04-24 15:45:44 +00:00
|
|
|
// |dictionary| already contains enough space for all properties.
|
2015-06-01 16:24:59 +00:00
|
|
|
USE(GlobalDictionary::Add(dictionary, name, cell, d));
|
2013-10-21 13:55:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Allocate the global object and initialize it with the backing store.
|
2015-11-02 14:57:59 +00:00
|
|
|
Handle<JSGlobalObject> global = New<JSGlobalObject>(map, OLD_SPACE);
|
2013-10-21 13:55:24 +00:00
|
|
|
isolate()->heap()->InitializeJSObjectFromMap(*global, *dictionary, *map);
|
|
|
|
|
|
|
|
// Create a new map for the global object.
|
|
|
|
Handle<Map> new_map = Map::CopyDropDescriptors(map);
|
|
|
|
new_map->set_dictionary_map(true);
|
|
|
|
|
|
|
|
// Set up the global object as a normalized object.
|
|
|
|
global->set_map(*new_map);
|
|
|
|
global->set_properties(*dictionary);
|
|
|
|
|
|
|
|
// Make sure result is a global object with properties in dictionary.
|
2015-11-02 14:57:59 +00:00
|
|
|
DCHECK(global->IsJSGlobalObject() && !global->HasFastProperties());
|
2013-10-21 13:55:24 +00:00
|
|
|
return global;
|
|
|
|
}
|
|
|
|
|
2009-06-30 10:05:36 +00:00
|
|
|
|
2014-03-17 15:01:45 +00:00
|
|
|
Handle<JSObject> Factory::NewJSObjectFromMap(
|
|
|
|
Handle<Map> map,
|
|
|
|
PretenureFlag pretenure,
|
|
|
|
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,
|
|
|
|
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,
|
2015-06-08 12:17:58 +00:00
|
|
|
Strength strength,
|
2014-04-07 10:00:14 +00:00
|
|
|
PretenureFlag pretenure) {
|
2015-06-02 11:46:04 +00:00
|
|
|
Map* map = isolate()->get_initial_js_array_map(elements_kind, strength);
|
|
|
|
if (map == nullptr) {
|
2015-06-08 12:17:58 +00:00
|
|
|
DCHECK(strength == Strength::WEAK);
|
2015-06-02 11:46:04 +00:00
|
|
|
Context* native_context = isolate()->context()->native_context();
|
|
|
|
JSFunction* array_function = native_context->array_function();
|
|
|
|
map = array_function->initial_map();
|
|
|
|
}
|
2014-04-07 12:43:35 +00:00
|
|
|
return Handle<JSArray>::cast(NewJSObjectFromMap(handle(map), pretenure));
|
2014-04-07 10:00:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-02 11:46:04 +00:00
|
|
|
Handle<JSArray> Factory::NewJSArray(ElementsKind elements_kind, int length,
|
2015-06-08 12:17:58 +00:00
|
|
|
int capacity, Strength strength,
|
2014-03-27 16:41:09 +00:00
|
|
|
ArrayStorageAllocationMode mode,
|
2008-07-03 15:10:15 +00:00
|
|
|
PretenureFlag pretenure) {
|
2015-06-02 11:46:04 +00:00
|
|
|
Handle<JSArray> array = NewJSArray(elements_kind, strength, pretenure);
|
2014-04-07 12:43:35 +00:00
|
|
|
NewJSArrayStorage(array, length, capacity, mode);
|
|
|
|
return array;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-12-09 08:50:19 +00:00
|
|
|
Handle<JSArray> Factory::NewJSArrayWithElements(Handle<FixedArrayBase> elements,
|
2012-01-26 21:47:57 +00:00
|
|
|
ElementsKind elements_kind,
|
2015-06-08 12:17:58 +00:00
|
|
|
int length, Strength strength,
|
2008-07-03 15:10:15 +00:00
|
|
|
PretenureFlag pretenure) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(length <= elements->length());
|
2015-06-02 11:46:04 +00:00
|
|
|
Handle<JSArray> array = NewJSArray(elements_kind, strength, pretenure);
|
2014-04-07 10:00:14 +00:00
|
|
|
|
|
|
|
array->set_elements(*elements);
|
|
|
|
array->set_length(Smi::FromInt(length));
|
|
|
|
JSObject::ValidateElements(array);
|
|
|
|
return array;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-17 15:01:45 +00:00
|
|
|
void Factory::NewJSArrayStorage(Handle<JSArray> array,
|
2014-04-14 16:05:19 +00:00
|
|
|
int length,
|
|
|
|
int capacity,
|
|
|
|
ArrayStorageAllocationMode mode) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(capacity >= length);
|
2014-04-14 16:05:19 +00:00
|
|
|
|
|
|
|
if (capacity == 0) {
|
|
|
|
array->set_length(Smi::FromInt(0));
|
|
|
|
array->set_elements(*empty_fixed_array());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-12-18 12:50:02 +00:00
|
|
|
HandleScope inner_scope(isolate());
|
2014-04-14 16:05:19 +00:00
|
|
|
Handle<FixedArrayBase> elms;
|
|
|
|
ElementsKind elements_kind = array->GetElementsKind();
|
|
|
|
if (IsFastDoubleElementsKind(elements_kind)) {
|
|
|
|
if (mode == DONT_INITIALIZE_ARRAY_ELEMENTS) {
|
|
|
|
elms = NewFixedDoubleArray(capacity);
|
|
|
|
} else {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(mode == INITIALIZE_ARRAY_ELEMENTS_WITH_HOLE);
|
2014-04-14 16:05:19 +00:00
|
|
|
elms = NewFixedDoubleArrayWithHoles(capacity);
|
|
|
|
}
|
|
|
|
} else {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(IsFastSmiOrObjectElementsKind(elements_kind));
|
2014-04-14 16:05:19 +00:00
|
|
|
if (mode == DONT_INITIALIZE_ARRAY_ELEMENTS) {
|
|
|
|
elms = NewUninitializedFixedArray(capacity);
|
|
|
|
} else {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(mode == INITIALIZE_ARRAY_ELEMENTS_WITH_HOLE);
|
2014-04-14 16:05:19 +00:00
|
|
|
elms = NewFixedArrayWithHoles(capacity);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
array->set_elements(*elms);
|
|
|
|
array->set_length(Smi::FromInt(length));
|
2014-03-17 15:01:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-11-05 12:11:27 +00:00
|
|
|
Handle<JSGeneratorObject> Factory::NewJSGeneratorObject(
|
|
|
|
Handle<JSFunction> function) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(function->shared()->is_generator());
|
2013-11-05 12:11:27 +00:00
|
|
|
JSFunction::EnsureHasInitialMap(function);
|
|
|
|
Handle<Map> map(function->initial_map());
|
2015-12-03 10:02:46 +00:00
|
|
|
DCHECK_EQ(JS_GENERATOR_OBJECT_TYPE, map->instance_type());
|
2014-04-08 06:15:20 +00:00
|
|
|
CALL_HEAP_FUNCTION(
|
|
|
|
isolate(),
|
|
|
|
isolate()->heap()->AllocateJSObjectFromMap(*map),
|
|
|
|
JSGeneratorObject);
|
2013-11-05 12:11:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-09-17 14:47:37 +00:00
|
|
|
Handle<JSArrayBuffer> Factory::NewJSArrayBuffer(SharedFlag shared,
|
|
|
|
PretenureFlag pretenure) {
|
2013-08-06 19:14:51 +00:00
|
|
|
Handle<JSFunction> array_buffer_fun(
|
2015-05-22 13:43:38 +00:00
|
|
|
shared == SharedFlag::kShared
|
|
|
|
? isolate()->native_context()->shared_array_buffer_fun()
|
|
|
|
: isolate()->native_context()->array_buffer_fun());
|
2015-09-17 14:47:37 +00:00
|
|
|
CALL_HEAP_FUNCTION(isolate(), isolate()->heap()->AllocateJSObject(
|
|
|
|
*array_buffer_fun, pretenure),
|
|
|
|
JSArrayBuffer);
|
2013-04-25 12:02:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-21 13:02:38 +00:00
|
|
|
Handle<JSDataView> Factory::NewJSDataView() {
|
2013-08-06 19:14:51 +00:00
|
|
|
Handle<JSFunction> data_view_fun(
|
2014-07-01 12:12:34 +00:00
|
|
|
isolate()->native_context()->data_view_fun());
|
2014-04-08 06:15:20 +00:00
|
|
|
CALL_HEAP_FUNCTION(
|
|
|
|
isolate(),
|
|
|
|
isolate()->heap()->AllocateJSObject(*data_view_fun),
|
|
|
|
JSDataView);
|
2013-06-21 13:02:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-05-26 17:36:48 +00:00
|
|
|
Handle<JSMap> Factory::NewJSMap() {
|
|
|
|
Handle<Map> map(isolate()->native_context()->js_map_map());
|
|
|
|
Handle<JSMap> js_map = Handle<JSMap>::cast(NewJSObjectFromMap(map));
|
2015-08-26 12:46:10 +00:00
|
|
|
JSMap::Initialize(js_map, isolate());
|
2015-05-26 17:36:48 +00:00
|
|
|
return js_map;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Handle<JSSet> Factory::NewJSSet() {
|
|
|
|
Handle<Map> map(isolate()->native_context()->js_set_map());
|
|
|
|
Handle<JSSet> js_set = Handle<JSSet>::cast(NewJSObjectFromMap(map));
|
2015-08-26 12:46:10 +00:00
|
|
|
JSSet::Initialize(js_set, isolate());
|
2015-05-26 17:36:48 +00:00
|
|
|
return js_set;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-11-04 10:02:25 +00:00
|
|
|
Handle<JSMapIterator> Factory::NewJSMapIterator() {
|
|
|
|
Handle<Map> map(isolate()->native_context()->map_iterator_map());
|
|
|
|
CALL_HEAP_FUNCTION(isolate(),
|
|
|
|
isolate()->heap()->AllocateJSObjectFromMap(*map),
|
|
|
|
JSMapIterator);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Handle<JSSetIterator> Factory::NewJSSetIterator() {
|
|
|
|
Handle<Map> map(isolate()->native_context()->set_iterator_map());
|
|
|
|
CALL_HEAP_FUNCTION(isolate(),
|
|
|
|
isolate()->heap()->AllocateJSObjectFromMap(*map),
|
|
|
|
JSSetIterator);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-10-07 07:36:21 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
ElementsKind GetExternalArrayElementsKind(ExternalArrayType type) {
|
|
|
|
switch (type) {
|
|
|
|
#define TYPED_ARRAY_CASE(Type, type, TYPE, ctype, size) \
|
|
|
|
case kExternal##Type##Array: \
|
2015-07-28 09:29:34 +00:00
|
|
|
return TYPE##_ELEMENTS;
|
2014-10-07 07:36:21 +00:00
|
|
|
TYPED_ARRAYS(TYPED_ARRAY_CASE)
|
|
|
|
}
|
|
|
|
UNREACHABLE();
|
2015-07-28 09:29:34 +00:00
|
|
|
return FIRST_FIXED_TYPED_ARRAY_ELEMENTS_KIND;
|
2014-10-07 07:36:21 +00:00
|
|
|
#undef TYPED_ARRAY_CASE
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-10-10 08:10:29 +00:00
|
|
|
size_t GetExternalArrayElementSize(ExternalArrayType type) {
|
|
|
|
switch (type) {
|
|
|
|
#define TYPED_ARRAY_CASE(Type, type, TYPE, ctype, size) \
|
|
|
|
case kExternal##Type##Array: \
|
|
|
|
return size;
|
|
|
|
TYPED_ARRAYS(TYPED_ARRAY_CASE)
|
2015-04-27 09:28:16 +00:00
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#undef TYPED_ARRAY_CASE
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
size_t GetFixedTypedArraysElementSize(ElementsKind kind) {
|
|
|
|
switch (kind) {
|
|
|
|
#define TYPED_ARRAY_CASE(Type, type, TYPE, ctype, size) \
|
|
|
|
case TYPE##_ELEMENTS: \
|
|
|
|
return size;
|
|
|
|
TYPED_ARRAYS(TYPED_ARRAY_CASE)
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#undef TYPED_ARRAY_CASE
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ExternalArrayType GetArrayTypeFromElementsKind(ElementsKind kind) {
|
|
|
|
switch (kind) {
|
|
|
|
#define TYPED_ARRAY_CASE(Type, type, TYPE, ctype, size) \
|
|
|
|
case TYPE##_ELEMENTS: \
|
|
|
|
return kExternal##Type##Array;
|
|
|
|
TYPED_ARRAYS(TYPED_ARRAY_CASE)
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
return kExternalInt8Array;
|
2014-10-10 08:10:29 +00:00
|
|
|
}
|
|
|
|
#undef TYPED_ARRAY_CASE
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-10-07 07:36:21 +00:00
|
|
|
JSFunction* GetTypedArrayFun(ExternalArrayType type, Isolate* isolate) {
|
2013-08-06 19:14:51 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-10-17 09:04:58 +00:00
|
|
|
|
2015-04-27 09:28:16 +00:00
|
|
|
JSFunction* GetTypedArrayFun(ElementsKind elements_kind, Isolate* isolate) {
|
|
|
|
Context* native_context = isolate->context()->native_context();
|
|
|
|
switch (elements_kind) {
|
|
|
|
#define TYPED_ARRAY_FUN(Type, type, TYPE, ctype, size) \
|
|
|
|
case TYPE##_ELEMENTS: \
|
|
|
|
return native_context->type##_array_fun();
|
|
|
|
|
|
|
|
TYPED_ARRAYS(TYPED_ARRAY_FUN)
|
|
|
|
#undef TYPED_ARRAY_FUN
|
|
|
|
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-10-17 09:04:58 +00:00
|
|
|
void SetupArrayBufferView(i::Isolate* isolate,
|
|
|
|
i::Handle<i::JSArrayBufferView> obj,
|
|
|
|
i::Handle<i::JSArrayBuffer> buffer,
|
2015-09-17 14:47:37 +00:00
|
|
|
size_t byte_offset, size_t byte_length,
|
|
|
|
PretenureFlag pretenure = NOT_TENURED) {
|
2014-10-17 09:04:58 +00:00
|
|
|
DCHECK(byte_offset + byte_length <=
|
|
|
|
static_cast<size_t>(buffer->byte_length()->Number()));
|
|
|
|
|
|
|
|
obj->set_buffer(*buffer);
|
|
|
|
|
|
|
|
i::Handle<i::Object> byte_offset_object =
|
2015-09-17 14:47:37 +00:00
|
|
|
isolate->factory()->NewNumberFromSize(byte_offset, pretenure);
|
2014-10-17 09:04:58 +00:00
|
|
|
obj->set_byte_offset(*byte_offset_object);
|
|
|
|
|
|
|
|
i::Handle<i::Object> byte_length_object =
|
2015-09-17 14:47:37 +00:00
|
|
|
isolate->factory()->NewNumberFromSize(byte_length, pretenure);
|
2014-10-17 09:04:58 +00:00
|
|
|
obj->set_byte_length(*byte_length_object);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-10-07 07:36:21 +00:00
|
|
|
} // namespace
|
|
|
|
|
2013-08-06 19:14:51 +00:00
|
|
|
|
2015-09-17 14:47:37 +00:00
|
|
|
Handle<JSTypedArray> Factory::NewJSTypedArray(ExternalArrayType type,
|
|
|
|
PretenureFlag pretenure) {
|
2013-08-06 19:14:51 +00:00
|
|
|
Handle<JSFunction> typed_array_fun_handle(GetTypedArrayFun(type, isolate()));
|
2014-04-08 06:15:20 +00:00
|
|
|
|
2015-09-17 14:47:37 +00:00
|
|
|
CALL_HEAP_FUNCTION(isolate(), isolate()->heap()->AllocateJSObject(
|
|
|
|
*typed_array_fun_handle, pretenure),
|
|
|
|
JSTypedArray);
|
2013-04-29 11:09:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-09-17 14:47:37 +00:00
|
|
|
Handle<JSTypedArray> Factory::NewJSTypedArray(ElementsKind elements_kind,
|
|
|
|
PretenureFlag pretenure) {
|
2015-04-27 09:28:16 +00:00
|
|
|
Handle<JSFunction> typed_array_fun_handle(
|
|
|
|
GetTypedArrayFun(elements_kind, isolate()));
|
|
|
|
|
2015-09-17 14:47:37 +00:00
|
|
|
CALL_HEAP_FUNCTION(isolate(), isolate()->heap()->AllocateJSObject(
|
|
|
|
*typed_array_fun_handle, pretenure),
|
|
|
|
JSTypedArray);
|
2015-04-27 09:28:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-10-07 07:36:21 +00:00
|
|
|
Handle<JSTypedArray> Factory::NewJSTypedArray(ExternalArrayType type,
|
|
|
|
Handle<JSArrayBuffer> buffer,
|
2015-09-17 14:47:37 +00:00
|
|
|
size_t byte_offset, size_t length,
|
|
|
|
PretenureFlag pretenure) {
|
|
|
|
Handle<JSTypedArray> obj = NewJSTypedArray(type, pretenure);
|
2014-10-17 09:04:58 +00:00
|
|
|
|
|
|
|
size_t element_size = GetExternalArrayElementSize(type);
|
|
|
|
ElementsKind elements_kind = GetExternalArrayElementsKind(type);
|
|
|
|
|
|
|
|
CHECK(byte_offset % element_size == 0);
|
|
|
|
|
|
|
|
CHECK(length <= (std::numeric_limits<size_t>::max() / element_size));
|
|
|
|
CHECK(length <= static_cast<size_t>(Smi::kMaxValue));
|
|
|
|
size_t byte_length = length * element_size;
|
2015-09-17 14:47:37 +00:00
|
|
|
SetupArrayBufferView(isolate(), obj, buffer, byte_offset, byte_length,
|
|
|
|
pretenure);
|
2014-10-17 09:04:58 +00:00
|
|
|
|
2015-09-17 14:47:37 +00:00
|
|
|
Handle<Object> length_object = NewNumberFromSize(length, pretenure);
|
2014-10-17 09:04:58 +00:00
|
|
|
obj->set_length(*length_object);
|
|
|
|
|
2015-07-28 09:29:34 +00:00
|
|
|
Handle<FixedTypedArrayBase> elements = NewFixedTypedArrayWithExternalPointer(
|
2014-10-17 09:04:58 +00:00
|
|
|
static_cast<int>(length), type,
|
2015-09-17 14:47:37 +00:00
|
|
|
static_cast<uint8_t*>(buffer->backing_store()) + byte_offset, pretenure);
|
2014-10-17 09:04:58 +00:00
|
|
|
Handle<Map> map = JSObject::GetElementsTransitionMap(obj, elements_kind);
|
|
|
|
JSObject::SetMapAndElements(obj, map, elements);
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-04-27 09:28:16 +00:00
|
|
|
Handle<JSTypedArray> Factory::NewJSTypedArray(ElementsKind elements_kind,
|
2015-09-17 14:47:37 +00:00
|
|
|
size_t number_of_elements,
|
|
|
|
PretenureFlag pretenure) {
|
|
|
|
Handle<JSTypedArray> obj = NewJSTypedArray(elements_kind, pretenure);
|
2015-04-27 09:28:16 +00:00
|
|
|
|
|
|
|
size_t element_size = GetFixedTypedArraysElementSize(elements_kind);
|
|
|
|
ExternalArrayType array_type = GetArrayTypeFromElementsKind(elements_kind);
|
|
|
|
|
|
|
|
CHECK(number_of_elements <=
|
|
|
|
(std::numeric_limits<size_t>::max() / element_size));
|
|
|
|
CHECK(number_of_elements <= static_cast<size_t>(Smi::kMaxValue));
|
|
|
|
size_t byte_length = number_of_elements * element_size;
|
|
|
|
|
|
|
|
obj->set_byte_offset(Smi::FromInt(0));
|
|
|
|
i::Handle<i::Object> byte_length_object =
|
2015-09-17 14:47:37 +00:00
|
|
|
NewNumberFromSize(byte_length, pretenure);
|
2015-04-27 09:28:16 +00:00
|
|
|
obj->set_byte_length(*byte_length_object);
|
2015-09-17 14:47:37 +00:00
|
|
|
Handle<Object> length_object =
|
|
|
|
NewNumberFromSize(number_of_elements, pretenure);
|
2015-04-27 09:28:16 +00:00
|
|
|
obj->set_length(*length_object);
|
|
|
|
|
2015-09-17 14:47:37 +00:00
|
|
|
Handle<JSArrayBuffer> buffer =
|
|
|
|
NewJSArrayBuffer(SharedFlag::kNotShared, pretenure);
|
2015-08-26 13:59:35 +00:00
|
|
|
JSArrayBuffer::Setup(buffer, isolate(), true, NULL, byte_length,
|
|
|
|
SharedFlag::kNotShared);
|
2015-04-30 13:46:27 +00:00
|
|
|
obj->set_buffer(*buffer);
|
2015-09-17 14:47:37 +00:00
|
|
|
Handle<FixedTypedArrayBase> elements = NewFixedTypedArray(
|
|
|
|
static_cast<int>(number_of_elements), array_type, true, pretenure);
|
2015-04-27 09:28:16 +00:00
|
|
|
obj->set_elements(*elements);
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-10-17 09:04:58 +00:00
|
|
|
Handle<JSDataView> Factory::NewJSDataView(Handle<JSArrayBuffer> buffer,
|
|
|
|
size_t byte_offset,
|
|
|
|
size_t byte_length) {
|
|
|
|
Handle<JSDataView> obj = NewJSDataView();
|
|
|
|
SetupArrayBufferView(isolate(), obj, buffer, byte_offset, byte_length);
|
|
|
|
return obj;
|
2014-10-07 07:36:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-27 06:30:53 +00:00
|
|
|
MaybeHandle<JSBoundFunction> Factory::NewJSBoundFunction(
|
|
|
|
Handle<JSReceiver> target_function, Handle<Object> bound_this,
|
|
|
|
Vector<Handle<Object>> bound_args) {
|
|
|
|
DCHECK(target_function->IsCallable());
|
|
|
|
STATIC_ASSERT(Code::kMaxArguments <= FixedArray::kMaxLength);
|
|
|
|
if (bound_args.length() >= Code::kMaxArguments) {
|
|
|
|
THROW_NEW_ERROR(isolate(),
|
|
|
|
NewRangeError(MessageTemplate::kTooManyArguments),
|
|
|
|
JSBoundFunction);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Determine the prototype of the {target_function}.
|
|
|
|
Handle<Object> prototype;
|
2016-02-08 13:49:47 +00:00
|
|
|
ASSIGN_RETURN_ON_EXCEPTION(
|
|
|
|
isolate(), prototype,
|
|
|
|
JSReceiver::GetPrototype(isolate(), target_function), JSBoundFunction);
|
2015-12-27 06:30:53 +00:00
|
|
|
|
|
|
|
// Create the [[BoundArguments]] for the result.
|
|
|
|
Handle<FixedArray> bound_arguments;
|
|
|
|
if (bound_args.length() == 0) {
|
|
|
|
bound_arguments = empty_fixed_array();
|
|
|
|
} else {
|
|
|
|
bound_arguments = NewFixedArray(bound_args.length());
|
|
|
|
for (int i = 0; i < bound_args.length(); ++i) {
|
|
|
|
bound_arguments->set(i, *bound_args[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup the map for the JSBoundFunction instance.
|
|
|
|
Handle<Map> map = handle(
|
|
|
|
target_function->IsConstructor()
|
|
|
|
? isolate()->native_context()->bound_function_with_constructor_map()
|
|
|
|
: isolate()
|
|
|
|
->native_context()
|
|
|
|
->bound_function_without_constructor_map(),
|
|
|
|
isolate());
|
|
|
|
if (map->prototype() != *prototype) {
|
|
|
|
map = Map::TransitionToPrototype(map, prototype, REGULAR_PROTOTYPE);
|
|
|
|
}
|
|
|
|
DCHECK_EQ(target_function->IsConstructor(), map->is_constructor());
|
|
|
|
|
|
|
|
// Setup the JSBoundFunction instance.
|
|
|
|
Handle<JSBoundFunction> result =
|
|
|
|
Handle<JSBoundFunction>::cast(NewJSObjectFromMap(map));
|
|
|
|
result->set_bound_target_function(*target_function);
|
|
|
|
result->set_bound_this(*bound_this);
|
|
|
|
result->set_bound_arguments(*bound_arguments);
|
|
|
|
result->set_length(Smi::FromInt(0));
|
|
|
|
result->set_name(*undefined_value(), SKIP_WRITE_BARRIER);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-22 16:36:47 +00:00
|
|
|
// ES6 section 9.5.15 ProxyCreate (target, handler)
|
2015-11-12 22:11:53 +00:00
|
|
|
Handle<JSProxy> Factory::NewJSProxy(Handle<JSReceiver> target,
|
2015-11-13 14:13:50 +00:00
|
|
|
Handle<JSReceiver> handler) {
|
2014-04-16 13:35:36 +00:00
|
|
|
// Allocate the proxy object.
|
2015-12-08 16:04:08 +00:00
|
|
|
Handle<Map> map;
|
|
|
|
if (target->IsCallable()) {
|
|
|
|
if (target->IsConstructor()) {
|
|
|
|
map = Handle<Map>(isolate()->proxy_constructor_map());
|
|
|
|
} else {
|
|
|
|
map = Handle<Map>(isolate()->proxy_callable_map());
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
map = Handle<Map>(isolate()->proxy_map());
|
|
|
|
}
|
2015-11-19 16:11:00 +00:00
|
|
|
DCHECK(map->prototype()->IsNull());
|
2014-04-16 13:35:36 +00:00
|
|
|
Handle<JSProxy> result = New<JSProxy>(map, NEW_SPACE);
|
2016-01-12 15:43:41 +00:00
|
|
|
result->initialize_properties();
|
2015-11-12 22:11:53 +00:00
|
|
|
result->set_target(*target);
|
2014-04-16 13:35:36 +00:00
|
|
|
result->set_handler(*handler);
|
|
|
|
result->set_hash(*undefined_value(), SKIP_WRITE_BARRIER);
|
|
|
|
return result;
|
2011-05-13 10:58:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-14 16:42:15 +00:00
|
|
|
Handle<JSGlobalProxy> Factory::NewUninitializedJSGlobalProxy() {
|
|
|
|
// Create an empty shell of a JSGlobalProxy that needs to be reinitialized
|
|
|
|
// via ReinitializeJSGlobalProxy later.
|
|
|
|
Handle<Map> map = NewMap(JS_GLOBAL_PROXY_TYPE, JSGlobalProxy::kSize);
|
|
|
|
// Maintain invariant expected from any JSGlobalProxy.
|
|
|
|
map->set_is_access_check_needed(true);
|
2015-07-13 08:26:21 +00:00
|
|
|
CALL_HEAP_FUNCTION(
|
|
|
|
isolate(), isolate()->heap()->AllocateJSObjectFromMap(*map, NOT_TENURED),
|
|
|
|
JSGlobalProxy);
|
2015-01-14 16:42:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-15 11:51:34 +00:00
|
|
|
void Factory::ReinitializeJSGlobalProxy(Handle<JSGlobalProxy> object,
|
|
|
|
Handle<JSFunction> constructor) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(constructor->has_initial_map());
|
2014-04-15 11:51:34 +00:00
|
|
|
Handle<Map> map(constructor->initial_map(), isolate());
|
2016-01-05 16:15:00 +00:00
|
|
|
Handle<Map> old_map(object->map(), isolate());
|
2014-04-15 11:51:34 +00:00
|
|
|
|
2014-05-05 18:27:57 +00:00
|
|
|
// The proxy's hash should be retained across reinitialization.
|
|
|
|
Handle<Object> hash(object->hash(), isolate());
|
|
|
|
|
2016-01-05 16:15:00 +00:00
|
|
|
JSObject::InvalidatePrototypeChains(*old_map);
|
|
|
|
if (old_map->is_prototype_map()) {
|
|
|
|
map = Map::Copy(map, "CopyAsPrototypeForJSGlobalProxy");
|
|
|
|
map->set_is_prototype_map(true);
|
|
|
|
}
|
|
|
|
JSObject::UpdatePrototypeUserRegistration(old_map, map, isolate());
|
|
|
|
|
2014-04-15 11:51:34 +00:00
|
|
|
// Check that the already allocated object has the same size and type as
|
|
|
|
// objects allocated using the constructor.
|
2016-01-05 16:15:00 +00:00
|
|
|
DCHECK(map->instance_size() == old_map->instance_size());
|
|
|
|
DCHECK(map->instance_type() == old_map->instance_type());
|
2014-04-15 11:51:34 +00:00
|
|
|
|
|
|
|
// Allocate the backing storage for the properties.
|
2015-07-13 08:26:21 +00:00
|
|
|
Handle<FixedArray> properties = empty_fixed_array();
|
2014-04-15 11:51:34 +00:00
|
|
|
|
|
|
|
// In order to keep heap in consistent state there must be no allocations
|
|
|
|
// before object re-initialization is finished.
|
|
|
|
DisallowHeapAllocation no_allocation;
|
|
|
|
|
|
|
|
// Reset the map for the object.
|
2014-06-24 14:53:48 +00:00
|
|
|
object->synchronized_set_map(*map);
|
2014-04-15 11:51:34 +00:00
|
|
|
|
|
|
|
Heap* heap = isolate()->heap();
|
|
|
|
// Reinitialize the object from the constructor map.
|
|
|
|
heap->InitializeJSObjectFromMap(*object, *properties, *map);
|
2014-05-05 18:27:57 +00:00
|
|
|
|
|
|
|
// Restore the saved hash.
|
|
|
|
object->set_hash(*hash);
|
2014-04-15 11:51:34 +00:00
|
|
|
}
|
|
|
|
|
2016-02-05 10:48:27 +00:00
|
|
|
|
2010-03-23 06:04:44 +00:00
|
|
|
Handle<SharedFunctionInfo> Factory::NewSharedFunctionInfo(
|
2014-09-10 16:39:42 +00:00
|
|
|
Handle<String> name, int number_of_literals, FunctionKind kind,
|
|
|
|
Handle<Code> code, Handle<ScopeInfo> scope_info,
|
2016-02-05 10:48:27 +00:00
|
|
|
Handle<TypeFeedbackVector> feedback_vector) {
|
2014-09-10 16:39:42 +00:00
|
|
|
DCHECK(IsValidFunctionKind(kind));
|
2015-11-24 17:16:40 +00:00
|
|
|
Handle<SharedFunctionInfo> shared = NewSharedFunctionInfo(
|
|
|
|
name, code, IsConstructable(kind, scope_info->language_mode()));
|
2010-07-13 13:06:33 +00:00
|
|
|
shared->set_scope_info(*scope_info);
|
2016-02-05 10:48:27 +00:00
|
|
|
shared->set_feedback_vector(*feedback_vector);
|
2014-09-10 16:39:42 +00:00
|
|
|
shared->set_kind(kind);
|
2015-02-25 22:41:40 +00:00
|
|
|
shared->set_num_literals(number_of_literals);
|
2014-09-10 16:39:42 +00:00
|
|
|
if (IsGeneratorFunction(kind)) {
|
2013-04-17 15:01:25 +00:00
|
|
|
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(
|
2015-05-18 08:34:05 +00:00
|
|
|
MessageTemplate::Template message, Handle<Object> argument,
|
|
|
|
int start_position, int end_position, Handle<Object> script,
|
2011-02-02 13:31:52 +00:00
|
|
|
Handle<Object> stack_frames) {
|
2014-04-16 13:35:36 +00:00
|
|
|
Handle<Map> map = message_object_map();
|
2015-05-18 08:34:05 +00:00
|
|
|
Handle<JSMessageObject> message_obj = New<JSMessageObject>(map, NEW_SPACE);
|
|
|
|
message_obj->set_properties(*empty_fixed_array(), SKIP_WRITE_BARRIER);
|
|
|
|
message_obj->initialize_elements();
|
|
|
|
message_obj->set_elements(*empty_fixed_array(), SKIP_WRITE_BARRIER);
|
|
|
|
message_obj->set_type(message);
|
|
|
|
message_obj->set_argument(*argument);
|
|
|
|
message_obj->set_start_position(start_position);
|
|
|
|
message_obj->set_end_position(end_position);
|
|
|
|
message_obj->set_script(*script);
|
|
|
|
message_obj->set_stack_frames(*stack_frames);
|
|
|
|
return message_obj;
|
2011-02-02 13:31:52 +00:00
|
|
|
}
|
|
|
|
|
2013-07-05 09:52:11 +00:00
|
|
|
|
2014-05-09 17:39:54 +00:00
|
|
|
Handle<SharedFunctionInfo> Factory::NewSharedFunctionInfo(
|
2015-11-24 17:16:40 +00:00
|
|
|
Handle<String> name, MaybeHandle<Code> maybe_code, bool is_constructor) {
|
2016-02-11 19:54:26 +00:00
|
|
|
// Function names are assumed to be flat elsewhere. Must flatten before
|
|
|
|
// allocating SharedFunctionInfo to avoid GC seeing the uninitialized SFI.
|
|
|
|
name = String::Flatten(name, TENURED);
|
|
|
|
|
2014-04-16 13:35:36 +00:00
|
|
|
Handle<Map> map = shared_function_info_map();
|
2015-04-07 11:31:57 +00:00
|
|
|
Handle<SharedFunctionInfo> share = New<SharedFunctionInfo>(map, OLD_SPACE);
|
2014-04-16 13:35:36 +00:00
|
|
|
|
|
|
|
// Set pointer fields.
|
|
|
|
share->set_name(*name);
|
2014-05-09 17:39:54 +00:00
|
|
|
Handle<Code> code;
|
|
|
|
if (!maybe_code.ToHandle(&code)) {
|
2015-11-24 17:16:40 +00:00
|
|
|
code = isolate()->builtins()->Illegal();
|
2014-05-09 17:39:54 +00:00
|
|
|
}
|
|
|
|
share->set_code(*code);
|
2015-11-19 19:31:13 +00:00
|
|
|
share->set_optimized_code_map(*cleared_optimized_code_map());
|
2014-04-16 13:35:36 +00:00
|
|
|
share->set_scope_info(ScopeInfo::Empty(isolate()));
|
2015-11-24 17:16:40 +00:00
|
|
|
Handle<Code> construct_stub =
|
|
|
|
is_constructor ? isolate()->builtins()->JSConstructStubGeneric()
|
|
|
|
: isolate()->builtins()->ConstructedNonConstructable();
|
|
|
|
share->set_construct_stub(*construct_stub);
|
2014-04-16 13:35:36 +00:00
|
|
|
share->set_instance_class_name(*Object_string());
|
|
|
|
share->set_function_data(*undefined_value(), SKIP_WRITE_BARRIER);
|
|
|
|
share->set_script(*undefined_value(), SKIP_WRITE_BARRIER);
|
|
|
|
share->set_debug_info(*undefined_value(), SKIP_WRITE_BARRIER);
|
|
|
|
share->set_inferred_name(*empty_string(), SKIP_WRITE_BARRIER);
|
2015-09-28 11:41:40 +00:00
|
|
|
StaticFeedbackVectorSpec empty_spec;
|
2015-10-07 10:33:22 +00:00
|
|
|
Handle<TypeFeedbackMetadata> feedback_metadata =
|
|
|
|
TypeFeedbackMetadata::New(isolate(), &empty_spec);
|
2016-02-05 10:48:27 +00:00
|
|
|
Handle<TypeFeedbackVector> feedback_vector =
|
|
|
|
TypeFeedbackVector::New(isolate(), feedback_metadata);
|
|
|
|
share->set_feedback_vector(*feedback_vector, SKIP_WRITE_BARRIER);
|
2014-11-07 16:03:11 +00:00
|
|
|
#if TRACE_MAPS
|
|
|
|
share->set_unique_id(isolate()->GetNextUniqueSharedFunctionInfoId());
|
|
|
|
#endif
|
2014-04-23 07:07:54 +00:00
|
|
|
share->set_profiler_ticks(0);
|
2014-04-16 13:35:36 +00:00
|
|
|
share->set_ast_node_count(0);
|
|
|
|
share->set_counters(0);
|
|
|
|
|
|
|
|
// Set integer fields (smi or int, depending on the architecture).
|
|
|
|
share->set_length(0);
|
2015-02-11 09:47:32 +00:00
|
|
|
share->set_internal_formal_parameter_count(0);
|
2014-04-16 13:35:36 +00:00
|
|
|
share->set_expected_nof_properties(0);
|
|
|
|
share->set_num_literals(0);
|
|
|
|
share->set_start_position_and_type(0);
|
|
|
|
share->set_end_position(0);
|
|
|
|
share->set_function_token_position(0);
|
|
|
|
// All compiler hints default to false or 0.
|
|
|
|
share->set_compiler_hints(0);
|
|
|
|
share->set_opt_count_and_bailout_reason(0);
|
|
|
|
|
2015-10-27 11:40:31 +00:00
|
|
|
// Link into the list.
|
|
|
|
Handle<Object> new_noscript_list =
|
|
|
|
WeakFixedArray::Add(noscript_shared_function_infos(), share);
|
|
|
|
isolate()->heap()->set_noscript_shared_function_infos(*new_noscript_list);
|
|
|
|
|
2014-04-16 13:35:36 +00:00
|
|
|
return share;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-16 14:04:54 +00:00
|
|
|
static inline int NumberCacheHash(Handle<FixedArray> cache,
|
|
|
|
Handle<Object> number) {
|
|
|
|
int mask = (cache->length() >> 1) - 1;
|
|
|
|
if (number->IsSmi()) {
|
|
|
|
return Handle<Smi>::cast(number)->value() & mask;
|
|
|
|
} else {
|
|
|
|
DoubleRepresentation rep(number->Number());
|
|
|
|
return
|
|
|
|
(static_cast<int>(rep.bits) ^ static_cast<int>(rep.bits >> 32)) & mask;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Handle<Object> Factory::GetNumberStringCache(Handle<Object> number) {
|
|
|
|
DisallowHeapAllocation no_gc;
|
|
|
|
int hash = NumberCacheHash(number_string_cache(), number);
|
|
|
|
Object* key = number_string_cache()->get(hash * 2);
|
|
|
|
if (key == *number || (key->IsHeapNumber() && number->IsHeapNumber() &&
|
|
|
|
key->Number() == number->Number())) {
|
|
|
|
return Handle<String>(
|
|
|
|
String::cast(number_string_cache()->get(hash * 2 + 1)), isolate());
|
|
|
|
}
|
|
|
|
return undefined_value();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Factory::SetNumberStringCache(Handle<Object> number,
|
|
|
|
Handle<String> string) {
|
|
|
|
int hash = NumberCacheHash(number_string_cache(), number);
|
|
|
|
if (number_string_cache()->get(hash * 2) != *undefined_value()) {
|
|
|
|
int full_size = isolate()->heap()->FullSizeNumberStringCacheLength();
|
|
|
|
if (number_string_cache()->length() != full_size) {
|
|
|
|
Handle<FixedArray> new_cache = NewFixedArray(full_size, TENURED);
|
|
|
|
isolate()->heap()->set_number_string_cache(*new_cache);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
number_string_cache()->set(hash * 2, *number);
|
|
|
|
number_string_cache()->set(hash * 2 + 1, *string);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-15 11:16:02 +00:00
|
|
|
Handle<String> Factory::NumberToString(Handle<Object> number,
|
|
|
|
bool check_number_string_cache) {
|
2014-04-16 14:04:54 +00:00
|
|
|
isolate()->counters()->number_to_string_runtime()->Increment();
|
|
|
|
if (check_number_string_cache) {
|
|
|
|
Handle<Object> cached = GetNumberStringCache(number);
|
|
|
|
if (!cached->IsUndefined()) return Handle<String>::cast(cached);
|
|
|
|
}
|
2009-10-02 13:43:16 +00:00
|
|
|
|
2014-04-16 14:04:54 +00:00
|
|
|
char arr[100];
|
2014-08-26 09:19:24 +00:00
|
|
|
Vector<char> buffer(arr, arraysize(arr));
|
2014-04-16 14:04:54 +00:00
|
|
|
const char* str;
|
|
|
|
if (number->IsSmi()) {
|
|
|
|
int num = Handle<Smi>::cast(number)->value();
|
|
|
|
str = IntToCString(num, buffer);
|
|
|
|
} else {
|
|
|
|
double num = Handle<HeapNumber>::cast(number)->value();
|
|
|
|
str = DoubleToCString(num, buffer);
|
|
|
|
}
|
2009-10-02 13:43:16 +00:00
|
|
|
|
2014-04-16 14:04:54 +00:00
|
|
|
// We tenure the allocated string since it is referenced from the
|
|
|
|
// number-string cache which lives in the old space.
|
2014-04-17 13:27:02 +00:00
|
|
|
Handle<String> js_string = NewStringFromAsciiChecked(str, TENURED);
|
2014-04-16 14:04:54 +00:00
|
|
|
SetNumberStringCache(number, js_string);
|
|
|
|
return js_string;
|
2011-09-22 10:45:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-01 10:15:45 +00:00
|
|
|
Handle<DebugInfo> Factory::NewDebugInfo(Handle<SharedFunctionInfo> shared) {
|
|
|
|
// Allocate initial fixed array for active break points before allocating the
|
|
|
|
// debug info object to avoid allocation while setting up the debug info
|
|
|
|
// object.
|
|
|
|
Handle<FixedArray> break_points(
|
2014-06-02 12:51:19 +00:00
|
|
|
NewFixedArray(DebugInfo::kEstimatedNofBreakPointsInFunction));
|
2008-09-01 10:15:45 +00:00
|
|
|
|
|
|
|
// Create and set up the debug info object. Debug info contains function, a
|
|
|
|
// copy of the original code, the executing code and initial fixed array for
|
|
|
|
// active break points.
|
|
|
|
Handle<DebugInfo> debug_info =
|
2011-03-18 20:35:07 +00:00
|
|
|
Handle<DebugInfo>::cast(NewStruct(DEBUG_INFO_TYPE));
|
2008-09-01 10:15:45 +00:00
|
|
|
debug_info->set_shared(*shared);
|
2016-02-10 14:19:31 +00:00
|
|
|
if (shared->HasBytecodeArray()) {
|
|
|
|
debug_info->set_abstract_code(AbstractCode::cast(shared->bytecode_array()));
|
|
|
|
} else {
|
|
|
|
debug_info->set_abstract_code(AbstractCode::cast(shared->code()));
|
|
|
|
}
|
2008-09-01 10:15:45 +00:00
|
|
|
debug_info->set_break_points(*break_points);
|
|
|
|
|
|
|
|
// Link debug info to function.
|
|
|
|
shared->set_debug_info(*debug_info);
|
|
|
|
|
|
|
|
return debug_info;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-07-07 13:27:37 +00:00
|
|
|
Handle<JSObject> Factory::NewArgumentsObject(Handle<JSFunction> callee,
|
2008-07-03 15:10:15 +00:00
|
|
|
int length) {
|
2015-02-14 00:14:46 +00:00
|
|
|
bool strict_mode_callee = is_strict(callee->shared()->language_mode()) ||
|
2015-11-04 14:56:11 +00:00
|
|
|
!callee->shared()->has_simple_parameters();
|
2014-07-07 13:27:37 +00:00
|
|
|
Handle<Map> map = strict_mode_callee ? isolate()->strict_arguments_map()
|
|
|
|
: isolate()->sloppy_arguments_map();
|
|
|
|
AllocationSiteUsageContext context(isolate(), Handle<AllocationSite>(),
|
|
|
|
false);
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(!isolate()->has_pending_exception());
|
2014-07-07 13:27:37 +00:00
|
|
|
Handle<JSObject> result = NewJSObjectFromMap(map);
|
|
|
|
Handle<Smi> value(Smi::FromInt(length), isolate());
|
2014-07-22 08:28:49 +00:00
|
|
|
Object::SetProperty(result, length_string(), value, STRICT).Assert();
|
2014-07-07 13:27:37 +00:00
|
|
|
if (!strict_mode_callee) {
|
2014-07-22 08:28:49 +00:00
|
|
|
Object::SetProperty(result, callee_string(), callee, STRICT).Assert();
|
2014-07-07 13:27:37 +00:00
|
|
|
}
|
|
|
|
return result;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-05 09:40:13 +00:00
|
|
|
Handle<JSWeakMap> Factory::NewJSWeakMap() {
|
|
|
|
// TODO(adamk): Currently the map is only created three times per
|
|
|
|
// isolate. If it's created more often, the map should be moved into the
|
|
|
|
// strong root list.
|
|
|
|
Handle<Map> map = NewMap(JS_WEAK_MAP_TYPE, JSWeakMap::kSize);
|
|
|
|
return Handle<JSWeakMap>::cast(NewJSObjectFromMap(map));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-25 07:46:07 +00:00
|
|
|
Handle<Map> Factory::ObjectLiteralMapFromCache(Handle<Context> context,
|
2014-11-10 18:03:50 +00:00
|
|
|
int number_of_properties,
|
2015-05-20 07:07:54 +00:00
|
|
|
bool is_strong,
|
2014-11-10 18:03:50 +00:00
|
|
|
bool* is_result_from_cache) {
|
|
|
|
const int kMapCacheSize = 128;
|
|
|
|
|
2015-05-05 13:59:08 +00:00
|
|
|
// We do not cache maps for too many properties or when running builtin code.
|
2015-05-22 10:51:49 +00:00
|
|
|
if (number_of_properties > kMapCacheSize ||
|
2015-05-05 13:59:08 +00:00
|
|
|
isolate()->bootstrapper()->IsActive()) {
|
2014-11-10 18:03:50 +00:00
|
|
|
*is_result_from_cache = false;
|
2015-05-20 07:07:54 +00:00
|
|
|
Handle<Map> map = Map::Create(isolate(), number_of_properties);
|
2015-05-22 10:51:49 +00:00
|
|
|
if (is_strong) map->set_is_strong();
|
2015-05-20 07:07:54 +00:00
|
|
|
return map;
|
2014-11-10 18:03:50 +00:00
|
|
|
}
|
|
|
|
*is_result_from_cache = true;
|
|
|
|
if (number_of_properties == 0) {
|
|
|
|
// Reuse the initial map of the Object function if the literal has no
|
2015-05-22 10:51:49 +00:00
|
|
|
// predeclared properties, or the strong map if strong.
|
|
|
|
return handle(is_strong
|
|
|
|
? context->js_object_strong_map()
|
|
|
|
: context->object_function()->initial_map(), isolate());
|
2014-11-10 18:03:50 +00:00
|
|
|
}
|
2015-05-22 10:51:49 +00:00
|
|
|
|
2014-11-10 18:03:50 +00:00
|
|
|
int cache_index = number_of_properties - 1;
|
2015-06-02 10:15:06 +00:00
|
|
|
Handle<Object> maybe_cache(is_strong ? context->strong_map_cache()
|
|
|
|
: context->map_cache(), isolate());
|
|
|
|
if (maybe_cache->IsUndefined()) {
|
|
|
|
// Allocate the new map cache for the native context.
|
|
|
|
maybe_cache = NewFixedArray(kMapCacheSize, TENURED);
|
|
|
|
if (is_strong) {
|
|
|
|
context->set_strong_map_cache(*maybe_cache);
|
|
|
|
} else {
|
|
|
|
context->set_map_cache(*maybe_cache);
|
2015-05-22 10:51:49 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Check to see whether there is a matching element in the cache.
|
2015-06-02 10:15:06 +00:00
|
|
|
Handle<FixedArray> cache = Handle<FixedArray>::cast(maybe_cache);
|
2014-11-10 18:03:50 +00:00
|
|
|
Object* result = cache->get(cache_index);
|
|
|
|
if (result->IsWeakCell()) {
|
|
|
|
WeakCell* cell = WeakCell::cast(result);
|
|
|
|
if (!cell->cleared()) {
|
|
|
|
return handle(Map::cast(cell->value()), isolate());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-06-02 10:15:06 +00:00
|
|
|
// Create a new map and add it to the cache.
|
|
|
|
Handle<FixedArray> cache = Handle<FixedArray>::cast(maybe_cache);
|
|
|
|
Handle<Map> map = Map::Create(isolate(), number_of_properties);
|
|
|
|
if (is_strong) map->set_is_strong();
|
2014-11-10 18:03:50 +00:00
|
|
|
Handle<WeakCell> cell = NewWeakCell(map);
|
|
|
|
cache->set(cache_index, *cell);
|
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);
|
2015-11-25 13:46:09 +00:00
|
|
|
store->set(JSRegExp::kFlagsIndex, Smi::FromInt(flags));
|
2008-10-24 08:40:02 +00:00
|
|
|
store->set(JSRegExp::kAtomPatternIndex, *data);
|
|
|
|
regexp->set_data(*store);
|
|
|
|
}
|
|
|
|
|
2014-11-10 18:03:50 +00:00
|
|
|
|
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);
|
2015-11-25 13:46:09 +00:00
|
|
|
store->set(JSRegExp::kFlagsIndex, Smi::FromInt(flags));
|
2014-09-10 12:38:12 +00:00
|
|
|
store->set(JSRegExp::kIrregexpLatin1CodeIndex, uninitialized);
|
2011-07-05 06:19:53 +00:00
|
|
|
store->set(JSRegExp::kIrregexpUC16CodeIndex, uninitialized);
|
2014-09-10 12:38:12 +00:00
|
|
|
store->set(JSRegExp::kIrregexpLatin1CodeSavedIndex, uninitialized);
|
2011-07-05 06:19:53 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-04-13 16:22:11 +00:00
|
|
|
Handle<Object> Factory::GlobalConstantFor(Handle<Name> name) {
|
|
|
|
if (Name::Equals(name, undefined_string())) return undefined_value();
|
|
|
|
if (Name::Equals(name, nan_string())) return nan_value();
|
|
|
|
if (Name::Equals(name, infinity_string())) return infinity_value();
|
2011-09-23 08:00:06 +00:00
|
|
|
return Handle<Object>::null();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-10-10 09:21:48 +00:00
|
|
|
Handle<Object> Factory::ToBoolean(bool value) {
|
2013-02-25 14:46:09 +00:00
|
|
|
return value ? true_value() : false_value();
|
2011-10-10 09:21:48 +00:00
|
|
|
}
|
|
|
|
|
2014-04-17 17:45:32 +00:00
|
|
|
|
2015-06-01 22:46:54 +00:00
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|