-
Notifications
You must be signed in to change notification settings - Fork 683
Add TypedArray C API #2165
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
Add TypedArray C API #2165
Conversation
5348e04
to
d104953
Compare
docs/02.API-REFERENCE.md
Outdated
**Summary** | ||
|
||
Create a jerry_value_t representing an TypedArray object using | ||
an already exisint ArrayBuffer 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.
There is a typo in word existing
.
docs/02.API-REFERENCE.md
Outdated
**Summary** | ||
|
||
Create a jerry_value_t representing an TypedArray object using | ||
an already exisint ArrayBuffer object and by specifying the byteOffset, and length properties. |
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.
jerry-core/api/jerry.c
Outdated
* Notes: | ||
* * returns TypeError if an incorrect type (class_name) is specified. | ||
* * byteOffset property will be set to 0. | ||
* * byteLength property will be a muliple of the length parameter (based on the type). |
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 a typo in word multiple
.
d104953
to
cbe9917
Compare
@rerobika Thanks for the checks! I've updated the PR. |
docs/02.API-REFERENCE.md
Outdated
|
||
```c | ||
jerry_value_t | ||
jerry_create_typedarray_with_arraybuffer (jerry_typedarray_class_t class_name, |
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.
jerry_create_typedarray_for_arraybuffer ?
docs/02.API-REFERENCE.md
Outdated
|
||
jerry_typedarray_class_t klass = jerry_get_typedarray_type (typedarray); | ||
|
||
// klass is now JERRY_TYPEDARRAY_UINT32 |
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.
klass?
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.
Yeah, I've used klass
as class
is a keyword in C++. So if someone copies the code into c++ it would still compile without problems.
docs/02.API-REFERENCE.md
Outdated
|
||
```c | ||
bool | ||
jerry_value_is_typedbuffer (const jerry_value_t 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.
jerry_value_is_typedarray
docs/02.API-REFERENCE.md
Outdated
``` | ||
|
||
- `class_name` - type of TypedArray to create | ||
- `length` - length of the new TypedArray |
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.
number of items?
docs/02.API-REFERENCE.md
Outdated
- JERRY_TYPEDARRAY_INT32 - represents the Int32Array TypedArray | ||
- JERRY_TYPEDARRAY_FLOAT32 - represents the Float32Array TypedArray | ||
- JERRY_TYPEDARRAY_FLOAT64 - represents the Float64Array TypedArray | ||
- JERRY_TYPEDARRAY_UNKNOWN - represents an invalid TypedArray |
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 would prefer INVALID over UNKNOWN. The latter says it is a typedarray, but its properties are unknown. This is not true.
```c | ||
jerry_value_t jerry_get_typedarray_buffer (jerry_value_t value, | ||
jerry_length_t *byteOffset, | ||
jerry_length_t *byteLength); |
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.
Can these be NULL? If not, why?
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.
yes, they can be null. Then the developer would not get the underlying array buffer information.
docs/02.API-REFERENCE.md
Outdated
|
||
- `value` - TypedArray to get the ArrayBuffer from | ||
- `byteOffset` - start offset of the ArrayBuffer for the TypedArray | ||
- `byteLength` - number of bytes used from the ArrayBuffer for the TypedArray |
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 byteLength when we use items in other cases?
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.
byteLength
here means the length of the underlying arraybuffer
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.
to be more precise the number of bytes used from the arraybuffer. That is why this is not the same as the item number/count.
69189b6
to
25d89d8
Compare
@zherczeg I've updated the PR and did a bit of renaming (typedarray_class -> typedarray_type) |
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 update
docs/02.API-REFERENCE.md
Outdated
|
||
- `value` - TypedArray to get the ArrayBuffer from | ||
- `byteOffset` - returns the start offset of the ArrayBuffer for the TypedArray | ||
- `byteLength` - returns the number of bytes used from the ArrayBuffer for the TypedArray |
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 mention that these are optional arguments.
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've update the PR with the "optional" markings for the parameters.
25d89d8
to
31c8342
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.
Still LGTM.
jerry-core/api/jerry.c
Outdated
|
||
#undef TYPEDARRAY_ENTRY | ||
}; | ||
#endif |
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 comment:
#endif /* !CONFIG_DISABLE_ES2015_TYPEDARRAY_BUILTIN */
jerry-core/api/jerry.c
Outdated
jerry_length_t byte_offset, /**< offset for the ArrayBuffer */ | ||
jerry_length_t length) /**< number of elements to use from ArrayBuffer */ | ||
{ | ||
#ifndef 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.
jerry_assert_api_available ();
jerry-core/api/jerry.c
Outdated
* TypeError if the object is not a TypedArray. | ||
*/ | ||
jerry_value_t | ||
jerry_get_typedarray_buffer (jerry_value_t value, jerry_length_t *byte_offset, jerry_length_t *byte_length) |
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 comments of function parameters
tests/unit-core/test-typedarray.c
Outdated
#include <stdio.h> | ||
|
||
// TODO: remove define if the arraybuffer with external buffer support landed | ||
#define HAS_EXTERNAL_ARRAYBUFFER_API 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.
#2162 is landed
jerry-core/api/jerry.c
Outdated
if (prototype_id == 0) | ||
{ | ||
return jerry_throw (ecma_raise_type_error (ECMA_ERR_MSG ("incorrect type for TypedArray."))); | ||
} |
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.
lines 3004-3022 is the same as 2949-2968, it would be great if we can merge them (eg.: use a helper)
31c8342
to
05c9ab1
Compare
@LaszloLango Updated the PR based on the requests (and also rebased it) |
New API functions added: - jerry_value_is_typedarray - jerry_create_typedarray - jerry_create_typedarray_for_arraybuffer_sz - jerry_create_typedarray_for_arraybuffer - jerry_get_typedarray_type - jerry_get_typedarray_length - jerry_get_typedarray_buffer JerryScript-DCO-1.0-Signed-off-by: Peter Gal [email protected]
05c9ab1
to
638795c
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
New API functions added: