From 89732db69e7cfce67bdd097bf9ccca5799ecf62f Mon Sep 17 00:00:00 2001 From: "ishell@chromium.org" Date: Mon, 24 Mar 2014 08:48:36 +0000 Subject: [PATCH] Builtin helper function EnsureJSArrayWithWritableFastElements() handlified. R=yangguo@chromium.org Review URL: https://codereview.chromium.org/208033002 git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@20180 ce2b1a6d-e550-0410-aec6-3dcde31c8c00 --- src/builtins.cc | 90 +++++++++++++++++++++------------------------------------ 1 file changed, 33 insertions(+), 57 deletions(-) diff --git a/src/builtins.cc b/src/builtins.cc index 8f30260..b73ed6b 100644 --- a/src/builtins.cc +++ b/src/builtins.cc @@ -300,33 +300,35 @@ static bool ArrayPrototypeHasNoElements(Heap* heap, } +// Returns empty handle if not applicable. MUST_USE_RESULT -static inline MaybeObject* EnsureJSArrayWithWritableFastElements( - Heap* heap, Object* receiver, Arguments* args, int first_added_arg) { - if (!receiver->IsJSArray()) return NULL; - JSArray* array = JSArray::cast(receiver); - if (array->map()->is_observed()) return NULL; - if (!array->map()->is_extensible()) return NULL; - HeapObject* elms = array->elements(); +static inline Handle EnsureJSArrayWithWritableFastElements( + Isolate* isolate, + Handle receiver, + Arguments* args, + int first_added_arg) { + if (!receiver->IsJSArray()) return Handle::null(); + Handle array = Handle::cast(receiver); + if (array->map()->is_observed()) return Handle::null(); + if (!array->map()->is_extensible()) return Handle::null(); + Handle elms(array->elements()); + Heap* heap = isolate->heap(); Map* map = elms->map(); if (map == heap->fixed_array_map()) { if (args == NULL || array->HasFastObjectElements()) return elms; } else if (map == heap->fixed_cow_array_map()) { - MaybeObject* maybe_writable_result = array->EnsureWritableFastElements(); - if (args == NULL || array->HasFastObjectElements() || - !maybe_writable_result->To(&elms)) { - return maybe_writable_result; - } + elms = JSObject::EnsureWritableFastElements(array); + if (args == NULL || array->HasFastObjectElements()) return elms; } else if (map == heap->fixed_double_array_map()) { if (args == NULL) return elms; } else { - return NULL; + return Handle::null(); } // Need to ensure that the arguments passed in args can be contained in // the array. int args_length = args->length(); - if (first_added_arg >= args_length) return array->elements(); + if (first_added_arg >= args_length) return handle(array->elements()); ElementsKind origin_kind = array->map()->elements_kind(); ASSERT(!IsFastObjectElementsKind(origin_kind)); @@ -345,28 +347,13 @@ static inline MaybeObject* EnsureJSArrayWithWritableFastElements( } } if (target_kind != origin_kind) { - MaybeObject* maybe_failure = array->TransitionElementsKind(target_kind); - if (maybe_failure->IsFailure()) return maybe_failure; - return array->elements(); + JSObject::TransitionElementsKind(array, target_kind); + return handle(array->elements()); } return elms; } -// TODO(ishell): Temporary wrapper until handlified. -MUST_USE_RESULT -static inline Handle EnsureJSArrayWithWritableFastElementsWrapper( - Isolate* isolate, - Handle receiver, - Arguments* args, - int first_added_arg) { - CALL_HEAP_FUNCTION(isolate, - EnsureJSArrayWithWritableFastElements( - isolate->heap(), *receiver, args, first_added_arg), - Object); -} - - // TODO(ishell): Handlify when all Array* builtins are handlified. static inline bool IsJSArrayFastElementMovingAllowed(Heap* heap, JSArray* receiver) { @@ -409,12 +396,10 @@ MUST_USE_RESULT static MaybeObject* CallJsBuiltin( BUILTIN(ArrayPush) { HandleScope scope(isolate); Handle receiver = args.receiver(); - Handle elms_or_null = - EnsureJSArrayWithWritableFastElementsWrapper(isolate, receiver, &args, 1); - RETURN_IF_EMPTY_HANDLE(isolate, elms_or_null); - if (*elms_or_null == NULL) return CallJsBuiltin(isolate, "ArrayPush", args); + Handle elms_obj = + EnsureJSArrayWithWritableFastElements(isolate, receiver, &args, 1); + if (elms_obj.is_null()) return CallJsBuiltin(isolate, "ArrayPush", args); - Handle elms_obj = Handle::cast(elms_or_null); Handle array = Handle::cast(receiver); ASSERT(!array->map()->is_observed()); @@ -544,12 +529,10 @@ static Handle ElementsAccessorGetWrapper( BUILTIN(ArrayPop) { HandleScope scope(isolate); Handle receiver = args.receiver(); - Handle elms_or_null = - EnsureJSArrayWithWritableFastElementsWrapper(isolate, receiver, NULL, 0); - RETURN_IF_EMPTY_HANDLE(isolate, elms_or_null); - if (*elms_or_null == NULL) return CallJsBuiltin(isolate, "ArrayPop", args); + Handle elms_obj = + EnsureJSArrayWithWritableFastElements(isolate, receiver, NULL, 0); + if (elms_obj.is_null()) return CallJsBuiltin(isolate, "ArrayPop", args); - Handle elms_obj = Handle::cast(elms_or_null); Handle array = Handle::cast(receiver); ASSERT(!array->map()->is_observed()); @@ -579,15 +562,13 @@ BUILTIN(ArrayShift) { HandleScope scope(isolate); Heap* heap = isolate->heap(); Handle receiver = args.receiver(); - Handle elms_or_null = - EnsureJSArrayWithWritableFastElementsWrapper(isolate, receiver, NULL, 0); - RETURN_IF_EMPTY_HANDLE(isolate, elms_or_null); - if ((*elms_or_null == NULL) || + Handle elms_obj = + EnsureJSArrayWithWritableFastElements(isolate, receiver, NULL, 0); + if (elms_obj.is_null() || !IsJSArrayFastElementMovingAllowed(heap, *Handle::cast(receiver))) { return CallJsBuiltin(isolate, "ArrayShift", args); } - Handle elms_obj = Handle::cast(elms_or_null); Handle array = Handle::cast(receiver); ASSERT(!array->map()->is_observed()); @@ -628,15 +609,13 @@ BUILTIN(ArrayUnshift) { HandleScope scope(isolate); Heap* heap = isolate->heap(); Handle receiver = args.receiver(); - Handle elms_or_null = - EnsureJSArrayWithWritableFastElementsWrapper(isolate, receiver, NULL, 0); - RETURN_IF_EMPTY_HANDLE(isolate, elms_or_null); - if ((*elms_or_null == NULL) || + Handle elms_obj = + EnsureJSArrayWithWritableFastElements(isolate, receiver, NULL, 0); + if (elms_obj.is_null() || !IsJSArrayFastElementMovingAllowed(heap, *Handle::cast(receiver))) { return CallJsBuiltin(isolate, "ArrayUnshift", args); } - Handle elms_obj = Handle::cast(elms_or_null); Handle array = Handle::cast(receiver); ASSERT(!array->map()->is_observed()); if (!array->HasFastSmiOrObjectElements()) { @@ -823,16 +802,13 @@ BUILTIN(ArraySplice) { HandleScope scope(isolate); Heap* heap = isolate->heap(); Handle receiver = args.receiver(); - Handle elms_or_null = - EnsureJSArrayWithWritableFastElementsWrapper(isolate, receiver, &args, 3); - RETURN_IF_EMPTY_HANDLE(isolate, elms_or_null); - - if ((*elms_or_null == NULL) || + Handle elms_obj = + EnsureJSArrayWithWritableFastElements(isolate, receiver, &args, 3); + if (elms_obj.is_null() || !IsJSArrayFastElementMovingAllowed(heap, *Handle::cast(receiver))) { return CallJsBuiltin(isolate, "ArraySplice", args); } - Handle elms_obj = Handle::cast(elms_or_null); Handle array = Handle::cast(receiver); ASSERT(!array->map()->is_observed()); -- 2.7.4