Skip to content

[ES2015][TypedArray] Add other 8 types #1532

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
Jan 24, 2017

Conversation

jiangzidong
Copy link
Contributor

add Uint8Array, Int16Array Uint16Array, Int32Array, Uint32Array, Float32Array Float64Array Uint8ClampedArray

Support the conversion between any pairs of those types.

JerryScript-DCO-1.0-Signed-off-by: Zidong Jiang [email protected]

@LaszloLango LaszloLango added ecma builtins Related to ECMA built-in routines feature request Requested feature labels Jan 18, 2017
@@ -83,7 +83,7 @@ ecma_builtin_helper_object_to_string (const ecma_value_t this_arg) /**< this arg
/* Building string "[object #type#]" where type is 'Undefined',
'Null' or one of possible object's classes.
The string with null character is maximum 19 characters long. */
const lit_utf8_size_t buffer_size = 19;
const lit_utf8_size_t buffer_size = 27;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

because now the longest class name is Uint8ClampedArray, and 19 is not enough

Copy link
Contributor

Choose a reason for hiding this comment

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

Please also update the comment.

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.

Looks good in general. More tests would be great (push, pop, manipulating array elements, etc.)


return val;
} /* ecma_builtin_uint32array_dispatch_construct */

Copy link
Contributor

Choose a reason for hiding this comment

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

Missing end of doxygen groups comment:

/**
  * @}
  * @}
  * @}
  */


return val;
} /* ecma_builtin_uint8array_dispatch_construct */

Copy link
Contributor

Choose a reason for hiding this comment

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

ditto


return val;
} /* ecma_builtin_uint8clampedarray_dispatch_construct */

Copy link
Contributor

Choose a reason for hiding this comment

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

ditto

assert(a[0] === 2);
assert(a[1] === 2);
assert(a[2] === 0);
assert(a[3] === 255);
Copy link
Contributor

Choose a reason for hiding this comment

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

Missing new line at the end of file.

@jiangzidong
Copy link
Contributor Author

@LaszloLango Thanks for the comments, About the test, this patch mainly focus on the conversion of different types, and we didn't implement the method such as "forEach, fill, indexOf, ..." yet. I will add more tests about the conversion, and will implement other methods in the following patch.

@jiangzidong
Copy link
Contributor Author

updated

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.

Great patch!

The string with null character is maximum 19 characters long. */
const lit_utf8_size_t buffer_size = 19;
The string with null character is maximum 27 characters long. */
const lit_utf8_size_t buffer_size = 27;
JMEM_DEFINE_LOCAL_ARRAY (str_buffer, buffer_size, lit_utf8_byte_t);
Copy link
Member

Choose a reason for hiding this comment

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

Perhaps we should change this to:

lit_utf8_byte_t str_buffer[buffer_size];

