-
Notifications
You must be signed in to change notification settings - Fork 684
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
Conversation
* limitations under the License. | ||
*/ | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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.
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. |
a13243c
to
2a15bf7
Compare
There was a problem hiding this 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 */ |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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)) |
There was a problem hiding this comment.
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 */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ditto.
93ae976
to
5e0ddfa
Compare
@@ -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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wrong indentation
There was a problem hiding this comment.
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) */ |
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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++) |
There was a problem hiding this comment.
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)) |
There was a problem hiding this comment.
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)) |
There was a problem hiding this comment.
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) | ||
|
There was a problem hiding this comment.
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)) |
There was a problem hiding this comment.
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 ||
.
b5a7ee7
to
ec73aab
Compare
|
||
if (ECMA_IS_VALUE_ERROR (index_value)) | ||
{ | ||
return index_value; |
There was a problem hiding this comment.
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 */ | ||
|
||
|
||
/** |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One newline above.
b6eebe5
to
7950d02
Compare
|
||
/* 3. */ | ||
uint32_t length = ecma_number_to_uint32 (length_number); | ||
|
There was a problem hiding this comment.
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); | ||
|
There was a problem hiding this comment.
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 */ |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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*
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
True.
There was a problem hiding this 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 */ |
There was a problem hiding this comment.
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*
There was a problem hiding this 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 */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
True.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
JerryScript-DCO-1.0-Signed-off-by: Bela Toth [email protected]
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 addedtypedArray-tostring.js
andtypedArray-join.js
with Asserts to test the features.JerryScript-DCO-1.0-Signed-off-by: Bela Toth [email protected]