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

Code before:
} /* ecma_builtin_json_serialize_array */

/**
 * Abstract operation 'SerializeJSONProperty' defined in 24.3.2.1
 *
 * @return ecma value
 *         Returned value must be freed with ecma_free_value.
 */
static ecma_value_t
ecma_builtin_json_serialize_property (ecma_json_stringify_context_t *context_p, /**< context*/
                                      ecma_object_t *holder_p, /**< the object*/
                                      ecma_string_t *key_p) /**< property key*/
{
  /* 1. */
  ecma_value_t value = ecma_op_object_get (holder_p, key_p);

  /* 2. */
  if (ECMA_IS_VALUE_ERROR (value))
  {
    return value;
  }

  /* 3. */
  if (ecma_is_value_object (value) || ecma_is_value_bigint (value))
  {
    ecma_value_t temp_val = ecma_op_to_object (value);

    if (ECMA_IS_VALUE_ERROR (temp_val))
    {
      ecma_free_value (value);
      return temp_val;
    }

    ecma_object_t *value_obj_p = ecma_get_object_from_value (temp_val);
    ecma_value_t to_json = ecma_op_object_get_with_receiver (value_obj_p,
                                                             ecma_get_magic_string (LIT_MAGIC_STRING_TO_JSON_UL),
                                                             value);

    ecma_deref_object (value_obj_p);
    if (ECMA_IS_VALUE_ERROR (to_json))
    {
      return to_json;
    }

    /* 3.c */
    if (ecma_op_is_callable (to_json))
    {
      ecma_value_t key_value = ecma_make_string_value (key_p);
      ecma_value_t call_args[] = { key_value };
      ecma_object_t *to_json_obj_p = ecma_get_object_from_value (to_json);


Review comment (suggestion):
I don't like the name of `temp_val`. What about `to_object_value` ? And it would be good to avoid this conversion for objects (which is a gain for ES5.1)

Code after:
} /* ecma_builtin_json_serialize_array */

/**
 * Abstract operation 'SerializeJSONProperty' defined in 24.3.2.1
 *
 * @return ecma value
 *         Returned value must be freed with ecma_free_value.
 */
static ecma_value_t
ecma_builtin_json_serialize_property (ecma_json_stringify_context_t *context_p, /**< context*/
                                      ecma_object_t *holder_p, /**< the object*/
                                      ecma_string_t *key_p) /**< property key*/
{
  /* 1. */
  ecma_value_t value = ecma_op_object_get (holder_p, key_p);

  /* 2. */
  if (ECMA_IS_VALUE_ERROR (value))
  {
    return value;
  }

  /* 3. */
  if (ecma_is_value_object (value) || ecma_is_value_bigint (value))
  {
    ecma_value_t to_object_value = ecma_op_to_object (value);

    if (ECMA_IS_VALUE_ERROR (to_object_value))
    {
      ecma_free_value (value);
      return to_object_value;
    }

    ecma_object_t *value_obj_p = ecma_get_object_from_value (to_object_value);
    ecma_value_t to_json = ecma_op_object_get_with_receiver (value_obj_p,
                                                             ecma_get_magic_string (LIT_MAGIC_STRING_TO_JSON_UL),
                                                             value);

    ecma_deref_object (value_obj_p);

    if (ECMA_IS_VALUE_ERROR (to_json))
    {
      return to_json;
    }

    /* 3.c */
    if (ecma_op_is_callable (to_json))
    {
      ecma_value_t key_value = ecma_make_string_value (key_p);
      ecma_value_t call_args[] = { key_value };
      ecma_object_t *to_json_obj_p = ecma_get_object_from_value (to_json);