I don't think malloc is needed here.

}
case LIT_MAGIC_STRING_FLOAT64_ARRAY_UL:
{
*((double *) dst_buf) = (double) tmp;
Copy link
Member

Choose a reason for hiding this comment

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

Actually double support is not required for jerry. Perhaps we should make it optional.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I see,
what about the following rule?
"only if we define both typedarray_builtin and CONFIG_ECMA_NUMBER_FLOAT64, we will implement Float64Array"

{
case LIT_MAGIC_STRING_INT8_ARRAY_UL:
{
GET_VALUE_FROM_ARRAYBUFFER (int8_t);
Copy link
Member

Choose a reason for hiding this comment

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

For me these macros are hard to read, since they return something but no return value.

tmp = GET_VALUE_FROM_ARRAYBUFFER (int8_t, src_buf);

This would look better.

Copy link
Contributor Author

@jiangzidong jiangzidong Jan 20, 2017

Choose a reason for hiding this comment

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

It seems like the function-like macro have return value. And because we have var definition inside the macro, we should use gcc' statement exprs instead of c99's comma exprs. It may damage the portability.

so I choose to use GET_VALUE_FROM_ARRAYBUFFER (int8_t, src_buf, tmp);

}
else
{
tmp_int = (int64_t) tmp;
Copy link
Member

Choose a reason for hiding this comment

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

Do we need int64 casting here? Not exactly a cheap operation on 32 bit systems.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In this case, yes, we should remove that because in the 'else' block, the tmp's range is 0 - 255.

}
case LIT_MAGIC_STRING_FLOAT64_ARRAY_UL:
{
*((double *) target_p) = (double) value_num;
Copy link
Member

Choose a reason for hiding this comment

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

This seems a lot of code duplication, perhaps we should combine them. Code size matters :)

@zherczeg
Copy link
Member

jerry-core/ecma/operations/ecma-typedarray-object.c:304: style(variableScope): The scope of the variable 'tmp' can be reduced.
This is the fail

@LaszloLango
Copy link
Contributor

@jiangzidong please fix the doxygen related errors. Run doxygen and check the related warnings. There are some issues with your group names.

* \addtogroup float32array prototype ECMA Float32Array.prototype object built-in
must be
* \addtogroup float32arrayprototype ECMA Float32Array.prototype object built-in
and * \addtogroup string ECMA Float32Array object built-in
must be
* \addtogroup float32array ECMA Float32Array object built-in
etc.

@jiangzidong
Copy link
Contributor Author

@LaszloLango updated. sorry that i didn't notice the doxygen part.

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.

Apart from some style issues, the patch is good for me. Please fix them before landing.

true,
true,
float32array)
#if CONFIG_ECMA_NUMBER_TYPE == CONFIG_ECMA_NUMBER_FLOAT64
Copy link
Member

Choose a reason for hiding this comment

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

Put a newline before this # if

lit_magic_string_id_t class_id) /**< class name of the typedarray */
{

#define SET_ELEMENT(type, location, number) \
Copy link
Member

Choose a reason for hiding this comment

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

No nned a newline above this.

} /* set_typedarray_element */



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 is enough.

for (uint32_t i = 0; i < array_length; i++)
{
ecma_number_t tmp = get_typedarray_element (src_buf,
ecma_object_get_class_name (typedarray_p));
Copy link
Member

Choose a reason for hiding this comment

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

Line alignment.

@tilmannOSG tilmannOSG changed the title [ES2015][TypedArray] add other 8 types [ES2015][TypedArray] Add other 8 types Jan 24, 2017
@jiangzidong jiangzidong mentioned this pull request Jan 24, 2017
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, after minor changes

get_typedarray_element (lit_utf8_byte_t *src, /**< the location in the internal arraybuffer */
lit_magic_string_id_t class_id) /**< class name of the typedarray */
{
#define GET_ELEMENT(type, location, number) \
Copy link
Contributor

Choose a reason for hiding this comment

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

Place of GET_ELEMENT and SET_ELEMENT macros are a bit odd to me. I'd move them out of the functions add descriptions to them too.

}
case LIT_MAGIC_STRING_FLOAT32_ARRAY_UL:
{
*((float *) dst) = (float) value;
Copy link
Contributor

Choose a reason for hiding this comment

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

SET_ELEMENT (float, dst, value);

Copy link
Contributor Author

@jiangzidong jiangzidong Jan 24, 2017

Choose a reason for hiding this comment

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

for float/double type, it cannot use the SET_ELEMENT macro.
SET_ELEMRNT macro will convert the ecma_number_t into a integer type, then convert to the target type. float -> int -> float will lose precision.

Why I have to convert to integer first, because "behavior of float type convert to shorter int type or unsigned int type is undefined by C99"

#if CONFIG_ECMA_NUMBER_TYPE == CONFIG_ECMA_NUMBER_FLOAT64
case LIT_MAGIC_STRING_FLOAT64_ARRAY_UL:
{
*((double *) dst) = (double) value;
Copy link
Contributor

Choose a reason for hiding this comment

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

SET_ELEMENT (double, dst, value);

add Uint8Array, Int16Array Uint16Array, ...

JerryScript-DCO-1.0-Signed-off-by: Zidong Jiang [email protected]
@LaszloLango
Copy link
Contributor

LGTM

@LaszloLango LaszloLango merged commit 0547b31 into jerryscript-project:master Jan 24, 2017
@jiangzidong jiangzidong deleted the typedarray0118 branch February 8, 2017 02:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ecma builtins Related to ECMA built-in routines feature request Requested feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants