Skip to content

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

Merged
merged 1 commit into from
Feb 6, 2018

Conversation

galpeter
Copy link
Contributor

New API functions added:

  • jerry_value_is_typedarray
  • jerry_create_typedarray
  • jerry_create_typedarray_with_arraybuffer_sz
  • jerry_create_typedarray_with_arraybuffer
  • jerry_get_typedarray_type
  • jerry_get_typedarray_length
  • jerry_get_typedarray_buffer

@galpeter galpeter added enhancement An improvement feature request Requested feature api Related to the public API ES2015 Related to ES2015 features labels Jan 12, 2018
**Summary**

Create a jerry_value_t representing an TypedArray object using
an already exisint ArrayBuffer object.
Copy link
Member

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.

**Summary**

Create a jerry_value_t representing an TypedArray object using
an already exisint ArrayBuffer object and by specifying the byteOffset, and length properties.
Copy link
Member

Choose a reason for hiding this comment

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

Ditto.

* 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).
Copy link
Member

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.

@galpeter
Copy link
Contributor Author

@rerobika Thanks for the checks! I've updated the PR.


```c
jerry_value_t
jerry_create_typedarray_with_arraybuffer (jerry_typedarray_class_t class_name,
Copy link
Member

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 ?


jerry_typedarray_class_t klass = jerry_get_typedarray_type (typedarray);

// klass is now JERRY_TYPEDARRAY_UINT32
Copy link
Member

Choose a reason for hiding this comment

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

klass?

Copy link
Contributor Author

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.


```c
bool
jerry_value_is_typedbuffer (const jerry_value_t value)
Copy link
Member

Choose a reason for hiding this comment

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

jerry_value_is_typedarray

```

- `class_name` - type of TypedArray to create
- `length` - length of the new TypedArray
Copy link
Member

Choose a reason for hiding this comment

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

number of items?

- 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
Copy link
Member

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);
Copy link
Member

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?

Copy link
Contributor Author

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.


- `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
Copy link
Member

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?

Copy link
Contributor Author

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

Copy link
Contributor Author

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.

@galpeter galpeter force-pushed the c-api-typedarray branch 4 times, most recently from 69189b6 to 25d89d8 Compare January 29, 2018 16:25
@galpeter
Copy link
Contributor Author

@zherczeg I've updated the PR and did a bit of renaming (typedarray_class -> typedarray_type)

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 after minor update


- `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
Copy link
Member

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.

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've update the PR with the "optional" markings for the parameters.

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.

Still LGTM.


#undef TYPEDARRAY_ENTRY
};
#endif
Copy link
Contributor

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_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
Copy link
Contributor

Choose a reason for hiding this comment

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

jerry_assert_api_available ();

* 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)
Copy link
Contributor

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

#include <stdio.h>

// TODO: remove define if the arraybuffer with external buffer support landed
#define HAS_EXTERNAL_ARRAYBUFFER_API 0
Copy link
Contributor

Choose a reason for hiding this comment

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

#2162 is landed

if (prototype_id == 0)
{
return jerry_throw (ecma_raise_type_error (ECMA_ERR_MSG ("incorrect type for TypedArray.")));
}
Copy link
Contributor

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)

@galpeter
Copy link
Contributor Author

galpeter commented Feb 5, 2018

@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]
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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api Related to the public API enhancement An improvement ES2015 Related to ES2015 features feature request Requested feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants