Skip to content

Implement toString and join for TypedArrays. #2255

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 19, 2018

Conversation

Achie72
Copy link
Member

@Achie72 Achie72 commented Mar 27, 2018

Fixed the issue that caused typedarray's toString() to output [object type], where type is the called typedarray's type, because there was no implementation for toString, and join, and default object toString was called. Now it prints out elements just as normal arrays, and as expected. Also added typedArray-tostring.js and typedArray-join.js with Asserts to test the features.

JerryScript-DCO-1.0-Signed-off-by: Bela Toth [email protected]

* limitations under the License.
*/
*
* Licensed under the Apache License, Version 2.0 (the "License");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do not change the license header

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be just a copy-paste issue, since i cannot find any difference (Compared it to ecma-builtin-boolean.c to avoid maybe same issue in another modified file).

/* Routine properties:
* (property name, C routine name, arguments number or NON_FIXED, value of the routine's length property) */
ROUTINE (LIT_MAGIC_STRING_TO_STRING_UL, ecma_builtin_float32array_prototype_object_to_string, 0, 0)
ROUTINE (LIT_MAGIC_STRING_JOIN, ecma_builtin_float32array_prototype_join, 1, 1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These ROUTINES should not be defined per typedarray version, but simply in the typedarray-prototype file. Please see the ecma_builtin_typedarray_prototype_every implementation on how this can be done. By putting the implementation in the correct place we can reduce the code duplications.

@galpeter
Copy link
Contributor

Additional extra note: this PR does not fix the toString method (well it does but it's akward) as there was no toString implementation before. Thus this PR implements the correct toString behaviour for the TypedArrays. Also not just the toString but the join method is also implemented here which should be mentioned in the commit message.

@Achie72 Achie72 changed the title Fix TypedArray toString method for all types. Implement toString and join for TypedArrays. Mar 28, 2018
@Achie72 Achie72 force-pushed the toString branch 2 times, most recently from a13243c to 2a15bf7 Compare March 29, 2018 16:19
Copy link
Member

@zherczeg zherczeg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are a lot of indentation issues with this patch, please fix all of them.

*/
static ecma_value_t
ecma_op_typedarray_get_to_string_at_index (ecma_object_t *obj_p, /**< this object */
uint32_t index) /**< array index */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add some spaces before uint32_t.


ECMA_TRY_CATCH (index_value,
ecma_op_object_get (obj_p, index_string_p),
ret_value);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also wrong indentation.

ret_value);

if (ecma_is_value_undefined (index_value)
|| ecma_is_value_null (index_value))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto.

*/
static ecma_value_t
ecma_builtin_typedarray_prototype_join (const ecma_value_t this_arg, /**< this argument */
const ecma_value_t separator_arg) /**< separator argument */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto.

@Achie72 Achie72 force-pushed the toString branch 2 times, most recently from 93ae976 to 5e0ddfa Compare April 4, 2018 08:43
@@ -110,7 +110,8 @@ LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_EXEC, "exec")
#if !defined (CONFIG_DISABLE_ES2015_TYPEDARRAY_BUILTIN)
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_FROM, "from")
#endif
#if !defined (CONFIG_DISABLE_ARRAY_BUILTIN)
#if !defined (CONFIG_DISABLE_ARRAY_BUILTIN) \
|| !defined (CONFIG_DISABLE_ES2015_TYPEDARRAY_BUILTIN)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wrong indentation

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That code was generated by gen-magic-strings.py.

@@ -45,6 +45,10 @@ ACCESSOR_READ_ONLY (LIT_MAGIC_STRING_LENGTH,
ecma_builtin_typedarray_prototype_length_getter,
ECMA_PROPERTY_FIXED)

/* Routine properties:
* (property name, C routine name, arguments number or NON_FIXED, value of the routine's length property) */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wrong indentation, missing space before *

ecma_value_t ret_value = ECMA_VALUE_EMPTY;
ecma_string_t *index_string_p = ecma_new_ecma_string_from_uint32 (index);

ECMA_TRY_CATCH (index_value,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We've started the elimination of ECMA_TRY_CATCH, so it would be better to avoid the usage of it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll look up the whole file for it then, is there a doc or something about the usage of the other method?

Copy link
Contributor

@LaszloLango LaszloLango Apr 6, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no documentation, but it easy to avoid the usage of it. Check the PR of @galpeter #2246. You must check the returned ecma_value_t with ECMA_IS_VALUE_ERROR and return if it is an error. Don't forget to free everything before you return. E.g:

static ecma_value_t
ecma_op_typedarray_get_to_string_at_index (ecma_object_t *obj_p, /**< this object */
                                           uint32_t index) /**< array index */
{
  ecma_string_t *index_string_p = ecma_new_ecma_string_from_uint32 (index);
  ecma_value_t index_value = ecma_op_object_get (obj_p, index_string_p);
  ecma_deref_ecma_string (index_string_p);

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

  ecma_value_t ret_val = ECMA_VALUE_EMPTY;
  if (ecma_is_value_undefined (index_value)
      || ecma_is_value_null (index_value))
  {
    ret_val = ecma_make_magic_string_value (LIT_MAGIC_STRING__EMPTY);
  }
  else
  {
    ret_val = ecma_op_to_string (index_value);
  }

  ecma_free_value (index_value);
  return ret_val;
} /* ecma_op_typedarray_get_to_string_at_index */

ecma_ref_ecma_string (return_string_p);

/* 9-10. */
for (uint32_t k = 1; ecma_is_value_empty (ret_value) && (k < length); k++)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This loop does not change ret_value thereby the function call ecma_is_value_empty (ret_value) in every iteration is unnecessary. This loop can be merged with the if statement below.

{
return join_value;
}
if (!ecma_op_is_callable (join_value))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add a newline before the if


/* 4-5. */
ecma_value_t separator_value = ecma_op_typedarray_get_separator_string (separator_arg);
if( ECMA_IS_VALUE_ERROR (separator_value))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the space should be before the (

return separator_value;
}
if (length == 0)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the empty line should be before the if.

}

if (ecma_is_value_undefined (index_value)
|| ecma_is_value_null (index_value))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a space is needed before the ||.

@Achie72 Achie72 force-pushed the toString branch 2 times, most recently from b5a7ee7 to ec73aab Compare April 10, 2018 11:32

if (ECMA_IS_VALUE_ERROR (index_value))
{
return index_value;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

index_string_p must be freed.

} /* ecma_builtin_typedarray_prototype_join */


/**
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One newline above.

@Achie72 Achie72 force-pushed the toString branch 2 times, most recently from b6eebe5 to 7950d02 Compare April 13, 2018 08:16

/* 3. */
uint32_t length = ecma_number_to_uint32 (length_number);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

length_number won't be freed in any cases. ECMA_OP_TO_NUMBER_FINALIZE (length_number); should be moved up. e.g.:

uint32_t length = 0;
ECMA_OP_TO_NUMBER_TRY_CATCH (length_number,
                             length_value,
                             ret_value);
/* 3. */
length = ecma_number_to_uint32 (length_number);
ECMA_OP_TO_NUMBER_FINALIZE (length_number);

if (ECMA_IS_VALUE_ERROR (ret_value)
{
  ...

ecma_value_t ret_value = ECMA_VALUE_EMPTY;
ecma_string_t *index_string_p = ecma_new_ecma_string_from_uint32 (index);
ecma_value_t index_value = ecma_op_object_get (obj_p, index_string_p);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

index_string_p can be freed here, so you can replace the two ecma_deref_ecma_string (index_string_p); calls by one line.

JerryScript-DCO-1.0-Signed-off-by: Bela Toth [email protected]
* Returned value must be freed with ecma_free_value.
*/
static ecma_value_t
ecma_op_typedarray_get_to_string_at_index (ecma_object_t *obj_p, /**< this object */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't we return with ecma_string_t*? It seems concatenation uses it anyway.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zherczeg there is a return case where the index_value "errored" and that is probably not a ecma_string_t*

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True.

Copy link
Contributor

@galpeter galpeter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm.

* Returned value must be freed with ecma_free_value.
*/
static ecma_value_t
ecma_op_typedarray_get_to_string_at_index (ecma_object_t *obj_p, /**< this object */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zherczeg there is a return case where the index_value "errored" and that is probably not a ecma_string_t*

Copy link
Member

@zherczeg zherczeg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

* Returned value must be freed with ecma_free_value.
*/
static ecma_value_t
ecma_op_typedarray_get_to_string_at_index (ecma_object_t *obj_p, /**< this object */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True.

Copy link
Contributor

@LaszloLango LaszloLango left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@LaszloLango LaszloLango merged commit 3df6ef3 into jerryscript-project:master Apr 19, 2018
jimmy-huang pushed a commit to jimmy-huang/jerryscript that referenced this pull request May 8, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants