Repository: jerryscript-project/jerryscript
File: jerry-core/ecma/builtin-objects/ecma-builtin-helpers-sort.c (C)

Code before:
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "ecma-builtin-helpers.h"
#include "ecma-globals.h"

/**
 * Function used to merge two arrays for merge sort.
 * Arrays are stored as below:
 * First  -> array_a_p [left_idx : right_idx - 1]
 * Second -> array_a_p [right_idx : end_idx - 1]
 * Output -> array_b_p
 *
 * @return ecma value
 *         Returned value must be freed with ecma_free_value.
 */
static ecma_value_t
ecma_builtin_helper_array_merge_sort_bottom_up (ecma_value_t *array_a_p, /**< arrays to merge */
                                                uint32_t left_idx, /**< first array begin */
                                                uint32_t right_idx, /**< first array end */
                                                uint32_t end_idx, /**< second array end */
                                                ecma_value_t *array_b_p, /**< output array */
                                                ecma_value_t compare_func, /**< compare function */
                                                const ecma_builtin_helper_sort_compare_fn_t sort_cb) /**< sorting cb */
{
  ecma_value_t ret_value = ECMA_VALUE_EMPTY;
  uint32_t i = left_idx, j = right_idx;

  for (uint32_t k = left_idx; k < end_idx; k++)
  {
    ecma_value_t compare_value = ecma_make_number_value (ECMA_NUMBER_ZERO);

    if (j < end_idx)
    {
      compare_value = sort_cb (array_a_p[i], array_a_p[j], compare_func);
      if (ECMA_IS_VALUE_ERROR (compare_value))
      {
        ret_value = ECMA_VALUE_ERROR;
        break;
      }
    }

    if (i < right_idx && ecma_get_number_from_value (compare_value) <= ECMA_NUMBER_ZERO)

Review comment (suggestion):
IMO it would be better to use more descriptive names for the arrays, `array_a_p` could be `source_array_p`, and `array_b_p` could be `dest_array_p`, or `output_array_p`.

Code after:
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "ecma-builtin-helpers.h"
#include "ecma-globals.h"

/**
 * Function used to merge two arrays for merge sort.
 * Arrays are stored as below:
 * First  -> source_array_p [left_idx : right_idx - 1]
 * Second -> source_array_p [right_idx : end_idx - 1]
 * Output -> output_array_p
 *
 * @return ecma value
 *         Returned value must be freed with ecma_free_value.
 */
static ecma_value_t
ecma_builtin_helper_array_merge_sort_bottom_up (ecma_value_t *source_array_p, /**< arrays to merge */
                                                uint32_t left_idx, /**< first array begin */
                                                uint32_t right_idx, /**< first array end */
                                                uint32_t end_idx, /**< second array end */
                                                ecma_value_t *output_array_p, /**< output array */
                                                ecma_value_t compare_func, /**< compare function */
                                                const ecma_builtin_helper_sort_compare_fn_t sort_cb) /**< sorting cb */
{
  ecma_value_t ret_value = ECMA_VALUE_EMPTY;
  uint32_t i = left_idx, j = right_idx;

  for (uint32_t k = left_idx; k < end_idx; k++)
  {
    ecma_value_t compare_value = ecma_make_number_value (ECMA_NUMBER_ZERO);

    if (i < right_idx && j < end_idx)
    {
      compare_value = sort_cb (source_array_p[i], source_array_p[j], compare_func);
      if (ECMA_IS_VALUE_ERROR (compare_value))
      {
        ret_value = ECMA_VALUE_ERROR;
        break;
      }
    }

    if (i < right_idx && ecma_get_number_from_value (compare_value) <= ECMA_NUMBER_ZERO)