-
Notifications
You must be signed in to change notification settings - Fork 683
[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
[ES2015][TypedArray] Add other 8 types #1532
Conversation
9875727
to
a120c9e
Compare
@@ -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; |
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.
because now the longest class name is Uint8ClampedArray, and 19 is not enough
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 also update the comment.
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.
Looks good in general. More tests would be great (push, pop, manipulating array elements, etc.)
|
||
return val; | ||
} /* ecma_builtin_uint32array_dispatch_construct */ | ||
|
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.
Missing end of doxygen groups comment:
/**
* @}
* @}
* @}
*/
|
||
return val; | ||
} /* ecma_builtin_uint8array_dispatch_construct */ | ||
|
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
|
||
return val; | ||
} /* ecma_builtin_uint8clampedarray_dispatch_construct */ | ||
|
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
assert(a[0] === 2); | ||
assert(a[1] === 2); | ||
assert(a[2] === 0); | ||
assert(a[3] === 255); |
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.
Missing new line at the end of file.
@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. |
a120c9e
to
c9c815f
Compare
updated |
c9c815f
to
a317755
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.
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); |
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.
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; |
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.
Actually double support is not required for jerry. Perhaps we should make it optional.
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 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); |
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.
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.
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 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; |
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.
Do we need int64 casting here? Not exactly a cheap operation on 32 bit systems.
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.
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; |
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 seems a lot of code duplication, perhaps we should combine them. Code size matters :)
a317755
to
f98a846
Compare
jerry-core/ecma/operations/ecma-typedarray-object.c:304: style(variableScope): The scope of the variable 'tmp' can be reduced. |
f98a846
to
b9b9a6a
Compare
@jiangzidong please fix the doxygen related errors. Run
|
b9b9a6a
to
0715019
Compare
@LaszloLango updated. sorry that i didn't notice the doxygen part. |
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.
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 |
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.
Put a newline before this # if
lit_magic_string_id_t class_id) /**< class name of the typedarray */ | ||
{ | ||
|
||
#define SET_ELEMENT(type, location, 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.
No nned a newline above this.
} /* set_typedarray_element */ | ||
|
||
|
||
|
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 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)); |
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.
Line alignment.
0715019
to
b3b870f
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.
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) \ |
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.
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; |
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.
SET_ELEMENT (float, dst, 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.
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; |
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.
SET_ELEMENT (double, dst, value);
b3b870f
to
cb020a5
Compare
add Uint8Array, Int16Array Uint16Array, ... JerryScript-DCO-1.0-Signed-off-by: Zidong Jiang [email protected]
LGTM |
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]