Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1 | // Copyright 2012 the V8 project authors. All rights reserved. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5 | #include "src/v8.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7 | #include "src/api.h" |
| 8 | #include "src/arguments.h" |
| 9 | #include "src/base/once.h" |
| 10 | #include "src/bootstrapper.h" |
| 11 | #include "src/builtins.h" |
| 12 | #include "src/cpu-profiler.h" |
| 13 | #include "src/gdb-jit.h" |
| 14 | #include "src/heap/mark-compact.h" |
| 15 | #include "src/heap-profiler.h" |
| 16 | #include "src/ic/handler-compiler.h" |
| 17 | #include "src/ic/ic.h" |
| 18 | #include "src/prototype.h" |
| 19 | #include "src/vm-state-inl.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 20 | |
| 21 | namespace v8 { |
| 22 | namespace internal { |
| 23 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 24 | namespace { |
| 25 | |
| 26 | // Arguments object passed to C++ builtins. |
| 27 | template <BuiltinExtraArguments extra_args> |
| 28 | class BuiltinArguments : public Arguments { |
| 29 | public: |
| 30 | BuiltinArguments(int length, Object** arguments) |
| 31 | : Arguments(length, arguments) { } |
| 32 | |
| 33 | Object*& operator[] (int index) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 34 | DCHECK(index < length()); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 35 | return Arguments::operator[](index); |
| 36 | } |
| 37 | |
| 38 | template <class S> Handle<S> at(int index) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 39 | DCHECK(index < length()); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 40 | return Arguments::at<S>(index); |
| 41 | } |
| 42 | |
| 43 | Handle<Object> receiver() { |
| 44 | return Arguments::at<Object>(0); |
| 45 | } |
| 46 | |
| 47 | Handle<JSFunction> called_function() { |
| 48 | STATIC_ASSERT(extra_args == NEEDS_CALLED_FUNCTION); |
| 49 | return Arguments::at<JSFunction>(Arguments::length() - 1); |
| 50 | } |
| 51 | |
| 52 | // Gets the total number of arguments including the receiver (but |
| 53 | // excluding extra arguments). |
| 54 | int length() const { |
| 55 | STATIC_ASSERT(extra_args == NO_EXTRA_ARGUMENTS); |
| 56 | return Arguments::length(); |
| 57 | } |
| 58 | |
| 59 | #ifdef DEBUG |
| 60 | void Verify() { |
| 61 | // Check we have at least the receiver. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 62 | DCHECK(Arguments::length() >= 1); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 63 | } |
| 64 | #endif |
| 65 | }; |
| 66 | |
| 67 | |
| 68 | // Specialize BuiltinArguments for the called function extra argument. |
| 69 | |
| 70 | template <> |
| 71 | int BuiltinArguments<NEEDS_CALLED_FUNCTION>::length() const { |
| 72 | return Arguments::length() - 1; |
| 73 | } |
| 74 | |
| 75 | #ifdef DEBUG |
| 76 | template <> |
| 77 | void BuiltinArguments<NEEDS_CALLED_FUNCTION>::Verify() { |
| 78 | // Check we have at least the receiver and the called function. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 79 | DCHECK(Arguments::length() >= 2); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 80 | // Make sure cast to JSFunction succeeds. |
| 81 | called_function(); |
| 82 | } |
| 83 | #endif |
| 84 | |
| 85 | |
| 86 | #define DEF_ARG_TYPE(name, spec) \ |
| 87 | typedef BuiltinArguments<spec> name##ArgumentsType; |
| 88 | BUILTIN_LIST_C(DEF_ARG_TYPE) |
| 89 | #undef DEF_ARG_TYPE |
| 90 | |
| 91 | } // namespace |
| 92 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 93 | // ---------------------------------------------------------------------------- |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 94 | // Support macro for defining builtins in C++. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 95 | // ---------------------------------------------------------------------------- |
| 96 | // |
| 97 | // A builtin function is defined by writing: |
| 98 | // |
| 99 | // BUILTIN(name) { |
| 100 | // ... |
| 101 | // } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 102 | // |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 103 | // In the body of the builtin function the arguments can be accessed |
| 104 | // through the BuiltinArguments object args. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 105 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 106 | #ifdef DEBUG |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 107 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 108 | #define BUILTIN(name) \ |
| 109 | MUST_USE_RESULT static Object* Builtin_Impl_##name( \ |
| 110 | name##ArgumentsType args, Isolate* isolate); \ |
| 111 | MUST_USE_RESULT static Object* Builtin_##name( \ |
| 112 | int args_length, Object** args_object, Isolate* isolate) { \ |
| 113 | name##ArgumentsType args(args_length, args_object); \ |
| 114 | args.Verify(); \ |
| 115 | return Builtin_Impl_##name(args, isolate); \ |
| 116 | } \ |
| 117 | MUST_USE_RESULT static Object* Builtin_Impl_##name( \ |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 118 | name##ArgumentsType args, Isolate* isolate) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 119 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 120 | #else // For release mode. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 121 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 122 | #define BUILTIN(name) \ |
| 123 | static Object* Builtin_impl##name( \ |
| 124 | name##ArgumentsType args, Isolate* isolate); \ |
| 125 | static Object* Builtin_##name( \ |
| 126 | int args_length, Object** args_object, Isolate* isolate) { \ |
| 127 | name##ArgumentsType args(args_length, args_object); \ |
| 128 | return Builtin_impl##name(args, isolate); \ |
| 129 | } \ |
| 130 | static Object* Builtin_impl##name( \ |
| 131 | name##ArgumentsType args, Isolate* isolate) |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 132 | #endif |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 133 | |
| 134 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 135 | #ifdef DEBUG |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 136 | static inline bool CalledAsConstructor(Isolate* isolate) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 137 | // Calculate the result using a full stack frame iterator and check |
| 138 | // that the state of the stack is as we assume it to be in the |
| 139 | // code below. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 140 | StackFrameIterator it(isolate); |
| 141 | DCHECK(it.frame()->is_exit()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 142 | it.Advance(); |
| 143 | StackFrame* frame = it.frame(); |
| 144 | bool reference_result = frame->is_construct(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 145 | Address fp = Isolate::c_entry_fp(isolate->thread_local_top()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 146 | // Because we know fp points to an exit frame we can use the relevant |
| 147 | // part of ExitFrame::ComputeCallerState directly. |
| 148 | const int kCallerOffset = ExitFrameConstants::kCallerFPOffset; |
| 149 | Address caller_fp = Memory::Address_at(fp + kCallerOffset); |
| 150 | // This inlines the part of StackFrame::ComputeType that grabs the |
| 151 | // type of the current frame. Note that StackFrame::ComputeType |
| 152 | // has been specialized for each architecture so if any one of them |
| 153 | // changes this code has to be changed as well. |
| 154 | const int kMarkerOffset = StandardFrameConstants::kMarkerOffset; |
| 155 | const Smi* kConstructMarker = Smi::FromInt(StackFrame::CONSTRUCT); |
| 156 | Object* marker = Memory::Object_at(caller_fp + kMarkerOffset); |
| 157 | bool result = (marker == kConstructMarker); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 158 | DCHECK_EQ(result, reference_result); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 159 | return result; |
| 160 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 161 | #endif |
| 162 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 163 | |
| 164 | // ---------------------------------------------------------------------------- |
| 165 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 166 | BUILTIN(Illegal) { |
| 167 | UNREACHABLE(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 168 | return isolate->heap()->undefined_value(); // Make compiler happy. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 169 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 170 | |
| 171 | |
| 172 | BUILTIN(EmptyFunction) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 173 | return isolate->heap()->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 174 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 175 | |
| 176 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 177 | static void MoveDoubleElements(FixedDoubleArray* dst, int dst_index, |
| 178 | FixedDoubleArray* src, int src_index, int len) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 179 | if (len == 0) return; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 180 | MemMove(dst->data_start() + dst_index, src->data_start() + src_index, |
| 181 | len * kDoubleSize); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 185 | static bool ArrayPrototypeHasNoElements(Heap* heap, PrototypeIterator* iter) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 186 | DisallowHeapAllocation no_gc; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 187 | for (; !iter->IsAtEnd(); iter->Advance()) { |
| 188 | if (iter->GetCurrent()->IsJSProxy()) return false; |
| 189 | if (JSObject::cast(iter->GetCurrent())->elements() != |
| 190 | heap->empty_fixed_array()) { |
| 191 | return false; |
| 192 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 193 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 194 | return true; |
| 195 | } |
| 196 | |
| 197 | |
| 198 | static inline bool IsJSArrayFastElementMovingAllowed(Heap* heap, |
| 199 | JSArray* receiver) { |
| 200 | DisallowHeapAllocation no_gc; |
| 201 | PrototypeIterator iter(heap->isolate(), receiver); |
| 202 | return ArrayPrototypeHasNoElements(heap, &iter); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 206 | // Returns empty handle if not applicable. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 207 | MUST_USE_RESULT |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 208 | static inline MaybeHandle<FixedArrayBase> EnsureJSArrayWithWritableFastElements( |
| 209 | Isolate* isolate, |
| 210 | Handle<Object> receiver, |
| 211 | Arguments* args, |
| 212 | int first_added_arg) { |
| 213 | if (!receiver->IsJSArray()) return MaybeHandle<FixedArrayBase>(); |
| 214 | Handle<JSArray> array = Handle<JSArray>::cast(receiver); |
| 215 | // If there may be elements accessors in the prototype chain, the fast path |
| 216 | // cannot be used if there arguments to add to the array. |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 217 | Heap* heap = isolate->heap(); |
| 218 | if (args != NULL && !IsJSArrayFastElementMovingAllowed(heap, *array)) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 219 | return MaybeHandle<FixedArrayBase>(); |
| 220 | } |
| 221 | if (array->map()->is_observed()) return MaybeHandle<FixedArrayBase>(); |
| 222 | if (!array->map()->is_extensible()) return MaybeHandle<FixedArrayBase>(); |
| 223 | Handle<FixedArrayBase> elms(array->elements(), isolate); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 224 | Map* map = elms->map(); |
| 225 | if (map == heap->fixed_array_map()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 226 | if (args == NULL || array->HasFastObjectElements()) return elms; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 227 | } else if (map == heap->fixed_cow_array_map()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 228 | elms = JSObject::EnsureWritableFastElements(array); |
| 229 | if (args == NULL || array->HasFastObjectElements()) return elms; |
| 230 | } else if (map == heap->fixed_double_array_map()) { |
| 231 | if (args == NULL) return elms; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 232 | } else { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 233 | return MaybeHandle<FixedArrayBase>(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 234 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 235 | |
| 236 | // Need to ensure that the arguments passed in args can be contained in |
| 237 | // the array. |
| 238 | int args_length = args->length(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 239 | if (first_added_arg >= args_length) return handle(array->elements(), isolate); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 240 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 241 | ElementsKind origin_kind = array->map()->elements_kind(); |
| 242 | DCHECK(!IsFastObjectElementsKind(origin_kind)); |
| 243 | ElementsKind target_kind = origin_kind; |
| 244 | { |
| 245 | DisallowHeapAllocation no_gc; |
| 246 | int arg_count = args->length() - first_added_arg; |
| 247 | Object** arguments = args->arguments() - first_added_arg - (arg_count - 1); |
| 248 | for (int i = 0; i < arg_count; i++) { |
| 249 | Object* arg = arguments[i]; |
| 250 | if (arg->IsHeapObject()) { |
| 251 | if (arg->IsHeapNumber()) { |
| 252 | target_kind = FAST_DOUBLE_ELEMENTS; |
| 253 | } else { |
| 254 | target_kind = FAST_ELEMENTS; |
| 255 | break; |
| 256 | } |
| 257 | } |
| 258 | } |
| 259 | } |
| 260 | if (target_kind != origin_kind) { |
| 261 | JSObject::TransitionElementsKind(array, target_kind); |
| 262 | return handle(array->elements(), isolate); |
| 263 | } |
| 264 | return elms; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 268 | MUST_USE_RESULT static Object* CallJsBuiltin( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 269 | Isolate* isolate, |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 270 | const char* name, |
| 271 | BuiltinArguments<NO_EXTRA_ARGUMENTS> args) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 272 | HandleScope handleScope(isolate); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 273 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 274 | Handle<Object> js_builtin = Object::GetProperty( |
| 275 | isolate, |
| 276 | handle(isolate->native_context()->builtins(), isolate), |
| 277 | name).ToHandleChecked(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 278 | Handle<JSFunction> function = Handle<JSFunction>::cast(js_builtin); |
| 279 | int argc = args.length() - 1; |
| 280 | ScopedVector<Handle<Object> > argv(argc); |
| 281 | for (int i = 0; i < argc; ++i) { |
| 282 | argv[i] = args.at<Object>(i + 1); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 283 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 284 | Handle<Object> result; |
| 285 | ASSIGN_RETURN_FAILURE_ON_EXCEPTION( |
| 286 | isolate, result, |
| 287 | Execution::Call(isolate, |
| 288 | function, |
| 289 | args.receiver(), |
| 290 | argc, |
| 291 | argv.start())); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 292 | return *result; |
| 293 | } |
| 294 | |
| 295 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 296 | BUILTIN(ArrayPush) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 297 | HandleScope scope(isolate); |
| 298 | Handle<Object> receiver = args.receiver(); |
| 299 | MaybeHandle<FixedArrayBase> maybe_elms_obj = |
| 300 | EnsureJSArrayWithWritableFastElements(isolate, receiver, &args, 1); |
| 301 | Handle<FixedArrayBase> elms_obj; |
| 302 | if (!maybe_elms_obj.ToHandle(&elms_obj)) { |
| 303 | return CallJsBuiltin(isolate, "ArrayPush", args); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 304 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 305 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 306 | Handle<JSArray> array = Handle<JSArray>::cast(receiver); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 307 | int len = Smi::cast(array->length())->value(); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 308 | int to_add = args.length() - 1; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 309 | if (to_add > 0 && JSArray::WouldChangeReadOnlyLength(array, len + to_add)) { |
| 310 | return CallJsBuiltin(isolate, "ArrayPush", args); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 311 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 312 | DCHECK(!array->map()->is_observed()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 313 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 314 | ElementsKind kind = array->GetElementsKind(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 315 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 316 | if (IsFastSmiOrObjectElementsKind(kind)) { |
| 317 | Handle<FixedArray> elms = Handle<FixedArray>::cast(elms_obj); |
| 318 | if (to_add == 0) { |
| 319 | return Smi::FromInt(len); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 320 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 321 | // Currently fixed arrays cannot grow too big, so |
| 322 | // we should never hit this case. |
| 323 | DCHECK(to_add <= (Smi::kMaxValue - len)); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 324 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 325 | int new_length = len + to_add; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 326 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 327 | if (new_length > elms->length()) { |
| 328 | // New backing storage is needed. |
| 329 | int capacity = new_length + (new_length >> 1) + 16; |
| 330 | Handle<FixedArray> new_elms = |
| 331 | isolate->factory()->NewUninitializedFixedArray(capacity); |
| 332 | |
| 333 | ElementsAccessor* accessor = array->GetElementsAccessor(); |
| 334 | accessor->CopyElements( |
| 335 | elms_obj, 0, kind, new_elms, 0, |
| 336 | ElementsAccessor::kCopyToEndAndInitializeToHole); |
| 337 | |
| 338 | elms = new_elms; |
| 339 | } |
| 340 | |
| 341 | // Add the provided values. |
| 342 | DisallowHeapAllocation no_gc; |
| 343 | WriteBarrierMode mode = elms->GetWriteBarrierMode(no_gc); |
| 344 | for (int index = 0; index < to_add; index++) { |
| 345 | elms->set(index + len, args[index + 1], mode); |
| 346 | } |
| 347 | |
| 348 | if (*elms != array->elements()) { |
| 349 | array->set_elements(*elms); |
| 350 | } |
| 351 | |
| 352 | // Set the length. |
| 353 | array->set_length(Smi::FromInt(new_length)); |
| 354 | return Smi::FromInt(new_length); |
| 355 | } else { |
| 356 | int elms_len = elms_obj->length(); |
| 357 | if (to_add == 0) { |
| 358 | return Smi::FromInt(len); |
| 359 | } |
| 360 | // Currently fixed arrays cannot grow too big, so |
| 361 | // we should never hit this case. |
| 362 | DCHECK(to_add <= (Smi::kMaxValue - len)); |
| 363 | |
| 364 | int new_length = len + to_add; |
| 365 | |
| 366 | Handle<FixedDoubleArray> new_elms; |
| 367 | |
| 368 | if (new_length > elms_len) { |
| 369 | // New backing storage is needed. |
| 370 | int capacity = new_length + (new_length >> 1) + 16; |
| 371 | // Create new backing store; since capacity > 0, we can |
| 372 | // safely cast to FixedDoubleArray. |
| 373 | new_elms = Handle<FixedDoubleArray>::cast( |
| 374 | isolate->factory()->NewFixedDoubleArray(capacity)); |
| 375 | |
| 376 | ElementsAccessor* accessor = array->GetElementsAccessor(); |
| 377 | accessor->CopyElements( |
| 378 | elms_obj, 0, kind, new_elms, 0, |
| 379 | ElementsAccessor::kCopyToEndAndInitializeToHole); |
| 380 | |
| 381 | } else { |
| 382 | // to_add is > 0 and new_length <= elms_len, so elms_obj cannot be the |
| 383 | // empty_fixed_array. |
| 384 | new_elms = Handle<FixedDoubleArray>::cast(elms_obj); |
| 385 | } |
| 386 | |
| 387 | // Add the provided values. |
| 388 | DisallowHeapAllocation no_gc; |
| 389 | int index; |
| 390 | for (index = 0; index < to_add; index++) { |
| 391 | Object* arg = args[index + 1]; |
| 392 | new_elms->set(index + len, arg->Number()); |
| 393 | } |
| 394 | |
| 395 | if (*new_elms != array->elements()) { |
| 396 | array->set_elements(*new_elms); |
| 397 | } |
| 398 | |
| 399 | // Set the length. |
| 400 | array->set_length(Smi::FromInt(new_length)); |
| 401 | return Smi::FromInt(new_length); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 402 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 403 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 404 | |
| 405 | |
| 406 | BUILTIN(ArrayPop) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 407 | HandleScope scope(isolate); |
| 408 | Handle<Object> receiver = args.receiver(); |
| 409 | MaybeHandle<FixedArrayBase> maybe_elms_obj = |
| 410 | EnsureJSArrayWithWritableFastElements(isolate, receiver, NULL, 0); |
| 411 | Handle<FixedArrayBase> elms_obj; |
| 412 | if (!maybe_elms_obj.ToHandle(&elms_obj)) { |
| 413 | return CallJsBuiltin(isolate, "ArrayPop", args); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 414 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 415 | |
| 416 | Handle<JSArray> array = Handle<JSArray>::cast(receiver); |
| 417 | DCHECK(!array->map()->is_observed()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 418 | |
| 419 | int len = Smi::cast(array->length())->value(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 420 | if (len == 0) return isolate->heap()->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 421 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 422 | if (JSArray::HasReadOnlyLength(array)) { |
| 423 | return CallJsBuiltin(isolate, "ArrayPop", args); |
| 424 | } |
| 425 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 426 | ElementsAccessor* accessor = array->GetElementsAccessor(); |
| 427 | int new_length = len - 1; |
| 428 | Handle<Object> element = |
| 429 | accessor->Get(array, array, new_length, elms_obj).ToHandleChecked(); |
| 430 | if (element->IsTheHole()) { |
| 431 | return CallJsBuiltin(isolate, "ArrayPop", args); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 432 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 433 | RETURN_FAILURE_ON_EXCEPTION( |
| 434 | isolate, |
| 435 | accessor->SetLength(array, handle(Smi::FromInt(new_length), isolate))); |
| 436 | return *element; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 437 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 438 | |
| 439 | |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 440 | BUILTIN(ArrayShift) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 441 | HandleScope scope(isolate); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 442 | Heap* heap = isolate->heap(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 443 | Handle<Object> receiver = args.receiver(); |
| 444 | MaybeHandle<FixedArrayBase> maybe_elms_obj = |
| 445 | EnsureJSArrayWithWritableFastElements(isolate, receiver, NULL, 0); |
| 446 | Handle<FixedArrayBase> elms_obj; |
| 447 | if (!maybe_elms_obj.ToHandle(&elms_obj) || |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 448 | !IsJSArrayFastElementMovingAllowed(heap, JSArray::cast(*receiver))) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 449 | return CallJsBuiltin(isolate, "ArrayShift", args); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 450 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 451 | Handle<JSArray> array = Handle<JSArray>::cast(receiver); |
| 452 | DCHECK(!array->map()->is_observed()); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 453 | |
| 454 | int len = Smi::cast(array->length())->value(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 455 | if (len == 0) return heap->undefined_value(); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 456 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 457 | if (JSArray::HasReadOnlyLength(array)) { |
| 458 | return CallJsBuiltin(isolate, "ArrayShift", args); |
| 459 | } |
| 460 | |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 461 | // Get first element |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 462 | ElementsAccessor* accessor = array->GetElementsAccessor(); |
| 463 | Handle<Object> first = |
| 464 | accessor->Get(array, array, 0, elms_obj).ToHandleChecked(); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 465 | if (first->IsTheHole()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 466 | return CallJsBuiltin(isolate, "ArrayShift", args); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 467 | } |
| 468 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 469 | if (heap->CanMoveObjectStart(*elms_obj)) { |
| 470 | array->set_elements(heap->LeftTrimFixedArray(*elms_obj, 1)); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 471 | } else { |
| 472 | // Shift the elements. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 473 | if (elms_obj->IsFixedArray()) { |
| 474 | Handle<FixedArray> elms = Handle<FixedArray>::cast(elms_obj); |
| 475 | DisallowHeapAllocation no_gc; |
| 476 | heap->MoveElements(*elms, 0, 1, len - 1); |
| 477 | elms->set(len - 1, heap->the_hole_value()); |
| 478 | } else { |
| 479 | Handle<FixedDoubleArray> elms = Handle<FixedDoubleArray>::cast(elms_obj); |
| 480 | MoveDoubleElements(*elms, 0, *elms, 1, len - 1); |
| 481 | elms->set_the_hole(len - 1); |
| 482 | } |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 483 | } |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 484 | |
| 485 | // Set the length. |
| 486 | array->set_length(Smi::FromInt(len - 1)); |
| 487 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 488 | return *first; |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | |
| 492 | BUILTIN(ArrayUnshift) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 493 | HandleScope scope(isolate); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 494 | Heap* heap = isolate->heap(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 495 | Handle<Object> receiver = args.receiver(); |
| 496 | MaybeHandle<FixedArrayBase> maybe_elms_obj = |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 497 | EnsureJSArrayWithWritableFastElements(isolate, receiver, &args, 1); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 498 | Handle<FixedArrayBase> elms_obj; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 499 | if (!maybe_elms_obj.ToHandle(&elms_obj)) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 500 | return CallJsBuiltin(isolate, "ArrayUnshift", args); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 501 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 502 | Handle<JSArray> array = Handle<JSArray>::cast(receiver); |
| 503 | DCHECK(!array->map()->is_observed()); |
| 504 | if (!array->HasFastSmiOrObjectElements()) { |
| 505 | return CallJsBuiltin(isolate, "ArrayUnshift", args); |
| 506 | } |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 507 | int len = Smi::cast(array->length())->value(); |
| 508 | int to_add = args.length() - 1; |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 509 | int new_length = len + to_add; |
| 510 | // Currently fixed arrays cannot grow too big, so |
| 511 | // we should never hit this case. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 512 | DCHECK(to_add <= (Smi::kMaxValue - len)); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 513 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 514 | if (to_add > 0 && JSArray::WouldChangeReadOnlyLength(array, len + to_add)) { |
| 515 | return CallJsBuiltin(isolate, "ArrayUnshift", args); |
| 516 | } |
| 517 | |
| 518 | Handle<FixedArray> elms = Handle<FixedArray>::cast(elms_obj); |
| 519 | |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 520 | if (new_length > elms->length()) { |
| 521 | // New backing storage is needed. |
| 522 | int capacity = new_length + (new_length >> 1) + 16; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 523 | Handle<FixedArray> new_elms = |
| 524 | isolate->factory()->NewUninitializedFixedArray(capacity); |
| 525 | |
| 526 | ElementsKind kind = array->GetElementsKind(); |
| 527 | ElementsAccessor* accessor = array->GetElementsAccessor(); |
| 528 | accessor->CopyElements( |
| 529 | elms, 0, kind, new_elms, to_add, |
| 530 | ElementsAccessor::kCopyToEndAndInitializeToHole); |
| 531 | |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 532 | elms = new_elms; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 533 | array->set_elements(*elms); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 534 | } else { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 535 | DisallowHeapAllocation no_gc; |
| 536 | heap->MoveElements(*elms, to_add, 0, len); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | // Add the provided values. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 540 | DisallowHeapAllocation no_gc; |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 541 | WriteBarrierMode mode = elms->GetWriteBarrierMode(no_gc); |
| 542 | for (int i = 0; i < to_add; i++) { |
| 543 | elms->set(i, args[i + 1], mode); |
| 544 | } |
| 545 | |
| 546 | // Set the length. |
| 547 | array->set_length(Smi::FromInt(new_length)); |
| 548 | return Smi::FromInt(new_length); |
| 549 | } |
| 550 | |
| 551 | |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 552 | BUILTIN(ArraySlice) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 553 | HandleScope scope(isolate); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 554 | Heap* heap = isolate->heap(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 555 | Handle<Object> receiver = args.receiver(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 556 | int len = -1; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 557 | int relative_start = 0; |
| 558 | int relative_end = 0; |
| 559 | { |
| 560 | DisallowHeapAllocation no_gc; |
| 561 | if (receiver->IsJSArray()) { |
| 562 | JSArray* array = JSArray::cast(*receiver); |
| 563 | if (!IsJSArrayFastElementMovingAllowed(heap, array)) { |
| 564 | AllowHeapAllocation allow_allocation; |
| 565 | return CallJsBuiltin(isolate, "ArraySlice", args); |
| 566 | } |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 567 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 568 | if (!array->HasFastElements()) { |
| 569 | AllowHeapAllocation allow_allocation; |
| 570 | return CallJsBuiltin(isolate, "ArraySlice", args); |
| 571 | } |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 572 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 573 | len = Smi::cast(array->length())->value(); |
| 574 | } else { |
| 575 | // Array.slice(arguments, ...) is quite a common idiom (notably more |
| 576 | // than 50% of invocations in Web apps). Treat it in C++ as well. |
| 577 | Map* arguments_map = |
| 578 | isolate->context()->native_context()->sloppy_arguments_map(); |
| 579 | |
| 580 | bool is_arguments_object_with_fast_elements = |
| 581 | receiver->IsJSObject() && |
| 582 | JSObject::cast(*receiver)->map() == arguments_map; |
| 583 | if (!is_arguments_object_with_fast_elements) { |
| 584 | AllowHeapAllocation allow_allocation; |
| 585 | return CallJsBuiltin(isolate, "ArraySlice", args); |
| 586 | } |
| 587 | JSObject* object = JSObject::cast(*receiver); |
| 588 | |
| 589 | if (!object->HasFastElements()) { |
| 590 | AllowHeapAllocation allow_allocation; |
| 591 | return CallJsBuiltin(isolate, "ArraySlice", args); |
| 592 | } |
| 593 | |
| 594 | Object* len_obj = object->InObjectPropertyAt(Heap::kArgumentsLengthIndex); |
| 595 | if (!len_obj->IsSmi()) { |
| 596 | AllowHeapAllocation allow_allocation; |
| 597 | return CallJsBuiltin(isolate, "ArraySlice", args); |
| 598 | } |
| 599 | len = Smi::cast(len_obj)->value(); |
| 600 | if (len > object->elements()->length()) { |
| 601 | AllowHeapAllocation allow_allocation; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 602 | return CallJsBuiltin(isolate, "ArraySlice", args); |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 603 | } |
| 604 | } |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 605 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 606 | DCHECK(len >= 0); |
| 607 | int n_arguments = args.length() - 1; |
| 608 | |
| 609 | // Note carefully choosen defaults---if argument is missing, |
| 610 | // it's undefined which gets converted to 0 for relative_start |
| 611 | // and to len for relative_end. |
| 612 | relative_start = 0; |
| 613 | relative_end = len; |
| 614 | if (n_arguments > 0) { |
| 615 | Object* arg1 = args[1]; |
| 616 | if (arg1->IsSmi()) { |
| 617 | relative_start = Smi::cast(arg1)->value(); |
| 618 | } else if (arg1->IsHeapNumber()) { |
| 619 | double start = HeapNumber::cast(arg1)->value(); |
| 620 | if (start < kMinInt || start > kMaxInt) { |
| 621 | AllowHeapAllocation allow_allocation; |
| 622 | return CallJsBuiltin(isolate, "ArraySlice", args); |
| 623 | } |
| 624 | relative_start = std::isnan(start) ? 0 : static_cast<int>(start); |
| 625 | } else if (!arg1->IsUndefined()) { |
| 626 | AllowHeapAllocation allow_allocation; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 627 | return CallJsBuiltin(isolate, "ArraySlice", args); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 628 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 629 | if (n_arguments > 1) { |
| 630 | Object* arg2 = args[2]; |
| 631 | if (arg2->IsSmi()) { |
| 632 | relative_end = Smi::cast(arg2)->value(); |
| 633 | } else if (arg2->IsHeapNumber()) { |
| 634 | double end = HeapNumber::cast(arg2)->value(); |
| 635 | if (end < kMinInt || end > kMaxInt) { |
| 636 | AllowHeapAllocation allow_allocation; |
| 637 | return CallJsBuiltin(isolate, "ArraySlice", args); |
| 638 | } |
| 639 | relative_end = std::isnan(end) ? 0 : static_cast<int>(end); |
| 640 | } else if (!arg2->IsUndefined()) { |
| 641 | AllowHeapAllocation allow_allocation; |
| 642 | return CallJsBuiltin(isolate, "ArraySlice", args); |
| 643 | } |
| 644 | } |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 645 | } |
| 646 | } |
| 647 | |
| 648 | // ECMAScript 232, 3rd Edition, Section 15.4.4.10, step 6. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 649 | int k = (relative_start < 0) ? Max(len + relative_start, 0) |
| 650 | : Min(relative_start, len); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 651 | |
| 652 | // ECMAScript 232, 3rd Edition, Section 15.4.4.10, step 8. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 653 | int final = (relative_end < 0) ? Max(len + relative_end, 0) |
| 654 | : Min(relative_end, len); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 655 | |
Ben Murdoch | 5d4cdbf | 2012-04-11 10:23:59 +0100 | [diff] [blame] | 656 | // Calculate the length of result array. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 657 | int result_len = Max(final - k, 0); |
Ben Murdoch | 5d4cdbf | 2012-04-11 10:23:59 +0100 | [diff] [blame] | 658 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 659 | Handle<JSObject> object = Handle<JSObject>::cast(receiver); |
| 660 | Handle<FixedArrayBase> elms(object->elements(), isolate); |
Ben Murdoch | 5d4cdbf | 2012-04-11 10:23:59 +0100 | [diff] [blame] | 661 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 662 | ElementsKind kind = object->GetElementsKind(); |
| 663 | if (IsHoleyElementsKind(kind)) { |
| 664 | DisallowHeapAllocation no_gc; |
| 665 | bool packed = true; |
| 666 | ElementsAccessor* accessor = ElementsAccessor::ForKind(kind); |
| 667 | for (int i = k; i < final; i++) { |
| 668 | if (!accessor->HasElement(object, object, i, elms)) { |
| 669 | packed = false; |
| 670 | break; |
| 671 | } |
| 672 | } |
| 673 | if (packed) { |
| 674 | kind = GetPackedElementsKind(kind); |
| 675 | } else if (!receiver->IsJSArray()) { |
| 676 | AllowHeapAllocation allow_allocation; |
| 677 | return CallJsBuiltin(isolate, "ArraySlice", args); |
| 678 | } |
| 679 | } |
Ben Murdoch | 5d4cdbf | 2012-04-11 10:23:59 +0100 | [diff] [blame] | 680 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 681 | Handle<JSArray> result_array = |
| 682 | isolate->factory()->NewJSArray(kind, result_len, result_len); |
| 683 | |
| 684 | DisallowHeapAllocation no_gc; |
| 685 | if (result_len == 0) return *result_array; |
| 686 | |
| 687 | ElementsAccessor* accessor = object->GetElementsAccessor(); |
| 688 | accessor->CopyElements( |
| 689 | elms, k, kind, handle(result_array->elements(), isolate), 0, result_len); |
| 690 | return *result_array; |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | |
| 694 | BUILTIN(ArraySplice) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 695 | HandleScope scope(isolate); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 696 | Heap* heap = isolate->heap(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 697 | Handle<Object> receiver = args.receiver(); |
| 698 | MaybeHandle<FixedArrayBase> maybe_elms_obj = |
| 699 | EnsureJSArrayWithWritableFastElements(isolate, receiver, &args, 3); |
| 700 | Handle<FixedArrayBase> elms_obj; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 701 | if (!maybe_elms_obj.ToHandle(&elms_obj)) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 702 | return CallJsBuiltin(isolate, "ArraySplice", args); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 703 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 704 | Handle<JSArray> array = Handle<JSArray>::cast(receiver); |
| 705 | DCHECK(!array->map()->is_observed()); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 706 | |
| 707 | int len = Smi::cast(array->length())->value(); |
| 708 | |
| 709 | int n_arguments = args.length() - 1; |
| 710 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 711 | int relative_start = 0; |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 712 | if (n_arguments > 0) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 713 | DisallowHeapAllocation no_gc; |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 714 | Object* arg1 = args[1]; |
| 715 | if (arg1->IsSmi()) { |
| 716 | relative_start = Smi::cast(arg1)->value(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 717 | } else if (arg1->IsHeapNumber()) { |
| 718 | double start = HeapNumber::cast(arg1)->value(); |
| 719 | if (start < kMinInt || start > kMaxInt) { |
| 720 | AllowHeapAllocation allow_allocation; |
| 721 | return CallJsBuiltin(isolate, "ArraySplice", args); |
| 722 | } |
| 723 | relative_start = std::isnan(start) ? 0 : static_cast<int>(start); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 724 | } else if (!arg1->IsUndefined()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 725 | AllowHeapAllocation allow_allocation; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 726 | return CallJsBuiltin(isolate, "ArraySplice", args); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 727 | } |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 728 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 729 | int actual_start = (relative_start < 0) ? Max(len + relative_start, 0) |
| 730 | : Min(relative_start, len); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 731 | |
| 732 | // SpiderMonkey, TraceMonkey and JSC treat the case where no delete count is |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 733 | // given as a request to delete all the elements from the start. |
| 734 | // And it differs from the case of undefined delete count. |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 735 | // This does not follow ECMA-262, but we do the same for |
| 736 | // compatibility. |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 737 | int actual_delete_count; |
| 738 | if (n_arguments == 1) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 739 | DCHECK(len - actual_start >= 0); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 740 | actual_delete_count = len - actual_start; |
| 741 | } else { |
| 742 | int value = 0; // ToInteger(undefined) == 0 |
| 743 | if (n_arguments > 1) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 744 | DisallowHeapAllocation no_gc; |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 745 | Object* arg2 = args[2]; |
| 746 | if (arg2->IsSmi()) { |
| 747 | value = Smi::cast(arg2)->value(); |
| 748 | } else { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 749 | AllowHeapAllocation allow_allocation; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 750 | return CallJsBuiltin(isolate, "ArraySplice", args); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 751 | } |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 752 | } |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 753 | actual_delete_count = Min(Max(value, 0), len - actual_start); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 754 | } |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 755 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 756 | ElementsKind elements_kind = array->GetElementsKind(); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 757 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 758 | int item_count = (n_arguments > 1) ? (n_arguments - 2) : 0; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 759 | int new_length = len - actual_delete_count + item_count; |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 760 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 761 | // For double mode we do not support changing the length. |
| 762 | if (new_length > len && IsFastDoubleElementsKind(elements_kind)) { |
| 763 | return CallJsBuiltin(isolate, "ArraySplice", args); |
| 764 | } |
| 765 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 766 | if (new_length != len && JSArray::HasReadOnlyLength(array)) { |
| 767 | AllowHeapAllocation allow_allocation; |
| 768 | return CallJsBuiltin(isolate, "ArraySplice", args); |
| 769 | } |
| 770 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 771 | if (new_length == 0) { |
| 772 | Handle<JSArray> result = isolate->factory()->NewJSArrayWithElements( |
| 773 | elms_obj, elements_kind, actual_delete_count); |
| 774 | array->set_elements(heap->empty_fixed_array()); |
| 775 | array->set_length(Smi::FromInt(0)); |
| 776 | return *result; |
| 777 | } |
| 778 | |
| 779 | Handle<JSArray> result_array = |
| 780 | isolate->factory()->NewJSArray(elements_kind, |
| 781 | actual_delete_count, |
| 782 | actual_delete_count); |
| 783 | |
| 784 | if (actual_delete_count > 0) { |
| 785 | DisallowHeapAllocation no_gc; |
| 786 | ElementsAccessor* accessor = array->GetElementsAccessor(); |
| 787 | accessor->CopyElements( |
| 788 | elms_obj, actual_start, elements_kind, |
| 789 | handle(result_array->elements(), isolate), 0, actual_delete_count); |
| 790 | } |
| 791 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 792 | bool elms_changed = false; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 793 | if (item_count < actual_delete_count) { |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 794 | // Shrink the array. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 795 | const bool trim_array = !heap->lo_space()->Contains(*elms_obj) && |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 796 | ((actual_start + item_count) < |
| 797 | (len - actual_delete_count - actual_start)); |
| 798 | if (trim_array) { |
| 799 | const int delta = actual_delete_count - item_count; |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 800 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 801 | if (elms_obj->IsFixedDoubleArray()) { |
| 802 | Handle<FixedDoubleArray> elms = |
| 803 | Handle<FixedDoubleArray>::cast(elms_obj); |
| 804 | MoveDoubleElements(*elms, delta, *elms, 0, actual_start); |
| 805 | } else { |
| 806 | Handle<FixedArray> elms = Handle<FixedArray>::cast(elms_obj); |
| 807 | DisallowHeapAllocation no_gc; |
| 808 | heap->MoveElements(*elms, delta, 0, actual_start); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 809 | } |
| 810 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 811 | if (heap->CanMoveObjectStart(*elms_obj)) { |
| 812 | // On the fast path we move the start of the object in memory. |
| 813 | elms_obj = handle(heap->LeftTrimFixedArray(*elms_obj, delta)); |
| 814 | } else { |
| 815 | // This is the slow path. We are going to move the elements to the left |
| 816 | // by copying them. For trimmed values we store the hole. |
| 817 | if (elms_obj->IsFixedDoubleArray()) { |
| 818 | Handle<FixedDoubleArray> elms = |
| 819 | Handle<FixedDoubleArray>::cast(elms_obj); |
| 820 | MoveDoubleElements(*elms, 0, *elms, delta, len - delta); |
| 821 | elms->FillWithHoles(len - delta, len); |
| 822 | } else { |
| 823 | Handle<FixedArray> elms = Handle<FixedArray>::cast(elms_obj); |
| 824 | DisallowHeapAllocation no_gc; |
| 825 | heap->MoveElements(*elms, 0, delta, len - delta); |
| 826 | elms->FillWithHoles(len - delta, len); |
| 827 | } |
| 828 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 829 | elms_changed = true; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 830 | } else { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 831 | if (elms_obj->IsFixedDoubleArray()) { |
| 832 | Handle<FixedDoubleArray> elms = |
| 833 | Handle<FixedDoubleArray>::cast(elms_obj); |
| 834 | MoveDoubleElements(*elms, actual_start + item_count, |
| 835 | *elms, actual_start + actual_delete_count, |
| 836 | (len - actual_delete_count - actual_start)); |
| 837 | elms->FillWithHoles(new_length, len); |
| 838 | } else { |
| 839 | Handle<FixedArray> elms = Handle<FixedArray>::cast(elms_obj); |
| 840 | DisallowHeapAllocation no_gc; |
| 841 | heap->MoveElements(*elms, actual_start + item_count, |
| 842 | actual_start + actual_delete_count, |
| 843 | (len - actual_delete_count - actual_start)); |
| 844 | elms->FillWithHoles(new_length, len); |
| 845 | } |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 846 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 847 | } else if (item_count > actual_delete_count) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 848 | Handle<FixedArray> elms = Handle<FixedArray>::cast(elms_obj); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 849 | // Currently fixed arrays cannot grow too big, so |
| 850 | // we should never hit this case. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 851 | DCHECK((item_count - actual_delete_count) <= (Smi::kMaxValue - len)); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 852 | |
| 853 | // Check if array need to grow. |
| 854 | if (new_length > elms->length()) { |
| 855 | // New backing storage is needed. |
| 856 | int capacity = new_length + (new_length >> 1) + 16; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 857 | Handle<FixedArray> new_elms = |
| 858 | isolate->factory()->NewUninitializedFixedArray(capacity); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 859 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 860 | DisallowHeapAllocation no_gc; |
| 861 | |
| 862 | ElementsKind kind = array->GetElementsKind(); |
| 863 | ElementsAccessor* accessor = array->GetElementsAccessor(); |
| 864 | if (actual_start > 0) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 865 | // Copy the part before actual_start as is. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 866 | accessor->CopyElements( |
| 867 | elms, 0, kind, new_elms, 0, actual_start); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 868 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 869 | accessor->CopyElements( |
| 870 | elms, actual_start + actual_delete_count, kind, |
| 871 | new_elms, actual_start + item_count, |
| 872 | ElementsAccessor::kCopyToEndAndInitializeToHole); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 873 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 874 | elms_obj = new_elms; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 875 | elms_changed = true; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 876 | } else { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 877 | DisallowHeapAllocation no_gc; |
| 878 | heap->MoveElements(*elms, actual_start + item_count, |
| 879 | actual_start + actual_delete_count, |
| 880 | (len - actual_delete_count - actual_start)); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 881 | } |
| 882 | } |
| 883 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 884 | if (IsFastDoubleElementsKind(elements_kind)) { |
| 885 | Handle<FixedDoubleArray> elms = Handle<FixedDoubleArray>::cast(elms_obj); |
| 886 | for (int k = actual_start; k < actual_start + item_count; k++) { |
| 887 | Object* arg = args[3 + k - actual_start]; |
| 888 | if (arg->IsSmi()) { |
| 889 | elms->set(k, Smi::cast(arg)->value()); |
| 890 | } else { |
| 891 | elms->set(k, HeapNumber::cast(arg)->value()); |
| 892 | } |
| 893 | } |
| 894 | } else { |
| 895 | Handle<FixedArray> elms = Handle<FixedArray>::cast(elms_obj); |
| 896 | DisallowHeapAllocation no_gc; |
| 897 | WriteBarrierMode mode = elms->GetWriteBarrierMode(no_gc); |
| 898 | for (int k = actual_start; k < actual_start + item_count; k++) { |
| 899 | elms->set(k, args[3 + k - actual_start], mode); |
| 900 | } |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 901 | } |
| 902 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 903 | if (elms_changed) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 904 | array->set_elements(*elms_obj); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 905 | } |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 906 | // Set the length. |
| 907 | array->set_length(Smi::FromInt(new_length)); |
| 908 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 909 | return *result_array; |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 910 | } |
| 911 | |
| 912 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 913 | BUILTIN(ArrayConcat) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 914 | HandleScope scope(isolate); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 915 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 916 | int n_arguments = args.length(); |
| 917 | int result_len = 0; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 918 | ElementsKind elements_kind = GetInitialFastElementsKind(); |
| 919 | bool has_double = false; |
| 920 | { |
| 921 | DisallowHeapAllocation no_gc; |
| 922 | Heap* heap = isolate->heap(); |
| 923 | Context* native_context = isolate->context()->native_context(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 924 | Object* array_proto = native_context->array_function()->prototype(); |
| 925 | PrototypeIterator iter(isolate, array_proto, |
| 926 | PrototypeIterator::START_AT_RECEIVER); |
| 927 | if (!ArrayPrototypeHasNoElements(heap, &iter)) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 928 | AllowHeapAllocation allow_allocation; |
| 929 | return CallJsBuiltin(isolate, "ArrayConcatJS", args); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 930 | } |
| 931 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 932 | // Iterate through all the arguments performing checks |
| 933 | // and calculating total length. |
| 934 | bool is_holey = false; |
| 935 | for (int i = 0; i < n_arguments; i++) { |
| 936 | Object* arg = args[i]; |
| 937 | PrototypeIterator iter(isolate, arg); |
| 938 | if (!arg->IsJSArray() || !JSArray::cast(arg)->HasFastElements() || |
| 939 | iter.GetCurrent() != array_proto) { |
| 940 | AllowHeapAllocation allow_allocation; |
| 941 | return CallJsBuiltin(isolate, "ArrayConcatJS", args); |
| 942 | } |
| 943 | int len = Smi::cast(JSArray::cast(arg)->length())->value(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 944 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 945 | // We shouldn't overflow when adding another len. |
| 946 | const int kHalfOfMaxInt = 1 << (kBitsPerInt - 2); |
| 947 | STATIC_ASSERT(FixedArray::kMaxLength < kHalfOfMaxInt); |
| 948 | USE(kHalfOfMaxInt); |
| 949 | result_len += len; |
| 950 | DCHECK(result_len >= 0); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 951 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 952 | if (result_len > FixedDoubleArray::kMaxLength) { |
| 953 | AllowHeapAllocation allow_allocation; |
| 954 | return CallJsBuiltin(isolate, "ArrayConcatJS", args); |
| 955 | } |
| 956 | |
| 957 | ElementsKind arg_kind = JSArray::cast(arg)->map()->elements_kind(); |
| 958 | has_double = has_double || IsFastDoubleElementsKind(arg_kind); |
| 959 | is_holey = is_holey || IsFastHoleyElementsKind(arg_kind); |
| 960 | if (IsMoreGeneralElementsKindTransition(elements_kind, arg_kind)) { |
| 961 | elements_kind = arg_kind; |
| 962 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 963 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 964 | if (is_holey) elements_kind = GetHoleyElementsKind(elements_kind); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 965 | } |
| 966 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 967 | // If a double array is concatted into a fast elements array, the fast |
| 968 | // elements array needs to be initialized to contain proper holes, since |
| 969 | // boxing doubles may cause incremental marking. |
| 970 | ArrayStorageAllocationMode mode = |
| 971 | has_double && IsFastObjectElementsKind(elements_kind) |
| 972 | ? INITIALIZE_ARRAY_ELEMENTS_WITH_HOLE : DONT_INITIALIZE_ARRAY_ELEMENTS; |
| 973 | Handle<JSArray> result_array = |
| 974 | isolate->factory()->NewJSArray(elements_kind, |
| 975 | result_len, |
| 976 | result_len, |
| 977 | mode); |
| 978 | if (result_len == 0) return *result_array; |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 979 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 980 | int j = 0; |
| 981 | Handle<FixedArrayBase> storage(result_array->elements(), isolate); |
| 982 | ElementsAccessor* accessor = ElementsAccessor::ForKind(elements_kind); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 983 | for (int i = 0; i < n_arguments; i++) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 984 | // It is crucial to keep |array| in a raw pointer form to avoid performance |
| 985 | // degradation. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 986 | JSArray* array = JSArray::cast(args[i]); |
| 987 | int len = Smi::cast(array->length())->value(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 988 | if (len > 0) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 989 | ElementsKind from_kind = array->GetElementsKind(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 990 | accessor->CopyElements(array, 0, from_kind, storage, j, len); |
| 991 | j += len; |
| 992 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 993 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 994 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 995 | DCHECK(j == result_len); |
| 996 | |
| 997 | return *result_array; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 998 | } |
| 999 | |
| 1000 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1001 | // ----------------------------------------------------------------------------- |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1002 | // Generator and strict mode poison pills |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1003 | |
| 1004 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1005 | BUILTIN(StrictModePoisonPill) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1006 | HandleScope scope(isolate); |
| 1007 | THROW_NEW_ERROR_RETURN_FAILURE( |
| 1008 | isolate, |
| 1009 | NewTypeError("strict_poison_pill", HandleVector<Object>(NULL, 0))); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1010 | } |
| 1011 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1012 | |
| 1013 | BUILTIN(GeneratorPoisonPill) { |
| 1014 | HandleScope scope(isolate); |
| 1015 | THROW_NEW_ERROR_RETURN_FAILURE( |
| 1016 | isolate, |
| 1017 | NewTypeError("generator_poison_pill", HandleVector<Object>(NULL, 0))); |
| 1018 | } |
| 1019 | |
| 1020 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1021 | // ----------------------------------------------------------------------------- |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1022 | // |
| 1023 | |
| 1024 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1025 | // Searches the hidden prototype chain of the given object for the first |
| 1026 | // object that is an instance of the given type. If no such object can |
| 1027 | // be found then Heap::null_value() is returned. |
| 1028 | static inline Object* FindHidden(Heap* heap, |
| 1029 | Object* object, |
| 1030 | FunctionTemplateInfo* type) { |
| 1031 | for (PrototypeIterator iter(heap->isolate(), object, |
| 1032 | PrototypeIterator::START_AT_RECEIVER); |
| 1033 | !iter.IsAtEnd(PrototypeIterator::END_AT_NON_HIDDEN); iter.Advance()) { |
| 1034 | if (type->IsTemplateFor(iter.GetCurrent())) { |
| 1035 | return iter.GetCurrent(); |
| 1036 | } |
| 1037 | } |
| 1038 | return heap->null_value(); |
| 1039 | } |
| 1040 | |
| 1041 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1042 | // Returns the holder JSObject if the function can legally be called |
| 1043 | // with this receiver. Returns Heap::null_value() if the call is |
| 1044 | // illegal. Any arguments that don't fit the expected type is |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1045 | // overwritten with undefined. Note that holder and the arguments are |
| 1046 | // implicitly rewritten with the first object in the hidden prototype |
| 1047 | // chain that actually has the expected type. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1048 | static inline Object* TypeCheck(Heap* heap, |
| 1049 | int argc, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1050 | Object** argv, |
| 1051 | FunctionTemplateInfo* info) { |
| 1052 | Object* recv = argv[0]; |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1053 | // API calls are only supported with JSObject receivers. |
| 1054 | if (!recv->IsJSObject()) return heap->null_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1055 | Object* sig_obj = info->signature(); |
| 1056 | if (sig_obj->IsUndefined()) return recv; |
| 1057 | SignatureInfo* sig = SignatureInfo::cast(sig_obj); |
| 1058 | // If necessary, check the receiver |
| 1059 | Object* recv_type = sig->receiver(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1060 | Object* holder = recv; |
| 1061 | if (!recv_type->IsUndefined()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1062 | holder = FindHidden(heap, holder, FunctionTemplateInfo::cast(recv_type)); |
| 1063 | if (holder == heap->null_value()) return heap->null_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1064 | } |
| 1065 | Object* args_obj = sig->args(); |
| 1066 | // If there is no argument signature we're done |
| 1067 | if (args_obj->IsUndefined()) return holder; |
| 1068 | FixedArray* args = FixedArray::cast(args_obj); |
| 1069 | int length = args->length(); |
| 1070 | if (argc <= length) length = argc - 1; |
| 1071 | for (int i = 0; i < length; i++) { |
| 1072 | Object* argtype = args->get(i); |
| 1073 | if (argtype->IsUndefined()) continue; |
| 1074 | Object** arg = &argv[-1 - i]; |
| 1075 | Object* current = *arg; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1076 | current = FindHidden(heap, current, FunctionTemplateInfo::cast(argtype)); |
| 1077 | if (current == heap->null_value()) current = heap->undefined_value(); |
| 1078 | *arg = current; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1079 | } |
| 1080 | return holder; |
| 1081 | } |
| 1082 | |
| 1083 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1084 | template <bool is_construct> |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1085 | MUST_USE_RESULT static Object* HandleApiCallHelper( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1086 | BuiltinArguments<NEEDS_CALLED_FUNCTION> args, Isolate* isolate) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1087 | DCHECK(is_construct == CalledAsConstructor(isolate)); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1088 | Heap* heap = isolate->heap(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1089 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1090 | HandleScope scope(isolate); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1091 | Handle<JSFunction> function = args.called_function(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1092 | DCHECK(function->shared()->IsApiFunction()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1093 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1094 | Handle<FunctionTemplateInfo> fun_data( |
| 1095 | function->shared()->get_api_func_data(), isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1096 | if (is_construct) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1097 | ASSIGN_RETURN_FAILURE_ON_EXCEPTION( |
| 1098 | isolate, fun_data, |
| 1099 | isolate->factory()->ConfigureInstance( |
| 1100 | fun_data, Handle<JSObject>::cast(args.receiver()))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1101 | } |
| 1102 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1103 | SharedFunctionInfo* shared = function->shared(); |
| 1104 | if (shared->strict_mode() == SLOPPY && !shared->native()) { |
| 1105 | Object* recv = args[0]; |
| 1106 | DCHECK(!recv->IsNull()); |
| 1107 | if (recv->IsUndefined()) args[0] = function->global_proxy(); |
| 1108 | } |
| 1109 | |
| 1110 | Object* raw_holder = TypeCheck(heap, args.length(), &args[0], *fun_data); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1111 | |
| 1112 | if (raw_holder->IsNull()) { |
| 1113 | // This function cannot be called with the given receiver. Abort! |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1114 | THROW_NEW_ERROR_RETURN_FAILURE( |
| 1115 | isolate, |
| 1116 | NewTypeError("illegal_invocation", HandleVector(&function, 1))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1117 | } |
| 1118 | |
| 1119 | Object* raw_call_data = fun_data->call_code(); |
| 1120 | if (!raw_call_data->IsUndefined()) { |
| 1121 | CallHandlerInfo* call_data = CallHandlerInfo::cast(raw_call_data); |
| 1122 | Object* callback_obj = call_data->callback(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1123 | v8::FunctionCallback callback = |
| 1124 | v8::ToCData<v8::FunctionCallback>(callback_obj); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1125 | Object* data_obj = call_data->data(); |
| 1126 | Object* result; |
| 1127 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1128 | LOG(isolate, ApiObjectAccess("call", JSObject::cast(*args.receiver()))); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1129 | DCHECK(raw_holder->IsJSObject()); |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 1130 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1131 | FunctionCallbackArguments custom(isolate, |
| 1132 | data_obj, |
| 1133 | *function, |
| 1134 | raw_holder, |
| 1135 | &args[0] - 1, |
| 1136 | args.length() - 1, |
| 1137 | is_construct); |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 1138 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1139 | v8::Handle<v8::Value> value = custom.Call(callback); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1140 | if (value.IsEmpty()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1141 | result = heap->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1142 | } else { |
| 1143 | result = *reinterpret_cast<Object**>(*value); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1144 | result->VerifyApiCallResultType(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1145 | } |
| 1146 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1147 | RETURN_FAILURE_IF_SCHEDULED_EXCEPTION(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1148 | if (!is_construct || result->IsJSObject()) return result; |
| 1149 | } |
| 1150 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1151 | return *args.receiver(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1152 | } |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1153 | |
| 1154 | |
| 1155 | BUILTIN(HandleApiCall) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1156 | return HandleApiCallHelper<false>(args, isolate); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1157 | } |
| 1158 | |
| 1159 | |
| 1160 | BUILTIN(HandleApiCallConstruct) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1161 | return HandleApiCallHelper<true>(args, isolate); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1162 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1163 | |
| 1164 | |
| 1165 | // Helper function to handle calls to non-function objects created through the |
| 1166 | // API. The object can be called as either a constructor (using new) or just as |
| 1167 | // a function (without new). |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1168 | MUST_USE_RESULT static Object* HandleApiCallAsFunctionOrConstructor( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1169 | Isolate* isolate, |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1170 | bool is_construct_call, |
| 1171 | BuiltinArguments<NO_EXTRA_ARGUMENTS> args) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1172 | // Non-functions are never called as constructors. Even if this is an object |
| 1173 | // called as a constructor the delegate call is not a construct call. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1174 | DCHECK(!CalledAsConstructor(isolate)); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1175 | Heap* heap = isolate->heap(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1176 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1177 | Handle<Object> receiver = args.receiver(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1178 | |
| 1179 | // Get the object called. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1180 | JSObject* obj = JSObject::cast(*receiver); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1181 | |
| 1182 | // Get the invocation callback from the function descriptor that was |
| 1183 | // used to create the called object. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1184 | DCHECK(obj->map()->has_instance_call_handler()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1185 | JSFunction* constructor = JSFunction::cast(obj->map()->constructor()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1186 | DCHECK(constructor->shared()->IsApiFunction()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1187 | Object* handler = |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1188 | constructor->shared()->get_api_func_data()->instance_call_handler(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1189 | DCHECK(!handler->IsUndefined()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1190 | CallHandlerInfo* call_data = CallHandlerInfo::cast(handler); |
| 1191 | Object* callback_obj = call_data->callback(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1192 | v8::FunctionCallback callback = |
| 1193 | v8::ToCData<v8::FunctionCallback>(callback_obj); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1194 | |
| 1195 | // Get the data for the call and perform the callback. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1196 | Object* result; |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 1197 | { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1198 | HandleScope scope(isolate); |
| 1199 | LOG(isolate, ApiObjectAccess("call non-function", obj)); |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 1200 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1201 | FunctionCallbackArguments custom(isolate, |
| 1202 | call_data->data(), |
| 1203 | constructor, |
| 1204 | obj, |
| 1205 | &args[0] - 1, |
| 1206 | args.length() - 1, |
| 1207 | is_construct_call); |
| 1208 | v8::Handle<v8::Value> value = custom.Call(callback); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1209 | if (value.IsEmpty()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1210 | result = heap->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1211 | } else { |
| 1212 | result = *reinterpret_cast<Object**>(*value); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1213 | result->VerifyApiCallResultType(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1214 | } |
| 1215 | } |
| 1216 | // Check for exceptions and return result. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1217 | RETURN_FAILURE_IF_SCHEDULED_EXCEPTION(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1218 | return result; |
| 1219 | } |
| 1220 | |
| 1221 | |
| 1222 | // Handle calls to non-function objects created through the API. This delegate |
| 1223 | // function is used when the call is a normal function call. |
| 1224 | BUILTIN(HandleApiCallAsFunction) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1225 | return HandleApiCallAsFunctionOrConstructor(isolate, false, args); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1226 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1227 | |
| 1228 | |
| 1229 | // Handle calls to non-function objects created through the API. This delegate |
| 1230 | // function is used when the call is a construct call. |
| 1231 | BUILTIN(HandleApiCallAsConstructor) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1232 | return HandleApiCallAsFunctionOrConstructor(isolate, true, args); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1233 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1234 | |
| 1235 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1236 | static void Generate_LoadIC_Miss(MacroAssembler* masm) { |
| 1237 | LoadIC::GenerateMiss(masm); |
| 1238 | } |
| 1239 | |
| 1240 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1241 | static void Generate_LoadIC_Normal(MacroAssembler* masm) { |
| 1242 | LoadIC::GenerateNormal(masm); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1243 | } |
| 1244 | |
| 1245 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1246 | static void Generate_LoadIC_Getter_ForDeopt(MacroAssembler* masm) { |
| 1247 | NamedLoadHandlerCompiler::GenerateLoadViaGetterForDeopt(masm); |
| 1248 | } |
| 1249 | |
| 1250 | |
| 1251 | static void Generate_LoadIC_Slow(MacroAssembler* masm) { |
| 1252 | LoadIC::GenerateRuntimeGetProperty(masm); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1253 | } |
| 1254 | |
| 1255 | |
| 1256 | static void Generate_KeyedLoadIC_Initialize(MacroAssembler* masm) { |
| 1257 | KeyedLoadIC::GenerateInitialize(masm); |
| 1258 | } |
| 1259 | |
| 1260 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1261 | static void Generate_KeyedLoadIC_Slow(MacroAssembler* masm) { |
| 1262 | KeyedLoadIC::GenerateRuntimeGetProperty(masm); |
| 1263 | } |
| 1264 | |
| 1265 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1266 | static void Generate_KeyedLoadIC_Miss(MacroAssembler* masm) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1267 | KeyedLoadIC::GenerateMiss(masm); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1268 | } |
| 1269 | |
| 1270 | |
| 1271 | static void Generate_KeyedLoadIC_Generic(MacroAssembler* masm) { |
| 1272 | KeyedLoadIC::GenerateGeneric(masm); |
| 1273 | } |
| 1274 | |
| 1275 | |
| 1276 | static void Generate_KeyedLoadIC_PreMonomorphic(MacroAssembler* masm) { |
| 1277 | KeyedLoadIC::GeneratePreMonomorphic(masm); |
| 1278 | } |
| 1279 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 1280 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1281 | static void Generate_StoreIC_Miss(MacroAssembler* masm) { |
| 1282 | StoreIC::GenerateMiss(masm); |
| 1283 | } |
| 1284 | |
| 1285 | |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1286 | static void Generate_StoreIC_Normal(MacroAssembler* masm) { |
| 1287 | StoreIC::GenerateNormal(masm); |
| 1288 | } |
| 1289 | |
| 1290 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1291 | static void Generate_StoreIC_Slow(MacroAssembler* masm) { |
| 1292 | NamedStoreHandlerCompiler::GenerateSlow(masm); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1293 | } |
| 1294 | |
| 1295 | |
| 1296 | static void Generate_KeyedStoreIC_Slow(MacroAssembler* masm) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1297 | ElementHandlerCompiler::GenerateStoreSlow(masm); |
| 1298 | } |
| 1299 | |
| 1300 | |
| 1301 | static void Generate_StoreIC_Setter_ForDeopt(MacroAssembler* masm) { |
| 1302 | NamedStoreHandlerCompiler::GenerateStoreViaSetterForDeopt(masm); |
| 1303 | } |
| 1304 | |
| 1305 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 1306 | static void Generate_KeyedStoreIC_Megamorphic(MacroAssembler* masm) { |
| 1307 | KeyedStoreIC::GenerateMegamorphic(masm, SLOPPY); |
| 1308 | } |
| 1309 | |
| 1310 | |
| 1311 | static void Generate_KeyedStoreIC_Megamorphic_Strict(MacroAssembler* masm) { |
| 1312 | KeyedStoreIC::GenerateMegamorphic(masm, STRICT); |
| 1313 | } |
| 1314 | |
| 1315 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1316 | static void Generate_KeyedStoreIC_Generic(MacroAssembler* masm) { |
| 1317 | KeyedStoreIC::GenerateGeneric(masm, SLOPPY); |
| 1318 | } |
| 1319 | |
| 1320 | |
| 1321 | static void Generate_KeyedStoreIC_Generic_Strict(MacroAssembler* masm) { |
| 1322 | KeyedStoreIC::GenerateGeneric(masm, STRICT); |
| 1323 | } |
| 1324 | |
| 1325 | |
| 1326 | static void Generate_KeyedStoreIC_Miss(MacroAssembler* masm) { |
| 1327 | KeyedStoreIC::GenerateMiss(masm); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1328 | } |
| 1329 | |
| 1330 | |
| 1331 | static void Generate_KeyedStoreIC_Initialize(MacroAssembler* masm) { |
| 1332 | KeyedStoreIC::GenerateInitialize(masm); |
| 1333 | } |
| 1334 | |
| 1335 | |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 1336 | static void Generate_KeyedStoreIC_Initialize_Strict(MacroAssembler* masm) { |
| 1337 | KeyedStoreIC::GenerateInitialize(masm); |
| 1338 | } |
| 1339 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1340 | |
| 1341 | static void Generate_KeyedStoreIC_PreMonomorphic(MacroAssembler* masm) { |
| 1342 | KeyedStoreIC::GeneratePreMonomorphic(masm); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1343 | } |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 1344 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1345 | |
| 1346 | static void Generate_KeyedStoreIC_PreMonomorphic_Strict(MacroAssembler* masm) { |
| 1347 | KeyedStoreIC::GeneratePreMonomorphic(masm); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1348 | } |
| 1349 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1350 | |
| 1351 | static void Generate_KeyedStoreIC_SloppyArguments(MacroAssembler* masm) { |
| 1352 | KeyedStoreIC::GenerateSloppyArguments(masm); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1353 | } |
| 1354 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1355 | |
| 1356 | static void Generate_CallICStub_DebugBreak(MacroAssembler* masm) { |
| 1357 | DebugCodegen::GenerateCallICStubDebugBreak(masm); |
| 1358 | } |
| 1359 | |
| 1360 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1361 | static void Generate_LoadIC_DebugBreak(MacroAssembler* masm) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1362 | DebugCodegen::GenerateLoadICDebugBreak(masm); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1363 | } |
| 1364 | |
| 1365 | |
| 1366 | static void Generate_StoreIC_DebugBreak(MacroAssembler* masm) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1367 | DebugCodegen::GenerateStoreICDebugBreak(masm); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1368 | } |
| 1369 | |
| 1370 | |
| 1371 | static void Generate_KeyedLoadIC_DebugBreak(MacroAssembler* masm) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1372 | DebugCodegen::GenerateKeyedLoadICDebugBreak(masm); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1373 | } |
| 1374 | |
| 1375 | |
| 1376 | static void Generate_KeyedStoreIC_DebugBreak(MacroAssembler* masm) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1377 | DebugCodegen::GenerateKeyedStoreICDebugBreak(masm); |
| 1378 | } |
| 1379 | |
| 1380 | |
| 1381 | static void Generate_CompareNilIC_DebugBreak(MacroAssembler* masm) { |
| 1382 | DebugCodegen::GenerateCompareNilICDebugBreak(masm); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1383 | } |
| 1384 | |
| 1385 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1386 | static void Generate_Return_DebugBreak(MacroAssembler* masm) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1387 | DebugCodegen::GenerateReturnDebugBreak(masm); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1388 | } |
| 1389 | |
| 1390 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1391 | static void Generate_CallFunctionStub_DebugBreak(MacroAssembler* masm) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1392 | DebugCodegen::GenerateCallFunctionStubDebugBreak(masm); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1393 | } |
| 1394 | |
| 1395 | |
| 1396 | static void Generate_CallConstructStub_DebugBreak(MacroAssembler* masm) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1397 | DebugCodegen::GenerateCallConstructStubDebugBreak(masm); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1398 | } |
| 1399 | |
| 1400 | |
| 1401 | static void Generate_CallConstructStub_Recording_DebugBreak( |
| 1402 | MacroAssembler* masm) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1403 | DebugCodegen::GenerateCallConstructStubRecordDebugBreak(masm); |
Ben Murdoch | 5d4cdbf | 2012-04-11 10:23:59 +0100 | [diff] [blame] | 1404 | } |
| 1405 | |
| 1406 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1407 | static void Generate_Slot_DebugBreak(MacroAssembler* masm) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1408 | DebugCodegen::GenerateSlotDebugBreak(masm); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1409 | } |
| 1410 | |
| 1411 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1412 | static void Generate_PlainReturn_LiveEdit(MacroAssembler* masm) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1413 | DebugCodegen::GeneratePlainReturnLiveEdit(masm); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1414 | } |
| 1415 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1416 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1417 | static void Generate_FrameDropper_LiveEdit(MacroAssembler* masm) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1418 | DebugCodegen::GenerateFrameDropperLiveEdit(masm); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1419 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1420 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1421 | |
| 1422 | Builtins::Builtins() : initialized_(false) { |
| 1423 | memset(builtins_, 0, sizeof(builtins_[0]) * builtin_count); |
| 1424 | memset(names_, 0, sizeof(names_[0]) * builtin_count); |
| 1425 | } |
| 1426 | |
| 1427 | |
| 1428 | Builtins::~Builtins() { |
| 1429 | } |
| 1430 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1431 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1432 | #define DEF_ENUM_C(name, ignore) FUNCTION_ADDR(Builtin_##name), |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1433 | Address const Builtins::c_functions_[cfunction_count] = { |
| 1434 | BUILTIN_LIST_C(DEF_ENUM_C) |
| 1435 | }; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1436 | #undef DEF_ENUM_C |
| 1437 | |
| 1438 | #define DEF_JS_NAME(name, ignore) #name, |
| 1439 | #define DEF_JS_ARGC(ignore, argc) argc, |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1440 | const char* const Builtins::javascript_names_[id_count] = { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1441 | BUILTINS_LIST_JS(DEF_JS_NAME) |
| 1442 | }; |
| 1443 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1444 | int const Builtins::javascript_argc_[id_count] = { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1445 | BUILTINS_LIST_JS(DEF_JS_ARGC) |
| 1446 | }; |
| 1447 | #undef DEF_JS_NAME |
| 1448 | #undef DEF_JS_ARGC |
| 1449 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1450 | struct BuiltinDesc { |
| 1451 | byte* generator; |
| 1452 | byte* c_code; |
| 1453 | const char* s_name; // name is only used for generating log information. |
| 1454 | int name; |
| 1455 | Code::Flags flags; |
| 1456 | BuiltinExtraArguments extra_args; |
| 1457 | }; |
| 1458 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1459 | #define BUILTIN_FUNCTION_TABLE_INIT { V8_ONCE_INIT, {} } |
| 1460 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1461 | class BuiltinFunctionTable { |
| 1462 | public: |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1463 | BuiltinDesc* functions() { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1464 | base::CallOnce(&once_, &Builtins::InitBuiltinFunctionTable); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1465 | return functions_; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1466 | } |
| 1467 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1468 | base::OnceType once_; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1469 | BuiltinDesc functions_[Builtins::builtin_count + 1]; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1470 | |
| 1471 | friend class Builtins; |
| 1472 | }; |
| 1473 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1474 | static BuiltinFunctionTable builtin_function_table = |
| 1475 | BUILTIN_FUNCTION_TABLE_INIT; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1476 | |
| 1477 | // Define array of pointers to generators and C builtin functions. |
| 1478 | // We do this in a sort of roundabout way so that we can do the initialization |
| 1479 | // within the lexical scope of Builtins:: and within a context where |
| 1480 | // Code::Flags names a non-abstract type. |
| 1481 | void Builtins::InitBuiltinFunctionTable() { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1482 | BuiltinDesc* functions = builtin_function_table.functions_; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1483 | functions[builtin_count].generator = NULL; |
| 1484 | functions[builtin_count].c_code = NULL; |
| 1485 | functions[builtin_count].s_name = NULL; |
| 1486 | functions[builtin_count].name = builtin_count; |
| 1487 | functions[builtin_count].flags = static_cast<Code::Flags>(0); |
| 1488 | functions[builtin_count].extra_args = NO_EXTRA_ARGUMENTS; |
| 1489 | |
| 1490 | #define DEF_FUNCTION_PTR_C(aname, aextra_args) \ |
| 1491 | functions->generator = FUNCTION_ADDR(Generate_Adaptor); \ |
| 1492 | functions->c_code = FUNCTION_ADDR(Builtin_##aname); \ |
| 1493 | functions->s_name = #aname; \ |
| 1494 | functions->name = c_##aname; \ |
| 1495 | functions->flags = Code::ComputeFlags(Code::BUILTIN); \ |
| 1496 | functions->extra_args = aextra_args; \ |
| 1497 | ++functions; |
| 1498 | |
| 1499 | #define DEF_FUNCTION_PTR_A(aname, kind, state, extra) \ |
| 1500 | functions->generator = FUNCTION_ADDR(Generate_##aname); \ |
| 1501 | functions->c_code = NULL; \ |
| 1502 | functions->s_name = #aname; \ |
| 1503 | functions->name = k##aname; \ |
| 1504 | functions->flags = Code::ComputeFlags(Code::kind, \ |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1505 | state, \ |
| 1506 | extra); \ |
| 1507 | functions->extra_args = NO_EXTRA_ARGUMENTS; \ |
| 1508 | ++functions; |
| 1509 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1510 | #define DEF_FUNCTION_PTR_H(aname, kind) \ |
| 1511 | functions->generator = FUNCTION_ADDR(Generate_##aname); \ |
| 1512 | functions->c_code = NULL; \ |
| 1513 | functions->s_name = #aname; \ |
| 1514 | functions->name = k##aname; \ |
| 1515 | functions->flags = Code::ComputeHandlerFlags(Code::kind); \ |
| 1516 | functions->extra_args = NO_EXTRA_ARGUMENTS; \ |
| 1517 | ++functions; |
| 1518 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1519 | BUILTIN_LIST_C(DEF_FUNCTION_PTR_C) |
| 1520 | BUILTIN_LIST_A(DEF_FUNCTION_PTR_A) |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1521 | BUILTIN_LIST_H(DEF_FUNCTION_PTR_H) |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1522 | BUILTIN_LIST_DEBUG_A(DEF_FUNCTION_PTR_A) |
| 1523 | |
| 1524 | #undef DEF_FUNCTION_PTR_C |
| 1525 | #undef DEF_FUNCTION_PTR_A |
| 1526 | } |
| 1527 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1528 | |
| 1529 | void Builtins::SetUp(Isolate* isolate, bool create_heap_objects) { |
| 1530 | DCHECK(!initialized_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1531 | |
| 1532 | // Create a scope for the handles in the builtins. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1533 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1534 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1535 | const BuiltinDesc* functions = builtin_function_table.functions(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1536 | |
| 1537 | // For now we generate builtin adaptor code into a stack-allocated |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1538 | // buffer, before copying it into individual code objects. Be careful |
| 1539 | // with alignment, some platforms don't like unaligned code. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1540 | #ifdef DEBUG |
| 1541 | // We can generate a lot of debug code on Arm64. |
| 1542 | const size_t buffer_size = 32*KB; |
| 1543 | #else |
| 1544 | const size_t buffer_size = 8*KB; |
| 1545 | #endif |
| 1546 | union { int force_alignment; byte buffer[buffer_size]; } u; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1547 | |
| 1548 | // Traverse the list of builtins and generate an adaptor in a |
| 1549 | // separate code object for each one. |
| 1550 | for (int i = 0; i < builtin_count; i++) { |
| 1551 | if (create_heap_objects) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1552 | MacroAssembler masm(isolate, u.buffer, sizeof u.buffer); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1553 | // Generate the code/adaptor. |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1554 | typedef void (*Generator)(MacroAssembler*, int, BuiltinExtraArguments); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1555 | Generator g = FUNCTION_CAST<Generator>(functions[i].generator); |
| 1556 | // We pass all arguments to the generator, but it may not use all of |
| 1557 | // them. This works because the first arguments are on top of the |
| 1558 | // stack. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1559 | DCHECK(!masm.has_frame()); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1560 | g(&masm, functions[i].name, functions[i].extra_args); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1561 | // Move the code into the object heap. |
| 1562 | CodeDesc desc; |
| 1563 | masm.GetCode(&desc); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 1564 | Code::Flags flags = functions[i].flags; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1565 | Handle<Code> code = |
| 1566 | isolate->factory()->NewCode(desc, flags, masm.CodeObject()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1567 | // Log the event and add the code to the builtins array. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1568 | PROFILE(isolate, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1569 | CodeCreateEvent(Logger::BUILTIN_TAG, *code, functions[i].s_name)); |
| 1570 | builtins_[i] = *code; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 1571 | code->set_builtin_index(i); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1572 | #ifdef ENABLE_DISASSEMBLER |
| 1573 | if (FLAG_print_builtin_code) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1574 | CodeTracer::Scope trace_scope(isolate->GetCodeTracer()); |
| 1575 | OFStream os(trace_scope.file()); |
| 1576 | os << "Builtin: " << functions[i].s_name << "\n"; |
| 1577 | code->Disassemble(functions[i].s_name, os); |
| 1578 | os << "\n"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1579 | } |
| 1580 | #endif |
| 1581 | } else { |
| 1582 | // Deserializing. The values will be filled in during IterateBuiltins. |
| 1583 | builtins_[i] = NULL; |
| 1584 | } |
| 1585 | names_[i] = functions[i].s_name; |
| 1586 | } |
| 1587 | |
| 1588 | // Mark as initialized. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1589 | initialized_ = true; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1590 | } |
| 1591 | |
| 1592 | |
| 1593 | void Builtins::TearDown() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1594 | initialized_ = false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1595 | } |
| 1596 | |
| 1597 | |
| 1598 | void Builtins::IterateBuiltins(ObjectVisitor* v) { |
| 1599 | v->VisitPointers(&builtins_[0], &builtins_[0] + builtin_count); |
| 1600 | } |
| 1601 | |
| 1602 | |
| 1603 | const char* Builtins::Lookup(byte* pc) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1604 | // may be called during initialization (disassembler!) |
| 1605 | if (initialized_) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1606 | for (int i = 0; i < builtin_count; i++) { |
| 1607 | Code* entry = Code::cast(builtins_[i]); |
| 1608 | if (entry->contains(pc)) { |
| 1609 | return names_[i]; |
| 1610 | } |
| 1611 | } |
| 1612 | } |
| 1613 | return NULL; |
| 1614 | } |
| 1615 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1616 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1617 | void Builtins::Generate_InterruptCheck(MacroAssembler* masm) { |
| 1618 | masm->TailCallRuntime(Runtime::kInterrupt, 0, 1); |
| 1619 | } |
| 1620 | |
| 1621 | |
| 1622 | void Builtins::Generate_StackCheck(MacroAssembler* masm) { |
| 1623 | masm->TailCallRuntime(Runtime::kStackGuard, 0, 1); |
| 1624 | } |
| 1625 | |
| 1626 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1627 | #define DEFINE_BUILTIN_ACCESSOR_C(name, ignore) \ |
| 1628 | Handle<Code> Builtins::name() { \ |
| 1629 | Code** code_address = \ |
| 1630 | reinterpret_cast<Code**>(builtin_address(k##name)); \ |
| 1631 | return Handle<Code>(code_address); \ |
| 1632 | } |
| 1633 | #define DEFINE_BUILTIN_ACCESSOR_A(name, kind, state, extra) \ |
| 1634 | Handle<Code> Builtins::name() { \ |
| 1635 | Code** code_address = \ |
| 1636 | reinterpret_cast<Code**>(builtin_address(k##name)); \ |
| 1637 | return Handle<Code>(code_address); \ |
| 1638 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1639 | #define DEFINE_BUILTIN_ACCESSOR_H(name, kind) \ |
| 1640 | Handle<Code> Builtins::name() { \ |
| 1641 | Code** code_address = \ |
| 1642 | reinterpret_cast<Code**>(builtin_address(k##name)); \ |
| 1643 | return Handle<Code>(code_address); \ |
| 1644 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1645 | BUILTIN_LIST_C(DEFINE_BUILTIN_ACCESSOR_C) |
| 1646 | BUILTIN_LIST_A(DEFINE_BUILTIN_ACCESSOR_A) |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1647 | BUILTIN_LIST_H(DEFINE_BUILTIN_ACCESSOR_H) |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1648 | BUILTIN_LIST_DEBUG_A(DEFINE_BUILTIN_ACCESSOR_A) |
| 1649 | #undef DEFINE_BUILTIN_ACCESSOR_C |
| 1650 | #undef DEFINE_BUILTIN_ACCESSOR_A |
| 1651 | |
| 1652 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1653 | } } // namespace v8::internal |