2012-01-26 21:47:57 +00:00
|
|
|
// Copyright 2012 the V8 project authors. All rights reserved.
|
2008-07-03 15:10:15 +00:00
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are
|
|
|
|
// met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above
|
|
|
|
// copyright notice, this list of conditions and the following
|
|
|
|
// disclaimer in the documentation and/or other materials provided
|
|
|
|
// with the distribution.
|
|
|
|
// * Neither the name of Google Inc. nor the names of its
|
|
|
|
// contributors may be used to endorse or promote products derived
|
|
|
|
// from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
#include "v8.h"
|
|
|
|
|
|
|
|
#include "api.h"
|
2009-08-27 11:24:43 +00:00
|
|
|
#include "arguments.h"
|
2008-07-03 15:10:15 +00:00
|
|
|
#include "bootstrapper.h"
|
|
|
|
#include "builtins.h"
|
2011-01-18 16:11:01 +00:00
|
|
|
#include "gdb-jit.h"
|
2008-07-03 15:10:15 +00:00
|
|
|
#include "ic-inl.h"
|
2012-03-20 14:35:05 +00:00
|
|
|
#include "heap-profiler.h"
|
2011-09-19 18:36:47 +00:00
|
|
|
#include "mark-compact.h"
|
2012-08-17 10:43:32 +00:00
|
|
|
#include "stub-cache.h"
|
2010-12-07 11:31:57 +00:00
|
|
|
#include "vm-state-inl.h"
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2009-05-25 10:05:56 +00:00
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2010-01-15 12:25:24 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
// Arguments object passed to C++ builtins.
|
|
|
|
template <BuiltinExtraArguments extra_args>
|
|
|
|
class BuiltinArguments : public Arguments {
|
|
|
|
public:
|
2010-01-15 13:43:28 +00:00
|
|
|
BuiltinArguments(int length, Object** arguments)
|
|
|
|
: Arguments(length, arguments) { }
|
|
|
|
|
2010-01-15 12:25:24 +00:00
|
|
|
Object*& operator[] (int index) {
|
|
|
|
ASSERT(index < length());
|
|
|
|
return Arguments::operator[](index);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class S> Handle<S> at(int index) {
|
|
|
|
ASSERT(index < length());
|
|
|
|
return Arguments::at<S>(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
Handle<Object> receiver() {
|
|
|
|
return Arguments::at<Object>(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
Handle<JSFunction> called_function() {
|
|
|
|
STATIC_ASSERT(extra_args == NEEDS_CALLED_FUNCTION);
|
|
|
|
return Arguments::at<JSFunction>(Arguments::length() - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Gets the total number of arguments including the receiver (but
|
|
|
|
// excluding extra arguments).
|
|
|
|
int length() const {
|
|
|
|
STATIC_ASSERT(extra_args == NO_EXTRA_ARGUMENTS);
|
|
|
|
return Arguments::length();
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
void Verify() {
|
|
|
|
// Check we have at least the receiver.
|
|
|
|
ASSERT(Arguments::length() >= 1);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Specialize BuiltinArguments for the called function extra argument.
|
|
|
|
|
|
|
|
template <>
|
|
|
|
int BuiltinArguments<NEEDS_CALLED_FUNCTION>::length() const {
|
|
|
|
return Arguments::length() - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
template <>
|
|
|
|
void BuiltinArguments<NEEDS_CALLED_FUNCTION>::Verify() {
|
|
|
|
// Check we have at least the receiver and the called function.
|
|
|
|
ASSERT(Arguments::length() >= 2);
|
|
|
|
// Make sure cast to JSFunction succeeds.
|
|
|
|
called_function();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
#define DEF_ARG_TYPE(name, spec) \
|
|
|
|
typedef BuiltinArguments<spec> name##ArgumentsType;
|
|
|
|
BUILTIN_LIST_C(DEF_ARG_TYPE)
|
|
|
|
#undef DEF_ARG_TYPE
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
2010-01-15 12:25:24 +00:00
|
|
|
// Support macro for defining builtins in C++.
|
2008-07-03 15:10:15 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// A builtin function is defined by writing:
|
|
|
|
//
|
2008-09-16 10:12:32 +00:00
|
|
|
// BUILTIN(name) {
|
2008-07-03 15:10:15 +00:00
|
|
|
// ...
|
|
|
|
// }
|
|
|
|
//
|
2010-01-15 12:25:24 +00:00
|
|
|
// In the body of the builtin function the arguments can be accessed
|
|
|
|
// through the BuiltinArguments object args.
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2010-01-15 12:25:24 +00:00
|
|
|
#ifdef DEBUG
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2013-04-30 11:34:43 +00:00
|
|
|
#define BUILTIN(name) \
|
|
|
|
MUST_USE_RESULT static MaybeObject* Builtin_Impl_##name( \
|
|
|
|
name##ArgumentsType args, Isolate* isolate); \
|
|
|
|
MUST_USE_RESULT static MaybeObject* Builtin_##name( \
|
|
|
|
int args_length, Object** args_object, Isolate* isolate) { \
|
|
|
|
name##ArgumentsType args(args_length, args_object); \
|
|
|
|
ASSERT(isolate == Isolate::Current()); \
|
|
|
|
args.Verify(); \
|
|
|
|
return Builtin_Impl_##name(args, isolate); \
|
|
|
|
} \
|
|
|
|
MUST_USE_RESULT static MaybeObject* Builtin_Impl_##name( \
|
2011-03-18 20:35:07 +00:00
|
|
|
name##ArgumentsType args, Isolate* isolate)
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2010-01-15 12:25:24 +00:00
|
|
|
#else // For release mode.
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2013-04-30 11:34:43 +00:00
|
|
|
#define BUILTIN(name) \
|
|
|
|
static MaybeObject* Builtin_impl##name( \
|
|
|
|
name##ArgumentsType args, Isolate* isolate); \
|
|
|
|
static MaybeObject* Builtin_##name( \
|
|
|
|
int args_length, Object** args_object, Isolate* isolate) { \
|
|
|
|
name##ArgumentsType args(args_length, args_object); \
|
|
|
|
return Builtin_impl##name(args, isolate); \
|
|
|
|
} \
|
|
|
|
static MaybeObject* Builtin_impl##name( \
|
|
|
|
name##ArgumentsType args, Isolate* isolate)
|
2010-01-15 12:25:24 +00:00
|
|
|
#endif
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
static inline bool CalledAsConstructor(Isolate* isolate) {
|
2009-07-29 12:50:20 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
// Calculate the result using a full stack frame iterator and check
|
|
|
|
// that the state of the stack is as we assume it to be in the
|
|
|
|
// code below.
|
2013-02-15 09:27:10 +00:00
|
|
|
StackFrameIterator it(isolate);
|
2008-09-16 10:12:32 +00:00
|
|
|
ASSERT(it.frame()->is_exit());
|
|
|
|
it.Advance();
|
|
|
|
StackFrame* frame = it.frame();
|
2009-07-29 12:50:20 +00:00
|
|
|
bool reference_result = frame->is_construct();
|
|
|
|
#endif
|
2011-03-18 20:35:07 +00:00
|
|
|
Address fp = Isolate::c_entry_fp(isolate->thread_local_top());
|
2009-07-29 12:50:20 +00:00
|
|
|
// Because we know fp points to an exit frame we can use the relevant
|
|
|
|
// part of ExitFrame::ComputeCallerState directly.
|
|
|
|
const int kCallerOffset = ExitFrameConstants::kCallerFPOffset;
|
|
|
|
Address caller_fp = Memory::Address_at(fp + kCallerOffset);
|
|
|
|
// This inlines the part of StackFrame::ComputeType that grabs the
|
|
|
|
// type of the current frame. Note that StackFrame::ComputeType
|
|
|
|
// has been specialized for each architecture so if any one of them
|
|
|
|
// changes this code has to be changed as well.
|
|
|
|
const int kMarkerOffset = StandardFrameConstants::kMarkerOffset;
|
|
|
|
const Smi* kConstructMarker = Smi::FromInt(StackFrame::CONSTRUCT);
|
|
|
|
Object* marker = Memory::Object_at(caller_fp + kMarkerOffset);
|
|
|
|
bool result = (marker == kConstructMarker);
|
|
|
|
ASSERT_EQ(result, reference_result);
|
|
|
|
return result;
|
2008-09-16 10:12:32 +00:00
|
|
|
}
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2008-09-16 07:24:46 +00:00
|
|
|
BUILTIN(Illegal) {
|
2008-07-03 15:10:15 +00:00
|
|
|
UNREACHABLE();
|
2011-03-18 20:35:07 +00:00
|
|
|
return isolate->heap()->undefined_value(); // Make compiler happy.
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-16 07:24:46 +00:00
|
|
|
BUILTIN(EmptyFunction) {
|
2011-03-18 20:35:07 +00:00
|
|
|
return isolate->heap()->undefined_value();
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-12-29 12:19:34 +00:00
|
|
|
static MaybeObject* ArrayCodeGenericCommon(Arguments* args,
|
|
|
|
Isolate* isolate,
|
|
|
|
JSFunction* constructor) {
|
2013-03-01 16:06:34 +00:00
|
|
|
ASSERT(args->length() >= 1);
|
2011-03-18 20:35:07 +00:00
|
|
|
Heap* heap = isolate->heap();
|
|
|
|
isolate->counters()->array_function_runtime()->Increment();
|
2009-09-16 11:17:57 +00:00
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
JSArray* array;
|
2011-03-18 20:35:07 +00:00
|
|
|
if (CalledAsConstructor(isolate)) {
|
2011-12-29 12:19:34 +00:00
|
|
|
array = JSArray::cast((*args)[0]);
|
2012-01-26 21:47:57 +00:00
|
|
|
// Initialize elements and length in case later allocations fail so that the
|
|
|
|
// array object is initialized in a valid state.
|
2013-03-01 16:06:34 +00:00
|
|
|
MaybeObject* maybe_array = array->Initialize(0);
|
|
|
|
if (maybe_array->IsFailure()) return maybe_array;
|
|
|
|
|
|
|
|
if (FLAG_optimize_constructed_arrays) {
|
|
|
|
AllocationSiteInfo* info = AllocationSiteInfo::FindForJSObject(array);
|
|
|
|
ElementsKind to_kind = array->GetElementsKind();
|
|
|
|
if (info != NULL && info->GetElementsKindPayload(&to_kind)) {
|
|
|
|
if (IsMoreGeneralElementsKindTransition(array->GetElementsKind(),
|
|
|
|
to_kind)) {
|
|
|
|
// We have advice that we should change the elements kind
|
|
|
|
if (FLAG_trace_track_allocation_sites) {
|
|
|
|
PrintF("AllocationSiteInfo: pre-transitioning array %p(%s->%s)\n",
|
|
|
|
reinterpret_cast<void*>(array),
|
|
|
|
ElementsKindToString(array->GetElementsKind()),
|
|
|
|
ElementsKindToString(to_kind));
|
|
|
|
}
|
|
|
|
|
|
|
|
maybe_array = array->TransitionElementsKind(to_kind);
|
|
|
|
if (maybe_array->IsFailure()) return maybe_array;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-26 21:47:57 +00:00
|
|
|
if (!FLAG_smi_only_arrays) {
|
2012-08-17 09:03:08 +00:00
|
|
|
Context* native_context = isolate->context()->native_context();
|
2012-05-23 14:24:29 +00:00
|
|
|
if (array->GetElementsKind() == GetInitialFastElementsKind() &&
|
2012-08-17 09:03:08 +00:00
|
|
|
!native_context->js_array_maps()->IsUndefined()) {
|
2012-05-23 14:24:29 +00:00
|
|
|
FixedArray* map_array =
|
2012-08-17 09:03:08 +00:00
|
|
|
FixedArray::cast(native_context->js_array_maps());
|
2012-05-23 14:24:29 +00:00
|
|
|
array->set_map(Map::cast(map_array->
|
|
|
|
get(TERMINAL_FAST_ELEMENTS_KIND)));
|
2012-01-26 21:47:57 +00:00
|
|
|
}
|
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
} else {
|
|
|
|
// Allocate the JS Array
|
2012-02-15 13:45:42 +00:00
|
|
|
MaybeObject* maybe_obj = heap->AllocateJSObject(constructor);
|
2012-01-26 21:47:57 +00:00
|
|
|
if (!maybe_obj->To(&array)) return maybe_obj;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
2013-03-01 16:06:34 +00:00
|
|
|
Arguments adjusted_arguments(args->length() - 1, args->arguments() - 1);
|
|
|
|
ASSERT(adjusted_arguments.length() < 1 ||
|
|
|
|
adjusted_arguments[0] == (*args)[1]);
|
|
|
|
return ArrayConstructInitializeElements(array, &adjusted_arguments);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-12-29 12:19:34 +00:00
|
|
|
BUILTIN(InternalArrayCodeGeneric) {
|
|
|
|
return ArrayCodeGenericCommon(
|
|
|
|
&args,
|
|
|
|
isolate,
|
2012-08-17 09:03:08 +00:00
|
|
|
isolate->context()->native_context()->internal_array_function());
|
2011-12-29 12:19:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BUILTIN(ArrayCodeGeneric) {
|
|
|
|
return ArrayCodeGenericCommon(
|
|
|
|
&args,
|
|
|
|
isolate,
|
2012-08-17 09:03:08 +00:00
|
|
|
isolate->context()->native_context()->array_function());
|
2011-12-29 12:19:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-11-15 12:19:14 +00:00
|
|
|
static void MoveDoubleElements(FixedDoubleArray* dst,
|
|
|
|
int dst_index,
|
|
|
|
FixedDoubleArray* src,
|
|
|
|
int src_index,
|
|
|
|
int len) {
|
|
|
|
if (len == 0) return;
|
2013-04-16 12:30:51 +00:00
|
|
|
OS::MemMove(dst->data_start() + dst_index,
|
|
|
|
src->data_start() + src_index,
|
|
|
|
len * kDoubleSize);
|
2012-11-15 12:19:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
static void FillWithHoles(Heap* heap, FixedArray* dst, int from, int to) {
|
|
|
|
ASSERT(dst->map() != heap->fixed_cow_array_map());
|
|
|
|
MemsetPointer(dst->data_start() + from, heap->the_hole_value(), to - from);
|
2010-03-01 15:33:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-11-15 12:19:14 +00:00
|
|
|
static void FillWithHoles(FixedDoubleArray* dst, int from, int to) {
|
|
|
|
for (int i = from; i < to; i++) {
|
|
|
|
dst->set_the_hole(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static FixedArrayBase* LeftTrimFixedArray(Heap* heap,
|
|
|
|
FixedArrayBase* elms,
|
|
|
|
int to_trim) {
|
|
|
|
Map* map = elms->map();
|
|
|
|
int entry_size;
|
|
|
|
if (elms->IsFixedArray()) {
|
|
|
|
entry_size = kPointerSize;
|
|
|
|
} else {
|
|
|
|
entry_size = kDoubleSize;
|
|
|
|
}
|
2011-03-18 20:35:07 +00:00
|
|
|
ASSERT(elms->map() != HEAP->fixed_cow_array_map());
|
2010-08-20 12:38:33 +00:00
|
|
|
// For now this trick is only applied to fixed arrays in new and paged space.
|
2010-03-30 12:48:55 +00:00
|
|
|
// In large object space the object's start must coincide with chunk
|
|
|
|
// and thus the trick is just not applicable.
|
2011-03-18 20:35:07 +00:00
|
|
|
ASSERT(!HEAP->lo_space()->Contains(elms));
|
2010-03-30 12:48:55 +00:00
|
|
|
|
2012-11-15 12:19:14 +00:00
|
|
|
STATIC_ASSERT(FixedArrayBase::kMapOffset == 0);
|
|
|
|
STATIC_ASSERT(FixedArrayBase::kLengthOffset == kPointerSize);
|
|
|
|
STATIC_ASSERT(FixedArrayBase::kHeaderSize == 2 * kPointerSize);
|
2010-03-30 12:48:55 +00:00
|
|
|
|
|
|
|
Object** former_start = HeapObject::RawField(elms, 0);
|
|
|
|
|
|
|
|
const int len = elms->length();
|
|
|
|
|
2012-11-22 16:22:57 +00:00
|
|
|
if (to_trim * entry_size > FixedArrayBase::kHeaderSize &&
|
2012-11-15 12:19:14 +00:00
|
|
|
elms->IsFixedArray() &&
|
2011-03-18 20:35:07 +00:00
|
|
|
!heap->new_space()->Contains(elms)) {
|
2010-08-20 12:38:33 +00:00
|
|
|
// If we are doing a big trim in old space then we zap the space that was
|
|
|
|
// formerly part of the array so that the GC (aided by the card-based
|
|
|
|
// remembered set) won't find pointers to new-space there.
|
|
|
|
Object** zap = reinterpret_cast<Object**>(elms->address());
|
|
|
|
zap++; // Header of filler must be at least one word so skip that.
|
|
|
|
for (int i = 1; i < to_trim; i++) {
|
|
|
|
*zap++ = Smi::FromInt(0);
|
|
|
|
}
|
|
|
|
}
|
2010-03-30 12:48:55 +00:00
|
|
|
// Technically in new space this write might be omitted (except for
|
|
|
|
// debug mode which iterates through the heap), but to play safer
|
|
|
|
// we still do it.
|
2012-11-15 12:19:14 +00:00
|
|
|
heap->CreateFillerObjectAt(elms->address(), to_trim * entry_size);
|
2010-03-30 12:48:55 +00:00
|
|
|
|
2012-11-15 12:19:14 +00:00
|
|
|
int new_start_index = to_trim * (entry_size / kPointerSize);
|
|
|
|
former_start[new_start_index] = map;
|
|
|
|
former_start[new_start_index + 1] = Smi::FromInt(len - to_trim);
|
2010-03-30 12:48:55 +00:00
|
|
|
|
2011-09-19 18:36:47 +00:00
|
|
|
// Maintain marking consistency for HeapObjectIterator and
|
|
|
|
// IncrementalMarking.
|
2012-11-15 12:19:14 +00:00
|
|
|
int size_delta = to_trim * entry_size;
|
2011-09-19 18:36:47 +00:00
|
|
|
if (heap->marking()->TransferMark(elms->address(),
|
|
|
|
elms->address() + size_delta)) {
|
2012-01-18 09:21:07 +00:00
|
|
|
MemoryChunk::IncrementLiveBytesFromMutator(elms->address(), -size_delta);
|
2011-09-19 18:36:47 +00:00
|
|
|
}
|
|
|
|
|
2012-03-20 14:35:05 +00:00
|
|
|
HEAP_PROFILE(heap, ObjectMoveEvent(elms->address(),
|
|
|
|
elms->address() + size_delta));
|
2012-11-15 12:19:14 +00:00
|
|
|
return FixedArrayBase::cast(HeapObject::FromAddress(
|
|
|
|
elms->address() + to_trim * entry_size));
|
2010-03-30 12:48:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
static bool ArrayPrototypeHasNoElements(Heap* heap,
|
2012-08-17 09:03:08 +00:00
|
|
|
Context* native_context,
|
2010-05-12 12:22:09 +00:00
|
|
|
JSObject* array_proto) {
|
2010-03-01 15:33:30 +00:00
|
|
|
// This method depends on non writability of Object and Array prototype
|
|
|
|
// fields.
|
2011-03-18 20:35:07 +00:00
|
|
|
if (array_proto->elements() != heap->empty_fixed_array()) return false;
|
2010-03-01 15:33:30 +00:00
|
|
|
// Object.prototype
|
2011-02-08 19:56:44 +00:00
|
|
|
Object* proto = array_proto->GetPrototype();
|
2011-03-18 20:35:07 +00:00
|
|
|
if (proto == heap->null_value()) return false;
|
2011-02-08 19:56:44 +00:00
|
|
|
array_proto = JSObject::cast(proto);
|
2012-08-17 09:03:08 +00:00
|
|
|
if (array_proto != native_context->initial_object_prototype()) return false;
|
2011-03-18 20:35:07 +00:00
|
|
|
if (array_proto->elements() != heap->empty_fixed_array()) return false;
|
2011-05-24 12:28:10 +00:00
|
|
|
return array_proto->GetPrototype()->IsNull();
|
2010-03-01 15:33:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-10-25 15:22:03 +00:00
|
|
|
MUST_USE_RESULT
|
|
|
|
static inline MaybeObject* EnsureJSArrayWithWritableFastElements(
|
2011-10-10 08:31:06 +00:00
|
|
|
Heap* heap, Object* receiver, Arguments* args, int first_added_arg) {
|
2010-08-16 16:06:46 +00:00
|
|
|
if (!receiver->IsJSArray()) return NULL;
|
2010-03-11 13:13:21 +00:00
|
|
|
JSArray* array = JSArray::cast(receiver);
|
2010-12-22 15:45:48 +00:00
|
|
|
HeapObject* elms = array->elements();
|
2011-10-10 08:31:06 +00:00
|
|
|
Map* map = elms->map();
|
|
|
|
if (map == heap->fixed_array_map()) {
|
2012-05-23 14:24:29 +00:00
|
|
|
if (args == NULL || array->HasFastObjectElements()) return elms;
|
2011-10-10 08:31:06 +00:00
|
|
|
} else if (map == heap->fixed_cow_array_map()) {
|
|
|
|
MaybeObject* maybe_writable_result = array->EnsureWritableFastElements();
|
2012-05-23 14:24:29 +00:00
|
|
|
if (args == NULL || array->HasFastObjectElements() ||
|
2012-11-15 12:19:14 +00:00
|
|
|
!maybe_writable_result->To(&elms)) {
|
2011-10-10 08:31:06 +00:00
|
|
|
return maybe_writable_result;
|
|
|
|
}
|
2012-11-15 12:19:14 +00:00
|
|
|
} else if (map == heap->fixed_double_array_map()) {
|
|
|
|
if (args == NULL) return elms;
|
2011-10-10 08:31:06 +00:00
|
|
|
} else {
|
|
|
|
return NULL;
|
2010-03-11 13:13:21 +00:00
|
|
|
}
|
2011-10-10 08:31:06 +00:00
|
|
|
|
|
|
|
// Need to ensure that the arguments passed in args can be contained in
|
|
|
|
// the array.
|
|
|
|
int args_length = args->length();
|
|
|
|
if (first_added_arg >= args_length) return array->elements();
|
|
|
|
|
2012-11-15 12:19:14 +00:00
|
|
|
ElementsKind origin_kind = array->map()->elements_kind();
|
|
|
|
ASSERT(!IsFastObjectElementsKind(origin_kind));
|
|
|
|
ElementsKind target_kind = origin_kind;
|
|
|
|
int arg_count = args->length() - first_added_arg;
|
|
|
|
Object** arguments = args->arguments() - first_added_arg - (arg_count - 1);
|
|
|
|
for (int i = 0; i < arg_count; i++) {
|
|
|
|
Object* arg = arguments[i];
|
|
|
|
if (arg->IsHeapObject()) {
|
|
|
|
if (arg->IsHeapNumber()) {
|
|
|
|
target_kind = FAST_DOUBLE_ELEMENTS;
|
|
|
|
} else {
|
|
|
|
target_kind = FAST_ELEMENTS;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (target_kind != origin_kind) {
|
|
|
|
MaybeObject* maybe_failure = array->TransitionElementsKind(target_kind);
|
|
|
|
if (maybe_failure->IsFailure()) return maybe_failure;
|
|
|
|
return array->elements();
|
|
|
|
}
|
|
|
|
return elms;
|
2010-03-11 13:13:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
static inline bool IsJSArrayFastElementMovingAllowed(Heap* heap,
|
|
|
|
JSArray* receiver) {
|
2011-10-11 11:35:04 +00:00
|
|
|
if (!FLAG_clever_optimizations) return false;
|
2012-08-17 09:03:08 +00:00
|
|
|
Context* native_context = heap->isolate()->context()->native_context();
|
2010-05-12 12:22:09 +00:00
|
|
|
JSObject* array_proto =
|
2012-08-17 09:03:08 +00:00
|
|
|
JSObject::cast(native_context->array_function()->prototype());
|
2010-08-16 16:06:46 +00:00
|
|
|
return receiver->GetPrototype() == array_proto &&
|
2012-08-17 09:03:08 +00:00
|
|
|
ArrayPrototypeHasNoElements(heap, native_context, array_proto);
|
2010-05-12 12:22:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-10-25 15:22:03 +00:00
|
|
|
MUST_USE_RESULT static MaybeObject* CallJsBuiltin(
|
2011-03-18 20:35:07 +00:00
|
|
|
Isolate* isolate,
|
2010-10-25 15:22:03 +00:00
|
|
|
const char* name,
|
|
|
|
BuiltinArguments<NO_EXTRA_ARGUMENTS> args) {
|
2011-03-18 20:35:07 +00:00
|
|
|
HandleScope handleScope(isolate);
|
2010-03-01 15:33:30 +00:00
|
|
|
|
|
|
|
Handle<Object> js_builtin =
|
2012-08-17 09:03:08 +00:00
|
|
|
GetProperty(Handle<JSObject>(isolate->native_context()->builtins()),
|
2011-10-06 09:31:38 +00:00
|
|
|
name);
|
|
|
|
Handle<JSFunction> function = Handle<JSFunction>::cast(js_builtin);
|
|
|
|
int argc = args.length() - 1;
|
|
|
|
ScopedVector<Handle<Object> > argv(argc);
|
|
|
|
for (int i = 0; i < argc; ++i) {
|
|
|
|
argv[i] = args.at<Object>(i + 1);
|
2010-03-01 15:33:30 +00:00
|
|
|
}
|
2011-09-22 17:12:41 +00:00
|
|
|
bool pending_exception;
|
2010-03-01 15:33:30 +00:00
|
|
|
Handle<Object> result = Execution::Call(function,
|
|
|
|
args.receiver(),
|
2011-10-06 09:31:38 +00:00
|
|
|
argc,
|
2010-03-01 15:33:30 +00:00
|
|
|
argv.start(),
|
|
|
|
&pending_exception);
|
|
|
|
if (pending_exception) return Failure::Exception();
|
|
|
|
return *result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-16 10:12:32 +00:00
|
|
|
BUILTIN(ArrayPush) {
|
2011-03-18 20:35:07 +00:00
|
|
|
Heap* heap = isolate->heap();
|
2010-03-11 13:13:21 +00:00
|
|
|
Object* receiver = *args.receiver();
|
2012-11-15 12:19:14 +00:00
|
|
|
FixedArrayBase* elms_obj;
|
|
|
|
MaybeObject* maybe_elms_obj =
|
|
|
|
EnsureJSArrayWithWritableFastElements(heap, receiver, &args, 1);
|
|
|
|
if (maybe_elms_obj == NULL) {
|
|
|
|
return CallJsBuiltin(isolate, "ArrayPush", args);
|
2010-10-25 15:22:03 +00:00
|
|
|
}
|
2012-11-15 12:19:14 +00:00
|
|
|
if (!maybe_elms_obj->To(&elms_obj)) return maybe_elms_obj;
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2012-11-15 12:19:14 +00:00
|
|
|
JSArray* array = JSArray::cast(receiver);
|
2013-02-06 17:35:35 +00:00
|
|
|
ASSERT(!array->map()->is_observed());
|
|
|
|
|
2012-11-15 12:19:14 +00:00
|
|
|
ElementsKind kind = array->GetElementsKind();
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2012-11-15 12:19:14 +00:00
|
|
|
if (IsFastSmiOrObjectElementsKind(kind)) {
|
|
|
|
FixedArray* elms = FixedArray::cast(elms_obj);
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2012-11-15 12:19:14 +00:00
|
|
|
int len = Smi::cast(array->length())->value();
|
|
|
|
int to_add = args.length() - 1;
|
|
|
|
if (to_add == 0) {
|
|
|
|
return Smi::FromInt(len);
|
2010-10-25 15:22:03 +00:00
|
|
|
}
|
2012-11-15 12:19:14 +00:00
|
|
|
// Currently fixed arrays cannot grow too big, so
|
|
|
|
// we should never hit this case.
|
|
|
|
ASSERT(to_add <= (Smi::kMaxValue - len));
|
2010-01-29 11:46:55 +00:00
|
|
|
|
2012-11-15 12:19:14 +00:00
|
|
|
int new_length = len + to_add;
|
2010-03-01 15:33:30 +00:00
|
|
|
|
2012-11-15 12:19:14 +00:00
|
|
|
if (new_length > elms->length()) {
|
|
|
|
// New backing storage is needed.
|
|
|
|
int capacity = new_length + (new_length >> 1) + 16;
|
|
|
|
FixedArray* new_elms;
|
|
|
|
MaybeObject* maybe_obj = heap->AllocateUninitializedFixedArray(capacity);
|
|
|
|
if (!maybe_obj->To(&new_elms)) return maybe_obj;
|
|
|
|
|
2012-11-27 12:01:14 +00:00
|
|
|
ElementsAccessor* accessor = array->GetElementsAccessor();
|
|
|
|
MaybeObject* maybe_failure = accessor->CopyElements(
|
2013-01-02 10:09:42 +00:00
|
|
|
NULL, 0, kind, new_elms, 0,
|
2012-11-27 12:01:14 +00:00
|
|
|
ElementsAccessor::kCopyToEndAndInitializeToHole, elms_obj);
|
|
|
|
ASSERT(!maybe_failure->IsFailure());
|
|
|
|
USE(maybe_failure);
|
2010-02-15 13:25:06 +00:00
|
|
|
|
2012-11-15 12:19:14 +00:00
|
|
|
elms = new_elms;
|
|
|
|
}
|
2010-02-15 13:25:06 +00:00
|
|
|
|
2012-11-15 12:19:14 +00:00
|
|
|
// Add the provided values.
|
2013-06-03 15:32:22 +00:00
|
|
|
DisallowHeapAllocation no_gc;
|
2012-11-15 12:19:14 +00:00
|
|
|
WriteBarrierMode mode = elms->GetWriteBarrierMode(no_gc);
|
|
|
|
for (int index = 0; index < to_add; index++) {
|
|
|
|
elms->set(index + len, args[index + 1], mode);
|
|
|
|
}
|
2011-09-22 11:30:04 +00:00
|
|
|
|
2012-11-15 12:19:14 +00:00
|
|
|
if (elms != array->elements()) {
|
|
|
|
array->set_elements(elms);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the length.
|
|
|
|
array->set_length(Smi::FromInt(new_length));
|
|
|
|
return Smi::FromInt(new_length);
|
|
|
|
} else {
|
|
|
|
int len = Smi::cast(array->length())->value();
|
|
|
|
int elms_len = elms_obj->length();
|
|
|
|
|
|
|
|
int to_add = args.length() - 1;
|
|
|
|
if (to_add == 0) {
|
|
|
|
return Smi::FromInt(len);
|
|
|
|
}
|
|
|
|
// Currently fixed arrays cannot grow too big, so
|
|
|
|
// we should never hit this case.
|
|
|
|
ASSERT(to_add <= (Smi::kMaxValue - len));
|
|
|
|
|
|
|
|
int new_length = len + to_add;
|
|
|
|
|
|
|
|
FixedDoubleArray* new_elms;
|
|
|
|
|
|
|
|
if (new_length > elms_len) {
|
|
|
|
// New backing storage is needed.
|
|
|
|
int capacity = new_length + (new_length >> 1) + 16;
|
|
|
|
MaybeObject* maybe_obj =
|
|
|
|
heap->AllocateUninitializedFixedDoubleArray(capacity);
|
|
|
|
if (!maybe_obj->To(&new_elms)) return maybe_obj;
|
|
|
|
|
2012-11-27 12:01:14 +00:00
|
|
|
ElementsAccessor* accessor = array->GetElementsAccessor();
|
|
|
|
MaybeObject* maybe_failure = accessor->CopyElements(
|
2013-01-02 10:09:42 +00:00
|
|
|
NULL, 0, kind, new_elms, 0,
|
2012-11-27 12:01:14 +00:00
|
|
|
ElementsAccessor::kCopyToEndAndInitializeToHole, elms_obj);
|
|
|
|
ASSERT(!maybe_failure->IsFailure());
|
|
|
|
USE(maybe_failure);
|
2012-11-15 12:19:14 +00:00
|
|
|
} else {
|
|
|
|
// to_add is > 0 and new_length <= elms_len, so elms_obj cannot be the
|
|
|
|
// empty_fixed_array.
|
|
|
|
new_elms = FixedDoubleArray::cast(elms_obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the provided values.
|
2013-06-03 15:32:22 +00:00
|
|
|
DisallowHeapAllocation no_gc;
|
2012-11-15 12:19:14 +00:00
|
|
|
int index;
|
|
|
|
for (index = 0; index < to_add; index++) {
|
|
|
|
Object* arg = args[index + 1];
|
|
|
|
new_elms->set(index + len, arg->Number());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (new_elms != array->elements()) {
|
|
|
|
array->set_elements(new_elms);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the length.
|
|
|
|
array->set_length(Smi::FromInt(new_length));
|
|
|
|
return Smi::FromInt(new_length);
|
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-16 10:12:32 +00:00
|
|
|
BUILTIN(ArrayPop) {
|
2011-03-18 20:35:07 +00:00
|
|
|
Heap* heap = isolate->heap();
|
2010-03-11 13:13:21 +00:00
|
|
|
Object* receiver = *args.receiver();
|
2012-11-15 12:19:14 +00:00
|
|
|
FixedArrayBase* elms_obj;
|
|
|
|
MaybeObject* maybe_elms =
|
|
|
|
EnsureJSArrayWithWritableFastElements(heap, receiver, NULL, 0);
|
|
|
|
if (maybe_elms == NULL) return CallJsBuiltin(isolate, "ArrayPop", args);
|
|
|
|
if (!maybe_elms->To(&elms_obj)) return maybe_elms;
|
|
|
|
|
2010-03-11 13:13:21 +00:00
|
|
|
JSArray* array = JSArray::cast(receiver);
|
2013-02-06 17:35:35 +00:00
|
|
|
ASSERT(!array->map()->is_observed());
|
2012-11-09 12:28:22 +00:00
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
int len = Smi::cast(array->length())->value();
|
2011-03-18 20:35:07 +00:00
|
|
|
if (len == 0) return heap->undefined_value();
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2012-11-15 12:19:14 +00:00
|
|
|
ElementsAccessor* accessor = array->GetElementsAccessor();
|
|
|
|
int new_length = len - 1;
|
2012-11-15 14:30:01 +00:00
|
|
|
MaybeObject* maybe_result;
|
|
|
|
if (accessor->HasElement(array, array, new_length, elms_obj)) {
|
|
|
|
maybe_result = accessor->Get(array, array, new_length, elms_obj);
|
|
|
|
} else {
|
|
|
|
maybe_result = array->GetPrototype()->GetElement(len - 1);
|
|
|
|
}
|
|
|
|
if (maybe_result->IsFailure()) return maybe_result;
|
2012-11-15 12:19:14 +00:00
|
|
|
MaybeObject* maybe_failure =
|
|
|
|
accessor->SetLength(array, Smi::FromInt(new_length));
|
|
|
|
if (maybe_failure->IsFailure()) return maybe_failure;
|
2012-11-15 14:30:01 +00:00
|
|
|
return maybe_result;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-01 15:33:30 +00:00
|
|
|
BUILTIN(ArrayShift) {
|
2011-03-18 20:35:07 +00:00
|
|
|
Heap* heap = isolate->heap();
|
2010-03-11 13:13:21 +00:00
|
|
|
Object* receiver = *args.receiver();
|
2012-11-15 12:19:14 +00:00
|
|
|
FixedArrayBase* elms_obj;
|
|
|
|
MaybeObject* maybe_elms_obj =
|
|
|
|
EnsureJSArrayWithWritableFastElements(heap, receiver, NULL, 0);
|
|
|
|
if (maybe_elms_obj == NULL)
|
|
|
|
return CallJsBuiltin(isolate, "ArrayShift", args);
|
|
|
|
if (!maybe_elms_obj->To(&elms_obj)) return maybe_elms_obj;
|
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
if (!IsJSArrayFastElementMovingAllowed(heap, JSArray::cast(receiver))) {
|
|
|
|
return CallJsBuiltin(isolate, "ArrayShift", args);
|
2010-02-15 13:25:06 +00:00
|
|
|
}
|
2010-03-11 13:13:21 +00:00
|
|
|
JSArray* array = JSArray::cast(receiver);
|
2013-02-06 17:35:35 +00:00
|
|
|
ASSERT(!array->map()->is_observed());
|
2012-11-09 12:28:22 +00:00
|
|
|
|
2010-02-15 09:17:38 +00:00
|
|
|
int len = Smi::cast(array->length())->value();
|
2011-03-18 20:35:07 +00:00
|
|
|
if (len == 0) return heap->undefined_value();
|
2010-02-15 09:17:38 +00:00
|
|
|
|
2010-02-15 13:25:06 +00:00
|
|
|
// Get first element
|
2012-11-15 12:19:14 +00:00
|
|
|
ElementsAccessor* accessor = array->GetElementsAccessor();
|
|
|
|
Object* first;
|
|
|
|
MaybeObject* maybe_first = accessor->Get(receiver, array, 0, elms_obj);
|
|
|
|
if (!maybe_first->To(&first)) return maybe_first;
|
2012-11-15 15:27:06 +00:00
|
|
|
if (first->IsTheHole()) {
|
|
|
|
first = heap->undefined_value();
|
|
|
|
}
|
2010-02-15 09:17:38 +00:00
|
|
|
|
2012-11-15 12:19:14 +00:00
|
|
|
if (!heap->lo_space()->Contains(elms_obj)) {
|
|
|
|
array->set_elements(LeftTrimFixedArray(heap, elms_obj, 1));
|
2010-03-23 14:05:16 +00:00
|
|
|
} else {
|
|
|
|
// Shift the elements.
|
2012-11-15 12:19:14 +00:00
|
|
|
if (elms_obj->IsFixedArray()) {
|
|
|
|
FixedArray* elms = FixedArray::cast(elms_obj);
|
2013-06-03 15:32:22 +00:00
|
|
|
DisallowHeapAllocation no_gc;
|
2013-01-09 14:01:39 +00:00
|
|
|
heap->MoveElements(elms, 0, 1, len - 1);
|
2012-11-15 12:19:14 +00:00
|
|
|
elms->set(len - 1, heap->the_hole_value());
|
|
|
|
} else {
|
|
|
|
FixedDoubleArray* elms = FixedDoubleArray::cast(elms_obj);
|
|
|
|
MoveDoubleElements(elms, 0, elms, 1, len - 1);
|
|
|
|
elms->set_the_hole(len - 1);
|
|
|
|
}
|
2010-03-23 14:05:16 +00:00
|
|
|
}
|
2010-02-15 09:17:38 +00:00
|
|
|
|
|
|
|
// Set the length.
|
|
|
|
array->set_length(Smi::FromInt(len - 1));
|
|
|
|
|
|
|
|
return first;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-02-15 13:25:06 +00:00
|
|
|
BUILTIN(ArrayUnshift) {
|
2011-03-18 20:35:07 +00:00
|
|
|
Heap* heap = isolate->heap();
|
2010-03-11 13:13:21 +00:00
|
|
|
Object* receiver = *args.receiver();
|
2012-11-15 12:19:14 +00:00
|
|
|
FixedArrayBase* elms_obj;
|
|
|
|
MaybeObject* maybe_elms_obj =
|
|
|
|
EnsureJSArrayWithWritableFastElements(heap, receiver, NULL, 0);
|
|
|
|
if (maybe_elms_obj == NULL)
|
|
|
|
return CallJsBuiltin(isolate, "ArrayUnshift", args);
|
|
|
|
if (!maybe_elms_obj->To(&elms_obj)) return maybe_elms_obj;
|
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
if (!IsJSArrayFastElementMovingAllowed(heap, JSArray::cast(receiver))) {
|
|
|
|
return CallJsBuiltin(isolate, "ArrayUnshift", args);
|
2010-03-01 15:33:30 +00:00
|
|
|
}
|
2010-03-11 13:13:21 +00:00
|
|
|
JSArray* array = JSArray::cast(receiver);
|
2013-02-06 17:35:35 +00:00
|
|
|
ASSERT(!array->map()->is_observed());
|
2012-11-15 12:19:14 +00:00
|
|
|
if (!array->HasFastSmiOrObjectElements()) {
|
|
|
|
return CallJsBuiltin(isolate, "ArrayUnshift", args);
|
|
|
|
}
|
|
|
|
FixedArray* elms = FixedArray::cast(elms_obj);
|
2010-02-15 13:25:06 +00:00
|
|
|
|
|
|
|
int len = Smi::cast(array->length())->value();
|
|
|
|
int to_add = args.length() - 1;
|
|
|
|
int new_length = len + to_add;
|
2010-02-17 13:04:30 +00:00
|
|
|
// Currently fixed arrays cannot grow too big, so
|
|
|
|
// we should never hit this case.
|
|
|
|
ASSERT(to_add <= (Smi::kMaxValue - len));
|
|
|
|
|
2011-10-10 08:31:06 +00:00
|
|
|
MaybeObject* maybe_object =
|
2011-12-09 08:50:19 +00:00
|
|
|
array->EnsureCanContainElements(&args, 1, to_add,
|
|
|
|
DONT_ALLOW_DOUBLE_ELEMENTS);
|
2011-10-10 08:31:06 +00:00
|
|
|
if (maybe_object->IsFailure()) return maybe_object;
|
2011-09-22 11:30:04 +00:00
|
|
|
|
2010-02-15 13:25:06 +00:00
|
|
|
if (new_length > elms->length()) {
|
|
|
|
// New backing storage is needed.
|
|
|
|
int capacity = new_length + (new_length >> 1) + 16;
|
2012-11-15 12:19:14 +00:00
|
|
|
FixedArray* new_elms;
|
|
|
|
MaybeObject* maybe_elms = heap->AllocateUninitializedFixedArray(capacity);
|
|
|
|
if (!maybe_elms->To(&new_elms)) return maybe_elms;
|
|
|
|
|
2012-11-27 12:01:14 +00:00
|
|
|
ElementsKind kind = array->GetElementsKind();
|
|
|
|
ElementsAccessor* accessor = array->GetElementsAccessor();
|
|
|
|
MaybeObject* maybe_failure = accessor->CopyElements(
|
2013-01-02 10:09:42 +00:00
|
|
|
NULL, 0, kind, new_elms, to_add,
|
2012-11-27 12:01:14 +00:00
|
|
|
ElementsAccessor::kCopyToEndAndInitializeToHole, elms);
|
|
|
|
ASSERT(!maybe_failure->IsFailure());
|
|
|
|
USE(maybe_failure);
|
2012-11-15 12:19:14 +00:00
|
|
|
|
2010-02-15 13:25:06 +00:00
|
|
|
elms = new_elms;
|
|
|
|
array->set_elements(elms);
|
|
|
|
} else {
|
2013-06-03 15:32:22 +00:00
|
|
|
DisallowHeapAllocation no_gc;
|
2013-01-09 14:01:39 +00:00
|
|
|
heap->MoveElements(elms, to_add, 0, len);
|
2010-02-15 13:25:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add the provided values.
|
2013-06-03 15:32:22 +00:00
|
|
|
DisallowHeapAllocation no_gc;
|
2010-02-15 13:25:06 +00:00
|
|
|
WriteBarrierMode mode = elms->GetWriteBarrierMode(no_gc);
|
|
|
|
for (int i = 0; i < to_add; i++) {
|
|
|
|
elms->set(i, args[i + 1], mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the length.
|
|
|
|
array->set_length(Smi::FromInt(new_length));
|
|
|
|
return Smi::FromInt(new_length);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-01 15:33:30 +00:00
|
|
|
BUILTIN(ArraySlice) {
|
2011-03-18 20:35:07 +00:00
|
|
|
Heap* heap = isolate->heap();
|
2010-03-11 13:13:21 +00:00
|
|
|
Object* receiver = *args.receiver();
|
2012-11-15 12:19:14 +00:00
|
|
|
FixedArrayBase* elms;
|
2010-12-20 14:41:41 +00:00
|
|
|
int len = -1;
|
2010-12-22 15:45:48 +00:00
|
|
|
if (receiver->IsJSArray()) {
|
|
|
|
JSArray* array = JSArray::cast(receiver);
|
2012-11-15 12:19:14 +00:00
|
|
|
if (!IsJSArrayFastElementMovingAllowed(heap, array)) {
|
|
|
|
return CallJsBuiltin(isolate, "ArraySlice", args);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (array->HasFastElements()) {
|
|
|
|
elms = array->elements();
|
|
|
|
} else {
|
2011-03-18 20:35:07 +00:00
|
|
|
return CallJsBuiltin(isolate, "ArraySlice", args);
|
2010-12-22 15:45:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
len = Smi::cast(array->length())->value();
|
|
|
|
} else {
|
|
|
|
// Array.slice(arguments, ...) is quite a common idiom (notably more
|
|
|
|
// than 50% of invocations in Web apps). Treat it in C++ as well.
|
|
|
|
Map* arguments_map =
|
2012-08-17 09:03:08 +00:00
|
|
|
isolate->context()->native_context()->arguments_boilerplate()->map();
|
2010-12-22 15:45:48 +00:00
|
|
|
|
|
|
|
bool is_arguments_object_with_fast_elements =
|
2012-11-15 12:19:14 +00:00
|
|
|
receiver->IsJSObject() &&
|
|
|
|
JSObject::cast(receiver)->map() == arguments_map;
|
2010-12-22 15:45:48 +00:00
|
|
|
if (!is_arguments_object_with_fast_elements) {
|
2011-03-18 20:35:07 +00:00
|
|
|
return CallJsBuiltin(isolate, "ArraySlice", args);
|
2010-12-22 15:45:48 +00:00
|
|
|
}
|
2012-11-15 12:19:14 +00:00
|
|
|
JSObject* object = JSObject::cast(receiver);
|
|
|
|
|
|
|
|
if (object->HasFastElements()) {
|
|
|
|
elms = object->elements();
|
|
|
|
} else {
|
|
|
|
return CallJsBuiltin(isolate, "ArraySlice", args);
|
|
|
|
}
|
|
|
|
Object* len_obj = object->InObjectPropertyAt(Heap::kArgumentsLengthIndex);
|
2011-01-11 11:23:40 +00:00
|
|
|
if (!len_obj->IsSmi()) {
|
2011-03-18 20:35:07 +00:00
|
|
|
return CallJsBuiltin(isolate, "ArraySlice", args);
|
2011-01-11 11:23:40 +00:00
|
|
|
}
|
|
|
|
len = Smi::cast(len_obj)->value();
|
|
|
|
if (len > elms->length()) {
|
2011-03-18 20:35:07 +00:00
|
|
|
return CallJsBuiltin(isolate, "ArraySlice", args);
|
2011-01-11 11:23:40 +00:00
|
|
|
}
|
2012-11-15 12:19:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
JSObject* object = JSObject::cast(receiver);
|
|
|
|
|
2010-12-20 14:41:41 +00:00
|
|
|
ASSERT(len >= 0);
|
2010-02-16 12:14:23 +00:00
|
|
|
int n_arguments = args.length() - 1;
|
|
|
|
|
|
|
|
// Note carefully choosen defaults---if argument is missing,
|
2010-03-05 11:19:37 +00:00
|
|
|
// it's undefined which gets converted to 0 for relative_start
|
|
|
|
// and to len for relative_end.
|
|
|
|
int relative_start = 0;
|
|
|
|
int relative_end = len;
|
2010-02-16 12:14:23 +00:00
|
|
|
if (n_arguments > 0) {
|
|
|
|
Object* arg1 = args[1];
|
|
|
|
if (arg1->IsSmi()) {
|
2010-03-05 11:19:37 +00:00
|
|
|
relative_start = Smi::cast(arg1)->value();
|
2012-11-15 12:19:14 +00:00
|
|
|
} else if (arg1->IsHeapNumber()) {
|
|
|
|
double start = HeapNumber::cast(arg1)->value();
|
|
|
|
if (start < kMinInt || start > kMaxInt) {
|
|
|
|
return CallJsBuiltin(isolate, "ArraySlice", args);
|
|
|
|
}
|
2013-05-10 13:04:42 +00:00
|
|
|
relative_start = std::isnan(start) ? 0 : static_cast<int>(start);
|
2010-02-16 12:14:23 +00:00
|
|
|
} else if (!arg1->IsUndefined()) {
|
2011-03-18 20:35:07 +00:00
|
|
|
return CallJsBuiltin(isolate, "ArraySlice", args);
|
2010-02-16 12:14:23 +00:00
|
|
|
}
|
|
|
|
if (n_arguments > 1) {
|
|
|
|
Object* arg2 = args[2];
|
|
|
|
if (arg2->IsSmi()) {
|
2010-03-05 11:19:37 +00:00
|
|
|
relative_end = Smi::cast(arg2)->value();
|
2012-11-15 12:19:14 +00:00
|
|
|
} else if (arg2->IsHeapNumber()) {
|
|
|
|
double end = HeapNumber::cast(arg2)->value();
|
|
|
|
if (end < kMinInt || end > kMaxInt) {
|
|
|
|
return CallJsBuiltin(isolate, "ArraySlice", args);
|
|
|
|
}
|
2013-05-10 13:04:42 +00:00
|
|
|
relative_end = std::isnan(end) ? 0 : static_cast<int>(end);
|
2010-02-16 12:14:23 +00:00
|
|
|
} else if (!arg2->IsUndefined()) {
|
2011-03-18 20:35:07 +00:00
|
|
|
return CallJsBuiltin(isolate, "ArraySlice", args);
|
2010-02-16 12:14:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ECMAScript 232, 3rd Edition, Section 15.4.4.10, step 6.
|
2010-03-05 11:19:37 +00:00
|
|
|
int k = (relative_start < 0) ? Max(len + relative_start, 0)
|
|
|
|
: Min(relative_start, len);
|
2010-02-16 12:14:23 +00:00
|
|
|
|
|
|
|
// ECMAScript 232, 3rd Edition, Section 15.4.4.10, step 8.
|
2010-03-05 11:19:37 +00:00
|
|
|
int final = (relative_end < 0) ? Max(len + relative_end, 0)
|
|
|
|
: Min(relative_end, len);
|
2010-02-16 12:14:23 +00:00
|
|
|
|
2012-01-26 21:47:57 +00:00
|
|
|
// Calculate the length of result array.
|
|
|
|
int result_len = Max(final - k, 0);
|
2010-02-16 12:14:23 +00:00
|
|
|
|
2012-12-12 15:20:45 +00:00
|
|
|
ElementsKind kind = object->GetElementsKind();
|
|
|
|
if (IsHoleyElementsKind(kind)) {
|
|
|
|
bool packed = true;
|
|
|
|
ElementsAccessor* accessor = ElementsAccessor::ForKind(kind);
|
|
|
|
for (int i = k; i < final; i++) {
|
|
|
|
if (!accessor->HasElement(object, object, i, elms)) {
|
|
|
|
packed = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (packed) {
|
|
|
|
kind = GetPackedElementsKind(kind);
|
|
|
|
} else if (!receiver->IsJSArray()) {
|
|
|
|
return CallJsBuiltin(isolate, "ArraySlice", args);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-26 21:47:57 +00:00
|
|
|
JSArray* result_array;
|
2012-11-15 12:19:14 +00:00
|
|
|
MaybeObject* maybe_array = heap->AllocateJSArrayAndStorage(kind,
|
|
|
|
result_len,
|
|
|
|
result_len);
|
2012-11-19 15:31:38 +00:00
|
|
|
|
2013-06-03 15:32:22 +00:00
|
|
|
DisallowHeapAllocation no_gc;
|
2012-11-19 15:31:38 +00:00
|
|
|
if (result_len == 0) return maybe_array;
|
2012-01-26 21:47:57 +00:00
|
|
|
if (!maybe_array->To(&result_array)) return maybe_array;
|
2011-09-22 11:30:04 +00:00
|
|
|
|
2012-11-15 12:19:14 +00:00
|
|
|
ElementsAccessor* accessor = object->GetElementsAccessor();
|
2013-01-02 10:09:42 +00:00
|
|
|
MaybeObject* maybe_failure = accessor->CopyElements(
|
|
|
|
NULL, k, kind, result_array->elements(), 0, result_len, elms);
|
2012-11-15 12:19:14 +00:00
|
|
|
ASSERT(!maybe_failure->IsFailure());
|
|
|
|
USE(maybe_failure);
|
2010-02-16 12:14:23 +00:00
|
|
|
|
|
|
|
return result_array;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-02-17 10:54:49 +00:00
|
|
|
BUILTIN(ArraySplice) {
|
2011-03-18 20:35:07 +00:00
|
|
|
Heap* heap = isolate->heap();
|
2010-03-11 13:13:21 +00:00
|
|
|
Object* receiver = *args.receiver();
|
2012-11-15 12:19:14 +00:00
|
|
|
FixedArrayBase* elms_obj;
|
|
|
|
MaybeObject* maybe_elms =
|
|
|
|
EnsureJSArrayWithWritableFastElements(heap, receiver, &args, 3);
|
|
|
|
if (maybe_elms == NULL) {
|
|
|
|
return CallJsBuiltin(isolate, "ArraySplice", args);
|
2010-10-25 15:22:03 +00:00
|
|
|
}
|
2012-11-15 12:19:14 +00:00
|
|
|
if (!maybe_elms->To(&elms_obj)) return maybe_elms;
|
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
if (!IsJSArrayFastElementMovingAllowed(heap, JSArray::cast(receiver))) {
|
|
|
|
return CallJsBuiltin(isolate, "ArraySplice", args);
|
2010-03-01 15:33:30 +00:00
|
|
|
}
|
2010-03-11 13:13:21 +00:00
|
|
|
JSArray* array = JSArray::cast(receiver);
|
2013-02-06 17:35:35 +00:00
|
|
|
ASSERT(!array->map()->is_observed());
|
2012-11-09 12:28:22 +00:00
|
|
|
|
2010-02-17 10:54:49 +00:00
|
|
|
int len = Smi::cast(array->length())->value();
|
|
|
|
|
|
|
|
int n_arguments = args.length() - 1;
|
|
|
|
|
2010-03-05 11:19:37 +00:00
|
|
|
int relative_start = 0;
|
2011-02-02 14:17:47 +00:00
|
|
|
if (n_arguments > 0) {
|
|
|
|
Object* arg1 = args[1];
|
|
|
|
if (arg1->IsSmi()) {
|
|
|
|
relative_start = Smi::cast(arg1)->value();
|
2012-11-15 12:19:14 +00:00
|
|
|
} else if (arg1->IsHeapNumber()) {
|
|
|
|
double start = HeapNumber::cast(arg1)->value();
|
|
|
|
if (start < kMinInt || start > kMaxInt) {
|
|
|
|
return CallJsBuiltin(isolate, "ArraySplice", args);
|
|
|
|
}
|
2013-04-24 19:10:02 +00:00
|
|
|
relative_start = std::isnan(start) ? 0 : static_cast<int>(start);
|
2011-02-02 14:17:47 +00:00
|
|
|
} else if (!arg1->IsUndefined()) {
|
2011-03-18 20:35:07 +00:00
|
|
|
return CallJsBuiltin(isolate, "ArraySplice", args);
|
2011-02-02 14:17:47 +00:00
|
|
|
}
|
2010-02-17 10:54:49 +00:00
|
|
|
}
|
2010-03-05 11:19:37 +00:00
|
|
|
int actual_start = (relative_start < 0) ? Max(len + relative_start, 0)
|
|
|
|
: Min(relative_start, len);
|
2010-02-17 10:54:49 +00:00
|
|
|
|
|
|
|
// SpiderMonkey, TraceMonkey and JSC treat the case where no delete count is
|
2011-01-31 14:54:53 +00:00
|
|
|
// given as a request to delete all the elements from the start.
|
|
|
|
// And it differs from the case of undefined delete count.
|
2010-02-17 10:54:49 +00:00
|
|
|
// This does not follow ECMA-262, but we do the same for
|
|
|
|
// compatibility.
|
2011-01-31 14:54:53 +00:00
|
|
|
int actual_delete_count;
|
|
|
|
if (n_arguments == 1) {
|
|
|
|
ASSERT(len - actual_start >= 0);
|
|
|
|
actual_delete_count = len - actual_start;
|
|
|
|
} else {
|
|
|
|
int value = 0; // ToInteger(undefined) == 0
|
|
|
|
if (n_arguments > 1) {
|
|
|
|
Object* arg2 = args[2];
|
|
|
|
if (arg2->IsSmi()) {
|
|
|
|
value = Smi::cast(arg2)->value();
|
|
|
|
} else {
|
2011-03-18 20:35:07 +00:00
|
|
|
return CallJsBuiltin(isolate, "ArraySplice", args);
|
2011-01-31 14:54:53 +00:00
|
|
|
}
|
2010-02-17 10:54:49 +00:00
|
|
|
}
|
2011-01-31 14:54:53 +00:00
|
|
|
actual_delete_count = Min(Max(value, 0), len - actual_start);
|
2010-02-17 10:54:49 +00:00
|
|
|
}
|
|
|
|
|
2012-11-15 12:19:14 +00:00
|
|
|
ElementsKind elements_kind = array->GetElementsKind();
|
|
|
|
|
|
|
|
int item_count = (n_arguments > 1) ? (n_arguments - 2) : 0;
|
|
|
|
int new_length = len - actual_delete_count + item_count;
|
|
|
|
|
|
|
|
// For double mode we do not support changing the length.
|
|
|
|
if (new_length > len && IsFastDoubleElementsKind(elements_kind)) {
|
|
|
|
return CallJsBuiltin(isolate, "ArraySplice", args);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (new_length == 0) {
|
|
|
|
MaybeObject* maybe_array = heap->AllocateJSArrayWithElements(
|
|
|
|
elms_obj, elements_kind, actual_delete_count);
|
|
|
|
if (maybe_array->IsFailure()) return maybe_array;
|
|
|
|
array->set_elements(heap->empty_fixed_array());
|
|
|
|
array->set_length(Smi::FromInt(0));
|
|
|
|
return maybe_array;
|
|
|
|
}
|
|
|
|
|
2010-03-04 22:16:58 +00:00
|
|
|
JSArray* result_array = NULL;
|
2012-01-26 21:47:57 +00:00
|
|
|
MaybeObject* maybe_array =
|
|
|
|
heap->AllocateJSArrayAndStorage(elements_kind,
|
|
|
|
actual_delete_count,
|
|
|
|
actual_delete_count);
|
|
|
|
if (!maybe_array->To(&result_array)) return maybe_array;
|
|
|
|
|
2012-11-15 12:19:14 +00:00
|
|
|
if (actual_delete_count > 0) {
|
2013-06-03 15:32:22 +00:00
|
|
|
DisallowHeapAllocation no_gc;
|
2012-11-15 12:19:14 +00:00
|
|
|
ElementsAccessor* accessor = array->GetElementsAccessor();
|
2013-01-02 10:09:42 +00:00
|
|
|
MaybeObject* maybe_failure = accessor->CopyElements(
|
|
|
|
NULL, actual_start, elements_kind, result_array->elements(),
|
|
|
|
0, actual_delete_count, elms_obj);
|
2012-11-15 12:19:14 +00:00
|
|
|
// Cannot fail since the origin and target array are of the same elements
|
|
|
|
// kind.
|
|
|
|
ASSERT(!maybe_failure->IsFailure());
|
|
|
|
USE(maybe_failure);
|
2010-03-04 22:16:58 +00:00
|
|
|
}
|
2010-02-17 10:54:49 +00:00
|
|
|
|
2011-09-22 11:30:04 +00:00
|
|
|
bool elms_changed = false;
|
2010-03-05 11:19:37 +00:00
|
|
|
if (item_count < actual_delete_count) {
|
2010-02-17 10:54:49 +00:00
|
|
|
// Shrink the array.
|
2012-11-15 12:19:14 +00:00
|
|
|
const bool trim_array = !heap->lo_space()->Contains(elms_obj) &&
|
2010-03-30 12:48:55 +00:00
|
|
|
((actual_start + item_count) <
|
|
|
|
(len - actual_delete_count - actual_start));
|
|
|
|
if (trim_array) {
|
|
|
|
const int delta = actual_delete_count - item_count;
|
|
|
|
|
2012-11-15 12:19:14 +00:00
|
|
|
if (elms_obj->IsFixedDoubleArray()) {
|
|
|
|
FixedDoubleArray* elms = FixedDoubleArray::cast(elms_obj);
|
|
|
|
MoveDoubleElements(elms, delta, elms, 0, actual_start);
|
|
|
|
} else {
|
|
|
|
FixedArray* elms = FixedArray::cast(elms_obj);
|
2013-06-03 15:32:22 +00:00
|
|
|
DisallowHeapAllocation no_gc;
|
2013-01-09 14:01:39 +00:00
|
|
|
heap->MoveElements(elms, delta, 0, actual_start);
|
2010-03-30 12:48:55 +00:00
|
|
|
}
|
|
|
|
|
2012-11-15 12:19:14 +00:00
|
|
|
elms_obj = LeftTrimFixedArray(heap, elms_obj, delta);
|
2011-09-22 11:30:04 +00:00
|
|
|
|
|
|
|
elms_changed = true;
|
2010-03-30 12:48:55 +00:00
|
|
|
} else {
|
2012-11-15 12:19:14 +00:00
|
|
|
if (elms_obj->IsFixedDoubleArray()) {
|
|
|
|
FixedDoubleArray* elms = FixedDoubleArray::cast(elms_obj);
|
|
|
|
MoveDoubleElements(elms, actual_start + item_count,
|
|
|
|
elms, actual_start + actual_delete_count,
|
|
|
|
(len - actual_delete_count - actual_start));
|
|
|
|
FillWithHoles(elms, new_length, len);
|
|
|
|
} else {
|
|
|
|
FixedArray* elms = FixedArray::cast(elms_obj);
|
2013-06-03 15:32:22 +00:00
|
|
|
DisallowHeapAllocation no_gc;
|
2013-01-09 14:01:39 +00:00
|
|
|
heap->MoveElements(elms, actual_start + item_count,
|
|
|
|
actual_start + actual_delete_count,
|
|
|
|
(len - actual_delete_count - actual_start));
|
2012-11-15 12:19:14 +00:00
|
|
|
FillWithHoles(heap, elms, new_length, len);
|
|
|
|
}
|
2010-03-30 12:48:55 +00:00
|
|
|
}
|
2010-03-05 11:19:37 +00:00
|
|
|
} else if (item_count > actual_delete_count) {
|
2012-11-15 12:19:14 +00:00
|
|
|
FixedArray* elms = FixedArray::cast(elms_obj);
|
2010-02-17 13:04:30 +00:00
|
|
|
// Currently fixed arrays cannot grow too big, so
|
|
|
|
// we should never hit this case.
|
2010-03-05 11:19:37 +00:00
|
|
|
ASSERT((item_count - actual_delete_count) <= (Smi::kMaxValue - len));
|
2010-02-17 13:04:30 +00:00
|
|
|
|
2010-02-17 10:54:49 +00:00
|
|
|
// Check if array need to grow.
|
|
|
|
if (new_length > elms->length()) {
|
|
|
|
// New backing storage is needed.
|
|
|
|
int capacity = new_length + (new_length >> 1) + 16;
|
2012-11-15 12:19:14 +00:00
|
|
|
FixedArray* new_elms;
|
|
|
|
MaybeObject* maybe_obj = heap->AllocateUninitializedFixedArray(capacity);
|
|
|
|
if (!maybe_obj->To(&new_elms)) return maybe_obj;
|
|
|
|
|
2013-06-03 15:32:22 +00:00
|
|
|
DisallowHeapAllocation no_gc;
|
2012-11-22 16:22:57 +00:00
|
|
|
|
2012-11-15 12:19:14 +00:00
|
|
|
ElementsKind kind = array->GetElementsKind();
|
|
|
|
ElementsAccessor* accessor = array->GetElementsAccessor();
|
2012-11-19 15:31:38 +00:00
|
|
|
if (actual_start > 0) {
|
|
|
|
// Copy the part before actual_start as is.
|
|
|
|
MaybeObject* maybe_failure = accessor->CopyElements(
|
2013-01-02 10:09:42 +00:00
|
|
|
NULL, 0, kind, new_elms, 0, actual_start, elms);
|
2012-11-19 15:31:38 +00:00
|
|
|
ASSERT(!maybe_failure->IsFailure());
|
|
|
|
USE(maybe_failure);
|
|
|
|
}
|
2012-11-27 12:01:14 +00:00
|
|
|
MaybeObject* maybe_failure = accessor->CopyElements(
|
2013-01-02 10:09:42 +00:00
|
|
|
NULL, actual_start + actual_delete_count, kind, new_elms,
|
2012-11-27 12:01:14 +00:00
|
|
|
actual_start + item_count,
|
|
|
|
ElementsAccessor::kCopyToEndAndInitializeToHole, elms);
|
|
|
|
ASSERT(!maybe_failure->IsFailure());
|
|
|
|
USE(maybe_failure);
|
2010-02-17 10:54:49 +00:00
|
|
|
|
2012-11-15 12:19:14 +00:00
|
|
|
elms_obj = new_elms;
|
2011-09-22 11:30:04 +00:00
|
|
|
elms_changed = true;
|
2010-03-05 15:07:53 +00:00
|
|
|
} else {
|
2013-06-03 15:32:22 +00:00
|
|
|
DisallowHeapAllocation no_gc;
|
2013-01-09 14:01:39 +00:00
|
|
|
heap->MoveElements(elms, actual_start + item_count,
|
|
|
|
actual_start + actual_delete_count,
|
|
|
|
(len - actual_delete_count - actual_start));
|
2010-02-17 10:54:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-15 12:19:14 +00:00
|
|
|
if (IsFastDoubleElementsKind(elements_kind)) {
|
|
|
|
FixedDoubleArray* elms = FixedDoubleArray::cast(elms_obj);
|
|
|
|
for (int k = actual_start; k < actual_start + item_count; k++) {
|
|
|
|
Object* arg = args[3 + k - actual_start];
|
|
|
|
if (arg->IsSmi()) {
|
|
|
|
elms->set(k, Smi::cast(arg)->value());
|
|
|
|
} else {
|
|
|
|
elms->set(k, HeapNumber::cast(arg)->value());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
FixedArray* elms = FixedArray::cast(elms_obj);
|
2013-06-03 15:32:22 +00:00
|
|
|
DisallowHeapAllocation no_gc;
|
2012-11-15 12:19:14 +00:00
|
|
|
WriteBarrierMode mode = elms->GetWriteBarrierMode(no_gc);
|
|
|
|
for (int k = actual_start; k < actual_start + item_count; k++) {
|
|
|
|
elms->set(k, args[3 + k - actual_start], mode);
|
|
|
|
}
|
2010-02-17 10:54:49 +00:00
|
|
|
}
|
|
|
|
|
2011-09-22 11:30:04 +00:00
|
|
|
if (elms_changed) {
|
2012-11-15 12:19:14 +00:00
|
|
|
array->set_elements(elms_obj);
|
2011-09-22 11:30:04 +00:00
|
|
|
}
|
2010-02-17 10:54:49 +00:00
|
|
|
// Set the length.
|
|
|
|
array->set_length(Smi::FromInt(new_length));
|
|
|
|
|
|
|
|
return result_array;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-15 10:52:38 +00:00
|
|
|
BUILTIN(ArrayConcat) {
|
2011-03-18 20:35:07 +00:00
|
|
|
Heap* heap = isolate->heap();
|
2012-08-17 09:03:08 +00:00
|
|
|
Context* native_context = isolate->context()->native_context();
|
2010-05-12 12:22:09 +00:00
|
|
|
JSObject* array_proto =
|
2012-08-17 09:03:08 +00:00
|
|
|
JSObject::cast(native_context->array_function()->prototype());
|
|
|
|
if (!ArrayPrototypeHasNoElements(heap, native_context, array_proto)) {
|
2011-03-18 20:35:07 +00:00
|
|
|
return CallJsBuiltin(isolate, "ArrayConcat", args);
|
2010-03-15 10:52:38 +00:00
|
|
|
}
|
|
|
|
|
2010-03-17 09:40:54 +00:00
|
|
|
// Iterate through all the arguments performing checks
|
|
|
|
// and calculating total length.
|
|
|
|
int n_arguments = args.length();
|
|
|
|
int result_len = 0;
|
2012-05-23 14:24:29 +00:00
|
|
|
ElementsKind elements_kind = GetInitialFastElementsKind();
|
2012-11-22 16:22:57 +00:00
|
|
|
bool has_double = false;
|
|
|
|
bool is_holey = false;
|
2010-03-17 09:40:54 +00:00
|
|
|
for (int i = 0; i < n_arguments; i++) {
|
|
|
|
Object* arg = args[i];
|
2012-05-23 14:24:29 +00:00
|
|
|
if (!arg->IsJSArray() ||
|
2012-11-15 12:19:14 +00:00
|
|
|
!JSArray::cast(arg)->HasFastElements() ||
|
2012-05-23 14:24:29 +00:00
|
|
|
JSArray::cast(arg)->GetPrototype() != array_proto) {
|
2011-03-18 20:35:07 +00:00
|
|
|
return CallJsBuiltin(isolate, "ArrayConcat", args);
|
2010-03-17 09:40:54 +00:00
|
|
|
}
|
|
|
|
int len = Smi::cast(JSArray::cast(arg)->length())->value();
|
2010-03-15 10:52:38 +00:00
|
|
|
|
2010-03-17 09:40:54 +00:00
|
|
|
// We shouldn't overflow when adding another len.
|
|
|
|
const int kHalfOfMaxInt = 1 << (kBitsPerInt - 2);
|
|
|
|
STATIC_ASSERT(FixedArray::kMaxLength < kHalfOfMaxInt);
|
|
|
|
USE(kHalfOfMaxInt);
|
|
|
|
result_len += len;
|
|
|
|
ASSERT(result_len >= 0);
|
2010-03-15 10:52:38 +00:00
|
|
|
|
2012-11-15 12:19:14 +00:00
|
|
|
if (result_len > FixedDoubleArray::kMaxLength) {
|
2011-03-18 20:35:07 +00:00
|
|
|
return CallJsBuiltin(isolate, "ArrayConcat", args);
|
2010-03-17 09:40:54 +00:00
|
|
|
}
|
|
|
|
|
2012-11-15 12:19:14 +00:00
|
|
|
ElementsKind arg_kind = JSArray::cast(arg)->map()->elements_kind();
|
2012-11-22 16:22:57 +00:00
|
|
|
has_double = has_double || IsFastDoubleElementsKind(arg_kind);
|
|
|
|
is_holey = is_holey || IsFastHoleyElementsKind(arg_kind);
|
2012-11-19 15:31:38 +00:00
|
|
|
if (IsMoreGeneralElementsKindTransition(elements_kind, arg_kind)) {
|
2012-11-22 16:22:57 +00:00
|
|
|
elements_kind = arg_kind;
|
2012-05-23 14:24:29 +00:00
|
|
|
}
|
2010-03-15 10:52:38 +00:00
|
|
|
}
|
|
|
|
|
2012-11-22 16:22:57 +00:00
|
|
|
if (is_holey) elements_kind = GetHoleyElementsKind(elements_kind);
|
|
|
|
|
|
|
|
// If a double array is concatted into a fast elements array, the fast
|
|
|
|
// elements array needs to be initialized to contain proper holes, since
|
|
|
|
// boxing doubles may cause incremental marking.
|
|
|
|
ArrayStorageAllocationMode mode =
|
|
|
|
has_double && IsFastObjectElementsKind(elements_kind)
|
|
|
|
? INITIALIZE_ARRAY_ELEMENTS_WITH_HOLE : DONT_INITIALIZE_ARRAY_ELEMENTS;
|
2012-01-26 21:47:57 +00:00
|
|
|
JSArray* result_array;
|
2012-11-15 12:19:14 +00:00
|
|
|
// Allocate result.
|
2012-01-26 21:47:57 +00:00
|
|
|
MaybeObject* maybe_array =
|
|
|
|
heap->AllocateJSArrayAndStorage(elements_kind,
|
|
|
|
result_len,
|
2012-11-22 16:22:57 +00:00
|
|
|
result_len,
|
|
|
|
mode);
|
2012-01-26 21:47:57 +00:00
|
|
|
if (!maybe_array->To(&result_array)) return maybe_array;
|
|
|
|
if (result_len == 0) return result_array;
|
2011-09-22 11:30:04 +00:00
|
|
|
|
2012-11-15 12:19:14 +00:00
|
|
|
int j = 0;
|
|
|
|
FixedArrayBase* storage = result_array->elements();
|
2013-01-02 10:09:42 +00:00
|
|
|
ElementsAccessor* accessor = ElementsAccessor::ForKind(elements_kind);
|
2010-03-17 09:40:54 +00:00
|
|
|
for (int i = 0; i < n_arguments; i++) {
|
|
|
|
JSArray* array = JSArray::cast(args[i]);
|
|
|
|
int len = Smi::cast(array->length())->value();
|
2013-01-02 10:09:42 +00:00
|
|
|
ElementsKind from_kind = array->GetElementsKind();
|
2012-11-19 15:31:38 +00:00
|
|
|
if (len > 0) {
|
|
|
|
MaybeObject* maybe_failure =
|
2013-01-02 10:09:42 +00:00
|
|
|
accessor->CopyElements(array, 0, from_kind, storage, j, len);
|
2012-11-19 15:31:38 +00:00
|
|
|
if (maybe_failure->IsFailure()) return maybe_failure;
|
|
|
|
j += len;
|
|
|
|
}
|
2010-03-17 09:40:54 +00:00
|
|
|
}
|
2012-11-15 12:19:14 +00:00
|
|
|
|
|
|
|
ASSERT(j == result_len);
|
2010-03-15 10:52:38 +00:00
|
|
|
|
|
|
|
return result_array;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-03-17 20:28:17 +00:00
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
// Strict mode poison pills
|
|
|
|
|
|
|
|
|
2011-05-24 11:07:06 +00:00
|
|
|
BUILTIN(StrictModePoisonPill) {
|
2013-02-15 09:27:10 +00:00
|
|
|
HandleScope scope(isolate);
|
2011-03-18 20:35:07 +00:00
|
|
|
return isolate->Throw(*isolate->factory()->NewTypeError(
|
2011-05-24 11:07:06 +00:00
|
|
|
"strict_poison_pill", HandleVector<Object>(NULL, 0)));
|
2011-03-17 20:28:41 +00:00
|
|
|
}
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
|
|
|
|
|
2012-12-04 13:45:48 +00:00
|
|
|
// Searches the hidden prototype chain of the given object for the first
|
|
|
|
// object that is an instance of the given type. If no such object can
|
|
|
|
// be found then Heap::null_value() is returned.
|
|
|
|
static inline Object* FindHidden(Heap* heap,
|
|
|
|
Object* object,
|
|
|
|
FunctionTemplateInfo* type) {
|
|
|
|
if (object->IsInstanceOf(type)) return object;
|
2013-02-27 13:22:29 +00:00
|
|
|
Object* proto = object->GetPrototype(heap->isolate());
|
2012-12-04 13:45:48 +00:00
|
|
|
if (proto->IsJSObject() &&
|
|
|
|
JSObject::cast(proto)->map()->is_hidden_prototype()) {
|
|
|
|
return FindHidden(heap, proto, type);
|
|
|
|
}
|
|
|
|
return heap->null_value();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
// Returns the holder JSObject if the function can legally be called
|
|
|
|
// with this receiver. Returns Heap::null_value() if the call is
|
|
|
|
// illegal. Any arguments that don't fit the expected type is
|
2012-12-04 13:45:48 +00:00
|
|
|
// overwritten with undefined. Note that holder and the arguments are
|
|
|
|
// implicitly rewritten with the first object in the hidden prototype
|
|
|
|
// chain that actually has the expected type.
|
2011-03-18 20:35:07 +00:00
|
|
|
static inline Object* TypeCheck(Heap* heap,
|
|
|
|
int argc,
|
2008-07-03 15:10:15 +00:00
|
|
|
Object** argv,
|
|
|
|
FunctionTemplateInfo* info) {
|
|
|
|
Object* recv = argv[0];
|
2011-05-06 14:14:16 +00:00
|
|
|
// API calls are only supported with JSObject receivers.
|
|
|
|
if (!recv->IsJSObject()) return heap->null_value();
|
2008-07-03 15:10:15 +00:00
|
|
|
Object* sig_obj = info->signature();
|
|
|
|
if (sig_obj->IsUndefined()) return recv;
|
|
|
|
SignatureInfo* sig = SignatureInfo::cast(sig_obj);
|
|
|
|
// If necessary, check the receiver
|
|
|
|
Object* recv_type = sig->receiver();
|
|
|
|
Object* holder = recv;
|
|
|
|
if (!recv_type->IsUndefined()) {
|
2012-12-04 13:45:48 +00:00
|
|
|
holder = FindHidden(heap, holder, FunctionTemplateInfo::cast(recv_type));
|
|
|
|
if (holder == heap->null_value()) return heap->null_value();
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
Object* args_obj = sig->args();
|
|
|
|
// If there is no argument signature we're done
|
|
|
|
if (args_obj->IsUndefined()) return holder;
|
|
|
|
FixedArray* args = FixedArray::cast(args_obj);
|
|
|
|
int length = args->length();
|
2008-08-13 09:32:07 +00:00
|
|
|
if (argc <= length) length = argc - 1;
|
2008-07-03 15:10:15 +00:00
|
|
|
for (int i = 0; i < length; i++) {
|
|
|
|
Object* argtype = args->get(i);
|
|
|
|
if (argtype->IsUndefined()) continue;
|
|
|
|
Object** arg = &argv[-1 - i];
|
|
|
|
Object* current = *arg;
|
2012-12-04 13:45:48 +00:00
|
|
|
current = FindHidden(heap, current, FunctionTemplateInfo::cast(argtype));
|
|
|
|
if (current == heap->null_value()) current = heap->undefined_value();
|
|
|
|
*arg = current;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
return holder;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-01-15 12:25:24 +00:00
|
|
|
template <bool is_construct>
|
2010-10-25 15:22:03 +00:00
|
|
|
MUST_USE_RESULT static MaybeObject* HandleApiCallHelper(
|
2011-03-18 20:35:07 +00:00
|
|
|
BuiltinArguments<NEEDS_CALLED_FUNCTION> args, Isolate* isolate) {
|
|
|
|
ASSERT(is_construct == CalledAsConstructor(isolate));
|
|
|
|
Heap* heap = isolate->heap();
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
HandleScope scope(isolate);
|
2010-01-15 12:25:24 +00:00
|
|
|
Handle<JSFunction> function = args.called_function();
|
2010-03-11 16:24:31 +00:00
|
|
|
ASSERT(function->shared()->IsApiFunction());
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2010-03-11 16:24:31 +00:00
|
|
|
FunctionTemplateInfo* fun_data = function->shared()->get_api_func_data();
|
2008-07-03 15:10:15 +00:00
|
|
|
if (is_construct) {
|
2011-03-18 20:35:07 +00:00
|
|
|
Handle<FunctionTemplateInfo> desc(fun_data, isolate);
|
2008-07-03 15:10:15 +00:00
|
|
|
bool pending_exception = false;
|
2011-03-18 20:35:07 +00:00
|
|
|
isolate->factory()->ConfigureInstance(
|
|
|
|
desc, Handle<JSObject>::cast(args.receiver()), &pending_exception);
|
|
|
|
ASSERT(isolate->has_pending_exception() == pending_exception);
|
2008-07-03 15:10:15 +00:00
|
|
|
if (pending_exception) return Failure::Exception();
|
2010-03-11 16:24:31 +00:00
|
|
|
fun_data = *desc;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
Object* raw_holder = TypeCheck(heap, args.length(), &args[0], fun_data);
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
if (raw_holder->IsNull()) {
|
|
|
|
// This function cannot be called with the given receiver. Abort!
|
|
|
|
Handle<Object> obj =
|
2011-03-18 20:35:07 +00:00
|
|
|
isolate->factory()->NewTypeError(
|
|
|
|
"illegal_invocation", HandleVector(&function, 1));
|
|
|
|
return isolate->Throw(*obj);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Object* raw_call_data = fun_data->call_code();
|
|
|
|
if (!raw_call_data->IsUndefined()) {
|
|
|
|
CallHandlerInfo* call_data = CallHandlerInfo::cast(raw_call_data);
|
|
|
|
Object* callback_obj = call_data->callback();
|
|
|
|
v8::InvocationCallback callback =
|
|
|
|
v8::ToCData<v8::InvocationCallback>(callback_obj);
|
|
|
|
Object* data_obj = call_data->data();
|
|
|
|
Object* result;
|
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
LOG(isolate, ApiObjectAccess("call", JSObject::cast(*args.receiver())));
|
2010-11-01 10:51:44 +00:00
|
|
|
ASSERT(raw_holder->IsJSObject());
|
|
|
|
|
2013-05-21 06:36:24 +00:00
|
|
|
FunctionCallbackArguments custom(isolate,
|
|
|
|
data_obj,
|
|
|
|
*function,
|
|
|
|
raw_holder,
|
|
|
|
&args[0] - 1,
|
|
|
|
args.length() - 1,
|
|
|
|
is_construct);
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
v8::Handle<v8::Value> value;
|
|
|
|
{
|
|
|
|
// Leaving JavaScript.
|
2013-04-24 14:44:08 +00:00
|
|
|
VMState<EXTERNAL> state(isolate);
|
2011-03-18 20:35:07 +00:00
|
|
|
ExternalCallbackScope call_scope(isolate,
|
|
|
|
v8::ToCData<Address>(callback_obj));
|
2013-05-21 06:36:24 +00:00
|
|
|
value = custom.Call(callback);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
if (value.IsEmpty()) {
|
2011-03-18 20:35:07 +00:00
|
|
|
result = heap->undefined_value();
|
2008-07-03 15:10:15 +00:00
|
|
|
} else {
|
|
|
|
result = *reinterpret_cast<Object**>(*value);
|
2012-09-10 13:38:21 +00:00
|
|
|
result->VerifyApiCallResultType();
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
RETURN_IF_SCHEDULED_EXCEPTION(isolate);
|
2008-07-03 15:10:15 +00:00
|
|
|
if (!is_construct || result->IsJSObject()) return result;
|
|
|
|
}
|
|
|
|
|
2010-01-15 12:25:24 +00:00
|
|
|
return *args.receiver();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BUILTIN(HandleApiCall) {
|
2011-03-18 20:35:07 +00:00
|
|
|
return HandleApiCallHelper<false>(args, isolate);
|
2010-01-15 12:25:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BUILTIN(HandleApiCallConstruct) {
|
2011-03-18 20:35:07 +00:00
|
|
|
return HandleApiCallHelper<true>(args, isolate);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-05-20 19:33:44 +00:00
|
|
|
// Helper function to handle calls to non-function objects created through the
|
|
|
|
// API. The object can be called as either a constructor (using new) or just as
|
|
|
|
// a function (without new).
|
2010-10-25 15:22:03 +00:00
|
|
|
MUST_USE_RESULT static MaybeObject* HandleApiCallAsFunctionOrConstructor(
|
2011-03-18 20:35:07 +00:00
|
|
|
Isolate* isolate,
|
2010-01-15 12:25:24 +00:00
|
|
|
bool is_construct_call,
|
|
|
|
BuiltinArguments<NO_EXTRA_ARGUMENTS> args) {
|
2009-05-20 19:33:44 +00:00
|
|
|
// Non-functions are never called as constructors. Even if this is an object
|
|
|
|
// called as a constructor the delegate call is not a construct call.
|
2011-03-18 20:35:07 +00:00
|
|
|
ASSERT(!CalledAsConstructor(isolate));
|
|
|
|
Heap* heap = isolate->heap();
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2011-07-20 08:09:58 +00:00
|
|
|
Handle<Object> receiver = args.receiver();
|
2009-05-20 19:33:44 +00:00
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
// Get the object called.
|
2011-07-20 08:09:58 +00:00
|
|
|
JSObject* obj = JSObject::cast(*receiver);
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
// Get the invocation callback from the function descriptor that was
|
|
|
|
// used to create the called object.
|
|
|
|
ASSERT(obj->map()->has_instance_call_handler());
|
|
|
|
JSFunction* constructor = JSFunction::cast(obj->map()->constructor());
|
2010-03-11 16:24:31 +00:00
|
|
|
ASSERT(constructor->shared()->IsApiFunction());
|
2008-07-03 15:10:15 +00:00
|
|
|
Object* handler =
|
2010-03-11 16:24:31 +00:00
|
|
|
constructor->shared()->get_api_func_data()->instance_call_handler();
|
2008-07-03 15:10:15 +00:00
|
|
|
ASSERT(!handler->IsUndefined());
|
|
|
|
CallHandlerInfo* call_data = CallHandlerInfo::cast(handler);
|
|
|
|
Object* callback_obj = call_data->callback();
|
|
|
|
v8::InvocationCallback callback =
|
|
|
|
v8::ToCData<v8::InvocationCallback>(callback_obj);
|
|
|
|
|
|
|
|
// Get the data for the call and perform the callback.
|
|
|
|
Object* result;
|
2010-11-01 10:51:44 +00:00
|
|
|
{
|
2011-03-18 20:35:07 +00:00
|
|
|
HandleScope scope(isolate);
|
|
|
|
LOG(isolate, ApiObjectAccess("call non-function", obj));
|
2010-11-01 10:51:44 +00:00
|
|
|
|
2013-05-21 06:36:24 +00:00
|
|
|
FunctionCallbackArguments custom(isolate,
|
|
|
|
call_data->data(),
|
|
|
|
constructor,
|
|
|
|
obj,
|
|
|
|
&args[0] - 1,
|
|
|
|
args.length() - 1,
|
|
|
|
is_construct_call);
|
2008-07-03 15:10:15 +00:00
|
|
|
v8::Handle<v8::Value> value;
|
|
|
|
{
|
|
|
|
// Leaving JavaScript.
|
2013-04-24 14:44:08 +00:00
|
|
|
VMState<EXTERNAL> state(isolate);
|
2011-03-18 20:35:07 +00:00
|
|
|
ExternalCallbackScope call_scope(isolate,
|
|
|
|
v8::ToCData<Address>(callback_obj));
|
2013-05-21 06:36:24 +00:00
|
|
|
value = custom.Call(callback);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
if (value.IsEmpty()) {
|
2011-03-18 20:35:07 +00:00
|
|
|
result = heap->undefined_value();
|
2008-07-03 15:10:15 +00:00
|
|
|
} else {
|
|
|
|
result = *reinterpret_cast<Object**>(*value);
|
2012-09-10 13:38:21 +00:00
|
|
|
result->VerifyApiCallResultType();
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Check for exceptions and return result.
|
2011-03-18 20:35:07 +00:00
|
|
|
RETURN_IF_SCHEDULED_EXCEPTION(isolate);
|
2008-07-03 15:10:15 +00:00
|
|
|
return result;
|
|
|
|
}
|
2009-05-20 19:33:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
// Handle calls to non-function objects created through the API. This delegate
|
|
|
|
// function is used when the call is a normal function call.
|
|
|
|
BUILTIN(HandleApiCallAsFunction) {
|
2011-03-18 20:35:07 +00:00
|
|
|
return HandleApiCallAsFunctionOrConstructor(isolate, false, args);
|
2009-05-20 19:33:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Handle calls to non-function objects created through the API. This delegate
|
|
|
|
// function is used when the call is a construct call.
|
|
|
|
BUILTIN(HandleApiCallAsConstructor) {
|
2011-03-18 20:35:07 +00:00
|
|
|
return HandleApiCallAsFunctionOrConstructor(isolate, true, args);
|
2009-05-20 19:33:44 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
static void Generate_LoadIC_Initialize(MacroAssembler* masm) {
|
|
|
|
LoadIC::GenerateInitialize(masm);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void Generate_LoadIC_PreMonomorphic(MacroAssembler* masm) {
|
|
|
|
LoadIC::GeneratePreMonomorphic(masm);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void Generate_LoadIC_Miss(MacroAssembler* masm) {
|
|
|
|
LoadIC::GenerateMiss(masm);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void Generate_LoadIC_Megamorphic(MacroAssembler* masm) {
|
|
|
|
LoadIC::GenerateMegamorphic(masm);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void Generate_LoadIC_Normal(MacroAssembler* masm) {
|
|
|
|
LoadIC::GenerateNormal(masm);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-09-07 09:01:54 +00:00
|
|
|
static void Generate_LoadIC_Getter_ForDeopt(MacroAssembler* masm) {
|
|
|
|
LoadStubCompiler::GenerateLoadViaGetter(masm, Handle<JSFunction>());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-05 11:12:49 +00:00
|
|
|
static void Generate_LoadIC_Slow(MacroAssembler* masm) {
|
|
|
|
LoadIC::GenerateRuntimeGetProperty(masm);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
static void Generate_KeyedLoadIC_Initialize(MacroAssembler* masm) {
|
|
|
|
KeyedLoadIC::GenerateInitialize(masm);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-18 13:17:29 +00:00
|
|
|
static void Generate_KeyedLoadIC_Slow(MacroAssembler* masm) {
|
|
|
|
KeyedLoadIC::GenerateRuntimeGetProperty(masm);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
static void Generate_KeyedLoadIC_Miss(MacroAssembler* masm) {
|
2013-01-04 15:37:59 +00:00
|
|
|
KeyedLoadIC::GenerateMiss(masm, MISS);
|
2011-05-18 13:17:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void Generate_KeyedLoadIC_MissForceGeneric(MacroAssembler* masm) {
|
2013-01-04 15:37:59 +00:00
|
|
|
KeyedLoadIC::GenerateMiss(masm, MISS_FORCE_GENERIC);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void Generate_KeyedLoadIC_Generic(MacroAssembler* masm) {
|
|
|
|
KeyedLoadIC::GenerateGeneric(masm);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-01-07 10:25:20 +00:00
|
|
|
static void Generate_KeyedLoadIC_String(MacroAssembler* masm) {
|
|
|
|
KeyedLoadIC::GenerateString(masm);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
static void Generate_KeyedLoadIC_PreMonomorphic(MacroAssembler* masm) {
|
|
|
|
KeyedLoadIC::GeneratePreMonomorphic(masm);
|
|
|
|
}
|
|
|
|
|
2010-02-12 14:21:18 +00:00
|
|
|
static void Generate_KeyedLoadIC_IndexedInterceptor(MacroAssembler* masm) {
|
|
|
|
KeyedLoadIC::GenerateIndexedInterceptor(masm);
|
|
|
|
}
|
|
|
|
|
2011-06-16 14:12:58 +00:00
|
|
|
static void Generate_KeyedLoadIC_NonStrictArguments(MacroAssembler* masm) {
|
|
|
|
KeyedLoadIC::GenerateNonStrictArguments(masm);
|
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2013-05-08 15:02:08 +00:00
|
|
|
static void Generate_StoreIC_Slow(MacroAssembler* masm) {
|
|
|
|
StoreIC::GenerateSlow(masm);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
static void Generate_StoreIC_Initialize(MacroAssembler* masm) {
|
|
|
|
StoreIC::GenerateInitialize(masm);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-02-13 16:19:53 +00:00
|
|
|
static void Generate_StoreIC_Initialize_Strict(MacroAssembler* masm) {
|
|
|
|
StoreIC::GenerateInitialize(masm);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
static void Generate_StoreIC_Miss(MacroAssembler* masm) {
|
|
|
|
StoreIC::GenerateMiss(masm);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-06-30 12:27:49 +00:00
|
|
|
static void Generate_StoreIC_Normal(MacroAssembler* masm) {
|
|
|
|
StoreIC::GenerateNormal(masm);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-02-13 16:19:53 +00:00
|
|
|
static void Generate_StoreIC_Normal_Strict(MacroAssembler* masm) {
|
|
|
|
StoreIC::GenerateNormal(masm);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
static void Generate_StoreIC_Megamorphic(MacroAssembler* masm) {
|
2011-03-02 04:53:43 +00:00
|
|
|
StoreIC::GenerateMegamorphic(masm, kNonStrictMode);
|
2011-02-13 16:19:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void Generate_StoreIC_Megamorphic_Strict(MacroAssembler* masm) {
|
2011-03-02 04:53:43 +00:00
|
|
|
StoreIC::GenerateMegamorphic(masm, kStrictMode);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-12-07 11:31:57 +00:00
|
|
|
static void Generate_StoreIC_GlobalProxy(MacroAssembler* masm) {
|
2013-06-14 11:21:34 +00:00
|
|
|
StoreIC::GenerateRuntimeSetProperty(masm, kNonStrictMode);
|
2010-12-07 11:31:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-02-13 16:19:53 +00:00
|
|
|
static void Generate_StoreIC_GlobalProxy_Strict(MacroAssembler* masm) {
|
2013-06-14 11:21:34 +00:00
|
|
|
StoreIC::GenerateRuntimeSetProperty(masm, kStrictMode);
|
2011-02-13 16:19:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-08-17 10:43:32 +00:00
|
|
|
static void Generate_StoreIC_Setter_ForDeopt(MacroAssembler* masm) {
|
|
|
|
StoreStubCompiler::GenerateStoreViaSetter(masm, Handle<JSFunction>());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-14 11:21:34 +00:00
|
|
|
static void Generate_StoreIC_Generic(MacroAssembler* masm) {
|
|
|
|
StoreIC::GenerateRuntimeSetProperty(masm, kNonStrictMode);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void Generate_StoreIC_Generic_Strict(MacroAssembler* masm) {
|
|
|
|
StoreIC::GenerateRuntimeSetProperty(masm, kStrictMode);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
static void Generate_KeyedStoreIC_Generic(MacroAssembler* masm) {
|
2011-03-02 04:53:43 +00:00
|
|
|
KeyedStoreIC::GenerateGeneric(masm, kNonStrictMode);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void Generate_KeyedStoreIC_Generic_Strict(MacroAssembler* masm) {
|
|
|
|
KeyedStoreIC::GenerateGeneric(masm, kStrictMode);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void Generate_KeyedStoreIC_Miss(MacroAssembler* masm) {
|
2013-01-04 15:37:59 +00:00
|
|
|
KeyedStoreIC::GenerateMiss(masm, MISS);
|
2011-05-18 13:17:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void Generate_KeyedStoreIC_MissForceGeneric(MacroAssembler* masm) {
|
2013-01-04 15:37:59 +00:00
|
|
|
KeyedStoreIC::GenerateMiss(masm, MISS_FORCE_GENERIC);
|
2011-05-18 13:17:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void Generate_KeyedStoreIC_Slow(MacroAssembler* masm) {
|
|
|
|
KeyedStoreIC::GenerateSlow(masm);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void Generate_KeyedStoreIC_Initialize(MacroAssembler* masm) {
|
|
|
|
KeyedStoreIC::GenerateInitialize(masm);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-03-02 04:53:43 +00:00
|
|
|
static void Generate_KeyedStoreIC_Initialize_Strict(MacroAssembler* masm) {
|
|
|
|
KeyedStoreIC::GenerateInitialize(masm);
|
|
|
|
}
|
|
|
|
|
2011-06-16 14:12:58 +00:00
|
|
|
static void Generate_KeyedStoreIC_NonStrictArguments(MacroAssembler* masm) {
|
|
|
|
KeyedStoreIC::GenerateNonStrictArguments(masm);
|
|
|
|
}
|
2011-03-02 04:53:43 +00:00
|
|
|
|
2011-10-19 12:10:18 +00:00
|
|
|
static void Generate_TransitionElementsSmiToDouble(MacroAssembler* masm) {
|
|
|
|
KeyedStoreIC::GenerateTransitionElementsSmiToDouble(masm);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void Generate_TransitionElementsDoubleToObject(MacroAssembler* masm) {
|
|
|
|
KeyedStoreIC::GenerateTransitionElementsDoubleToObject(masm);
|
|
|
|
}
|
|
|
|
|
2009-04-20 16:36:13 +00:00
|
|
|
#ifdef ENABLE_DEBUGGER_SUPPORT
|
2008-12-09 11:12:14 +00:00
|
|
|
static void Generate_LoadIC_DebugBreak(MacroAssembler* masm) {
|
|
|
|
Debug::GenerateLoadICDebugBreak(masm);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void Generate_StoreIC_DebugBreak(MacroAssembler* masm) {
|
|
|
|
Debug::GenerateStoreICDebugBreak(masm);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void Generate_KeyedLoadIC_DebugBreak(MacroAssembler* masm) {
|
|
|
|
Debug::GenerateKeyedLoadICDebugBreak(masm);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void Generate_KeyedStoreIC_DebugBreak(MacroAssembler* masm) {
|
|
|
|
Debug::GenerateKeyedStoreICDebugBreak(masm);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-04-30 07:56:09 +00:00
|
|
|
static void Generate_CompareNilIC_DebugBreak(MacroAssembler* masm) {
|
|
|
|
Debug::GenerateCompareNilICDebugBreak(masm);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-12-09 11:12:14 +00:00
|
|
|
static void Generate_Return_DebugBreak(MacroAssembler* masm) {
|
|
|
|
Debug::GenerateReturnDebugBreak(masm);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-08 14:39:37 +00:00
|
|
|
static void Generate_CallFunctionStub_DebugBreak(MacroAssembler* masm) {
|
|
|
|
Debug::GenerateCallFunctionStubDebugBreak(masm);
|
2008-12-09 11:12:14 +00:00
|
|
|
}
|
2010-04-06 17:58:28 +00:00
|
|
|
|
2010-06-08 12:04:49 +00:00
|
|
|
|
2012-01-27 16:09:20 +00:00
|
|
|
static void Generate_CallFunctionStub_Recording_DebugBreak(
|
|
|
|
MacroAssembler* masm) {
|
|
|
|
Debug::GenerateCallFunctionStubRecordDebugBreak(masm);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void Generate_CallConstructStub_DebugBreak(MacroAssembler* masm) {
|
|
|
|
Debug::GenerateCallConstructStubDebugBreak(masm);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void Generate_CallConstructStub_Recording_DebugBreak(
|
|
|
|
MacroAssembler* masm) {
|
|
|
|
Debug::GenerateCallConstructStubRecordDebugBreak(masm);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-06-08 12:04:49 +00:00
|
|
|
static void Generate_Slot_DebugBreak(MacroAssembler* masm) {
|
|
|
|
Debug::GenerateSlotDebugBreak(masm);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-04-06 17:58:28 +00:00
|
|
|
static void Generate_PlainReturn_LiveEdit(MacroAssembler* masm) {
|
|
|
|
Debug::GeneratePlainReturnLiveEdit(masm);
|
|
|
|
}
|
|
|
|
|
2010-06-08 12:04:49 +00:00
|
|
|
|
2010-04-06 17:58:28 +00:00
|
|
|
static void Generate_FrameDropper_LiveEdit(MacroAssembler* masm) {
|
|
|
|
Debug::GenerateFrameDropperLiveEdit(masm);
|
|
|
|
}
|
2009-04-20 16:36:13 +00:00
|
|
|
#endif
|
2008-12-09 11:12:14 +00:00
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
|
|
|
|
Builtins::Builtins() : initialized_(false) {
|
|
|
|
memset(builtins_, 0, sizeof(builtins_[0]) * builtin_count);
|
|
|
|
memset(names_, 0, sizeof(names_[0]) * builtin_count);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Builtins::~Builtins() {
|
|
|
|
}
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2010-01-15 12:25:24 +00:00
|
|
|
#define DEF_ENUM_C(name, ignore) FUNCTION_ADDR(Builtin_##name),
|
2011-03-18 20:35:07 +00:00
|
|
|
Address const Builtins::c_functions_[cfunction_count] = {
|
|
|
|
BUILTIN_LIST_C(DEF_ENUM_C)
|
|
|
|
};
|
2008-07-03 15:10:15 +00:00
|
|
|
#undef DEF_ENUM_C
|
|
|
|
|
|
|
|
#define DEF_JS_NAME(name, ignore) #name,
|
|
|
|
#define DEF_JS_ARGC(ignore, argc) argc,
|
2011-03-18 20:35:07 +00:00
|
|
|
const char* const Builtins::javascript_names_[id_count] = {
|
2008-07-03 15:10:15 +00:00
|
|
|
BUILTINS_LIST_JS(DEF_JS_NAME)
|
|
|
|
};
|
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
int const Builtins::javascript_argc_[id_count] = {
|
2008-07-03 15:10:15 +00:00
|
|
|
BUILTINS_LIST_JS(DEF_JS_ARGC)
|
|
|
|
};
|
|
|
|
#undef DEF_JS_NAME
|
|
|
|
#undef DEF_JS_ARGC
|
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
struct BuiltinDesc {
|
|
|
|
byte* generator;
|
|
|
|
byte* c_code;
|
|
|
|
const char* s_name; // name is only used for generating log information.
|
|
|
|
int name;
|
|
|
|
Code::Flags flags;
|
|
|
|
BuiltinExtraArguments extra_args;
|
|
|
|
};
|
2011-03-18 18:49:56 +00:00
|
|
|
|
2012-03-12 13:56:56 +00:00
|
|
|
#define BUILTIN_FUNCTION_TABLE_INIT { V8_ONCE_INIT, {} }
|
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
class BuiltinFunctionTable {
|
|
|
|
public:
|
2012-03-12 13:56:56 +00:00
|
|
|
BuiltinDesc* functions() {
|
|
|
|
CallOnce(&once_, &Builtins::InitBuiltinFunctionTable);
|
|
|
|
return functions_;
|
2011-03-18 20:35:07 +00:00
|
|
|
}
|
|
|
|
|
2012-03-12 13:56:56 +00:00
|
|
|
OnceType once_;
|
|
|
|
BuiltinDesc functions_[Builtins::builtin_count + 1];
|
2011-03-18 20:35:07 +00:00
|
|
|
|
|
|
|
friend class Builtins;
|
|
|
|
};
|
2011-03-18 18:49:56 +00:00
|
|
|
|
2012-03-12 13:56:56 +00:00
|
|
|
static BuiltinFunctionTable builtin_function_table =
|
|
|
|
BUILTIN_FUNCTION_TABLE_INIT;
|
2011-03-18 20:35:07 +00:00
|
|
|
|
|
|
|
// Define array of pointers to generators and C builtin functions.
|
|
|
|
// We do this in a sort of roundabout way so that we can do the initialization
|
|
|
|
// within the lexical scope of Builtins:: and within a context where
|
|
|
|
// Code::Flags names a non-abstract type.
|
|
|
|
void Builtins::InitBuiltinFunctionTable() {
|
2012-03-12 13:56:56 +00:00
|
|
|
BuiltinDesc* functions = builtin_function_table.functions_;
|
2011-03-18 20:35:07 +00:00
|
|
|
functions[builtin_count].generator = NULL;
|
|
|
|
functions[builtin_count].c_code = NULL;
|
|
|
|
functions[builtin_count].s_name = NULL;
|
|
|
|
functions[builtin_count].name = builtin_count;
|
|
|
|
functions[builtin_count].flags = static_cast<Code::Flags>(0);
|
|
|
|
functions[builtin_count].extra_args = NO_EXTRA_ARGUMENTS;
|
|
|
|
|
|
|
|
#define DEF_FUNCTION_PTR_C(aname, aextra_args) \
|
|
|
|
functions->generator = FUNCTION_ADDR(Generate_Adaptor); \
|
|
|
|
functions->c_code = FUNCTION_ADDR(Builtin_##aname); \
|
|
|
|
functions->s_name = #aname; \
|
|
|
|
functions->name = c_##aname; \
|
|
|
|
functions->flags = Code::ComputeFlags(Code::BUILTIN); \
|
|
|
|
functions->extra_args = aextra_args; \
|
|
|
|
++functions;
|
|
|
|
|
|
|
|
#define DEF_FUNCTION_PTR_A(aname, kind, state, extra) \
|
|
|
|
functions->generator = FUNCTION_ADDR(Generate_##aname); \
|
|
|
|
functions->c_code = NULL; \
|
|
|
|
functions->s_name = #aname; \
|
2011-03-23 13:40:07 +00:00
|
|
|
functions->name = k##aname; \
|
2011-03-18 20:35:07 +00:00
|
|
|
functions->flags = Code::ComputeFlags(Code::kind, \
|
|
|
|
state, \
|
|
|
|
extra); \
|
|
|
|
functions->extra_args = NO_EXTRA_ARGUMENTS; \
|
|
|
|
++functions;
|
|
|
|
|
|
|
|
BUILTIN_LIST_C(DEF_FUNCTION_PTR_C)
|
|
|
|
BUILTIN_LIST_A(DEF_FUNCTION_PTR_A)
|
|
|
|
BUILTIN_LIST_DEBUG_A(DEF_FUNCTION_PTR_A)
|
2011-03-18 19:41:05 +00:00
|
|
|
|
|
|
|
#undef DEF_FUNCTION_PTR_C
|
|
|
|
#undef DEF_FUNCTION_PTR_A
|
2011-03-18 20:35:07 +00:00
|
|
|
}
|
|
|
|
|
2012-01-13 13:09:52 +00:00
|
|
|
void Builtins::SetUp(bool create_heap_objects) {
|
2011-03-18 20:35:07 +00:00
|
|
|
ASSERT(!initialized_);
|
2011-04-01 14:46:30 +00:00
|
|
|
Isolate* isolate = Isolate::Current();
|
|
|
|
Heap* heap = isolate->heap();
|
2011-03-18 20:35:07 +00:00
|
|
|
|
|
|
|
// Create a scope for the handles in the builtins.
|
2011-04-01 14:46:30 +00:00
|
|
|
HandleScope scope(isolate);
|
2011-03-18 20:35:07 +00:00
|
|
|
|
2012-03-12 13:56:56 +00:00
|
|
|
const BuiltinDesc* functions = builtin_function_table.functions();
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
// For now we generate builtin adaptor code into a stack-allocated
|
2011-09-21 07:59:28 +00:00
|
|
|
// buffer, before copying it into individual code objects. Be careful
|
|
|
|
// with alignment, some platforms don't like unaligned code.
|
2012-09-25 13:35:42 +00:00
|
|
|
union { int force_alignment; byte buffer[8*KB]; } u;
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
// Traverse the list of builtins and generate an adaptor in a
|
|
|
|
// separate code object for each one.
|
|
|
|
for (int i = 0; i < builtin_count; i++) {
|
|
|
|
if (create_heap_objects) {
|
2011-09-21 07:59:28 +00:00
|
|
|
MacroAssembler masm(isolate, u.buffer, sizeof u.buffer);
|
2008-07-03 15:10:15 +00:00
|
|
|
// Generate the code/adaptor.
|
2010-01-15 12:25:24 +00:00
|
|
|
typedef void (*Generator)(MacroAssembler*, int, BuiltinExtraArguments);
|
2008-07-03 15:10:15 +00:00
|
|
|
Generator g = FUNCTION_CAST<Generator>(functions[i].generator);
|
|
|
|
// We pass all arguments to the generator, but it may not use all of
|
|
|
|
// them. This works because the first arguments are on top of the
|
|
|
|
// stack.
|
2011-09-15 11:30:45 +00:00
|
|
|
ASSERT(!masm.has_frame());
|
2010-01-15 12:25:24 +00:00
|
|
|
g(&masm, functions[i].name, functions[i].extra_args);
|
2008-07-03 15:10:15 +00:00
|
|
|
// Move the code into the object heap.
|
|
|
|
CodeDesc desc;
|
|
|
|
masm.GetCode(&desc);
|
|
|
|
Code::Flags flags = functions[i].flags;
|
2011-01-18 16:11:01 +00:00
|
|
|
Object* code = NULL;
|
2008-10-30 11:12:08 +00:00
|
|
|
{
|
|
|
|
// During startup it's OK to always allocate and defer GC to later.
|
|
|
|
// This simplifies things because we don't need to retry.
|
|
|
|
AlwaysAllocateScope __scope__;
|
2010-10-25 15:22:03 +00:00
|
|
|
{ MaybeObject* maybe_code =
|
2011-03-18 20:35:07 +00:00
|
|
|
heap->CreateCode(desc, flags, masm.CodeObject());
|
2010-10-25 15:22:03 +00:00
|
|
|
if (!maybe_code->ToObject(&code)) {
|
|
|
|
v8::internal::V8::FatalProcessOutOfMemory("CreateCode");
|
|
|
|
}
|
2008-10-09 11:26:37 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
// Log the event and add the code to the builtins array.
|
2011-04-01 14:46:30 +00:00
|
|
|
PROFILE(isolate,
|
2011-03-18 20:35:07 +00:00
|
|
|
CodeCreateEvent(Logger::BUILTIN_TAG,
|
2011-01-18 16:11:01 +00:00
|
|
|
Code::cast(code),
|
|
|
|
functions[i].s_name));
|
|
|
|
GDBJIT(AddCode(GDBJITInterface::BUILTIN,
|
|
|
|
functions[i].s_name,
|
|
|
|
Code::cast(code)));
|
2008-07-03 15:10:15 +00:00
|
|
|
builtins_[i] = code;
|
2009-03-04 06:14:34 +00:00
|
|
|
#ifdef ENABLE_DISASSEMBLER
|
2008-08-13 09:32:07 +00:00
|
|
|
if (FLAG_print_builtin_code) {
|
|
|
|
PrintF("Builtin: %s\n", functions[i].s_name);
|
2009-03-04 06:14:34 +00:00
|
|
|
Code::cast(code)->Disassemble(functions[i].s_name);
|
2008-08-13 09:32:07 +00:00
|
|
|
PrintF("\n");
|
|
|
|
}
|
|
|
|
#endif
|
2008-07-03 15:10:15 +00:00
|
|
|
} else {
|
|
|
|
// Deserializing. The values will be filled in during IterateBuiltins.
|
|
|
|
builtins_[i] = NULL;
|
|
|
|
}
|
|
|
|
names_[i] = functions[i].s_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mark as initialized.
|
2011-03-18 20:35:07 +00:00
|
|
|
initialized_ = true;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Builtins::TearDown() {
|
2011-03-18 20:35:07 +00:00
|
|
|
initialized_ = false;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Builtins::IterateBuiltins(ObjectVisitor* v) {
|
|
|
|
v->VisitPointers(&builtins_[0], &builtins_[0] + builtin_count);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const char* Builtins::Lookup(byte* pc) {
|
2011-03-18 20:35:07 +00:00
|
|
|
// may be called during initialization (disassembler!)
|
|
|
|
if (initialized_) {
|
2008-07-03 15:10:15 +00:00
|
|
|
for (int i = 0; i < builtin_count; i++) {
|
|
|
|
Code* entry = Code::cast(builtins_[i]);
|
|
|
|
if (entry->contains(pc)) {
|
|
|
|
return names_[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-12-07 11:31:57 +00:00
|
|
|
|
2011-03-23 13:40:07 +00:00
|
|
|
#define DEFINE_BUILTIN_ACCESSOR_C(name, ignore) \
|
|
|
|
Handle<Code> Builtins::name() { \
|
|
|
|
Code** code_address = \
|
|
|
|
reinterpret_cast<Code**>(builtin_address(k##name)); \
|
|
|
|
return Handle<Code>(code_address); \
|
|
|
|
}
|
|
|
|
#define DEFINE_BUILTIN_ACCESSOR_A(name, kind, state, extra) \
|
|
|
|
Handle<Code> Builtins::name() { \
|
|
|
|
Code** code_address = \
|
|
|
|
reinterpret_cast<Code**>(builtin_address(k##name)); \
|
|
|
|
return Handle<Code>(code_address); \
|
|
|
|
}
|
|
|
|
BUILTIN_LIST_C(DEFINE_BUILTIN_ACCESSOR_C)
|
|
|
|
BUILTIN_LIST_A(DEFINE_BUILTIN_ACCESSOR_A)
|
|
|
|
BUILTIN_LIST_DEBUG_A(DEFINE_BUILTIN_ACCESSOR_A)
|
|
|
|
#undef DEFINE_BUILTIN_ACCESSOR_C
|
|
|
|
#undef DEFINE_BUILTIN_ACCESSOR_A
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
} } // namespace v8::internal
|